structuration du header

This commit is contained in:
2025-12-28 19:35:48 +01:00
parent 31e4273903
commit 2b2470f65a
2 changed files with 41 additions and 15 deletions

View File

@@ -6,9 +6,34 @@ import (
"strings" "strings"
) )
func ReadRequest(reader *bufio.Reader) ([]string, string, error) { type HTTPHeader struct {
var message []byte Version string
Method string
Path string
Lines map[string] string
}
func ParseHeader(content string) (HTTPHeader, error) {
lines := strings.Split(content, "\r\n")
startingLine := strings.Split(lines[0], " ")
Header := HTTPHeader{
Method: startingLine[0],
Path: startingLine[1],
Version: startingLine[2],
Lines: make(map[string]string),
}
for _, line := range lines[1:] {
if line == "" {
break
}
args := strings.Split(line, ": ")
Header.Lines[args[0]] = args[1]
}
return Header, nil
}
func ReadRequest(reader *bufio.Reader) (HTTPHeader, string, error) {
var message []byte
buffer := make([]byte, 8) buffer := make([]byte, 8)
for { for {
@@ -27,19 +52,11 @@ func ReadRequest(reader *bufio.Reader) ([]string, string, error) {
break break
} }
} }
parts := strings.Split(string(message), "\r\n\r\n") header, _ := ParseHeader(string(message))
body := strings.Split(string(message), "\r\n\r\n")[1]
headerLines := strings.Split(parts[0], "\r\n") return header, body, nil
body := parts[1]
if len(parts) != 2 {
return nil, "", fmt.Errorf("Wrong")
}
return headerLines, body, nil
} }
func ParseURL(path string) (string, map[string]string, error) { func ParseURL(path string) (string, map[string]string, error) {
//Décomposition du chemin et des paramètres //Décomposition du chemin et des paramètres
url := strings.Split(path, "?") url := strings.Split(path, "?")
@@ -64,3 +81,12 @@ func ParseURL(path string) (string, map[string]string, error) {
} }
return url[0], parameters, nil return url[0], parameters, nil
} }
func CheckRequestValidity(header HTTPHeader) bool {
return true
}

View File

@@ -18,7 +18,7 @@ func (router *Router) HandleClient(conn net.Conn) error {
return err return err
} }
response := buildResponse(router.Handle(strings.Split(requestHeader[0], " ")[0], strings.Split(requestHeader[0], " ")[1])) response := buildResponse(router.Handle(requestHeader.Method, requestHeader.Path))
conn.Write([]byte(response)) conn.Write([]byte(response))