Added a check for malformed request paths
This commit is contained in:
@ -69,6 +69,7 @@ PASS: TestRun
|
||||
PASS: TestBadRequest
|
||||
PASS: TestBadRequestHeader
|
||||
PASS: TestBadRequestMethod
|
||||
PASS: TestBadRequestPath
|
||||
PASS: TestBadRequestProtocol
|
||||
PASS: TestConnectionClose
|
||||
PASS: TestEarlyClose
|
||||
|
@ -157,7 +157,14 @@ func (s *server) handleConnection(conn net.Conn) {
|
||||
lastSpace = len(message) - len("\r\n")
|
||||
}
|
||||
|
||||
url = message[space+1 : lastSpace]
|
||||
space += 1
|
||||
|
||||
if space > lastSpace {
|
||||
io.WriteString(conn, "HTTP/1.1 400 Bad Request\r\n\r\n")
|
||||
return
|
||||
}
|
||||
|
||||
url = message[space:lastSpace]
|
||||
|
||||
// Add headers until we meet an empty line
|
||||
for {
|
||||
|
@ -110,6 +110,27 @@ func TestBadRequestMethod(t *testing.T) {
|
||||
s.Run(":8080")
|
||||
}
|
||||
|
||||
func TestBadRequestPath(t *testing.T) {
|
||||
s := web.NewServer()
|
||||
|
||||
go func() {
|
||||
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
|
||||
|
||||
conn, err := net.Dial("tcp", ":8080")
|
||||
assert.Nil(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
_, err = io.WriteString(conn, "GET \n")
|
||||
assert.Nil(t, err)
|
||||
|
||||
response, err := io.ReadAll(conn)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, string(response), "HTTP/1.1 400 Bad Request\r\n\r\n")
|
||||
}()
|
||||
|
||||
s.Run(":8080")
|
||||
}
|
||||
|
||||
func TestBadRequestProtocol(t *testing.T) {
|
||||
s := web.NewServer()
|
||||
|
||||
|
Reference in New Issue
Block a user