Skip to content

Commit

Permalink
Respond with 404
Browse files Browse the repository at this point in the history
  • Loading branch information
Anwar Ziani committed Oct 20, 2023
1 parent 37a3ddb commit 8a484f6
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net"
"strings"
)

func main() {
Expand All @@ -20,19 +21,35 @@ func main() {
log.Fatalf("Error accepting connection: %s", err.Error())
}

handleConn(conn)
}

func handleConn(conn net.Conn) {
defer conn.Close()

// read data from connection
buffer := make([]byte, 1024) // 1024 is a good start
_, err = conn.Read(buffer)
_, err := conn.Read(buffer)
if err != nil {
log.Printf("Error reading data: %v", err)
}

_, rest, ok := strings.Cut(string(buffer), " ")
if !ok {
log.Println("Invalid http request")
}
path, _, ok := strings.Cut(rest, " ")
if !ok {
log.Println("Invalid http request")
}

// send response
_, err = fmt.Fprintf(conn, "%s", "HTTP/1.1 200 OK\r\n\r\n")
response := "HTTP/1.1 400 Not Found\r\n\r\n"
if path == "/" {
response = "HTTP/1.1 200 OK\r\n\r\n"
}
_, err = fmt.Fprintf(conn, "%s", response)
if err != nil {
log.Fatalf("Failed to respond %v", err)
}

conn.Close()

}

0 comments on commit 8a484f6

Please sign in to comment.