mirror of
https://github.com/cupcakearmy/codecrafters-http-server-go.git
synced 2024-12-22 08:06:27 +00:00
user agent
This commit is contained in:
parent
87aa98c68e
commit
69bfabc3ac
@ -110,6 +110,22 @@ func handleConnection(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if request.Path == "/user-agent" {
|
||||||
|
for _, header := range request.Headers {
|
||||||
|
if header.Name != "User-Agent" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
Respond(conn, Response{
|
||||||
|
Version: request.Version,
|
||||||
|
Code: OK,
|
||||||
|
Body: header.Value,
|
||||||
|
Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Respond(conn, Response{Version: request.Version, Code: BadRequest})
|
||||||
|
}
|
||||||
|
|
||||||
re := regexp.MustCompile(`^/echo/([A-Za-z]+)$`)
|
re := regexp.MustCompile(`^/echo/([A-Za-z]+)$`)
|
||||||
matches := re.FindStringSubmatch(request.Path)
|
matches := re.FindStringSubmatch(request.Path)
|
||||||
if len(matches) > 0 {
|
if len(matches) > 0 {
|
||||||
@ -117,7 +133,7 @@ func handleConnection(conn net.Conn) {
|
|||||||
Version: request.Version,
|
Version: request.Version,
|
||||||
Code: OK,
|
Code: OK,
|
||||||
Body: matches[1],
|
Body: matches[1],
|
||||||
Headers: []Header{Header{Name: "Content-Type", Value: "text/plain"}},
|
Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user