Skip to content
Merged
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
80 changes: 47 additions & 33 deletions wish/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(
abseil
GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
GIT_TAG 20260107.1
GIT_TAG 255c84dadd029fd8ad25c5efb5933e47beaa00c7 # 20260107.1
)
FetchContent_MakeAvailable(abseil)

Expand All @@ -42,7 +42,7 @@ FetchContent_Declare(
wslay
GIT_REPOSITORY https://github.com/tatsuhiro-t/wslay.git
GIT_TAG 0e7d106ff89ad6638090fd811a9b2e4c5dda8d40
PATCH_COMMAND git apply --reverse --check ${CMAKE_CURRENT_SOURCE_DIR}/patches/wslay-disable-mask.patch || git apply ${CMAKE_CURRENT_SOURCE_DIR}/patches/wslay-disable-mask.patch
PATCH_COMMAND git apply --reverse --check ${CMAKE_CURRENT_SOURCE_DIR}/patches/wslay.patch || git apply ${CMAKE_CURRENT_SOURCE_DIR}/patches/wslay.patch
)


Expand All @@ -61,7 +61,7 @@ set(EVENT__DISABLE_SAMPLES ON CACHE BOOL "" FORCE)
FetchContent_Declare(
libevent
GIT_REPOSITORY https://github.com/libevent/libevent.git
GIT_TAG master
GIT_TAG 780acfe8b2495949f0dc3ebd6f18eea2dec605a6
)


Expand All @@ -72,42 +72,50 @@ FetchContent_Declare(
)


FetchContent_Declare(
picohttpparser
GIT_REPOSITORY https://github.com/h2o/picohttpparser.git
GIT_TAG 539bb9f510f5aefc8efda53aaf822a21ecae2832 # v1.2
)


# These variables are unprefixed, so MakeAvailable is called immediately and
# they are unset afterwards to avoid leaking into other dependencies.

set(ENABLE_FAILMALLOC OFF CACHE BOOL "" FORCE)

# Since BUILD_SHARED_LIBS=OFF, we need to explicitly enable the static target (nghttp2_static)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(BUILD_STATIC_LIBS ON CACHE BOOL "" FORCE)

# nghttp2: build only the core library (no TLS apps, no examples).
set(ENABLE_LIB_ONLY ON CACHE BOOL "" FORCE)
set(ENABLE_APP OFF CACHE BOOL "" FORCE)
set(ENABLE_HPACK_TOOLS OFF CACHE BOOL "" FORCE)

set(ENABLE_DOC OFF CACHE BOOL "" FORCE)
set(ENABLE_PYTHON_BINDINGS OFF CACHE BOOL "" FORCE)
set(ENABLE_EXAMPLES OFF CACHE BOOL "" FORCE)
set(ENABLE_FAILMALLOC OFF CACHE BOOL "" FORCE)
set(WITH_JEMALLOC OFF CACHE BOOL "" FORCE)
set(WITH_LIBEVENT_OPENSSL OFF CACHE BOOL "" FORCE)
set(WITH_OPENSSL OFF CACHE BOOL "" FORCE)

set(WITH_LIBXML2 OFF CACHE BOOL "" FORCE)
set(WITH_SYSTEMD OFF CACHE BOOL "" FORCE)
set(WITH_JEMALLOC OFF CACHE BOOL "" FORCE)
set(WITH_MRUBY OFF CACHE BOOL "" FORCE)
set(WITH_NEVERBLEED OFF CACHE BOOL "" FORCE)

# Since BUILD_SHARED_LIBS=OFF, we need to explicitly enable the static target (nghttp2_static)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(BUILD_STATIC_LIBS ON CACHE BOOL "" FORCE)

FetchContent_Declare(
nghttp2
GIT_REPOSITORY https://github.com/nghttp2/nghttp2.git
GIT_TAG v1.65.0
GIT_TAG 68cb6900fde14c77f0cd7add0e094a862960eb99 # v1.69.0
)
FetchContent_MakeAvailable(nghttp2)

# Reset the cached variables to avoid affecting other dependencies. They are not prefixed.
foreach(_var IN ITEMS
ENABLE_LIB_ONLY ENABLE_APP ENABLE_HPACK_TOOLS ENABLE_DOC
ENABLE_PYTHON_BINDINGS ENABLE_EXAMPLES ENABLE_FAILMALLOC
WITH_JEMALLOC WITH_LIBEVENT_OPENSSL WITH_OPENSSL WITH_LIBXML2
WITH_SYSTEMD WITH_MRUBY WITH_NEVERBLEED BUILD_STATIC_LIBS BUILD_SHARED_LIBS)
ENABLE_LIB_ONLY
ENABLE_DOC
ENABLE_FAILMALLOC
BUILD_SHARED_LIBS
BUILD_STATIC_LIBS
WITH_LIBXML2
WITH_JEMALLOC
WITH_MRUBY
WITH_NEVERBLEED)
unset(${_var} CACHE)
endforeach()

