summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-06-08 14:49:35 -0600
committercflip <cflip@cflip.net>2023-06-08 14:49:35 -0600
commit8724eb379bcda51e7a9ecaaa6698a699ac042621 (patch)
tree2605288870ee94365c805b2f7582ff80d66ccbb2
parenteb7a9ca573c24f09d3cc2d3485af848c914aedac (diff)
Parse HTTP POST requests and request body
-rw-r--r--http.c27
-rw-r--r--http.h4
2 files changed, 24 insertions, 7 deletions
diff --git a/http.c b/http.c
index 01a80d4..3521e32 100644
--- a/http.c
+++ b/http.c
@@ -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[] = {
diff --git a/http.h b/http.h
index 90bd4f3..caeb28d 100644
--- a/http.h
+++ b/http.h
@@ -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 *);