[Libreoffice-commits] online.git: wsd/FileServer.cpp

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Wed Mar 6 01:14:33 UTC 2019


 wsd/FileServer.cpp |   50 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 11 deletions(-)

New commits:
commit b879f9dd06afec3275cb820cdc88e776978790f5
Author:     Henry Castro <hcastro at collabora.com>
AuthorDate: Sun Feb 10 18:02:44 2019 -0400
Commit:     Henry Castro <hcastro at collabora.com>
CommitDate: Tue Mar 5 21:14:04 2019 -0400

    wsd: allow compression gzip for html and js resources
    
    Change-Id: I0c6030c91e379cf1d78950516d2b6b8aa6bd018b

diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp
index 97509813d..9626daf76 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: */


More information about the Libreoffice-commits mailing list