Expand All @@ -121,7 +129,7 @@ if(WISH_BUILD_BENCHMARKS)
FetchContent_Declare(
googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG main
GIT_TAG 192ef10025eb2c4cdd392bc502f0c852196baa48 # v1.9.5
)
FetchContent_MakeAvailable(googlebenchmark)
endif()
Expand All @@ -130,33 +138,39 @@ if(WISH_BUILD_TESTS)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.17.0
GIT_TAG 52eb8108c5bdec04579160ae17225d66034bd723 # v1.17.0
)
FetchContent_MakeAvailable(googletest)
endif()


FetchContent_MakeAvailable(wslay libevent boringssl)

FetchContent_MakeAvailable(wslay libevent boringssl picohttpparser)

include_directories(${wslay_SOURCE_DIR}/lib/includes)
include_directories(${libevent_SOURCE_DIR}/include)
include_directories(${libevent_BINARY_DIR}/include)
include_directories(${boringssl_SOURCE_DIR}/include)
include_directories(${nghttp2_SOURCE_DIR}/lib/includes)
include_directories(${nghttp2_BINARY_DIR}/lib/includes)
include_directories(${picohttpparser_SOURCE_DIR})
include_directories(src)

add_library(http1_handshake STATIC
src/handshake.h
src/handshake.cc
${picohttpparser_SOURCE_DIR}/picohttpparser.c
${picohttpparser_SOURCE_DIR}/picohttpparser.h
)
target_link_libraries(http1_handshake event absl::strings absl::log)

