core-legacy/handlers/connection.c

112 lines
3.5 KiB
C
Raw Normal View History

2020-05-20 23:33:18 +00:00
#include <string.h>
2020-05-31 00:07:25 +00:00
#include <macros.h>
#include <constants.h>
2020-05-05 09:42:02 +00:00
#include <mongoose.h>
#include <logger.h>
#include <router.h>
#include <handlers.h>
2020-06-02 22:47:49 +00:00
#define HEADERS_FMT "Content-Type: %s"
2020-05-20 23:33:18 +00:00
// -2 for "%s" -1 for \0
#define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3)
2020-06-02 22:47:49 +00:00
static void
send_response(struct mg_connection *nc, endpoint_response_t *response)
{
if(response->status_code)
{
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(nc, response->status_code, response->content_length, response_headers);
mg_printf(nc, "%s", response->content);
free(response_headers);
if(response->alloced_content)
{
free((char*)response->content);
}
}
}
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-31 22:45:08 +00:00
LOG_TRACE("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-06-02 22:47:49 +00:00
endpoint_response_t response;
static const char content[] = "the server did not create a response";
endpoint_response_text(&response, 500, content, STRLEN(content));
2020-05-20 23:33:18 +00:00
2020-06-02 22:47:49 +00:00
if(!endpoint)
{
/* Normalize path - resolve "." and ".." (in-place). */
if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) {
mg_http_send_error(nc, 400, NULL);
return;
}
char *request_file_org = malloc(sizeof(char) * hm->uri.len);
strncpy(request_file_org, hm->uri.p + 1, hm->uri.len);
request_file_org[hm->uri.len - 1] = '\0';
2020-05-20 23:33:18 +00:00
2020-06-02 22:47:49 +00:00
char *request_file = request_file_org;
while(request_file[0] == '/')
{
++request_file;
}
2020-06-02 22:47:49 +00:00
LOG_DEBUG("%s\n", request_file);
int access_result = access(request_file, R_OK);
free(request_file_org);
if(access_result != -1)
{
response.status_code = 0;
mg_serve_http(nc, hm, global_config.http_server_opts);
return;
}
else
{
2020-06-02 22:47:49 +00:00
endpoint = router_get_not_found_endpoint();
}
2020-05-05 09:42:02 +00:00
}
2020-06-02 22:47:49 +00:00
if(!endpoint->func)
2020-05-05 09:42:02 +00:00
{
2020-06-02 22:47:49 +00:00
if(endpoint->method == HTTP_METHOD_OPTIONS)
{
char options_header[256]; // TODO make more generic
sprintf(options_header, "Allow: OPTIONS%s%s%s%s",
endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : ""
);
mg_send_head(nc, 204, 0, options_header);
}
else
{
mg_send_head(nc, 501, 0, "Content-Type: text/plain");
}
2020-05-05 09:42:02 +00:00
}
2020-06-02 22:47:49 +00:00
endpoint->func(nc, hm, endpoint->args, &response);
send_response(nc, &response);
for(int i = 0; i < endpoint->args_count; ++i)
{
if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR)
{
free((char*)endpoint->args[i].value.v_str);
}
}
2020-05-05 09:42:02 +00:00
}
}