codecrafters-http-server-go/app/http.go

137 lines
2.5 KiB
Go
Raw Normal View History

2024-05-25 11:51:11 +02:00
package main
import (
"fmt"
2024-05-25 12:31:01 +02:00
"log"
2024-05-25 11:51:11 +02:00
"net"
"regexp"
"strconv"
2024-05-25 12:31:01 +02:00
"strings"
2024-05-25 11:51:11 +02:00
)
const (
HTTPDelimiter = "\r\n"
)
2024-05-25 18:27:53 +02:00
// type Header struct {
// Name string
// Value string
// }
2024-05-25 11:51:11 +02:00
type Request struct {
Method string
Path string
Version string
Body string
2024-05-25 12:11:36 +02:00
BodyRaw []byte
2024-05-25 18:27:53 +02:00
Headers map[string]string
2024-05-25 11:51:11 +02:00
}
type HttpCode struct {
Code uint
Message string
}
var (
BadRequest = HttpCode{Code: 400, Message: "Bad Response"}
NotFound = HttpCode{Code: 404, Message: "Not Found"}
OK = HttpCode{Code: 200, Message: "OK"}
2024-05-25 12:12:32 +02:00
Created = HttpCode{Code: 201, Message: "Created"}
2024-05-25 11:51:11 +02:00
)
type Response struct {
Code HttpCode
Version string
Body string
BodyRaw []byte
2024-05-25 18:27:53 +02:00
Headers map[string]string
2024-05-25 11:51:11 +02:00
}
type StringRoute struct {
path string
method string
handler func(req Request) Response
}
type RegexRoute struct {
regex *regexp.Regexp
method string
handler func(req Request, matches []string) Response
}
type Routes struct {
stringRoutes []StringRoute
regexpRoutes []RegexRoute
}
2024-05-25 18:27:53 +02:00
func Respond(conn net.Conn, req Request, res Response) {
2024-05-26 01:03:46 +02:00
if res.Headers == nil {
res.Headers = make(map[string]string)
}
2024-05-26 01:07:40 +02:00
if req.Headers["Accept-Encoding"] == "gzip" {
res.Headers["Content-Encoding"] = "gzip"
}
2024-05-26 01:03:46 +02:00
2024-05-25 18:27:53 +02:00
fmt.Fprintf(conn, "%s %d %s%s", res.Version, res.Code.Code, res.Code.Message, HTTPDelimiter)
2024-05-25 11:51:11 +02:00
bodySize := 0
2024-05-25 18:27:53 +02:00
if res.Body != "" {
bodySize = len(res.Body)
2024-05-25 11:51:11 +02:00
} else {
2024-05-25 18:27:53 +02:00
bodySize = len(res.BodyRaw)
2024-05-25 11:51:11 +02:00
}
if bodySize > 0 {
2024-05-25 18:27:53 +02:00
res.Headers["Content-Length"] = strconv.Itoa(bodySize)
2024-05-25 11:51:11 +02:00
}
2024-05-25 18:27:53 +02:00
for header, value := range res.Headers {
fmt.Fprintf(conn, "%s: %s%s", header, value, HTTPDelimiter)
2024-05-25 11:51:11 +02:00
}
fmt.Fprint(conn, HTTPDelimiter)
if bodySize > 0 {
2024-05-25 18:27:53 +02:00
if res.Body != "" {
fmt.Fprint(conn, res.Body)
2024-05-25 11:51:11 +02:00
} else {
2024-05-25 18:27:53 +02:00
conn.Write(res.BodyRaw)
2024-05-25 11:51:11 +02:00
}
}
2024-05-25 12:31:01 +02:00
}
func parseRequest(conn net.Conn) (Request, bool) {
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
log.Fatal(err)
}
contents := string(buffer[:n])
parts := strings.Split(contents, HTTPDelimiter)
2024-05-25 18:27:53 +02:00
request := Request{Headers: map[string]string{}}
2024-05-25 12:31:01 +02:00
isBody := false
for i, part := range parts {
if i == 0 {
head := strings.Split(part, " ")
if len(head) != 3 {
return Request{}, false
}
request.Method = head[0]
request.Path = head[1]
request.Version = head[2]
continue
}
if isBody {
request.Body = part
break
}
// Headers
if part == "" {
isBody = true
continue
}
h := strings.SplitN(part, ": ", 2)
2024-05-25 18:27:53 +02:00
request.Headers[h[0]] = h[1]
2024-05-25 12:31:01 +02:00
}
2024-05-25 11:51:11 +02:00
2024-05-25 12:31:01 +02:00
return request, true
2024-05-25 11:51:11 +02:00
}