forked from uNetworking/uWebSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoBody.cpp
More file actions
39 lines (34 loc) · 1.38 KB
/
EchoBody.cpp
File metadata and controls
39 lines (34 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}