wsd: allow compression gzip for html and js resources

Change-Id: I0c6030c91e379cf1d78950516d2b6b8aa6bd018b
This commit is contained in:
Henry Castro
2019-02-10 18:02:44 -04:00
parent 306b12b9bc
commit b879f9dd06

View File

@ -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;
response.write(oss);
oss << ostr.str();
socket->send(oss.str());
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 << (gzip ? ogzip.str() : ostr.str());
socket->send(oss.str());
LOG_DBG("Sent file: " << relPath);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */