commit cab7aef94d3938ff4fb70b4e62e4ecf65557c881
parent 983401f647a256d9432734ad1ff8b3ebf97dc80b
Author: Francesco Saccone <francesco@francescosaccone.com>
Date: Sun, 30 Mar 2025 19:18:15 +0200
feat: add parse_http_request function
Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>
Diffstat:
M | http.c | | | 34 | ++++++++++++++++++++++++++++++++++ |
M | http.h | | | 5 | ++++- |
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/http.c b/http.c
@@ -1 +1,35 @@
#include "http.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct http_request *
+parse_http_request(char *request) {
+ struct http_request *result = malloc(sizeof(struct http_request));
+
+ if (result == NULL) {
+ return NULL;
+ }
+
+ char *method_string = strtok(request, " "),
+ *path_string = strtok(NULL, " ");
+
+ if (method_string != NULL) {
+ if (strcmp(method_string, "GET") == 0) {
+ result->method = GET;
+ } else if (strcmp(method_string, "HEAD") == 0) {
+ result->method = HEAD;
+ } else {
+ /* only GET and HEAD are supported */
+ free(result);
+ return NULL;
+ }
+ }
+
+ if (path_string != NULL) {
+ strncpy(result->path, path_string, HTTP_REQUEST_PATH_MAX_LENGTH - 1);
+ result->path[HTTP_REQUEST_PATH_MAX_LENGTH - 1] = '\0';
+ }
+
+ return result;
+}
diff --git a/http.h b/http.h
@@ -58,7 +58,10 @@ struct http_response {
enum http_response_status status;
const char *content_type;
char *body;
- size_t body_length;
+ unsigned int body_length;
};
+struct http_request *
+parse_http_request(char *request);
+
#endif