From 0c4c8d952f0f81573b8f178afcd3e28f34380363 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 11 Oct 2007 16:04:19 +0000 Subject: mantis 1285 --- src/daemon/Makefile.am | 1 + src/daemon/connection.c | 7 ++- src/daemon/reason_phrase.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ src/daemon/reason_phrase.h | 38 ++++++++++++++++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/daemon/reason_phrase.c create mode 100644 src/daemon/reason_phrase.h diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index c9b73287..b35b45d7 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -15,6 +15,7 @@ libmicrohttpd_la_LDFLAGS = \ -export-dynamic -version-info 2:0:0 $(retaincommand) libmicrohttpd_la_SOURCES = \ connection.c connection.h \ + reason_phrase.c reason_phrase.h \ daemon.c \ internal.c internal.h \ memorypool.c memorypool.h \ diff --git a/src/daemon/connection.c b/src/daemon/connection.c index 01163154..7abe0205 100644 --- a/src/daemon/connection.c +++ b/src/daemon/connection.c @@ -30,6 +30,7 @@ #include "connection.h" #include "memorypool.h" #include "response.h" +#include "reason_phrase.h" /** * Message to transmit when http 1.1 request is received @@ -986,12 +987,14 @@ MHD_build_header_response (struct MHD_Connection *connection) size_t size; size_t off; struct MHD_HTTP_Header *pos; - char code[32]; + char code[128]; char date[128]; char *data; MHD_add_extra_headers (connection); - SPRINTF (code, "%s %u\r\n", MHD_HTTP_VERSION_1_1, connection->responseCode); + const char* reason_phrase = MHD_get_reason_phrase_for(connection->responseCode); + _REAL_SNPRINTF (code, 128, "%s %u %s\r\n", MHD_HTTP_VERSION_1_1, + connection->responseCode, reason_phrase); off = strlen (code); /* estimate size */ size = off + 2; /* extra \r\n at the end */ diff --git a/src/daemon/reason_phrase.c b/src/daemon/reason_phrase.c new file mode 100644 index 00000000..27d4d73c --- /dev/null +++ b/src/daemon/reason_phrase.c @@ -0,0 +1,108 @@ +/* + This file is part of libmicrohttpd + (C) 2007 Lymba + + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file reason_phrase.c + * @brief Tables of the string response phrases + * @author Elliot Glaysher + * @author Christian Grothoff (minor code clean up) + */ + +#include "reason_phrase.h" + +static const char * invalid_hundred[] = { }; + +static const char * one_hundred[] = { + "Continue", + "Switching Protocols", + "Processing" +}; + +static const char* two_hundred[] = { + "OK", + "Created", + "Accepted", + "Non-Authoritative Information", + "No Content", + "Reset Content", + "Partial Content" +}; + +static const char* three_hundred[] = { + "Multiple Choices", + "Moved Permanently", + "Moved Temporarily", + "See Other", + "Not Modified", + "Use Proxy" +}; + +static const char* four_hundred[] = { + "Bad Request", + "Unauthorized", + "Payment Required", + "Forbidden", + "Not Found", + "Method Not Allowed", + "Not Acceptable", + "Proxy Authentication Required", + "Request Time-out", + "Conflict", + "Gone", + "Length Required", + "Precondition Failed", + "Request Entity Too Large", + "Request-URI Too Large", + "Unsupported Media Type" +}; + +static const char* five_hundred[] = { + "Internal Server Error", + "Bad Gateway", + "Service Unavailable", + "Gateway Time-out", + "HTTP Version not supported" +}; + + +struct MHD_Reason_Block { + unsigned int max; + const char ** data; +}; + +#define BLOCK(m) { (sizeof(m) / sizeof(char*)), m } + +static const struct MHD_Reason_Block reasons[] = { + BLOCK(one_hundred), + BLOCK(two_hundred), + BLOCK(three_hundred), + BLOCK(four_hundred), + BLOCK(five_hundred), +}; + +const char * +MHD_get_reason_phrase_for(unsigned int code) +{ + if ( (code >= 100 && code < 600) && + (reasons[code / 100].max > code % 100) ) + return reasons[code / 100].data[code % 100]; + return "Unknown"; +} diff --git a/src/daemon/reason_phrase.h b/src/daemon/reason_phrase.h new file mode 100644 index 00000000..b6376a2f --- /dev/null +++ b/src/daemon/reason_phrase.h @@ -0,0 +1,38 @@ +/* + This file is part of libmicrohttpd + (C) 2007 Lymba + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/** + * @file reason_phrase.c + * @brief Tables of the string response phrases + * @author Elliot Glaysher + */ + +#ifndef REASON_PHRASE_H +#define REASON_PHRASE_H + +/** + * Returns the string reason phrase for a response code. + * + * If we don't have a string for a status code, we give the first + * message in that status code class. + */ +const char* +MHD_get_reason_phrase_for(unsigned int code); + +#endif -- cgit v1.2.3