[httpd] More refactoring, reduce code duplication in the httpd_xxx modules

This commit is contained in:
ejurgensen
2017-11-08 00:32:03 +01:00
parent 9ed810d9df
commit 473a29ef8a
10 changed files with 844 additions and 1004 deletions

View File

@@ -253,6 +253,7 @@ httpd_fixup_uri(struct evhttp_request *req)
}
*/
/* --------------------------- REQUEST HELPERS ------------------------------ */
static void
@@ -877,6 +878,46 @@ httpd_uri_parse(const char *uri)
return NULL;
}
struct httpd_request *
httpd_request_parse(struct evhttp_request *req, struct httpd_uri_parsed *uri_parsed, const char *user_agent, struct httpd_uri_map *uri_map)
{
struct httpd_request *hreq;
struct evkeyvalq *headers;
int i;
int ret;
CHECK_NULL(L_HTTPD, hreq = calloc(1, sizeof(struct httpd_request)));
// Note req is allowed to be NULL
hreq->req = req;
hreq->uri_parsed = uri_parsed;
hreq->query = &(uri_parsed->ev_query);
if (req && !user_agent)
{
headers = evhttp_request_get_input_headers(req);
hreq->user_agent = evhttp_find_header(headers, "User-Agent");
}
else
hreq->user_agent = user_agent;
// Find a handler for the path
for (i = 0; uri_map[i].handler; i++)
{
ret = regexec(&uri_map[i].preg, uri_parsed->path, 0, NULL, 0);
if (ret == 0)
{
hreq->handler = uri_map[i].handler;
return hreq; // Success
}
}
// Handler not found, that's an error
free(hreq);
return NULL;
}
/* Thread: httpd */
void
httpd_stream_file(struct evhttp_request *req, int id)