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

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -43,12 +44,18 @@ type Response struct {
Code HttpCode Code HttpCode
Version string Version string
Body string Body string
BodyRaw []byte
Headers []Header Headers []Header
} }
func Respond(conn net.Conn, response Response) { func Respond(conn net.Conn, response Response) {
fmt.Fprintf(conn, "%s %d %s%s", response.Version, response.Code.Code, response.Code.Message, HTTPDelimiter) fmt.Fprintf(conn, "%s %d %s%s", response.Version, response.Code.Code, response.Code.Message, HTTPDelimiter)
bodySize := len(response.Body) bodySize := 0
if response.Body != "" {
bodySize = len(response.Body)
} else {
bodySize = len(response.BodyRaw)
}
if bodySize > 0 { if bodySize > 0 {
response.Headers = append(response.Headers, Header{Name: "Content-Length", Value: strconv.Itoa(bodySize)}) response.Headers = append(response.Headers, Header{Name: "Content-Length", Value: strconv.Itoa(bodySize)})
} }
@ -58,7 +65,11 @@ func Respond(conn net.Conn, response Response) {
fmt.Fprint(conn, HTTPDelimiter) fmt.Fprint(conn, HTTPDelimiter)
if bodySize > 0 { if bodySize > 0 {
if response.Body != "" {
fmt.Fprint(conn, response.Body) fmt.Fprint(conn, response.Body)
} else {
conn.Write(response.BodyRaw)
}
} }
// fmt.Fprintf(conn, "HTTP/1.1 %d %s%s%s", response.Code, response.Message, HTTPDelimiter, HTTPDelimiter) // fmt.Fprintf(conn, "HTTP/1.1 %d %s%s%s", response.Code, response.Message, HTTPDelimiter, HTTPDelimiter)
@ -126,9 +137,34 @@ func handleConnection(conn net.Conn) {
Respond(conn, Response{Version: request.Version, Code: BadRequest}) Respond(conn, Response{Version: request.Version, Code: BadRequest})
} }
re := regexp.MustCompile(`^/echo/([A-Za-z]+)$`) reFiles := regexp.MustCompile(`^/files/([A-Za-z0-9_\-.]+)$`)
matches := re.FindStringSubmatch(request.Path) if matches := reFiles.FindStringSubmatch(request.Path); len(matches) > 0 {
if len(matches) > 0 {
if len(os.Args) != 3 {
log.Fatal("Not enough args")
}
dir, err := filepath.Abs(os.Args[2])
if err != nil {
log.Fatal(err)
}
filename := filepath.Join(dir, matches[1])
// fmt.Println(file)
file, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
Respond(conn, Response{
Version: request.Version,
Code: OK,
BodyRaw: file,
Headers: []Header{{Name: "Content-Type", Value: "application/octet-stream"}},
})
return
}
reEcho := regexp.MustCompile(`^/echo/([A-Za-z]+)$`)
if matches := reEcho.FindStringSubmatch(request.Path); len(matches) > 0 {
Respond(conn, Response{ Respond(conn, Response{
Version: request.Version, Version: request.Version,
Code: OK, Code: OK,