This commit is contained in:
Vladimir Serdyuk 2022-12-05 16:12:24 +08:00 committed by GitHub
commit 9963b2b8a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 112 additions and 50 deletions

View File

@ -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}
@ -275,7 +283,7 @@ if(ENABLE_ASIO_LIB)
asio_client_tls_context.cc
)
add_library(nghttp2_asio SHARED
add_library(nghttp2_asio ${LIB_TYPE}
${NGHTTP2_ASIO_SOURCES}
$<TARGET_OBJECTS:llhttp>
$<TARGET_OBJECTS:url-parser>
@ -284,13 +292,19 @@ 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"
)
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
${NGHTTP2_TARGET}
${OPENSSL_LIBRARIES}
${Boost_LIBRARIES}
)

View File

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

View File

@ -60,10 +60,10 @@ namespace nghttp2 {
template <size_t N> 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<uint8_t, N> buf;
uint8_t *pos, *last;
Memchunk *knext;

View File

@ -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';
@ -399,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';
@ -442,15 +468,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 +496,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) {
@ -1683,7 +1723,9 @@ int daemonize(int nochdir, int noclose) {
}
}
return 0;
#else // !__APPLE__
#elif defined(_MSC_VER)
return -1;
#else // !__APPLE__ && !_MSC_VER
return daemon(nochdir, noclose);
#endif // !__APPLE__
}
@ -1735,6 +1777,7 @@ int msghdr_get_local_addr(Address &dest, msghdr *msg, int family) {
return -1;
}
#endif
unsigned int msghdr_get_ecn(msghdr *msg, int family) {
switch (family) {

View File

@ -944,6 +944,7 @@ StringRef rstrip(BlockAllocator &balloc, const StringRef &s);
#ifdef ENABLE_HTTP3
int msghdr_get_local_addr(Address &dest, msghdr *msg, int family);
#endif
unsigned int msghdr_get_ecn(msghdr *msg, int family);