-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new EchoBody example as compliance subject
- Loading branch information
1 parent
addac0b
commit 85b1bda
Showing
3 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters