Skip to content

Commit

Permalink
Fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jan 27, 2025
1 parent f1cc38e commit ba00df5
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 244 deletions.
8 changes: 4 additions & 4 deletions examples/Broadcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ int main() {
* You may swap to using uWS:App() if you don't need SSL */
uWS::SSLApp app = uWS::SSLApp({
/* There are example certificates in uWebSockets.js repo */
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).ws<PerSocketData>("/*", {
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).ws<PerSocketData>("/*", {
/* Settings */
.compression = uWS::SHARED_COMPRESSOR,
.maxPayloadLength = 16 * 1024 * 1024,
Expand Down
8 changes: 4 additions & 4 deletions examples/BroadcastingEchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ int main() {
* You may swap to using uWS:App() if you don't need SSL */
uWS::SSLApp *app = new uWS::SSLApp({
/* There are example certificates in uWebSockets.js repo */
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
});
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
});

app->ws<PerSocketData>("/*", {
/* Settings */
Expand Down
62 changes: 31 additions & 31 deletions examples/Crc32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,40 @@ uint32_t crc32(const char *s, size_t n, uint32_t crc = 0xFFFFFFFF) {

int main() {

uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).post("/*", [](auto *res, auto *req) {
uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).post("/*", [](auto *res, auto *req) {

/* Display the headers */
std::cout << " --- " << req->getUrl() << " --- " << std::endl;
for (auto [key, value] : *req) {
std::cout << key << ": " << value << std::endl;
}
/* Display the headers */
std::cout << " --- " << req->getUrl() << " --- " << std::endl;
for (auto [key, value] : *req) {
std::cout << key << ": " << value << std::endl;
}

auto isAborted = std::make_shared<bool>(false);
uint32_t crc = 0xFFFFFFFF;
res->onData([res, isAborted, crc](std::string_view chunk, bool isFin) mutable {
if (chunk.length()) {
crc = crc32(chunk.data(), chunk.length(), crc);
}
auto isAborted = std::make_shared<bool>(false);
uint32_t crc = 0xFFFFFFFF;
res->onData([res, isAborted, crc](std::string_view chunk, bool isFin) mutable {
if (chunk.length()) {
crc = crc32(chunk.data(), chunk.length(), crc);
}

if (isFin && !*isAborted) {
std::stringstream s;
s << std::hex << (~crc) << std::endl;
res->end(s.str());
}
});
if (isFin && !*isAborted) {
std::stringstream s;
s << std::hex << (~crc) << std::endl;
res->end(s.str());
}
});

res->onAborted([isAborted]() {
*isAborted = true;
});
}).listen(3000, [](auto *listen_socket) {
if (listen_socket) {
std::cerr << "Listening on port " << 3000 << std::endl;
}
}).run();
res->onAborted([isAborted]() {
*isAborted = true;
});
}).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;
std::cout << "Failed to listen on port 3000" << std::endl;
}
58 changes: 29 additions & 29 deletions examples/EchoBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@

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);
}
}
});
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();
/* 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;
std::cout << "Failed to listen on port 3000" << std::endl;
}
8 changes: 4 additions & 4 deletions examples/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ int main() {
* You may swap to using uWS:App() if you don't need SSL */
uWS::App({
/* There are example certificates in uWebSockets.js repo */
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).ws<PerSocketData>("/*", {
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).ws<PerSocketData>("/*", {
/* Settings */
.compression = uWS::CompressOptions(uWS::DEDICATED_COMPRESSOR_4KB | uWS::DEDICATED_DECOMPRESSOR),
.maxPayloadLength = 100 * 1024 * 1024,
Expand Down
26 changes: 13 additions & 13 deletions examples/HelloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */

int main() {
/* Overly simple hello world app */
uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).get("/*", [](auto *res, auto */*req*/) {
res->end("Hello world!");
}).listen(3000, [](auto *listen_socket) {
if (listen_socket) {
std::cout << "Listening on port " << 3000 << std::endl;
}
}).run();
/* Overly simple hello world app */
uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).get("/*", [](auto *res, auto */*req*/) {
res->end("Hello world!");
}).listen(3000, [](auto *listen_socket) {
if (listen_socket) {
std::cout << "Listening on port " << 3000 << std::endl;
}
}).run();

std::cout << "Failed to listen on port 3000" << std::endl;
std::cout << "Failed to listen on port 3000" << std::endl;
}
140 changes: 70 additions & 70 deletions examples/Http3Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,84 @@
/* Todo: use onWritable and tryEnd instead of end */
int main() {

/* Read video file to memory */
std::ifstream file("video.mp4", std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
/* Read video file to memory */
std::ifstream file("video.mp4", std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);

std::vector<char> buffer(size);
if (!file.read(buffer.data(), size)) {
std::cout << "Failed to load video.mp4" << std::endl;
return 0;
}
std::vector<char> buffer(size);
if (!file.read(buffer.data(), size)) {
std::cout << "Failed to load video.mp4" << std::endl;
return 0;
}

/* We need a bootstrapping server that instructs
* the web browser to use HTTP3 */
(*new uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
})).get("/*", [&buffer](auto *res, auto *req) {
res->writeHeader("Alt-Svc", "h3=\":9004\"");
res->writeHeader("Alternative-Protocol", "quic:9004");
res->end("<html><h1>This is not HTTP3! Try refreshing (works in Firefox!)</h1></html>");
}).listen(9004, [](auto *listen_socket) {
if (listen_socket) {
std::cout << "Bootstrapping server Listening on port " << 9004 << std::endl;
}
});
/* We need a bootstrapping server that instructs
* the web browser to use HTTP3 */
(*new uWS::SSLApp({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
})).get("/*", [&buffer](auto *res, auto *req) {
res->writeHeader("Alt-Svc", "h3=\":9004\"");
res->writeHeader("Alternative-Protocol", "quic:9004");
res->end("<html><h1>This is not HTTP3! Try refreshing (works in Firefox!)</h1></html>");
}).listen(9004, [](auto *listen_socket) {
if (listen_socket) {
std::cout << "Bootstrapping server Listening on port " << 9004 << std::endl;
}
});

/* And we serve the video over HTTP3 */
uWS::H3App({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).get("/*", [&buffer](auto *res, auto *req) {
res->end("<html><h1>Welcome to HTTP3! <a href=\"video.mp4\">Go see a movie</a></html></h1>");
}).get("/video.mp4", [&buffer](auto *res, auto *req) {
/* Send back a video */
res->end({&buffer[0], buffer.size()});
}).post("/*", [](auto *res, auto *req) {
/* And we serve the video over HTTP3 */
uWS::H3App({
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"
}).get("/*", [&buffer](auto *res, auto *req) {
res->end("<html><h1>Welcome to HTTP3! <a href=\"video.mp4\">Go see a movie</a></html></h1>");
}).get("/video.mp4", [&buffer](auto *res, auto *req) {
/* Send back a video */
res->end({&buffer[0], buffer.size()});
}).post("/*", [](auto *res, auto *req) {

std::cout << "Got POST request at " << req->getHeader(":path") << std::endl;
std::cout << "Got POST request at " << req->getHeader(":path") << std::endl;

/* 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) {
std::cout << "Sending back posted body now" << std::endl;
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);
}
/* 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) {
std::cout << "Sending back posted body now" << std::endl;
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 << "HTTP/3 server Listening on port " << 9004 << std::endl;
}
}).run();
/* 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 << "HTTP/3 server Listening on port " << 9004 << std::endl;
}
}).run();

std::cout << "Failed to listen on port 9004" << std::endl;
std::cout << "Failed to listen on port 9004" << std::endl;
}

#else
Expand Down
Loading

0 comments on commit ba00df5

Please # to comment.