[Libreoffice-commits] online.git: Branch 'private/mmeeks/admin-nonblock' - net/Socket.cpp net/Socket.hpp wsd/FileServer.cpp

Michael Meeks michael.meeks at collabora.com
Wed Mar 15 18:23:39 UTC 2017


 net/Socket.cpp     |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/Socket.hpp     |   51 +++--------------------------------------------
 wsd/FileServer.cpp |    2 -
 3 files changed, 62 insertions(+), 48 deletions(-)

New commits:
commit f392d9e6f0103360dbcaaf99fc4768dfd9624eba
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Wed Mar 15 18:21:59 2017 +0000

    Move http serving into socket impl.
    
    Avoid caching headers with parameter, and add Date: parameter.

diff --git a/net/Socket.cpp b/net/Socket.cpp
index 5f13202..7af8760 100644
--- a/net/Socket.cpp
+++ b/net/Socket.cpp
@@ -12,6 +12,10 @@
 #include <stdio.h>
 #include <ctype.h>
 
+#include <Poco/DateTime.h>
+#include <Poco/DateTimeFormat.h>
+#include <Poco/DateTimeFormatter.h>
+
 #include "SigUtil.hpp"
 #include "Socket.hpp"
 #include "ServerSocket.hpp"
@@ -147,4 +151,57 @@ void SocketPoll::dumpState(std::ostream& os)
         i->dumpState(os);
 }
 
+namespace HttpHelper
+{
+    void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path,
+                  Poco::Net::HTTPResponse& response, bool noCache)
+    {
+        struct stat st;
+        if (stat(path.c_str(), &st) != 0)
+        {
+            LOG_WRN("#" << socket->getFD() << ": Failed to stat [" << path << "]. File will not be sent.");
+            throw Poco::FileNotFoundException("Failed to stat [" + path + "]. File will not be sent.");
+            return;
+        }
+
+        int bufferSize = std::min(st.st_size, (off_t)Socket::MaximumSendBufferSize);
+        if (st.st_size >= socket->getSendBufferSize())
+        {
+            socket->setSocketBufferSize(bufferSize);
+            bufferSize = socket->getSendBufferSize();
+        }
+
+        response.setContentLength(st.st_size);
+        response.set("User-Agent", HTTP_AGENT_STRING);
+        response.set("Date", Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT));
+        if (!noCache)
+        {
+            // 60 * 60 * 24 * 128 (days) = 11059200
+            response.set("Cache-Control", "max-age=11059200");
+            response.set("ETag", "\"" LOOLWSD_VERSION_HASH "\"");
+        }
+
+        std::ostringstream oss;
+        response.write(oss);
+        const std::string header = oss.str();
+        LOG_TRC("#" << socket->getFD() << ": Sending file [" << path << "]: " << header);
+        socket->send(header);
+
+        std::ifstream file(path, std::ios::binary);
+        bool flush = true;
+        do
+        {
+            char buf[bufferSize];
+            file.read(buf, sizeof(buf));
+            const int size = file.gcount();
+            if (size > 0)
+                socket->send(buf, size, flush);
+            else
+                break;
+            flush = false;
+        }
+        while (file);
+    }
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/net/Socket.hpp b/net/Socket.hpp
index 9460c45..708ae39 100644
--- a/net/Socket.hpp
+++ b/net/Socket.hpp
@@ -820,58 +820,15 @@ protected:
 
 namespace HttpHelper
 {
-    inline void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path,
-                         Poco::Net::HTTPResponse& response)
-    {
-        struct stat st;
-        if (stat(path.c_str(), &st) != 0)
-        {
-            LOG_WRN("#" << socket->getFD() << ": Failed to stat [" << path << "]. File will not be sent.");
-            throw Poco::FileNotFoundException("Failed to stat [" + path + "]. File will not be sent.");
-            return;
-        }
-
-        int bufferSize = std::min(st.st_size, (off_t)Socket::MaximumSendBufferSize);
-        if (st.st_size >= socket->getSendBufferSize())
-        {
-            socket->setSocketBufferSize(bufferSize);
-            bufferSize = socket->getSendBufferSize();
-        }
-
-        response.setContentLength(st.st_size);
-        response.set("User-Agent", HTTP_AGENT_STRING);
-        // 60 * 60 * 24 * 128 (days) = 11059200
-        response.set("Cache-Control", "max-age=11059200");
-        response.set("ETag", "\"" LOOLWSD_VERSION_HASH "\"");
-
-        std::ostringstream oss;
-        response.write(oss);
-        const std::string header = oss.str();
-        LOG_TRC("#" << socket->getFD() << ": Sending file [" << path << "]: " << header);
-        socket->send(header);
-
-        std::ifstream file(path, std::ios::binary);
-        bool flush = true;
-        do
-        {
-            char buf[bufferSize];
-            file.read(buf, sizeof(buf));
-            const int size = file.gcount();
-            if (size > 0)
-                socket->send(buf, size, flush);
-            else
-                break;
-            flush = false;
-        }
-        while (file);
-    }
+    void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path,
+                  Poco::Net::HTTPResponse& response, bool noCache = false);
 
     inline void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path,
-                         const std::string& mediaType)
+                         const std::string& mediaType, bool noCache = false)
     {
         Poco::Net::HTTPResponse response;
         response.setContentType(mediaType);
-        sendFile(socket, path, response);
+        sendFile(socket, path, response, noCache);
     }
 };
 
diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp
index 2189205..bba77d5 100644
--- a/wsd/FileServer.cpp
+++ b/wsd/FileServer.cpp
@@ -197,7 +197,7 @@ void FileServerRequestHandler::handleRequest(const HTTPRequest& request, Poco::M
             }
 
             response.setContentType(mimeType);
-            HttpHelper::sendFile(socket, filepath, response);
+            HttpHelper::sendFile(socket, filepath, response, noCache);
         }
     }
     catch (const Poco::Net::NotAuthenticatedException& exc)


More information about the Libreoffice-commits mailing list