# We will compile libevent's bufferevent_openssl.c inside our library
# since we disabled it in libevent's own build.
# Build as a static library: libevent is also static and was not compiled with
# -fPIC, so it cannot be linked into a shared object.
add_library(buffer_event_web_stream STATIC
add_library(web_stream STATIC
src/web_stream.h
src/buffer_event_web_stream.cc
src/buffer_event_web_stream.h
src/handshake.h
src/handshake.cc
${libevent_SOURCE_DIR}/bufferevent_openssl.c
${libevent_SOURCE_DIR}/bufferevent_ssl.c
src/plain_server.cc
Expand All @@ -181,8 +195,8 @@ add_library(buffer_event_web_stream STATIC
src/h2_tls_client.h
)
# BoringSSL targets: ssl and crypto. nghttp2 for HTTP/2 framing.
target_link_libraries(buffer_event_web_stream wslay event ssl crypto nghttp2::nghttp2 absl::strings absl::log)
target_compile_definitions(buffer_event_web_stream PUBLIC EVENT__HAVE_OPENSSL=1)
target_link_libraries(web_stream http1_handshake wslay event ssl crypto nghttp2_static absl::strings absl::log)
target_compile_definitions(web_stream PUBLIC EVENT__HAVE_OPENSSL=1)


if(WISH_BUILD_EXAMPLES)
Expand All @@ -197,14 +211,14 @@ if(WISH_BUILD_TESTS)
enable_testing()

add_executable(buffer_event_web_stream_test src/buffer_event_web_stream_test.cc)
target_link_libraries(buffer_event_web_stream_test buffer_event_web_stream gtest_main gtest event wslay)
target_link_libraries(buffer_event_web_stream_test web_stream gtest_main gtest)
add_test(NAME buffer_event_web_stream_test COMMAND buffer_event_web_stream_test)

add_executable(nghttp2_web_stream_test src/nghttp2_web_stream_test.cc)
target_link_libraries(nghttp2_web_stream_test buffer_event_web_stream gtest_main gtest nghttp2::nghttp2 wslay)
target_link_libraries(nghttp2_web_stream_test web_stream gtest_main gtest)
add_test(NAME nghttp2_web_stream_test COMMAND nghttp2_web_stream_test)

add_executable(handshake_test src/handshake_test.cc)
target_link_libraries(handshake_test buffer_event_web_stream gtest_main gtest event wslay)
target_link_libraries(handshake_test http1_handshake gtest_main gtest event)
add_test(NAME handshake_test COMMAND handshake_test)
endif()
6 changes: 3 additions & 3 deletions wish/cpp/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_executable(plain_benchmark_client
plain_client.cc
)
target_link_libraries(plain_benchmark_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -16,7 +16,7 @@ add_executable(high_qps_benchmark_client
high_qps_client.cc
)
target_link_libraries(high_qps_benchmark_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -29,7 +29,7 @@ add_executable(tls_benchmark_client
tls_client.cc
)
target_link_libraries(tls_benchmark_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand Down
16 changes: 8 additions & 8 deletions wish/cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_executable(plain_echo_server
plain_echo_server.cc
)
target_link_libraries(plain_echo_server
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -15,7 +15,7 @@ add_executable(plain_hello_client
plain_hello_client.cc
)
target_link_libraries(plain_hello_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -27,7 +27,7 @@ add_executable(tls_echo_server
tls_echo_server.cc
)
target_link_libraries(tls_echo_server
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -39,7 +39,7 @@ add_executable(tls_hello_client
tls_hello_client.cc
)
target_link_libraries(tls_hello_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -53,7 +53,7 @@ add_executable(h2_echo_server
h2_echo_server.cc
)
target_link_libraries(h2_echo_server
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -65,7 +65,7 @@ add_executable(h2_hello_client
h2_hello_client.cc
)
target_link_libraries(h2_hello_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -77,7 +77,7 @@ add_executable(h2_tls_echo_server
h2_tls_echo_server.cc
)
target_link_libraries(h2_tls_echo_server
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand All @@ -89,7 +89,7 @@ add_executable(h2_tls_hello_client
h2_tls_hello_client.cc
)
target_link_libraries(h2_tls_hello_client
buffer_event_web_stream
web_stream
absl::flags
absl::flags_parse
absl::log
Expand Down
File renamed without changes.
44 changes: 35 additions & 9 deletions wish/cpp/src/buffer_event_web_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ void BufferEventWebStream::Start() {
}

// If there is already data in the input buffer, process it immediately.
if (evbuffer_get_length(bufferevent_get_input(bev_)) > 0) {
size_t input_len = evbuffer_get_length(bufferevent_get_input(bev_));
if (input_len > 0) {
ReadCallback(bev_, this);
}
}
Expand Down Expand Up @@ -186,7 +187,8 @@ void BufferEventWebStream::DrainCallback(bufferevent* bev,
BufferEventWebStream* stream = static_cast<BufferEventWebStream*>(ctx);

// Delete the stream only once all queued outbound data has been sent.
if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
size_t output_len = evbuffer_get_length(bufferevent_get_output(bev));
if (output_len == 0) {
stream->state_ = CLOSED;
delete stream;
}
Expand All @@ -205,9 +207,6 @@ void BufferEventWebStream::EventCallback(bufferevent* bev,
}

if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
// Connection closed
LOG(INFO) << "Connection closed.";

BufferEventWebStream* stream = static_cast<BufferEventWebStream*>(ctx);

// If the stream is still in OPEN state, the underlying connection closed
Expand Down Expand Up @@ -247,14 +246,40 @@ ssize_t BufferEventWebStream::WslaySendCallback(wslay_event_context* ctx,
int header_len = snprintf(header, sizeof(header), "%zx\r\n", len);
if (header_len <= 0) {
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);

return -1;
}

int write_header_rv = bufferevent_write(stream->bev_,
header,
static_cast<size_t>(header_len));
if (write_header_rv != 0) {
LOG(ERROR) << "bufferevent_write() failed";

wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);

return -1;
}

int write_data_rv = bufferevent_write(stream->bev_,
data,
len);
if (write_data_rv != 0) {
LOG(ERROR) << "bufferevent_write() failed";

wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);

return -1;
}

if (bufferevent_write(stream->bev_, header, static_cast<size_t>(header_len)) != 0 ||
bufferevent_write(stream->bev_, data, len) != 0 ||
bufferevent_write(stream->bev_, "\r\n", 2) != 0) {
int write_trailer_rv = bufferevent_write(stream->bev_,
"\r\n",
2);
if (write_trailer_rv != 0) {
LOG(ERROR) << "bufferevent_write() failed";

wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);

return -1;
}

Expand Down Expand Up @@ -376,7 +401,8 @@ ssize_t BufferEventWebStream::ReadChunkedBytes(uint8_t* buf, size_t len) {
}

case ChunkState::TRAILER: {
if (evbuffer_get_length(input) < 2) {
size_t input_len = evbuffer_get_length(input);
if (input_len < 2) {
wslay_event_set_error(ctx_, WSLAY_ERR_WOULDBLOCK);
return -1;
}
Expand Down
13 changes: 6 additions & 7 deletions wish/cpp/src/h2_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ bool H2Client::Init() {
return false;
}

if (bufferevent_socket_connect_hostname(bev,
dns_base_,
AF_INET,
host_.c_str(),
port_) < 0) {
int connect_rv = bufferevent_socket_connect_hostname(bev,
dns_base_,
AF_INET,
host_.c_str(),
port_);
if (connect_rv < 0) {
LOG(ERROR) << "bufferevent_socket_connect_hostname() failed";

return false;
Expand All @@ -109,8 +110,6 @@ bool H2Client::Init() {
void H2Client::SetOnOpen(OpenCallback cb) { on_open_ = cb; }

void H2Client::Run() {
LOG(INFO) << "Running...";

event_base_dispatch(base_);
}

Expand Down
Loading