From 760874afe4d70abc1c6779a6b8163d62fb8d347b Mon Sep 17 00:00:00 2001 From: Russell King Date: Tue, 14 Sep 2021 22:40:54 +0100 Subject: event-httpd: split out request parsing Split out the parsing of the request line and headers into a separate function to simplify how we free the received line and deal with receiving the next line of the request. Signed-off-by: Russell King --- event-httpd.c | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/event-httpd.c b/event-httpd.c index 63fb2f3..c080107 100644 --- a/event-httpd.c +++ b/event-httpd.c @@ -11,6 +11,7 @@ // directly to this server. #include +#include #include #include @@ -122,6 +123,28 @@ static void update(GObject *source, GAsyncResult *res, gpointer user_data) g_data_input_stream_read_line_async(c->data, 0, NULL, update, c); } +static bool parse_request(struct client *c, const char *line) +{ + // In the interest of robustness, servers SHOULD ignore any empty + // line(s) received where a Request-Line is expected. + if (!c->request) { + if (line[0]) + c->request = g_strdup(line); + return true; + } + + // Continue reading the request headers (we discard them) + if (line[0]) { + // Detect any X-Forwarded-* header + // FIXME: should this be case-insensitive? + if (g_str_has_prefix(line, "X-Forwarded-")) + c->forwarded = TRUE; + return true; + } + + return false; +} + enum method { GET, UPDATE, @@ -135,6 +158,7 @@ static void receive(GObject *source, GAsyncResult *res, gpointer user_data) GError *error = NULL; gsize len; char *line, *uri, *query, *unescaped, *version; + bool more; line = g_data_input_stream_read_line_finish(c->data, res, &len, &error); if (error || !line) { @@ -144,35 +168,16 @@ static void receive(GObject *source, GAsyncResult *res, gpointer user_data) return; } - // In the interest of robustness, servers SHOULD ignore any empty - // line(s) received where a Request-Line is expected. - if (!c->request) { - if (line[0]) - c->request = line; - else - g_free(line); - - g_data_input_stream_read_line_async(c->data, 0, NULL, - receive, c); - return; - } - - // Continue reading the request headers (we discard them) - if (line[0]) { - // Detect any X-Forwarded-* header - // FIXME: should this be case-insensitive? - if (g_str_has_prefix(line, "X-Forwarded-")) - c->forwarded = TRUE; - - g_free(line); + more = parse_request(c, line); + g_free(line); + // If there is more header to parse, read another line. + if (more) { g_data_input_stream_read_line_async(c->data, 0, NULL, receive, c); return; } - g_free(line); - // End of request. Parse it. // Find the URI uri = strchr(c->request, ' '); -- cgit