diff --git a/CMakeLists.txt b/CMakeLists.txt index 89a3368..2b0e77a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -187,13 +187,23 @@ if (EVHTP_DISABLE_SSL) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DEVHTP_DISABLE_SSL") endif() -find_library (LIB_DL dl) -set (SYS_LIBS ${LIB_DL}) -if (NOT APPLE) - find_library (LIB_RT rt) - set (SYS_LIBS ${SYS_LIBS} ${LIB_RT}) -endif() + +IF (WIN32) + ADD_DEFINITIONS(-DWIN32) + ADD_DEFINITIONS(-march=i486) + find_library (LIB_WS32 ws2_32) + set (SYS_LIBS ${SYS_LIBS} ${LIB_WS32}) +ELSE () + find_library (LIB_DL dl) + set (SYS_LIBS ${LIB_DL}) + + if (NOT APPLE) + find_library (LIB_RT rt) + set (SYS_LIBS ${SYS_LIBS} ${LIB_RT}) + endif() + +ENDIF (WIN32) if (EVHTP_BUILD_SHARED) add_library(libevhtpShared SHARED ${LIBEVHTP_SOURCES} ${ONIG_SOURCES}) @@ -241,3 +251,8 @@ endif() install (FILES evhtp.h DESTINATION include) install (FILES htparse/htparse.h DESTINATION include) install (FILES evthr/evthr.h DESTINATION include) + +IF (WIN32) + install (FILES compat/sys/queue.h DESTINATION include/sys) + install (FILES oniguruma/onigposix.h DESTINATION include) +ENDIF (WIN32) diff --git a/README.markdown b/README.markdown index 87a2e14..39017d0 100644 --- a/README.markdown +++ b/README.markdown @@ -91,3 +91,10 @@ This example uses redis, mainly because most people who have asked me "is evhtp thread-safe" were attempting to *other things* before sending a response to a request. And on more than one occasion, those *other things* were communicating with redis. + + +## For Windows MinGW + + cmake -G "MinGW Makefiles" -DCMAKE_INCLUDE_PATH=/mingw/include -DCMAKE_LIBRARY_PATH=/mingw/lib -DCMAKE_INSTALL_PREFIX=/mingw . + + mingw32-make.exe diff --git a/evhtp.c b/evhtp.c index da308df..2cde1da 100644 --- a/evhtp.c +++ b/evhtp.c @@ -6,13 +6,20 @@ #include #include #include -#include +#ifndef WIN32 + #include + #include + #include + #include +#else + #define WINVER 0x0501 + #include + #include +#endif #ifndef NO_SYS_UN #include #endif -#include -#include -#include + #include #include "evhtp.h" @@ -33,7 +40,7 @@ static int _evhtp_request_parser_headers_start(htparser * p); static void _evhtp_connection_readcb(evbev_t * bev, void * arg); -static evhtp_connection_t * _evhtp_connection_new(evhtp_t * htp, int sock, evhtp_type type); +static evhtp_connection_t * _evhtp_connection_new(evhtp_t * htp, evutil_socket_t sock, evhtp_type type); static evhtp_uri_t * _evhtp_uri_new(void); static void _evhtp_uri_free(evhtp_uri_t * uri); @@ -1339,8 +1346,11 @@ _evhtp_create_reply(evhtp_request_t * request, evhtp_res code) { if (!evhtp_header_find(request->headers_out, "Content-Length")) { char lstr[128]; int sres; - +#ifndef WIN32 sres = snprintf(lstr, sizeof(lstr), "%zu", +#else + sres = snprintf(lstr, sizeof(lstr), "%u", +#endif evbuffer_get_length(request->buffer_out)); if (sres >= sizeof(lstr) || sres < 0) { @@ -1674,7 +1684,7 @@ _evhtp_default_request_cb(evhtp_request_t * request, void * arg) { } static evhtp_connection_t * -_evhtp_connection_new(evhtp_t * htp, int sock, evhtp_type type) { +_evhtp_connection_new(evhtp_t * htp, evutil_socket_t sock, evhtp_type type) { evhtp_connection_t * connection; htp_type ptype; @@ -1800,7 +1810,11 @@ _evhtp_accept_cb(evserv_t * serv, int fd, struct sockaddr * s, int sl, void * ar #ifndef EVHTP_DISABLE_EVTHR static unsigned long _evhtp_ssl_get_thread_id(void) { +#ifndef WIN32 return (unsigned long)pthread_self(); +#else + return (unsigned long)(pthread_self().p); +#endif } static void @@ -2681,7 +2695,9 @@ evhtp_unbind_socket(evhtp_t * htp) { int evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr * sa, size_t sin_len, int backlog) { +#ifndef WIN32 signal(SIGPIPE, SIG_IGN); +#endif htp->server = evconnlistener_new_bind(htp->evbase, _evhtp_accept_cb, (void *)htp, LEV_OPT_THREADSAFE | LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, diff --git a/evhtp.h b/evhtp.h index b2817aa..a44b573 100644 --- a/evhtp.h +++ b/evhtp.h @@ -426,7 +426,7 @@ struct evhtp_connection_s { struct sockaddr * saddr; struct timeval recv_timeo; /**< conn read timeouts (overrides global) */ struct timeval send_timeo; /**< conn write timeouts (overrides global) */ - int sock; + evutil_socket_t sock; uint8_t error; uint8_t owner; /**< set to 1 if this structure owns the bufferevent */ uint8_t vhost_via_sni; /**< set to 1 if the vhost was found via SSL SNI */ diff --git a/evthr/evthr.c b/evthr/evthr.c index 9294910..f94cd92 100644 --- a/evthr/evthr.c +++ b/evthr/evthr.c @@ -7,9 +7,12 @@ #include #include #include +#ifndef WIN32 #include #include #include +#endif + #include #include @@ -89,7 +92,7 @@ evthr_set_max_backlog(evthr_t * evthr, int max) { } static void -_evthr_read_cmd(int sock, short __unused__ which, void * args) { +_evthr_read_cmd(evutil_socket_t sock, short __unused__ which, void * args) { evthr_t * thread; evthr_cmd_t cmd; ssize_t recvd; @@ -264,7 +267,7 @@ evthr_new(evthr_init_cb init_cb, void * args) { evthr_t * thread; int fds[2]; - if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) { + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) { return NULL; } diff --git a/test.c b/test.c index 4c2a661..25ec7b7 100644 --- a/test.c +++ b/test.c @@ -30,7 +30,7 @@ struct pauser { /* pause testing */ static void -resume_request_timer(int sock, short which, void * arg) { +resume_request_timer(evutil_socket_t sock, short which, void * arg) { struct pauser * pause = (struct pauser *)arg; printf("resume_request_timer(%p) timer_ev = %p\n", pause->request->conn, pause->timer_ev); diff --git a/test_proxy.c b/test_proxy.c index 234a55d..2b39161 100644 --- a/test_proxy.c +++ b/test_proxy.c @@ -112,10 +112,10 @@ main(int argc, char ** argv) { #endif evhtp_use_threads(evhtp, init_thread_cb, 8, NULL); - +#ifndef WIN32 ev_sigterm = evsignal_new(evbase, SIGTERM, sigterm_cb, evbase); evsignal_add(ev_sigterm, NULL); - +#endif evhtp_bind_socket(evhtp, "0.0.0.0", 8081, 1024); event_base_loop(evbase, 0);