wsd/FileServer.cpp | 38 +++++++++++++++++++++++++++++++++++++- wsd/FileServer.hpp | 1 + 2 files changed, 38 insertions(+), 1 deletion(-)
New commits: commit 306b12b9bc2e0808751ea24282be792839d9ca40 Author: Henry Castro <hcas...@collabora.com> AuthorDate: Sun Feb 10 17:03:31 2019 -0400 Commit: Henry Castro <hcas...@collabora.com> CommitDate: Tue Mar 5 20:56:46 2019 -0400 wsd: preprocess javascript file for L10n Change-Id: Ib802beca673e1e90ad5dd9c33cd5191300ae7bf8 diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp index b82becbf1..97509813d 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: */ diff --git a/wsd/FileServer.hpp b/wsd/FileServer.hpp index c5a5133cd..df33ca217 100644 --- a/wsd/FileServer.hpp +++ b/wsd/FileServer.hpp @@ -22,6 +22,7 @@ class FileServerRequestHandler static void getToken(std::istringstream&, std::string&); static void parse(const std::locale&, std::istringstream&, std::ostringstream&, const std::function<bool(const std::string&)>&); + static void preprocessJS(const Poco::Net::HTTPRequest& request, const std::shared_ptr<StreamSocket>& socket); static void preprocessFile(const Poco::Net::HTTPRequest& request, Poco::MemoryInputStream& message, const std::shared_ptr<StreamSocket>& socket); static void preprocessAdminFile(const Poco::Net::HTTPRequest& request, const std::shared_ptr<StreamSocket>& socket); public: _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits