Transport: Fix static file serving from SSL connections
This patch fixes the behavior of serveFile with SSL enabled servers. Before, the sendfile(2) syscall was used, but cannot work if our current version of OpenSSL, because the transfer happens kernel-side, and the SSL encryption does not. So I've implemented a _very_ basic sendfile utility for SSL connections, which only stream 4096 bytes of the file per call, in order not to block the whole pipeline. It's worth noting that this patch will be useless when the upstream moves to OpenSSL 3.0, which uses Kernel TLS, and implement a zero-copy version of SSL_sendfile, which is far more superior than the one implemented in this patch. I've tested the basic use cases with this patch, whis is file downloading. I've used small and big (45G) files, and it appears to be working. I've launched kip's tests[1] on my code, and it seems to be passing everything: Start 25: https_server_test 25/25 Test #25: https_server_test ................ Passed 0.07 sec [1] https://github.com/kiplingw/pistache/tree/kip-ssl-servefile-test Here is my example file: #include <pistache/endpoint.h> #include <iostream> using namespace Pistache; struct HelloHandler : public Http::Handler { HTTP_PROTOTYPE(HelloHandler) void onRequest(const Http::Request& request, Http::ResponseWriter writer) { Http::serveFile(writer, "main.cpp"); } }; int main(void) { std::cout << "Starting server\n"; Pistache::Address addr; auto opts = Http::Endpoint::options().threads(5); addr = Pistache::Address(Pistache::Ipv4::any(), Pistache::Port(9999)); Http::Endpoint server(addr); server.init(opts); server.setHandler(std::make_shared<HelloHandler>()); server.useSSL("./cas/server/server.crt", "./cas/server/server.key", true); std::cout << "SSL enabled\n"; server.serve(); return 0; } $> curl https://localhost:9999 -k > test % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 770 100 770 0 0 30800 0 --:--:-- --:--:-- --:--:-- 30800 $> diff test main.cpp $> Pull Request: https://github.com/oktal/pistache/pull/620 Fork: https://git.mobley.ne02ptzero.me/~louis/pistacheSigned-off-by:Louis Solofrizzo <lsolofrizzo@online.net>
Showing
include/pistache/utils.h
0 → 100644
src/common/utils.cc
0 → 100644
Please register or sign in to comment