user agent

This commit is contained in:
Niccolo Borgioli 2024-05-25 00:29:26 +02:00
parent 87aa98c68e
commit 69bfabc3ac
No known key found for this signature in database
GPG Key ID: 4897ACD13A65977C

View File

@ -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
} }