libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 85b7d00955a8adb9e6d8d42ba37c5c0d53cafc53
parent c75bf1093162231d4e2f09f399f2d6039d2c321f
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sun, 12 Aug 2007 06:01:48 +0000

formatting and versioning

Diffstat:
MREADME | 1-
Mconfigure.ac | 4++--
Mdoc/libmicrohttpd.3 | 6+++---
Msrc/daemon/Makefile.am | 5++---
Msrc/daemon/connection.c | 1541++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/daemon/connection.h | 18+++++++-----------
Msrc/daemon/daemon.c | 747++++++++++++++++++++++++++++++++++++++-----------------------------------------
Msrc/daemon/daemontest.c | 153+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/daemon/daemontest_get.c | 470+++++++++++++++++++++++++++++++++----------------------------------------------
Msrc/daemon/daemontest_long_header.c | 321+++++++++++++++++++++++++++++++++----------------------------------------------
Msrc/daemon/daemontest_post.c | 538++++++++++++++++++++++++++++++++-----------------------------------------------
Msrc/daemon/daemontest_put.c | 562+++++++++++++++++++++++++++++++++----------------------------------------------
Msrc/daemon/fileserver_example.c | 113++++++++++++++++++++++++++++++++++++-------------------------------------------
Msrc/daemon/internal.c | 15+++++++--------
Msrc/daemon/internal.h | 67+++++++++++++++++++++++++++++++++++--------------------------------
Msrc/daemon/memorypool.c | 147+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/daemon/memorypool.h | 16+++++++---------
Msrc/daemon/minimal_example.c | 67++++++++++++++++++++++++++++++-------------------------------------
Msrc/daemon/plibc.h | 774++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/daemon/response.c | 257+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/daemon/response.h | 2+-
Msrc/include/microhttpd.h | 156+++++++++++++++++++++++++++++++++++--------------------------------------------
22 files changed, 2805 insertions(+), 3175 deletions(-)

