From 376c8b29fb8fa5b0c5f100a0ee6bb06d1f387d4d Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Tue, 18 Nov 2003 10:19:29 +0000 Subject: [PATCH] . Make parse_request return int so it can return failure on malformed requests. . Make it bomb out on malformed requests like "moo\r\n\r\n" --- trunk/darkhttpd.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index f391fce..5908ef1 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -1254,7 +1254,7 @@ static void parse_range_field(struct connection *conn) * url (/), the referer (if given) and the user-agent (if given). Remember to * deallocate all these buffers. The method will be returned in uppercase. */ -static void parse_request(struct connection *conn) +static int parse_request(struct connection *conn) { size_t bound1, bound2; char *tmp; @@ -1273,6 +1273,8 @@ static void parse_request(struct connection *conn) conn->request[bound1] == ' '; bound1++) ; + if (bound1 == conn->request_length) return 0; /* fail */ + for (bound2=bound1+1; bound2 < conn->request_length && conn->request[bound2] != ' ' && conn->request[bound2] != '\r'; bound2++) @@ -1312,6 +1314,7 @@ static void parse_request(struct connection *conn) conn->user_agent = parse_field(conn, "User-Agent: "); parse_range_field(conn); + return 1; } @@ -1478,9 +1481,12 @@ static void process_get(struct connection *conn) */ static void process_request(struct connection *conn) { - parse_request(conn); - - if (strcmp(conn->method, "GET") == 0) + if (!parse_request(conn)) + { + default_reply(conn, 400, "Bad Request", + "You sent a request that the server couldn't understand."); + } + else if (strcmp(conn->method, "GET") == 0) { process_get(conn); }