From 7f1d6bbc335288df1e24e7c8f305c32afe6b050a Mon Sep 17 00:00:00 2001 From: cflip Date: Mon, 8 May 2023 21:25:21 -0600 Subject: Begin rewriting cfws in C --- http.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 http.c (limited to 'http.c') diff --git a/http.c b/http.c new file mode 100644 index 0000000..d6f495d --- /dev/null +++ b/http.c @@ -0,0 +1,37 @@ +#include "http.h" + +#include +#include +#include +#include + +struct http_request http_parse_request(const char *reqstr) +{ + struct http_request req; + char uribuf[CFWS_MAXURI]; + size_t urilen; + + sscanf(reqstr, "GET %s HTTP/1.1", uribuf); + + /* TODO: Support other method types, notably POST */ + req.method = HTTP_METHOD_GET; + + urilen = strlen(uribuf); + req.uri = malloc(urilen + 1); + memcpy(req.uri, uribuf, urilen + 1); + + /* TODO: Parse request headers */ + + return req; +} + +void http_free_request(struct http_request *req) +{ + free(req->uri); +} + +void http_build_response(char **res, enum http_res_code code, const char *msg) +{ + *res = malloc(128); + sprintf(*res, "HTTP/1.1 200 OK\r\n\r\n%s\r\n", msg); +} -- cgit v1.2.3