diff options
author | Henry Castro <hcastro@collabora.com> | 2019-02-10 17:03:31 -0400 |
---|---|---|
committer | Henry Castro <hcastro@collabora.com> | 2019-03-05 20:56:46 -0400 |
commit | 306b12b9bc2e0808751ea24282be792839d9ca40 (patch) | |
tree | f369235317067330dfd6a05c8838fb1516227b2b /wsd/FileServer.cpp | |
parent | wsd: add boost::locale generation and caching (diff) | |
download | online-306b12b9bc2e0808751ea24282be792839d9ca40.tar.gz online-306b12b9bc2e0808751ea24282be792839d9ca40.zip |
wsd: preprocess javascript file for L10n
Change-Id: Ib802beca673e1e90ad5dd9c33cd5191300ae7bf8
Diffstat (limited to 'wsd/FileServer.cpp')
-rw-r--r-- | wsd/FileServer.cpp | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp index b82becbf17..97509813dc 100644 --- a/wsd/FileServer.cpp +++ b/wsd/FileServer.cpp @@ -335,7 +335,10 @@ void FileServerRequestHandler::handleRequest(const HTTPRequest& request, Poco::M const std::string fileType = endPoint.substr(extPoint + 1); std::string mimeType; if (fileType == "js") - mimeType = "application/javascript"; + { + preprocessJS(request, socket); + return; + } else if (fileType == "css") mimeType = "text/css"; else if (fileType == "html") @@ -1047,4 +1050,37 @@ void FileServerRequestHandler::parse(const std::locale& locale, std::istringstre ostr << varL10n.str(); } } + +void FileServerRequestHandler::preprocessJS(const HTTPRequest& request, const std::shared_ptr<StreamSocket>& socket) +{ + std::string lang("en"); + Poco::Net::HTTPResponse response; + const Poco::URI::QueryParameters params = Poco::URI(request.getURI()).getQueryParameters(); + auto pos = std::find_if(params.begin(), params.end(), + [](const std::pair<std::string, std::string>& it) { return it.first == "lang"; }); + 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::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()); + + LOG_DBG("Sent file: " << relPath); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |