[Libreoffice-commits] online.git: Branch 'distro/collabora/collabora-online-2-1' - common/Unit.hpp kit/Kit.cpp net/Socket.hpp net/WebSocketHandler.hpp test/UnitWOPISaveAs.cpp wsd/LOOLWSD.cpp

Jan Holesovsky kendy at collabora.com
Thu Oct 26 10:27:28 UTC 2017


 common/Unit.hpp          |   10 +++++++++-
 kit/Kit.cpp              |    2 +-
 net/Socket.hpp           |   15 +++++++++++++++
 net/WebSocketHandler.hpp |   25 +++++++------------------
 test/UnitWOPISaveAs.cpp  |   13 ++++++++++++-
 wsd/LOOLWSD.cpp          |    4 ++--
 6 files changed, 46 insertions(+), 23 deletions(-)

New commits:
commit 01f9526b103c70719887180db23adaf7506182fa
Author: Jan Holesovsky <kendy at collabora.com>
Date:   Thu Oct 26 10:38:57 2017 +0200

    tdf#99744 SaveAs: Extend test to check that the Save As result was sent.
    
    Change-Id: I3788b87d2599c01000af97f496ee2b840c0cae3e
    Reviewed-on: https://gerrit.libreoffice.org/43874
    Reviewed-by: Tor Lillqvist <tml at collabora.com>
    Tested-by: Tor Lillqvist <tml at collabora.com>

diff --git a/common/Unit.hpp b/common/Unit.hpp
index 85139f8c..9dea4003 100644
--- a/common/Unit.hpp
+++ b/common/Unit.hpp
@@ -15,7 +15,7 @@
 #include <string>
 
 #include <LOOLWebSocket.hpp>
-#include "net/WebSocketHandler.hpp"
+#include "net/Socket.hpp"
 
 class UnitBase;
 class UnitWSD;
@@ -24,6 +24,8 @@ class UnitTimeout;
 class UnitHTTPServerRequest;
 class UnitHTTPServerResponse;
 
+class WebSocketHandler;
+
 // Forward declaration to avoid pulling the world here.
 namespace Poco
 {
@@ -95,6 +97,12 @@ public:
         return false;
     }
 
+    /// Message that is about to be sent via the websocket.
+    virtual bool filterSendMessage(const char* /* data */, const size_t /* len */, const WSOpCode /* code */, const bool /* flush */, int& /*unitReturn*/)
+    {
+        return false;
+    }
+
     /// Hook the disk space check
     virtual bool filterCheckDiskSpace(const std::string & /* path */,
                                       bool & /* newResult */)
diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index d4037818..894d7f6d 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -1453,7 +1453,7 @@ private:
                     vect.assign(data, data + size);
 
                     // TODO loolnb - this is probably wrong...
-                    session->handleMessage(/* fin = */ false, WebSocketHandler::WSOpCode::Binary, vect);
+                    session->handleMessage(/* fin = */ false, WSOpCode::Binary, vect);
                     return true;
                 }
             }
diff --git a/net/Socket.hpp b/net/Socket.hpp
index 8699db88..394a841e 100644
--- a/net/Socket.hpp
+++ b/net/Socket.hpp
@@ -961,6 +961,21 @@ protected:
     friend class SimpleResponseClient;
 };
 
+enum class WSOpCode : unsigned char {
+    Continuation = 0x0,
+    Text         = 0x1,
+    Binary       = 0x2,
+    Reserved1    = 0x3,
+    Reserved2    = 0x4,
+    Reserved3    = 0x5,
+    Reserved4    = 0x6,
+    Reserved5    = 0x7,
+    Close        = 0x8,
+    Ping         = 0x9,
+    Pong         = 0xa
+    // ... reserved
+};
+
 namespace HttpHelper
 {
     void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path, const std::string& mediaType,
diff --git a/net/WebSocketHandler.hpp b/net/WebSocketHandler.hpp
index 69c8ed3b..149fd808 100644
--- a/net/WebSocketHandler.hpp
+++ b/net/WebSocketHandler.hpp
@@ -13,6 +13,7 @@
 #include "Common.hpp"
 #include "Log.hpp"
 #include "Socket.hpp"
+#include "Unit.hpp"
 
 #include <Poco/Net/HTTPRequest.h>
 #include <Poco/Net/WebSocket.h>
@@ -68,21 +69,6 @@ public:
         LOG_TRC("#" << socket->getFD() << " Connected to WS Handler 0x" << std::hex << this << std::dec);
     }
 
-    enum WSOpCode {
-        Continuation, // 0x0
-        Text,         // 0x1
-        Binary,       // 0x2
-        Reserved1,    // 0x3
-        Reserved2,    // 0x4
-        Reserved3,    // 0x5
-        Reserved4,    // 0x6
-        Reserved5,    // 0x7
-        Close,        // 0x8
-        Ping,         // 0x9
-        Pong          // 0xa
-        // ... reserved
-    };
-
     /// Status codes sent to peer on shutdown.
     enum class StatusCodes : unsigned short
     {
@@ -200,7 +186,7 @@ public:
         socket->_inBuffer.erase(socket->_inBuffer.begin(), socket->_inBuffer.begin() + headerLen + payloadLen);
 
         // FIXME: fin, aggregating payloads into _wsPayload etc.
-        LOG_TRC("#" << socket->getFD() << ": Incoming WebSocket message code " << code <<
+        LOG_TRC("#" << socket->getFD() << ": Incoming WebSocket message code " << static_cast<unsigned>(code) <<
                 ", fin? " << fin << ", mask? " << hasMask << ", payload length: " << _wsPayload.size() <<
                 ", residual socket data: " << socket->_inBuffer.size() << " bytes.");
 
@@ -320,11 +306,14 @@ public:
     /// 0 for closed/invalid socket, and -1 for other errors.
     int sendMessage(const char* data, const size_t len, const WSOpCode code, const bool flush = true) const
     {
+        int unitReturn = -1;
+        if (UnitWSD::get().filterSendMessage(data, len, code, flush, unitReturn))
+            return unitReturn;
+
         //TODO: Support fragmented messages.
-        static const unsigned char Fin = static_cast<unsigned char>(WSFrameMask::Fin);
 
         auto socket = _socket.lock();
-        return sendFrame(socket, data, len, static_cast<unsigned char>(Fin | code), flush);
+        return sendFrame(socket, data, len, static_cast<unsigned char>(WSFrameMask::Fin) | static_cast<unsigned char>(code), flush);
     }
 
 protected:
diff --git a/test/UnitWOPISaveAs.cpp b/test/UnitWOPISaveAs.cpp
index cb9ff9c3..a0c5c3df 100644
--- a/test/UnitWOPISaveAs.cpp
+++ b/test/UnitWOPISaveAs.cpp
@@ -35,8 +35,19 @@ public:
     {
         // spec says UTF-7...
         CPPUNIT_ASSERT_EQUAL(std::string("/jan/hole+AWE-ovsk+AP0-/hello world.txt"), request.get("X-WOPI-SuggestedTarget"));
+    }
+
+    bool filterSendMessage(const char* data, const size_t len, const WSOpCode /* code */, const bool /* flush */, int& /*unitReturn*/) override
+    {
+        std::string message(data, len);
+        if (message == "saveas: url=https://127.0.0.1:9980/something%20wopi/files/1?access_token=anything filename=hello%20world.txt")
+        {
+            // successfully exit the test if we also got the outgoing message
+            // notifying about saving the file
+            exitTest(TestResult::Ok);
+        }
 
-        exitTest(TestResult::Ok);
+        return false;
     }
 
     void invokeTest() override
diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index e597a790..b3aaab7e 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -1997,7 +1997,7 @@ private:
                             URI::encode(docBroker->getPublicUri().getPath(), "", encodedFrom);
                             const std::string load = "load url=" + encodedFrom;
                             std::vector<char> loadRequest(load.begin(), load.end());
-                            clientSession->handleMessage(true, WebSocketHandler::WSOpCode::Text, loadRequest);
+                            clientSession->handleMessage(true, WSOpCode::Text, loadRequest);
 
                             // FIXME: Check for security violations.
                             Path toPath(docBroker->getPublicUri().getPath());
@@ -2009,7 +2009,7 @@ private:
                             // Convert it to the requested format.
                             const auto saveas = "saveas url=" + encodedTo + " format=" + format + " options=";
                             std::vector<char> saveasRequest(saveas.begin(), saveas.end());
-                            clientSession->handleMessage(true, WebSocketHandler::WSOpCode::Text, saveasRequest);
+                            clientSession->handleMessage(true, WSOpCode::Text, saveasRequest);
                         });
                         });
 


More information about the Libreoffice-commits mailing list