2020-05-05 09:42:02 +00:00
|
|
|
#include <mongoose.h>
|
|
|
|
#include <logger.h>
|
|
|
|
#include <router.h>
|
|
|
|
#include <handlers.h>
|
|
|
|
|
|
|
|
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;
|
2020-05-11 12:50:25 +00:00
|
|
|
LOG_DEBUG("new http request for %.*s\n", 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);
|
|
|
|
|
|
|
|
if(endpoint && endpoint->func)
|
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
if(endpoint->func)
|
2020-05-06 08:53:42 +00:00
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
endpoint->func(c, endpoint->args, p);
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
mg_send_head(c, 501, 0, "Content-Type: text/plain");
|
|
|
|
}
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-11 12:50:25 +00:00
|
|
|
mg_send_head(c, 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);
|
|
|
|
}
|
|
|
|
}
|