add: file sending capability

This commit is contained in:
Tobias Reisinger 2020-05-31 02:07:25 +02:00
parent 6178ef3c7b
commit 760cec9a20
18 changed files with 79 additions and 53 deletions
handlers

View file

@ -1,5 +1,6 @@
#include <string.h>
#include <macros.h>
#include <constants.h>
#include <mongoose.h>
#include <logger.h>
@ -12,11 +13,8 @@
// -2 for "%s" -1 for \0
#define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3)
#define STD_RESPONSE_CONTENT "the server did not create a response"
#define STD_RESPONSE_CONTENT_LEN (sizeof(STD_RESPONSE_CONTENT) - 1)
void
handler_connection(struct mg_connection *c, int ev, void *p)
handler_connection(struct mg_connection *nc, int ev, void *p)
{
if (ev == MG_EV_HTTP_REQUEST)
{
@ -30,19 +28,17 @@ handler_connection(struct mg_connection *c, int ev, void *p)
if(endpoint->func)
{
endpoint_response_t response;
response.status_code = 500;
response.content_type = "text/plain";
response.content_length = STD_RESPONSE_CONTENT_LEN;
response.content = STD_RESPONSE_CONTENT;
response.alloced_content = false;
endpoint->func(p, endpoint->args, &response);
static const char content[] = "the server did not create a response";
endpoint_response_text(&response, 500, content, STRLEN(content));
endpoint->func(nc, hm, endpoint->args, &response);
char *response_headers = malloc(sizeof(char) * (HEADERS_FMT_LEN + strlen(response.content_type) + 1));
sprintf(response_headers, HEADERS_FMT, response.content_type);
mg_send_head(c, response.status_code, response.content_length, response_headers);
mg_printf(c, "%s", response.content);
mg_send_head(nc, response.status_code, response.content_length, response_headers);
mg_printf(nc, "%s", response.content);
free(response_headers);
@ -70,17 +66,17 @@ handler_connection(struct mg_connection *c, int ev, void *p)
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : ""
);
mg_send_head(c, 204, 0, options_header);
mg_send_head(nc, 204, 0, options_header);
}
else
{
mg_send_head(c, 501, 0, "Content-Type: text/plain");
mg_send_head(nc, 501, 0, "Content-Type: text/plain");
}
}
}
else
{
mg_send_head(c, 500, 0, "Content-Type: text/plain");
mg_send_head(nc, 500, 0, "Content-Type: text/plain");
}
//mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p);