diff --git a/README b/README @@ -38,4 +38,3 @@ Documentation: ============== - manual (texinfo, man) - tutorial -- web page diff --git a/configure.ac b/configure.ac @@ -21,8 +21,8 @@ # # AC_PREREQ(2.57) -AC_INIT([libmicrohttpd], [0.0.0],[libmicrohttpd@gnunet.org]) -AM_INIT_AUTOMAKE([libmicrohttpd], [0.0.0]) +AC_INIT([libmicrohttpd], [0.0.1],[libmicrohttpd@gnunet.org]) +AM_INIT_AUTOMAKE([libmicrohttpd], [0.0.1]) AM_CONFIG_HEADER([config.h]) AH_TOP([#define _GNU_SOURCE 1]) diff --git a/doc/libmicrohttpd.3 b/doc/libmicrohttpd.3 @@ -5,11 +5,11 @@ libmicrohttpd \- library for embedding HTTP servers \fB#include <microhttpd.h> -\fPInsert API here. - .SH "DESCRIPTION" .P -Insert API description here. +libmicrohttpd (short MHD) allows applications to easily integrate the functionality of a simple HTTP server. +.P +The details of the API are described in comments in the header file and on the webpage. .P .SH "SEE ALSO" diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am @@ -6,7 +6,7 @@ lib_LTLIBRARIES = \ libmicrohttpd.la libmicrohttpd_la_LDFLAGS = \ - -export-dynamic -version-info 0:0:0 + -export-dynamic -version-info 0:1:0 libmicrohttpd_la_SOURCES = \ connection.c connection.h \ daemon.c \ @@ -92,4 +92,4 @@ daemontest_long_header_LDADD = \ $(top_builddir)/src/daemon/libmicrohttpd.la \ @LIBCURL@ -endif -\ No newline at end of file +endif diff --git a/src/daemon/connection.c b/src/daemon/connection.c @@ -50,29 +50,29 @@ * @return number of entries iterated over */ int -MHD_get_connection_values(struct MHD_Connection * connection, - enum MHD_ValueKind kind, - MHD_KeyValueIterator iterator, - void * iterator_cls) { +MHD_get_connection_values (struct MHD_Connection *connection, + enum MHD_ValueKind kind, + MHD_KeyValueIterator iterator, void *iterator_cls) +{ int ret; - struct MHD_HTTP_Header * pos; + struct MHD_HTTP_Header *pos; if (connection == NULL) return -1; ret = 0; pos = connection->headers_received; - while (pos != NULL) { - if (0 != (pos->kind & kind)) { - ret++; - if ( (iterator != NULL) && - (MHD_YES != iterator(iterator_cls, - kind, - pos->header, - pos->value)) ) - return ret; + while (pos != NULL) + { + if (0 != (pos->kind & kind)) + { + ret++; + if ((iterator != NULL) && + (MHD_YES != iterator (iterator_cls, + kind, pos->header, pos->value))) + return ret; + } + pos = pos->next; } - pos = pos->next; - } return ret; } @@ -85,21 +85,20 @@ MHD_get_connection_values(struct MHD_Connection * connection, * @return NULL if no such item was found */ const char * -MHD_lookup_connection_value(struct MHD_Connection * connection, - enum MHD_ValueKind kind, - const char * key) { - struct MHD_HTTP_Header * pos; +MHD_lookup_connection_value (struct MHD_Connection *connection, + enum MHD_ValueKind kind, const char *key) +{ + struct MHD_HTTP_Header *pos; if (connection == NULL) return NULL; pos = connection->headers_received; - while (pos != NULL) { - if ( (0 != (pos->kind & kind)) && - (0 == strcasecmp(key, - pos->header)) ) - return pos->value; - pos = pos->next; - } + while (pos != NULL) + { + if ((0 != (pos->kind & kind)) && (0 == strcasecmp (key, pos->header))) + return pos->value; + pos = pos->next; + } return NULL; } @@ -114,25 +113,24 @@ MHD_lookup_connection_value(struct MHD_Connection * connection, * MHD_YES on success or if message has been queued */ int -MHD_queue_response(struct MHD_Connection * connection, - unsigned int status_code, - struct MHD_Response * response) { - if ( (connection == NULL) || - (response == NULL) || - (connection->response != NULL) || - (connection->bodyReceived == 0) || - (connection->headersReceived == 0) ) - return MHD_NO; - MHD_increment_response_rc(response); +MHD_queue_response (struct MHD_Connection *connection, + unsigned int status_code, struct MHD_Response *response) +{ + if ((connection == NULL) || + (response == NULL) || + (connection->response != NULL) || + (connection->bodyReceived == 0) || (connection->headersReceived == 0)) + return MHD_NO; + MHD_increment_response_rc (response); connection->response = response; connection->responseCode = status_code; - if ( (connection->method != NULL) && - (0 == strcasecmp(connection->method, - MHD_HTTP_METHOD_HEAD)) ) { - /* if this is a "HEAD" request, pretend that we - have already sent the full message body */ - connection->messagePos = response->total_size; - } + if ((connection->method != NULL) && + (0 == strcasecmp (connection->method, MHD_HTTP_METHOD_HEAD))) + { + /* if this is a "HEAD" request, pretend that we + have already sent the full message body */ + connection->messagePos = response->total_size; + } return MHD_YES; } @@ -140,20 +138,20 @@ MHD_queue_response(struct MHD_Connection * connection, * Do we (still) need to send a 100 continue * message for this connection? */ -static int -MHD_need_100_continue(struct MHD_Connection * connection) { - const char * expect; +static int +MHD_need_100_continue (struct MHD_Connection *connection) +{ + const char *expect; - return ( (connection->version != NULL) && - (0 == strcasecmp(connection->version, - MHD_HTTP_VERSION_1_1)) && - (connection->headersReceived == 1) && - (NULL != (expect = MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_EXPECT))) && - (0 == strcasecmp(expect, - "100-continue")) && - (connection->continuePos < strlen(HTTP_100_CONTINUE)) ); + return ((connection->version != NULL) && + (0 == strcasecmp (connection->version, + MHD_HTTP_VERSION_1_1)) && + (connection->headersReceived == 1) && + (NULL != (expect = MHD_lookup_connection_value (connection, + MHD_HEADER_KIND, + MHD_HTTP_HEADER_EXPECT))) + && (0 == strcasecmp (expect, "100-continue")) + && (connection->continuePos < strlen (HTTP_100_CONTINUE))); } /** @@ -162,49 +160,56 @@ MHD_need_100_continue(struct MHD_Connection * connection) { * @return MHD_YES on success */ int -MHD_connection_get_fdset(struct MHD_Connection * connection, - fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - int * max_fd) { +MHD_connection_get_fdset (struct MHD_Connection *connection, + fd_set * read_fd_set, + fd_set * write_fd_set, + fd_set * except_fd_set, int *max_fd) +{ int fd; - void * buf; + void *buf; fd = connection->socket_fd; if (fd == -1) return MHD_YES; - if ( (connection->read_close == MHD_NO) && - ( (connection->headersReceived == 0) || - (connection->readLoc < connection->read_buffer_size) ) ) { - FD_SET(fd, read_fd_set); - if (fd > *max_fd) - *max_fd = fd; - } else { - if ( (connection->read_close == MHD_NO) && - ( (connection->headersReceived == 1) && - (connection->post_processed == MHD_NO) && - (connection->readLoc == connection->read_buffer_size) ) ) { - /* try growing the read buffer, just in case */ - buf = MHD_pool_reallocate(connection->pool, - connection->read_buffer, - connection->read_buffer_size, - connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE); - if (buf != NULL) { - /* we can actually grow the buffer, do it! */ - connection->read_buffer = buf; - connection->read_buffer_size = connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE; - FD_SET(fd, read_fd_set); - if (fd > *max_fd) - *max_fd = fd; - } + if ((connection->read_close == MHD_NO) && + ((connection->headersReceived == 0) || + (connection->readLoc < connection->read_buffer_size))) + { + FD_SET (fd, read_fd_set); + if (fd > *max_fd) + *max_fd = fd; + } + else + { + if ((connection->read_close == MHD_NO) && + ((connection->headersReceived == 1) && + (connection->post_processed == MHD_NO) && + (connection->readLoc == connection->read_buffer_size))) + { + /* try growing the read buffer, just in case */ + buf = MHD_pool_reallocate (connection->pool, + connection->read_buffer, + connection->read_buffer_size, + connection->read_buffer_size * 2 + + MHD_BUF_INC_SIZE); + if (buf != NULL) + { + /* we can actually grow the buffer, do it! */ + connection->read_buffer = buf; + connection->read_buffer_size = + connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE; + FD_SET (fd, read_fd_set); + if (fd > *max_fd) + *max_fd = fd; + } + } + } + if ((connection->response != NULL) || MHD_need_100_continue (connection)) + { + FD_SET (fd, write_fd_set); + if (fd > *max_fd) + *max_fd = fd; } - } - if ( (connection->response != NULL) || - MHD_need_100_continue(connection) ) { - FD_SET(fd, write_fd_set); - if (fd > *max_fd) - *max_fd = fd; - } return MHD_YES; } @@ -216,9 +221,10 @@ MHD_connection_get_fdset(struct MHD_Connection * connection, * @param status_code the response code to send (413 or 414) */ static void -MHD_excessive_data_handler(struct MHD_Connection * connection, - unsigned int status_code) { - struct MHD_Response * response; +MHD_excessive_data_handler (struct MHD_Connection *connection, + unsigned int status_code) +{ + struct MHD_Response *response; /* die, header far too long to be reasonable; FIXME: send proper response to client @@ -226,16 +232,12 @@ MHD_excessive_data_handler(struct MHD_Connection * connection, connection->read_close = MHD_YES; connection->headersReceived = MHD_YES; connection->bodyReceived = MHD_YES; - MHD_DLOG(connection->daemon, - "Received excessively long header, closing connection.\n"); - response = MHD_create_response_from_data(strlen(REQUEST_TOO_BIG), - REQUEST_TOO_BIG, - MHD_NO, - MHD_NO); - MHD_queue_response(connection, - status_code, - response); - MHD_destroy_response(response); + MHD_DLOG (connection->daemon, + "Received excessively long header, closing connection.\n"); + response = MHD_create_response_from_data (strlen (REQUEST_TOO_BIG), + REQUEST_TOO_BIG, MHD_NO, MHD_NO); + MHD_queue_response (connection, status_code, response); + MHD_destroy_response (response); } /** @@ -247,41 +249,47 @@ MHD_excessive_data_handler(struct MHD_Connection * connection, * return NULL. Otherwise return a pointer to the line. */ static char * -MHD_get_next_header_line(struct MHD_Connection * connection) { - char * rbuf; +MHD_get_next_header_line (struct MHD_Connection *connection) +{ + char *rbuf; size_t pos; if (connection->readLoc == 0) return NULL; pos = 0; rbuf = connection->read_buffer; - while ( (pos < connection->readLoc - 1) && - (rbuf[pos] != '\r') && - (rbuf[pos] != '\n') ) + while ((pos < connection->readLoc - 1) && + (rbuf[pos] != '\r') && (rbuf[pos] != '\n')) pos++; - if (pos == connection->readLoc - 1) { - /* not found, consider growing... */ - if (connection->readLoc == connection->read_buffer_size) { - rbuf = MHD_pool_reallocate(connection->pool, - connection->read_buffer, - connection->read_buffer_size, - connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE); - if (rbuf == NULL) { - MHD_excessive_data_handler(connection, - (connection->url != NULL) - ? MHD_HTTP_REQUEST_ENTITY_TOO_LARGE - : MHD_HTTP_REQUEST_URI_TOO_LONG); - } else { - connection->read_buffer_size = connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE; - connection->read_buffer = rbuf; - } + if (pos == connection->readLoc - 1) + { + /* not found, consider growing... */ + if (connection->readLoc == connection->read_buffer_size) + { + rbuf = MHD_pool_reallocate (connection->pool, + connection->read_buffer, + connection->read_buffer_size, + connection->read_buffer_size * 2 + + MHD_BUF_INC_SIZE); + if (rbuf == NULL) + { + MHD_excessive_data_handler (connection, + (connection->url != NULL) + ? MHD_HTTP_REQUEST_ENTITY_TOO_LARGE + : MHD_HTTP_REQUEST_URI_TOO_LONG); + } + else + { + connection->read_buffer_size = + connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE; + connection->read_buffer = rbuf; + } + } + return NULL; } - return NULL; - } /* found, check if we have proper CRLF */ - if ( (rbuf[pos] == '\r') && - (rbuf[pos+1] == '\n') ) - rbuf[pos++] = '\0'; /* skip both r and n */ + if ((rbuf[pos] == '\r') && (rbuf[pos + 1] == '\n')) + rbuf[pos++] = '\0'; /* skip both r and n */ rbuf[pos++] = '\0'; connection->read_buffer += pos; connection->read_buffer_size -= pos; @@ -293,22 +301,21 @@ MHD_get_next_header_line(struct MHD_Connection * connection) { * @return MHD_NO on failure (out of memory), MHD_YES for success */ static int -MHD_connection_add_header(struct MHD_Connection * connection, - char * key, - char * value, - enum MHD_ValueKind kind) { - struct MHD_HTTP_Header * hdr; +MHD_connection_add_header (struct MHD_Connection *connection, + char *key, char *value, enum MHD_ValueKind kind) +{ + struct MHD_HTTP_Header *hdr; - hdr = MHD_pool_allocate(connection->pool, - sizeof(struct MHD_HTTP_Header), - MHD_YES); - if (hdr == NULL) { - MHD_DLOG(connection->daemon, - "Not enough memory to allocate header record!\n"); - MHD_excessive_data_handler(connection, - MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); - return MHD_NO; - } + hdr = MHD_pool_allocate (connection->pool, + sizeof (struct MHD_HTTP_Header), MHD_YES); + if (hdr == NULL) + { + MHD_DLOG (connection->daemon, + "Not enough memory to allocate header record!\n"); + MHD_excessive_data_handler (connection, + MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); + return MHD_NO; + } hdr->next = connection->headers_received; hdr->header = key; hdr->value = value; @@ -319,60 +326,57 @@ MHD_connection_add_header(struct MHD_Connection * connection, /** * Process escape sequences ('+'=space, %HH) - */ + */ static void -MHD_http_unescape(char * val) { - char * esc; +MHD_http_unescape (char *val) +{ + char *esc; unsigned int num; - while (NULL != (esc = strstr(val, "+"))) - *esc = ' '; - while (NULL != (esc = strstr(val, "%"))) { - if ( (1 == sscanf(&esc[1], - "%2x", - &num)) || - (1 == sscanf(&esc[1], - "%2X", - &num)) ) { - esc[0] = (unsigned char) num; - memmove(&esc[1], - &esc[3], - strlen(&esc[3])); + while (NULL != (esc = strstr (val, "+"))) + *esc = ' '; + while (NULL != (esc = strstr (val, "%"))) + { + if ((1 == sscanf (&esc[1], + "%2x", &num)) || (1 == sscanf (&esc[1], "%2X", &num))) + { + esc[0] = (unsigned char) num; + memmove (&esc[1], &esc[3], strlen (&esc[3])); + } + val = esc + 1; } - val = esc+1; - } } /** * @return MHD_NO on failure (out of memory), MHD_YES for success */ static int -parse_arguments(enum MHD_ValueKind kind, - struct MHD_Connection * connection, - char * args) { - char * equals; - char * amper; +parse_arguments (enum MHD_ValueKind kind, + struct MHD_Connection *connection, char *args) +{ + char *equals; + char *amper; - while (args != NULL) { - equals = strstr(args, "="); - if (equals == NULL) - return MHD_NO; /* invalid, ignore */ - equals[0] = '\0'; - equals++; - amper = strstr(equals, "&"); - if (amper != NULL) { - amper[0] = '\0'; - amper++; + while (args != NULL) + { + equals = strstr (args, "="); + if (equals == NULL) + return MHD_NO; /* invalid, ignore */ + equals[0] = '\0'; + equals++; + amper = strstr (equals, "&"); + if (amper != NULL) + { + amper[0] = '\0'; + amper++; + } + MHD_http_unescape (args); + MHD_http_unescape (equals); + if (MHD_NO == MHD_connection_add_header (connection, + args, equals, kind)) + return MHD_NO; + args = amper; } - MHD_http_unescape(args); - MHD_http_unescape(equals); - if (MHD_NO == MHD_connection_add_header(connection, - args, - equals, - kind)) - return MHD_NO; - args = amper; - } return MHD_YES; } @@ -382,68 +386,63 @@ parse_arguments(enum MHD_ValueKind kind, * @return MHD_YES for success, MHD_NO for failure (malformed, out of memory) */ static int -MHD_parse_cookie_header(struct MHD_Connection * connection) { - const char * hdr; - char * cpy; - char * pos; - char * semicolon; - char * equals; +MHD_parse_cookie_header (struct MHD_Connection *connection) +{ + const char *hdr; + char *cpy; + char *pos; + char *semicolon; + char *equals; int quotes; - hdr = MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - "Cookie"); + hdr = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "Cookie"); if (hdr == NULL) return MHD_YES; - cpy = MHD_pool_allocate(connection->pool, - strlen(hdr)+1, - MHD_YES); - if (cpy == NULL) { - MHD_DLOG(connection->daemon, - "Not enough memory to parse cookies!\n"); - MHD_excessive_data_handler(connection, - MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); - return MHD_NO; - } - memcpy(cpy, - hdr, - strlen(hdr)+1); - pos = cpy; - while (pos != NULL) { - equals = strstr(pos, "="); - if (equals == NULL) - break; - equals[0] = '\0'; - equals++; - quotes = 0; - semicolon = equals; - while ( (semicolon[0] != '\0') && - ( (quotes != 0) || - ( (semicolon[0] != ';') && - (semicolon[0] != ',') ) ) ) { - if (semicolon[0] == '"') - quotes = (quotes + 1) & 1; - semicolon++; - } - if (semicolon[0] == '\0') - semicolon = NULL; - if (semicolon != NULL) { - semicolon[0] = '\0'; - semicolon++; + cpy = MHD_pool_allocate (connection->pool, strlen (hdr) + 1, MHD_YES); + if (cpy == NULL) + { + MHD_DLOG (connection->daemon, "Not enough memory to parse cookies!\n"); + MHD_excessive_data_handler (connection, + MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); + return MHD_NO; } - /* remove quotes */ - if ( (equals[0] == '"') && - (equals[strlen(equals)-1] == '"') ) { - equals[strlen(equals)-1] = '\0'; + memcpy (cpy, hdr, strlen (hdr) + 1); + pos = cpy; + while (pos != NULL) + { + equals = strstr (pos, "="); + if (equals == NULL) + break; + equals[0] = '\0'; equals++; + quotes = 0; + semicolon = equals; + while ((semicolon[0] != '\0') && + ((quotes != 0) || + ((semicolon[0] != ';') && (semicolon[0] != ',')))) + { + if (semicolon[0] == '"') + quotes = (quotes + 1) & 1; + semicolon++; + } + if (semicolon[0] == '\0') + semicolon = NULL; + if (semicolon != NULL) + { + semicolon[0] = '\0'; + semicolon++; + } + /* remove quotes */ + if ((equals[0] == '"') && (equals[strlen (equals) - 1] == '"')) + { + equals[strlen (equals) - 1] = '\0'; + equals++; + } + if (MHD_NO == MHD_connection_add_header (connection, + pos, equals, MHD_COOKIE_KIND)) + return MHD_NO; + pos = semicolon; } - if (MHD_NO == MHD_connection_add_header(connection, - pos, - equals, - MHD_COOKIE_KIND)) - return MHD_NO; - pos = semicolon; - } return MHD_YES; } @@ -455,33 +454,33 @@ MHD_parse_cookie_header(struct MHD_Connection * connection) { * @return MHD_YES if the line is ok, MHD_NO if it is malformed */ static int -parse_initial_message_line(struct MHD_Connection * connection, - char * line) { - char * uri; - char * httpVersion; - char * args; +parse_initial_message_line (struct MHD_Connection *connection, char *line) +{ + char *uri; + char *httpVersion; + char *args; - uri = strstr(line, " "); + uri = strstr (line, " "); if (uri == NULL) - return MHD_NO; /* serious error */ + return MHD_NO; /* serious error */ uri[0] = '\0'; connection->method = line; uri++; while (uri[0] == ' ') uri++; - httpVersion = strstr(uri, " "); - if (httpVersion != NULL) { - httpVersion[0] = '\0'; - httpVersion++; - } - args = strstr(uri, "?"); - if (args != NULL) { - args[0] = '\0'; - args++; - parse_arguments(MHD_GET_ARGUMENT_KIND, - connection, - args); - } + httpVersion = strstr (uri, " "); + if (httpVersion != NULL) + { + httpVersion[0] = '\0'; + httpVersion++; + } + args = strstr (uri, "?"); + if (args != NULL) + { + args[0] = '\0'; + args++; + parse_arguments (MHD_GET_ARGUMENT_KIND, connection, args); + } connection->url = uri; if (httpVersion == NULL) connection->version = ""; @@ -502,131 +501,137 @@ parse_initial_message_line(struct MHD_Connection * connection, * size of the body is unknown, it should be set to -1. */ static void -MHD_parse_connection_headers(struct MHD_Connection * connection) { - char * last; - char * line; - char * colon; - char * tmp; - const char * clen; - const char * end; +MHD_parse_connection_headers (struct MHD_Connection *connection) +{ + char *last; + char *line; + char *colon; + char *tmp; + const char *clen; + const char *end; unsigned long long cval; if (connection->bodyReceived == 1) - abort(); + abort (); last = NULL; - while (NULL != (line = MHD_get_next_header_line(connection))) { - if (last != NULL) { - if ( (line[0] == ' ') || - (line[0] == '\t') ) { - /* value was continued on the next line, see - http://www.jmarshall.com/easy/http/ */ - last = MHD_pool_reallocate(connection->pool, - last, - strlen(last)+1, - strlen(line) + strlen(last) + 1); - if (last == NULL) { - MHD_excessive_data_handler(connection, - MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); - break; - } - tmp = line; - while ( (tmp[0] == ' ') || - (tmp[0] == '\t') ) - tmp++; /* skip whitespace at start of 2nd line */ - strcat(last, tmp); - continue; /* possibly more than 2 lines... */ - } else { - if (MHD_NO == MHD_connection_add_header(connection, - last, - colon, - MHD_HEADER_KIND)) - return; - last = NULL; - } + while (NULL != (line = MHD_get_next_header_line (connection))) + { + if (last != NULL) + { + if ((line[0] == ' ') || (line[0] == '\t')) + { + /* value was continued on the next line, see + http://www.jmarshall.com/easy/http/ */ + last = MHD_pool_reallocate (connection->pool, + last, + strlen (last) + 1, + strlen (line) + strlen (last) + 1); + if (last == NULL) + { + MHD_excessive_data_handler (connection, + MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); + break; + } + tmp = line; + while ((tmp[0] == ' ') || (tmp[0] == '\t')) + tmp++; /* skip whitespace at start of 2nd line */ + strcat (last, tmp); + continue; /* possibly more than 2 lines... */ + } + else + { + if (MHD_NO == MHD_connection_add_header (connection, + last, + colon, + MHD_HEADER_KIND)) + return; + last = NULL; + } + } + if (connection->url == NULL) + { + /* line must be request line (first line of header) */ + if (MHD_NO == parse_initial_message_line (connection, line)) + goto DIE; + continue; + } + /* check if this is the end of the header */ + if (strlen (line) == 0) + { + /* end of header */ + connection->headersReceived = 1; + clen = MHD_lookup_connection_value (connection, + MHD_HEADER_KIND, + MHD_HTTP_HEADER_CONTENT_LENGTH); + if (clen != NULL) + { + if (1 != sscanf (clen, "%llu", &cval)) + { + MHD_DLOG (connection->daemon, + "Failed to parse `%s' header `%s', closing connection.\n", + MHD_HTTP_HEADER_CONTENT_LENGTH, clen); + goto DIE; + } + connection->uploadSize = cval; + connection->bodyReceived = cval == 0 ? 1 : 0; + } + else + { + if (NULL == MHD_lookup_connection_value (connection, + MHD_HEADER_KIND, + MHD_HTTP_HEADER_TRANSFER_ENCODING)) + { + /* this request does not have a body */ + connection->uploadSize = 0; + connection->bodyReceived = 1; + } + else + { + connection->uploadSize = -1; /* unknown size */ + connection->bodyReceived = 0; + } + } + end = MHD_lookup_connection_value (connection, + MHD_HEADER_KIND, + MHD_HTTP_HEADER_CONNECTION); + if ((end != NULL) && (0 == strcasecmp (end, "close"))) + { + /* other side explicitly requested + that we close the connection after + this request */ + connection->read_close = MHD_YES; + } + break; + } + /* line should be normal header line, find colon */ + colon = strstr (line, ":"); + if (colon == NULL) + { + /* error in header line, die hard */ + MHD_DLOG (connection->daemon, + "Received malformed line (no colon), closing connection.\n"); + goto DIE; + } + /* zero-terminate header */ + colon[0] = '\0'; + colon++; /* advance to value */ + while ((colon[0] != '\0') && ((colon[0] == ' ') || (colon[0] == '\t'))) + colon++; + /* we do the actual adding of the connection + header at the beginning of the while + loop since we need to be able to inspect + the *next* header line (in case it starts + with a space...) */ + last = line; } - if (connection->url == NULL) { - /* line must be request line (first line of header) */ - if (MHD_NO == parse_initial_message_line(connection, - line)) - goto DIE; - continue; - } - /* check if this is the end of the header */ - if (strlen(line) == 0) { - /* end of header */ - connection->headersReceived = 1; - clen = MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_CONTENT_LENGTH); - if (clen != NULL) { - if (1 != sscanf(clen, - "%llu", - &cval)) { - MHD_DLOG(connection->daemon, - "Failed to parse `%s' header `%s', closing connection.\n", - MHD_HTTP_HEADER_CONTENT_LENGTH, - clen); - goto DIE; - } - connection->uploadSize = cval; - connection->bodyReceived = cval == 0 ? 1 : 0; - } else { - if (NULL == MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_TRANSFER_ENCODING)) { - /* this request does not have a body */ - connection->uploadSize = 0; - connection->bodyReceived = 1; - } else { - connection->uploadSize = -1; /* unknown size */ - connection->bodyReceived = 0; - } - } - end = MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_CONNECTION); - if ( (end != NULL) && - (0 == strcasecmp(end, - "close")) ) { - /* other side explicitly requested - that we close the connection after - this request */ - connection->read_close = MHD_YES; - } - break; - } - /* line should be normal header line, find colon */ - colon = strstr(line, ":"); - if (colon == NULL) { - /* error in header line, die hard */ - MHD_DLOG(connection->daemon, - "Received malformed line (no colon), closing connection.\n"); - goto DIE; - } - /* zero-terminate header */ - colon[0] = '\0'; - colon++; /* advance to value */ - while ( (colon[0] != '\0') && - ( (colon[0] == ' ') || - (colon[0] == '\t') ) ) - colon++; - /* we do the actual adding of the connection - header at the beginning of the while - loop since we need to be able to inspect - the *next* header line (in case it starts - with a space...) */ - last = line; - } - if ( (last != NULL) && - (MHD_NO == MHD_connection_add_header(connection, - last, - colon, - MHD_HEADER_KIND)) ) - return; /* error */ - MHD_parse_cookie_header(connection); + if ((last != NULL) && + (MHD_NO == MHD_connection_add_header (connection, + last, colon, MHD_HEADER_KIND))) + return; /* error */ + MHD_parse_cookie_header (connection); return; - DIE: - CLOSE(connection->socket_fd); +DIE: + CLOSE (connection->socket_fd); connection->socket_fd = -1; } @@ -635,16 +640,17 @@ MHD_parse_connection_headers(struct MHD_Connection * connection) { * Find the handler responsible for this request. */ static struct MHD_Access_Handler * -MHD_find_access_handler(struct MHD_Connection * connection) { - struct MHD_Access_Handler * pos; +MHD_find_access_handler (struct MHD_Connection *connection) +{ + struct MHD_Access_Handler *pos; pos = connection->daemon->handlers; - while (pos != NULL) { - if (0 == strcmp(connection->url, - pos->uri_prefix)) - return pos; - pos = pos->next; - } + while (pos != NULL) + { + if (0 == strcmp (connection->url, pos->uri_prefix)) + return pos; + pos = pos->next; + } return &connection->daemon->default_handler; } @@ -659,33 +665,33 @@ MHD_find_access_handler(struct MHD_Connection * connection) { * @return MHD_YES if so */ static int -MHD_test_post_data(struct MHD_Connection * connection) { - const char * encoding; - void * buf; +MHD_test_post_data (struct MHD_Connection *connection) +{ + const char *encoding; + void *buf; - if ( (connection->method == NULL) || - (connection->response != NULL) || - (0 != strcasecmp(connection->method, - MHD_HTTP_METHOD_POST)) ) + if ((connection->method == NULL) || + (connection->response != NULL) || + (0 != strcasecmp (connection->method, MHD_HTTP_METHOD_POST))) return MHD_NO; - encoding = MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_CONTENT_TYPE); - if (encoding == NULL) - return MHD_NO; - if ( (0 == strcasecmp(MHD_HTTP_POST_ENCODING_FORM_URLENCODED, - encoding)) && - (connection->uploadSize != -1) ) { - buf = MHD_pool_reallocate(connection->pool, - connection->read_buffer, - connection->read_buffer_size, - connection->uploadSize + 1); - if (buf == NULL) - return MHD_NO; - connection->read_buffer_size = connection->uploadSize + 1; - connection->read_buffer = buf; - return MHD_YES; - } + encoding = MHD_lookup_connection_value (connection, + MHD_HEADER_KIND, + MHD_HTTP_HEADER_CONTENT_TYPE); + if (encoding == NULL) + return MHD_NO; + if ((0 == strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, + encoding)) && (connection->uploadSize != -1)) + { + buf = MHD_pool_reallocate (connection->pool, + connection->read_buffer, + connection->read_buffer_size, + connection->uploadSize + 1); + if (buf == NULL) + return MHD_NO; + connection->read_buffer_size = connection->uploadSize + 1; + connection->read_buffer = buf; + return MHD_YES; + } return MHD_NO; } @@ -704,47 +710,47 @@ MHD_test_post_data(struct MHD_Connection * connection) { * memory). */ static int -MHD_parse_post_data(struct MHD_Connection * connection) { - const char * encoding; +MHD_parse_post_data (struct MHD_Connection *connection) +{ + const char *encoding; int ret; - encoding = MHD_lookup_connection_value(connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_CONTENT_TYPE); - if (encoding == NULL) - return MHD_NO; - if (0 == strcasecmp(MHD_HTTP_POST_ENCODING_FORM_URLENCODED, - encoding)) { - ret = parse_arguments(MHD_POSTDATA_KIND, - connection, - connection->read_buffer); - /* invalidate read buffer for other uses -- - in particular, do not give it to the - client; if this were to be needed, we would - have to make a copy, which would double memory - requirements */ - connection->read_buffer_size = 0; - connection->readLoc = 0; - connection->uploadSize = 0; - connection->read_buffer = NULL; - return ret; - } - if (0 == strcasecmp(MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, - encoding)) { - /* this code should never been reached right now, - since the test_post_data function would already - return MHD_NO; code is here only for future - extensions... */ - /* see http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 */ - MHD_DLOG(connection->daemon, - "Unsupported multipart encoding of POST data specified, not processing POST data.\n"); + encoding = MHD_lookup_connection_value (connection, + MHD_HEADER_KIND, + MHD_HTTP_HEADER_CONTENT_TYPE); + if (encoding == NULL) return MHD_NO; - } + if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding)) + { + ret = parse_arguments (MHD_POSTDATA_KIND, + connection, connection->read_buffer); + /* invalidate read buffer for other uses -- + in particular, do not give it to the + client; if this were to be needed, we would + have to make a copy, which would double memory + requirements */ + connection->read_buffer_size = 0; + connection->readLoc = 0; + connection->uploadSize = 0; + connection->read_buffer = NULL; + return ret; + } + if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, encoding)) + { + /* this code should never been reached right now, + since the test_post_data function would already + return MHD_NO; code is here only for future + extensions... */ + /* see http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 */ + MHD_DLOG (connection->daemon, + "Unsupported multipart encoding of POST data specified, not processing POST data.\n"); + return MHD_NO; + } /* this should never be reached, just here for error checking */ - MHD_DLOG(connection->daemon, - "Unknown encoding of POST data specified, not processing POST data.\n"); - return MHD_NO; + MHD_DLOG (connection->daemon, + "Unknown encoding of POST data specified, not processing POST data.\n"); + return MHD_NO; } /** @@ -752,46 +758,47 @@ MHD_parse_post_data(struct MHD_Connection * connection) { * connection. */ void -MHD_call_connection_handler(struct MHD_Connection * connection) { - struct MHD_Access_Handler * ah; +MHD_call_connection_handler (struct MHD_Connection *connection) +{ + struct MHD_Access_Handler *ah; unsigned int processed; if (connection->response != NULL) - return; /* already queued a response */ + return; /* already queued a response */ if (connection->headersReceived == 0) - abort(); /* bad timing... */ - ah = MHD_find_access_handler(connection); + abort (); /* bad timing... */ + ah = MHD_find_access_handler (connection); processed = connection->readLoc; - if (MHD_NO == ah->dh(ah->dh_cls, - connection, - connection->url, - connection->method, - connection->version, - connection->read_buffer, - &processed)) { - /* serios internal error, close connection */ - MHD_DLOG(connection->daemon, - "Internal application error, closing connection.\n"); - CLOSE(connection->socket_fd); - connection->socket_fd = -1; - return; - } + if (MHD_NO == ah->dh (ah->dh_cls, + connection, + connection->url, + connection->method, + connection->version, + connection->read_buffer, &processed)) + { + /* serios internal error, close connection */ + MHD_DLOG (connection->daemon, + "Internal application error, closing connection.\n"); + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + return; + } /* dh left "processed" bytes in buffer for next time... */ - memmove(connection->read_buffer, - &connection->read_buffer[connection->readLoc - processed], - processed); + memmove (connection->read_buffer, + &connection->read_buffer[connection->readLoc - processed], + processed); if (connection->uploadSize != -1) connection->uploadSize -= (connection->readLoc - processed); connection->readLoc = processed; - if ( (connection->uploadSize == 0) || - ( (connection->readLoc == 0) && - (connection->uploadSize == -1) && - (connection->socket_fd == -1) ) ) { - connection->bodyReceived = 1; - connection->readLoc = 0; - connection->read_buffer_size = 0; - connection->read_buffer = NULL; - } + if ((connection->uploadSize == 0) || + ((connection->readLoc == 0) && + (connection->uploadSize == -1) && (connection->socket_fd == -1))) + { + connection->bodyReceived = 1; + connection->readLoc = 0; + connection->read_buffer_size = 0; + connection->read_buffer = NULL; + } } @@ -802,81 +809,88 @@ MHD_call_connection_handler(struct MHD_Connection * connection) { * to handle reads. */ int -MHD_connection_handle_read(struct MHD_Connection * connection) { +MHD_connection_handle_read (struct MHD_Connection *connection) +{ int bytes_read; - void * tmp; + void *tmp; if (connection->pool == NULL) - connection->pool = MHD_pool_create(connection->daemon->pool_size); - if (connection->pool == NULL) { - MHD_DLOG(connection->daemon, - "Failed to create memory pool!\n"); - CLOSE(connection->socket_fd); - connection->socket_fd = -1; - return MHD_NO; - } - if ( (connection->readLoc >= connection->read_buffer_size) && - (connection->headersReceived == 0) ) { - /* need to grow read buffer */ - tmp = MHD_pool_reallocate(connection->pool, - connection->read_buffer, - connection->read_buffer_size, - connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE); - if (tmp == NULL) { - MHD_DLOG(connection->daemon, - "Not enough memory for reading headers!\n"); - MHD_excessive_data_handler(connection, - MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); + connection->pool = MHD_pool_create (connection->daemon->pool_size); + if (connection->pool == NULL) + { + MHD_DLOG (connection->daemon, "Failed to create memory pool!\n"); + CLOSE (connection->socket_fd); + connection->socket_fd = -1; return MHD_NO; } - connection->read_buffer = tmp; - connection->read_buffer_size = connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE; - } - if (connection->readLoc >= connection->read_buffer_size) { - MHD_DLOG(connection->daemon, - "Unexpected call to %s.\n", - __FUNCTION__); - return MHD_NO; - } - bytes_read = RECV(connection->socket_fd, - &connection->read_buffer[connection->readLoc], - connection->read_buffer_size - connection->readLoc, - 0); - if (bytes_read < 0) { - if (errno == EINTR) + if ((connection->readLoc >= connection->read_buffer_size) && + (connection->headersReceived == 0)) + { + /* need to grow read buffer */ + tmp = MHD_pool_reallocate (connection->pool, + connection->read_buffer, + connection->read_buffer_size, + connection->read_buffer_size * 2 + + MHD_BUF_INC_SIZE); + if (tmp == NULL) + { + MHD_DLOG (connection->daemon, + "Not enough memory for reading headers!\n"); + MHD_excessive_data_handler (connection, + MHD_HTTP_REQUEST_ENTITY_TOO_LARGE); + return MHD_NO; + } + connection->read_buffer = tmp; + connection->read_buffer_size = + connection->read_buffer_size * 2 + MHD_BUF_INC_SIZE; + } + if (connection->readLoc >= connection->read_buffer_size) + { + MHD_DLOG (connection->daemon, "Unexpected call to %s.\n", __FUNCTION__); return MHD_NO; - MHD_DLOG(connection->daemon, - "Failed to receive data: %s\n", - STRERROR(errno)); - CLOSE(connection->socket_fd); - connection->socket_fd = -1; - return MHD_YES; - } - if (bytes_read == 0) { - /* other side closed connection */ - connection->read_close = MHD_YES; - if (connection->readLoc > 0) - MHD_call_connection_handler(connection); - shutdown(connection->socket_fd, SHUT_RD); - return MHD_YES; - } + } + bytes_read = RECV (connection->socket_fd, + &connection->read_buffer[connection->readLoc], + connection->read_buffer_size - connection->readLoc, 0); + if (bytes_read < 0) + { + if (errno == EINTR) + return MHD_NO; + MHD_DLOG (connection->daemon, + "Failed to receive data: %s\n", STRERROR (errno)); + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + return MHD_YES; + } + if (bytes_read == 0) + { + /* other side closed connection */ + connection->read_close = MHD_YES; + if (connection->readLoc > 0) + MHD_call_connection_handler (connection); + shutdown (connection->socket_fd, SHUT_RD); + return MHD_YES; + } connection->readLoc += bytes_read; - if (connection->headersReceived == 0) { - MHD_parse_connection_headers(connection); - if (connection->headersReceived == 1) { - connection->post_processed = MHD_test_post_data(connection); + if (connection->headersReceived == 0) + { + MHD_parse_connection_headers (connection); + if (connection->headersReceived == 1) + { + connection->post_processed = MHD_test_post_data (connection); + } + } + if (connection->headersReceived == 1) + { + if ((connection->post_processed == MHD_YES) && + (connection->uploadSize == connection->readLoc)) + if (MHD_NO == MHD_parse_post_data (connection)) + connection->post_processed = MHD_NO; + if (((connection->post_processed == MHD_NO) || + (connection->read_buffer_size == connection->readLoc)) && + (connection->method != NULL)) + MHD_call_connection_handler (connection); } - } - if (connection->headersReceived == 1) { - if ( (connection->post_processed == MHD_YES) && - (connection->uploadSize == connection->readLoc) ) - if (MHD_NO == MHD_parse_post_data(connection)) - connection->post_processed = MHD_NO; - if ( ( (connection->post_processed == MHD_NO) || - (connection->read_buffer_size == connection->readLoc) ) && - (connection->method != NULL) ) - MHD_call_connection_handler(connection); - } return MHD_YES; } @@ -885,48 +899,51 @@ MHD_connection_handle_read(struct MHD_Connection * connection) { * for http-compiliance. */ static void -MHD_add_extra_headers(struct MHD_Connection * connection) { - const char * have; +MHD_add_extra_headers (struct MHD_Connection *connection) +{ + const char *have; char buf[128]; - if (connection->response->total_size == -1) { - have = MHD_get_response_header(connection->response, - MHD_HTTP_HEADER_CONNECTION); - if (have == NULL) - MHD_add_response_header(connection->response, - MHD_HTTP_HEADER_CONNECTION, - "close"); - } else if (NULL == MHD_get_response_header(connection->response, - MHD_HTTP_HEADER_CONTENT_LENGTH)) { - _REAL_SNPRINTF(buf, - 128, - "%llu", - (unsigned long long) connection->response->total_size); - MHD_add_response_header(connection->response, - MHD_HTTP_HEADER_CONTENT_LENGTH, - buf); - } + if (connection->response->total_size == -1) + { + have = MHD_get_response_header (connection->response, + MHD_HTTP_HEADER_CONNECTION); + if (have == NULL) + MHD_add_response_header (connection->response, + MHD_HTTP_HEADER_CONNECTION, "close"); + } + else if (NULL == MHD_get_response_header (connection->response, + MHD_HTTP_HEADER_CONTENT_LENGTH)) + { + _REAL_SNPRINTF (buf, + 128, + "%llu", + (unsigned long long) connection->response->total_size); + MHD_add_response_header (connection->response, + MHD_HTTP_HEADER_CONTENT_LENGTH, buf); + } } -static void get_date_string(char * date, - unsigned int max) { - static const char * days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - static const char * mons[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +static void +get_date_string (char *date, unsigned int max) +{ + static const char *days[] = + { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + static const char *mons[] = + { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", +"Nov", "Dec" }; struct tm now; time_t t; - time(&t); - gmtime_r(&t, &now); - snprintf(date, - max-1, - "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n", - days[now.tm_wday % 7], - now.tm_mday, - mons[now.tm_mon % 12], - now.tm_year, - now.tm_hour, - now.tm_min, - now.tm_sec); + time (&t); + gmtime_r (&t, &now); + snprintf (date, + max - 1, + "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n", + days[now.tm_wday % 7], + now.tm_mday, + mons[now.tm_mon % 12], + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec); } /** @@ -935,62 +952,53 @@ static void get_date_string(char * date, * HTTPd's response. */ static int -MHD_build_header_response(struct MHD_Connection * connection) { +MHD_build_header_response (struct MHD_Connection *connection) +{ size_t size; size_t off; - struct MHD_HTTP_Header * pos; + struct MHD_HTTP_Header *pos; char code[32]; char date[128]; - char * data; + char *data; - MHD_add_extra_headers(connection); - SPRINTF(code, - "%s %u\r\n", - MHD_HTTP_VERSION_1_1, - connection->responseCode); - off = strlen(code); + MHD_add_extra_headers (connection); + SPRINTF (code, "%s %u\r\n", MHD_HTTP_VERSION_1_1, connection->responseCode); + off = strlen (code); /* estimate size */ - size = off + 2; /* extra \r\n at the end */ + size = off + 2; /* extra \r\n at the end */ pos = connection->response->first_header; - while (pos != NULL) { - size += strlen(pos->header) + strlen(pos->value) + 4; /* colon, space, linefeeds */ - pos = pos->next; - } - if (NULL == MHD_get_response_header(connection->response, - MHD_HTTP_HEADER_DATE)) - get_date_string(date, sizeof(date)); + while (pos != NULL) + { + size += strlen (pos->header) + strlen (pos->value) + 4; /* colon, space, linefeeds */ + pos = pos->next; + } + if (NULL == MHD_get_response_header (connection->response, + MHD_HTTP_HEADER_DATE)) + get_date_string (date, sizeof (date)); else - date[0] = '\0'; - size += strlen(date); + date[0] = '\0'; + size += strlen (date); /* produce data */ - data = MHD_pool_allocate(connection->pool, - size + 1, - MHD_YES); - if (data == NULL) { - MHD_DLOG(connection->daemon, - "Not enough memory for write!\n"); - return MHD_NO; - } - memcpy(data, - code, - off); + data = MHD_pool_allocate (connection->pool, size + 1, MHD_YES); + if (data == NULL) + { + MHD_DLOG (connection->daemon, "Not enough memory for write!\n"); + return MHD_NO; + } + memcpy (data, code, off); pos = connection->response->first_header; - while (pos != NULL) { - SPRINTF(&data[off], - "%s: %s\r\n", - pos->header, - pos->value); - off += strlen(pos->header) + strlen(pos->value) + 4; - pos = pos->next; - } - strcpy(&data[off], - date); - off += strlen(date); - sprintf(&data[off], - "\r\n"); + while (pos != NULL) + { + SPRINTF (&data[off], "%s: %s\r\n", pos->header, pos->value); + off += strlen (pos->header) + strlen (pos->value) + 4; + pos = pos->next; + } + strcpy (&data[off], date); + off += strlen (date); + sprintf (&data[off], "\r\n"); off += 2; if (off != size) - abort(); + abort (); connection->write_buffer = data; connection->writeLoc = size; connection->writePos = 0; @@ -1005,159 +1013,164 @@ MHD_build_header_response(struct MHD_Connection * connection) { * call this function */ int -MHD_connection_handle_write(struct MHD_Connection * connection) { - struct MHD_Response * response; +MHD_connection_handle_write (struct MHD_Connection *connection) +{ + struct MHD_Response *response; int ret; - if (MHD_need_100_continue(connection)) { - ret = SEND(connection->socket_fd, - &HTTP_100_CONTINUE[connection->continuePos], - strlen(HTTP_100_CONTINUE) - connection->continuePos, - 0); - if (ret < 0) { - if (errno == EINTR) - return MHD_YES; - MHD_DLOG(connection->daemon, - "Failed to send data: %s\n", - STRERROR(errno)); - CLOSE(connection->socket_fd); - connection->socket_fd = -1; + if (MHD_need_100_continue (connection)) + { + ret = SEND (connection->socket_fd, + &HTTP_100_CONTINUE[connection->continuePos], + strlen (HTTP_100_CONTINUE) - connection->continuePos, 0); + if (ret < 0) + { + if (errno == EINTR) + return MHD_YES; + MHD_DLOG (connection->daemon, + "Failed to send data: %s\n", STRERROR (errno)); + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + return MHD_YES; + } + connection->continuePos += ret; return MHD_YES; } - connection->continuePos += ret; - return MHD_YES; - } response = connection->response; - if(response == NULL) { - MHD_DLOG(connection->daemon, - "Unexpected call to %s.\n", - __FUNCTION__); - return MHD_NO; - } - if (! connection->headersSent) { - if ( (connection->write_buffer == NULL) && - (MHD_NO == MHD_build_header_response(connection)) ) { - /* oops - close! */ - CLOSE(connection->socket_fd); - connection->socket_fd = -1; - return MHD_NO; + if (response == NULL) + { + MHD_DLOG (connection->daemon, "Unexpected call to %s.\n", __FUNCTION__); + return MHD_NO; } - ret = SEND(connection->socket_fd, - &connection->write_buffer[connection->writePos], - connection->writeLoc - connection->writePos, - 0); - if (ret < 0) { - if (errno == EINTR) - return MHD_YES; - MHD_DLOG(connection->daemon, - "Failed to send data: %s\n", - STRERROR(errno)); - CLOSE(connection->socket_fd); - connection->socket_fd = -1; + if (!connection->headersSent) + { + if ((connection->write_buffer == NULL) && + (MHD_NO == MHD_build_header_response (connection))) + { + /* oops - close! */ + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + return MHD_NO; + } + ret = SEND (connection->socket_fd, + &connection->write_buffer[connection->writePos], + connection->writeLoc - connection->writePos, 0); + if (ret < 0) + { + if (errno == EINTR) + return MHD_YES; + MHD_DLOG (connection->daemon, + "Failed to send data: %s\n", STRERROR (errno)); + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + return MHD_YES; + } + connection->writePos += ret; + if (connection->writeLoc == connection->writePos) + { + connection->writeLoc = 0; + connection->writePos = 0; + connection->headersSent = 1; + MHD_pool_reallocate (connection->pool, + connection->write_buffer, + connection->write_buffer_size, 0); + connection->write_buffer = NULL; + connection->write_buffer_size = 0; + } return MHD_YES; } - connection->writePos += ret; - if (connection->writeLoc == connection->writePos) { - connection->writeLoc = 0; - connection->writePos = 0; - connection->headersSent = 1; - MHD_pool_reallocate(connection->pool, - connection->write_buffer, - connection->write_buffer_size, - 0); - connection->write_buffer = NULL; - connection->write_buffer_size = 0; - } - return MHD_YES; - } if (response->total_size < connection->messagePos) - abort(); /* internal error */ + abort (); /* internal error */ if (response->crc != NULL) - pthread_mutex_lock(&response->mutex); + pthread_mutex_lock (&response->mutex); - /* prepare send buffer */ - if ( (response->crc != NULL) && - ( (response->data_start > connection->messagePos) || - (response->data_start + response->data_size <= connection->messagePos) ) ) { - ret = response->crc(response->crc_cls, - connection->messagePos, - response->data, - MIN(response->data_buffer_size, - response->total_size - connection->messagePos)); - if (ret == -1) { - /* end of message, signal other side by closing! */ - response->total_size = connection->messagePos; - CLOSE(connection->socket_fd); - connection->socket_fd = -1; - if (response->crc != NULL) - pthread_mutex_unlock(&response->mutex); - return MHD_YES; - } - response->data_start = connection->messagePos; - response->data_size = ret; - if (ret == 0) { - if (response->crc != NULL) - pthread_mutex_unlock(&response->mutex); - return MHD_YES; + /* prepare send buffer */ + if ((response->crc != NULL) && + ((response->data_start > connection->messagePos) || + (response->data_start + response->data_size <= + connection->messagePos))) + { + ret = response->crc (response->crc_cls, + connection->messagePos, + response->data, + MIN (response->data_buffer_size, + response->total_size - + connection->messagePos)); + if (ret == -1) + { + /* end of message, signal other side by closing! */ + response->total_size = connection->messagePos; + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + if (response->crc != NULL) + pthread_mutex_unlock (&response->mutex); + return MHD_YES; + } + response->data_start = connection->messagePos; + response->data_size = ret; + if (ret == 0) + { + if (response->crc != NULL) + pthread_mutex_unlock (&response->mutex); + return MHD_YES; + } } - } /* transmit */ - ret = SEND(connection->socket_fd, - &response->data[connection->messagePos - response->data_start], - response->data_size - (connection->messagePos - response->data_start), - 0); + ret = SEND (connection->socket_fd, + &response->data[connection->messagePos - response->data_start], + response->data_size - (connection->messagePos - + response->data_start), 0); if (response->crc != NULL) - pthread_mutex_unlock(&response->mutex); - if (ret < 0) { - if (errno == EINTR) + pthread_mutex_unlock (&response->mutex); + if (ret < 0) + { + if (errno == EINTR) + return MHD_YES; + MHD_DLOG (connection->daemon, + "Failed to send data: %s\n", STRERROR (errno)); + CLOSE (connection->socket_fd); + connection->socket_fd = -1; return MHD_YES; - MHD_DLOG(connection->daemon, - "Failed to send data: %s\n", - STRERROR(errno)); - CLOSE(connection->socket_fd); - connection->socket_fd = -1; - return MHD_YES; - } + } connection->messagePos += ret; if (connection->messagePos > response->total_size) - abort(); /* internal error */ - if (connection->messagePos == response->total_size) { - if ( (connection->bodyReceived == 0) || - (connection->headersReceived == 0) ) - abort(); /* internal error */ - MHD_destroy_response(response); - connection->continuePos = 0; - connection->responseCode = 0; - connection->response = NULL; - connection->headers_received = NULL; - connection->headersReceived = 0; - connection->headersSent = 0; - connection->bodyReceived = 0; - connection->messagePos = 0; - connection->method = NULL; - connection->url = NULL; - if ( (connection->read_close == MHD_YES) || - (0 != strcasecmp(MHD_HTTP_VERSION_1_1, - connection->version)) ) { - /* closed for reading => close for good! */ - if (connection->socket_fd != -1) - CLOSE(connection->socket_fd); - connection->socket_fd = -1; + abort (); /* internal error */ + if (connection->messagePos == response->total_size) + { + if ((connection->bodyReceived == 0) || + (connection->headersReceived == 0)) + abort (); /* internal error */ + MHD_destroy_response (response); + connection->continuePos = 0; + connection->responseCode = 0; + connection->response = NULL; + connection->headers_received = NULL; + connection->headersReceived = 0; + connection->headersSent = 0; + connection->bodyReceived = 0; + connection->messagePos = 0; + connection->method = NULL; + connection->url = NULL; + if ((connection->read_close == MHD_YES) || + (0 != strcasecmp (MHD_HTTP_VERSION_1_1, connection->version))) + { + /* closed for reading => close for good! */ + if (connection->socket_fd != -1) + CLOSE (connection->socket_fd); + connection->socket_fd = -1; + } + connection->version = NULL; + connection->read_buffer = NULL; + connection->write_buffer = NULL; + connection->read_buffer_size = 0; + connection->readLoc = 0; + connection->write_buffer_size = 0; + connection->writePos = 0; + connection->writeLoc = 0; + MHD_pool_destroy (connection->pool); + connection->pool = NULL; } - connection->version = NULL; - connection->read_buffer = NULL; - connection->write_buffer = NULL; - connection->read_buffer_size = 0; - connection->readLoc = 0; - connection->write_buffer_size = 0; - connection->writePos = 0; - connection->writeLoc = 0; - MHD_pool_destroy(connection->pool); - connection->pool = NULL; - } return MHD_YES; } /* end of connection.c */ - - diff --git a/src/daemon/connection.h b/src/daemon/connection.h @@ -35,19 +35,17 @@ * @return MHD_YES on success */ int -MHD_connection_get_fdset(struct MHD_Connection * connection, - fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - int * max_fd); +MHD_connection_get_fdset (struct MHD_Connection *connection, + fd_set * read_fd_set, + fd_set * write_fd_set, + fd_set * except_fd_set, int *max_fd); /** * Call the handler of the application for this * connection. */ -void -MHD_call_connection_handler(struct MHD_Connection * connection); +void MHD_call_connection_handler (struct MHD_Connection *connection); /** * This function handles a particular connection when it has been @@ -55,8 +53,7 @@ MHD_call_connection_handler(struct MHD_Connection * connection); * (multithreaded, external select, internal select) call this function * to handle reads. */ -int -MHD_connection_handle_read(struct MHD_Connection * connection); +int MHD_connection_handle_read (struct MHD_Connection *connection); /** @@ -65,8 +62,7 @@ MHD_connection_handle_read(struct MHD_Connection * connection); * to be written, however, the function call does nothing. All implementations * (multithreaded, external select, internal select) call this function */ -int -MHD_connection_handle_write(struct MHD_Connection * connection); +int MHD_connection_handle_write (struct MHD_Connection *connection); #endif diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c @@ -48,30 +48,28 @@ * already exists */ int -MHD_register_handler(struct MHD_Daemon * daemon, - const char * uri_prefix, - MHD_AccessHandlerCallback dh, - void * dh_cls) { - struct MHD_Access_Handler * ah; - - if ( (daemon == NULL) || - (uri_prefix == NULL) || - (dh == NULL) ) - return MHD_NO; +MHD_register_handler (struct MHD_Daemon *daemon, + const char *uri_prefix, + MHD_AccessHandlerCallback dh, void *dh_cls) +{ + struct MHD_Access_Handler *ah; + + if ((daemon == NULL) || (uri_prefix == NULL) || (dh == NULL)) + return MHD_NO; ah = daemon->handlers; - while (ah != NULL) { - if (0 == strcmp(uri_prefix, - ah->uri_prefix)) - return MHD_NO; - ah = ah->next; - } - ah = malloc(sizeof(struct MHD_Access_Handler)); + while (ah != NULL) + { + if (0 == strcmp (uri_prefix, ah->uri_prefix)) + return MHD_NO; + ah = ah->next; + } + ah = malloc (sizeof (struct MHD_Access_Handler)); ah->next = daemon->handlers; - ah->uri_prefix = strdup(uri_prefix); + ah->uri_prefix = strdup (uri_prefix); ah->dh = dh; ah->dh_cls = dh_cls; daemon->handlers = ah; - return MHD_YES; + return MHD_YES; } @@ -84,34 +82,33 @@ MHD_register_handler(struct MHD_Daemon * daemon, * is not known for this daemon */ int -MHD_unregister_handler(struct MHD_Daemon * daemon, - const char * uri_prefix, - MHD_AccessHandlerCallback dh, - void * dh_cls) { - struct MHD_Access_Handler * prev; - struct MHD_Access_Handler * pos; - - if ( (daemon == NULL) || - (uri_prefix == NULL) || - (dh == NULL) ) - return MHD_NO; +MHD_unregister_handler (struct MHD_Daemon *daemon, + const char *uri_prefix, + MHD_AccessHandlerCallback dh, void *dh_cls) +{ + struct MHD_Access_Handler *prev; + struct MHD_Access_Handler *pos; + + if ((daemon == NULL) || (uri_prefix == NULL) || (dh == NULL)) + return MHD_NO; pos = daemon->handlers; prev = NULL; - while (pos != NULL) { - if ( (dh == pos->dh) && - (dh_cls == pos->dh_cls) && - (0 == strcmp(uri_prefix, - pos->uri_prefix)) ) { - if (prev == NULL) - daemon->handlers = pos->next; - else - prev->next = pos->next; - free(pos); - return MHD_YES; + while (pos != NULL) + { + if ((dh == pos->dh) && + (dh_cls == pos->dh_cls) && + (0 == strcmp (uri_prefix, pos->uri_prefix))) + { + if (prev == NULL) + daemon->handlers = pos->next; + else + prev->next = pos->next; + free (pos); + return MHD_YES; + } + prev = pos; + pos = pos->next; } - prev = pos; - pos = pos->next; - } return MHD_NO; } @@ -123,34 +120,32 @@ MHD_unregister_handler(struct MHD_Daemon * daemon, * options for this call. */ int -MHD_get_fdset(struct MHD_Daemon * daemon, - fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - int * max_fd) { - struct MHD_Connection * pos; - - if ( (daemon == NULL) || - (read_fd_set == NULL) || - (write_fd_set == NULL) || - (except_fd_set == NULL) || - (max_fd == NULL) || - ( (daemon->options & MHD_USE_THREAD_PER_CONNECTION) != 0) ) - return MHD_NO; - FD_SET(daemon->socket_fd, - read_fd_set); - if ( (*max_fd) < daemon->socket_fd) +MHD_get_fdset (struct MHD_Daemon *daemon, + fd_set * read_fd_set, + fd_set * write_fd_set, fd_set * except_fd_set, int *max_fd) +{ + struct MHD_Connection *pos; + + if ((daemon == NULL) || + (read_fd_set == NULL) || + (write_fd_set == NULL) || + (except_fd_set == NULL) || + (max_fd == NULL) || + ((daemon->options & MHD_USE_THREAD_PER_CONNECTION) != 0)) + return MHD_NO; + FD_SET (daemon->socket_fd, read_fd_set); + if ((*max_fd) < daemon->socket_fd) *max_fd = daemon->socket_fd; pos = daemon->connections; - while (pos != NULL) { - if (MHD_YES != MHD_connection_get_fdset(pos, - read_fd_set, - write_fd_set, - except_fd_set, - max_fd)) - return MHD_NO; - pos = pos->next; - } + while (pos != NULL) + { + if (MHD_YES != MHD_connection_get_fdset (pos, + read_fd_set, + write_fd_set, + except_fd_set, max_fd)) + return MHD_NO; + pos = pos->next; + } return MHD_YES; } @@ -160,8 +155,9 @@ MHD_get_fdset(struct MHD_Daemon * daemon, * connection. */ static void * -MHD_handle_connection(void * data) { - struct MHD_Connection * con = data; +MHD_handle_connection (void *data) +{ + struct MHD_Connection *con = data; int num_ready; fd_set rs; fd_set ws; @@ -169,42 +165,35 @@ MHD_handle_connection(void * data) { int max; if (con == NULL) - abort(); - while ( (! con->daemon->shutdown) && - (con->socket_fd != -1) ) { - FD_ZERO(&rs); - FD_ZERO(&ws); - FD_ZERO(&es); - max = 0; - MHD_connection_get_fdset(con, - &rs, - &ws, - &es, - &max); - num_ready = SELECT(max + 1, - &rs, - &ws, - &es, - NULL); - if (num_ready <= 0) { - if (errno == EINTR) - continue; - break; + abort (); + while ((!con->daemon->shutdown) && (con->socket_fd != -1)) + { + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + max = 0; + MHD_connection_get_fdset (con, &rs, &ws, &es, &max); + num_ready = SELECT (max + 1, &rs, &ws, &es, NULL); + if (num_ready <= 0) + { + if (errno == EINTR) + continue; + break; + } + if (((FD_ISSET (con->socket_fd, &rs)) && + (MHD_YES != MHD_connection_handle_read (con))) || + ((con->socket_fd != -1) && + (FD_ISSET (con->socket_fd, &ws)) && + (MHD_YES != MHD_connection_handle_write (con)))) + break; + if ((con->headersReceived == 1) && (con->response == NULL)) + MHD_call_connection_handler (con); + } + if (con->socket_fd != -1) + { + CLOSE (con->socket_fd); + con->socket_fd = -1; } - if ( ( (FD_ISSET(con->socket_fd, &rs)) && - (MHD_YES != MHD_connection_handle_read(con)) ) || - ( (con->socket_fd != -1) && - (FD_ISSET(con->socket_fd, &ws)) && - (MHD_YES != MHD_connection_handle_write(con)) ) ) - break; - if ( (con->headersReceived == 1) && - (con->response == NULL) ) - MHD_call_connection_handler(con); - } - if (con->socket_fd != -1) { - CLOSE(con->socket_fd); - con->socket_fd = -1; - } return NULL; } @@ -215,77 +204,66 @@ MHD_handle_connection(void * data) { * accept policy callback. */ static int -MHD_accept_connection(struct MHD_Daemon * daemon) { - struct MHD_Connection * connection; +MHD_accept_connection (struct MHD_Daemon *daemon) +{ + struct MHD_Connection *connection; struct sockaddr_in6 addr6; - struct sockaddr * addr = (struct sockaddr*) &addr6; + struct sockaddr *addr = (struct sockaddr *) &addr6; socklen_t addrlen; int s; - if (sizeof(struct sockaddr) > sizeof(struct sockaddr_in6)) - abort(); /* fatal, serious error */ - addrlen = sizeof(struct sockaddr_in6); - memset(addr, - 0, - sizeof(struct sockaddr_in6)); - s = ACCEPT(daemon->socket_fd, - addr, - &addrlen); - if ( (s < 0) || - (addrlen <= 0) ) { - MHD_DLOG(daemon, - "Error accepting connection: %s\n", - STRERROR(errno)); - if (s != -1) - CLOSE(s); /* just in case */ - return MHD_NO; - } - if (daemon->max_connections == 0) { - /* above connection limit - reject */ - CLOSE(s); - return MHD_NO; - } - if ( (daemon->apc != NULL) && - (MHD_NO == daemon->apc(daemon->apc_cls, - addr, - addrlen)) ) { - CLOSE(s); - return MHD_YES; - } - connection = malloc(sizeof(struct MHD_Connection)); - memset(connection, - 0, - sizeof(struct MHD_Connection)); + if (sizeof (struct sockaddr) > sizeof (struct sockaddr_in6)) + abort (); /* fatal, serious error */ + addrlen = sizeof (struct sockaddr_in6); + memset (addr, 0, sizeof (struct sockaddr_in6)); + s = ACCEPT (daemon->socket_fd, addr, &addrlen); + if ((s < 0) || (addrlen <= 0)) + { + MHD_DLOG (daemon, "Error accepting connection: %s\n", STRERROR (errno)); + if (s != -1) + CLOSE (s); /* just in case */ + return MHD_NO; + } + if (daemon->max_connections == 0) + { + /* above connection limit - reject */ + CLOSE (s); + return MHD_NO; + } + if ((daemon->apc != NULL) && + (MHD_NO == daemon->apc (daemon->apc_cls, addr, addrlen))) + { + CLOSE (s); + return MHD_YES; + } + connection = malloc (sizeof (struct MHD_Connection)); + memset (connection, 0, sizeof (struct MHD_Connection)); connection->pool = NULL; - connection->addr = malloc(addrlen); - if (connection->addr == NULL) { - CLOSE(s); - free(connection); - return MHD_NO; - } - memcpy(connection->addr, - addr, - addrlen); + connection->addr = malloc (addrlen); + if (connection->addr == NULL) + { + CLOSE (s); + free (connection); + return MHD_NO; + } + memcpy (connection->addr, addr, addrlen); connection->addr_len = addrlen; connection->socket_fd = s; connection->daemon = daemon; - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION) ) && - (0 != pthread_create(&connection->pid, - NULL, - &MHD_handle_connection, - connection)) ) { - MHD_DLOG(daemon, - "Failed to create a thread: %s\n", - STRERROR(errno)); - CLOSE(s); - free(connection->addr); - free(connection); - return MHD_NO; - } + if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && + (0 != pthread_create (&connection->pid, + NULL, &MHD_handle_connection, connection))) + { + MHD_DLOG (daemon, "Failed to create a thread: %s\n", STRERROR (errno)); + CLOSE (s); + free (connection->addr); + free (connection); + return MHD_NO; + } connection->next = daemon->connections; daemon->connections = connection; - daemon->max_connections--; + daemon->max_connections--; return MHD_YES; } @@ -301,43 +279,46 @@ MHD_accept_connection(struct MHD_Daemon * daemon) { * the upload data buffer is full). */ static void -MHD_cleanup_connections(struct MHD_Daemon * daemon) { - struct MHD_Connection * pos; - struct MHD_Connection * prev; - void * unused; +MHD_cleanup_connections (struct MHD_Daemon *daemon) +{ + struct MHD_Connection *pos; + struct MHD_Connection *prev; + void *unused; pos = daemon->connections; prev = NULL; - while (pos != NULL) { - if (pos->socket_fd == -1) { - if (prev == NULL) - daemon->connections = pos->next; - else - prev->next = pos->next; - if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - pthread_kill(pos->pid, SIGALRM); - pthread_join(pos->pid, &unused); - } - if (pos->response != NULL) - MHD_destroy_response(pos->response); - MHD_pool_destroy(pos->pool); - free(pos->addr); - free(pos); - daemon->max_connections++; - if (prev == NULL) - pos = daemon->connections; - else - pos = prev->next; - continue; + while (pos != NULL) + { + if (pos->socket_fd == -1) + { + if (prev == NULL) + daemon->connections = pos->next; + else + prev->next = pos->next; + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + { + pthread_kill (pos->pid, SIGALRM); + pthread_join (pos->pid, &unused); + } + if (pos->response != NULL) + MHD_destroy_response (pos->response); + MHD_pool_destroy (pos->pool); + free (pos->addr); + free (pos); + daemon->max_connections++; + if (prev == NULL) + pos = daemon->connections; + else + pos = prev->next; + continue; + } + + if ((pos->headersReceived == 1) && (pos->response == NULL)) + MHD_call_connection_handler (pos); + + prev = pos; + pos = pos->next; } - - if ( (pos->headersReceived == 1) && - (pos->response == NULL) ) - MHD_call_connection_handler(pos); - - prev = pos; - pos = pos->next; - } } @@ -348,9 +329,9 @@ MHD_cleanup_connections(struct MHD_Daemon * daemon) { * @return MHD_NO on serious errors, MHD_YES on success */ static int -MHD_select(struct MHD_Daemon * daemon, - int may_block) { - struct MHD_Connection * pos; +MHD_select (struct MHD_Daemon *daemon, int may_block) +{ + struct MHD_Connection *pos; int num_ready; fd_set rs; fd_set ws; @@ -361,61 +342,58 @@ MHD_select(struct MHD_Daemon * daemon, timeout.tv_sec = 0; timeout.tv_usec = 0; - if(daemon == NULL) - abort(); - FD_ZERO(&rs); - FD_ZERO(&ws); - FD_ZERO(&es); + if (daemon == NULL) + abort (); + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); max = 0; - if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - /* single-threaded, go over everything */ - if (MHD_NO == MHD_get_fdset(daemon, - &rs, - &ws, - &es, - &max)) + if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + { + /* single-threaded, go over everything */ + if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max)) + return MHD_NO; + } + else + { + /* accept only, have one thread per connection */ + max = daemon->socket_fd; + FD_SET (daemon->socket_fd, &rs); + } + num_ready = SELECT (max + 1, + &rs, &ws, &es, may_block == MHD_NO ? &timeout : NULL); + if (num_ready < 0) + { + if (errno == EINTR) + return MHD_YES; + MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); return MHD_NO; - } else { - /* accept only, have one thread per connection */ - max = daemon->socket_fd; - FD_SET(daemon->socket_fd, &rs); - } - num_ready = SELECT(max + 1, - &rs, - &ws, - &es, - may_block == MHD_NO ? &timeout : NULL); - if (num_ready < 0) { - if (errno == EINTR) - return MHD_YES; - MHD_DLOG(daemon, - "Select failed: %s\n", - STRERROR(errno)); - return MHD_NO; - } + } ds = daemon->socket_fd; if (ds == -1) return MHD_YES; - if (FD_ISSET(ds, - &rs)) - MHD_accept_connection(daemon); - if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - /* do not have a thread per connection, process all connections now */ - pos = daemon->connections; - while (pos != NULL) { - ds = pos->socket_fd; - if (ds == -1) { - pos = pos->next; - continue; - } - if (FD_ISSET(ds, &rs)) - MHD_connection_handle_read(pos); - if (FD_ISSET(ds, &ws)) - MHD_connection_handle_write(pos); - pos = pos->next; + if (FD_ISSET (ds, &rs)) + MHD_accept_connection (daemon); + if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + { + /* do not have a thread per connection, process all connections now */ + pos = daemon->connections; + while (pos != NULL) + { + ds = pos->socket_fd; + if (ds == -1) + { + pos = pos->next; + continue; + } + if (FD_ISSET (ds, &rs)) + MHD_connection_handle_read (pos); + if (FD_ISSET (ds, &ws)) + MHD_connection_handle_write (pos); + pos = pos->next; + } } - } return MHD_YES; } @@ -431,13 +409,14 @@ MHD_select(struct MHD_Daemon * daemon, * options for this call. */ int -MHD_run(struct MHD_Daemon * daemon) { - if ( (daemon->shutdown != 0) || - (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || - (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) ) +MHD_run (struct MHD_Daemon *daemon) +{ + if ((daemon->shutdown != 0) || + (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || + (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY))) return MHD_NO; - MHD_select(daemon, MHD_NO); - MHD_cleanup_connections(daemon); + MHD_select (daemon, MHD_NO); + MHD_cleanup_connections (daemon); return MHD_YES; } @@ -447,12 +426,14 @@ MHD_run(struct MHD_Daemon * daemon) { * is explicitly shut down. */ static void * -MHD_select_thread(void * cls) { - struct MHD_Daemon * daemon = cls; - while (daemon->shutdown == 0) { - MHD_select(daemon, MHD_YES); - MHD_cleanup_connections(daemon); - } +MHD_select_thread (void *cls) +{ + struct MHD_Daemon *daemon = cls; + while (daemon->shutdown == 0) + { + MHD_select (daemon, MHD_YES); + MHD_cleanup_connections (daemon); + } return NULL; } @@ -469,88 +450,75 @@ MHD_select_thread(void * cls) { * @return NULL on error, handle to daemon on success */ struct MHD_Daemon * -MHD_start_daemon(unsigned int options, - unsigned short port, - MHD_AcceptPolicyCallback apc, - void * apc_cls, - MHD_AccessHandlerCallback dh, - void * dh_cls, - ...) { +MHD_start_daemon (unsigned int options, + unsigned short port, + MHD_AcceptPolicyCallback apc, + void *apc_cls, + MHD_AccessHandlerCallback dh, void *dh_cls, ...) +{ const int on = 1; - struct MHD_Daemon * retVal; + struct MHD_Daemon *retVal; int socket_fd; - struct sockaddr_in servaddr4; - struct sockaddr_in6 servaddr6; - const struct sockaddr * servaddr; + struct sockaddr_in servaddr4; + struct sockaddr_in6 servaddr6; + const struct sockaddr *servaddr; socklen_t addrlen; va_list ap; enum MHD_OPTION opt; - + if ((options & MHD_USE_SSL) != 0) return NULL; - if ( (port == 0) || - (dh == NULL) ) + if ((port == 0) || (dh == NULL)) return NULL; if ((options & MHD_USE_IPv6) != 0) - socket_fd = SOCKET(PF_INET6, SOCK_STREAM, 0); + socket_fd = SOCKET (PF_INET6, SOCK_STREAM, 0); else - socket_fd = SOCKET(PF_INET, SOCK_STREAM, 0); - if (socket_fd < 0) { - if ((options & MHD_USE_DEBUG) != 0) - fprintf(stderr, - "Call to socket failed: %s\n", - STRERROR(errno)); - return NULL; - } - if ( (SETSOCKOPT(socket_fd, - SOL_SOCKET, - SO_REUSEADDR, - &on, - sizeof(on)) < 0) && - (options & MHD_USE_DEBUG) != 0) - fprintf(stderr, - "setsockopt failed: %s\n", - STRERROR(errno)); - if ((options & MHD_USE_IPv6) != 0) { - memset(&servaddr6, - 0, - sizeof(struct sockaddr_in6)); - servaddr6.sin6_family = AF_INET6; - servaddr6.sin6_port = htons(port); - servaddr = (struct sockaddr*) &servaddr6; - addrlen = sizeof(struct sockaddr_in6); - } else { - memset(&servaddr4, - 0, - sizeof(struct sockaddr_in)); - servaddr4.sin_family = AF_INET; - servaddr4.sin_port = htons(port); - servaddr = (struct sockaddr*) &servaddr4; - addrlen = sizeof(struct sockaddr_in); - } - if (BIND(socket_fd, - servaddr, - addrlen) < 0) { - if ( (options & MHD_USE_DEBUG) != 0) - fprintf(stderr, - "Failed to bind to port %u: %s\n", - port, - STRERROR(errno)); - CLOSE(socket_fd); - return NULL; - } - if (LISTEN(socket_fd, 20) < 0) { - if ((options & MHD_USE_DEBUG) != 0) - fprintf(stderr, - "Failed to listen for connections: %s\n", - STRERROR(errno)); - CLOSE(socket_fd); - return NULL; - } - retVal = malloc(sizeof(struct MHD_Daemon)); - memset(retVal, - 0, - sizeof(struct MHD_Daemon)); + socket_fd = SOCKET (PF_INET, SOCK_STREAM, 0); + if (socket_fd < 0) + { + if ((options & MHD_USE_DEBUG) != 0) + fprintf (stderr, "Call to socket failed: %s\n", STRERROR (errno)); + return NULL; + } + if ((SETSOCKOPT (socket_fd, + SOL_SOCKET, + SO_REUSEADDR, + &on, sizeof (on)) < 0) && (options & MHD_USE_DEBUG) != 0) + fprintf (stderr, "setsockopt failed: %s\n", STRERROR (errno)); + if ((options & MHD_USE_IPv6) != 0) + { + memset (&servaddr6, 0, sizeof (struct sockaddr_in6)); + servaddr6.sin6_family = AF_INET6; + servaddr6.sin6_port = htons (port); + servaddr = (struct sockaddr *) &servaddr6; + addrlen = sizeof (struct sockaddr_in6); + } + else + { + memset (&servaddr4, 0, sizeof (struct sockaddr_in)); + servaddr4.sin_family = AF_INET; + servaddr4.sin_port = htons (port); + servaddr = (struct sockaddr *) &servaddr4; + addrlen = sizeof (struct sockaddr_in); + } + if (BIND (socket_fd, servaddr, addrlen) < 0) + { + if ((options & MHD_USE_DEBUG) != 0) + fprintf (stderr, + "Failed to bind to port %u: %s\n", port, STRERROR (errno)); + CLOSE (socket_fd); + return NULL; + } + if (LISTEN (socket_fd, 20) < 0) + { + if ((options & MHD_USE_DEBUG) != 0) + fprintf (stderr, + "Failed to listen for connections: %s\n", STRERROR (errno)); + CLOSE (socket_fd); + return NULL; + } + retVal = malloc (sizeof (struct MHD_Daemon)); + memset (retVal, 0, sizeof (struct MHD_Daemon)); retVal->options = options; retVal->port = port; retVal->apc = apc; @@ -562,35 +530,34 @@ MHD_start_daemon(unsigned int options, retVal->default_handler.next = NULL; retVal->max_connections = MHD_MAX_CONNECTIONS_DEFAULT; retVal->pool_size = MHD_POOL_SIZE_DEFAULT; - va_start(ap, dh_cls); - while (MHD_OPTION_END != (opt = va_arg(ap, enum MHD_OPTION))) { - switch (opt) { - case MHD_OPTION_CONNECTION_MEMORY_LIMIT: - retVal->pool_size = va_arg(ap, unsigned int); - break; - case MHD_OPTION_CONNECTION_LIMIT: - retVal->max_connections = va_arg(ap, unsigned int); - break; - default: - fprintf(stderr, - "Invalid MHD_OPTION argument! (Did you terminate the list with MHD_OPTION_END?)\n"); - abort(); + va_start (ap, dh_cls); + while (MHD_OPTION_END != (opt = va_arg (ap, enum MHD_OPTION))) + { + switch (opt) + { + case MHD_OPTION_CONNECTION_MEMORY_LIMIT: + retVal->pool_size = va_arg (ap, unsigned int); + break; + case MHD_OPTION_CONNECTION_LIMIT: + retVal->max_connections = va_arg (ap, unsigned int); + break; + default: + fprintf (stderr, + "Invalid MHD_OPTION argument! (Did you terminate the list with MHD_OPTION_END?)\n"); + abort (); + } + } + va_end (ap); + if (((0 != (options & MHD_USE_THREAD_PER_CONNECTION)) || + (0 != (options & MHD_USE_SELECT_INTERNALLY))) && + (0 != pthread_create (&retVal->pid, NULL, &MHD_select_thread, retVal))) + { + MHD_DLOG (retVal, + "Failed to create listen thread: %s\n", STRERROR (errno)); + free (retVal); + CLOSE (socket_fd); + return NULL; } - } - va_end(ap); - if ( ( (0 != (options & MHD_USE_THREAD_PER_CONNECTION)) || - (0 != (options & MHD_USE_SELECT_INTERNALLY)) ) && - (0 != pthread_create(&retVal->pid, - NULL, - &MHD_select_thread, - retVal)) ) { - MHD_DLOG(retVal, - "Failed to create listen thread: %s\n", - STRERROR(errno)); - free(retVal); - CLOSE(socket_fd); - return NULL; - } return retVal; } @@ -598,27 +565,31 @@ MHD_start_daemon(unsigned int options, * Shutdown an http daemon. */ void -MHD_stop_daemon(struct MHD_Daemon * daemon) { - void * unused; +MHD_stop_daemon (struct MHD_Daemon *daemon) +{ + void *unused; if (daemon == NULL) return; daemon->shutdown = 1; - CLOSE(daemon->socket_fd); + CLOSE (daemon->socket_fd); daemon->socket_fd = -1; - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || - (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) ) { - pthread_kill(daemon->pid, SIGALRM); - pthread_join(daemon->pid, &unused); - } - while (daemon->connections != NULL) { - if (-1 != daemon->connections->socket_fd) { - CLOSE(daemon->connections->socket_fd); - daemon->connections->socket_fd = -1; + if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || + (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY))) + { + pthread_kill (daemon->pid, SIGALRM); + pthread_join (daemon->pid, &unused); + } + while (daemon->connections != NULL) + { + if (-1 != daemon->connections->socket_fd) + { + CLOSE (daemon->connections->socket_fd); + daemon->connections->socket_fd = -1; + } + MHD_cleanup_connections (daemon); } - MHD_cleanup_connections(daemon); - } - free(daemon); + free (daemon); } #ifndef WINDOWS @@ -627,23 +598,27 @@ static struct sigaction sig; static struct sigaction old; -static void sigalrmHandler(int sig) { +static void +sigalrmHandler (int sig) +{ } /** * Initialize the signal handler for SIGALRM. */ -void __attribute__ ((constructor)) MHD_pthread_handlers_ltdl_init() { +void __attribute__ ((constructor)) MHD_pthread_handlers_ltdl_init () +{ /* make sure SIGALRM does not kill us */ - memset(&sig, 0, sizeof(struct sigaction)); - memset(&old, 0, sizeof(struct sigaction)); + memset (&sig, 0, sizeof (struct sigaction)); + memset (&old, 0, sizeof (struct sigaction)); sig.sa_flags = SA_NODEFER; - sig.sa_handler = &sigalrmHandler; - sigaction(SIGALRM, &sig, &old); + sig.sa_handler = &sigalrmHandler; + sigaction (SIGALRM, &sig, &old); } -void __attribute__ ((destructor)) MHD_pthread_handlers_ltdl_fini() { - sigaction(SIGALRM, &old, &sig); +void __attribute__ ((destructor)) MHD_pthread_handlers_ltdl_fini () +{ + sigaction (SIGALRM, &old, &sig); } #endif diff --git a/src/daemon/daemontest.c b/src/daemon/daemontest.c @@ -31,131 +31,128 @@ #include <string.h> #include <stdio.h> -static int testStartError() { - struct MHD_Daemon * d; +static int +testStartError () +{ + struct MHD_Daemon *d; - d = MHD_start_daemon(MHD_USE_DEBUG, 0, NULL, NULL, NULL, NULL); + d = MHD_start_daemon (MHD_USE_DEBUG, 0, NULL, NULL, NULL, NULL); if (d != NULL) return 1; return 0; } -static int apc_nothing(void * cls, - const struct sockaddr * addr, - socklen_t addrlen) { +static int +apc_nothing (void *cls, const struct sockaddr *addr, socklen_t addrlen) +{ return MHD_NO; } -static int apc_all(void * cls, - const struct sockaddr * addr, - socklen_t addrlen) { +static int +apc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen) +{ return MHD_YES; } -static int ahc_nothing(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * version, - const char * upload_data, - unsigned int * upload_data_size) { +static int +ahc_nothing (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, unsigned int *upload_data_size) +{ return MHD_NO; } -static int testStartStop() { - struct MHD_Daemon * d; +static int +testStartStop () +{ + struct MHD_Daemon *d; - d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, - 1080, - &apc_nothing, - NULL, - &ahc_nothing, - NULL, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, + 1080, + &apc_nothing, + NULL, &ahc_nothing, NULL, MHD_OPTION_END); if (d == NULL) return 2; - MHD_stop_daemon(d); + MHD_stop_daemon (d); return 0; } -static int testExternalRun() { - struct MHD_Daemon * d; +static int +testExternalRun () +{ + struct MHD_Daemon *d; fd_set rs; int maxfd; int i; - d = MHD_start_daemon(MHD_USE_DEBUG, - 1081, - &apc_all, - NULL, - &ahc_nothing, - NULL, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_DEBUG, + 1081, + &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END); if (d == NULL) return 4; i = 0; - while(i < 15) { - maxfd = 0; - FD_ZERO(&rs); - MHD_get_fdset(d, &rs, &rs, &rs, &maxfd); - if (MHD_run(d) == MHD_NO) { - MHD_stop_daemon(d); - return 8; + while (i < 15) + { + maxfd = 0; + FD_ZERO (&rs); + MHD_get_fdset (d, &rs, &rs, &rs, &maxfd); + if (MHD_run (d) == MHD_NO) + { + MHD_stop_daemon (d); + return 8; + } + i++; } - i++; - } - MHD_stop_daemon(d); + MHD_stop_daemon (d); return 0; } -static int testThread() { - struct MHD_Daemon * d; - d = MHD_start_daemon(MHD_USE_DEBUG | MHD_USE_SELECT_INTERNALLY, - 1082, - &apc_all, - NULL, - &ahc_nothing, - NULL, - MHD_OPTION_END); +static int +testThread () +{ + struct MHD_Daemon *d; + d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SELECT_INTERNALLY, + 1082, + &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END); if (d == NULL) return 16; - if (MHD_run(d) != MHD_NO) + if (MHD_run (d) != MHD_NO) return 32; - MHD_stop_daemon(d); + MHD_stop_daemon (d); return 0; } -static int testMultithread() { - struct MHD_Daemon * d; - d = MHD_start_daemon(MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION, - 1083, - &apc_all, - NULL, - &ahc_nothing, - NULL, - MHD_OPTION_END); +static int +testMultithread () +{ + struct MHD_Daemon *d; + d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION, + 1083, + &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END); if (d == NULL) return 64; - if (MHD_run(d) != MHD_NO) + if (MHD_run (d) != MHD_NO) return 128; - MHD_stop_daemon(d); + MHD_stop_daemon (d); return 0; } -int main(int argc, - char * const * argv) { +int +main (int argc, char *const *argv) +{ unsigned int errorCount = 0; - errorCount += testStartError(); - errorCount += testStartStop(); - errorCount += testExternalRun(); - errorCount += testThread(); - errorCount += testMultithread(); + errorCount += testStartError (); + errorCount += testStartStop (); + errorCount += testExternalRun (); + errorCount += testThread (); + errorCount += testMultithread (); if (errorCount != 0) - fprintf(stderr, - "Error (code: %u)\n", - errorCount); - return errorCount != 0; /* 0 == pass */ + fprintf (stderr, "Error (code: %u)\n", errorCount); + return errorCount != 0; /* 0 == pass */ } diff --git a/src/daemon/daemontest_get.c b/src/daemon/daemontest_get.c @@ -35,210 +35,167 @@ static int oneone; -struct CBC { - char * buf; +struct CBC +{ + char *buf; size_t pos; size_t size; }; -static size_t copyBuffer(void * ptr, - size_t size, - size_t nmemb, - void * ctx) { - struct CBC * cbc = ctx; +static size_t +copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) +{ + struct CBC *cbc = ctx; if (cbc->pos + size * nmemb > cbc->size) - return 0; /* overflow */ - memcpy(&cbc->buf[cbc->pos], - ptr, - size * nmemb); + return 0; /* overflow */ + memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); cbc->pos += size * nmemb; return size * nmemb; } -static int ahc_echo(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * version, - const char * upload_data, - unsigned int * upload_data_size) { - const char * me = cls; - struct MHD_Response * response; +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, unsigned int *upload_data_size) +{ + const char *me = cls; + struct MHD_Response *response; int ret; - if (0 != strcmp(me, method)) - return MHD_NO; /* unexpected method */ - response = MHD_create_response_from_data(strlen(url), - (void*) url, - MHD_NO, - MHD_YES); - ret = MHD_queue_response(connection, - MHD_HTTP_OK, - response); - MHD_destroy_response(response); + if (0 != strcmp (me, method)) + return MHD_NO; /* unexpected method */ + response = MHD_create_response_from_data (strlen (url), + (void *) url, MHD_NO, MHD_YES); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); return ret; } -static int testInternalGet() { - struct MHD_Daemon * d; - CURL * c; +static int +testInternalGet () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, - 1080, - NULL, - NULL, - &ahc_echo, - "GET", - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, + 1080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); if (d == NULL) return 1; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1080/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 2L); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 2L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - // NOTE: use of CONNECTTIMEOUT without also + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK != curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 2; - } - curl_easy_cleanup(c); - if (cbc.pos != strlen("/hello_world")) { - MHD_stop_daemon(d); - return 4; - } + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK != curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2; + } + curl_easy_cleanup (c); + if (cbc.pos != strlen ("/hello_world")) + { + MHD_stop_daemon (d); + return 4; + } - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) { - MHD_stop_daemon(d); - return 8; - } - MHD_stop_daemon(d); + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) + { + MHD_stop_daemon (d); + return 8; + } + MHD_stop_daemon (d); return 0; } -static int testMultithreadedGet() { - struct MHD_Daemon * d; - CURL * c; +static int +testMultithreadedGet () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, - 1081, - NULL, - NULL, - &ahc_echo, - "GET", - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, + 1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); if (d == NULL) return 16; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1081/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 2L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 2L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK != curl_easy_perform(c)) { - MHD_stop_daemon(d); - return 32; - } - curl_easy_cleanup(c); - if (cbc.pos != strlen("/hello_world")) { - MHD_stop_daemon(d); - return 64; - } - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) { - MHD_stop_daemon(d); - return 128; - } - MHD_stop_daemon(d); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK != curl_easy_perform (c)) + { + MHD_stop_daemon (d); + return 32; + } + curl_easy_cleanup (c); + if (cbc.pos != strlen ("/hello_world")) + { + MHD_stop_daemon (d); + return 64; + } + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) + { + MHD_stop_daemon (d); + return 128; + } + MHD_stop_daemon (d); return 0; } -static int testExternalGet() { - struct MHD_Daemon * d; - CURL * c; +static int +testExternalGet () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; - CURLM * multi; + CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; int max; int running; - struct CURLMsg * msg; + struct CURLMsg *msg; time_t start; struct timeval tv; @@ -246,154 +203,121 @@ static int testExternalGet() { cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_DEBUG, - 1082, - NULL, - NULL, - &ahc_echo, - "GET", - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_DEBUG, + 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); if (d == NULL) return 256; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1082/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 5L); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 5L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 5L); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); - multi = curl_multi_init(); - if (multi == NULL) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 512; - } - mret = curl_multi_add_handle(multi, c); - if (mret != CURLM_OK) { - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 1024; - } - start = time(NULL); - while ( (time(NULL) - start < 5) && - (multi != NULL) ) { - max = 0; - FD_ZERO(&rs); - FD_ZERO(&ws); - FD_ZERO(&es); - curl_multi_perform(multi, &running); - mret = curl_multi_fdset(multi, - &rs, - &ws, - &es, - &max); - if (mret != CURLM_OK) { - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 2048; + multi = curl_multi_init (); + if (multi == NULL) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 512; + } + mret = curl_multi_add_handle (multi, c); + if (mret != CURLM_OK) + { + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 1024; + } + start = time (NULL); + while ((time (NULL) - start < 5) && (multi != NULL)) + { + max = 0; + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + curl_multi_perform (multi, &running); + mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); + if (mret != CURLM_OK) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2048; + } + if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 4096; + } + tv.tv_sec = 0; + tv.tv_usec = 1000; + select (max + 1, &rs, &ws, &es, &tv); + curl_multi_perform (multi, &running); + if (running == 0) + { + msg = curl_multi_info_read (multi, &running); + if (msg == NULL) + break; + if (msg->msg == CURLMSG_DONE) + { + if (msg->data.result != CURLE_OK) + printf ("%s failed at %s:%d: `%s'\n", + "curl_multi_perform", + __FILE__, + __LINE__, curl_easy_strerror (msg->data.result)); + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + c = NULL; + multi = NULL; + } + } + MHD_run (d); } - if (MHD_YES != MHD_get_fdset(d, - &rs, - &ws, - &es, - &max)) { - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 4096; + if (multi != NULL) + { + curl_multi_remove_handle (multi, c); + curl_easy_cleanup (c); + curl_multi_cleanup (multi); } - tv.tv_sec = 0; - tv.tv_usec = 1000; - select(max + 1, - &rs, - &ws, - &es, - &tv); - curl_multi_perform(multi, &running); - if (running == 0) { - msg = curl_multi_info_read(multi, - &running); - if (msg == NULL) - break; - if (msg->msg == CURLMSG_DONE) { - if (msg->data.result != CURLE_OK) - printf("%s failed at %s:%d: `%s'\n", - "curl_multi_perform", - __FILE__, - __LINE__, - curl_easy_strerror(msg->data.result)); - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - c = NULL; - multi = NULL; - } - } - MHD_run(d); - } - if (multi != NULL) { - curl_multi_remove_handle(multi, c); - curl_easy_cleanup(c); - curl_multi_cleanup(multi); - } - MHD_stop_daemon(d); - if (cbc.pos != strlen("/hello_world")) + MHD_stop_daemon (d); + if (cbc.pos != strlen ("/hello_world")) return 8192; - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) return 16384; return 0; } -int main(int argc, - char * const * argv) { +int +main (int argc, char *const *argv) +{ unsigned int errorCount = 0; - oneone = NULL != strstr(argv[0], "11"); - if (0 != curl_global_init(CURL_GLOBAL_WIN32)) + oneone = NULL != strstr (argv[0], "11"); + if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; - errorCount += testInternalGet(); - errorCount += testMultithreadedGet(); - errorCount += testExternalGet(); + errorCount += testInternalGet (); + errorCount += testMultithreadedGet (); + errorCount += testExternalGet (); if (errorCount != 0) - fprintf(stderr, - "Error (code: %u)\n", - errorCount); - curl_global_cleanup(); - return errorCount != 0; /* 0 == pass */ + fprintf (stderr, "Error (code: %u)\n", errorCount); + curl_global_cleanup (); + return errorCount != 0; /* 0 == pass */ } diff --git a/src/daemon/daemontest_long_header.c b/src/daemon/daemontest_long_header.c @@ -41,247 +41,196 @@ static int oneone; -static int apc_all(void * cls, - const struct sockaddr * addr, - socklen_t addrlen) { +static int +apc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen) +{ return MHD_YES; } -struct CBC { - char * buf; +struct CBC +{ + char *buf; size_t pos; size_t size; }; -static size_t copyBuffer(void * ptr, - size_t size, - size_t nmemb, - void * ctx) { +static size_t +copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) +{ return size * nmemb; } -static int ahc_echo(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * version, - const char * upload_data, - unsigned int * upload_data_size) { - const char * me = cls; - struct MHD_Response * response; +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, unsigned int *upload_data_size) +{ + const char *me = cls; + struct MHD_Response *response; int ret; - if (0 != strcmp(me, method)) - return MHD_NO; /* unexpected method */ - response = MHD_create_response_from_data(strlen(url), - (void*) url, - MHD_NO, - MHD_YES); - ret = MHD_queue_response(connection, - MHD_HTTP_OK, - response); - MHD_destroy_response(response); + if (0 != strcmp (me, method)) + return MHD_NO; /* unexpected method */ + response = MHD_create_response_from_data (strlen (url), + (void *) url, MHD_NO, MHD_YES); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); return ret; } -static int testLongUrlGet() { - struct MHD_Daemon * d; - CURL * c; +static int +testLongUrlGet () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; - char * url; + char *url; long code; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */, - 1080, - &apc_all, - NULL, - &ahc_echo, - "GET", - MHD_OPTION_CONNECTION_MEMORY_LIMIT, - VERY_LONG / 2, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ , + 1080, + &apc_all, + NULL, + &ahc_echo, + "GET", + MHD_OPTION_CONNECTION_MEMORY_LIMIT, + VERY_LONG / 2, MHD_OPTION_END); if (d == NULL) return 1; - c = curl_easy_init(); - url = malloc(VERY_LONG); - memset(url, - 'a', - VERY_LONG); - url[VERY_LONG-1] = '\0'; - memcpy(url, - "http://localhost:1080/", - strlen("http://localhost:1080/")); - curl_easy_setopt(c, - CURLOPT_URL, - url); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 2L); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 2L); + c = curl_easy_init (); + url = malloc (VERY_LONG); + memset (url, 'a', VERY_LONG); + url[VERY_LONG - 1] = '\0'; + memcpy (url, "http://localhost:1080/", strlen ("http://localhost:1080/")); + curl_easy_setopt (c, CURLOPT_URL, url); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - // NOTE: use of CONNECTTIMEOUT without also + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK == curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - free(url); - return 2; - } - if (CURLE_OK != curl_easy_getinfo(c, - CURLINFO_RESPONSE_CODE, - &code)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - free(url); - return 4; - } - curl_easy_cleanup(c); - MHD_stop_daemon(d); - free(url); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK == curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + free (url); + return 2; + } + if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + free (url); + return 4; + } + curl_easy_cleanup (c); + MHD_stop_daemon (d); + free (url); if (code != MHD_HTTP_REQUEST_URI_TOO_LONG) return 8; return 0; } -static int testLongHeaderGet() { - struct MHD_Daemon * d; - CURL * c; +static int +testLongHeaderGet () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; - char * url; + char *url; long code; - struct curl_slist * header = NULL; + struct curl_slist *header = NULL; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */, - 1080, - &apc_all, - NULL, - &ahc_echo, - "GET", - MHD_OPTION_CONNECTION_MEMORY_LIMIT, - VERY_LONG / 2, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ , + 1080, + &apc_all, + NULL, + &ahc_echo, + "GET", + MHD_OPTION_CONNECTION_MEMORY_LIMIT, + VERY_LONG / 2, MHD_OPTION_END); if (d == NULL) return 16; - c = curl_easy_init(); - url = malloc(VERY_LONG); - memset(url, - 'a', - VERY_LONG); - url[VERY_LONG-1] = '\0'; - url[VERY_LONG/2] = ':'; - url[VERY_LONG/2+1] = ':'; - header = curl_slist_append(header, - url); - - curl_easy_setopt(c, - CURLOPT_HTTPHEADER, - header); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1080/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 2L); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 2L); + c = curl_easy_init (); + url = malloc (VERY_LONG); + memset (url, 'a', VERY_LONG); + url[VERY_LONG - 1] = '\0'; + url[VERY_LONG / 2] = ':'; + url[VERY_LONG / 2 + 1] = ':'; + header = curl_slist_append (header, url); + + curl_easy_setopt (c, CURLOPT_HTTPHEADER, header); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - // NOTE: use of CONNECTTIMEOUT without also + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK == curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - curl_slist_free_all(header); - free(url); - return 32; - } - if (CURLE_OK != curl_easy_getinfo(c, - CURLINFO_RESPONSE_CODE, - &code)) { - curl_slist_free_all(header); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - free(url); - return 64; - } - curl_slist_free_all(header); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - free(url); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK == curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + curl_slist_free_all (header); + free (url); + return 32; + } + if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code)) + { + curl_slist_free_all (header); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + free (url); + return 64; + } + curl_slist_free_all (header); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + free (url); if (code != MHD_HTTP_REQUEST_ENTITY_TOO_LARGE) return 128; return 0; } - - - - -int main(int argc, - char * const * argv) { +int +main (int argc, char *const *argv) +{ unsigned int errorCount = 0; - oneone = NULL != strstr(argv[0], "11"); - if (0 != curl_global_init(CURL_GLOBAL_WIN32)) + oneone = NULL != strstr (argv[0], "11"); + if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; - errorCount += testLongUrlGet(); - errorCount += testLongHeaderGet(); + errorCount += testLongUrlGet (); + errorCount += testLongHeaderGet (); if (errorCount != 0) - fprintf(stderr, - "Error (code: %u)\n", - errorCount); - curl_global_cleanup(); - return errorCount != 0; /* 0 == pass */ + fprintf (stderr, "Error (code: %u)\n", errorCount); + curl_global_cleanup (); + return errorCount != 0; /* 0 == pass */ } diff --git a/src/daemon/daemontest_post.c b/src/daemon/daemontest_post.c @@ -41,246 +41,187 @@ static int oneone; -struct CBC { - char * buf; +struct CBC +{ + char *buf; size_t pos; size_t size; }; -static size_t copyBuffer(void * ptr, - size_t size, - size_t nmemb, - void * ctx) { - struct CBC * cbc = ctx; +static size_t +copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) +{ + struct CBC *cbc = ctx; if (cbc->pos + size * nmemb > cbc->size) - return 0; /* overflow */ - memcpy(&cbc->buf[cbc->pos], - ptr, - size * nmemb); + return 0; /* overflow */ + memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); cbc->pos += size * nmemb; return size * nmemb; } -static int ahc_echo(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * version, - const char * upload_data, - unsigned int * upload_data_size) { - struct MHD_Response * response; +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, unsigned int *upload_data_size) +{ + struct MHD_Response *response; int ret; - const char * r1; - const char * r2; + const char *r1; + const char *r2; - if (0 != strcmp("POST", method)) { - printf("METHOD: %s\n", method); - return MHD_NO; /* unexpected method */ - } - r1 = MHD_lookup_connection_value(connection, - MHD_POSTDATA_KIND, - "name"); - r2 = MHD_lookup_connection_value(connection, - MHD_POSTDATA_KIND, - "project"); - if ( (r1 != NULL) && - (r2 != NULL) && - (0 == strcmp("daniel", - r1)) && - (0 == strcmp("curl", - r2)) ) { - response = MHD_create_response_from_data(strlen(url), - (void*) url, - MHD_NO, - MHD_YES); - ret = MHD_queue_response(connection, - MHD_HTTP_OK, - response); - MHD_destroy_response(response); - return MHD_YES; /* done */ - } + if (0 != strcmp ("POST", method)) + { + printf ("METHOD: %s\n", method); + return MHD_NO; /* unexpected method */ + } + r1 = MHD_lookup_connection_value (connection, MHD_POSTDATA_KIND, "name"); + r2 = MHD_lookup_connection_value (connection, MHD_POSTDATA_KIND, "project"); + if ((r1 != NULL) && + (r2 != NULL) && + (0 == strcmp ("daniel", r1)) && (0 == strcmp ("curl", r2))) + { + response = MHD_create_response_from_data (strlen (url), + (void *) url, + MHD_NO, MHD_YES); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); + return MHD_YES; /* done */ + } return MHD_YES; } -static int testInternalPost() { - struct MHD_Daemon * d; - CURL * c; +static int +testInternalPost () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; - + cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, - 1080, - NULL, - NULL, - &ahc_echo, - NULL, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, + 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (d == NULL) return 1; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1080/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_POSTFIELDS, - POST_DATA); - curl_easy_setopt(c, - CURLOPT_POSTFIELDSIZE, - strlen(POST_DATA)); - curl_easy_setopt(c, - CURLOPT_POST, - 1L); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 2L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); + curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); + curl_easy_setopt (c, CURLOPT_POST, 1L); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 2L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK != curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 2; - } - curl_easy_cleanup(c); - if (cbc.pos != strlen("/hello_world")) { - MHD_stop_daemon(d); - return 4; - } + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK != curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2; + } + curl_easy_cleanup (c); + if (cbc.pos != strlen ("/hello_world")) + { + MHD_stop_daemon (d); + return 4; + } - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) { - MHD_stop_daemon(d); - return 8; - } - MHD_stop_daemon(d); + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) + { + MHD_stop_daemon (d); + return 8; + } + MHD_stop_daemon (d); return 0; } -static int testMultithreadedPost() { - struct MHD_Daemon * d; - CURL * c; +static int +testMultithreadedPost () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION |MHD_USE_DEBUG, - 1081, - NULL, - NULL, - &ahc_echo, - NULL, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, + 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (d == NULL) return 16; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1081/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_POSTFIELDS, - POST_DATA); - curl_easy_setopt(c, - CURLOPT_POSTFIELDSIZE, - strlen(POST_DATA)); - curl_easy_setopt(c, - CURLOPT_POST, - 1L); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 2L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); + curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); + curl_easy_setopt (c, CURLOPT_POST, 1L); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 2L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK != curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 32; - } - curl_easy_cleanup(c); - if (cbc.pos != strlen("/hello_world")) { - MHD_stop_daemon(d); - return 64; - } - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) { - MHD_stop_daemon(d); - return 128; - } - MHD_stop_daemon(d); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK != curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 32; + } + curl_easy_cleanup (c); + if (cbc.pos != strlen ("/hello_world")) + { + MHD_stop_daemon (d); + return 64; + } + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) + { + MHD_stop_daemon (d); + return 128; + } + MHD_stop_daemon (d); return 0; } -static int testExternalPost() { - struct MHD_Daemon * d; - CURL * c; +static int +testExternalPost () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; - CURLM * multi; + CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; int max; int running; - struct CURLMsg * msg; + struct CURLMsg *msg; time_t start; struct timeval tv; @@ -288,163 +229,124 @@ static int testExternalPost() { cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_DEBUG, - 1082, - NULL, - NULL, - &ahc_echo, - NULL, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_DEBUG, + 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (d == NULL) return 256; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1082/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_POSTFIELDS, - POST_DATA); - curl_easy_setopt(c, - CURLOPT_POSTFIELDSIZE, - strlen(POST_DATA)); - curl_easy_setopt(c, - CURLOPT_POST, - 1L); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 5L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); + curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); + curl_easy_setopt (c, CURLOPT_POST, 1L); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 5L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 5L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); - multi = curl_multi_init(); - if (multi == NULL) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 512; - } - mret = curl_multi_add_handle(multi, c); - if (mret != CURLM_OK) { - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 1024; - } - start = time(NULL); - while ( (time(NULL) - start < 5) && - (multi != NULL) ) { - max = 0; - FD_ZERO(&rs); - FD_ZERO(&ws); - FD_ZERO(&es); - curl_multi_perform(multi, &running); - mret = curl_multi_fdset(multi, - &rs, - &ws, - &es, - &max); - if (mret != CURLM_OK) { - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 2048; + multi = curl_multi_init (); + if (multi == NULL) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 512; + } + mret = curl_multi_add_handle (multi, c); + if (mret != CURLM_OK) + { + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 1024; + } + start = time (NULL); + while ((time (NULL) - start < 5) && (multi != NULL)) + { + max = 0; + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + curl_multi_perform (multi, &running); + mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); + if (mret != CURLM_OK) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2048; + } + if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 4096; + } + tv.tv_sec = 0; + tv.tv_usec = 1000; + select (max + 1, &rs, &ws, &es, &tv); + curl_multi_perform (multi, &running); + if (running == 0) + { + msg = curl_multi_info_read (multi, &running); + if (msg == NULL) + break; + if (msg->msg == CURLMSG_DONE) + { + if (msg->data.result != CURLE_OK) + printf ("%s failed at %s:%d: `%s'\n", + "curl_multi_perform", + __FILE__, + __LINE__, curl_easy_strerror (msg->data.result)); + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + c = NULL; + multi = NULL; + } + } + MHD_run (d); } - if (MHD_YES != MHD_get_fdset(d, - &rs, - &ws, - &es, - &max)) { - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 4096; + if (multi != NULL) + { + curl_multi_remove_handle (multi, c); + curl_easy_cleanup (c); + curl_multi_cleanup (multi); } - tv.tv_sec = 0; - tv.tv_usec = 1000; - select(max + 1, - &rs, - &ws, - &es, - &tv); - curl_multi_perform(multi, &running); - if (running == 0) { - msg = curl_multi_info_read(multi, - &running); - if (msg == NULL) - break; - if (msg->msg == CURLMSG_DONE) { - if (msg->data.result != CURLE_OK) - printf("%s failed at %s:%d: `%s'\n", - "curl_multi_perform", - __FILE__, - __LINE__, - curl_easy_strerror(msg->data.result)); - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - c = NULL; - multi = NULL; - } - } - MHD_run(d); - } - if (multi != NULL) { - curl_multi_remove_handle(multi, c); - curl_easy_cleanup(c); - curl_multi_cleanup(multi); - } - MHD_stop_daemon(d); - if (cbc.pos != strlen("/hello_world")) + MHD_stop_daemon (d); + if (cbc.pos != strlen ("/hello_world")) return 8192; - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) return 16384; return 0; } -int main(int argc, - char * const * argv) { +int +main (int argc, char *const *argv) +{ unsigned int errorCount = 0; - oneone = NULL != strstr(argv[0], "11"); - if (0 != curl_global_init(CURL_GLOBAL_WIN32)) + oneone = NULL != strstr (argv[0], "11"); + if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; - errorCount += testInternalPost(); - errorCount += testMultithreadedPost(); - errorCount += testExternalPost(); + errorCount += testInternalPost (); + errorCount += testMultithreadedPost (); + errorCount += testExternalPost (); if (errorCount != 0) - fprintf(stderr, - "Error (code: %u)\n", - errorCount); - curl_global_cleanup(); - return errorCount != 0; /* 0 == pass */ + fprintf (stderr, "Error (code: %u)\n", errorCount); + curl_global_cleanup (); + return errorCount != 0; /* 0 == pass */ } diff --git a/src/daemon/daemontest_put.c b/src/daemon/daemontest_put.c @@ -34,87 +34,82 @@ static int oneone; -struct CBC { - char * buf; +struct CBC +{ + char *buf; size_t pos; size_t size; }; -static size_t putBuffer(void * stream, - size_t size, - size_t nmemb, - void * ptr) { - unsigned int * pos = ptr; +static size_t +putBuffer (void *stream, size_t size, size_t nmemb, void *ptr) +{ + unsigned int *pos = ptr; unsigned int wrt; wrt = size * nmemb; if (wrt > 8 - (*pos)) wrt = 8 - (*pos); - memcpy(stream, - &("Hello123"[*pos]), - wrt); + memcpy (stream, &("Hello123"[*pos]), wrt); (*pos) += wrt; return wrt; } -static size_t copyBuffer(void * ptr, - size_t size, - size_t nmemb, - void * ctx) { - struct CBC * cbc = ctx; +static size_t +copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) +{ + struct CBC *cbc = ctx; if (cbc->pos + size * nmemb > cbc->size) - return 0; /* overflow */ - memcpy(&cbc->buf[cbc->pos], - ptr, - size * nmemb); + return 0; /* overflow */ + memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); cbc->pos += size * nmemb; return size * nmemb; } -static int ahc_echo(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * version, - const char * upload_data, - unsigned int * upload_data_size) { - int * done = cls; - struct MHD_Response * response; +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, unsigned int *upload_data_size) +{ + int *done = cls; + struct MHD_Response *response; int ret; - if (0 != strcmp("PUT", method)) - return MHD_NO; /* unexpected method */ - if ((*done) == 0) { - if (*upload_data_size != 8) - return MHD_YES; /* not yet ready */ - if (0 == memcmp(upload_data, - "Hello123", - 8)) { - *upload_data_size = 0; - } else { - printf("Invalid upload data `%8s'!\n", - upload_data); - return MHD_NO; + if (0 != strcmp ("PUT", method)) + return MHD_NO; /* unexpected method */ + if ((*done) == 0) + { + if (*upload_data_size != 8) + return MHD_YES; /* not yet ready */ + if (0 == memcmp (upload_data, "Hello123", 8)) + { + *upload_data_size = 0; + } + else + { + printf ("Invalid upload data `%8s'!\n", upload_data); + return MHD_NO; + } + *done = 1; + return MHD_YES; } - *done = 1; - return MHD_YES; - } - response = MHD_create_response_from_data(strlen(url), - (void*) url, - MHD_NO, - MHD_YES); - ret = MHD_queue_response(connection, - MHD_HTTP_OK, - response); - MHD_destroy_response(response); + response = MHD_create_response_from_data (strlen (url), + (void *) url, MHD_NO, MHD_YES); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); return ret; } -static int testInternalPut() { - struct MHD_Daemon * d; - CURL * c; +static int +testInternalPut () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; unsigned int pos = 0; @@ -123,85 +118,58 @@ static int testInternalPut() { cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, - 1080, - NULL, - NULL, - &ahc_echo, - &done_flag, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, + 1080, + NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); if (d == NULL) return 1; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1080/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_READFUNCTION, - &putBuffer); - curl_easy_setopt(c, - CURLOPT_READDATA, - &pos); - curl_easy_setopt(c, - CURLOPT_UPLOAD, - 1L); - curl_easy_setopt(c, - CURLOPT_INFILESIZE_LARGE, - (curl_off_t) 8L); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 15L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); + curl_easy_setopt (c, CURLOPT_READDATA, &pos); + curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); + curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 15L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK != curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 2; - } - curl_easy_cleanup(c); - if (cbc.pos != strlen("/hello_world")) { - MHD_stop_daemon(d); - return 4; - } + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK != curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2; + } + curl_easy_cleanup (c); + if (cbc.pos != strlen ("/hello_world")) + { + MHD_stop_daemon (d); + return 4; + } - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) { - MHD_stop_daemon(d); - return 8; - } - MHD_stop_daemon(d); + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) + { + MHD_stop_daemon (d); + return 8; + } + MHD_stop_daemon (d); return 0; } -static int testMultithreadedPut() { - struct MHD_Daemon * d; - CURL * c; +static int +testMultithreadedPut () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; unsigned int pos = 0; @@ -210,94 +178,68 @@ static int testMultithreadedPut() { cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, - 1081, - NULL, NULL, - &ahc_echo, - &done_flag, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, + 1081, + NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); if (d == NULL) return 16; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1081/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_READFUNCTION, - &putBuffer); - curl_easy_setopt(c, - CURLOPT_READDATA, - &pos); - curl_easy_setopt(c, - CURLOPT_UPLOAD, - 1L); - curl_easy_setopt(c, - CURLOPT_INFILESIZE_LARGE, - (curl_off_t) 8L); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 15L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); + curl_easy_setopt (c, CURLOPT_READDATA, &pos); + curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); + curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 15L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); - if (CURLE_OK != curl_easy_perform(c)) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 32; - } - curl_easy_cleanup(c); - if (cbc.pos != strlen("/hello_world")) { - MHD_stop_daemon(d); - return 64; - } - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) { - MHD_stop_daemon(d); - return 128; - } - MHD_stop_daemon(d); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); + if (CURLE_OK != curl_easy_perform (c)) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 32; + } + curl_easy_cleanup (c); + if (cbc.pos != strlen ("/hello_world")) + { + MHD_stop_daemon (d); + return 64; + } + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) + { + MHD_stop_daemon (d); + return 128; + } + MHD_stop_daemon (d); return 0; } -static int testExternalPut() { - struct MHD_Daemon * d; - CURL * c; +static int +testExternalPut () +{ + struct MHD_Daemon *d; + CURL *c; char buf[2048]; struct CBC cbc; - CURLM * multi; + CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; int max; int running; - struct CURLMsg * msg; + struct CURLMsg *msg; time_t start; struct timeval tv; unsigned int pos = 0; @@ -307,166 +249,126 @@ static int testExternalPut() { cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; - d = MHD_start_daemon(MHD_USE_DEBUG, - 1082, - NULL, - NULL, - &ahc_echo, - &done_flag, - MHD_OPTION_END); + d = MHD_start_daemon (MHD_USE_DEBUG, + 1082, + NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); if (d == NULL) return 256; - c = curl_easy_init(); - curl_easy_setopt(c, - CURLOPT_URL, - "http://localhost:1082/hello_world"); - curl_easy_setopt(c, - CURLOPT_WRITEFUNCTION, - &copyBuffer); - curl_easy_setopt(c, - CURLOPT_WRITEDATA, - &cbc); - curl_easy_setopt(c, - CURLOPT_READFUNCTION, - &putBuffer); - curl_easy_setopt(c, - CURLOPT_READDATA, - &pos); - curl_easy_setopt(c, - CURLOPT_UPLOAD, - 1L); - curl_easy_setopt(c, - CURLOPT_INFILESIZE_LARGE, - (curl_off_t) 8L); - curl_easy_setopt(c, - CURLOPT_FAILONERROR, - 1); - curl_easy_setopt(c, - CURLOPT_TIMEOUT, - 15L); + c = curl_easy_init (); + curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world"); + curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); + curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); + curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); + curl_easy_setopt (c, CURLOPT_READDATA, &pos); + curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); + curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); + curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); if (oneone) - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_1); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else - curl_easy_setopt(c, - CURLOPT_HTTP_VERSION, - CURL_HTTP_VERSION_1_0); - curl_easy_setopt(c, - CURLOPT_CONNECTTIMEOUT, - 15L); + curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! - curl_easy_setopt(c, - CURLOPT_NOSIGNAL, - 1); + curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); - multi = curl_multi_init(); - if (multi == NULL) { - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 512; - } - mret = curl_multi_add_handle(multi, c); - if (mret != CURLM_OK) { - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 1024; - } - start = time(NULL); - while ( (time(NULL) - start < 5) && - (multi != NULL) ) { - max = 0; - FD_ZERO(&rs); - FD_ZERO(&ws); - FD_ZERO(&es); - curl_multi_perform(multi, &running); - mret = curl_multi_fdset(multi, - &rs, - &ws, - &es, - &max); - if (mret != CURLM_OK) { - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 2048; + multi = curl_multi_init (); + if (multi == NULL) + { + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 512; + } + mret = curl_multi_add_handle (multi, c); + if (mret != CURLM_OK) + { + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 1024; + } + start = time (NULL); + while ((time (NULL) - start < 5) && (multi != NULL)) + { + max = 0; + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + curl_multi_perform (multi, &running); + mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); + if (mret != CURLM_OK) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2048; + } + if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 4096; + } + tv.tv_sec = 0; + tv.tv_usec = 1000; + select (max + 1, &rs, &ws, &es, &tv); + curl_multi_perform (multi, &running); + if (running == 0) + { + msg = curl_multi_info_read (multi, &running); + if (msg == NULL) + break; + if (msg->msg == CURLMSG_DONE) + { + if (msg->data.result != CURLE_OK) + printf ("%s failed at %s:%d: `%s'\n", + "curl_multi_perform", + __FILE__, + __LINE__, curl_easy_strerror (msg->data.result)); + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + c = NULL; + multi = NULL; + } + } + MHD_run (d); } - if (MHD_YES != MHD_get_fdset(d, - &rs, - &ws, - &es, - &max)) { - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - MHD_stop_daemon(d); - return 4096; + if (multi != NULL) + { + curl_multi_remove_handle (multi, c); + curl_easy_cleanup (c); + curl_multi_cleanup (multi); } - tv.tv_sec = 0; - tv.tv_usec = 1000; - select(max + 1, - &rs, - &ws, - &es, - &tv); - curl_multi_perform(multi, &running); - if (running == 0) { - msg = curl_multi_info_read(multi, - &running); - if (msg == NULL) - break; - if (msg->msg == CURLMSG_DONE) { - if (msg->data.result != CURLE_OK) - printf("%s failed at %s:%d: `%s'\n", - "curl_multi_perform", - __FILE__, - __LINE__, - curl_easy_strerror(msg->data.result)); - curl_multi_remove_handle(multi, c); - curl_multi_cleanup(multi); - curl_easy_cleanup(c); - c = NULL; - multi = NULL; - } - } - MHD_run(d); - } - if (multi != NULL) { - curl_multi_remove_handle(multi, c); - curl_easy_cleanup(c); - curl_multi_cleanup(multi); - } - MHD_stop_daemon(d); - if (cbc.pos != strlen("/hello_world")) + MHD_stop_daemon (d); + if (cbc.pos != strlen ("/hello_world")) return 8192; - if (0 != strncmp("/hello_world", - cbc.buf, - strlen("/hello_world"))) + if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) return 16384; return 0; } -int main(int argc, - char * const * argv) { +int +main (int argc, char *const *argv) +{ unsigned int errorCount = 0; - oneone = NULL != strstr(argv[0], "11"); - if (0 != curl_global_init(CURL_GLOBAL_WIN32)) + oneone = NULL != strstr (argv[0], "11"); + if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; - errorCount += testInternalPut(); - errorCount += testMultithreadedPut(); - errorCount += testExternalPut(); + errorCount += testInternalPut (); + errorCount += testMultithreadedPut (); + errorCount += testExternalPut (); if (errorCount != 0) - fprintf(stderr, - "Error (code: %u)\n", - errorCount); - curl_global_cleanup(); - return errorCount != 0; /* 0 == pass */ + fprintf (stderr, "Error (code: %u)\n", errorCount); + curl_global_cleanup (); + return errorCount != 0; /* 0 == pass */ } diff --git a/src/daemon/fileserver_example.c b/src/daemon/fileserver_example.c @@ -37,78 +37,69 @@ #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" -static int file_reader(void * cls, - size_t pos, - char * buf, - int max) { - FILE * file = cls; +static int +file_reader (void *cls, size_t pos, char *buf, int max) +{ + FILE *file = cls; - fseek(file, pos, SEEK_SET); - return fread(buf, - 1, - max, - file); + fseek (file, pos, SEEK_SET); + return fread (buf, 1, max, file); } -static int ahc_echo(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * upload_data, - const char * version, - unsigned int * upload_data_size) { - struct MHD_Response * response; +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *upload_data, + const char *version, unsigned int *upload_data_size) +{ + struct MHD_Response *response; int ret; - FILE * file; + FILE *file; struct stat buf; - if (0 != strcmp(method, "GET")) - return MHD_NO; /* unexpected method */ - file = fopen(&url[1], "r"); - if (file == NULL) { - response = MHD_create_response_from_data(strlen(PAGE), - (void*) PAGE, - MHD_NO, - MHD_NO); - ret = MHD_queue_response(connection, - MHD_HTTP_NOT_FOUND, - response); - MHD_destroy_response(response); - } else { - stat(&url[1], - &buf); - response = MHD_create_response_from_callback(buf.st_size, - &file_reader, - file, - (MHD_ContentReaderFreeCallback) &fclose); - ret = MHD_queue_response(connection, - MHD_HTTP_OK, - response); - MHD_destroy_response(response); - } + if (0 != strcmp (method, "GET")) + return MHD_NO; /* unexpected method */ + file = fopen (&url[1], "r"); + if (file == NULL) + { + response = MHD_create_response_from_data (strlen (PAGE), + (void *) PAGE, + MHD_NO, MHD_NO); + ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); + MHD_destroy_response (response); + } + else + { + stat (&url[1], &buf); + response = MHD_create_response_from_callback (buf.st_size, + &file_reader, + file, + (MHD_ContentReaderFreeCallback) + & fclose); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); + } return ret; } -int main(int argc, - char * const * argv) { - struct MHD_Daemon * d; +int +main (int argc, char *const *argv) +{ + struct MHD_Daemon *d; - if (argc != 3) { - printf("%s PORT SECONDS-TO-RUN\n", - argv[0]); - return 1; - } - d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, - atoi(argv[1]), - NULL, - NULL, - &ahc_echo, - PAGE, - MHD_OPTION_END); + if (argc != 3) + { + printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); + return 1; + } + d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, + atoi (argv[1]), + NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); if (d == NULL) return 1; - sleep(atoi(argv[2])); - MHD_stop_daemon(d); + sleep (atoi (argv[2])); + MHD_stop_daemon (d); return 0; } - diff --git a/src/daemon/internal.c b/src/daemon/internal.c @@ -32,15 +32,14 @@ * fprintf-like helper function for logging debug * messages. */ -void MHD_DLOG(const struct MHD_Daemon * daemon, - const char * format, - ...) { +void +MHD_DLOG (const struct MHD_Daemon *daemon, const char *format, ...) +{ va_list va; - if ( (daemon->options & MHD_USE_DEBUG) == 0) + if ((daemon->options & MHD_USE_DEBUG) == 0) return; - va_start(va, format); - VFPRINTF(stderr, format, va); - va_end(va); + va_start (va, format); + VFPRINTF (stderr, format, va); + va_end (va); } - diff --git a/src/daemon/internal.h b/src/daemon/internal.h @@ -62,59 +62,60 @@ * fprintf-like helper function for logging debug * messages. */ -void MHD_DLOG(const struct MHD_Daemon * daemon, - const char * format, - ...); +void MHD_DLOG (const struct MHD_Daemon *daemon, const char *format, ...); /** * Header or cookie in HTTP request or response. */ -struct MHD_HTTP_Header { - struct MHD_HTTP_Header * next; +struct MHD_HTTP_Header +{ + struct MHD_HTTP_Header *next; - char * header; + char *header; - char * value; + char *value; enum MHD_ValueKind kind; }; -struct MHD_Access_Handler { - struct MHD_Access_Handler * next; +struct MHD_Access_Handler +{ + struct MHD_Access_Handler *next; - char * uri_prefix; + char *uri_prefix; MHD_AccessHandlerCallback dh; - void * dh_cls; + void *dh_cls; }; /** * Representation of a response. */ -struct MHD_Response { +struct MHD_Response +{ /** * Headers to send for the response. Initially * the linked list is created in inverse order; * the order should be inverted before sending! */ - struct MHD_HTTP_Header * first_header; + struct MHD_HTTP_Header *first_header; /** * Buffer pointing to data that we are supposed * to send as a response. */ - char * data; + char *data; /** * Closure to give to the content reader * free callback. */ - void * crc_cls; + void *crc_cls; /** * How do we get more data? NULL if we are @@ -165,27 +166,28 @@ struct MHD_Response { -struct MHD_Connection { +struct MHD_Connection +{ /** * This is a linked list. */ - struct MHD_Connection * next; + struct MHD_Connection *next; /** * Reference to the MHD_Daemon struct. */ - struct MHD_Daemon * daemon; + struct MHD_Daemon *daemon; /** * Linked list of parsed headers. */ - struct MHD_HTTP_Header * headers_received; + struct MHD_HTTP_Header *headers_received; /** * Response to transmit (initially NULL). */ - struct MHD_Response * response; + struct MHD_Response *response; /** * The memory pool is created whenever we first read @@ -197,43 +199,43 @@ struct MHD_Connection { * connections) and the IP address (which persists * across individual requests). */ - struct MemoryPool * pool; + struct MemoryPool *pool; /** * Request method. Should be GET/POST/etc. Allocated * in pool. */ - char * method; + char *method; /** * Requested URL (everything after "GET" only). Allocated * in pool. */ - char * url; + char *url; /** * HTTP version string (i.e. http/1.1). Allocated * in pool. */ - char * version; + char *version; /** * Buffer for reading requests. Allocated * in pool. */ - char * read_buffer; + char *read_buffer; /** * Buffer for writing response (headers only). Allocated * in pool. */ - char * write_buffer; + char *write_buffer; /** * Foreign address (of length addr_len). MALLOCED (not * in pool!). */ - struct sockaddr_in * addr; + struct sockaddr_in *addr; /** * Thread for this connection (if we are using @@ -342,20 +344,21 @@ struct MHD_Connection { -struct MHD_Daemon { +struct MHD_Daemon +{ - struct MHD_Access_Handler * handlers; + struct MHD_Access_Handler *handlers; struct MHD_Access_Handler default_handler; /** * Linked list of our current connections. */ - struct MHD_Connection * connections; + struct MHD_Connection *connections; MHD_AcceptPolicyCallback apc; - void * apc_cls; + void *apc_cls; /** * PID of the select thread (if we have internal select) @@ -390,7 +393,7 @@ struct MHD_Daemon { /** * Listen port. */ - unsigned short port; + unsigned short port; }; diff --git a/src/daemon/memorypool.c b/src/daemon/memorypool.c @@ -26,12 +26,13 @@ #include "memorypool.h" -struct MemoryPool { +struct MemoryPool +{ /** * Pointer to the pool's memory - */ - char * memory; + */ + char *memory; /** * Size of the pool. @@ -59,25 +60,30 @@ struct MemoryPool { * * @param max maximum size of the pool */ -struct MemoryPool * MHD_pool_create(unsigned int max) { - struct MemoryPool * pool; +struct MemoryPool * +MHD_pool_create (unsigned int max) +{ + struct MemoryPool *pool; - pool = malloc(sizeof(struct MemoryPool)); + pool = malloc (sizeof (struct MemoryPool)); if (pool == NULL) return NULL; - pool->memory = MMAP(NULL, max, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS, -1, 0); - if ( (pool->memory == MAP_FAILED) || - (pool->memory == NULL) ) { - pool->memory = malloc(max); - if (pool->memory == NULL) { - free(pool); - return NULL; + pool->memory = MMAP (NULL, max, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS, -1, 0); + if ((pool->memory == MAP_FAILED) || (pool->memory == NULL)) + { + pool->memory = malloc (max); + if (pool->memory == NULL) + { + free (pool); + return NULL; + } + pool->is_mmap = 0; + } + else + { + pool->is_mmap = 1; } - pool->is_mmap = 0; - } else { - pool->is_mmap = 1; - } pool->pos = 0; pool->end = max; pool->size = max; @@ -87,14 +93,16 @@ struct MemoryPool * MHD_pool_create(unsigned int max) { /** * Destroy a memory pool. */ -void MHD_pool_destroy(struct MemoryPool * pool) { +void +MHD_pool_destroy (struct MemoryPool *pool) +{ if (pool == NULL) return; if (pool->is_mmap == 0) - free(pool->memory); + free (pool->memory); else - MUNMAP(pool->memory, pool->size); - free(pool); + MUNMAP (pool->memory, pool->size); + free (pool); } /** @@ -102,21 +110,23 @@ void MHD_pool_destroy(struct MemoryPool * pool) { * @return NULL if the pool cannot support size more * bytes */ -void * MHD_pool_allocate(struct MemoryPool * pool, - unsigned int size, - int from_end) { - void * ret; +void * +MHD_pool_allocate (struct MemoryPool *pool, unsigned int size, int from_end) +{ + void *ret; - if ( (pool->pos + size > pool->end) || - (pool->pos + size < pool->pos) ) + if ((pool->pos + size > pool->end) || (pool->pos + size < pool->pos)) return NULL; - if (from_end == MHD_YES) { - ret = &pool->memory[pool->end - size]; - pool->end -= size; - } else { - ret = &pool->memory[pool->pos]; - pool->pos += size; - } + if (from_end == MHD_YES) + { + ret = &pool->memory[pool->end - size]; + pool->end -= size; + } + else + { + ret = &pool->memory[pool->pos]; + pool->pos += size; + } return ret; } @@ -136,43 +146,40 @@ void * MHD_pool_allocate(struct MemoryPool * pool, * NULL if the pool cannot support new_size * bytes (old continues to be valid for old_size) */ -void * MHD_pool_reallocate(struct MemoryPool * pool, - void * old, - unsigned int old_size, - unsigned int new_size) { - void * ret; - - if ( (pool->end < old_size) || - (pool->end < new_size) ) - return NULL; /* unsatisfiable or bogus request */ - - if ( (pool->pos >= old_size) && - (&pool->memory[pool->pos - old_size] == old) ) { - /* was the previous allocation - optimize! */ - if (pool->pos + new_size - old_size <= pool->end) { - /* fits */ - pool->pos += new_size - old_size; - if (new_size < old_size) /* shrinking - zero again! */ - memset(&pool->memory[pool->pos], - 0, - old_size - new_size); - return old; +void * +MHD_pool_reallocate (struct MemoryPool *pool, + void *old, unsigned int old_size, unsigned int new_size) +{ + void *ret; + + if ((pool->end < old_size) || (pool->end < new_size)) + return NULL; /* unsatisfiable or bogus request */ + + if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old)) + { + /* was the previous allocation - optimize! */ + if (pool->pos + new_size - old_size <= pool->end) + { + /* fits */ + pool->pos += new_size - old_size; + if (new_size < old_size) /* shrinking - zero again! */ + memset (&pool->memory[pool->pos], 0, old_size - new_size); + return old; + } + /* does not fit */ + return NULL; } - /* does not fit */ - return NULL; - } if (new_size <= old_size) - return old; /* cannot shrink, no need to move */ - if ( (pool->pos + new_size >= pool->pos) && - (pool->pos + new_size <= pool->end) ) { - /* fits */ - ret = &pool->memory[pool->pos]; - memcpy(ret, - old, - old_size); - pool->pos += new_size; - return ret; - } + return old; /* cannot shrink, no need to move */ + if ((pool->pos + new_size >= pool->pos) && + (pool->pos + new_size <= pool->end)) + { + /* fits */ + ret = &pool->memory[pool->pos]; + memcpy (ret, old, old_size); + pool->pos += new_size; + return ret; + } /* does not fit */ return NULL; } diff --git a/src/daemon/memorypool.h b/src/daemon/memorypool.h @@ -43,12 +43,12 @@ struct MemoryPool; * * @param max maximum size of the pool */ -struct MemoryPool * MHD_pool_create(unsigned int max); +struct MemoryPool *MHD_pool_create (unsigned int max); /** * Destroy a memory pool. */ -void MHD_pool_destroy(struct MemoryPool * pool); +void MHD_pool_destroy (struct MemoryPool *pool); /** * Allocate size bytes from the pool. @@ -59,9 +59,8 @@ void MHD_pool_destroy(struct MemoryPool * pool); * @return NULL if the pool cannot support size more * bytes */ -void * MHD_pool_allocate(struct MemoryPool * pool, - unsigned int size, - int from_end); +void *MHD_pool_allocate (struct MemoryPool *pool, + unsigned int size, int from_end); /** * Reallocate a block of memory obtained from the pool. @@ -79,9 +78,8 @@ void * MHD_pool_allocate(struct MemoryPool * pool, * NULL if the pool cannot support new_size * bytes (old continues to be valid for old_size) */ -void * MHD_pool_reallocate(struct MemoryPool * pool, - void * old, - unsigned int old_size, - unsigned int new_size); +void *MHD_pool_reallocate (struct MemoryPool *pool, + void *old, + unsigned int old_size, unsigned int new_size); #endif diff --git a/src/daemon/minimal_example.c b/src/daemon/minimal_example.c @@ -35,50 +35,43 @@ #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" -static int ahc_echo(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * upload_data, - const char * version, - unsigned int * upload_data_size) { - const char * me = cls; - struct MHD_Response * response; +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *upload_data, + const char *version, unsigned int *upload_data_size) +{ + const char *me = cls; + struct MHD_Response *response; int ret; - if (0 != strcmp(method, "GET")) - return MHD_NO; /* unexpected method */ - response = MHD_create_response_from_data(strlen(me), - (void*) me, - MHD_NO, - MHD_NO); - ret = MHD_queue_response(connection, - MHD_HTTP_OK, - response); - MHD_destroy_response(response); + if (0 != strcmp (method, "GET")) + return MHD_NO; /* unexpected method */ + response = MHD_create_response_from_data (strlen (me), + (void *) me, MHD_NO, MHD_NO); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); return ret; } -int main(int argc, - char * const * argv) { - struct MHD_Daemon * d; +int +main (int argc, char *const *argv) +{ + struct MHD_Daemon *d; - if (argc != 3) { - printf("%s PORT SECONDS-TO-RUN\n", - argv[0]); - return 1; - } - d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, - atoi(argv[1]), - NULL, - NULL, - &ahc_echo, - PAGE, - MHD_OPTION_END); + if (argc != 3) + { + printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); + return 1; + } + d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, + atoi (argv[1]), + NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); if (d == NULL) return 1; - sleep(atoi(argv[2])); - MHD_stop_daemon(d); + sleep (atoi (argv[2])); + MHD_stop_daemon (d); return 0; } - diff --git a/src/daemon/plibc.h b/src/daemon/plibc.h @@ -29,21 +29,22 @@ #define _PLIBC_H_ #ifndef SIGALRM - #define SIGALRM 14 +#define SIGALRM 14 #endif #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif #ifdef Q_OS_WIN32 - #define WINDOWS 1 +#define WINDOWS 1 #endif #ifdef WINDOWS #if ENABLE_NLS - #include "langinfo.h" +#include "langinfo.h" #endif #include <windows.h> @@ -75,8 +76,8 @@ extern "C" { #define int64_t long long #define int32_t long -struct stat64 -{ + struct stat64 + { _dev_t st_dev; _ino_t st_ino; _mode_t st_mode; @@ -88,112 +89,112 @@ struct stat64 __time64_t st_atime; __time64_t st_mtime; __time64_t st_ctime; -}; + }; #ifndef pid_t - #define pid_t int +#define pid_t int #endif #ifndef WEXITSTATUS - #define WEXITSTATUS(status) (((status) & 0xff00) >> 8) +#define WEXITSTATUS(status) (((status) & 0xff00) >> 8) #endif /* Thanks to the Cygwin project */ -#define ENOCSI 43 /* No CSI structure available */ -#define EL2HLT 44 /* Level 2 halted */ +#define ENOCSI 43 /* No CSI structure available */ +#define EL2HLT 44 /* Level 2 halted */ #ifndef EDEADLK - #define EDEADLK 45 /* Deadlock condition */ +#define EDEADLK 45 /* Deadlock condition */ #endif #ifndef ENOLCK - #define ENOLCK 46 /* No record locks available */ +#define ENOLCK 46 /* No record locks available */ #endif -#define EBADE 50 /* Invalid exchange */ -#define EBADR 51 /* Invalid request descriptor */ -#define EXFULL 52 /* Exchange full */ -#define ENOANO 53 /* No anode */ -#define EBADRQC 54 /* Invalid request code */ -#define EBADSLT 55 /* Invalid slot */ +#define EBADE 50 /* Invalid exchange */ +#define EBADR 51 /* Invalid request descriptor */ +#define EXFULL 52 /* Exchange full */ +#define ENOANO 53 /* No anode */ +#define EBADRQC 54 /* Invalid request code */ +#define EBADSLT 55 /* Invalid slot */ #ifndef EDEADLOCK - #define EDEADLOCK EDEADLK /* File locking deadlock error */ +#define EDEADLOCK EDEADLK /* File locking deadlock error */ #endif -#define EBFONT 57 /* Bad font file fmt */ -#define ENOSTR 60 /* Device not a stream */ -#define ENODATA 61 /* No data (for no delay io) */ -#define ETIME 62 /* Timer expired */ -#define ENOSR 63 /* Out of streams resources */ -#define ENONET 64 /* Machine is not on the network */ -#define ENOPKG 65 /* Package not installed */ -#define EREMOTE 66 /* The object is remote */ -#define ENOLINK 67 /* The link has been severed */ -#define EADV 68 /* Advertise error */ -#define ESRMNT 69 /* Srmount error */ -#define ECOMM 70 /* Communication error on send */ -#define EPROTO 71 /* Protocol error */ -#define EMULTIHOP 74 /* Multihop attempted */ -#define ELBIN 75 /* Inode is remote (not really error) */ -#define EDOTDOT 76 /* Cross mount point (not really error) */ -#define EBADMSG 77 /* Trying to read unreadable message */ -#define ENOTUNIQ 80 /* Given log. name not unique */ -#define EBADFD 81 /* f.d. invalid for this operation */ -#define EREMCHG 82 /* Remote address changed */ -#define ELIBACC 83 /* Can't access a needed shared lib */ -#define ELIBBAD 84 /* Accessing a corrupted shared lib */ -#define ELIBSCN 85 /* .lib section in a.out corrupted */ -#define ELIBMAX 86 /* Attempting to link in too many libs */ -#define ELIBEXEC 87 /* Attempting to exec a shared library */ +#define EBFONT 57 /* Bad font file fmt */ +#define ENOSTR 60 /* Device not a stream */ +#define ENODATA 61 /* No data (for no delay io) */ +#define ETIME 62 /* Timer expired */ +#define ENOSR 63 /* Out of streams resources */ +#define ENONET 64 /* Machine is not on the network */ +#define ENOPKG 65 /* Package not installed */ +#define EREMOTE 66 /* The object is remote */ +#define ENOLINK 67 /* The link has been severed */ +#define EADV 68 /* Advertise error */ +#define ESRMNT 69 /* Srmount error */ +#define ECOMM 70 /* Communication error on send */ +#define EPROTO 71 /* Protocol error */ +#define EMULTIHOP 74 /* Multihop attempted */ +#define ELBIN 75 /* Inode is remote (not really error) */ +#define EDOTDOT 76 /* Cross mount point (not really error) */ +#define EBADMSG 77 /* Trying to read unreadable message */ +#define ENOTUNIQ 80 /* Given log. name not unique */ +#define EBADFD 81 /* f.d. invalid for this operation */ +#define EREMCHG 82 /* Remote address changed */ +#define ELIBACC 83 /* Can't access a needed shared lib */ +#define ELIBBAD 84 /* Accessing a corrupted shared lib */ +#define ELIBSCN 85 /* .lib section in a.out corrupted */ +#define ELIBMAX 86 /* Attempting to link in too many libs */ +#define ELIBEXEC 87 /* Attempting to exec a shared library */ #ifndef ENOSYS - #define ENOSYS 88 /* Function not implemented */ +#define ENOSYS 88 /* Function not implemented */ #endif -#define ENMFILE 89 /* No more files */ +#define ENMFILE 89 /* No more files */ #ifndef ENOTEMPTY - #define ENOTEMPTY 90 /* Directory not empty */ +#define ENOTEMPTY 90 /* Directory not empty */ #endif #ifndef ENAMETOOLONG - #define ENAMETOOLONG 91 /* File or path name too long */ +#define ENAMETOOLONG 91 /* File or path name too long */ #endif -#define ELOOP 92 /* Too many symbolic links */ -#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ -#define EPFNOSUPPORT 96 /* Protocol family not supported */ -#define ECONNRESET 104 /* Connection reset by peer */ -#define ENOBUFS 105 /* No buffer space available */ -#define EAFNOSUPPORT 106 /* Address family not supported by protocol family */ -#define EPROTOTYPE 107 /* Protocol wrong type for socket */ -#define ENOTSOCK 108 /* Socket operation on non-socket */ -#define ENOPROTOOPT 109 /* Protocol not available */ -#define ESHUTDOWN 110 /* Can't send after socket shutdown */ -#define ECONNREFUSED 111 /* Connection refused */ -#define EADDRINUSE 112 /* Address already in use */ -#define ECONNABORTED 113 /* Connection aborted */ -#define ENETUNREACH 114 /* Network is unreachable */ -#define ENETDOWN 115 /* Network interface is not configured */ +#define ELOOP 92 /* Too many symbolic links */ +#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ +#define EPFNOSUPPORT 96 /* Protocol family not supported */ +#define ECONNRESET 104 /* Connection reset by peer */ +#define ENOBUFS 105 /* No buffer space available */ +#define EAFNOSUPPORT 106 /* Address family not supported by protocol family */ +#define EPROTOTYPE 107 /* Protocol wrong type for socket */ +#define ENOTSOCK 108 /* Socket operation on non-socket */ +#define ENOPROTOOPT 109 /* Protocol not available */ +#define ESHUTDOWN 110 /* Can't send after socket shutdown */ +#define ECONNREFUSED 111 /* Connection refused */ +#define EADDRINUSE 112 /* Address already in use */ +#define ECONNABORTED 113 /* Connection aborted */ +#define ENETUNREACH 114 /* Network is unreachable */ +#define ENETDOWN 115 /* Network interface is not configured */ #ifndef ETIMEDOUT - #define ETIMEDOUT 116 /* Connection timed out */ +#define ETIMEDOUT 116 /* Connection timed out */ #endif -#define EHOSTDOWN 117 /* Host is down */ -#define EHOSTUNREACH 118 /* Host is unreachable */ -#define EINPROGRESS 119 /* Connection already in progress */ -#define EALREADY 120 /* Socket already connected */ -#define EDESTADDRREQ 121 /* Destination address required */ -#define EMSGSIZE 122 /* Message too long */ -#define EPROTONOSUPPORT 123 /* Unknown protocol */ -#define ESOCKTNOSUPPORT 124 /* Socket type not supported */ -#define EADDRNOTAVAIL 125 /* Address not available */ -#define ENETRESET 126 /* Connection aborted by network */ -#define EISCONN 127 /* Socket is already connected */ -#define ENOTCONN 128 /* Socket is not connected */ -#define ETOOMANYREFS 129 /* Too many references: cannot splice */ -#define EPROCLIM 130 /* Too many processes */ -#define EUSERS 131 /* Too many users */ -#define EDQUOT 132 /* Disk quota exceeded */ -#define ESTALE 133 /* Unknown error */ +#define EHOSTDOWN 117 /* Host is down */ +#define EHOSTUNREACH 118 /* Host is unreachable */ +#define EINPROGRESS 119 /* Connection already in progress */ +#define EALREADY 120 /* Socket already connected */ +#define EDESTADDRREQ 121 /* Destination address required */ +#define EMSGSIZE 122 /* Message too long */ +#define EPROTONOSUPPORT 123 /* Unknown protocol */ +#define ESOCKTNOSUPPORT 124 /* Socket type not supported */ +#define EADDRNOTAVAIL 125 /* Address not available */ +#define ENETRESET 126 /* Connection aborted by network */ +#define EISCONN 127 /* Socket is already connected */ +#define ENOTCONN 128 /* Socket is not connected */ +#define ETOOMANYREFS 129 /* Too many references: cannot splice */ +#define EPROCLIM 130 /* Too many processes */ +#define EUSERS 131 /* Too many users */ +#define EDQUOT 132 /* Disk quota exceeded */ +#define ESTALE 133 /* Unknown error */ #ifndef ENOTSUP - #define ENOTSUP 134 /* Not supported */ +#define ENOTSUP 134 /* Not supported */ #endif -#define ENOMEDIUM 135 /* No medium (in tape drive) */ -#define ENOSHARE 136 /* No such host or network path */ -#define ECASECLASH 137 /* Filename exists with different case */ -#define EWOULDBLOCK EAGAIN /* Operation would block */ -#define EOVERFLOW 139 /* Value too large for defined data type */ +#define ENOMEDIUM 135 /* No medium (in tape drive) */ +#define ENOSHARE 136 /* No such host or network path */ +#define ECASECLASH 137 /* Filename exists with different case */ +#define EWOULDBLOCK EAGAIN /* Operation would block */ +#define EOVERFLOW 139 /* Value too large for defined data type */ #undef HOST_NOT_FOUND #define HOST_NOT_FOUND 1 @@ -207,65 +208,65 @@ struct stat64 #define PROT_READ 0x1 #define PROT_WRITE 0x2 #define MAP_SHARED 0x1 -#define MAP_PRIVATE 0x2 /* unsupported */ +#define MAP_PRIVATE 0x2 /* unsupported */ #define MAP_FIXED 0x10 #define MAP_FAILED ((void *)-1) -struct statfs -{ - long f_type; /* type of filesystem (see below) */ - long f_bsize; /* optimal transfer block size */ - long f_blocks; /* total data blocks in file system */ - long f_bfree; /* free blocks in fs */ - long f_bavail; /* free blocks avail to non-superuser */ - long f_files; /* total file nodes in file system */ - long f_ffree; /* free file nodes in fs */ - long f_fsid; /* file system id */ - long f_namelen; /* maximum length of filenames */ - long f_spare[6]; /* spare for later */ -}; + struct statfs + { + long f_type; /* type of filesystem (see below) */ + long f_bsize; /* optimal transfer block size */ + long f_blocks; /* total data blocks in file system */ + long f_bfree; /* free blocks in fs */ + long f_bavail; /* free blocks avail to non-superuser */ + long f_files; /* total file nodes in file system */ + long f_ffree; /* free file nodes in fs */ + long f_fsid; /* file system id */ + long f_namelen; /* maximum length of filenames */ + long f_spare[6]; /* spare for later */ + }; /* Taken from the Wine project <http://www.winehq.org> /wine/include/winternl.h */ -enum SYSTEM_INFORMATION_CLASS -{ - SystemBasicInformation = 0, - Unknown1, - SystemPerformanceInformation = 2, - SystemTimeOfDayInformation = 3, /* was SystemTimeInformation */ - Unknown4, - SystemProcessInformation = 5, - Unknown6, - Unknown7, - SystemProcessorPerformanceInformation = 8, - Unknown9, - Unknown10, - SystemDriverInformation, - Unknown12, - Unknown13, - Unknown14, - Unknown15, - SystemHandleList, - Unknown17, - Unknown18, - Unknown19, - Unknown20, - SystemCacheInformation, - Unknown22, - SystemInterruptInformation = 23, - SystemExceptionInformation = 33, - SystemRegistryQuotaInformation = 37, - SystemLookasideInformation = 45 -}; - -typedef struct -{ + enum SYSTEM_INFORMATION_CLASS + { + SystemBasicInformation = 0, + Unknown1, + SystemPerformanceInformation = 2, + SystemTimeOfDayInformation = 3, /* was SystemTimeInformation */ + Unknown4, + SystemProcessInformation = 5, + Unknown6, + Unknown7, + SystemProcessorPerformanceInformation = 8, + Unknown9, + Unknown10, + SystemDriverInformation, + Unknown12, + Unknown13, + Unknown14, + Unknown15, + SystemHandleList, + Unknown17, + Unknown18, + Unknown19, + Unknown20, + SystemCacheInformation, + Unknown22, + SystemInterruptInformation = 23, + SystemExceptionInformation = 33, + SystemRegistryQuotaInformation = 37, + SystemLookasideInformation = 45 + }; + + typedef struct + { LARGE_INTEGER IdleTime; LARGE_INTEGER KernelTime; LARGE_INTEGER UserTime; LARGE_INTEGER Reserved1[2]; ULONG Reserved2; -} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION; + } SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION; #define sleep(secs) (Sleep(secs * 1000)) @@ -282,11 +283,11 @@ typedef struct #define SHUT_RDWR SD_BOTH /* Operations for flock() */ -#define LOCK_SH 1 /* shared lock */ -#define LOCK_EX 2 /* exclusive lock */ -#define LOCK_NB 4 /* or'd with one of the above to prevent - blocking */ -#define LOCK_UN 8 /* remove lock */ +#define LOCK_SH 1 /* shared lock */ +#define LOCK_EX 2 /* exclusive lock */ +#define LOCK_NB 4 /* or'd with one of the above to prevent + blocking */ +#define LOCK_UN 8 /* remove lock */ /* Not supported under MinGW */ #define S_IRGRP 0 @@ -308,272 +309,273 @@ typedef struct */ #define index(s, c) strchr(s, c) -BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest); -BOOL _plibc_DereferenceShortcut(char *pszShortcut); -char *plibc_ChooseDir(char *pszTitle, unsigned long ulFlags); -char *plibc_ChooseFile(char *pszTitle, unsigned long ulFlags); -long QueryRegistry(HKEY hMainKey, char *pszKey, char *pszSubKey, - char *pszBuffer, long *pdLength); - -BOOL __win_IsHandleMarkedAsBlocking(SOCKET hHandle); -void __win_SetHandleBlockingMode(SOCKET s, BOOL bBlocking); -void __win_DiscardHandleBlockingMode(SOCKET s); -int _win_isSocketValid(int s); -int plibc_conv_to_win_path(const char *pszUnix, char *pszWindows); - -typedef void (*TPanicProc) (int, char *); -void plibc_set_panic_proc(TPanicProc proc); - -int flock(int fd, int operation); -int fsync(int fildes); -int inet_pton(int af, const char *src, void *dst); -int inet_pton4(const char *src, u_char *dst, int pton); + BOOL _plibc_CreateShortcut (const char *pszSrc, const char *pszDest); + BOOL _plibc_DereferenceShortcut (char *pszShortcut); + char *plibc_ChooseDir (char *pszTitle, unsigned long ulFlags); + char *plibc_ChooseFile (char *pszTitle, unsigned long ulFlags); + long QueryRegistry (HKEY hMainKey, char *pszKey, char *pszSubKey, + char *pszBuffer, long *pdLength); + + BOOL __win_IsHandleMarkedAsBlocking (SOCKET hHandle); + void __win_SetHandleBlockingMode (SOCKET s, BOOL bBlocking); + void __win_DiscardHandleBlockingMode (SOCKET s); + int _win_isSocketValid (int s); + int plibc_conv_to_win_path (const char *pszUnix, char *pszWindows); + + typedef void (*TPanicProc) (int, char *); + void plibc_set_panic_proc (TPanicProc proc); + + int flock (int fd, int operation); + int fsync (int fildes); + int inet_pton (int af, const char *src, void *dst); + int inet_pton4 (const char *src, u_char * dst, int pton); #if USE_IPV6 -int inet_pton6(const char *src, u_char *dst); + int inet_pton6 (const char *src, u_char * dst); #endif -int truncate(const char *fname, int distance); -int statfs(const char *path, struct statfs *buf); -const char *hstrerror(int err); -void gettimeofday(struct timeval *tp, void *tzp); -int mkstemp(char *tmplate); -char *strptime (const char *buf, const char *format, struct tm *tm); -char *ctime(const time_t *clock); -char *ctime_r(const time_t *clock, char *buf); -int plibc_init(char *pszOrg, char *pszApp); -void plibc_shutdown(); -int plibc_initialized(); -int plibc_conv_to_win_path_ex(const char *pszUnix, char *pszWindows, int derefLinks); -void _SetErrnoFromWinError(long lWinError, char *pszCaller, int iLine); -void SetErrnoFromWinsockError(long lWinError); -void SetHErrnoFromWinError(long lWinError); -void SetErrnoFromHRESULT(HRESULT hRes); -FILE *_win_fopen(const char *filename, const char *mode); -DIR *_win_opendir(const char *dirname); -int _win_open(const char *filename, int oflag, ...); + int truncate (const char *fname, int distance); + int statfs (const char *path, struct statfs *buf); + const char *hstrerror (int err); + void gettimeofday (struct timeval *tp, void *tzp); + int mkstemp (char *tmplate); + char *strptime (const char *buf, const char *format, struct tm *tm); + char *ctime (const time_t * clock); + char *ctime_r (const time_t * clock, char *buf); + int plibc_init (char *pszOrg, char *pszApp); + void plibc_shutdown (); + int plibc_initialized (); + int plibc_conv_to_win_path_ex (const char *pszUnix, char *pszWindows, + int derefLinks); + void _SetErrnoFromWinError (long lWinError, char *pszCaller, int iLine); + void SetErrnoFromWinsockError (long lWinError); + void SetHErrnoFromWinError (long lWinError); + void SetErrnoFromHRESULT (HRESULT hRes); + FILE *_win_fopen (const char *filename, const char *mode); + DIR *_win_opendir (const char *dirname); + int _win_open (const char *filename, int oflag, ...); #ifdef ENABLE_NLS -char *_win_bindtextdomain(const char *domainname, const char *dirname); + char *_win_bindtextdomain (const char *domainname, const char *dirname); #endif -int _win_chdir(const char *path); -int _win_close(int fd); -int _win_creat(const char *path, mode_t mode); -int _win_fstat(int handle, struct stat *buffer); -int _win_pipe(int *phandles); -int _win_rmdir(const char *path); -int _win_access( const char *path, int mode ); -int _win_chmod(const char *filename, int pmode); -char *realpath(const char *file_name, char *resolved_name); -long _win_random(void); -int _win_remove(const char *path); -int _win_rename(const char *oldname, const char *newname); -int _win_stat(const char *path, struct stat *buffer); -int _win_stat64(const char *path, struct stat64 *buffer); -int _win_unlink(const char *filename); -int _win_write(int fildes, const void *buf, size_t nbyte); -int _win_read(int fildes, void *buf, size_t nbyte); -size_t _win_fwrite(const void *buffer, size_t size, size_t count, FILE *stream); -size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream ); -int _win_symlink(const char *path1, const char *path2); -void *_win_mmap(void *start, size_t len, int access, int flags, int fd, - unsigned long long offset); -int _win_munmap(void *start, size_t length); -int _win_lstat(const char *path, struct stat *buf); -int _win_lstat64(const char *path, struct stat64 *buf); -int _win_readlink(const char *path, char *buf, size_t bufsize); -int _win_accept(SOCKET s, struct sockaddr *addr, int *addrlen); -int _win_printf(const char *format,...); -int _win_fprintf(FILE *f,const char *format,...); -int _win_vprintf(const char *format, va_list ap); -int _win_vfprintf(FILE *stream, const char *format, va_list arg_ptr); -int _win_vsprintf(char *dest,const char *format, va_list arg_ptr); -int _win_vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr); -int _win_snprintf(char *str,size_t size,const char *format,...); -int _win_sprintf(char *dest,const char *format,...); -int _win_vsscanf(const char* str, const char* format, va_list arg_ptr); -int _win_sscanf(const char *str, const char *format, ...); -int _win_vfscanf(FILE *stream, const char *format, va_list arg_ptr); -int _win_vscanf(const char *format, va_list arg_ptr); -int _win_scanf(const char *format, ...); -int _win_fscanf(FILE *stream, const char *format, ...); -pid_t _win_waitpid(pid_t pid, int *stat_loc, int options); -int _win_bind(SOCKET s, const struct sockaddr *name, int namelen); -int _win_connect(SOCKET s,const struct sockaddr *name, int namelen); -int _win_getpeername(SOCKET s, struct sockaddr *name, - int *namelen); -int _win_getsockname(SOCKET s, struct sockaddr *name, - int *namelen); -int _win_getsockopt(SOCKET s, int level, int optname, char *optval, - int *optlen); -int _win_listen(SOCKET s, int backlog); -int _win_recv(SOCKET s, char *buf, int len, int flags); -int _win_recvfrom(SOCKET s, void *buf, int len, int flags, - struct sockaddr *from, int *fromlen); -int _win_select(int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds, - const struct timeval *tv); -int _win_send(SOCKET s, const char *buf, int len, int flags); -int _win_sendto(SOCKET s, const char *buf, int len, int flags, - const struct sockaddr *to, int tolen); -int _win_setsockopt(SOCKET s, int level, int optname, const void *optval, - int optlen); -int _win_shutdown(SOCKET s, int how); -SOCKET _win_socket(int af, int type, int protocol); -struct hostent *_win_gethostbyaddr(const char *addr, int len, int type); -struct hostent *_win_gethostbyname(const char *name); -char *_win_strerror(int errnum); -int IsWinNT(); + int _win_chdir (const char *path); + int _win_close (int fd); + int _win_creat (const char *path, mode_t mode); + int _win_fstat (int handle, struct stat *buffer); + int _win_pipe (int *phandles); + int _win_rmdir (const char *path); + int _win_access (const char *path, int mode); + int _win_chmod (const char *filename, int pmode); + char *realpath (const char *file_name, char *resolved_name); + long _win_random (void); + int _win_remove (const char *path); + int _win_rename (const char *oldname, const char *newname); + int _win_stat (const char *path, struct stat *buffer); + int _win_stat64 (const char *path, struct stat64 *buffer); + int _win_unlink (const char *filename); + int _win_write (int fildes, const void *buf, size_t nbyte); + int _win_read (int fildes, void *buf, size_t nbyte); + size_t _win_fwrite (const void *buffer, size_t size, size_t count, + FILE * stream); + size_t _win_fread (void *buffer, size_t size, size_t count, FILE * stream); + int _win_symlink (const char *path1, const char *path2); + void *_win_mmap (void *start, size_t len, int access, int flags, int fd, + unsigned long long offset); + int _win_munmap (void *start, size_t length); + int _win_lstat (const char *path, struct stat *buf); + int _win_lstat64 (const char *path, struct stat64 *buf); + int _win_readlink (const char *path, char *buf, size_t bufsize); + int _win_accept (SOCKET s, struct sockaddr *addr, int *addrlen); + int _win_printf (const char *format, ...); + int _win_fprintf (FILE * f, const char *format, ...); + int _win_vprintf (const char *format, va_list ap); + int _win_vfprintf (FILE * stream, const char *format, va_list arg_ptr); + int _win_vsprintf (char *dest, const char *format, va_list arg_ptr); + int _win_vsnprintf (char *str, size_t size, const char *format, + va_list arg_ptr); + int _win_snprintf (char *str, size_t size, const char *format, ...); + int _win_sprintf (char *dest, const char *format, ...); + int _win_vsscanf (const char *str, const char *format, va_list arg_ptr); + int _win_sscanf (const char *str, const char *format, ...); + int _win_vfscanf (FILE * stream, const char *format, va_list arg_ptr); + int _win_vscanf (const char *format, va_list arg_ptr); + int _win_scanf (const char *format, ...); + int _win_fscanf (FILE * stream, const char *format, ...); + pid_t _win_waitpid (pid_t pid, int *stat_loc, int options); + int _win_bind (SOCKET s, const struct sockaddr *name, int namelen); + int _win_connect (SOCKET s, const struct sockaddr *name, int namelen); + int _win_getpeername (SOCKET s, struct sockaddr *name, int *namelen); + int _win_getsockname (SOCKET s, struct sockaddr *name, int *namelen); + int _win_getsockopt (SOCKET s, int level, int optname, char *optval, + int *optlen); + int _win_listen (SOCKET s, int backlog); + int _win_recv (SOCKET s, char *buf, int len, int flags); + int _win_recvfrom (SOCKET s, void *buf, int len, int flags, + struct sockaddr *from, int *fromlen); + int _win_select (int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds, + const struct timeval *tv); + int _win_send (SOCKET s, const char *buf, int len, int flags); + int _win_sendto (SOCKET s, const char *buf, int len, int flags, + const struct sockaddr *to, int tolen); + int _win_setsockopt (SOCKET s, int level, int optname, const void *optval, + int optlen); + int _win_shutdown (SOCKET s, int how); + SOCKET _win_socket (int af, int type, int protocol); + struct hostent *_win_gethostbyaddr (const char *addr, int len, int type); + struct hostent *_win_gethostbyname (const char *name); + char *_win_strerror (int errnum); + int IsWinNT (); #if !HAVE_STRNDUP -char *strndup (const char *s, size_t n); + char *strndup (const char *s, size_t n); #endif #if !HAVE_STRNLEN -size_t strnlen (const char *str, size_t maxlen); + size_t strnlen (const char *str, size_t maxlen); #endif #define strcasecmp(a, b) stricmp(a, b) #define strncasecmp(a, b, c) strnicmp(a, b, c) -#endif /* WINDOWS */ +#endif /* WINDOWS */ #ifndef WINDOWS - #define DIR_SEPARATOR '/' - #define DIR_SEPARATOR_STR "/" - #define PATH_SEPARATOR ';' - #define PATH_SEPARATOR_STR ";" - #define NEWLINE "\n" +#define DIR_SEPARATOR '/' +#define DIR_SEPARATOR_STR "/" +#define PATH_SEPARATOR ';' +#define PATH_SEPARATOR_STR ";" +#define NEWLINE "\n" #ifdef ENABLE_NLS - #define BINDTEXTDOMAIN(d, n) bindtextdomain(d, n) +#define BINDTEXTDOMAIN(d, n) bindtextdomain(d, n) #endif - #define CREAT(p, m) creat(p, m) - #undef FOPEN - #define FOPEN(f, m) fopen(f, m) - #define OPENDIR(d) opendir(d) - #define OPEN(f) open(f) - #define CHDIR(d) chdir(d) - #define CLOSE(f) close(f) - #define RMDIR(f) rmdir(f) - #define ACCESS(p, m) access(p, m) - #define CHMOD(f, p) chmod(f, p) - #define FSTAT(h, b) fstat(h, b) - #define PIPE(h) pipe(h) - #define REMOVE(p) remove(p) - #define RENAME(o, n) rename(o, n) - #define STAT(p, b) stat(p, b) - #define STAT64(p, b) stat64(p, b) - #define UNLINK(f) unlink(f) - #define WRITE(f, b, n) write(f, b, n) - #define READ(f, b, n) read(f, b, n) - #define GN_FREAD(b, s, c, f) fread(b, s, c, f) - #define GN_FWRITE(b, s, c, f) fwrite(b, s, c, f) - #define SYMLINK(a, b) symlink(a, b) - #define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o) - #define MUNMAP(s, l) munmap(s, l) - #define STRERROR(i) strerror(i) - #define RANDOM() random() - #define READLINK(p, b, s) readlink(p, b, s) - #define LSTAT(p, b) lstat(p, b) - #define LSTAT64(p, b) lstat64(p, b) - #define PRINTF printf - #define FPRINTF fprintf - #define VPRINTF(f, a) vprintf(f, a) - #define VFPRINTF(s, f, a) vfprintf(s, f, a) - #define VSPRINTF(d, f, a) vsprintf(d, f, a) - #define VSNPRINTF(str, size, fmt, a) vsnprintf(str, size, fmt, a) - #define _REAL_SNPRINTF snprintf - #define SPRINTF sprintf - #define VSSCANF(s, f, a) vsscanf(s, f, a) - #define SSCANF sscanf - #define VFSCANF(s, f, a) vfscanf(s, f, a) - #define VSCANF(f, a) vscanf(f, a) - #define SCANF scanf - #define FSCANF fscanf - #define WAITPID(p, s, o) waitpid(p, s, o) - #define ACCEPT(s, a, l) accept(s, a, l) - #define BIND(s, n, l) bind(s, n, l) - #define CONNECT(s, n, l) connect(s, n, l) - #define GETPEERNAME(s, n, l) getpeername(s, n, l) - #define GETSOCKNAME(s, n, l) getsockname(s, n, l) - #define GETSOCKOPT(s, l, o, v, p) getsockopt(s, l, o, v, p) - #define LISTEN(s, b) listen(s, b) - #define RECV(s, b, l, f) recv(s, b, l, f) - #define RECVFROM(s, b, l, f, r, o) recvfrom(s, b, l, f, r, o) - #define SELECT(n, r, w, e, t) select(n, r, w, e, t) - #define SEND(s, b, l, f) send(s, b, l, f) - #define SENDTO(s, b, l, f, o, n) sendto(s, b, l, f, o, n) - #define SETSOCKOPT(s, l, o, v, n) setsockopt(s, l, o, v, n) - #define SHUTDOWN(s, h) shutdown(s, h) - #define SOCKET(a, t, p) socket(a, t, p) - #define GETHOSTBYADDR(a, l, t) gethostbyname(a, l, t) - #define GETHOSTBYNAME(n) gethostbyname(n) +#define CREAT(p, m) creat(p, m) +#undef FOPEN +#define FOPEN(f, m) fopen(f, m) +#define OPENDIR(d) opendir(d) +#define OPEN(f) open(f) +#define CHDIR(d) chdir(d) +#define CLOSE(f) close(f) +#define RMDIR(f) rmdir(f) +#define ACCESS(p, m) access(p, m) +#define CHMOD(f, p) chmod(f, p) +#define FSTAT(h, b) fstat(h, b) +#define PIPE(h) pipe(h) +#define REMOVE(p) remove(p) +#define RENAME(o, n) rename(o, n) +#define STAT(p, b) stat(p, b) +#define STAT64(p, b) stat64(p, b) +#define UNLINK(f) unlink(f) +#define WRITE(f, b, n) write(f, b, n) +#define READ(f, b, n) read(f, b, n) +#define GN_FREAD(b, s, c, f) fread(b, s, c, f) +#define GN_FWRITE(b, s, c, f) fwrite(b, s, c, f) +#define SYMLINK(a, b) symlink(a, b) +#define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o) +#define MUNMAP(s, l) munmap(s, l) +#define STRERROR(i) strerror(i) +#define RANDOM() random() +#define READLINK(p, b, s) readlink(p, b, s) +#define LSTAT(p, b) lstat(p, b) +#define LSTAT64(p, b) lstat64(p, b) +#define PRINTF printf +#define FPRINTF fprintf +#define VPRINTF(f, a) vprintf(f, a) +#define VFPRINTF(s, f, a) vfprintf(s, f, a) +#define VSPRINTF(d, f, a) vsprintf(d, f, a) +#define VSNPRINTF(str, size, fmt, a) vsnprintf(str, size, fmt, a) +#define _REAL_SNPRINTF snprintf +#define SPRINTF sprintf +#define VSSCANF(s, f, a) vsscanf(s, f, a) +#define SSCANF sscanf +#define VFSCANF(s, f, a) vfscanf(s, f, a) +#define VSCANF(f, a) vscanf(f, a) +#define SCANF scanf +#define FSCANF fscanf +#define WAITPID(p, s, o) waitpid(p, s, o) +#define ACCEPT(s, a, l) accept(s, a, l) +#define BIND(s, n, l) bind(s, n, l) +#define CONNECT(s, n, l) connect(s, n, l) +#define GETPEERNAME(s, n, l) getpeername(s, n, l) +#define GETSOCKNAME(s, n, l) getsockname(s, n, l) +#define GETSOCKOPT(s, l, o, v, p) getsockopt(s, l, o, v, p) +#define LISTEN(s, b) listen(s, b) +#define RECV(s, b, l, f) recv(s, b, l, f) +#define RECVFROM(s, b, l, f, r, o) recvfrom(s, b, l, f, r, o) +#define SELECT(n, r, w, e, t) select(n, r, w, e, t) +#define SEND(s, b, l, f) send(s, b, l, f) +#define SENDTO(s, b, l, f, o, n) sendto(s, b, l, f, o, n) +#define SETSOCKOPT(s, l, o, v, n) setsockopt(s, l, o, v, n) +#define SHUTDOWN(s, h) shutdown(s, h) +#define SOCKET(a, t, p) socket(a, t, p) +#define GETHOSTBYADDR(a, l, t) gethostbyname(a, l, t) +#define GETHOSTBYNAME(n) gethostbyname(n) #else - #define DIR_SEPARATOR '\\' - #define DIR_SEPARATOR_STR "\\" - #define PATH_SEPARATOR ':' - #define PATH_SEPARATOR_STR ":" - #define NEWLINE "\r\n" +#define DIR_SEPARATOR '\\' +#define DIR_SEPARATOR_STR "\\" +#define PATH_SEPARATOR ':' +#define PATH_SEPARATOR_STR ":" +#define NEWLINE "\r\n" #ifdef ENABLE_NLS - #define BINDTEXTDOMAIN(d, n) _win_bindtextdomain(d, n) +#define BINDTEXTDOMAIN(d, n) _win_bindtextdomain(d, n) #endif - #define CREAT(p, m) _win_creat(p, m) - #define FOPEN(f, m) _win_fopen(f, m) - #define OPENDIR(d) _win_opendir(d) - #define OPEN(f) _win_open(f) - #define CHDIR(d) _win_chdir(d) - #define CLOSE(f) _win_close(f) - #define FSTAT(h, b) _win_fstat(h, b) - #define RMDIR(f) _win_rmdir(f) - #define ACCESS(p, m) _win_access(p, m) - #define CHMOD(f, p) _win_chmod(f, p) - #define PIPE(h) _win_pipe(h) - #define RANDOM() _win_random() - #define REMOVE(p) _win_remove(p) - #define RENAME(o, n) _win_rename(o, n) - #define STAT(p, b) _win_stat(p, b) - #define STAT64(p, b) _win_stat64(p, b) - #define UNLINK(f) _win_unlink(f) - #define WRITE(f, b, n) _win_write(f, b, n) - #define READ(f, b, n) _win_read(f, b, n) - #define GN_FREAD(b, s, c, f) _win_fread(b, s, c, f) - #define GN_FWRITE(b, s, c, f) _win_fwrite(b, s, c, f) - #define SYMLINK(a, b) _win_symlink(a, b) - #define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o) - #define MUNMAP(s, l) _win_munmap(s, l) - #define STRERROR(i) _win_strerror(i) - #define READLINK(p, b, s) _win_readlink(p, b, s) - #define LSTAT(p, b) _win_lstat(p, b) - #define LSTAT64(p, b) _win_lstat64(p, b) - #define PRINTF(f, ...) _win_printf(f , __VA_ARGS__) - #define FPRINTF(fil, fmt, ...) _win_fprintf(fil, fmt, __VA_ARGS__) - #define VPRINTF(f, a) _win_vprintf(f, a) - #define VFPRINTF(s, f, a) _win_vfprintf(s, f, a) - #define VSPRINTF(d, f, a) _win_vsprintf(d, f, a) - #define VSNPRINTF(str, size, fmt, a) _win_vsnprintf(str, size, fmt, a) - #define _REAL_SNPRINTF(str, size, fmt, ...) _win_snprintf(str, size, fmt, __VA_ARGS__) - #define SPRINTF(d, f, ...) _win_sprintf(d, f, __VA_ARGS__) - #define VSSCANF(s, f, a) _win_vsscanf(s, f, a) - #define SSCANF(s, f, ...) _win_sscanf(s, f, __VA_ARGS__) - #define VFSCANF(s, f, a) _win_vfscanf(s, f, a) - #define VSCANF(f, a) _win_vscanf(f, a) - #define SCANF(f, ...) _win_scanf(f, __VA_ARGS__) - #define FSCANF(s, f, ...) _win_fscanf(s, f, __VA_ARGS__) - #define WAITPID(p, s, o) _win_waitpid(p, s, o) - #define ACCEPT(s, a, l) _win_accept(s, a, l) - #define BIND(s, n, l) _win_bind(s, n, l) - #define CONNECT(s, n, l) _win_connect(s, n, l) - #define GETPEERNAME(s, n, l) _win_getpeername(s, n, l) - #define GETSOCKNAME(s, n, l) _win_getsockname(s, n, l) - #define GETSOCKOPT(s, l, o, v, p) _win_getsockopt(s, l, o, v, p) - #define LISTEN(s, b) _win_listen(s, b) - #define RECV(s, b, l, f) _win_recv(s, b, l, f) - #define RECVFROM(s, b, l, f, r, o) _win_recvfrom(s, b, l, f, r, o) - #define SELECT(n, r, w, e, t) _win_select(n, r, w, e, t) - #define SEND(s, b, l, f) _win_send(s, b, l, f) - #define SENDTO(s, b, l, f, o, n) _win_sendto(s, b, l, f, o, n) - #define SETSOCKOPT(s, l, o, v, n) _win_setsockopt(s, l, o, v, n) - #define SHUTDOWN(s, h) _win_shutdown(s, h) - #define SOCKET(a, t, p) _win_socket(a, t, p) - #define GETHOSTBYADDR(a, l, t) _win_gethostbyname(a, l, t) - #define GETHOSTBYNAME(n) _win_gethostbyname(n) +#define CREAT(p, m) _win_creat(p, m) +#define FOPEN(f, m) _win_fopen(f, m) +#define OPENDIR(d) _win_opendir(d) +#define OPEN(f) _win_open(f) +#define CHDIR(d) _win_chdir(d) +#define CLOSE(f) _win_close(f) +#define FSTAT(h, b) _win_fstat(h, b) +#define RMDIR(f) _win_rmdir(f) +#define ACCESS(p, m) _win_access(p, m) +#define CHMOD(f, p) _win_chmod(f, p) +#define PIPE(h) _win_pipe(h) +#define RANDOM() _win_random() +#define REMOVE(p) _win_remove(p) +#define RENAME(o, n) _win_rename(o, n) +#define STAT(p, b) _win_stat(p, b) +#define STAT64(p, b) _win_stat64(p, b) +#define UNLINK(f) _win_unlink(f) +#define WRITE(f, b, n) _win_write(f, b, n) +#define READ(f, b, n) _win_read(f, b, n) +#define GN_FREAD(b, s, c, f) _win_fread(b, s, c, f) +#define GN_FWRITE(b, s, c, f) _win_fwrite(b, s, c, f) +#define SYMLINK(a, b) _win_symlink(a, b) +#define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o) +#define MUNMAP(s, l) _win_munmap(s, l) +#define STRERROR(i) _win_strerror(i) +#define READLINK(p, b, s) _win_readlink(p, b, s) +#define LSTAT(p, b) _win_lstat(p, b) +#define LSTAT64(p, b) _win_lstat64(p, b) +#define PRINTF(f, ...) _win_printf(f , __VA_ARGS__) +#define FPRINTF(fil, fmt, ...) _win_fprintf(fil, fmt, __VA_ARGS__) +#define VPRINTF(f, a) _win_vprintf(f, a) +#define VFPRINTF(s, f, a) _win_vfprintf(s, f, a) +#define VSPRINTF(d, f, a) _win_vsprintf(d, f, a) +#define VSNPRINTF(str, size, fmt, a) _win_vsnprintf(str, size, fmt, a) +#define _REAL_SNPRINTF(str, size, fmt, ...) _win_snprintf(str, size, fmt, __VA_ARGS__) +#define SPRINTF(d, f, ...) _win_sprintf(d, f, __VA_ARGS__) +#define VSSCANF(s, f, a) _win_vsscanf(s, f, a) +#define SSCANF(s, f, ...) _win_sscanf(s, f, __VA_ARGS__) +#define VFSCANF(s, f, a) _win_vfscanf(s, f, a) +#define VSCANF(f, a) _win_vscanf(f, a) +#define SCANF(f, ...) _win_scanf(f, __VA_ARGS__) +#define FSCANF(s, f, ...) _win_fscanf(s, f, __VA_ARGS__) +#define WAITPID(p, s, o) _win_waitpid(p, s, o) +#define ACCEPT(s, a, l) _win_accept(s, a, l) +#define BIND(s, n, l) _win_bind(s, n, l) +#define CONNECT(s, n, l) _win_connect(s, n, l) +#define GETPEERNAME(s, n, l) _win_getpeername(s, n, l) +#define GETSOCKNAME(s, n, l) _win_getsockname(s, n, l) +#define GETSOCKOPT(s, l, o, v, p) _win_getsockopt(s, l, o, v, p) +#define LISTEN(s, b) _win_listen(s, b) +#define RECV(s, b, l, f) _win_recv(s, b, l, f) +#define RECVFROM(s, b, l, f, r, o) _win_recvfrom(s, b, l, f, r, o) +#define SELECT(n, r, w, e, t) _win_select(n, r, w, e, t) +#define SEND(s, b, l, f) _win_send(s, b, l, f) +#define SENDTO(s, b, l, f, o, n) _win_sendto(s, b, l, f, o, n) +#define SETSOCKOPT(s, l, o, v, n) _win_setsockopt(s, l, o, v, n) +#define SHUTDOWN(s, h) _win_shutdown(s, h) +#define SOCKET(a, t, p) _win_socket(a, t, p) +#define GETHOSTBYADDR(a, l, t) _win_gethostbyname(a, l, t) +#define GETHOSTBYNAME(n) _win_gethostbyname(n) #endif @@ -582,6 +584,6 @@ size_t strnlen (const char *str, size_t maxlen); #endif -#endif //_PLIBC_H_ +#endif //_PLIBC_H_ /* end of plibc.h */ diff --git a/src/daemon/response.c b/src/daemon/response.c @@ -34,26 +34,25 @@ * @return MHD_NO on error (i.e. invalid header or content format). */ int -MHD_add_response_header(struct MHD_Response * response, - const char * header, - const char * content) { - struct MHD_HTTP_Header * hdr; +MHD_add_response_header (struct MHD_Response *response, + const char *header, const char *content) +{ + struct MHD_HTTP_Header *hdr; - if ( (response == NULL) || - (header == NULL) || - (content == NULL) || - (strlen(header) == 0) || - (strlen(content) == 0) || - (NULL != strstr(header, "\t")) || - (NULL != strstr(header, "\r")) || - (NULL != strstr(header, "\n")) || - (NULL != strstr(content, "\t")) || - (NULL != strstr(content, "\r")) || - (NULL != strstr(content, "\n")) ) + if ((response == NULL) || + (header == NULL) || + (content == NULL) || + (strlen (header) == 0) || + (strlen (content) == 0) || + (NULL != strstr (header, "\t")) || + (NULL != strstr (header, "\r")) || + (NULL != strstr (header, "\n")) || + (NULL != strstr (content, "\t")) || + (NULL != strstr (content, "\r")) || (NULL != strstr (content, "\n"))) return MHD_NO; - hdr = malloc(sizeof(struct MHD_HTTP_Header)); - hdr->header = strdup(header); - hdr->value = strdup(content); + hdr = malloc (sizeof (struct MHD_HTTP_Header)); + hdr->header = strdup (header); + hdr->value = strdup (content); hdr->kind = MHD_HEADER_KIND; hdr->next = response->first_header; response->first_header = hdr; @@ -66,32 +65,33 @@ MHD_add_response_header(struct MHD_Response * response, * @return MHD_NO on error (no such header known) */ int -MHD_del_response_header(struct MHD_Response * response, - const char * header, - const char * content) { - struct MHD_HTTP_Header * pos; - struct MHD_HTTP_Header * prev; +MHD_del_response_header (struct MHD_Response *response, + const char *header, const char *content) +{ + struct MHD_HTTP_Header *pos; + struct MHD_HTTP_Header *prev; - if ( (header == NULL) || - (content == NULL) ) + if ((header == NULL) || (content == NULL)) return MHD_NO; prev = NULL; pos = response->first_header; - while (pos != NULL) { - if ( (0 == strcmp(header, pos->header)) && - (0 == strcmp(content, pos->value)) ) { - free(pos->header); - free(pos->value); - if (prev == NULL) - response->first_header = pos->next; - else - prev->next = pos->next; - free(pos); - return MHD_YES; + while (pos != NULL) + { + if ((0 == strcmp (header, pos->header)) && + (0 == strcmp (content, pos->value))) + { + free (pos->header); + free (pos->value); + if (prev == NULL) + response->first_header = pos->next; + else + prev->next = pos->next; + free (pos); + return MHD_YES; + } + prev = pos; + pos = pos->next; } - prev = pos; - pos = pos->next; - } return MHD_NO; } @@ -104,22 +104,21 @@ MHD_del_response_header(struct MHD_Response * response, * @return number of entries iterated over */ int -MHD_get_response_headers(struct MHD_Response * response, - MHD_KeyValueIterator iterator, - void * iterator_cls) { - struct MHD_HTTP_Header * pos; +MHD_get_response_headers (struct MHD_Response *response, + MHD_KeyValueIterator iterator, void *iterator_cls) +{ + struct MHD_HTTP_Header *pos; int numHeaders = 0; pos = response->first_header; - while (pos != NULL) { - numHeaders++; - if ( (iterator != NULL) && - (MHD_YES != iterator(iterator_cls, - pos->kind, - pos->header, - pos->value)) ) - break; - pos = pos->next; - } + while (pos != NULL) + { + numHeaders++; + if ((iterator != NULL) && + (MHD_YES != iterator (iterator_cls, + pos->kind, pos->header, pos->value))) + break; + pos = pos->next; + } return numHeaders; } @@ -131,16 +130,16 @@ MHD_get_response_headers(struct MHD_Response * response, * @return NULL if header does not exist */ const char * -MHD_get_response_header(struct MHD_Response * response, - const char * key) { - struct MHD_HTTP_Header * pos; +MHD_get_response_header (struct MHD_Response *response, const char *key) +{ + struct MHD_HTTP_Header *pos; pos = response->first_header; - while (pos != NULL) { - if (0 == strcmp(key, - pos->header)) - return pos->value; - pos = pos->next; - } + while (pos != NULL) + { + if (0 == strcmp (key, pos->header)) + return pos->value; + pos = pos->next; + } return NULL; } @@ -156,29 +155,30 @@ MHD_get_response_header(struct MHD_Response * response, * @return NULL on error (i.e. invalid arguments, out of memory) */ struct MHD_Response * -MHD_create_response_from_callback(size_t size, - MHD_ContentReaderCallback crc, - void * crc_cls, - MHD_ContentReaderFreeCallback crfc) { - struct MHD_Response * retVal; +MHD_create_response_from_callback (size_t size, + MHD_ContentReaderCallback crc, + void *crc_cls, + MHD_ContentReaderFreeCallback crfc) +{ + struct MHD_Response *retVal; if (crc == NULL) return NULL; - retVal = malloc(sizeof(struct MHD_Response)); - memset(retVal, - 0, - sizeof(struct MHD_Response)); - retVal->data = malloc(MHD_BUF_INC_SIZE); - if (retVal->data == NULL) { - free(retVal); - return NULL; - } + retVal = malloc (sizeof (struct MHD_Response)); + memset (retVal, 0, sizeof (struct MHD_Response)); + retVal->data = malloc (MHD_BUF_INC_SIZE); + if (retVal->data == NULL) + { + free (retVal); + return NULL; + } retVal->data_buffer_size = MHD_BUF_INC_SIZE; - if (pthread_mutex_init(&retVal->mutex, NULL) != 0) { - free(retVal->data); - free(retVal); - return NULL; - } + if (pthread_mutex_init (&retVal->mutex, NULL) != 0) + { + free (retVal->data); + free (retVal); + return NULL; + } retVal->crc = crc; retVal->crfc = crfc; retVal->crc_cls = crc_cls; @@ -200,33 +200,28 @@ MHD_create_response_from_callback(size_t size, * @return NULL on error (i.e. invalid arguments, out of memory) */ struct MHD_Response * -MHD_create_response_from_data(size_t size, - void * data, - int must_free, - int must_copy) { - struct MHD_Response * retVal; - void * tmp; +MHD_create_response_from_data (size_t size, + void *data, int must_free, int must_copy) +{ + struct MHD_Response *retVal; + void *tmp; - if ( (data == NULL) && - (size > 0) ) + if ((data == NULL) && (size > 0)) return NULL; - retVal = malloc(sizeof(struct MHD_Response)); - memset(retVal, - 0, - sizeof(struct MHD_Response)); - if (pthread_mutex_init(&retVal->mutex, NULL) != 0) { - free(retVal); - return NULL; - } - if ( (must_copy) && - (size > 0) ) { - tmp = malloc(size); - memcpy(tmp, - data, - size); - must_free = 1; - data = tmp; - } + retVal = malloc (sizeof (struct MHD_Response)); + memset (retVal, 0, sizeof (struct MHD_Response)); + if (pthread_mutex_init (&retVal->mutex, NULL) != 0) + { + free (retVal); + return NULL; + } + if ((must_copy) && (size > 0)) + { + tmp = malloc (size); + memcpy (tmp, data, size); + must_free = 1; + data = tmp; + } retVal->crc = NULL; retVal->crfc = must_free ? &free : NULL; retVal->crc_cls = must_free ? data : NULL; @@ -244,38 +239,42 @@ MHD_create_response_from_data(size_t size, * necessarily be freed immediatley. */ void -MHD_destroy_response(struct MHD_Response * response) { - struct MHD_HTTP_Header * pos; +MHD_destroy_response (struct MHD_Response *response) +{ + struct MHD_HTTP_Header *pos; if (response == NULL) - return; - pthread_mutex_lock(&response->mutex); - if (0 != --response->reference_count) { - pthread_mutex_unlock(&response->mutex); return; - } - pthread_mutex_unlock(&response->mutex); - pthread_mutex_destroy(&response->mutex); + pthread_mutex_lock (&response->mutex); + if (0 != --response->reference_count) + { + pthread_mutex_unlock (&response->mutex); + return; + } + pthread_mutex_unlock (&response->mutex); + pthread_mutex_destroy (&response->mutex); if (response->crfc != NULL) - response->crfc(response->crc_cls); - while (response->first_header != NULL) { - pos = response->first_header; - response->first_header = pos->next; - free(pos->header); - free(pos->value); - free(pos); - } - if (response->crc != NULL) - free(response->data); - free(response); + response->crfc (response->crc_cls); + while (response->first_header != NULL) + { + pos = response->first_header; + response->first_header = pos->next; + free (pos->header); + free (pos->value); + free (pos); + } + if (response->crc != NULL) + free (response->data); + free (response); } void -MHD_increment_response_rc(struct MHD_Response * response) { - pthread_mutex_lock(&response->mutex); +MHD_increment_response_rc (struct MHD_Response *response) +{ + pthread_mutex_lock (&response->mutex); response->reference_count++; - pthread_mutex_unlock(&response->mutex); + pthread_mutex_unlock (&response->mutex); } diff --git a/src/daemon/response.h b/src/daemon/response.h @@ -32,7 +32,7 @@ * Increment response RC. Should this be part of the * public API? */ -void MHD_increment_response_rc(struct MHD_Response * response); +void MHD_increment_response_rc (struct MHD_Response *response); #endif diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h @@ -52,8 +52,9 @@ #endif #ifdef __cplusplus -extern "C" { -#if 0 /* keep Emacsens' auto-indent happy */ +extern "C" +{ +#if 0 /* keep Emacsens' auto-indent happy */ } #endif #endif @@ -216,7 +217,8 @@ extern "C" { * implemented or not supported on the target platform (i.e. no * support for SSL, threads or IPv6). */ -enum MHD_FLAG { +enum MHD_FLAG +{ /** * No options selected. */ @@ -256,7 +258,8 @@ enum MHD_FLAG { * MHD options. Passed in the varargs portion * of MHD_start_daemon. */ -enum MHD_OPTION { +enum MHD_OPTION +{ /** * No more options / last option. This is used @@ -282,7 +285,8 @@ enum MHD_OPTION { * The MHD_ValueKind specifies the source of * the key-value pairs in the HTTP protocol. */ -enum MHD_ValueKind { +enum MHD_ValueKind +{ /** * Response header @@ -344,9 +348,9 @@ struct MHD_Response; * @return MHD_YES if connection is allowed, MHD_NO if not */ typedef int -(*MHD_AcceptPolicyCallback)(void * cls, - const struct sockaddr * addr, - socklen_t addrlen); + (*MHD_AcceptPolicyCallback) (void *cls, + const struct sockaddr * addr, + socklen_t addrlen); /** * A client has requested the given url using the given method ("GET", @@ -372,13 +376,13 @@ typedef int * error while handling the request */ typedef int -(*MHD_AccessHandlerCallback)(void * cls, - struct MHD_Connection * connection, - const char * url, - const char * method, - const char * version, - const char * upload_data, - unsigned int * upload_data_size); + (*MHD_AccessHandlerCallback) (void *cls, + struct MHD_Connection * connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, + unsigned int *upload_data_size); /** * Iterator over key-value pairs. This iterator @@ -391,10 +395,9 @@ typedef int * MHD_NO to abort the iteration */ typedef int -(*MHD_KeyValueIterator)(void * cls, - enum MHD_ValueKind kind, - const char * key, - const char * value); + (*MHD_KeyValueIterator) (void *cls, + enum MHD_ValueKind kind, + const char *key, const char *value); /** * Callback used by libmicrohttpd in order to obtain content. The @@ -423,10 +426,7 @@ typedef int * with the client). */ typedef int -(*MHD_ContentReaderCallback)(void * cls, - size_t pos, - char * buf, - int max); + (*MHD_ContentReaderCallback) (void *cls, size_t pos, char *buf, int max); /** * This method is called by libmicrohttpd if we @@ -434,8 +434,7 @@ typedef int * be used to free resources associated with the * content reader. */ -typedef void -(*MHD_ContentReaderFreeCallback)(void * cls); +typedef void (*MHD_ContentReaderFreeCallback) (void *cls); /** * Start a webserver on the given port. @@ -452,22 +451,19 @@ typedef void * terminated with MHD_OPTION_END). * @return NULL on error, handle to daemon on success */ -struct MHD_Daemon * -MHD_start_daemon(unsigned int flags, - unsigned short port, - MHD_AcceptPolicyCallback apc, - void * apc_cls, - MHD_AccessHandlerCallback dh, - void * dh_cls, - ...); +struct MHD_Daemon *MHD_start_daemon (unsigned int flags, + unsigned short port, + MHD_AcceptPolicyCallback apc, + void *apc_cls, + MHD_AccessHandlerCallback dh, + void *dh_cls, ...); /** * Shutdown an http daemon. */ -void -MHD_stop_daemon(struct MHD_Daemon * daemon); +void MHD_stop_daemon (struct MHD_Daemon *daemon); /** @@ -478,11 +474,9 @@ MHD_stop_daemon(struct MHD_Daemon * daemon); * options for this call. */ int -MHD_get_fdset(struct MHD_Daemon * daemon, - fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - int * max_fd); +MHD_get_fdset (struct MHD_Daemon *daemon, + fd_set * read_fd_set, + fd_set * write_fd_set, fd_set * except_fd_set, int *max_fd); /** * Run webserver operations (without blocking unless @@ -494,8 +488,7 @@ MHD_get_fdset(struct MHD_Daemon * daemon, * daemon was not started with the right * options for this call. */ -int -MHD_run(struct MHD_Daemon * daemon); +int MHD_run (struct MHD_Daemon *daemon); /** @@ -506,10 +499,9 @@ MHD_run(struct MHD_Daemon * daemon); * already exists */ int -MHD_register_handler(struct MHD_Daemon * daemon, - const char * uri_prefix, - MHD_AccessHandlerCallback dh, - void * dh_cls); +MHD_register_handler (struct MHD_Daemon *daemon, + const char *uri_prefix, + MHD_AccessHandlerCallback dh, void *dh_cls); /** * Unregister an access handler for the URIs beginning with @@ -520,10 +512,9 @@ MHD_register_handler(struct MHD_Daemon * daemon, * is not known for this daemon */ int -MHD_unregister_handler(struct MHD_Daemon * daemon, - const char * uri_prefix, - MHD_AccessHandlerCallback dh, - void * dh_cls); +MHD_unregister_handler (struct MHD_Daemon *daemon, + const char *uri_prefix, + MHD_AccessHandlerCallback dh, void *dh_cls); /** * Get all of the headers from the request. @@ -534,10 +525,9 @@ MHD_unregister_handler(struct MHD_Daemon * daemon, * @return number of entries iterated over */ int -MHD_get_connection_values(struct MHD_Connection * connection, - enum MHD_ValueKind kind, - MHD_KeyValueIterator iterator, - void * iterator_cls); +MHD_get_connection_values (struct MHD_Connection *connection, + enum MHD_ValueKind kind, + MHD_KeyValueIterator iterator, void *iterator_cls); /** * Get a particular header value. If multiple @@ -546,10 +536,9 @@ MHD_get_connection_values(struct MHD_Connection * connection, * @param key the header to look for * @return NULL if no such item was found */ -const char * -MHD_lookup_connection_value(struct MHD_Connection * connection, - enum MHD_ValueKind kind, - const char * key); +const char *MHD_lookup_connection_value (struct MHD_Connection *connection, + enum MHD_ValueKind kind, + const char *key); /** * Queue a response to be transmitted to the client (as soon as @@ -562,11 +551,10 @@ MHD_lookup_connection_value(struct MHD_Connection * connection, * MHD_YES on success or if message has been queued */ int -MHD_queue_response(struct MHD_Connection * connection, - unsigned int status_code, - struct MHD_Response * response); +MHD_queue_response (struct MHD_Connection *connection, + unsigned int status_code, struct MHD_Response *response); + - /** * Create a response object. The response object can be extended with * header information and then be used any number of times. @@ -577,11 +565,11 @@ MHD_queue_response(struct MHD_Connection * connection, * @param crfc callback to call to free crc_cls resources * @return NULL on error (i.e. invalid arguments, out of memory) */ -struct MHD_Response * -MHD_create_response_from_callback(size_t size, - MHD_ContentReaderCallback crc, - void * crc_cls, - MHD_ContentReaderFreeCallback crfc); +struct MHD_Response *MHD_create_response_from_callback (size_t size, + MHD_ContentReaderCallback + crc, void *crc_cls, + MHD_ContentReaderFreeCallback + crfc); /** * Create a response object. The response object can be extended with @@ -595,11 +583,10 @@ MHD_create_response_from_callback(size_t size, * this call returns * @return NULL on error (i.e. invalid arguments, out of memory) */ -struct MHD_Response * -MHD_create_response_from_data(size_t size, - void * data, - int must_free, - int must_copy); +struct MHD_Response *MHD_create_response_from_data (size_t size, + void *data, + int must_free, + int must_copy); /** * Destroy a response object and associated resources. Note that @@ -607,8 +594,7 @@ MHD_create_response_from_data(size_t size, * is still in the queue for some clients, so the memory may not * necessarily be freed immediatley. */ -void -MHD_destroy_response(struct MHD_Response * response); +void MHD_destroy_response (struct MHD_Response *response); /** * Add a header line to the response. @@ -616,9 +602,8 @@ MHD_destroy_response(struct MHD_Response * response); * @return MHD_NO on error (i.e. invalid header or content format). */ int -MHD_add_response_header(struct MHD_Response * response, - const char * header, - const char * content); +MHD_add_response_header (struct MHD_Response *response, + const char *header, const char *content); /** * Delete a header line from the response. @@ -626,9 +611,8 @@ MHD_add_response_header(struct MHD_Response * response, * @return MHD_NO on error (no such header known) */ int -MHD_del_response_header(struct MHD_Response * response, - const char * header, - const char * content); +MHD_del_response_header (struct MHD_Response *response, + const char *header, const char *content); /** * Get all of the headers added to a response. @@ -639,9 +623,8 @@ MHD_del_response_header(struct MHD_Response * response, * @return number of entries iterated over */ int -MHD_get_response_headers(struct MHD_Response * response, - MHD_KeyValueIterator iterator, - void * iterator_cls); +MHD_get_response_headers (struct MHD_Response *response, + MHD_KeyValueIterator iterator, void *iterator_cls); /** @@ -650,12 +633,11 @@ MHD_get_response_headers(struct MHD_Response * response, * @param key which header to get * @return NULL if header does not exist */ -const char * -MHD_get_response_header(struct MHD_Response * response, - const char * key); +const char *MHD_get_response_header (struct MHD_Response *response, + const char *key); -#if 0 /* keep Emacsens' auto-indent happy */ +#if 0 /* keep Emacsens' auto-indent happy */ { #endif #ifdef __cplusplus