From 2e087254c9b13e60df0877c1e962b2a72c7c6705 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 9 Feb 2023 13:35:05 +0100 Subject: [PATCH] add http parser --- backend/main.c | 37 +++++++++++++++++++++++++++++++++++++ backend/utils.h | 4 ++++ 2 files changed, 41 insertions(+) create mode 100644 backend/utils.h diff --git a/backend/main.c b/backend/main.c index 96c0d26..99e57ac 100644 --- a/backend/main.c +++ b/backend/main.c @@ -3,9 +3,46 @@ #include #include #include +#include +#include #include #include +typedef enum { + HttpMethodGet, + HttpMethodPost, +} HttpMethod; + +typedef struct { + HttpMethod method; + size_t path_index, path_length; + size_t content_length; +} HttpRequestHeader; + +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) { } + + return (HttpRequestHeader) { 0 }; +} + const uint16_t port = 8000; int main(void) diff --git a/backend/utils.h b/backend/utils.h new file mode 100644 index 0000000..189356e --- /dev/null +++ b/backend/utils.h @@ -0,0 +1,4 @@ +#ifndef UTILS_H +#define UTILS_H + +#endif