I’m stuck on Stage #FS3.
I’ve implemented the functionality and it works on my machine but fails on some test.
Here are my logs:
remote: [tester::#FS3] Running tests for Stage #FS3 (Read header)
remote: [tester::#FS3] $ ./your_program.sh
remote: [tester::#FS3] $ curl -v http://localhost:4221/user-agent -H "User-Agent: apple/grape-mango"
remote: [your_program] panic: runtime error: index out of range [1] with length 1
remote: [your_program]
remote: [your_program] goroutine 1 [running]:
remote: [your_program] main.main()
remote: [your_program] /app/app/server.go:44 +0x5aa
remote: [tester::#FS3] Failed to read response:
remote: [tester::#FS3] Received: "" (no content received)
remote: [tester::#FS3] ^ error
remote: [tester::#FS3] Error: Expected: HTTP-version, Received: ""
remote: [tester::#FS3] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
And here’s a snippet of my code:
package main
import (
"fmt"
"net"
"os"
"strings"
)
func main() {
l, err := net.Listen("tcp", "0.0.0.0:4221")
if err != nil {
fmt.Println("Failed to bind to port 4221")
os.Exit(1)
}
defer l.Close()
conn, er := l.Accept()
if er != nil {
fmt.Println("Error accepting connection: ", err.Error())
os.Exit(1)
}
buf := make([]byte, 1024)
_, err = conn.Read(buf)
if err != nil {
fmt.Println("Error reading connection: ", err.Error())
os.Exit(1)
}
req := string(buf)
lines := strings.Split(req, "\r\n")
path := strings.Split(lines[0], " ")[1]
tokens := strings.Split(path, "/")
res := ""
if path == "/" {
res = "HTTP/1.1 200 OK\r\n\r\n"
} else if len(tokens) > 1 && tokens[1] == "echo" {
res = fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s", len(tokens[2]), tokens[2])
} else if len(tokens) > 1 && strings.ToLower(tokens[1]) == "user-agent" {
user_agent := strings.Split(lines[3], " ")[1]
res = fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s", len(user_agent), user_agent)
} else {
res = "HTTP/1.1 404 Not Found\r\n\r\n"
}
conn.Write([]byte(res))
}