Added more tests
This commit is contained in:
57
Server.go
57
Server.go
@ -128,39 +128,38 @@ func (s *server) handleConnection(conn net.Conn) {
|
||||
defer s.contextPool.Put(ctx)
|
||||
|
||||
for {
|
||||
// Search for a line containing HTTP method and url
|
||||
for {
|
||||
message, err := ctx.reader.ReadString('\n')
|
||||
// Read the HTTP request line
|
||||
message, err := ctx.reader.ReadString('\n')
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
space := strings.IndexByte(message, ' ')
|
||||
|
||||
if space <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
method = message[:space]
|
||||
|
||||
if !isRequestMethod(method) {
|
||||
continue
|
||||
}
|
||||
|
||||
lastSpace := strings.LastIndexByte(message, ' ')
|
||||
|
||||
if lastSpace == -1 {
|
||||
lastSpace = len(message)
|
||||
}
|
||||
|
||||
url = message[space+1 : lastSpace]
|
||||
break
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
space := strings.IndexByte(message, ' ')
|
||||
|
||||
if space <= 0 {
|
||||
fmt.Fprint(conn, "HTTP/1.1 400 Bad Request\r\n\r\n")
|
||||
return
|
||||
}
|
||||
|
||||
method = message[:space]
|
||||
|
||||
if !isRequestMethod(method) {
|
||||
fmt.Fprint(conn, "HTTP/1.1 400 Bad Request\r\n\r\n")
|
||||
return
|
||||
}
|
||||
|
||||
lastSpace := strings.LastIndexByte(message, ' ')
|
||||
|
||||
if lastSpace == space {
|
||||
lastSpace = len(message) - len("\r\n")
|
||||
}
|
||||
|
||||
url = message[space+1 : lastSpace]
|
||||
|
||||
// Add headers until we meet an empty line
|
||||
for {
|
||||
message, err := ctx.reader.ReadString('\n')
|
||||
message, err = ctx.reader.ReadString('\n')
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
@ -210,7 +209,7 @@ func (s *server) handleRequest(ctx *context, method string, url string, writer i
|
||||
s.errorHandler(ctx, err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "HTTP/1.1 %d %s\r\nContent-Length: %d\r\n%s\r\n%s", ctx.status, "OK", len(ctx.response.body), ctx.response.headerText(), ctx.response.body)
|
||||
fmt.Fprintf(writer, "HTTP/1.1 %d\r\nContent-Length: %d\r\n%s\r\n%s", ctx.status, len(ctx.response.body), ctx.response.headerText(), ctx.response.body)
|
||||
}
|
||||
|
||||
// newContext allocates a new context with the default state.
|
||||
|
Reference in New Issue
Block a user