Skip to content

Commit 85b1bda

Browse files
committed
Use new EchoBody example as compliance subject
1 parent addac0b commit 85b1bda

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

build.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int main(int argc, char **argv) {
99
char *CXX = strcpy(calloc(1024, 1), or_else(getenv("CXX"), "g++"));
1010
char *EXEC_SUFFIX = strcpy(calloc(1024, 1), maybe(getenv("EXEC_SUFFIX")));
1111

12-
char *EXAMPLE_FILES[] = {"CachingApp", "HelloWorldThreaded", "Http3Server", "Broadcast", "HelloWorld", "Crc32", "ServerName",
12+
char *EXAMPLE_FILES[] = {"EchoBody", "CachingApp", "HelloWorldThreaded", "Http3Server", "Broadcast", "HelloWorld", "Crc32", "ServerName",
1313
"EchoServer", "BroadcastingEchoServer", "UpgradeSync", "UpgradeAsync", "ParameterRoutes"};
1414

1515
strcat(CXXFLAGS, " -march=native -O3 -Wpedantic -Wall -Wextra -Wsign-conversion -Wconversion -std=c++20 -Isrc -IuSockets/src");

examples/EchoBody.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "App.h"
2+
3+
/* Takes any method and echoes back the sent body. Can be used to test compliance of HTTP spec. */
4+
/* This example is also a good benchmark for body echoing. */
5+
6+
int main() {
7+
8+
uWS::App().get("/*", [](auto *res, auto */*req*/) {
9+
/* Technically the any route could be used likewise, but GET is optimized like this */
10+
res->end();
11+
}).any("/*", [](auto *res, auto */*req*/) {
12+
std::unique_ptr<std::string> buffer;
13+
res->onData([res, buffer = std::move(buffer)](std::string_view chunk, bool isFin) mutable {
14+
if (isFin) [[likely]] {
15+
if (buffer.get()) [[unlikely]] {
16+
buffer->append(chunk);
17+
res->end(*buffer);
18+
} else {
19+
res->end(chunk);
20+
}
21+
} else {
22+
if (!buffer.get()) {
23+
buffer = std::make_unique<std::string>(chunk);
24+
} else {
25+
buffer->append(chunk);
26+
}
27+
}
28+
});
29+
30+
/* In this particular case we actually don't need to know this, as we only rely on RAII above. */
31+
res->onAborted([]() {});
32+
}).listen(3000, [](auto *listen_socket) {
33+
if (listen_socket) {
34+
std::cerr << "Listening on port " << 3000 << std::endl;
35+
}
36+
}).run();
37+
38+
std::cout << "Failed to listen on port 3000" << std::endl;
39+
}

tests/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ smoke:
2626
pkill Crc32
2727

2828
compliance:
29-
../HelloWorld &
29+
../EchoBody &
3030
sleep 1
3131
~/.deno/bin/deno run --allow-net ../h1spec/http_test.ts localhost 3000
32-
pkill HelloWorld
32+
pkill EchoBody

0 commit comments

Comments
 (0)