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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Header struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
type Request struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Version string
|
|
|
|
Body string
|
2024-05-25 12:11:36 +02:00
|
|
|
BodyRaw []byte
|
2024-05-25 11:51:11 +02:00
|
|
|
Headers []Header
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
Headers []Header
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func Respond(conn net.Conn, response Response) {
|
|
|
|
fmt.Fprintf(conn, "%s %d %s%s", response.Version, response.Code.Code, response.Code.Message, HTTPDelimiter)
|
|
|
|
bodySize := 0
|
|
|
|
if response.Body != "" {
|
|
|
|
bodySize = len(response.Body)
|
|
|
|
} else {
|
|
|
|
bodySize = len(response.BodyRaw)
|
|
|
|
}
|
|
|
|
if bodySize > 0 {
|
|
|
|
response.Headers = append(response.Headers, Header{Name: "Content-Length", Value: strconv.Itoa(bodySize)})
|
|
|
|
}
|
|
|
|
for _, header := range response.Headers {
|
|
|
|
fmt.Fprintf(conn, "%s: %s%s", header.Name, header.Value, HTTPDelimiter)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(conn, HTTPDelimiter)
|
|
|
|
if bodySize > 0 {
|
|
|
|
if response.Body != "" {
|
|
|
|
fmt.Fprint(conn, response.Body)
|
|
|
|
} else {
|
|
|
|
conn.Write(response.BodyRaw)
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
request := Request{}
|
|
|
|
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)
|
|
|
|
header := Header{Name: h[0], Value: h[1]}
|
|
|
|
request.Headers = append(request.Headers, header)
|
|
|
|
}
|
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
|
|
|
}
|