Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
compatible with Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Apr 11, 2020
1 parent edec9cb commit 948fd02
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 43 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 24 additions & 4 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <stdint.h>
#include <errno.h>
#include <signal.h>
#include <strings.h>
#include <inttypes.h>
#include <stdbool.h>
#ifndef WIN32
Expand All @@ -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))
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -2042,15 +2057,20 @@ 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)) {
break;
}
}

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
Expand Down
29 changes: 20 additions & 9 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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()
31 changes: 17 additions & 14 deletions examples/eutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifdef _WIN32
#include <ws2tcpip.h>
#endif
#include "evhtp.h"

static void *
mm__dup_(const void * src, size_t size)
Expand All @@ -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;
}
1 change: 0 additions & 1 deletion examples/example_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdint.h>
#include <errno.h>
#include <evhtp.h>
#include <unistd.h>

#include "./eutils.h"
#include "internal.h"
Expand Down
2 changes: 2 additions & 0 deletions include/evhtp/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions include/evhtp/evhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions include/evhtp/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
19 changes: 17 additions & 2 deletions include/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ 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]"
#define __log_info_color(X) "[\x1b[32m" X "\x1b[0;39m]"
#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 */
Expand Down Expand Up @@ -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 <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

#ifdef __cplusplus
}
Expand Down
5 changes: 3 additions & 2 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <arpa/inet.h>
#include <sys/queue.h>
#endif
#include "internal.h"
#include "evhtp/evhtp.h"
#include "evhtp/log.h"

Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand Down

0 comments on commit 948fd02

Please sign in to comment.