diff options
author | Henry Castro <hcastro@collabora.com> | 2019-02-10 18:02:44 -0400 |
---|---|---|
committer | Henry Castro <hcastro@collabora.com> | 2019-03-05 21:14:04 -0400 |
commit | b879f9dd06afec3275cb820cdc88e776978790f5 (patch) | |
tree | 1ac2e33474620fa225a93c27d95ba457aa0fc76c /wsd/FileServer.cpp | |
parent | wsd: preprocess javascript file for L10n (diff) | |
download | online-b879f9dd06afec3275cb820cdc88e776978790f5.tar.gz online-b879f9dd06afec3275cb820cdc88e776978790f5.zip |
wsd: allow compression gzip for html and js resources
Change-Id: I0c6030c91e379cf1d78950516d2b6b8aa6bd018b
Diffstat (limited to 'wsd/FileServer.cpp')
-rw-r--r-- | wsd/FileServer.cpp | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp index 97509813dc..9626daf763 100644 --- a/wsd/FileServer.cpp +++ b/wsd/FileServer.cpp @@ -24,6 +24,7 @@ #include <Poco/DateTime.h> #include <Poco/DateTimeFormat.h> #include <Poco/DateTimeFormatter.h> +#include <Poco/DeflatingStream.h> #include <Poco/Exception.h> #include <Poco/FileStream.h> #include <Poco/Net/HTMLForm.h> @@ -701,8 +702,9 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco:: documentSigningDiv = "<div id=\"document-signing-bar\"></div>"; } + std::streampos size; std::string lang("en"); - std::ostringstream ostr; + std::ostringstream ostr, ogzip; std::istringstream istr(preprocess); auto pos = std::find_if(params.begin(), params.end(), @@ -779,6 +781,16 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco:: }); const std::string mimeType = "text/html"; + bool gzip = request.hasToken("Accept-Encoding", "gzip"); + if (gzip) + { + Poco::DeflatingOutputStream deflater(ogzip, Poco::DeflatingStreamBuf::STREAM_GZIP, 8); + deflater << ostr.str(); + deflater.close(); + size = ogzip.tellp(); + } + else + size = ostr.tellp(); std::ostringstream oss; oss << "HTTP/1.1 200 OK\r\n" @@ -795,6 +807,9 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco:: // Document signing: if endpoint URL is configured, whitelist that for // iframe purposes. + if (gzip) + oss << "Content-Encoding: gzip\r\n"; + std::ostringstream cspOss; cspOss << "Content-Security-Policy: default-src 'none'; " << "frame-src 'self' blob: " << documentSigningURL << "; " @@ -904,7 +919,7 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco:: } oss << "\r\n" - << ostr.str(); + << (gzip ? ogzip.str() : ostr.str()); socket->send(oss.str()); LOG_DBG("Sent file: " << relPath << ": " << preprocess); @@ -1061,26 +1076,39 @@ void FileServerRequestHandler::preprocessJS(const HTTPRequest& request, const st if (pos != params.end()) lang = pos->second; - response.setContentType("application/javascript"); - response.set("User-Agent", HTTP_AGENT_STRING); - response.set("Date", Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT)); - response.add("X-Content-Type-Options", "nosniff"); - const std::string relPath = getRequestPathname(request); LOG_DBG("Preprocessing file: " << relPath); std::string preprocess = *getUncompressedFile(relPath); - std::ostringstream ostr; + std::streampos size; + std::ostringstream oss, ostr, ogzip; std::istringstream istr(preprocess); std::locale locale(LOOLWSD::Generator(lang + ".utf8")); parse(locale, istr, ostr, [](const std::string&) { return false; }); - std::ostringstream oss; + bool gzip = request.hasToken("Accept-Encoding", "gzip"); + if (gzip) + { + response.set("Content-Encoding", "gzip"); + Poco::DeflatingOutputStream deflater(ogzip, Poco::DeflatingStreamBuf::STREAM_GZIP, 8); + deflater << ostr.str(); + deflater.close(); + size = ogzip.tellp(); + } + else + size = ostr.tellp(); + + response.setContentType("application/javascript"); + response.setContentLength(static_cast<int>(size)); + response.setChunkedTransferEncoding(false); + response.set("User-Agent", HTTP_AGENT_STRING); + response.set("Date", Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT)); + response.add("X-Content-Type-Options", "nosniff"); response.write(oss); - oss << ostr.str(); - socket->send(oss.str()); + oss << (gzip ? ogzip.str() : ostr.str()); + socket->send(oss.str()); LOG_DBG("Sent file: " << relPath); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |