From a30ee9d24903d1476ace1f5521194563e10ca243 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 9 Feb 2023 15:55:57 +0100 Subject: [PATCH] backend: add parser char skipper --- backend/http.c | 65 +++++++++++++++++++++++--------- backend/http_request_example.txt | 28 ++++++++++++++ 2 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 backend/http_request_example.txt diff --git a/backend/http.c b/backend/http.c index 5ad2e5b..d97bfaa 100644 --- a/backend/http.c +++ b/backend/http.c @@ -1,26 +1,55 @@ #include "http.h" +#include +#include #include +#include + +typedef struct { + const char* message; + size_t message_size; + size_t i; +} HttpRequestHeaderParser; + +HttpMethod parse_method(HttpRequestHeaderParser* parser) +{ + if (parser->i + 3 < parser->message_size && strncmp(&parser->message[0], "GET", 3)) { + parser->i += 3; + return HttpMethodGet; + } else if (parser->i + 4 < parser->message_size && strncmp(&parser->message[0], "POST", 4)) { + parser->i += 4; + return HttpMethodPost; + } else { + printf("error: http parser: failed to parse http method\n"); + exit(1); + } +} + +void skip_char(HttpRequestHeaderParser* parser, char value) +{ + if (parser->message[parser->i] != value) { + printf("error: http parser: unexpected character, expected ' '\n"); + exit(1); + } + parser->i += 1; + if (parser->i >= parser->message_size) { + printf("error: http parser: unexpected end\n"); + exit(1); + } +} + +typedef struct { + size_t begin, length; +} PathSpan; HttpRequestHeader parse_http_request_header(const char* message, size_t message_size) { - size_t i = 0; - - // parse method - HttpMethod method; - if (i + 3 < message_size && strncmp(&message[0], "GET", 3)) { - method = HttpMethodGet; - i += 3; - } else if (i + 4 < message_size && strncmp(&message[0], "POST", 4)) { - method = HttpMethodPost; - i += 4; - } else { - printf("error: header parse fail #1\n"); - exit(1); - } - - // skip space - i += 1; - if (i >= message_size) { } + HttpRequestHeaderParser parser = (HttpRequestHeaderParser) { + .message = message, + .message_size = message_size, + .i = 0, + }; + HttpMethod method = parse_method(&parser); + skip_char(&parser, ' '); return (HttpRequestHeader) { 0 }; } diff --git a/backend/http_request_example.txt b/backend/http_request_example.txt new file mode 100644 index 0000000..e1dd00c --- /dev/null +++ b/backend/http_request_example.txt @@ -0,0 +1,28 @@ +starting server... +listening on port 8000 +waiting for client... +client connected +recieved: +GET / HTTP/1.1 +Host: localhost:8000 +User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109 +.0 +Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/w +ebp,*/*;q=0.8 +Accept-Language: en-US,en;q=0.5 +Accept-Encoding: gzip, deflate, br +Connection: keep-alive +Upgrade-Insecure-Requests: 1 +Sec-Fetch-Dest: document +Sec-Fetch-Mode: navigate +Sec-Fetch-Site: none +Sec-Fetch-User: ?1 + + +disconnecting client +error: could not recieve +waiting for client... +client connected +client disconnected +waiting for client... +