Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Build on Windows #13

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ set(LT_AGE 0)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
include(Version)

set(CMAKE_CXX_STANDARD 11)

math(EXPR LT_SOVERSION "${LT_CURRENT} - ${LT_AGE}")
set(LT_VERSION "${LT_SOVERSION}.${LT_AGE}.${LT_REVISION}")
set(PACKAGE_VERSION "${PROJECT_VERSION}")
Expand Down
70 changes: 46 additions & 24 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,57 @@ set(NGHTTP2_ASIO_SOURCES
asio_client_tls_context.cc
)

add_library(nghttp2_asio SHARED
${NGHTTP2_ASIO_SOURCES}
$<TARGET_OBJECTS:url-parser>
)
target_include_directories(nghttp2_asio PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/../third-party"
if (NOT ENABLE_SHARED_LIB AND NOT ENABLE_STATIC_LIB)
if (BUILD_SHARED_LIBS)
set(ENABLE_SHARED_LIB ON)
else()
set(ENABLE_STATIC_LIB ON)
endif()
endif()

${LIBNGHTTP2_INCLUDE_DIRS}
${OPENSSL_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
# Object lib used for both static and shared lib
add_library(nghttp2_asio_object OBJECT ${NGHTTP2_ASIO_SOURCES})
target_include_directories(nghttp2_asio_object
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/../third-party"
${LIBNGHTTP2_INCLUDE_DIRS}
${OPENSSL_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
INTERFACE
"${CMAKE_CURRENT_BINARY_DIR}/../lib/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/../lib/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
)
target_include_directories(nghttp2_asio INTERFACE
"${CMAKE_CURRENT_BINARY_DIR}/../lib/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/../lib/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
target_link_libraries(nghttp2_asio_object
PRIVATE
${LIBNGHTTP2_LIBRARIES}
${OPENSSL_LIBRARIES}
PUBLIC
${Boost_LIBRARIES}
)
target_link_libraries(nghttp2_asio
${LIBNGHTTP2_LIBRARIES}
${OPENSSL_LIBRARIES}
${Boost_LIBRARIES}
set_target_properties(nghttp2_asio_object
PROPERTIES
VERSION ${LT_VERSION}
SOVERSION ${LT_SOVERSION}
)
set_target_properties(nghttp2_asio PROPERTIES
VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION})

install(TARGETS nghttp2_asio
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
# Public shared library
if(ENABLE_SHARED_LIB)
set_property(TARGET nghttp2_asio_object PROPERTY POSITION_INDEPENDENT_CODE 1)
target_compile_definitions(nghttp2_asio_object PUBLIC BUILDING_NGHTTP2_ASIO)
add_library(nghttp2_asio SHARED)
target_link_libraries(nghttp2_asio PUBLIC nghttp2_asio_object $<TARGET_OBJECTS:url-parser>)
install(TARGETS nghttp2_asio)
endif()

# Static library
if(ENABLE_STATIC_LIB)
add_library(nghttp2_asio_static STATIC)
target_link_libraries(nghttp2_asio_static PUBLIC nghttp2_asio_object $<TARGET_OBJECTS:url-parser>)
set_target_properties(nghttp2_asio_static PROPERTIES ARCHIVE_OUTPUT_NAME nghttp2_asio${STATIC_LIB_SUFFIX})
install(TARGETS nghttp2_asio_static)
endif()

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libnghttp2_asio.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
8 changes: 5 additions & 3 deletions lib/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# include <sys/uio.h>
#endif // !_WIN32

#include <algorithm>
#include <cassert>
#include <utility>

Expand Down Expand Up @@ -59,7 +60,8 @@ struct BlockAllocator {
: retain(nullptr),
head(nullptr),
block_size(block_size),
isolation_threshold(std::min(block_size, isolation_threshold)) {
isolation_threshold(std::min<size_t>(block_size, isolation_threshold))
{
assert(isolation_threshold <= block_size);
}

Expand Down Expand Up @@ -109,7 +111,7 @@ struct BlockAllocator {

void *alloc(size_t size) {
if (size + sizeof(size_t) >= isolation_threshold) {
auto len = std::max(static_cast<size_t>(16), size);
auto len = std::max<size_t>(static_cast<size_t>(16), size);
// We will store the allocated size in size_t field.
auto mb = alloc_mem_block(len + sizeof(size_t));
auto sp = reinterpret_cast<size_t *>(mb->begin);
Expand Down Expand Up @@ -158,7 +160,7 @@ struct BlockAllocator {
return ptr;
}

auto nalloclen = std::max(size + 1, alloclen * 2);
auto nalloclen = std::max<size_t>(size + 1, alloclen * 2);

auto res = alloc(nalloclen);
std::copy_n(p, alloclen, static_cast<uint8_t *>(res));
Expand Down
18 changes: 14 additions & 4 deletions lib/asio_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <fcntl.h>
#include <memory>

#ifdef _WIN32
# include <io.h>
#endif

#include "util.h"
#include "template.h"
#include "http2.h"
Expand Down Expand Up @@ -80,7 +84,7 @@ generator_cb string_generator(std::string data) {
return [strio](uint8_t *buf, size_t len, uint32_t *data_flags) {
auto &data = strio->first;
auto &left = strio->second;
auto n = std::min(len, left);
auto n = std::min<size_t>(len, left);
std::copy_n(data.c_str() + data.size() - left, n, buf);
left -= n;
if (left == 0) {
Expand All @@ -102,8 +106,14 @@ std::shared_ptr<Defer<F, T...>> defer_shared(F &&f, T &&...t) {
std::forward<T>(t)...);
}

#ifdef _WIN32
# define FD_MAGIC(f) _##f
#else
# define FD_MAGIC(f) f
#endif

generator_cb file_generator(const std::string &path) {
auto fd = open(path.c_str(), O_RDONLY);
auto fd = FD_MAGIC(open)(path.c_str(), FD_MAGIC(O_RDONLY));
if (fd == -1) {
return generator_cb();
}
Expand All @@ -112,12 +122,12 @@ generator_cb file_generator(const std::string &path) {
}

generator_cb file_generator_from_fd(int fd) {
auto d = defer_shared(close, fd);
auto d = defer_shared(FD_MAGIC(close), fd);

return [fd, d](uint8_t *buf, size_t len,
uint32_t *data_flags) -> generator_cb::result_type {
ssize_t n;
while ((n = read(fd, buf, len)) == -1 && errno == EINTR)
while ((n = FD_MAGIC(read)(fd, buf, len)) == -1 && errno == EINTR)
;

if (n == -1) {
Expand Down
6 changes: 4 additions & 2 deletions lib/http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "http2.h"
#include "util.h"

#include <algorithm>

namespace nghttp2 {

namespace http2 {
Expand Down Expand Up @@ -599,9 +601,9 @@ StringRef path_join(BlockAllocator &balloc, const StringRef &base_path,
const StringRef &base_query, const StringRef &rel_path,
const StringRef &rel_query) {
auto res = make_byte_ref(
balloc, std::max(static_cast<size_t>(1), base_path.size()) +
balloc, std::max<size_t>(static_cast<size_t>(1), base_path.size()) +
rel_path.size() + 1 +
std::max(base_query.size(), rel_query.size()) + 1);
std::max<size_t>(base_query.size(), rel_query.size()) + 1);
auto p = res.base;

if (rel_path.empty()) {
Expand Down
37 changes: 26 additions & 11 deletions lib/includes/nghttp2/asio_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@

#include <nghttp2/nghttp2.h>

#ifndef __has_declspec_attribute
# define __has_declspec_attribute(x) false
#endif
#if defined(_WIN32) || \
(__has_declspec_attribute(dllexport) && __has_declspec_attribute(dllimport))
# ifdef BUILDING_NGHTTP2_ASIO
# define NGHTTP2_ASIO_EXPORT __declspec(dllexport)
# else
# define NGHTTP2_ASIO_EXPORT __declspec(dllimport)
# endif
#else
# define NGHTTP2_ASIO_EXPORT
#endif

namespace nghttp2 {

namespace asio_http2 {

struct header_value {
struct NGHTTP2_ASIO_EXPORT header_value {
// header field value
std::string value;
// true if the header field value is sensitive information, such as
Expand All @@ -55,9 +69,9 @@ struct header_value {
// header fields. The header field name must be lower-cased.
using header_map = std::multimap<std::string, header_value>;

const boost::system::error_category &nghttp2_category() noexcept;
NGHTTP2_ASIO_EXPORT const boost::system::error_category &nghttp2_category() noexcept;

struct uri_ref {
struct NGHTTP2_ASIO_EXPORT uri_ref {
std::string scheme;
std::string host;
// form after percent-encoding decoded
Expand Down Expand Up @@ -95,35 +109,36 @@ typedef std::function<ssize_t(uint8_t *buf, std::size_t len,

// Convenient function to create function to read file denoted by
// |path|. This can be passed to response::end().
generator_cb file_generator(const std::string &path);
NGHTTP2_ASIO_EXPORT generator_cb file_generator(const std::string &path);

// Like file_generator(const std::string&), but it takes opened file
// descriptor. The passed descriptor will be closed when returned
// function object is destroyed.
generator_cb file_generator_from_fd(int fd);
NGHTTP2_ASIO_EXPORT generator_cb file_generator_from_fd(int fd);

// Validates path so that it does not contain directory traversal
// vector. Returns true if path is safe. The |path| must start with
// "/" otherwise returns false. This function should be called after
// percent-decode was performed.
bool check_path(const std::string &path);
NGHTTP2_ASIO_EXPORT bool check_path(const std::string &path);

// Performs percent-decode against string |s|.
std::string percent_decode(const std::string &s);
NGHTTP2_ASIO_EXPORT std::string percent_decode(const std::string &s);

// Returns HTTP date representation of current posix time |t|.
std::string http_date(int64_t t);
NGHTTP2_ASIO_EXPORT std::string http_date(int64_t t);

// Parses |uri| and extract scheme, host and service. The service is
// port component of URI (e.g., "8443") if available, otherwise it is
// scheme (e.g., "https").
NGHTTP2_ASIO_EXPORT
boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
std::string &scheme,
std::string &host,
std::string &service,
const std::string &uri);

enum nghttp2_asio_error {
enum NGHTTP2_ASIO_EXPORT nghttp2_asio_error {
NGHTTP2_ASIO_ERR_NO_ERROR = 0,
NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED = 1,
};
Expand All @@ -136,11 +151,11 @@ namespace boost {

namespace system {

template <> struct is_error_code_enum<nghttp2_error> {
template <> struct NGHTTP2_ASIO_EXPORT is_error_code_enum<nghttp2_error> {
BOOST_STATIC_CONSTANT(bool, value = true);
};

template <> struct is_error_code_enum<nghttp2::asio_http2::nghttp2_asio_error> {
template <> struct NGHTTP2_ASIO_EXPORT is_error_code_enum<nghttp2::asio_http2::nghttp2_asio_error> {
BOOST_STATIC_CONSTANT(bool, value = true);
};

Expand Down
9 changes: 5 additions & 4 deletions lib/includes/nghttp2/asio_http2_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace client {

class response_impl;

class response {
class NGHTTP2_ASIO_EXPORT response {
public:
// Application must not call this directly.
response();
Expand Down Expand Up @@ -71,7 +71,7 @@ using connect_cb =

class request_impl;

class request {
class NGHTTP2_ASIO_EXPORT request {
public:
// Application must not call this directly.
request();
Expand Down Expand Up @@ -119,7 +119,7 @@ class request {
};

// Wrapper around an nghttp2_priority_spec.
class priority_spec {
class NGHTTP2_ASIO_EXPORT priority_spec {
public:
// The default ctor is used only by sentinel values.
priority_spec() = default;
Expand All @@ -142,7 +142,7 @@ class priority_spec {

class session_impl;

class session {
class NGHTTP2_ASIO_EXPORT session {
public:
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "80") using clear text TCP connection with connect timeout
Expand Down Expand Up @@ -239,6 +239,7 @@ class session {

// configure |tls_ctx| for client use. Currently, we just set NPN
// callback for HTTP/2.
NGHTTP2_ASIO_EXPORT
boost::system::error_code
configure_tls_context(boost::system::error_code &ec,
boost::asio::ssl::context &tls_ctx);
Expand Down
10 changes: 6 additions & 4 deletions lib/includes/nghttp2/asio_http2_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace server {
class request_impl;
class response_impl;

class request {
class NGHTTP2_ASIO_EXPORT request {
public:
// Application must not call this directly.
request();
Expand Down Expand Up @@ -66,7 +66,7 @@ class request {
std::unique_ptr<request_impl> impl_;
};

class response {
class NGHTTP2_ASIO_EXPORT response {
public:
// Application must not call this directly.
response();
Expand Down Expand Up @@ -130,7 +130,7 @@ typedef std::function<void(const request &, const response &)> request_cb;

class http2_impl;

class http2 {
class NGHTTP2_ASIO_EXPORT http2 {
public:
http2();
~http2();
Expand Down Expand Up @@ -224,17 +224,19 @@ class http2 {
// Configures |tls_context| for server use. This function sets couple
// of OpenSSL options (disables SSLv2 and SSLv3 and compression) and
// enables ECDHE ciphers. NPN callback is also configured.
NGHTTP2_ASIO_EXPORT
boost::system::error_code
configure_tls_context_easy(boost::system::error_code &ec,
boost::asio::ssl::context &tls_context);

// Returns request handler to do redirect to |uri| using
// |status_code|. The |uri| appears in "location" header field as is.
NGHTTP2_ASIO_EXPORT
request_cb redirect_handler(int status_code, std::string uri);

// Returns request handler to reply with given |status_code| and HTML
// including message about status code.
request_cb status_handler(int status_code);
NGHTTP2_ASIO_EXPORT request_cb status_handler(int status_code);

} // namespace server

Expand Down
Loading