asio: Fix MSVC build

This commit is contained in:
Vladimir Serdyuk 2021-09-11 16:49:11 +03:00
parent 67a531f5a3
commit 4181b070e9
4 changed files with 76 additions and 34 deletions

View File

@ -295,6 +295,9 @@ if(ENABLE_ASIO_LIB)
"${CMAKE_CURRENT_SOURCE_DIR}/../lib/includes" "${CMAKE_CURRENT_SOURCE_DIR}/../lib/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/includes" "${CMAKE_CURRENT_SOURCE_DIR}/includes"
) )
if(WIN32)
target_compile_definitions(nghttp2_asio PRIVATE NOMINMAX)
endif()
target_link_libraries(nghttp2_asio target_link_libraries(nghttp2_asio
${NGHTTP2_TARGET} ${NGHTTP2_TARGET}
${OPENSSL_LIBRARIES} ${OPENSSL_LIBRARIES}

View File

@ -31,6 +31,10 @@
#include "template.h" #include "template.h"
#include "http2.h" #include "http2.h"
#ifdef _MSC_VER
#include <io.h>
#endif
namespace nghttp2 { namespace nghttp2 {
namespace asio_http2 { namespace asio_http2 {

View File

@ -60,10 +60,10 @@ namespace nghttp2 {
template <size_t N> struct Memchunk { template <size_t N> struct Memchunk {
Memchunk(Memchunk *next_chunk) 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 len() const { return last - pos; }
size_t left() const { return std::end(buf) - last; } size_t left() const { return &(*std::end(buf)) - last; }
void reset() { pos = last = std::begin(buf); } void reset() { pos = last = &(*std::begin(buf)); }
std::array<uint8_t, N> buf; std::array<uint8_t, N> buf;
uint8_t *pos, *last; uint8_t *pos, *last;
Memchunk *knext; Memchunk *knext;

View File

@ -263,30 +263,36 @@ std::string http_date(time_t t) {
} }
char *http_date(char *res, time_t t) { char *http_date(char *res, time_t t) {
struct tm* ptms;
#if !defined(_MSC_VER)
struct tm tms; struct tm tms;
ptms = gmtime_r(&t, &tms);
#else
ptms = gmtime(&t);
#endif
if (gmtime_r(&t, &tms) == nullptr) { if (ptms == nullptr) {
return res; return res;
} }
auto p = 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 = std::copy_n(s, 3, p);
*p++ = ','; *p++ = ',';
*p++ = ' '; *p++ = ' ';
p = cpydig(p, tms.tm_mday, 2); p = cpydig(p, ptms->tm_mday, 2);
*p++ = ' '; *p++ = ' ';
s = MONTH[tms.tm_mon]; s = MONTH[ptms->tm_mon];
p = std::copy_n(s, 3, p); p = std::copy_n(s, 3, p);
*p++ = ' '; *p++ = ' ';
p = cpydig(p, tms.tm_year + 1900, 4); p = cpydig(p, ptms->tm_year + 1900, 4);
*p++ = ' '; *p++ = ' ';
p = cpydig(p, tms.tm_hour, 2); p = cpydig(p, ptms->tm_hour, 2);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_min, 2); p = cpydig(p, ptms->tm_min, 2);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_sec, 2); p = cpydig(p, ptms->tm_sec, 2);
s = " GMT"; s = " GMT";
p = std::copy_n(s, 4, p); 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) { char *common_log_date(char *res, time_t t) {
struct tm* ptms;
#if !defined(_MSC_VER)
struct tm tms; struct tm tms;
ptms = localtime_r(&t, &tms);
#else
ptms = localtime(&t);
#endif
if (localtime_r(&t, &tms) == nullptr) { if (ptms == nullptr) {
return res; return res;
} }
auto p = res; auto p = res;
p = cpydig(p, tms.tm_mday, 2); p = cpydig(p, ptms->tm_mday, 2);
*p++ = '/'; *p++ = '/';
auto s = MONTH[tms.tm_mon]; auto s = MONTH[ptms->tm_mon];
p = std::copy_n(s, 3, p); p = std::copy_n(s, 3, p);
*p++ = '/'; *p++ = '/';
p = cpydig(p, tms.tm_year + 1900, 4); p = cpydig(p, ptms->tm_year + 1900, 4);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_hour, 2); p = cpydig(p, ptms->tm_hour, 2);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_min, 2); p = cpydig(p, ptms->tm_min, 2);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_sec, 2); p = cpydig(p, ptms->tm_sec, 2);
*p++ = ' '; *p++ = ' ';
#ifdef HAVE_STRUCT_TM_TM_GMTOFF #ifdef HAVE_STRUCT_TM_TM_GMTOFF
auto gmtoff = tms.tm_gmtoff; auto gmtoff = ptms->tm_gmtoff;
#else // !HAVE_STRUCT_TM_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 #endif // !HAVE_STRUCT_TM_TM_GMTOFF
if (gmtoff >= 0) { if (gmtoff >= 0) {
*p++ = '+'; *p++ = '+';
@ -353,31 +365,38 @@ std::string iso8601_date(int64_t ms) {
char *iso8601_date(char *res, int64_t ms) { char *iso8601_date(char *res, int64_t ms) {
time_t sec = ms / 1000; time_t sec = ms / 1000;
tm tms; struct tm* ptms;
if (localtime_r(&sec, &tms) == nullptr) { #if !defined(_MSC_VER)
return res; struct tm tms;
ptms = localtime_r(&sec, &tms);
#else
ptms = localtime(&sec);
#endif
if (ptms == nullptr) {
return res;
} }
auto p = res; auto p = res;
p = cpydig(p, tms.tm_year + 1900, 4); p = cpydig(p, ptms->tm_year + 1900, 4);
*p++ = '-'; *p++ = '-';
p = cpydig(p, tms.tm_mon + 1, 2); p = cpydig(p, ptms->tm_mon + 1, 2);
*p++ = '-'; *p++ = '-';
p = cpydig(p, tms.tm_mday, 2); p = cpydig(p, ptms->tm_mday, 2);
*p++ = 'T'; *p++ = 'T';
p = cpydig(p, tms.tm_hour, 2); p = cpydig(p, ptms->tm_hour, 2);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_min, 2); p = cpydig(p, ptms->tm_min, 2);
*p++ = ':'; *p++ = ':';
p = cpydig(p, tms.tm_sec, 2); p = cpydig(p, ptms->tm_sec, 2);
*p++ = '.'; *p++ = '.';
p = cpydig(p, ms % 1000, 3); p = cpydig(p, ms % 1000, 3);
#ifdef HAVE_STRUCT_TM_TM_GMTOFF #ifdef HAVE_STRUCT_TM_TM_GMTOFF
auto gmtoff = tms.tm_gmtoff; auto gmtoff = ptms->tm_gmtoff;
#else // !HAVE_STRUCT_TM_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 #endif // !HAVE_STRUCT_TM_TM_GMTOFF
if (gmtoff == 0) { if (gmtoff == 0) {
*p++ = 'Z'; *p++ = 'Z';
@ -442,15 +461,17 @@ namespace bt = boost::posix_time;
// one-time definition of the locale that is used to parse UTC strings // 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) // (note that the time_input_facet is ref-counted and deleted automatically)
static const std::locale static const std::locale
ptime_locale(std::locale::classic(), http_ptime_locale(std::locale::classic(),
new bt::time_input_facet("%a, %d %b %Y %H:%M:%S GMT")); 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 #endif //_WIN32
time_t parse_http_date(const StringRef &s) { time_t parse_http_date(const StringRef &s) {
#ifdef _WIN32 #ifdef _WIN32
// there is no strptime - use boost // there is no strptime - use boost
std::stringstream sstr(s.str()); std::stringstream sstr(s.str());
sstr.imbue(ptime_locale); sstr.imbue(http_ptime_locale);
bt::ptime ltime; bt::ptime ltime;
sstr >> ltime; sstr >> ltime;
if (!sstr) if (!sstr)
@ -468,12 +489,24 @@ time_t parse_http_date(const StringRef &s) {
} }
time_t parse_openssl_asn1_time_print(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{}; tm tm{};
auto r = strptime(s.c_str(), "%b %d %H:%M:%S %Y GMT", &tm); auto r = strptime(s.c_str(), "%b %d %H:%M:%S %Y GMT", &tm);
if (r == nullptr) { if (r == nullptr) {
return 0; return 0;
} }
return nghttp2_timegm_without_yday(&tm); return nghttp2_timegm_without_yday(&tm);
#endif // !_WIN32
} }
char upcase(char c) { char upcase(char c) {
@ -1750,6 +1783,8 @@ int daemonize(int nochdir, int noclose) {
} }
} }
return 0; return 0;
#elif defined(_MSC_VER)
return -1;
#else // !defined(__APPLE__) #else // !defined(__APPLE__)
return daemon(nochdir, noclose); return daemon(nochdir, noclose);
#endif // !defined(__APPLE__) #endif // !defined(__APPLE__)