diff --git a/CMakeLists.txt b/CMakeLists.txt index 832c318..6662833 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,9 +186,9 @@ if(OPENSSL_FOUND AND APPLE) endif() if(WIN32) - target_compile_definitions(evhtp PUBLIC WIN32) - find_library(LIB_WS32 ws2_32) - list(APPEND SYS_LIBS ${LIB_WS32}) + target_compile_definitions(evhtp PUBLIC WIN32 _CRT_SECURE_NO_WARNINGS) + target_link_libraries(evhtp PUBLIC ws2_32 iphlpapi) + list(APPEND SYS_LIBS ws2_32 iphlpapi) endif() configure_file( diff --git a/evhtp.c b/evhtp.c index 2a3665a..8bbdadc 100644 --- a/evhtp.c +++ b/evhtp.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #ifndef WIN32 @@ -36,6 +35,13 @@ #include "numtoa.h" #include "evhtp/evhtp.h" +#ifdef NO_STRNDUP +static char * strndup(const char * s, size_t n); +#endif +#ifdef NO_STRNDUP +static char * strndup(const char * s, size_t n); +#endif + /* `MIN' is not defined on Windows and mingw */ #ifndef MIN #define MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -333,7 +339,7 @@ evhtp_set_mem_functions(void *(*mallocfn_)(size_t len), realloc_ = reallocfn_; free_ = freefn_; - return event_set_mem_functions(malloc_, realloc_, free_); + event_set_mem_functions(malloc_, realloc_, free_); #endif } @@ -2023,6 +2029,7 @@ static int htp__evbuffer_add_iovec_(struct evbuffer * buf, struct evbuffer_iovec * vec, int n_vec) { int n; + int res; size_t to_alloc; char * bufptr; size_t to_copy; @@ -2033,7 +2040,15 @@ htp__evbuffer_add_iovec_(struct evbuffer * buf, struct evbuffer_iovec * vec, int to_alloc += vec[n].iov_len; } +#ifdef _MSC_VER + char *buffer; + buffer = htp__malloc_(to_alloc * sizeof(char)); + if (evhtp_unlikely(buffer == NULL)) { + return -1; + } +#else char buffer[to_alloc]; +#endif bufptr = buffer; to_copy = to_alloc; @@ -2042,7 +2057,8 @@ htp__evbuffer_add_iovec_(struct evbuffer * buf, struct evbuffer_iovec * vec, int { size_t copy = MIN(vec[n].iov_len, to_copy); - bufptr = mempcpy(bufptr, vec[n].iov_base, copy); + memcpy(bufptr, vec[n].iov_base, copy); + bufptr += copy; to_copy -= copy; if (evhtp_unlikely(to_copy == 0)) { @@ -2050,7 +2066,11 @@ htp__evbuffer_add_iovec_(struct evbuffer * buf, struct evbuffer_iovec * vec, int } } - return evbuffer_add(buf, buffer, to_alloc); + res = evbuffer_add(buf, buffer, to_alloc); +#ifdef _MSV_VER + htp__free_(buffer); +#endif + return res; } static int diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index c53a116..006d500 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,11 +1,13 @@ add_custom_target(examples) -add_executable(test_extensive EXCLUDE_FROM_ALL test.c) +if (NOT WIN32) + add_executable(test_extensive EXCLUDE_FROM_ALL test.c) + add_executable(test_perf EXCLUDE_FROM_ALL test_perf.c) +endif() add_executable(test_basic EXCLUDE_FROM_ALL test_basic.c) add_executable(test_vhost EXCLUDE_FROM_ALL test_vhost.c) add_executable(test_client EXCLUDE_FROM_ALL test_client.c) add_executable(test_query EXCLUDE_FROM_ALL test_query.c) -add_executable(test_perf EXCLUDE_FROM_ALL test_perf.c) add_executable(example_vhost EXCLUDE_FROM_ALL example_vhost.c) add_executable(example_pause EXCLUDE_FROM_ALL example_pause.c) add_executable(example_chunked EXCLUDE_FROM_ALL example_chunked.c) @@ -29,19 +31,21 @@ if(NOT EVHTP_DISABLE_EVTHR) add_dependencies(examples test_proxy) endif() -target_link_libraries(test_extensive evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) +if (NOT WIN32) + target_link_libraries(test_extensive evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) + target_link_libraries(test_perf evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) +endif() target_link_libraries(test_basic evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(test_vhost evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(test_client evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(test_query evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) -target_link_libraries(test_perf evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(example_vhost evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(example_pause evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(example_chunked evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(example_request_fini evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) target_link_libraries(example_basic evhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS}) -if(NOT EVHTP_DISABLE_SSL) +if(NOT EVHTP_DISABLE_SSL AND NOT WIN32) file(COPY https/etc/ca.cnf https/etc/client1.cnf @@ -62,8 +66,15 @@ endif() add_dependencies(examples example_request_fini - example_chunked example_pause - example_vhost test_extensive + example_chunked + example_pause + example_vhost test_basic - test_vhost test_client - test_query test_perf example_basic) + test_vhost + test_client + test_query + example_basic) + +if (NOT WIN32) + add_dependencies(examples test_extensive test_perf) +endif() diff --git a/examples/eutils.h b/examples/eutils.h index 25b1650..e2d5f52 100644 --- a/examples/eutils.h +++ b/examples/eutils.h @@ -6,6 +6,7 @@ #ifdef _WIN32 #include #endif +#include "evhtp.h" static void * mm__dup_(const void * src, size_t size) @@ -18,17 +19,19 @@ mm__dup_(const void * src, size_t size) #define mm__alloc_(type, ...) \ (type *)mm__dup_((type[]) {__VA_ARGS__ }, sizeof(type)) -#define bind__sock_port0_(HTP) ({ \ - struct sockaddr_in sin; \ - socklen_t len = sizeof(struct sockaddr); \ - uint16_t port = 0; \ - \ - evhtp_bind_socket(HTP, "127.0.0.1", 9999, 128); \ - \ - if (getsockname( \ - evconnlistener_get_fd(HTP->server), \ - (struct sockaddr *)&sin, &len) == 0) { \ - port = ntohs(sin.sin_port); \ - } \ - port; \ - }) +static inline uint16_t +bind__sock_port0_(struct evhtp *HTP) +{ + struct sockaddr_in sin; + socklen_t len = sizeof(struct sockaddr); + uint16_t port = 0; + + evhtp_bind_socket(HTP, "127.0.0.1", 9999, 128); + + if (getsockname( + evconnlistener_get_fd(HTP->server), + (struct sockaddr *)&sin, &len) == 0) { + port = ntohs(sin.sin_port); + } + return port; +} diff --git a/examples/example_basic.c b/examples/example_basic.c index 9948152..bd3f2c4 100644 --- a/examples/example_basic.c +++ b/examples/example_basic.c @@ -4,7 +4,6 @@ #include #include #include -#include #include "./eutils.h" #include "internal.h" diff --git a/include/evhtp/config.h.in b/include/evhtp/config.h.in index e5e434f..1766aff 100644 --- a/include/evhtp/config.h.in +++ b/include/evhtp/config.h.in @@ -8,6 +8,8 @@ extern "C" { #ifndef EVHTP_EXPORT # if (defined __GNUC__ && __GNUC__ >= 4) || defined __INTEL_COMPILER || defined __clang__ # define EVHTP_EXPORT __attribute__ ((visibility("default"))) +# elif defined(_MSC_VER) +# define EVHTP_EXPORT __declspec(dllexport) # else # define EVHTP_EXPORT # endif diff --git a/include/evhtp/evhtp.h b/include/evhtp/evhtp.h index 9bcee44..3a04af7 100644 --- a/include/evhtp/evhtp.h +++ b/include/evhtp/evhtp.h @@ -607,8 +607,8 @@ EVHTP_EXPORT int evhtp_ssl_init(evhtp_t * htp, evhtp_ssl_cfg_t * ssl_cfg); * * @param htp */ -EVHTP_EXPORT void evhtp_disable_100_continue(evhtp_t * htp) -DEPRECATED("evhtp_disable_100 will soon be deprecated, use htp->flags instead"); +DEPRECATED("evhtp_disable_100 will soon be deprecated, use htp->flags instead") +EVHTP_EXPORT void evhtp_disable_100_continue(evhtp_t * htp); /** * @brief creates a lock around callbacks and hooks, allowing for threaded @@ -858,8 +858,8 @@ EVHTP_EXPORT int evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr *, * * @return */ -EVHTP_EXPORT int evhtp_use_threads(evhtp_t *, evhtp_thread_init_cb, int nthreads, void *) -DEPRECATED("will take on the syntax of evhtp_use_threads_wexit"); +DEPRECATED("will take on the syntax of evhtp_use_threads_wexit") +EVHTP_EXPORT int evhtp_use_threads(evhtp_t *, evhtp_thread_init_cb, int nthreads, void *); /** * @brief Temporary function which will be renamed evhtp_use_threads in the diff --git a/include/evhtp/thread.h b/include/evhtp/thread.h index 7479aa8..e4b49d6 100644 --- a/include/evhtp/thread.h +++ b/include/evhtp/thread.h @@ -35,8 +35,8 @@ typedef void (* evthr_cb)(evthr_t * thr, void * cmd_arg, void * shared); typedef void (* evthr_init_cb)(evthr_t * thr, void * shared); typedef void (* evthr_exit_cb)(evthr_t * thr, void * shared); -EVHTP_EXPORT evthr_t * evthr_new(evthr_init_cb, void *) - DEPRECATED("will take on the syntax of evthr_wexit_new"); +DEPRECATED("will take on the syntax of evthr_wexit_new") +EVHTP_EXPORT evthr_t * evthr_new(evthr_init_cb, void *); EVHTP_EXPORT evbase_t * evthr_get_base(evthr_t * thr); EVHTP_EXPORT void evthr_set_aux(evthr_t * thr, void * aux); @@ -46,8 +46,8 @@ EVHTP_EXPORT evthr_res evthr_stop(evthr_t * evthr); EVHTP_EXPORT evthr_res evthr_defer(evthr_t * evthr, evthr_cb cb, void *); EVHTP_EXPORT void evthr_free(evthr_t * evthr); -EVHTP_EXPORT evthr_pool_t * evthr_pool_new(int nthreads, evthr_init_cb, void *) - DEPRECATED("will take on the syntax of evthr_pool_wexit_new"); +DEPRECATED("will take on the syntax of evthr_pool_wexit_new") +EVHTP_EXPORT evthr_pool_t * evthr_pool_new(int nthreads, evthr_init_cb, void *); EVHTP_EXPORT int evthr_pool_start(evthr_pool_t * pool); EVHTP_EXPORT evthr_res evthr_pool_stop(evthr_pool_t * pool); diff --git a/include/internal.h b/include/internal.h index 194823c..f2fd64d 100644 --- a/include/internal.h +++ b/include/internal.h @@ -33,6 +33,7 @@ extern "C" { #define clean_errno() \ (errno == 0 ? "None" : strerror(errno)) +#ifndef _MSC_VER #define __log_debug_color(X) "[\x1b[1;36m" X "\x1b[0;39m]" #define __log_error_color(X) "[\x1b[1;31m" X "\x1b[0;39m]" #define __log_warn_color(X) "[\x1b[1;33m" X "\x1b[0;39m]" @@ -40,7 +41,15 @@ extern "C" { #define __log_func_color(X) "\x1b[33m" X "\x1b[39m" #define __log_args_color(X) "\x1b[94m" X "\x1b[39m" #define __log_errno_color(X) "\x1b[35m" X "\x1b[39m" - +#else +#define __log_debug_color(X) X +#define __log_error_color(X) X +#define __log_warn_color(X) X +#define __log_info_color(X) X +#define __log_func_color(X) X +#define __log_args_color(X) X +#define __log_errno_color(X) X +#endif #if !defined(EVHTP_DEBUG) /* compile with all debug messages removed */ @@ -127,8 +136,14 @@ extern "C" { #define evhtp_errno_assert(x) #endif +#ifdef _MSC_VER +#define strcasecmp stricmp +#define strncasecmp strnicmp - +/* On MSVC, ssize_t is SSIZE_T */ +#include +typedef SSIZE_T ssize_t; +#endif #ifdef __cplusplus } diff --git a/log.c b/log.c index 77e26cd..1f24c50 100644 --- a/log.c +++ b/log.c @@ -12,6 +12,7 @@ #include #include #endif +#include "internal.h" #include "evhtp/evhtp.h" #include "evhtp/log.h" @@ -282,7 +283,7 @@ evhtp_log_request_f(void * format_p, evhtp_request_t * request, FILE * fp) continue; case HTP_LOG_OP_HOST: logstr = - request->htp->server_name ? : evhtp_header_find(request->headers_in, "host"); + request->htp->server_name ? request->htp->server_name : evhtp_header_find(request->headers_in, "host"); break; case HTP_LOG_OP_PROTO: @@ -297,7 +298,7 @@ evhtp_log_request_f(void * format_p, evhtp_request_t * request, FILE * fp) break; } /* switch */ - fputs(logstr ? : "-", fp); + fputs(logstr ? logstr : "-", fp); } fputc('\n', fp);