From b959c6bc3abd4432cceeba0e463cee437eae1004 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Fri, 28 Feb 2003 11:21:31 +0000 Subject: [PATCH] . Added strntoupper() . rfc1123_date() now takes a time_t argument . default_reply() now takes a `reason' string . Skeleton process_get() . Implemented process_request() completely (for now) (I hope) . Changed oversized request error from 400 Bad Request to 413 Request Entity Too Large --- trunk/darkhttpd.c | 75 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index ebc229c..e391c16 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -288,6 +289,18 @@ static void *xrealloc(void *original, const size_t size) +/* --------------------------------------------------------------------------- + * Uppercasify all characters in a string of given length. + */ +static void strntoupper(char *str, const int length) +{ + int i; + for (i=0; ireply_length = asprintf(&(conn->reply), "%d %s\n" - "

%s


\n" - "%s\n" + "

%s

\n" /* errname */ + "%s\n" /* reason */ + "
\n" + "Generated by %s on %s\n" "\n", - errcode, errname, errname, pkgname); + errcode, errname, errname, reason, pkgname, rfc1123_date(time(NULL))); if (conn->reply == NULL) errx(1, "out of memory in asprintf()"); @@ -338,7 +353,7 @@ static void default_reply(struct connection *conn, "Content-Length: %d\r\n" "Content-Type: text/html\r\n" "\r\n", - errcode, errname, rfc1123_date(), pkgname, conn->reply_length); + errcode, errname, rfc1123_date(time(NULL)), pkgname, conn->reply_length); if (conn->header == NULL) errx(1, "out of memory in asprintf()"); conn->reply_type = REPLY_GENERATED; @@ -359,13 +374,27 @@ static void parse_request(const char *req, const int length, *method = (char*)xmalloc(bound1+1); memcpy(*method, req, bound1); - (*method)[bound1] = '\0'; + (*method)[bound1] = 0; + strntoupper(method, bound1); - for (bound2=bound1+1; bound2request, conn->request_length, &method, &url); - debugf("method=``%s'', url=``%s''\n", method, url); - debugf("%s", conn->request); - /* FIXME */ - default_reply(conn, 501, "Not Implemented"); + if (strcmp(method, "GET") == 0) + process_get(conn, url, 0); + else if (strcmp(method, "HEAD") == 0) + process_get(conn, url, 1); + else if (strcmp(method, "OPTIONS") == 0 || + strcmp(method, "POST") == 0 || + strcmp(method, "PUT") == 0 || + strcmp(method, "DELETE") == 0 || + strcmp(method, "TRACE") == 0 || + strcmp(method, "CONNECT") == 0) + default_reply(conn, 501, "Not Implemented", + "That method is not implemented."); + else + default_reply(conn, 400, "Bad Request", + "That method is not a valid HTTP/1.1 method."); + + /* advance state */ conn->state = SEND_HEADER; + /* request, method, url not needed anymore */ + debugf("%s", conn->request); free(conn->request); conn->request = NULL; debugf("%s-=-\n", conn->header); @@ -430,7 +474,8 @@ static void poll_recv_request(struct connection *conn) /* die if it's too long */ if (conn->request_length > MAX_REQUEST_LENGTH) { - default_reply(conn, 400, "Bad Request"); + default_reply(conn, 413, "Request Entity Too Large", + "Your request was dropped because it was too long."); conn->state = SEND_HEADER; } }