diff options
author | Russell King <rmk@armlinux.org.uk> | 2021-09-14 22:40:54 +0100 |
---|---|---|
committer | Russell King <rmk@armlinux.org.uk> | 2021-09-14 22:44:42 +0100 |
commit | 760874afe4d70abc1c6779a6b8163d62fb8d347b (patch) | |
tree | db3a93afeb55f1e946c3d4caf87856b0f1ca90da /event-httpd.c | |
parent | 9b52ca854449b2fb3b845f9d35a59f7089338e1a (diff) |
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 <rmk@armlinux.org.uk>
Diffstat (limited to 'event-httpd.c')
-rw-r--r-- | event-httpd.c | 51 |
1 files 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 <gio/gio.h> +#include <stdbool.h> #include <stdlib.h> #include <string.h> @@ -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, ' '); |