Skip to content

Commit

Permalink
Implement echo
Browse files Browse the repository at this point in the history
  • Loading branch information
Anwar Ziani committed Oct 20, 2023
1 parent d3eb706 commit 1fadccb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ func handleConn(conn net.Conn) {
if !ok {
log.Println("Invalid http request")
}

fmt.Println(path)
// send response
response := "HTTP/1.1 404 Not Found\r\n\r\n"
if path == "/" {
response = "HTTP/1.1 200 OK\r\n\r\n"
} else if strings.HasPrefix(strings.ToLower(path), "/echo/") {
_, content, _ := strings.Cut(strings.TrimPrefix(path, "/"), "/")
response = strings.Join([]string{
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
fmt.Sprintf("Content-Length: %d\r\n", len(content)),
content,
}, "\r\n")
}
_, err = fmt.Fprintf(conn, "%s", response)
if err != nil {
Expand Down

0 comments on commit 1fadccb

Please sign in to comment.