feat: add step to receive request body in buffer
This commit is contained in:
parent
ff0795bb55
commit
16103f9b24
10 changed files with 113 additions and 12 deletions
|
|
@ -26,6 +26,11 @@ void http_loop_ctx_reset(http_loop_ctx *ctx) {
|
|||
ctx->route = NULL;
|
||||
ctx->current_step = 0;
|
||||
|
||||
if (ctx->req.body != NULL) {
|
||||
free((void *)ctx->req.body);
|
||||
ctx->req.body = NULL;
|
||||
}
|
||||
|
||||
if (ctx->res.head != NULL) {
|
||||
free((void *)ctx->res.head);
|
||||
ctx->res.head = NULL;
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ http_parse_error http_loop_parse_request(event_loop_conn *conn) {
|
|||
return http_parse_error_incomplete;
|
||||
}
|
||||
|
||||
req->num_headers = num_headers;
|
||||
req->len = res;
|
||||
|
||||
// Try to parse the method type
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
#include "http_loop.h"
|
||||
#include "log.h"
|
||||
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
|
||||
static const char *http_response_format = "HTTP/1.1 %i %s\n"
|
||||
"Server: lander/" LANDER_VERSION "\n"
|
||||
"Content-Length: %lu\n";
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
|
@ -31,3 +32,68 @@ void http_loop_res_add_header(http_loop_ctx *ctx, http_header type,
|
|||
|
||||
ctx->res.header_count++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Converts a string to a number, returning true if the string contained a valid
|
||||
* positive number.
|
||||
*/
|
||||
static bool string_to_num(size_t *res, const char *s, size_t len) {
|
||||
*res = 0;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
int val = s[i] - '0';
|
||||
|
||||
if (val < 0 || val > 9) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*res += val * (int)pow(10, (len - 1) - i);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool http_loop_step_body_to_buf(event_loop_conn *conn) {
|
||||
http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
if (ctx->req.body_len == 0) {
|
||||
for (size_t i = 0; i < ctx->req.num_headers; i++) {
|
||||
struct phr_header *header = &ctx->req.headers[i];
|
||||
|
||||
if (strncmp(header->name, "Content-Length", header->name_len) == 0) {
|
||||
// If the content length header is present but contains an invalid
|
||||
// number, we return a bad request error
|
||||
if (!string_to_num(&ctx->req.body_len, header->value,
|
||||
header->value_len)) {
|
||||
ctx->res.status = http_bad_request;
|
||||
conn->state = event_loop_conn_state_res;
|
||||
|
||||
return true;
|
||||
}
|
||||
// The content length was actually 0, so we can instantly return here
|
||||
else if (ctx->req.body_len == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A zero here means there's no content length header
|
||||
if (ctx->req.body_len == 0) {
|
||||
ctx->res.status = http_length_required;
|
||||
conn->state = event_loop_conn_state_res;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ctx->req.body = malloc(ctx->req.body_len * sizeof(uint8_t));
|
||||
ctx->req.body_received = 0;
|
||||
}
|
||||
|
||||
size_t bytes_to_copy = MIN(conn->rbuf_size - conn->rbuf_read,
|
||||
ctx->req.body_len - ctx->req.body_received);
|
||||
memcpy(&ctx->req.body[ctx->req.body_received], &conn->rbuf[conn->rbuf_read],
|
||||
bytes_to_copy);
|
||||
ctx->req.body_received += bytes_to_copy;
|
||||
|
||||
return ctx->req.body_received == ctx->req.body_len;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,11 +47,16 @@ bool lander_get_entry(event_loop_conn *conn) {
|
|||
return true;
|
||||
}
|
||||
|
||||
http_route lander_routes[] = {{.type = http_route_literal,
|
||||
.method = http_get,
|
||||
.path = "/",
|
||||
.steps = {lander_get_index, NULL}},
|
||||
{.type = http_route_regex,
|
||||
.method = http_get,
|
||||
.path = "^/\\([^/]\\+\\)$",
|
||||
.steps = {lander_get_entry, NULL}}};
|
||||
http_route lander_routes[] = {
|
||||
{.type = http_route_literal,
|
||||
.method = http_get,
|
||||
.path = "/",
|
||||
.steps = {lander_get_index, NULL}},
|
||||
{.type = http_route_regex,
|
||||
.method = http_get,
|
||||
.path = "^/\\([^/]\\+\\)$",
|
||||
.steps = {lander_get_entry, NULL}},
|
||||
{.type = http_route_literal,
|
||||
.method = http_post,
|
||||
.path = "/s/",
|
||||
.steps = {http_loop_step_body_to_buf, lander_post_redirect, NULL}}};
|
||||
|
|
|
|||
12
src/lander/lander_post.c
Normal file
12
src/lander/lander_post.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "lander.h"
|
||||
#include "log.h"
|
||||
|
||||
bool lander_post_redirect(event_loop_conn *conn) {
|
||||
http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
info("%.*s", ctx->req.body_len, ctx->req.body);
|
||||
|
||||
conn->state = event_loop_conn_state_res;
|
||||
|
||||
return true;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue