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

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Tue Mar 5 20:32:21 UTC 2019


 loleaflet/html/loleaflet.html.m4 |   30 +++----
 wsd/FileServer.cpp               |  160 ++++++++++++++++++++++++++++++++++-----
 wsd/FileServer.hpp               |    1 
 3 files changed, 156 insertions(+), 35 deletions(-)

New commits:
commit ed89931ae8ceff62b720a31cf1e163eeee3280fd
Author:     Henry Castro <hcastro at collabora.com>
AuthorDate: Tue Feb 5 17:03:48 2019 -0400
Commit:     Henry Castro <hcastro at collabora.com>
CommitDate: Tue Mar 5 16:31:51 2019 -0400

    wsd: use a tiny parser, variable substitution
    
    Change-Id: I821d27ef504a01d0b040f2b7ae7f66e75b16eb96

diff --git a/loleaflet/html/loleaflet.html.m4 b/loleaflet/html/loleaflet.html.m4
index 6f0322d94..82808132e 100644
--- a/loleaflet/html/loleaflet.html.m4
+++ b/loleaflet/html/loleaflet.html.m4
@@ -54,12 +54,12 @@ ifelse(MOBILEAPP,[true],
     [<style>syscmd([cat ]BUNDLE_CSS)</style>
   ])],
   [ifelse(DEBUG,[true],
-    foreachq([fileCSS],[LOLEAFLET_CSS],[<link rel="stylesheet" href="%SERVICE_ROOT%/loleaflet/%VERSION%/fileCSS" />
+    foreachq([fileCSS],[LOLEAFLET_CSS],[<link rel="stylesheet" href="<%SERVICE_ROOT%>/loleaflet/<%VERSION%>/fileCSS" />
   ]),
     [<style>syscmd([cat ]BUNDLE_CSS)</style>
   ])]dnl
 )dnl
-<!--%BRANDING_CSS%--> <!-- add your logo here -->
+<%BRANDING_CSS%> <!-- add your logo here -->
 </head>
 
   <body style="user-select: none;">
@@ -103,9 +103,9 @@ ifelse(MOBILEAPP,[true],
      </tr>
     </table>
 
-    <!--%DOCUMENT_SIGNING_DIV%-->
+    <%DOCUMENT_SIGNING_DIV%>
     <script>
-      window.documentSigningURL = '%DOCUMENT_SIGNING_URL%';
+      window.documentSigningURL = '<%DOCUMENT_SIGNING_URL%>';
     </script>
 
     <input id="insertgraphic" type="file" style="position: fixed; top: -100em">
@@ -155,14 +155,14 @@ ifelse(MOBILEAPP,[true],
       window.outOfFocusTimeoutSecs = 1000000;
       window.idleTimeoutSecs = 1000000;
       window.tileSize = 256;],
-     [window.host = '%HOST%';
-      window.serviceRoot = '%SERVICE_ROOT%';
-      window.accessToken = '%ACCESS_TOKEN%';
-      window.accessTokenTTL = '%ACCESS_TOKEN_TTL%';
-      window.accessHeader = '%ACCESS_HEADER%';
-      window.loleafletLogging = '%LOLEAFLET_LOGGING%';
-      window.outOfFocusTimeoutSecs = %OUT_OF_FOCUS_TIMEOUT_SECS%;
-      window.idleTimeoutSecs = %IDLE_TIMEOUT_SECS%;
+     [window.host = '<%HOST%>';
+      window.serviceRoot = '<%SERVICE_ROOT%>';
+      window.accessToken = '<%ACCESS_TOKEN%>';
+      window.accessTokenTTL = '<%ACCESS_TOKEN_TTL%>';
+      window.accessHeader = '<%ACCESS_HEADER%>';
+      window.loleafletLogging = '<%LOLEAFLET_LOGGING%>';
+      window.outOfFocusTimeoutSecs = <%OUT_OF_FOCUS_TIMEOUT_SECS%>;
+      window.idleTimeoutSecs = <%IDLE_TIMEOUT_SECS%>;
       window.tileSize = 256;])
 syscmd([cat ]GLOBAL_JS)dnl
 syscmd([cat ]L10N_JS)dnl
@@ -210,10 +210,10 @@ ifelse(MOBILEAPP,[true],
   [    <script src="bundle.js" defer></script>
   ]),
   ifelse(DEBUG,[true],foreachq([fileJS],[LOLEAFLET_JS],
-  [    <script src="%SERVICE_ROOT%/loleaflet/%VERSION%/fileJS" defer></script>
+  [    <script src="<%SERVICE_ROOT%>/loleaflet/<%VERSION%>/fileJS" defer></script>
   ]),
-  [    <script src="%SERVICE_ROOT%/loleaflet/%VERSION%/bundle.js" defer></script>
+  [    <script src="<%SERVICE_ROOT%>/loleaflet/<%VERSION%>/bundle.js" defer></script>
   ])
 )dnl
-    <!--%BRANDING_JS%--> <!-- logo onclick handler -->
+    <%BRANDING_JS%> <!-- logo onclick handler -->
 </body></html>
diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp
index 07ee9c4cd..7c297deb1 100644
--- a/wsd/FileServer.cpp
+++ b/wsd/FileServer.cpp
@@ -567,6 +567,26 @@ constexpr char BRANDING[] = "branding";
 constexpr char BRANDING_UNSUPPORTED[] = "branding-unsupported";
 #endif
 
+void FileServerRequestHandler::getToken(std::istream& istr, std::string& token)
+{
+    token.clear();
+    int chr = istr.get();
+    if (chr != -1)
+    {
+        if (chr == '<' && istr.peek() == '%')
+        {
+            token += "<%";
+            istr.get();
+        }
+        else if (chr == '%' && istr.peek() == '>')
+        {
+            token += "%>";
+            istr.get();
+        }
+        else token += (char) chr;
+    }
+}
+
 void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco::MemoryInputStream& message, const std::shared_ptr<StreamSocket>& socket)
 {
     const auto host = ((LOOLWSD::isSSLEnabled() || LOOLWSD::isSSLTermination()) ? "wss://" : "ws://") + (LOOLWSD::ServerName.empty() ? request.getHost() : LOOLWSD::ServerName);
@@ -611,13 +631,6 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco::
         }
     }
 
-    Poco::replaceInPlace(preprocess, std::string("%ACCESS_TOKEN%"), escapedAccessToken);
-    Poco::replaceInPlace(preprocess, std::string("%ACCESS_TOKEN_TTL%"), std::to_string(tokenTtl));
-    Poco::replaceInPlace(preprocess, std::string("%ACCESS_HEADER%"), escapedAccessHeader);
-    Poco::replaceInPlace(preprocess, std::string("%HOST%"), host);
-    Poco::replaceInPlace(preprocess, std::string("%VERSION%"), std::string(LOOLWSD_VERSION_HASH));
-    Poco::replaceInPlace(preprocess, std::string("%SERVICE_ROOT%"), LOOLWSD::ServiceRoot);
-
     static const std::string linkCSS("<link rel=\"stylesheet\" href=\"%s/loleaflet/" LOOLWSD_VERSION_HASH "/%s.css\">");
     static const std::string scriptJS("<script src=\"%s/loleaflet/" LOOLWSD_VERSION_HASH "/%s.js\"></script>");
 
@@ -635,9 +648,6 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco::
     }
 #endif
 
