Skip to content

Commit

Permalink
Use new EchoBody example as compliance subject
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Oct 27, 2024
1 parent addac0b commit 85b1bda
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
39 changes: 39 additions & 0 deletions examples/EchoBody.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string> 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<std::string>(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;
}
4 changes: 2 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 85b1bda

Please sign in to comment.