2020-05-20 23:33:18 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-05-31 00:07:25 +00:00
|
|
|
#include <macros.h>
|
2020-05-13 23:37:08 +00:00
|
|
|
#include <constants.h>
|
2020-05-05 09:42:02 +00:00
|
|
|
#include <mongoose.h>
|
|
|
|
#include <logger.h>
|
|
|
|
#include <router.h>
|
|
|
|
#include <handlers.h>
|
|
|
|
|
2020-05-20 23:33:18 +00:00
|
|
|
#define STD_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Allow-Methods: *\r\n"
|
|
|
|
#define HEADERS_FMT STD_HEADERS "Content-Type: %s"
|
|
|
|
|
|
|
|
// -2 for "%s" -1 for \0
|
|
|
|
#define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3)
|
|
|
|
|
2020-05-05 09:42:02 +00:00
|
|
|
void
|
2020-05-31 00:07:25 +00:00
|
|
|
handler_connection(struct mg_connection *nc, int ev, void *p)
|
2020-05-05 09:42:02 +00:00
|
|
|
{
|
|
|
|
if (ev == MG_EV_HTTP_REQUEST)
|
|
|
|
{
|
|
|
|
struct http_message *hm = (struct http_message *) p;
|
2020-05-19 22:51:16 +00:00
|
|
|
LOG_DEBUG("new http %.*s request for %.*s\n", hm->method.len, hm->method.p, hm->uri.len, hm->uri.p);
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method);
|
|
|
|
|
2020-05-13 23:37:08 +00:00
|
|
|
if(endpoint)
|
2020-05-05 09:42:02 +00:00
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
if(endpoint->func)
|
2020-05-06 08:53:42 +00:00
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
endpoint_response_t response;
|
|
|
|
|
2020-05-31 00:07:25 +00:00
|
|
|
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);
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
char *response_headers = malloc(sizeof(char) * (HEADERS_FMT_LEN + strlen(response.content_type) + 1));
|
|
|
|
sprintf(response_headers, HEADERS_FMT, response.content_type);
|
|
|
|
|
2020-05-31 00:07:25 +00:00
|
|
|
mg_send_head(nc, response.status_code, response.content_length, response_headers);
|
|
|
|
mg_printf(nc, "%s", response.content);
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
free(response_headers);
|
|
|
|
|
|
|
|
if(response.alloced_content)
|
|
|
|
{
|
|
|
|
free((char*)response.content);
|
|
|
|
}
|
2020-05-11 12:50:25 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < endpoint->args_count; ++i)
|
2020-05-06 08:53:42 +00:00
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR)
|
|
|
|
{
|
|
|
|
free((char*)endpoint->args[i].value.v_str);
|
|
|
|
}
|
2020-05-06 08:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 12:50:25 +00:00
|
|
|
else
|
|
|
|
{
|
2020-05-13 23:37:08 +00:00
|
|
|
if(endpoint->method == HTTP_METHOD_OPTIONS)
|
|
|
|
{
|
|
|
|
char options_header[256]; // TODO make more generic
|
2020-05-20 23:33:18 +00:00
|
|
|
sprintf(options_header, STD_HEADERS "Allow: OPTIONS%s%s%s%s",
|
2020-05-13 23:37:08 +00:00
|
|
|
endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
|
|
|
|
endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
|
|
|
|
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
|
|
|
|
endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : ""
|
|
|
|
);
|
2020-05-31 00:07:25 +00:00
|
|
|
mg_send_head(nc, 204, 0, options_header);
|
2020-05-13 23:37:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-31 00:07:25 +00:00
|
|
|
mg_send_head(nc, 501, 0, "Content-Type: text/plain");
|
2020-05-13 23:37:08 +00:00
|
|
|
}
|
2020-05-11 12:50:25 +00:00
|
|
|
}
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-31 00:07:25 +00:00
|
|
|
mg_send_head(nc, 500, 0, "Content-Type: text/plain");
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p);
|
|
|
|
//mg_printf(c, "%.*s", (int)hm->body.len, hm->body.p);
|
|
|
|
}
|
|
|
|
}
|