From 67a531f5a396ca7646d4a73e4f1abbcf4c220020 Mon Sep 17 00:00:00 2001 From: Vladimir Serdyuk Date: Sat, 11 Sep 2021 16:48:44 +0300 Subject: [PATCH 1/4] asio: Enable static build --- src/CMakeLists.txt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8057de1b..f0286185 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,9 +25,17 @@ include_directories( ${LIBBPF_INCLUDE_DIRS} ) +if(ENABLE_STATIC_LIB) + set(NGHTTP2_TARGET nghttp2_static) + set(LIB_TYPE STATIC) +else() + set(NGHTTP2_TARGET nghttp2) + set(LIB_TYPE SHARED) +endif() + # XXX per-target? link_libraries( - nghttp2 + ${NGHTTP2_TARGET} ${JEMALLOC_LIBRARIES} ${LIBXML2_LIBRARIES} ${LIBEV_LIBRARIES} @@ -273,7 +281,7 @@ if(ENABLE_ASIO_LIB) asio_client_tls_context.cc ) - add_library(nghttp2_asio SHARED + add_library(nghttp2_asio ${LIB_TYPE} ${NGHTTP2_ASIO_SOURCES} $ $ @@ -282,13 +290,13 @@ if(ENABLE_ASIO_LIB) ${OPENSSL_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ) - target_include_directories(nghttp2_asio INTERFACE + target_include_directories(nghttp2_asio PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/../lib/includes" "${CMAKE_CURRENT_SOURCE_DIR}/../lib/includes" "${CMAKE_CURRENT_SOURCE_DIR}/includes" ) target_link_libraries(nghttp2_asio - nghttp2 + ${NGHTTP2_TARGET} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ) From 4181b070e98106fddb3ef344b7eb0608d4c65baf Mon Sep 17 00:00:00 2001 From: Vladimir Serdyuk Date: Sat, 11 Sep 2021 16:49:11 +0300 Subject: [PATCH 2/4] asio: Fix MSVC build --- src/CMakeLists.txt | 3 ++ src/asio_common.cc | 4 ++ src/memchunk.h | 6 +-- src/util.cc | 97 +++++++++++++++++++++++++++++++--------------- 4 files changed, 76 insertions(+), 34 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0286185..b162eea5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -295,6 +295,9 @@ if(ENABLE_ASIO_LIB) "${CMAKE_CURRENT_SOURCE_DIR}/../lib/includes" "${CMAKE_CURRENT_SOURCE_DIR}/includes" ) + if(WIN32) + target_compile_definitions(nghttp2_asio PRIVATE NOMINMAX) + endif() target_link_libraries(nghttp2_asio ${NGHTTP2_TARGET} ${OPENSSL_LIBRARIES} diff --git a/src/asio_common.cc b/src/asio_common.cc index 428dbd62..938a910c 100644 --- a/src/asio_common.cc +++ b/src/asio_common.cc @@ -31,6 +31,10 @@ #include "template.h" #include "http2.h" +#ifdef _MSC_VER +#include +#endif + namespace nghttp2 { namespace asio_http2 { diff --git a/src/memchunk.h b/src/memchunk.h index 7a7f2e9b..03ec0849 100644 --- a/src/memchunk.h +++ b/src/memchunk.h @@ -60,10 +60,10 @@ namespace nghttp2 { template struct Memchunk { Memchunk(Memchunk *next_chunk) - : pos(std::begin(buf)), last(pos), knext(next_chunk), next(nullptr) {} + : pos(&(*std::begin(buf))), last(pos), knext(next_chunk), next(nullptr) {} size_t len() const { return last - pos; } - size_t left() const { return std::end(buf) - last; } - void reset() { pos = last = std::begin(buf); } + size_t left() const { return &(*std::end(buf)) - last; } + void reset() { pos = last = &(*std::begin(buf)); } std::array buf; uint8_t *pos, *last; Memchunk *knext; diff --git a/src/util.cc b/src/util.cc index 6efc0e9e..ad7d681a 100644 --- a/src/util.cc +++ b/src/util.cc @@ -263,30 +263,36 @@ std::string http_date(time_t t) { } char *http_date(char *res, time_t t) { + struct tm* ptms; +#if !defined(_MSC_VER) struct tm tms; + ptms = gmtime_r(&t, &tms); +#else + ptms = gmtime(&t); +#endif - if (gmtime_r(&t, &tms) == nullptr) { + if (ptms == nullptr) { return res; } auto p = res; - auto s = DAY_OF_WEEK[tms.tm_wday]; + auto s = DAY_OF_WEEK[ptms->tm_wday]; p = std::copy_n(s, 3, p); *p++ = ','; *p++ = ' '; - p = cpydig(p, tms.tm_mday, 2); + p = cpydig(p, ptms->tm_mday, 2); *p++ = ' '; - s = MONTH[tms.tm_mon]; + s = MONTH[ptms->tm_mon]; p = std::copy_n(s, 3, p); *p++ = ' '; - p = cpydig(p, tms.tm_year + 1900, 4); + p = cpydig(p, ptms->tm_year + 1900, 4); *p++ = ' '; - p = cpydig(p, tms.tm_hour, 2); + p = cpydig(p, ptms->tm_hour, 2); *p++ = ':'; - p = cpydig(p, tms.tm_min, 2); + p = cpydig(p, ptms->tm_min, 2); *p++ = ':'; - p = cpydig(p, tms.tm_sec, 2); + p = cpydig(p, ptms->tm_sec, 2); s = " GMT"; p = std::copy_n(s, 4, p); @@ -301,32 +307,38 @@ std::string common_log_date(time_t t) { } char *common_log_date(char *res, time_t t) { + struct tm* ptms; +#if !defined(_MSC_VER) struct tm tms; + ptms = localtime_r(&t, &tms); +#else + ptms = localtime(&t); +#endif - if (localtime_r(&t, &tms) == nullptr) { + if (ptms == nullptr) { return res; } auto p = res; - p = cpydig(p, tms.tm_mday, 2); + p = cpydig(p, ptms->tm_mday, 2); *p++ = '/'; - auto s = MONTH[tms.tm_mon]; + auto s = MONTH[ptms->tm_mon]; p = std::copy_n(s, 3, p); *p++ = '/'; - p = cpydig(p, tms.tm_year + 1900, 4); + p = cpydig(p, ptms->tm_year + 1900, 4); *p++ = ':'; - p = cpydig(p, tms.tm_hour, 2); + p = cpydig(p, ptms->tm_hour, 2); *p++ = ':'; - p = cpydig(p, tms.tm_min, 2); + p = cpydig(p, ptms->tm_min, 2); *p++ = ':'; - p = cpydig(p, tms.tm_sec, 2); + p = cpydig(p, ptms->tm_sec, 2); *p++ = ' '; #ifdef HAVE_STRUCT_TM_TM_GMTOFF - auto gmtoff = tms.tm_gmtoff; + auto gmtoff = ptms->tm_gmtoff; #else // !HAVE_STRUCT_TM_TM_GMTOFF - auto gmtoff = nghttp2_timegm(&tms) - t; + auto gmtoff = nghttp2_timegm(ptms) - t; #endif // !HAVE_STRUCT_TM_TM_GMTOFF if (gmtoff >= 0) { *p++ = '+'; @@ -353,31 +365,38 @@ std::string iso8601_date(int64_t ms) { char *iso8601_date(char *res, int64_t ms) { time_t sec = ms / 1000; - tm tms; - if (localtime_r(&sec, &tms) == nullptr) { - return res; + struct tm* ptms; +#if !defined(_MSC_VER) + struct tm tms; + ptms = localtime_r(&sec, &tms); +#else + ptms = localtime(&sec); +#endif + + if (ptms == nullptr) { + return res; } auto p = res; - p = cpydig(p, tms.tm_year + 1900, 4); + p = cpydig(p, ptms->tm_year + 1900, 4); *p++ = '-'; - p = cpydig(p, tms.tm_mon + 1, 2); + p = cpydig(p, ptms->tm_mon + 1, 2); *p++ = '-'; - p = cpydig(p, tms.tm_mday, 2); + p = cpydig(p, ptms->tm_mday, 2); *p++ = 'T'; - p = cpydig(p, tms.tm_hour, 2); + p = cpydig(p, ptms->tm_hour, 2); *p++ = ':'; - p = cpydig(p, tms.tm_min, 2); + p = cpydig(p, ptms->tm_min, 2); *p++ = ':'; - p = cpydig(p, tms.tm_sec, 2); + p = cpydig(p, ptms->tm_sec, 2); *p++ = '.'; p = cpydig(p, ms % 1000, 3); #ifdef HAVE_STRUCT_TM_TM_GMTOFF - auto gmtoff = tms.tm_gmtoff; + auto gmtoff = ptms->tm_gmtoff; #else // !HAVE_STRUCT_TM_TM_GMTOFF - auto gmtoff = nghttp2_timegm(&tms) - sec; + auto gmtoff = nghttp2_timegm(ptms) - sec; #endif // !HAVE_STRUCT_TM_TM_GMTOFF if (gmtoff == 0) { *p++ = 'Z'; @@ -442,15 +461,17 @@ namespace bt = boost::posix_time; // one-time definition of the locale that is used to parse UTC strings // (note that the time_input_facet is ref-counted and deleted automatically) static const std::locale - ptime_locale(std::locale::classic(), - new bt::time_input_facet("%a, %d %b %Y %H:%M:%S GMT")); + http_ptime_locale(std::locale::classic(), + new bt::time_input_facet("%a, %d %b %Y %H:%M:%S GMT")), + openssl_asn1_ptime_locale(std::locale::classic(), + new bt::time_input_facet("%b %d %H:%M:%S %Y GMT")); #endif //_WIN32 time_t parse_http_date(const StringRef &s) { #ifdef _WIN32 // there is no strptime - use boost std::stringstream sstr(s.str()); - sstr.imbue(ptime_locale); + sstr.imbue(http_ptime_locale); bt::ptime ltime; sstr >> ltime; if (!sstr) @@ -468,12 +489,24 @@ time_t parse_http_date(const StringRef &s) { } time_t parse_openssl_asn1_time_print(const StringRef &s) { +#ifdef _WIN32 + // there is no strptime - use boost + std::stringstream sstr(s.str()); + sstr.imbue(openssl_asn1_ptime_locale); + bt::ptime ltime; + sstr >> ltime; + if (!sstr) + return 0; + + return boost::posix_time::to_time_t(ltime); +#else // !_WIN32 tm tm{}; auto r = strptime(s.c_str(), "%b %d %H:%M:%S %Y GMT", &tm); if (r == nullptr) { return 0; } return nghttp2_timegm_without_yday(&tm); +#endif // !_WIN32 } char upcase(char c) { @@ -1750,6 +1783,8 @@ int daemonize(int nochdir, int noclose) { } } return 0; +#elif defined(_MSC_VER) + return -1; #else // !defined(__APPLE__) return daemon(nochdir, noclose); #endif // !defined(__APPLE__) From 11d7e549fd66bcaf6af894d243486ac91a873a91 Mon Sep 17 00:00:00 2001 From: Vladimir Serdyuk Date: Sat, 11 Sep 2021 16:49:32 +0300 Subject: [PATCH 3/4] asio: Fix MSVC DLL exports --- src/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b162eea5..9f31b165 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -297,6 +297,9 @@ if(ENABLE_ASIO_LIB) ) if(WIN32) target_compile_definitions(nghttp2_asio PRIVATE NOMINMAX) + if(NOT ENABLE_STATIC_LIB) + set_target_properties(nghttp2_asio PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE) + endif() endif() target_link_libraries(nghttp2_asio ${NGHTTP2_TARGET} From 00eb9e2576ed6d6228b64169c37764ebf90cf8a2 Mon Sep 17 00:00:00 2001 From: Vladimir Serdyuk Date: Sat, 11 Sep 2021 16:49:46 +0300 Subject: [PATCH 4/4] asio: Fix MSVC build (after QUIC merge) --- src/util.cc | 31 ++++++++++++++++++++----------- src/util.h | 2 ++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/util.cc b/src/util.cc index ad7d681a..90f77777 100644 --- a/src/util.cc +++ b/src/util.cc @@ -418,27 +418,34 @@ char *iso8601_date(char *res, int64_t ms) { char *iso8601_basic_date(char *res, int64_t ms) { time_t sec = ms / 1000; - tm tms; - if (localtime_r(&sec, &tms) == nullptr) { - return res; + struct tm* ptms; +#if !defined(_MSC_VER) + struct tm tms; + ptms = localtime_r(&sec, &tms); +#else + ptms = localtime(&sec); +#endif + + if (ptms == nullptr) { + return res; } auto p = res; - p = cpydig(p, tms.tm_year + 1900, 4); - p = cpydig(p, tms.tm_mon + 1, 2); - p = cpydig(p, tms.tm_mday, 2); + p = cpydig(p, ptms->tm_year + 1900, 4); + p = cpydig(p, ptms->tm_mon + 1, 2); + p = cpydig(p, ptms->tm_mday, 2); *p++ = 'T'; - p = cpydig(p, tms.tm_hour, 2); - p = cpydig(p, tms.tm_min, 2); - p = cpydig(p, tms.tm_sec, 2); + p = cpydig(p, ptms->tm_hour, 2); + p = cpydig(p, ptms->tm_min, 2); + p = cpydig(p, ptms->tm_sec, 2); *p++ = '.'; p = cpydig(p, ms % 1000, 3); #ifdef HAVE_STRUCT_TM_TM_GMTOFF - auto gmtoff = tms.tm_gmtoff; + auto gmtoff = ptms->tm_gmtoff; #else // !HAVE_STRUCT_TM_TM_GMTOFF - auto gmtoff = nghttp2_timegm(&tms) - sec; + auto gmtoff = nghttp2_timegm(ptms) - sec; #endif // !HAVE_STRUCT_TM_TM_GMTOFF if (gmtoff == 0) { *p++ = 'Z'; @@ -1790,6 +1797,7 @@ int daemonize(int nochdir, int noclose) { #endif // !defined(__APPLE__) } +#ifdef ENABLE_HTTP3 int msghdr_get_local_addr(Address &dest, msghdr *msg, int family) { switch (family) { case AF_INET: @@ -1823,6 +1831,7 @@ int msghdr_get_local_addr(Address &dest, msghdr *msg, int family) { return -1; } +#endif } // namespace util diff --git a/src/util.h b/src/util.h index ca9b437d..17879694 100644 --- a/src/util.h +++ b/src/util.h @@ -863,7 +863,9 @@ std::mt19937 make_mt19937(); // daemon() using fork(). int daemonize(int nochdir, int noclose); +#ifdef ENABLE_HTTP3 int msghdr_get_local_addr(Address &dest, msghdr *msg, int family); +#endif } // namespace util