Http3 POST/GET example working

This commit is contained in:
Alex Hultman
2022-08-29 02:19:24 +02:00
parent f2ec5e8453
commit f740778997
4 changed files with 67 additions and 12 deletions
+37 -4
View File
@@ -4,19 +4,52 @@
#include "Http3App.h"
#include <iostream>
/* Tentative example of simple Http3 server */
/* Example of simple Http3 server handling both GET with headers and POST with body.
* You might be surprised to find out you can replace uWS::H3App with uWS::SSLApp and
* serve TCP/TLS-based HTTP instead of QUIC-based HTTP, using the same very code ;) */
int main() {
uWS::H3App({
uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).get("/*", [](auto *res, auto *req) {
/* Printing these should obviously be disabled if doing benchmarking */
//std::cout << req->getHeader(":path") << std::endl;
//std::cout << req->getHeader(":method") << std::endl;
std::cout << req->getHeader(":path") << std::endl;
std::cout << req->getHeader(":method") << std::endl;
res->end("Hello H3 from uWS!");
}).post("/*", [](auto *res, auto */*req*/) {
/* You also need to set onAborted if receiving data */
res->onData([res, bodyBuffer = (std::string *)nullptr](std::string_view chunk, bool isLast) mutable {
if (isLast) {
if (bodyBuffer) {
/* Send back the (chunked) body we got, as response */
bodyBuffer->append(chunk);
res->end(*bodyBuffer);
delete bodyBuffer;
} else {
/* Send back the body we got, as response (fast path) */
res->end(chunk);
}
} else {
/* Slow path */
if (!bodyBuffer) {
bodyBuffer = new std::string;
}
/* If we got the body in a chunk, buffer it up until whole */
bodyBuffer->append(chunk);
}
});
/* If you have pending, asynch work, you should abort such work in this callback */
res->onAborted([]() {
/* Again, just printing is not enough, you need to abort any pending work here
* so that nothing will call res->end, since the request was aborted and deleted */
printf("Stream was aborted!\n");
});
}).listen(9004, [](auto *listen_socket) {
if (listen_socket) {
std::cout << "Listening on port " << 9004 << std::endl;