libmicrohttpd

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

commit 7b8d280e45f434cab6508b2186cd3b286a9c7b7c
parent 6d7d5bf7847634a7906e379e08907ec7e2b5f920
Author: Christian Grothoff <christian@grothoff.org>
Date:   Wed,  7 Aug 2013 09:04:37 +0000

-reverting removal of Plibc, select on pipes problem is critial

Diffstat:
Msrc/examples/Makefile.am | 2+-
Msrc/include/platform.h | 27+--------------------------
Msrc/microhttpd/connection.c | 48++++++++++++++++++++++--------------------------
Msrc/microhttpd/daemon.c | 310+++++++++++++++++++++++++++++++------------------------------------------------
Msrc/microhttpd/memorypool.c | 18+++++++-----------
Msrc/microspdy/Makefile.am | 5+++++
Msrc/spdy2http/Makefile.am | 5+++++
Msrc/testcurl/test_options.c | 12++----------
Msrc/testspdy/Makefile.am | 5+++++
9 files changed, 169 insertions(+), 263 deletions(-)

diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am @@ -60,7 +60,7 @@ noinst_PROGRAMS += \ endif if HAVE_W32 -AM_CFLAGS += -DWINDOWS +AM_CFLAGS = -DWINDOWS endif minimal_example_SOURCES = \ diff --git a/src/include/platform.h b/src/include/platform.h @@ -109,32 +109,7 @@ #include <arpa/inet.h> #endif -#ifndef WINDOWS - -#define DIR_SEPARATOR '/' -#define DIR_SEPARATOR_STR "/" -#define PATH_SEPARATOR ':' -#define PATH_SEPARATOR_STR ":" -#define NEWLINE "\n" - -#else /* ifndef WINDOWS */ - -#include <ws2tcpip.h> -#include <windows.h> - -#define DIR_SEPARATOR '\\' -#define DIR_SEPARATOR_STR "\\" -#define PATH_SEPARATOR ';' -#define PATH_SEPARATOR_STR ";" -#define NEWLINE "\r\n" - -#define SHUT_WR SD_SEND -#define SHUT_RD SD_RECEIVE -#define SHUT_RDWR SD_BOTH - -#define SetErrnoFromWinError(e) _SetErrnoFromWinError(e, __FILE__, __LINE__) - -#endif /* ifndef WINDOWS */ +#include <plibc.h> #endif diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -262,7 +262,7 @@ MHD_connection_close (struct MHD_Connection *connection, daemon = connection->daemon; if (0 == (connection->daemon->options & MHD_USE_EPOLL_TURBO)) - shutdown (connection->socket_fd, + SHUTDOWN (connection->socket_fd, (MHD_YES == connection->read_closed) ? SHUT_WR : SHUT_RDWR); connection->state = MHD_CONNECTION_CLOSED; connection->event_loop_info = MHD_EVENT_LOOP_INFO_CLEANUP; @@ -566,7 +566,7 @@ add_extra_headers (struct MHD_Connection *connection) Note that the change from 'SHOULD NOT' to 'MUST NOT' is a recent development of the HTTP 1.1 specification. */ - sprintf (buf, + SPRINTF (buf, MHD_UNSIGNED_LONG_LONG_PRINTF, (MHD_UNSIGNED_LONG_LONG) connection->response->total_size); MHD_add_response_header (connection->response, @@ -598,19 +598,15 @@ get_date_string (char *date) time_t t; time (&t); -#ifndef WINDOWS gmtime_r (&t, &now); -#else - gmtime_s (&now, &t); -#endif - sprintf (date, + SPRINTF (date, "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n", days[now.tm_wday % 7], (unsigned int) now.tm_mday, mons[now.tm_mon % 12], (unsigned int) (1900 + now.tm_year), - (unsigned int) now.tm_hour, - (unsigned int) now.tm_min, + (unsigned int) now.tm_hour, + (unsigned int) now.tm_min, (unsigned int) now.tm_sec); } @@ -685,21 +681,21 @@ build_header_response (struct MHD_Connection *connection) add_extra_headers (connection); rc = connection->responseCode & (~MHD_ICY_FLAG); reason_phrase = MHD_get_reason_phrase_for (rc); - sprintf (code, + SPRINTF (code, "%s %u %s\r\n", (0 != (connection->responseCode & MHD_ICY_FLAG)) - ? "ICY" + ? "ICY" : ( (0 == strcasecmp (MHD_HTTP_VERSION_1_0, - connection->version)) - ? MHD_HTTP_VERSION_1_0 + connection->version)) + ? MHD_HTTP_VERSION_1_0 : MHD_HTTP_VERSION_1_1), - rc, + rc, reason_phrase); off = strlen (code); /* estimate size */ size = off + 2; /* extra \r\n at the end */ kind = MHD_HEADER_KIND; - if ( (0 == (connection->daemon->options & MHD_SUPPRESS_DATE_NO_CLOCK)) && + if ( (0 == (connection->daemon->options & MHD_SUPPRESS_DATE_NO_CLOCK)) && (NULL == MHD_get_response_header (connection->response, MHD_HTTP_HEADER_DATE)) ) get_date_string (date); @@ -749,9 +745,9 @@ build_header_response (struct MHD_Connection *connection) } for (pos = connection->response->first_header; NULL != pos; pos = pos->next) if (pos->kind == kind) - off += sprintf (&data[off], + off += SPRINTF (&data[off], "%s: %s\r\n", - pos->header, + pos->header, pos->value); if (connection->state == MHD_CONNECTION_FOOTERS_RECEIVED) { @@ -1511,9 +1507,9 @@ do_read (struct MHD_Connection *connection) "Failed to receive data: %s\n", gnutls_strerror (bytes_read)); else -#endif +#endif MHD_DLOG (connection->daemon, - "Failed to receive data: %s\n", strerror (errno)); + "Failed to receive data: %s\n", STRERROR (errno)); #endif CONNECTION_CLOSE_ERROR (connection, NULL); return MHD_YES; @@ -1524,7 +1520,7 @@ do_read (struct MHD_Connection *connection) connection->read_closed = MHD_YES; /* shutdown is not required here, as the other side already knows; so flagging this internally should suffice */ - /* shutdown (connection->socket_fd, SHUT_RD); */ + /* SHUTDOWN (connection->socket_fd, SHUT_RD); */ return MHD_YES; } connection->read_buffer_offset += bytes_read; @@ -1560,9 +1556,9 @@ do_write (struct MHD_Connection *connection) "Failed to send data: %s\n", gnutls_strerror (ret)); else -#endif +#endif MHD_DLOG (connection->daemon, - "Failed to send data: %s\n", strerror (errno)); + "Failed to send data: %s\n", STRERROR (errno)); #endif CONNECTION_CLOSE_ERROR (connection, NULL); return MHD_YES; @@ -1918,7 +1914,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) break; #if HAVE_MESSAGES MHD_DLOG (connection->daemon, - "Failed to send data: %s\n", strerror (errno)); + "Failed to send data: %s\n", STRERROR (errno)); #endif CONNECTION_CLOSE_ERROR (connection, NULL); return MHD_YES; @@ -1980,7 +1976,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) return MHD_YES; #if HAVE_MESSAGES MHD_DLOG (connection->daemon, - "Failed to send data: %s\n", strerror (errno)); + "Failed to send data: %s\n", STRERROR (errno)); #endif CONNECTION_CLOSE_ERROR (connection, NULL); return MHD_YES; @@ -2531,7 +2527,7 @@ MHD_connection_epoll_update_ (struct MHD_Connection *connection) if (0 != (daemon->options & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to epoll_ctl failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif connection->state = MHD_CONNECTION_CLOSED; cleanup_connection (connection); @@ -2690,7 +2686,7 @@ MHD_queue_response (struct MHD_Connection *connection, refuse to read body / footers or further requests! */ if (0 == (connection->daemon->options & MHD_USE_EPOLL_TURBO)) - (void) shutdown (connection->socket_fd, SHUT_RD); + (void) SHUTDOWN (connection->socket_fd, SHUT_RD); connection->read_closed = MHD_YES; connection->state = MHD_CONNECTION_FOOTERS_RECEIVED; } diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -294,14 +294,14 @@ MHD_ip_limit_add (struct MHD_Daemon *daemon, MHD_ip_count_lock (daemon); /* Search for the IP address */ - if (NULL == (nodep = tsearch (key, - &daemon->per_ip_connection_count, + if (NULL == (nodep = TSEARCH (key, + &daemon->per_ip_connection_count, &MHD_ip_addr_compare))) { #if HAVE_MESSAGES MHD_DLOG (daemon, "Failed to add IP connection count node\n"); -#endif +#endif MHD_ip_count_unlock (daemon); free (key); return MHD_NO; @@ -350,10 +350,10 @@ MHD_ip_limit_del (struct MHD_Daemon *daemon, MHD_ip_count_lock (daemon); /* Search for the IP address */ - if (NULL == (nodep = tfind (&search_key, - &daemon->per_ip_connection_count, + if (NULL == (nodep = TFIND (&search_key, + &daemon->per_ip_connection_count, &MHD_ip_addr_compare))) - { + { /* Something's wrong if we couldn't find an IP address * that was previously added */ MHD_PANIC ("Failed to find previously-added IP address\n"); @@ -367,8 +367,8 @@ MHD_ip_limit_del (struct MHD_Daemon *daemon, /* Remove the node entirely if count reduces to 0 */ if (0 == --found_key->count) { - tdelete (found_key, - &daemon->per_ip_connection_count, + TDELETE (found_key, + &daemon->per_ip_connection_count, &MHD_ip_addr_compare); free (found_key); } @@ -696,7 +696,7 @@ MHD_handle_connection (void *data) /* how did we get here!? */ goto exit; } - num_ready = select (max + 1, &rs, &ws, NULL, tvp); + num_ready = SELECT (max + 1, &rs, &ws, NULL, tvp); if (num_ready < 0) { if (EINTR == errno) @@ -705,7 +705,7 @@ MHD_handle_connection (void *data) MHD_DLOG (con->daemon, "Error during select (%d): `%s'\n", max, - strerror (errno)); + STRERROR (errno)); #endif break; } @@ -751,7 +751,7 @@ MHD_handle_connection (void *data) continue; #if HAVE_MESSAGES MHD_DLOG (con->daemon, "Error during poll: `%s'\n", - strerror (errno)); + STRERROR (errno)); #endif break; } @@ -814,7 +814,7 @@ recv_param_adapter (struct MHD_Connection *connection, errno = ENOTCONN; return -1; } - ret = recv (connection->socket_fd, other, i, MSG_NOSIGNAL); + ret = RECV (connection->socket_fd, other, i, MSG_NOSIGNAL); #if EPOLL_SUPPORT if (ret < (ssize_t) i) { @@ -853,7 +853,7 @@ send_param_adapter (struct MHD_Connection *connection, return -1; } if (0 != (connection->daemon->options & MHD_USE_SSL)) - return send (connection->socket_fd, other, i, MSG_NOSIGNAL); + return SEND (connection->socket_fd, other, i, MSG_NOSIGNAL); #if LINUX if ( (connection->write_buffer_append_offset == connection->write_buffer_send_offset) && @@ -889,7 +889,7 @@ send_param_adapter (struct MHD_Connection *connection, http://lists.gnu.org/archive/html/libmicrohttpd/2011-02/msg00015.html */ } #endif - ret = send (connection->socket_fd, other, i, MSG_NOSIGNAL); + ret = SEND (connection->socket_fd, other, i, MSG_NOSIGNAL); #if EPOLL_SUPPORT if (ret < (ssize_t) i) { @@ -1009,7 +1009,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, client_socket, addr, addrlen); /* all pools are at their connection limit, must refuse */ - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); return MHD_NO; } @@ -1024,7 +1024,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, client_socket, FD_SETSIZE); #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); return MHD_NO; } @@ -1044,7 +1044,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, MHD_DLOG (daemon, "Server reached connection limit (closing inbound connection)\n"); #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); return MHD_NO; } @@ -1059,7 +1059,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, MHD_DLOG (daemon, "Connection rejected, closing connection\n"); #endif #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); MHD_ip_limit_del (daemon, addr, addrlen); return MHD_NO; @@ -1080,9 +1080,9 @@ MHD_add_connection (struct MHD_Daemon *daemon, #if HAVE_MESSAGES MHD_DLOG (daemon, "Error allocating memory: %s\n", - strerror (errno)); + STRERROR (errno)); #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); MHD_ip_limit_del (daemon, addr, addrlen); return MHD_NO; @@ -1094,9 +1094,9 @@ MHD_add_connection (struct MHD_Daemon *daemon, #if HAVE_MESSAGES MHD_DLOG (daemon, "Error allocating memory: %s\n", - strerror (errno)); + STRERROR (errno)); #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); MHD_ip_limit_del (daemon, addr, addrlen); free (connection); @@ -1109,9 +1109,9 @@ MHD_add_connection (struct MHD_Daemon *daemon, #if HAVE_MESSAGES MHD_DLOG (daemon, "Error allocating memory: %s\n", - strerror (errno)); + STRERROR (errno)); #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); MHD_ip_limit_del (daemon, addr, addrlen); MHD_pool_destroy (connection->pool); @@ -1149,7 +1149,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, MHD_DLOG (daemon, "Failed to make socket %d non-blocking: %s\n", connection->socket_fd, - strerror (errno)); + STRERROR (errno)); #endif } #else @@ -1159,7 +1159,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, #if HAVE_MESSAGES MHD_DLOG (daemon, "Failed to make socket non-blocking: %s\n", - strerror (errno)); + STRERROR (errno)); #endif } #endif @@ -1190,7 +1190,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, "Failed to setup TLS credentials: unknown credential type %d\n", daemon->cred_type); #endif - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); MHD_ip_limit_del (daemon, addr, addrlen); free (connection->addr); @@ -1233,7 +1233,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, { #if HAVE_MESSAGES MHD_DLOG (daemon, "Failed to create a thread: %s\n", - strerror (res_thread_create)); + STRERROR (res_thread_create)); #endif goto cleanup; } @@ -1256,7 +1256,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, if (0 != (daemon->options & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to epoll_ctl failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif goto cleanup; } @@ -1276,7 +1276,7 @@ MHD_add_connection (struct MHD_Daemon *daemon, return MHD_YES; #if HTTPS_SUPPORT || EPOLL_SUPPORT cleanup: - if (0 != close (client_socket)) + if (0 != CLOSE (client_socket)) MHD_PANIC ("close failed\n"); MHD_ip_limit_del (daemon, addr, addrlen); if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && @@ -1321,9 +1321,6 @@ MHD_accept_connection (struct MHD_Daemon *daemon) #endif struct sockaddr *addr = (struct sockaddr *) &addrstorage; socklen_t addrlen; -#ifdef WINDOWS - LPVOID msgBuffer; -#endif int s; int fd; int nonblock; @@ -1344,37 +1341,20 @@ MHD_accept_connection (struct MHD_Daemon *daemon) #if HAVE_ACCEPT4 s = accept4 (fd, addr, &addrlen, SOCK_CLOEXEC | nonblock); #else - s = accept (fd, addr, &addrlen); + s = ACCEPT (fd, addr, &addrlen); #endif if ((-1 == s) || (addrlen <= 0)) { #if HAVE_MESSAGES /* This could be a common occurance with multiple worker threads */ if ((EAGAIN != errno) && (EWOULDBLOCK != errno)) - { -#ifndef WINDOWS MHD_DLOG (daemon, "Error accepting connection: %s\n", - strerror (errno)); -#else - FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - (DWORD) GetLastError (), - MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &msgBuffer, - 0, NULL); - MHD_DLOG (daemon, - "Error accepting connection: %s\n", - msgBuffer); - LocalFree (msgBuffer); + STRERROR (errno)); #endif -#endif - } if (-1 != s) { - if (0 != close (s)) + if (0 != CLOSE (s)) MHD_PANIC ("close failed\n"); /* just in case */ } @@ -1390,9 +1370,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon) if (0 != ioctlsocket (s, FIONBIO, &flags)) { #if HAVE_MESSAGES - MHD_DLOG (daemon, - "Failed to make socket non-blocking: %s\n", - strerror (errno)); + MHD_DLOG (daemon, + "Failed to make socket non-blocking: %s\n", + STRERROR (errno)); #endif } if (!GetHandleInformation ((HANDLE) s, &dwFlags) || @@ -1400,18 +1380,10 @@ MHD_accept_connection (struct MHD_Daemon *daemon) !SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0))) { #if HAVE_MESSAGES - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - (DWORD) GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &msgBuffer, - 0, NULL ); - MHD_DLOG (daemon, - "Error accepting connection: %s\n", - msgBuffer); - LocalFree(msgBuffer); + SetErrnoFromWinError (GetLastError ()); + MHD_DLOG (daemon, + "Failed to make socket non-inheritable: %s\n", + STRERROR (errno)); #endif } #else @@ -1429,8 +1401,8 @@ MHD_accept_connection (struct MHD_Daemon *daemon) { #if HAVE_MESSAGES MHD_DLOG (daemon, - "Failed to make socket non-inheritable: %s\n", - strerror (errno)); + "Failed to make socket non-inheritable: %s\n", + STRERROR (errno)); #endif } #endif @@ -1513,7 +1485,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) pos->response = NULL; } if ( (-1 != pos->socket_fd) && - (0 != close (pos->socket_fd)) ) + (0 != CLOSE (pos->socket_fd)) ) MHD_PANIC ("close failed\n"); if (NULL != pos->addr) free (pos->addr); @@ -1718,14 +1690,7 @@ MHD_select (struct MHD_Daemon *daemon, struct timeval timeout; struct timeval *tv; MHD_UNSIGNED_LONG_LONG ltimeout; -#ifdef WINDOWS - LPVOID msgBuffer; - DWORD dwBytes; -#endif -#ifdef WINDOWS -loop_select: -#endif timeout.tv_sec = 0; timeout.tv_usec = 0; if (MHD_YES == daemon->shutdown) @@ -1742,20 +1707,19 @@ loop_select: /* If we're at the connection limit, no need to accept new connections. */ - if ( (0 == daemon->max_connections) && + if ( (0 == daemon->max_connections) && (-1 != daemon->socket_fd) ) FD_CLR (daemon->socket_fd, &rs); } else { /* accept only, have one thread per connection */ - if (-1 != daemon->socket_fd) + if (-1 != daemon->socket_fd) { max = daemon->socket_fd; FD_SET (daemon->socket_fd, &rs); } } -#ifndef WINDOWS if (-1 != daemon->wpipe[0]) { FD_SET (daemon->wpipe[0], &rs); @@ -1763,18 +1727,12 @@ loop_select: if (max < daemon->wpipe[0]) max = daemon->wpipe[0]; } -#endif tv = NULL; if (MHD_NO == may_block) { -#ifndef WINDOWS timeout.tv_usec = 0; -#else - timeout.tv_usec = 100000; -#endif timeout.tv_sec = 0; - tv = &timeout; } else if ( (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && @@ -1787,15 +1745,7 @@ loop_select: } if (-1 == max) return MHD_YES; -#ifdef WINDOWS - if (tv == NULL) - { - timeout.tv_usec = 100000; - timeout.tv_sec = 0; - tv = &timeout; - } -#endif - num_ready = select (max + 1, &rs, &ws, &es, tv); + num_ready = SELECT (max + 1, &rs, &ws, &es, tv); if (MHD_YES == daemon->shutdown) return MHD_NO; if (num_ready < 0) @@ -1803,41 +1753,10 @@ loop_select: if (EINTR == errno) return MHD_YES; #if HAVE_MESSAGES -#ifndef WINDOWS - MHD_DLOG (daemon, "select failed: %s\n", strerror (errno)); -#else - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - (DWORD) GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &msgBuffer, - 0, NULL); - MHD_DLOG (daemon, "select failed: %s\n", msgBuffer); - LocalFree(msgBuffer); -#endif + MHD_DLOG (daemon, "select failed: %s\n", STRERROR (errno)); #endif return MHD_NO; } -#ifdef WINDOWS - if (num_ready == 0) { - if (-1 != daemon->wpipe[0]) - { - if (! PeekNamedPipe((HANDLE) daemon->wpipe[0], NULL, 0, NULL, &dwBytes, NULL)) - { - goto loop_select; - } - else if (dwBytes) - FD_SET (daemon->wpipe[0], &rs); - else - goto loop_select; - } - else - goto loop_select; - } -#endif - return MHD_run_from_select (daemon, &rs, &ws, &es); } @@ -1931,7 +1850,7 @@ MHD_poll_all (struct MHD_Daemon *daemon, #if HAVE_MESSAGES MHD_DLOG (daemon, "poll failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif return MHD_NO; } @@ -2031,7 +1950,7 @@ MHD_poll_listen_socket (struct MHD_Daemon *daemon, if (EINTR == errno) return MHD_YES; #if HAVE_MESSAGES - MHD_DLOG (daemon, "poll failed: %s\n", strerror (errno)); + MHD_DLOG (daemon, "poll failed: %s\n", STRERROR (errno)); #endif return MHD_NO; } @@ -2124,7 +2043,7 @@ MHD_epoll (struct MHD_Daemon *daemon, if (0 != (daemon->options & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to epoll_ctl failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif return MHD_NO; } @@ -2176,7 +2095,7 @@ MHD_epoll (struct MHD_Daemon *daemon, if (0 != (daemon->options & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to epoll_wait failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif return MHD_NO; } @@ -2762,19 +2681,19 @@ create_socket (int domain, int type, int protocol) #ifdef WINDOWS DWORD dwFlags; #endif - + /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo * implementations do not set ai_socktype, e.g. RHL6.2. */ - fd = socket (domain, ctype, protocol); + fd = SOCKET (domain, ctype, protocol); if ( (-1 == fd) && (EINVAL == errno) && (0 != sock_cloexec) ) { sock_cloexec = 0; - fd = socket(domain, type, protocol); + fd = SOCKET(domain, type, protocol); } if (-1 == fd) return -1; if (0 != sock_cloexec) - return fd; /* this is it */ + return fd; /* this is it */ /* flag was not set during 'socket' call, let's try setting it manually */ #ifndef WINDOWS flags = fcntl (fd, F_GETFD); @@ -2783,6 +2702,9 @@ create_socket (int domain, int type, int protocol) if (!GetHandleInformation ((HANDLE) fd, &dwFlags)) #endif { +#ifdef WINDOWS + SetErrnoFromWinError (GetLastError ()); +#endif return fd; /* good luck */ } #ifndef WINDOWS @@ -2796,6 +2718,9 @@ create_socket (int domain, int type, int protocol) if (!SetHandleInformation ((HANDLE) fd, HANDLE_FLAG_INHERIT, 0)) #endif { +#ifdef WINDOWS + SetErrnoFromWinError (GetLastError ()); +#endif return fd; /* good luck */ } return fd; @@ -2822,7 +2747,7 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon) if (0 != (daemon->options & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to epoll_create1 failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif return MHD_NO; } @@ -2839,7 +2764,7 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon) if (0 != (daemon->options & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to epoll_ctl failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif return MHD_NO; } @@ -2887,15 +2812,15 @@ MHD_start_daemon_va (unsigned int flags, #ifndef HAVE_INET6 if (0 != (flags & MHD_USE_IPv6)) - return NULL; + return NULL; #endif #ifndef HAVE_POLL_H if (0 != (flags & MHD_USE_POLL)) - return NULL; + return NULL; #endif #if ! HTTPS_SUPPORT if (0 != (flags & MHD_USE_SSL)) - return NULL; + return NULL; #endif if (NULL == dh) return NULL; @@ -2940,17 +2865,12 @@ MHD_start_daemon_va (unsigned int flags, if (0 == (flags & (MHD_USE_SELECT_INTERNALLY | MHD_USE_THREAD_PER_CONNECTION))) use_pipe = 0; /* useless if we are using 'external' select */ if ( (use_pipe) && -#ifndef WINDOWS - (0 != pipe (daemon->wpipe)) ) -#else - (0 == CreatePipe ((HANDLE *) &daemon->wpipe[0], - (HANDLE *) &daemon->wpipe[1], NULL, 0)) ) -#endif + (0 != PIPE (daemon->wpipe)) ) { #if HAVE_MESSAGES - MHD_DLOG (daemon, + MHD_DLOG (daemon, "Failed to create control pipe: %s\n", - strerror (errno)); + STRERROR (errno)); #endif free (daemon); return NULL; @@ -2963,9 +2883,9 @@ MHD_start_daemon_va (unsigned int flags, MHD_DLOG (daemon, "file descriptor for control pipe exceeds maximum value\n"); #endif - if (0 != close (daemon->wpipe[0])) + if (0 != CLOSE (daemon->wpipe[0])) MHD_PANIC ("close failed\n"); - if (0 != close (daemon->wpipe[1])) + if (0 != CLOSE (daemon->wpipe[1])) MHD_PANIC ("close failed\n"); free (daemon); return NULL; @@ -3017,7 +2937,7 @@ MHD_start_daemon_va (unsigned int flags, #if HAVE_MESSAGES MHD_DLOG (daemon, "Failed to allocate memory for nonce-nc map: %s\n", - strerror (errno)); + STRERROR (errno)); #endif #if HTTPS_SUPPORT if (0 != (flags & MHD_USE_SSL)) @@ -3105,20 +3025,20 @@ MHD_start_daemon_va (unsigned int flags, if (0 != (flags & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Call to socket failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif goto free_and_fail; } - if ( (setsockopt (socket_fd, - SOL_SOCKET, - SO_REUSEADDR, - &on, sizeof (on))) && + if ( (0 > SETSOCKOPT (socket_fd, + SOL_SOCKET, + SO_REUSEADDR, + &on, sizeof (on))) && (0 != (flags & MHD_USE_DEBUG)) ) { #if HAVE_MESSAGES MHD_DLOG (daemon, "setsockopt failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif } @@ -3170,30 +3090,30 @@ MHD_start_daemon_va (unsigned int flags, #else const char on = 1; #endif - if ( (setsockopt (socket_fd, - IPPROTO_IPV6, IPV6_V6ONLY, - &on, sizeof (on))) && + if ( (0 > SETSOCKOPT (socket_fd, + IPPROTO_IPV6, IPV6_V6ONLY, + &on, sizeof (on))) && (0 != (flags & MHD_USE_DEBUG)) ) { #if HAVE_MESSAGES MHD_DLOG (daemon, "setsockopt failed: %s\n", - strerror (errno)); + STRERROR (errno)); #endif } #endif #endif } - if (-1 == bind (socket_fd, servaddr, addrlen)) + if (-1 == BIND (socket_fd, servaddr, addrlen)) { #if HAVE_MESSAGES if (0 != (flags & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Failed to bind to port %u: %s\n", (unsigned int) port, - strerror (errno)); + STRERROR (errno)); #endif - if (0 != close (socket_fd)) + if (0 != CLOSE (socket_fd)) MHD_PANIC ("close failed\n"); goto free_and_fail; } @@ -3206,23 +3126,23 @@ MHD_start_daemon_va (unsigned int flags, #if HAVE_MESSAGES MHD_DLOG (daemon, "Failed to make listen socket non-blocking: %s\n", - strerror (errno)); + STRERROR (errno)); #endif - if (0 != close (socket_fd)) + if (0 != CLOSE (socket_fd)) MHD_PANIC ("close failed\n"); goto free_and_fail; } } #endif - if (listen (socket_fd, 32) < 0) + if (LISTEN (socket_fd, 32) < 0) { #if HAVE_MESSAGES if (0 != (flags & MHD_USE_DEBUG)) MHD_DLOG (daemon, "Failed to listen for connections: %s\n", - strerror (errno)); + STRERROR (errno)); #endif - if (0 != close (socket_fd)) + if (0 != CLOSE (socket_fd)) MHD_PANIC ("close failed\n"); goto free_and_fail; } @@ -3242,7 +3162,7 @@ MHD_start_daemon_va (unsigned int flags, socket_fd, FD_SETSIZE); #endif - if (0 != close (socket_fd)) + if (0 != CLOSE (socket_fd)) MHD_PANIC ("close failed\n"); goto free_and_fail; } @@ -3255,7 +3175,7 @@ MHD_start_daemon_va (unsigned int flags, "MHD failed to initialize IP connection limit mutex\n"); #endif if ( (-1 != socket_fd) && - (0 != close (socket_fd)) ) + (0 != CLOSE (socket_fd)) ) MHD_PANIC ("close failed\n"); goto free_and_fail; } @@ -3267,7 +3187,7 @@ MHD_start_daemon_va (unsigned int flags, #endif pthread_mutex_destroy (&daemon->cleanup_connection_mutex); if ( (-1 != socket_fd) && - (0 != close (socket_fd)) ) + (0 != CLOSE (socket_fd)) ) MHD_PANIC ("close failed\n"); goto free_and_fail; } @@ -3281,7 +3201,7 @@ MHD_start_daemon_va (unsigned int flags, "Failed to initialize TLS support\n"); #endif if ( (-1 != socket_fd) && - (0 != close (socket_fd)) ) + (0 != CLOSE (socket_fd)) ) MHD_PANIC ("close failed\n"); pthread_mutex_destroy (&daemon->cleanup_connection_mutex); pthread_mutex_destroy (&daemon->per_ip_connection_mutex); @@ -3298,12 +3218,12 @@ MHD_start_daemon_va (unsigned int flags, #if HAVE_MESSAGES MHD_DLOG (daemon, "Failed to create listen thread: %s\n", - strerror (res_thread_create)); + STRERROR (res_thread_create)); #endif pthread_mutex_destroy (&daemon->cleanup_connection_mutex); pthread_mutex_destroy (&daemon->per_ip_connection_mutex); if ( (-1 != socket_fd) && - (0 != close (socket_fd)) ) + (0 != CLOSE (socket_fd)) ) MHD_PANIC ("close failed\n"); goto free_and_fail; } @@ -3337,8 +3257,14 @@ MHD_start_daemon_va (unsigned int flags, goto thread_failed; #else sk_flags = 1; +#if HAVE_PLIBC_FD + if (SOCKET_ERROR == + ioctlsocket (plibc_fd_get_handle (socket_fd), FIONBIO, &sk_flags)) + goto thread_failed; +#else if (ioctlsocket (socket_fd, FIONBIO, &sk_flags) == SOCKET_ERROR) goto thread_failed; +#endif // PLIBC_FD #endif // MINGW /* Allocate memory for pooled objects */ @@ -3388,8 +3314,8 @@ MHD_start_daemon_va (unsigned int flags, { #if HAVE_MESSAGES MHD_DLOG (daemon, - "Failed to create pool thread: %s\n", - strerror (res_thread_create)); + "Failed to create pool thread: %s\n", + STRERROR (res_thread_create)); #endif /* Free memory for this worker; cleanup below handles * all previously-created workers. */ @@ -3408,7 +3334,7 @@ thread_failed: if (0 == i) { if ( (-1 != socket_fd) && - (0 != close (socket_fd)) ) + (0 != CLOSE (socket_fd)) ) MHD_PANIC ("close failed\n"); pthread_mutex_destroy (&daemon->cleanup_connection_mutex); pthread_mutex_destroy (&daemon->per_ip_connection_mutex); @@ -3496,7 +3422,7 @@ close_all_connections (struct MHD_Daemon *daemon) (0 != pthread_mutex_lock (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to acquire cleanup mutex\n"); for (pos = daemon->connections_head; NULL != pos; pos = pos->nextX) - shutdown (pos->socket_fd, + SHUTDOWN (pos->socket_fd, (pos->read_closed == MHD_YES) ? SHUT_WR : SHUT_RDWR); if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && (0 != pthread_mutex_unlock (&daemon->cleanup_connection_mutex)) ) @@ -3583,7 +3509,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) } if (-1 != daemon->wpipe[1]) { - if (1 != write (daemon->wpipe[1], "e", 1)) + if (1 != WRITE (daemon->wpipe[1], "e", 1)) MHD_PANIC ("failed to signal shutdownn via pipe"); } #ifdef HAVE_LISTEN_SHUTDOWN @@ -3591,7 +3517,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) { /* fd might be -1 here due to 'MHD_quiesce_daemon' */ if (-1 != fd) - (void) shutdown (fd, SHUT_RDWR); + (void) SHUTDOWN (fd, SHUT_RDWR); } #endif #if EPOLL_SUPPORT @@ -3620,7 +3546,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) pthread_mutex_destroy (&daemon->worker_pool[i].cleanup_connection_mutex); #if EPOLL_SUPPORT if ( (-1 != daemon->worker_pool[i].epoll_fd) && - (0 != close (daemon->worker_pool[i].epoll_fd)) ) + (0 != CLOSE (daemon->worker_pool[i].epoll_fd)) ) MHD_PANIC ("close failed\n"); #endif } @@ -3641,7 +3567,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) } close_all_connections (daemon); if ( (-1 != fd) && - (0 != close (fd)) ) + (0 != CLOSE (fd)) ) MHD_PANIC ("close failed\n"); /* TLS clean up */ @@ -3656,7 +3582,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) #if EPOLL_SUPPORT if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) && (-1 != daemon->epoll_fd) && - (0 != close (daemon->epoll_fd)) ) + (0 != CLOSE (daemon->epoll_fd)) ) MHD_PANIC ("close failed\n"); #endif @@ -3669,9 +3595,9 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) if (-1 != daemon->wpipe[1]) { - if (0 != close (daemon->wpipe[0])) + if (0 != CLOSE (daemon->wpipe[0])) MHD_PANIC ("close failed\n"); - if (0 != close (daemon->wpipe[1])) + if (0 != CLOSE (daemon->wpipe[1])) MHD_PANIC ("close failed\n"); } free (daemon); @@ -3761,12 +3687,15 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL; /** * Initialize do setup work. */ -void ATTRIBUTE_CONSTRUCTOR +void ATTRIBUTE_CONSTRUCTOR MHD_init () { mhd_panic = &mhd_panic_std; mhd_panic_cls = NULL; +#ifdef WINDOWS + plibc_init ("GNU", "libmicrohttpd"); +#endif #if HTTPS_SUPPORT gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); gnutls_global_init (); @@ -3774,12 +3703,15 @@ MHD_init () } -void ATTRIBUTE_DESTRUCTOR +void ATTRIBUTE_DESTRUCTOR MHD_fini () { #if HTTPS_SUPPORT gnutls_global_deinit (); #endif +#ifdef WINDOWS + plibc_shutdown (); +#endif } /* end of daemon.c */ diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c @@ -95,7 +95,7 @@ MHD_pool_create (size_t max) if (max <= 32 * 1024) pool->memory = MAP_FAILED; else - pool->memory = mmap (NULL, max, PROT_READ | PROT_WRITE, + pool->memory = MMAP (NULL, max, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #else pool->memory = MAP_FAILED; @@ -134,11 +134,7 @@ MHD_pool_destroy (struct MemoryPool *pool) if (pool->is_mmap == MHD_NO) free (pool->memory); else -#ifndef WINDOWS - munmap (pool->memory, pool->size); -#else - VirtualFree (pool->memory, 0, MEM_RELEASE); -#endif + MUNMAP (pool->memory, pool->size); free (pool); } @@ -155,7 +151,7 @@ MHD_pool_destroy (struct MemoryPool *pool) * bytes */ void * -MHD_pool_allocate (struct MemoryPool *pool, +MHD_pool_allocate (struct MemoryPool *pool, size_t size, int from_end) { void *ret; @@ -196,8 +192,8 @@ MHD_pool_allocate (struct MemoryPool *pool, */ void * MHD_pool_reallocate (struct MemoryPool *pool, - void *old, - size_t old_size, + void *old, + size_t old_size, size_t new_size) { void *ret; @@ -246,8 +242,8 @@ MHD_pool_reallocate (struct MemoryPool *pool, * @return addr new address of "keep" (if it had to change) */ void * -MHD_pool_reset (struct MemoryPool *pool, - void *keep, +MHD_pool_reset (struct MemoryPool *pool, + void *keep, size_t size) { size = ROUND_TO_ALIGN (size); diff --git a/src/microspdy/Makefile.am b/src/microspdy/Makefile.am @@ -1,4 +1,9 @@ +if USE_PRIVATE_PLIBC_H + PLIBC_INCLUDE = -I$(top_srcdir)/src/include/plibc +endif + AM_CPPFLAGS = \ + $(PLIBC_INCLUDE) \ -I$(top_srcdir)/src/include \ -I$(top_srcdir)/src/microspdy diff --git a/src/spdy2http/Makefile.am b/src/spdy2http/Makefile.am @@ -6,7 +6,12 @@ if USE_COVERAGE AM_CFLAGS += -fprofile-arcs -ftest-coverage endif +if USE_PRIVATE_PLIBC_H + PLIBC_INCLUDE = -I$(top_srcdir)/src/include/plibc +endif + AM_CPPFLAGS = \ + $(PLIBC_INCLUDE) \ -I$(top_srcdir) \ -I$(top_srcdir)/src/include \ -I$(top_srcdir)/src/applicationlayer \ diff --git a/src/testcurl/test_options.c b/src/testcurl/test_options.c @@ -19,22 +19,14 @@ */ /** - * @file test_options.c + * @file mhds_get_test.c * @brief Testcase for libmicrohttpd HTTPS GET operations * @author Sagie Amir */ + #include "platform.h" #include "microhttpd.h" -/* inet_pton requires Vista, add defines to declare that we - are compiling for Vista */ -#if (defined(__MINGW64__) || defined(__MINGW32__)) -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x0600 -#endif -#endif - - #define MHD_E_MEM "Error: memory error\n" #define MHD_E_SERVER_INIT "Error: failed to start server\n" diff --git a/src/testspdy/Makefile.am b/src/testspdy/Makefile.am @@ -6,7 +6,12 @@ if USE_COVERAGE AM_CFLAGS += -fprofile-arcs -ftest-coverage endif +if USE_PRIVATE_PLIBC_H + PLIBC_INCLUDE = -I$(top_srcdir)/src/include/plibc +endif + AM_CPPFLAGS = \ + $(PLIBC_INCLUDE) \ -I$(top_srcdir) \ -I$(top_srcdir)/src/include \ -I$(top_srcdir)/src/applicationlayer \