2727#include < unistd.h>
2828#include < sys/stat.h>
2929#include < sys/types.h>
30+ #include < sys/time.h>
31+
32+ #if defined(__MINGW32__) || defined(__CYGWIN32__)
33+ #include < winsock2.h>
34+ #define _WINDOWS
35+ #else
3036#include < netinet/ip.h>
37+ #endif
38+
3139#include < signal.h>
3240#include < fcntl.h>
3341#include < algorithm>
@@ -115,12 +123,16 @@ struct compare_value
115123 }
116124};
117125
126+ #ifndef __MINGW32__
118127static void catcher (int sig)
119128{
120129}
130+ #endif
121131
122132static void ignore_sigpipe ()
123133{
134+ // Mingw doesn't implement SIGPIPE
135+ #ifndef __MINGW32__
124136 struct sigaction oldsig;
125137 struct sigaction sig;
126138
@@ -136,6 +148,7 @@ static void ignore_sigpipe ()
136148 gettext (" Failed to install SIGPIPE handler: %s\n " ),
137149 strerror (errno)
138150 );
151+ #endif
139152}
140153
141154// WEBSERVER
@@ -259,7 +272,7 @@ void* webserver::select(void* self)
259272 fd_set es;
260273 struct timeval timeout_value;
261274 details::daemon_item* di = static_cast <details::daemon_item*>(self);
262- int max;
275+ MHD_socket max;
263276 while (di->ws ->is_running ())
264277 {
265278 max = 0 ;
@@ -298,7 +311,7 @@ void* webserver::select(void* self)
298311 int local_max;
299312 (*it).second .supply_events (&rs, &ws, &es, &local_max);
300313
301- if (local_max > max)
314+ if ((MHD_socket) local_max > max)
302315 max = local_max;
303316
304317 struct timeval t = (*it).second .get_timeout ();
@@ -321,7 +334,9 @@ void* webserver::select(void* self)
321334 timeout_value.tv_sec = timeout_secs;
322335 timeout_value.tv_usec = timeout_microsecs;
323336
324- ::select (max + 1 , &rs, &ws, &es, &timeout_value);
337+ /* On unix, MHD_socket will be an int anyway.
338+ On windows, the cast is safe because winsock ignores first argument to select*/
339+ ::select ((int ) max + 1, &rs, &ws, &es, &timeout_value);
325340 MHD_run (di->daemon );
326341
327342 // EVENT SUPPLIERS DISPATCHING
@@ -338,24 +353,25 @@ void* webserver::select(void* self)
338353 return 0x0 ;
339354}
340355
341- int create_socket (int domain, int type, int protocol)
356+ MHD_socket create_socket (int domain, int type, int protocol)
342357{
343358 int sock_cloexec = SOCK_CLOEXEC;
344359 int ctype = SOCK_STREAM | sock_cloexec;
345- int fd;
346-
347- /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
360+
361+ /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
348362 * implementations do not set ai_socktype, e.g. RHL6.2. */
349- fd = socket (domain, ctype, protocol);
363+ MHD_socket fd = socket (domain, ctype, protocol);
364+
365+ #ifdef _WINDOWS
366+ if (fd == INVALID_SOCKET)
367+ #else
350368 if ((fd == -1 ) &&
351369 (errno == EINVAL || errno == EPROTONOSUPPORT) && (sock_cloexec != 0 )
352370 )
371+ #endif
353372 {
354- sock_cloexec = 0 ;
355373 fd = socket (domain, type, protocol);
356374 }
357- if (-1 == fd)
358- return -1 ;
359375 return fd;
360376}
361377
@@ -488,24 +504,29 @@ bool webserver::start(bool blocking)
488504 setsockopt (bind_socket,
489505 SOL_SOCKET,
490506 SO_REUSEADDR,
491- &on, sizeof (on));
507+ ( const char *) &on, sizeof (on));
492508
493509 if (use_ipv6)
494510 {
495511#ifdef IPPROTO_IPV6
496512#ifdef IPV6_V6ONLY
497513 setsockopt (bind_socket,
498514 IPPROTO_IPV6, IPV6_V6ONLY,
499- &on, sizeof (on)
515+ ( const char *) &on, sizeof (on)
500516 );
501517#endif
502518#endif
503519 }
504520 bind (bind_socket, servaddr, addrlen);
505521 }
522+ #ifdef _WINDOWS
523+ unsigned long ioarg = 1 ;
524+ ioctlsocket (bind_socket, FIONBIO, &ioarg);
525+ #else
506526 int flags = fcntl (bind_socket, F_GETFL);
507527 flags |= O_NONBLOCK;
508528 fcntl (bind_socket, F_SETFL, flags);
529+ #endif
509530 if (!bind_settled)
510531 listen (bind_socket, 1 );
511532 iov.push_back (gen (MHD_OPTION_LISTEN_SOCKET, bind_socket));
@@ -730,7 +751,7 @@ int webserver::build_request_args (
730751 mr->dhr ->querystring += string (buf);
731752 }
732753 }
733- int size = internal_unescaper ((void *) mr->ws , value);
754+ size_t size = internal_unescaper ((void *) mr->ws , value);
734755 mr->dhr ->set_arg (key, string (value, size));
735756 return MHD_YES;
736757}
@@ -1046,18 +1067,17 @@ int webserver::finalize_answer(
10461067 details::http_resource_mirror
10471068 >::iterator it;
10481069
1049- int len = - 1 ;
1050- int tot_len = - 1 ;
1070+ size_t len = 0 ;
1071+ size_t tot_len = 0 ;
10511072 for (
10521073 it=registered_resources.begin ();
10531074 it!=registered_resources.end ();
10541075 ++it
10551076 )
10561077 {
1057- int endpoint_pieces_len = (*it).first .get_url_pieces_num ();
1058- int endpoint_tot_len = (*it).first .get_url_complete_size ();
1059- if (tot_len == -1 ||
1060- len == -1 ||
1078+ size_t endpoint_pieces_len = (*it).first .get_url_pieces_num ();
1079+ size_t endpoint_tot_len = (*it).first .get_url_complete_size ();
1080+ if (!found ||
10611081 endpoint_pieces_len > len ||
10621082 (
10631083 endpoint_pieces_len == len &&
@@ -1078,7 +1098,7 @@ int webserver::finalize_answer(
10781098 {
10791099 vector<string> url_pars;
10801100
1081- unsigned int pars_size =
1101+ size_t pars_size =
10821102 found_endpoint->first .get_url_pars (url_pars);
10831103
10841104 vector<string> url_pieces;
0 commit comments