backend: extracted http

This commit is contained in:
Simon 2023-02-09 13:50:26 +01:00
parent 3c6bd4dfc3
commit d3859091a2
6 changed files with 71 additions and 62 deletions

View File

@ -1,19 +0,0 @@
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion
HEADERS = $(wildcard *.h)
all: compile_flags.txt server
server: main.o linux.o
gcc $^ -o $@
%.o: %.c $(HEADERS)
gcc $< -c -o $@ $(CFLAGS)
clean:
rm -rf *.o server client
compile_flags.txt:
echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt

View File

@ -1,12 +1,19 @@
OBJS=main.obj windows.obj
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion
all: $(OBJS)
link /out:server.exe $(OBJS) WS2_32.lib
HEADERS = $(wildcard *.h)
.obj:
cl $*.c
all: compile_flags.txt server
server: main.o linux.o http.o
gcc $^ -o $@
%.o: %.c $(HEADERS)
gcc $< -c -o $@ $(CFLAGS)
clean:
del *.obj server.exe
rm -rf *.o server client
compile_flags.txt:
echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt

12
backend/NMakefile Normal file
View File

@ -0,0 +1,12 @@
OBJS=main.obj windows.obj
all: $(OBJS)
link /out:server.exe $(OBJS) WS2_32.lib
.obj:
cl $*.c
clean:
del *.obj server.exe

26
backend/http.c Normal file
View File

@ -0,0 +1,26 @@
#include "http.h"
#include <stdlib.h>
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 };
}

19
backend/http.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef HTTP_H
#define HTTP_H
#include <stdlib.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);
#endif

View File

@ -1,46 +1,10 @@
#include "native.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "native.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)