[Libreoffice-commits] online.git: 2 commits - loolwsd/Connect.cpp loolwsd/LOOLProtocol.hpp loolwsd/test

Tor Lillqvist tml at collabora.com
Tue Oct 18 14:04:27 UTC 2016


 loolwsd/Connect.cpp            |    2 +-
 loolwsd/LOOLProtocol.hpp       |   33 +++++++++++++++++++++++++++++++++
 loolwsd/test/helpers.hpp       |   12 +++---------
 loolwsd/test/httpcrashtest.cpp |    2 +-
 4 files changed, 38 insertions(+), 11 deletions(-)

New commits:
commit ece974092616445983da34283228c80a81642a19
Author: Tor Lillqvist <tml at collabora.com>
Date:   Tue Oct 18 16:54:31 2016 +0300

    Use LOOLProtocol::getAbbreviatedFrameDump()

diff --git a/loolwsd/Connect.cpp b/loolwsd/Connect.cpp
index 237e7c2..3c14b22 100644
--- a/loolwsd/Connect.cpp
+++ b/loolwsd/Connect.cpp
@@ -95,7 +95,7 @@ public:
                 {
                     {
                         std::unique_lock<std::mutex> lock(coutMutex);
-                        std::cout << "Got " << n << " bytes: " << getAbbreviatedMessage(buffer, n) << std::endl;
+                        std::cout << "Got " << getAbbreviatedFrameDump(buffer, n, flags) << std::endl;
                     }
 
                     std::string firstLine = getFirstLine(buffer, n);
diff --git a/loolwsd/test/helpers.hpp b/loolwsd/test/helpers.hpp
index ab2b1ba..cdc7515 100644
--- a/loolwsd/test/helpers.hpp
+++ b/loolwsd/test/helpers.hpp
@@ -185,6 +185,7 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
                 response.resize(READ_BUFFER_SIZE);
                 int bytes = ws.receiveFrame(response.data(), response.size(), flags);
                 response.resize(bytes >= 0 ? bytes : 0);
+                std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(response.data(), bytes, flags) << std::endl;
                 auto message = LOOLProtocol::getAbbreviatedMessage(response);
                 if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
                 {
@@ -206,12 +207,10 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
 
                     if (message.find(prefix) == 0)
                     {
-                        std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
                         return response;
                     }
                     else if (message.find("nextmessage") == 0)
                     {
-                        std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
                         Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
                         int size = 0;
                         if (tokens.count() == 2 &&
@@ -220,10 +219,10 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
                             response.resize(size);
                             bytes = ws.receiveFrame(response.data(), response.size(), flags);
                             response.resize(bytes >= 0 ? bytes : 0);
+                            std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(response.data(), bytes, flags) << std::endl;
                             message = LOOLProtocol::getAbbreviatedMessage(response);
                             if (bytes > 0 && message.find(prefix) == 0)
                             {
-                                std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
                                 return response;
                             }
                         }
@@ -232,7 +231,6 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
                 else
                 {
                     response.resize(0);
-                    std::cerr << name << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
                 }
 
                 if (bytes <= 0)
@@ -405,18 +403,14 @@ void SocketProcessor(const std::string& name,
         }
 
         n = socket->receiveFrame(buffer, sizeof(buffer), flags);
+        std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, n, flags) << std::endl;
         if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
         {
-            std::cerr << name << "Got " << n << " bytes: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl;
             if (!handler(std::string(buffer, n)))
             {
                 break;
             }
         }
-        else
-        {
-            std::cerr << name << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
-        }
     }
     while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
 }
diff --git a/loolwsd/test/httpcrashtest.cpp b/loolwsd/test/httpcrashtest.cpp
index 53e6f0c..ab8f018 100644
--- a/loolwsd/test/httpcrashtest.cpp
+++ b/loolwsd/test/httpcrashtest.cpp
@@ -188,7 +188,7 @@ void HTTPCrashTest::testCrashKit()
         do
         {
             bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
-            std::cerr << testname << "Got " << bytes << " bytes: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << std::endl;
+            std::cerr << testname << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, bytes, flags) << std::endl;
         }
         while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
 
commit 24442a080cd3e27db513a766d913c3a63f682613
Author: Tor Lillqvist <tml at collabora.com>
Date:   Tue Oct 18 16:24:41 2016 +0300

    Introduce LOOLProtocol::getAbbreviatedFrameDump()
    
    Produces a human-readable logging string describing a LOOL protocol
    frame. To be used for re-factoring snippets of similar code that
    sometimes print also the flags, sometimes pointlessly print the
    (binary) contents of a CLOSE frame (as if it was text), etc.

diff --git a/loolwsd/LOOLProtocol.hpp b/loolwsd/LOOLProtocol.hpp
index 3857a4e..41c5b3c 100644
--- a/loolwsd/LOOLProtocol.hpp
+++ b/loolwsd/LOOLProtocol.hpp
@@ -12,8 +12,11 @@
 
 #include <cstring>
 #include <map>
+#include <sstream>
 #include <string>
 
+#include <Poco/Format.h>
+#include <Poco/Net/WebSocket.h>
 #include <Poco/StringTokenizer.h>
 
 #define LOK_USE_UNSTABLE_API
@@ -168,6 +171,36 @@ namespace LOOLProtocol
     {
         return getAbbreviatedMessage(message.data(), message.size());
     }
+
+    // Return a string dump of a WebSocket frame: Its opcode, length, first line (if present),
+    // flags.  For human-readable logging purposes. Format not guaranteed to be stable. Not to be
+    // inspected programmatically.
+    inline
+    std::string getAbbreviatedFrameDump(const char *message, const int length, const int flags)
+    {
+        std::ostringstream result;
+        switch (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK)
+        {
+#define CASE(x) case Poco::Net::WebSocket::FRAME_OP_##x: result << #x; break
+        CASE(CONT);
+        CASE(TEXT);
+        CASE(BINARY);
+        CASE(CLOSE);
+        CASE(PING);
+        CASE(PONG);
+#undef CASE
+        default:
+            result << Poco::format("%#x", flags);
+            break;
+        }
+        result << " frame: " << length << " bytes";
+
+        if (length > 0 &&
+            ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_TEXT ||
+             (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_BINARY))
+            result << ": '" << getAbbreviatedMessage(message, length) << "'";
+        return result.str();
+    }
 };
 
 #endif


More information about the Libreoffice-commits mailing list