add: POST schedules/list
fix: CORS
This commit is contained in:
parent
a78815cb32
commit
309f2fbb52
5 changed files with 190 additions and 7 deletions
handlers
|
@ -12,6 +12,23 @@
|
|||
// -2 for "%s" -1 for \0
|
||||
#define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3)
|
||||
|
||||
static char*
|
||||
add_extra_headers(char *extra_headers)
|
||||
{
|
||||
char *result;
|
||||
size_t std_headers_len = strlen(global_config.http_server_opts.extra_headers);
|
||||
if(extra_headers == NULL)
|
||||
{
|
||||
result = malloc(sizeof(char) * (std_headers_len + 1));
|
||||
strcpy(result, global_config.http_server_opts.extra_headers);
|
||||
return result;
|
||||
}
|
||||
|
||||
result = malloc(sizeof(char) * (std_headers_len + strlen(extra_headers) + 3));
|
||||
sprintf(result, "%s\r\n%s", global_config.http_server_opts.extra_headers, extra_headers);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
send_response(struct mg_connection *nc, endpoint_response_t *response)
|
||||
{
|
||||
|
@ -20,10 +37,12 @@ send_response(struct mg_connection *nc, endpoint_response_t *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(nc, response->status_code, response->content_length, response_headers);
|
||||
char *extra_headers = add_extra_headers(response_headers);
|
||||
mg_send_head(nc, response->status_code, response->content_length, extra_headers);
|
||||
mg_printf(nc, "%s", response->content);
|
||||
|
||||
free(response_headers);
|
||||
free(extra_headers);
|
||||
|
||||
if(response->alloced_content)
|
||||
{
|
||||
|
@ -50,7 +69,7 @@ handler_connection(struct mg_connection *nc, int ev, void *p)
|
|||
{
|
||||
/* Normalize path - resolve "." and ".." (in-place). */
|
||||
if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) {
|
||||
mg_http_send_error(nc, 400, NULL);
|
||||
mg_http_send_error(nc, 400, global_config.http_server_opts.extra_headers);
|
||||
return;
|
||||
}
|
||||
char *request_file_org = malloc(sizeof(char) * hm->uri.len);
|
||||
|
@ -89,16 +108,20 @@ handler_connection(struct mg_connection *nc, int ev, void *p)
|
|||
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
|
||||
endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : ""
|
||||
);
|
||||
mg_send_head(nc, 204, 0, options_header);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
endpoint->func(nc, hm, endpoint->args, &response);
|
||||
send_response(nc, &response);
|
||||
else
|
||||
{
|
||||
endpoint->func(nc, hm, endpoint->args, &response);
|
||||
send_response(nc, &response);
|
||||
}
|
||||
|
||||
for(int i = 0; i < endpoint->args_count; ++i)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue