From 85b1bda3ef601392ba5e91e0e3803f5e197e9399 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 27 Oct 2024 22:37:15 +0100 Subject: [PATCH] Use new EchoBody example as compliance subject --- build.c | 2 +- examples/EchoBody.cpp | 39 +++++++++++++++++++++++++++++++++++++++ tests/Makefile | 4 ++-- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 examples/EchoBody.cpp diff --git a/build.c b/build.c index 3ef9bff4a..057b21a1e 100644 --- a/build.c +++ b/build.c @@ -9,7 +9,7 @@ int main(int argc, char **argv) { char *CXX = strcpy(calloc(1024, 1), or_else(getenv("CXX"), "g++")); char *EXEC_SUFFIX = strcpy(calloc(1024, 1), maybe(getenv("EXEC_SUFFIX"))); - char *EXAMPLE_FILES[] = {"CachingApp", "HelloWorldThreaded", "Http3Server", "Broadcast", "HelloWorld", "Crc32", "ServerName", + char *EXAMPLE_FILES[] = {"EchoBody", "CachingApp", "HelloWorldThreaded", "Http3Server", "Broadcast", "HelloWorld", "Crc32", "ServerName", "EchoServer", "BroadcastingEchoServer", "UpgradeSync", "UpgradeAsync", "ParameterRoutes"}; strcat(CXXFLAGS, " -march=native -O3 -Wpedantic -Wall -Wextra -Wsign-conversion -Wconversion -std=c++20 -Isrc -IuSockets/src"); diff --git a/examples/EchoBody.cpp b/examples/EchoBody.cpp new file mode 100644 index 000000000..7c453bd10 --- /dev/null +++ b/examples/EchoBody.cpp @@ -0,0 +1,39 @@ +#include "App.h" + +/* Takes any method and echoes back the sent body. Can be used to test compliance of HTTP spec. */ +/* This example is also a good benchmark for body echoing. */ + +int main() { + + uWS::App().get("/*", [](auto *res, auto */*req*/) { + /* Technically the any route could be used likewise, but GET is optimized like this */ + res->end(); + }).any("/*", [](auto *res, auto */*req*/) { + std::unique_ptr buffer; + res->onData([res, buffer = std::move(buffer)](std::string_view chunk, bool isFin) mutable { + if (isFin) [[likely]] { + if (buffer.get()) [[unlikely]] { + buffer->append(chunk); + res->end(*buffer); + } else { + res->end(chunk); + } + } else { + if (!buffer.get()) { + buffer = std::make_unique(chunk); + } else { + buffer->append(chunk); + } + } + }); + + /* In this particular case we actually don't need to know this, as we only rely on RAII above. */ + res->onAborted([]() {}); + }).listen(3000, [](auto *listen_socket) { + if (listen_socket) { + std::cerr << "Listening on port " << 3000 << std::endl; + } + }).run(); + + std::cout << "Failed to listen on port 3000" << std::endl; +} diff --git a/tests/Makefile b/tests/Makefile index 6c21bf165..bb079e52a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -26,7 +26,7 @@ smoke: pkill Crc32 compliance: - ../HelloWorld & + ../EchoBody & sleep 1 ~/.deno/bin/deno run --allow-net ../h1spec/http_test.ts localhost 3000 - pkill HelloWorld + pkill EchoBody