-    Poco::replaceInPlace(preprocess, std::string("<!--%BRANDING_CSS%-->"), brandCSS);
-    Poco::replaceInPlace(preprocess, std::string("<!--%BRANDING_JS%-->"), brandJS);
-
     // Customization related to document signing.
     std::string documentSigningDiv;
     const std::string documentSigningURL = config.getString("per_document.document_signing_url", "");
@@ -645,15 +655,125 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco::
     {
         documentSigningDiv = "<div id=\"document-signing-bar\"></div>";
     }
-    Poco::replaceInPlace(preprocess, std::string("<!--%DOCUMENT_SIGNING_DIV%-->"), documentSigningDiv);
-    Poco::replaceInPlace(preprocess, std::string("%DOCUMENT_SIGNING_URL%"), documentSigningURL);
 
-    const auto loleafletLogging = config.getString("loleaflet_logging", "false");
-    Poco::replaceInPlace(preprocess, std::string("%LOLEAFLET_LOGGING%"), loleafletLogging);
-    const std::string outOfFocusTimeoutSecs= config.getString("per_view.out_of_focus_timeout_secs", "60");
-    Poco::replaceInPlace(preprocess, std::string("%OUT_OF_FOCUS_TIMEOUT_SECS%"), outOfFocusTimeoutSecs);
-    const std::string idleTimeoutSecs= config.getString("per_view.idle_timeout_secs", "900");
-    Poco::replaceInPlace(preprocess, std::string("%IDLE_TIMEOUT_SECS%"), idleTimeoutSecs);
+    enum class ParseState
+    {
+        None,
+        Subs,
+        L10n
+    };
+
+    std::string token;
+    std::ostringstream ostr;
+    std::stringstream varSubs, varL10n;
+    std::istringstream istr(preprocess);
+    ParseState state = ParseState::None;
+
+    getToken(istr, token);
+    while (!token.empty())
+    {
+        if (token == "<%")
+        {
+            if (state == ParseState::None)
+            {
+                state = ParseState::Subs;
+                varSubs.str("");
+                varSubs.clear();
+            }
+            else ostr << token;
+        }
+        else if (token == "%>")
+        {
+            if (state == ParseState::Subs)
+            {
+                std::string var = varSubs.str();
+                if (var == "ACCESS_TOKEN")
+                {
+                    ostr << escapedAccessToken;
+                }
+                else if (var == "ACCESS_TOKEN_TTL")
+                {
+                    ostr << tokenTtl;
+                }
+                else if (var == "ACCESS_HEADER")
+                {
+                    ostr << escapedAccessHeader;
+                }
+                else if (var == "HOST")
+                {
+                    ostr << host;
+                }
+                else if (var == "VERSION")
+                {
+                    ostr << LOOLWSD_VERSION_HASH;
+                }
+                else if (var == "SERVICE_ROOT")
+                {
+                    ostr << LOOLWSD::ServiceRoot;
+                }
+                else if (var == "LOLEAFLET_LOGGING")
+                {
+                    ostr << config.getString("loleaflet_logging", "false");
+                }
+                else if (var == "OUT_OF_FOCUS_TIMEOUT_SECS")
+                {
+                    ostr << config.getString("per_view.out_of_focus_timeout_secs", "60");
+                }
+                else if (var == "IDLE_TIMEOUT_SECS")
+                {
+                    ostr << config.getString("per_view.idle_timeout_secs", "900");
+                }
+                else if (var == "DOCUMENT_SIGNING_DIV")
+                {
+                    ostr << documentSigningDiv;
+                }
+                else if (var == "DOCUMENT_SIGNING_URL")
+                {
+                    ostr << documentSigningURL;
+                }
+                else if (var == "BRANDING_CSS")
+                {
+                    ostr << brandCSS;
+                }
+                else if (var == "BRANDING_JS")
+                {
+                    ostr << brandJS;
+                }
+                else ostr << var;
+
+                state = ParseState::None;
+            }
+            else ostr << token;
+        }
+        else
+        {
+            switch (state)
+            {
+                case ParseState::None:
+                    ostr << token;
+                    break;
+
+                case ParseState::Subs:
+                    varSubs << token;
+                    break;
+
+                case ParseState::L10n:
+                    varL10n << token;
+                    break;
+            }
+        }
+
+        getToken(istr, token);
+    }
+
+    if (state == ParseState::Subs)
+    {
+        ostr << varSubs.str();
+    }
+    else if (state == ParseState::L10n)
+    {
+        ostr << varL10n.str();
+    }
 
     const std::string mimeType = "text/html";
 
