feat(lander): implement redirect posting using lnm

This commit is contained in:
Jef Roosens 2023-12-05 19:12:19 +01:00
parent ad243ea9f5
commit f3da5c78ef
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
10 changed files with 123 additions and 81 deletions

View file

@ -128,4 +128,6 @@ typedef struct lnm_http_loop_ctx {
void *c;
} lnm_http_loop_ctx;
lnm_http_step_err lnm_http_loop_step_body_to_buf(lnm_http_conn *conn);
#endif

View file

@ -35,8 +35,10 @@ typedef struct lnm_http_req {
} headers;
struct {
uint64_t expected_len;
uint64_t len;
char *buf;
bool owned;
} body;
uint64_t content_length;
} lnm_http_req;
typedef enum lnm_http_parse_err {

View file

@ -99,7 +99,7 @@ void lnm_http_loop_process_parse_headers(lnm_http_conn *conn) {
size_t value_len;
if (lnm_http_req_header_get(&value, &value_len, req,
lnm_http_header_content_length) == lnm_err_ok) {
req->content_length = lnm_atoi(value, value_len);
req->body.expected_len = lnm_atoi(value, value_len);
}
ctx->state = lnm_http_loop_state_steps;
@ -149,8 +149,8 @@ void lnm_http_loop_state_process_add_headers(lnm_http_conn *conn) {
}
sprintf(buf, "%lu", res->body.len);
lnm_http_res_add_header_len(res, lnm_http_header_content_length, buf,
digits, true);
lnm_http_res_add_header_len(res, lnm_http_header_content_length, buf, digits,
true);
ctx->state = lnm_http_loop_state_write_status_line;
}

View file

@ -0,0 +1,24 @@
#include <string.h>
#include "lnm/http/loop.h"
#include "lnm/loop.h"
lnm_http_step_err lnm_http_loop_step_body_to_buf(lnm_http_conn *conn) {
lnm_http_loop_ctx *ctx = conn->ctx;
if (ctx->req.body.buf == NULL) {
ctx->req.body.buf = malloc(ctx->req.body.expected_len * sizeof(char));
ctx->req.body.len = 0;
}
size_t to_read = LNM_MIN(conn->r.size - conn->r.read,
ctx->req.body.expected_len - ctx->req.body.len);
memcpy(&ctx->req.body.buf[ctx->req.body.len], &conn->r.buf[conn->r.read],
to_read);
ctx->req.body.len += to_read;
conn->r.read += to_read;
return ctx->req.body.len == ctx->req.body.expected_len
? lnm_http_step_err_done
: lnm_http_step_err_io_needed;
}

View file

@ -63,6 +63,10 @@ lnm_http_parse_err lnm_http_req_parse(lnm_http_req *req, char *buf,
}
void lnm_http_req_reset(lnm_http_req *req) {
if (req->body.owned) {
free(req->body.buf);
}
memset(req, 0, sizeof(lnm_http_req));
}