#include #include #include #include #include void handler_connection(struct mg_connection *c, int ev, void *p) { if (ev == MG_EV_HTTP_REQUEST) { struct http_message *hm = (struct http_message *) p; LOG_DEBUG("new http request for %.*s\n", hm->uri.len, hm->uri.p); endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method); if(endpoint) { if(endpoint->func) { endpoint->func(c, endpoint->args, p); 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); } } } else { if(endpoint->method == HTTP_METHOD_OPTIONS) { char options_header[256]; // TODO make more generic sprintf(options_header, "Allow: OPTIONS%s%s%s%s\r\n" STANDARD_HEADERS, 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(c, 204, 0, options_header); } else { mg_send_head(c, 501, 0, "Content-Type: text/plain"); } } } else { mg_send_head(c, 500, 0, "Content-Type: text/plain"); } //mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p); //mg_printf(c, "%.*s", (int)hm->body.len, hm->body.p); } }