diff options
author | cflip <cflip@cflip.net> | 2023-06-08 14:49:35 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-06-08 14:49:35 -0600 |
commit | 8724eb379bcda51e7a9ecaaa6698a699ac042621 (patch) | |
tree | 2605288870ee94365c805b2f7582ff80d66ccbb2 | |
parent | eb7a9ca573c24f09d3cc2d3485af848c914aedac (diff) |
Parse HTTP POST requests and request body
-rw-r--r-- | http.c | 27 | ||||
-rw-r--r-- | http.h | 4 |
2 files changed, 24 insertions, 7 deletions
@@ -16,16 +16,19 @@ struct http_request http_parse_request(const char *reqstr) req.method = HTTP_METHOD_UNKNOWN; req.uri = NULL; req.query_str = NULL; + req.body = NULL; - /* TODO: Support other request methods such as POST */ - if (strncmp(reqstr, "GET ", 4) != 0) { + if (strncmp(reqstr, "GET ", 4) == 0) { + req.method = HTTP_METHOD_GET; + counter = reqstr + 4; + } else if (strncmp(reqstr, "POST ", 5) == 0) { + req.method = HTTP_METHOD_POST; + counter = reqstr + 5; + } else { fprintf(stderr, "Unhandled request type: %s\n", reqstr); return req; } - req.method = HTTP_METHOD_GET; - - counter = reqstr + 4; while (*counter != ' ' && *counter != '?' && *counter != 0 && urilen < CFWS_MAXURI) { uribuf[urilen++] = *counter; @@ -52,7 +55,17 @@ struct http_request http_parse_request(const char *reqstr) req.query_str[query_len] = '\0'; } - /* TODO: Parse request headers */ + /* TODO: Parse request headers. For now they are just ignored. */ + counter = strstr(reqstr, "\r\n\r\n"); + if (req.method == HTTP_METHOD_POST) { + size_t body_len; + counter += 4; + + body_len = strlen(counter); + req.body = malloc(body_len + 1); + memcpy(req.body, counter, body_len); + req.body[body_len] = '\0'; + } return req; } @@ -62,6 +75,8 @@ void http_free_request(struct http_request *req) free(req->uri); if (req->query_str) free(req->query_str); + if (req->body) + free(req->body); } static const char *response_msg[] = { @@ -8,7 +8,8 @@ enum http_req_method { HTTP_METHOD_UNKNOWN, - HTTP_METHOD_GET + HTTP_METHOD_GET, + HTTP_METHOD_POST }; enum http_res_code { @@ -22,6 +23,7 @@ struct http_request { int method; char *uri; char *query_str; + char *body; }; struct http_request http_parse_request(const char *); |