a few upgrades for the HttpServer example (#955)

This commit is contained in:
rimmartin
2019-10-29 14:28:11 -04:00
committed by Alex Hultman
parent 8af994f713
commit 4a9ab556bd
3 changed files with 6 additions and 6 deletions
+2 -2
View File
@@ -28,7 +28,7 @@ int main(int argc, char **argv) {
};
int port = 3000;
struct us_new_socket_context_options_t ssl_options = {};
struct us_socket_context_options_t ssl_options = {};
while ((option = optparse_long(&options, longopts, nullptr)) != -1) {
switch (option) {
@@ -63,7 +63,7 @@ int main(int argc, char **argv) {
AsyncFileStreamer asyncFileStreamer(root);
/* Either serve over HTTP or HTTPS */
struct us_new_socket_context_options_t empty_ssl_options = {};
struct us_socket_context_options_t empty_ssl_options = {};
if (memcmp(&ssl_options, &empty_ssl_options, sizeof(empty_ssl_options))) {
/* HTTPS */
uWS::SSLApp(ssl_options).get("/*", [&asyncFileStreamer](auto *res, auto *req) {
+1 -1
View File
@@ -45,7 +45,7 @@ public:
// get loop for thread
loop = uWS::Loop::defaultLoop();
loop = uWS::Loop::get();
}
/* Returns any data already cached for this offset */
+3 -3
View File
@@ -1,4 +1,4 @@
#include <experimental/filesystem>
#include <filesystem>
struct AsyncFileStreamer {
@@ -12,7 +12,7 @@ struct AsyncFileStreamer {
void updateRootCache() {
// todo: if the root folder changes, we want to reload the cache
for(auto &p : std::experimental::filesystem::recursive_directory_iterator(root)) {
for(auto &p : std::filesystem::recursive_directory_iterator(root)) {
std::string url = p.path().string().substr(root.length());
if (url == "/index.html") {
url = "/";
@@ -38,7 +38,7 @@ struct AsyncFileStreamer {
static void streamFile(uWS::HttpResponse<SSL> *res, AsyncFileReader *asyncFileReader) {
/* Peek from cache */
std::string_view chunk = asyncFileReader->peek(res->getWriteOffset());
if (!chunk.length() || res->tryEnd(chunk, asyncFileReader->getFileSize())) {
if (!chunk.length() || res->tryEnd(chunk, asyncFileReader->getFileSize()).first) {
/* Request new chunk */
// todo: we need to abort this callback if peer closed!
// this also means Loop::defer needs to support aborting (functions should embedd an atomic boolean abort or something)