add http parser

This commit is contained in:
Simon 2023-02-09 13:35:05 +01:00
parent 7e8d7e1b11
commit 2e087254c9
2 changed files with 41 additions and 0 deletions

View File

@ -3,9 +3,46 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
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)

4
backend/utils.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef UTILS_H
#define UTILS_H
#endif