Update Http example server
This commit is contained in:
+28
-41
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
/* Helpers for this example */
|
/* Helpers for this example */
|
||||||
#include "helpers/AsyncFileReader.h"
|
#include "helpers/AsyncFileReader.h"
|
||||||
|
#include "helpers/AsyncFileStreamer.h"
|
||||||
#include "helpers/Middleware.h"
|
#include "helpers/Middleware.h"
|
||||||
|
|
||||||
/* optparse */
|
/* optparse */
|
||||||
@@ -19,68 +20,54 @@ int main(int argc, char **argv) {
|
|||||||
struct optparse_long longopts[] = {
|
struct optparse_long longopts[] = {
|
||||||
{"port", 'p', OPTPARSE_REQUIRED},
|
{"port", 'p', OPTPARSE_REQUIRED},
|
||||||
{"help", 'h', OPTPARSE_NONE},
|
{"help", 'h', OPTPARSE_NONE},
|
||||||
{"color", 'c', OPTPARSE_REQUIRED},
|
{"passphrase", 'a', OPTPARSE_REQUIRED},
|
||||||
{"delay", 'd', OPTPARSE_OPTIONAL},
|
{"key", 'k', OPTPARSE_REQUIRED},
|
||||||
|
{"cert", 'c', OPTPARSE_REQUIRED},
|
||||||
|
{"dh_params", 'd', OPTPARSE_REQUIRED},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
int port = 3000;
|
int port = 3000;
|
||||||
|
struct us_ssl_socket_context_options ssl_options = {};
|
||||||
|
|
||||||
while ((option = optparse_long(&options, longopts, nullptr)) != -1) {
|
while ((option = optparse_long(&options, longopts, nullptr)) != -1) {
|
||||||
switch (option) {
|
switch (option) {
|
||||||
case 'p':
|
case 'p':
|
||||||
port = /*options.optarg ? */atoi(options.optarg);// : port;
|
port = atoi(options.optarg);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'a':
|
||||||
|
ssl_options.passphrase = options.optarg;
|
||||||
//break;
|
|
||||||
/*case 'b':
|
|
||||||
brief = true;
|
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
color = options.optarg;
|
ssl_options.cert_file_name = options.optarg;
|
||||||
|
break;
|
||||||
|
case 'k':
|
||||||
|
ssl_options.key_file_name = options.optarg;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
delay = options.optarg ? atoi(options.optarg) : 1;
|
ssl_options.dh_params_file_name = options.optarg;
|
||||||
break;*/
|
break;
|
||||||
|
case 'h':
|
||||||
case '?':
|
case '?':
|
||||||
std::cout << "Usage: " << argv[0] << " [--help] [--port <port>] [--ssl <cert> <key>] [--passphrase <ssl key passphrase>] [--dh_params <ssl dh params file>] <public root>" << std::endl;
|
fail:
|
||||||
|
std::cout << "Usage: " << argv[0] << " [--help] [--port <port>] [--key <ssl key>] [--cert <ssl cert>] [--passphrase <ssl key passphrase>] [--dh_params <ssl dh params file>] <public root>" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
//fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
|
|
||||||
//exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print remaining arguments. */
|
char *root = optparse_arg(&options);
|
||||||
char *arg;
|
if (!root) {
|
||||||
while ((arg = optparse_arg(&options)))
|
goto fail;
|
||||||
printf("%s\n", arg);
|
|
||||||
|
|
||||||
std::cout << "Port is " << port << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (argc < 3 || argc > 7) {
|
|
||||||
std::cout << "Usage: HttpServer root port [ssl_cert ssl_key ssl_dh_params ssl_passphrase]" << std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//int port = atoi(argv[2]);
|
AsyncFileStreamer asyncFileStreamer(root);
|
||||||
const char *root = argv[1];
|
|
||||||
/* Cache files of root folder */
|
|
||||||
//FileCache fileCache(root);
|
|
||||||
|
|
||||||
/* Either serve over HTTP or HTTPS */
|
/* Either serve over HTTP or HTTPS */
|
||||||
if (argc > 5) {
|
struct us_ssl_socket_context_options empty_ssl_options = {};
|
||||||
|
if (memcmp(&ssl_options, &empty_ssl_options, sizeof(empty_ssl_options))) {
|
||||||
/* HTTPS */
|
/* HTTPS */
|
||||||
uWS::SSLApp({
|
uWS::SSLApp(ssl_options).get("/*", [&asyncFileStreamer](auto *res, auto *req) {
|
||||||
.cert_file_name = argv[3], /* Required */
|
//asyncFileStreamer.streamFile(res, req->getUrl());
|
||||||
.key_file_name = argv[4], /* Required */
|
|
||||||
.dh_params_file_name = argv[5], /* Required (in this example) */
|
|
||||||
.passphrase = (argc == 7 ? argv[6] : nullptr) /* Passphrase is optional */
|
|
||||||
}).get("/*", [](auto *res, auto *req) {
|
|
||||||
//serveFile(res, req)->write(fileCache.getFile(req->getUrl()));
|
|
||||||
}).listen(port, [port, root](auto *token) {
|
}).listen(port, [port, root](auto *token) {
|
||||||
if (token) {
|
if (token) {
|
||||||
std::cout << "Serving " << root << " over HTTPS a " << port << std::endl;
|
std::cout << "Serving " << root << " over HTTPS a " << port << std::endl;
|
||||||
@@ -88,8 +75,8 @@ int main(int argc, char **argv) {
|
|||||||
}).run();
|
}).run();
|
||||||
} else {
|
} else {
|
||||||
/* HTTP */
|
/* HTTP */
|
||||||
uWS::App().get("/*", [](auto *res, auto *req) {
|
uWS::App().get("/*", [&asyncFileStreamer](auto *res, auto *req) {
|
||||||
//serveFile(res, req)->write(fileCache.getFile(req->getUrl()));
|
asyncFileStreamer.streamFile(res, req->getUrl());
|
||||||
}).listen(port, [port, root](auto *token) {
|
}).listen(port, [port, root](auto *token) {
|
||||||
if (token) {
|
if (token) {
|
||||||
std::cout << "Serving " << root << " over HTTP a " << port << std::endl;
|
std::cout << "Serving " << root << " over HTTP a " << port << std::endl;
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#include <experimental/filesystem>
|
||||||
|
|
||||||
|
struct AsyncFileStreamer {
|
||||||
|
|
||||||
|
std::map<std::string_view, AsyncFileReader *> asyncFileReaders;
|
||||||
|
std::string root;
|
||||||
|
|
||||||
|
AsyncFileStreamer(std::string root) : root(root) {
|
||||||
|
// for all files in this path, init the map of AsyncFileReaders
|
||||||
|
updateRootCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateRootCache() {
|
||||||
|
// todo: if the root folder changes, we want to reload the cache
|
||||||
|
for(auto &p : std::experimental::filesystem::recursive_directory_iterator(root)) {
|
||||||
|
std::string url = p.path().string().substr(root.length());
|
||||||
|
if (url == "/index.html") {
|
||||||
|
url = "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
char *key = new char[url.length()];
|
||||||
|
memcpy(key, url.data(), url.length());
|
||||||
|
asyncFileReaders[std::string_view(key, url.length())] = new AsyncFileReader(p.path().string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void streamFile(uWS::HttpResponse<false> *res, std::string_view url) {
|
||||||
|
auto it = asyncFileReaders.find(url);
|
||||||
|
if (it == asyncFileReaders.end()) {
|
||||||
|
std::cout << "Did not find file: " << url << std::endl;
|
||||||
|
} else {
|
||||||
|
streamFile(res, it->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void streamFile(uWS::HttpResponse<false> *res, AsyncFileReader *asyncFileReader) {
|
||||||
|
/* Peek from cache */
|
||||||
|
std::string_view chunk = asyncFileReader->peek(res->getWriteOffset());
|
||||||
|
if (!chunk.length() || res->tryEnd(chunk, asyncFileReader->getFileSize())) {
|
||||||
|
/* Request new chunk */
|
||||||
|
// todo: we need to abort this callback if peer closed!
|
||||||
|
// this also means Loop::defer needs to support aborting
|
||||||
|
|
||||||
|
// us_socket_up_ref eftersom vi delar ägandeskapet
|
||||||
|
|
||||||
|
asyncFileReader->request(res->getWriteOffset(), [res, asyncFileReader](std::string_view chunk) {
|
||||||
|
// check if we were closed in the mean time
|
||||||
|
//if (us_socket_is_closed()) {
|
||||||
|
// free it here
|
||||||
|
//return;
|
||||||
|
//}
|
||||||
|
|
||||||
|
/* We were aborted for some reason */
|
||||||
|
if (!chunk.length()) {
|
||||||
|
// todo: make sure to check for is_closed internally after all callbacks!
|
||||||
|
res->close();
|
||||||
|
} else {
|
||||||
|
streamFile(res, asyncFileReader);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
/* We failed writing everything, so let's continue when we can */
|
||||||
|
res->onWritable([res, asyncFileReader](int offset) {
|
||||||
|
|
||||||
|
// här kan skiten avbrytas!
|
||||||
|
|
||||||
|
streamFile(res, asyncFileReader);
|
||||||
|
// todo: I don't really know what this is supposed to mean?
|
||||||
|
return false;
|
||||||
|
})->onAborted([]() {
|
||||||
|
std::cout << "ABORTED!" << std::endl;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
+1
-67
@@ -1,73 +1,7 @@
|
|||||||
#include "App.h"
|
#include "App.h"
|
||||||
|
|
||||||
#include "../examples/helpers/AsyncFileReader.h"
|
#include "../examples/helpers/AsyncFileReader.h"
|
||||||
|
#include "../examples/helpers/AsyncFileStreamer.h"
|
||||||
#include <experimental/filesystem>
|
|
||||||
|
|
||||||
struct AsyncFileStreamer {
|
|
||||||
|
|
||||||
std::map<std::string_view, AsyncFileReader *> asyncFileReaders;
|
|
||||||
std::string root;
|
|
||||||
|
|
||||||
AsyncFileStreamer(std::string root) : root(root) {
|
|
||||||
// for all files in this path, init the map of AsyncFileReaders
|
|
||||||
updateRootCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateRootCache() {
|
|
||||||
// todo: if the root folder changes, we want to reload the cache
|
|
||||||
for(auto &p : std::experimental::filesystem::recursive_directory_iterator(root)) {
|
|
||||||
std::string url = p.path().string().substr(root.length());
|
|
||||||
if (url == "/index.html") {
|
|
||||||
url = "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
char *key = new char[url.length()];
|
|
||||||
memcpy(key, url.data(), url.length());
|
|
||||||
asyncFileReaders[std::string_view(key, url.length())] = new AsyncFileReader(p.path().string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void streamFile(uWS::HttpResponse<false> *res, std::string_view url) {
|
|
||||||
auto it = asyncFileReaders.find(url);
|
|
||||||
if (it == asyncFileReaders.end()) {
|
|
||||||
std::cout << "Did not find file: " << url << std::endl;
|
|
||||||
} else {
|
|
||||||
streamFile(res, it->second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void streamFile(uWS::HttpResponse<false> *res, AsyncFileReader *asyncFileReader) {
|
|
||||||
/* Peek from cache */
|
|
||||||
std::string_view chunk = asyncFileReader->peek(res->getWriteOffset());
|
|
||||||
if (!chunk.length() || res->tryEnd(chunk, asyncFileReader->getFileSize())) {
|
|
||||||
/* Request new chunk */
|
|
||||||
// todo: we need to abort this callback if peer closed!
|
|
||||||
// this also means Loop::defer needs to support aborting
|
|
||||||
asyncFileReader->request(res->getWriteOffset(), [res, asyncFileReader](std::string_view chunk) {
|
|
||||||
/* We were aborted for some reason */
|
|
||||||
if (!chunk.length()) {
|
|
||||||
// todo: make sure to check for is_closed internally after all callbacks!
|
|
||||||
res->close();
|
|
||||||
} else {
|
|
||||||
streamFile(res, asyncFileReader);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
/* We failed writing everything, so let's continue when we can */
|
|
||||||
res->onWritable([res, asyncFileReader](int offset) {
|
|
||||||
|
|
||||||
// här kan skiten avbrytas!
|
|
||||||
|
|
||||||
streamFile(res, asyncFileReader);
|
|
||||||
// todo: I don't really know what this is supposed to mean?
|
|
||||||
return false;
|
|
||||||
})->onAborted([]() {
|
|
||||||
std::cout << "ABORTED!" << std::endl;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user