add: logging

fix: rename branch
This commit is contained in:
Tobias Reisinger 2020-08-11 00:10:59 +02:00
parent 9e5718db43
commit 2f9be90ec1
5 changed files with 63 additions and 49 deletions
src/handlers

View file

@ -93,6 +93,8 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
LOGGER_DEBUG("failed to normalize uri %.*s\n", hm->uri.len, hm->uri.p);
return;
}
LOGGER_DEBUG("requested file: %.*s\n", hm->uri.len, hm->uri.p);
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';
@ -103,7 +105,6 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
++request_file;
}
LOGGER_DEBUG("%s\n", request_file);
char *request_file_path = malloc(sizeof(char) * (strlen(request_file) + strlen(global_config.content_dir) + 2));
sprintf(request_file_path, "%s/%s", global_config.content_dir, request_file);
int access_result = access(request_file_path, R_OK);
@ -123,31 +124,27 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
}
}
if(!endpoint->func)
if(endpoint->method == HTTP_METHOD_OPTIONS)
{
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" : ""
);
char *extra_headers = add_extra_headers(options_header);
mg_send_head(nc, 204, 0, extra_headers);
free(extra_headers);
}
else
{
mg_send_head(nc, 501, 0, "Content-Type: text/plain");
}
LOGGER_DEBUG("sending options for %s\n", endpoint->full_route);
char options_header[128];
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" : ""
);
char *extra_headers = add_extra_headers(options_header);
mg_send_head(nc, 204, 0, extra_headers);
free(extra_headers);
}
else
{
LOGGER_DEBUG("calling endpoint function %p\n", endpoint->func);
LOGGER_DEBUG("calling endpoint function for route %s\n", endpoint->full_route);
endpoint->func(nc, hm, endpoint->args, &response);
LOGGER_DEBUG("sending response to %p\n", nc);
char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
LOGGER_DEBUG("sending response to %s\n", addr);
send_response(nc, &response);
}
@ -175,7 +172,9 @@ handler_http(struct mg_connection *nc, int ev, void *p)
}
if(ev == MG_EV_HTTP_REQUEST)
{
LOGGER_DEBUG("new http request (%p)\n", nc);
char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
LOGGER_DEBUG("new http request from %s\n", addr);
struct http_message *hm = (struct http_message*)p;
handle_http_request(nc, hm);
}