@@ -664,7 +784,7 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco::
         << "User-Agent: " << WOPI_AGENT_STRING << "\r\n"
         << "Cache-Control:max-age=11059200\r\n"
         << "ETag: \"" LOOLWSD_VERSION_HASH "\"\r\n"
-        << "Content-Length: " << preprocess.size() << "\r\n"
+        << "Content-Length: " << ostr.tellp() << "\r\n"
         << "Content-Type: " << mimeType << "\r\n"
         << "X-Content-Type-Options: nosniff\r\n"
         << "X-XSS-Protection: 1; mode=block\r\n"
@@ -781,7 +901,7 @@ void FileServerRequestHandler::preprocessFile(const HTTPRequest& request, Poco::
     }
 
     oss << "\r\n"
-        << preprocess;
+        << ostr.str();
 
     socket->send(oss.str());
     LOG_DBG("Sent file: " << relPath << ": " << preprocess);
diff --git a/wsd/FileServer.hpp b/wsd/FileServer.hpp
index b415eed65..637424468 100644
--- a/wsd/FileServer.hpp
+++ b/wsd/FileServer.hpp
@@ -20,6 +20,7 @@ class FileServerRequestHandler
 {
     static std::string getRequestPathname(const Poco::Net::HTTPRequest& request);
 
+    static void getToken(std::istream&, std::string&);
     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:


More information about the Libreoffice-commits mailing list