feat: paved the way for uploading pastes
This commit is contained in:
parent
94b07caeb3
commit
dfbd179c7a
8 changed files with 96 additions and 77 deletions
|
|
@ -26,9 +26,13 @@ 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->req.body_type == http_body_buf && ctx->req.body.buf != NULL) {
|
||||
free(ctx->req.body.buf);
|
||||
ctx->req.body.buf = NULL;
|
||||
} else if (ctx->req.body_type == http_body_file &&
|
||||
ctx->req.body.file != NULL) {
|
||||
fclose(ctx->req.body.file);
|
||||
ctx->req.body.file = NULL;
|
||||
}
|
||||
|
||||
if (ctx->res.head != NULL) {
|
||||
|
|
@ -36,16 +40,15 @@ void http_loop_ctx_reset(http_loop_ctx *ctx) {
|
|||
ctx->res.head = NULL;
|
||||
}
|
||||
|
||||
if (ctx->res.body != NULL) {
|
||||
if (ctx->res.body_type == http_response_body_buf && ctx->res.owns_body) {
|
||||
free((void *)ctx->res.body);
|
||||
} else if (ctx->res.body_type == http_response_body_file) {
|
||||
fclose(ctx->res.body);
|
||||
}
|
||||
if (ctx->res.body_type == http_body_buf && ctx->res.body.buf != NULL) {
|
||||
free(ctx->res.body.buf);
|
||||
ctx->res.body.buf = NULL;
|
||||
} else if (ctx->res.body_type == http_body_file &&
|
||||
ctx->res.body.file != NULL) {
|
||||
fclose(ctx->res.body.file);
|
||||
ctx->res.body.file = NULL;
|
||||
}
|
||||
|
||||
ctx->res.body = NULL;
|
||||
|
||||
for (size_t i = 0; i < ctx->res.header_count; i++) {
|
||||
if (ctx->res.headers[i].owned) {
|
||||
free((void *)ctx->res.headers[i].value);
|
||||
|
|
|
|||
|
|
@ -81,15 +81,15 @@ void http_loop_write_response(event_loop_conn *conn) {
|
|||
size_t bytes_written;
|
||||
|
||||
switch (res->body_type) {
|
||||
case http_response_body_buf:
|
||||
memcpy(&conn->wbuf[conn->wbuf_size],
|
||||
&((const char *)res->body)[res->body_written], bytes_to_write);
|
||||
case http_body_buf:
|
||||
memcpy(&conn->wbuf[conn->wbuf_size], &(res->body.buf)[res->body_written],
|
||||
bytes_to_write);
|
||||
conn->wbuf_size += bytes_to_write;
|
||||
res->body_written += bytes_to_write;
|
||||
break;
|
||||
case http_response_body_file:
|
||||
case http_body_file:
|
||||
bytes_written = fread(&conn->wbuf[conn->wbuf_size], sizeof(uint8_t),
|
||||
bytes_to_write, res->body);
|
||||
bytes_to_write, res->body.file);
|
||||
conn->wbuf_size += bytes_written;
|
||||
res->body_written += bytes_written;
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@
|
|||
|
||||
void http_loop_res_set_body_buf(http_loop_ctx *ctx, const char *body,
|
||||
size_t body_len, bool owned) {
|
||||
ctx->res.body_type = http_response_body_buf;
|
||||
ctx->res.body = (void *)body;
|
||||
ctx->res.body_type = http_body_buf;
|
||||
ctx->res.body.buf = (char *)body;
|
||||
ctx->res.body_len = body_len;
|
||||
ctx->res.owns_body = owned;
|
||||
}
|
||||
|
|
@ -19,8 +19,8 @@ void http_loop_res_set_body_file(http_loop_ctx *ctx, const char *filename) {
|
|||
// TODO error handling
|
||||
FILE *f = fopen(filename, "r");
|
||||
|
||||
ctx->res.body_type = http_response_body_file;
|
||||
ctx->res.body = f;
|
||||
ctx->res.body_type = http_body_file;
|
||||
ctx->res.body.file = f;
|
||||
ctx->res.body_len = st.st_size;
|
||||
}
|
||||
|
||||
|
|
@ -85,14 +85,15 @@ bool http_loop_step_body_to_buf(event_loop_conn *conn) {
|
|||
return true;
|
||||
}
|
||||
|
||||
ctx->req.body = malloc(ctx->req.body_len * sizeof(uint8_t));
|
||||
ctx->req.body_type = http_body_buf;
|
||||
ctx->req.body.buf = 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);
|
||||
memcpy(&ctx->req.body.buf[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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue