2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-26 09:49:07 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_LOOLPROTOCOL_HPP
|
|
|
|
#define INCLUDED_LOOLPROTOCOL_HPP
|
|
|
|
|
2017-01-04 06:36:13 -06:00
|
|
|
#include <cstdint>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <cstring>
|
2015-04-27 13:09:27 -05:00
|
|
|
#include <map>
|
2016-10-18 08:24:41 -05:00
|
|
|
#include <sstream>
|
2015-04-27 13:09:27 -05:00
|
|
|
#include <string>
|
|
|
|
|
2016-10-18 08:24:41 -05:00
|
|
|
#include <Poco/Format.h>
|
2016-04-20 08:44:12 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2016-11-12 09:40:37 -06:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2016-11-10 02:47:25 -06:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
|
|
|
|
|
|
|
|
namespace LOOLProtocol
|
|
|
|
{
|
2016-01-06 11:00:44 -06:00
|
|
|
// Protocol Version Number.
|
|
|
|
// See protocol.txt.
|
|
|
|
constexpr unsigned ProtocolMajorVersionNumber = 0;
|
|
|
|
constexpr unsigned ProtocolMinorVersionNumber = 1;
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline std::string GetProtocolVersion()
|
2016-01-06 11:00:44 -06:00
|
|
|
{
|
|
|
|
return std::to_string(ProtocolMajorVersionNumber) + '.'
|
2016-10-29 20:15:00 -05:00
|
|
|
+ std::to_string(ProtocolMinorVersionNumber);
|
2016-01-06 11:00:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a string into a version tuple.
|
|
|
|
// Negative numbers for error.
|
2016-02-23 02:22:18 -06:00
|
|
|
std::tuple<int, int, std::string> ParseVersion(const std::string& version);
|
2016-01-06 11:00:44 -06:00
|
|
|
|
2016-01-12 05:07:13 -06:00
|
|
|
bool stringToInteger(const std::string& input, int& value);
|
2017-06-14 11:28:07 -05:00
|
|
|
bool stringToUInt32(const std::string& input, uint32_t& value);
|
2017-01-04 06:36:13 -06:00
|
|
|
bool stringToUInt64(const std::string& input, uint64_t& value);
|
|
|
|
|
2016-10-08 11:40:21 -05:00
|
|
|
inline
|
|
|
|
bool parseNameValuePair(const std::string& token, std::string& name, std::string& value, const char delim = '=')
|
|
|
|
{
|
|
|
|
const auto mid = token.find_first_of(delim);
|
|
|
|
if (mid != std::string::npos)
|
|
|
|
{
|
|
|
|
name = token.substr(0, mid);
|
|
|
|
value = token.substr(mid + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2017-01-09 02:55:28 -06:00
|
|
|
bool parseNameIntegerPair(const std::string& token, std::string& name, int& value)
|
2016-10-08 11:40:21 -05:00
|
|
|
{
|
|
|
|
std::string strValue;
|
2017-01-09 02:55:28 -06:00
|
|
|
return parseNameValuePair(token, name, strValue, '=') && stringToInteger(strValue, value);
|
2016-10-08 11:40:21 -05:00
|
|
|
}
|
2016-03-27 13:30:03 -05:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenInteger(const std::string& token, const std::string& name, int& value);
|
2017-06-14 11:28:07 -05:00
|
|
|
bool getTokenUInt32(const std::string& token, const std::string& name, uint32_t& value);
|
2017-01-04 06:36:13 -06:00
|
|
|
bool getTokenUInt64(const std::string& token, const std::string& name, uint64_t& value);
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenString(const std::string& token, const std::string& name, std::string& value);
|
2015-04-27 13:09:27 -05:00
|
|
|
bool getTokenKeyword(const std::string& token, const std::string& name, const std::map<std::string, int>& map, int& value);
|
2015-03-26 09:49:07 -05:00
|
|
|
|
2016-04-20 08:44:12 -05:00
|
|
|
bool getTokenInteger(const Poco::StringTokenizer& tokens, const std::string& name, int& value);
|
|
|
|
bool getTokenString(const Poco::StringTokenizer& tokens, const std::string& name, std::string& value);
|
|
|
|
bool getTokenKeyword(const Poco::StringTokenizer& tokens, const std::string& name, const std::map<std::string, int>& map, int& value);
|
2017-01-30 19:43:27 -06:00
|
|
|
|
|
|
|
bool getTokenInteger(const std::vector<std::string>& tokens, const std::string& name, int& value);
|
|
|
|
|
2017-01-19 19:44:39 -06:00
|
|
|
inline bool getTokenString(const std::vector<std::string>& tokens,
|
|
|
|
const std::string& name,
|
|
|
|
std::string& value)
|
|
|
|
{
|
|
|
|
for (const auto& token : tokens)
|
|
|
|
{
|
|
|
|
if (getTokenString(token, name, value))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-20 08:44:12 -05:00
|
|
|
|
2016-09-26 11:31:19 -05:00
|
|
|
bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value);
|
|
|
|
bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value);
|
|
|
|
|
2016-12-17 20:54:34 -06:00
|
|
|
/// Tokenize space-delimited values until we hit new-line or the end.
|
|
|
|
inline
|
2017-05-17 03:48:51 -05:00
|
|
|
std::vector<std::string> tokenize(const char* data, const size_t size, const char delimeter = ' ')
|
2016-12-17 20:54:34 -06:00
|
|
|
{
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
if (size == 0 || data == nullptr)
|
|
|
|
{
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* start = data;
|
|
|
|
const char* end = data;
|
|
|
|
for (size_t i = 0; i < size && data[i] != '\n'; ++i, ++end)
|
|
|
|
{
|
2017-05-17 03:48:51 -05:00
|
|
|
if (data[i] == delimeter)
|
2016-12-17 20:54:34 -06:00
|
|
|
{
|
2017-05-17 03:48:51 -05:00
|
|
|
if (start != end && *start != delimeter)
|
2016-12-17 20:54:34 -06:00
|
|
|
{
|
|
|
|
tokens.emplace_back(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = end;
|
|
|
|
}
|
2017-05-17 03:48:51 -05:00
|
|
|
else if (*start == delimeter)
|
2016-12-17 20:54:34 -06:00
|
|
|
{
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 03:48:51 -05:00
|
|
|
if (start != end && *start != delimeter && *start != '\n')
|
2016-12-17 20:54:34 -06:00
|
|
|
{
|
|
|
|
tokens.emplace_back(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2017-05-17 03:48:51 -05:00
|
|
|
std::vector<std::string> tokenize(const std::string& s, const char delimeter = ' ')
|
2016-12-17 20:54:34 -06:00
|
|
|
{
|
2017-05-17 03:48:51 -05:00
|
|
|
return tokenize(s.data(), s.size(), delimeter);
|
2016-12-17 20:54:34 -06:00
|
|
|
}
|
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
inline bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value)
|
|
|
|
{
|
|
|
|
return getTokenInteger(tokenize(message), name, value);
|
|
|
|
}
|
|
|
|
|
2016-12-18 12:09:13 -06:00
|
|
|
inline size_t getDelimiterPosition(const char* message, const int length, const char delim)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
2016-12-18 12:09:13 -06:00
|
|
|
if (message && length > 0)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
2016-12-18 12:09:13 -06:00
|
|
|
const char *founddelim = static_cast<const char *>(std::memchr(message, delim, length));
|
|
|
|
const auto size = (founddelim == nullptr ? length : founddelim - message);
|
|
|
|
return size;
|
2016-03-27 13:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-12-18 12:09:13 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
|
|
|
|
{
|
|
|
|
const auto size = getDelimiterPosition(message, length, delim);
|
2016-03-27 13:30:03 -05:00
|
|
|
return std::string(message, size);
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:48:24 -05:00
|
|
|
/// Split a string in two at the delimeter, removing it.
|
|
|
|
inline
|
|
|
|
std::pair<std::string, std::string> split(const char* s, const int length, const char delimeter = ' ')
|
|
|
|
{
|
|
|
|
const auto size = getDelimiterPosition(s, length, delimeter);
|
|
|
|
return std::make_pair(std::string(s, size), std::string(s+size+1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split a string in two at the delimeter, removing it.
|
|
|
|
inline
|
|
|
|
std::pair<std::string, std::string> split(const std::string& s, const char delimeter = ' ')
|
|
|
|
{
|
|
|
|
return split(s.c_str(), s.size(), delimeter);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:11:49 -05:00
|
|
|
/// Returns the first token of a message.
|
|
|
|
inline
|
2017-01-19 22:25:51 -06:00
|
|
|
std::string getFirstToken(const char *message, const int length, const char delim = ' ')
|
2016-04-11 02:11:49 -05:00
|
|
|
{
|
2016-10-08 12:12:16 -05:00
|
|
|
return getDelimitedInitialSubstring(message, length, delim);
|
2016-04-11 02:11:49 -05:00
|
|
|
}
|
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
2016-10-08 12:12:16 -05:00
|
|
|
std::string getFirstToken(const T& message, const char delim = ' ')
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
2016-10-08 12:12:16 -05:00
|
|
|
return getFirstToken(message.data(), message.size(), delim);
|
2016-03-27 13:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-10-21 11:59:03 -05:00
|
|
|
inline
|
|
|
|
bool matchPrefix(const std::string& prefix, const std::string& message)
|
|
|
|
{
|
|
|
|
return (message.size() >= prefix.size() &&
|
|
|
|
message.compare(0, prefix.size(), prefix) == 0);
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
inline
|
|
|
|
bool matchPrefix(const std::string& prefix, const std::vector<char>& message)
|
|
|
|
{
|
|
|
|
return (message.size() >= prefix.size() &&
|
|
|
|
prefix.compare(0, prefix.size(), message.data(), prefix.size()) == 0);
|
|
|
|
}
|
|
|
|
|
2016-10-21 11:59:03 -05:00
|
|
|
inline
|
|
|
|
bool matchPrefix(const std::string& prefix, const std::string& message, const bool ignoreWhitespace)
|
|
|
|
{
|
|
|
|
if (ignoreWhitespace)
|
|
|
|
{
|
|
|
|
const auto posPre = prefix.find_first_not_of(' ');
|
|
|
|
const auto posMsg = message.find_first_not_of(' ');
|
|
|
|
|
|
|
|
return matchPrefix(posPre == std::string::npos ? prefix : prefix.substr(posPre),
|
|
|
|
posMsg == std::string::npos ? message : message.substr(posMsg));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return matchPrefix(prefix, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 22:16:09 -05:00
|
|
|
/// Returns true if the token is a user-interaction token.
|
|
|
|
/// Currently this excludes commands sent automatically.
|
|
|
|
/// Notice that this doesn't guarantee editing activity,
|
|
|
|
/// rather just user interaction with the UI.
|
2016-04-06 03:36:33 -05:00
|
|
|
inline
|
|
|
|
bool tokenIndicatesUserInteraction(const std::string& token)
|
|
|
|
{
|
2016-09-26 02:12:48 -05:00
|
|
|
// Exclude tokens that include these keywords, such as canceltiles statusindicator.
|
|
|
|
|
|
|
|
// FIXME: This is wrong. That the token happens to contain (or not) a certain substring is
|
|
|
|
// no guarantee that it "indicates user interaction". It might be like that at the moment,
|
|
|
|
// but that is coincidental. We should check what the actual whole token is, at least, not
|
|
|
|
// look for a substring.
|
|
|
|
|
2016-04-09 22:16:09 -05:00
|
|
|
return (token.find("tile") == std::string::npos &&
|
|
|
|
token.find("status") == std::string::npos &&
|
2016-12-14 09:23:42 -06:00
|
|
|
token.find("state") == std::string::npos &&
|
|
|
|
token != "userinactive");
|
2016-04-06 03:36:33 -05:00
|
|
|
}
|
|
|
|
|
2016-03-27 13:30:03 -05:00
|
|
|
/// Returns the first line of a message.
|
|
|
|
inline
|
|
|
|
std::string getFirstLine(const char *message, const int length)
|
|
|
|
{
|
2016-04-11 02:11:49 -05:00
|
|
|
return getDelimitedInitialSubstring(message, length, '\n');
|
2016-03-27 13:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-10-08 12:12:16 -05:00
|
|
|
/// Returns the first line of any data which payload char*.
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getFirstLine(const T& message)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
|
|
|
return getFirstLine(message.data(), message.size());
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:51:29 -05:00
|
|
|
/// Returns an abbereviation of the message (the first line, indicating truncation). We assume
|
|
|
|
/// that it adhers to the LOOL protocol, i.e. that there is always a first (or only) line that
|
|
|
|
/// is in printable UTF-8. I.e. no encoding of binary bytes is done. The format of the result is
|
|
|
|
/// not guaranteed to be stable. It is to be used for logging purposes only, not for decoding
|
|
|
|
/// protocol frames.
|
2016-10-08 11:40:21 -05:00
|
|
|
inline
|
|
|
|
std::string getAbbreviatedMessage(const char *message, const int length)
|
|
|
|
{
|
|
|
|
if (message == nullptr || length <= 0)
|
|
|
|
{
|
2016-12-18 12:09:13 -06:00
|
|
|
return std::string();
|
2016-10-08 11:40:21 -05:00
|
|
|
}
|
|
|
|
|
2017-01-11 08:13:46 -06:00
|
|
|
const auto firstLine = getFirstLine(message, std::min(length, 500));
|
2016-10-08 11:40:21 -05:00
|
|
|
|
|
|
|
// If first line is less than the length (minus newline), add ellipsis.
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
|
|
|
return firstLine + "...";
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstLine;
|
|
|
|
}
|
2016-04-10 12:13:05 -05:00
|
|
|
|
2016-12-18 12:09:13 -06:00
|
|
|
inline std::string getAbbreviatedMessage(const std::string& message)
|
|
|
|
{
|
2017-01-11 08:13:46 -06:00
|
|
|
const auto pos = getDelimiterPosition(message.data(), std::min(message.size(), 500UL), '\n');
|
2016-12-18 12:09:13 -06:00
|
|
|
|
|
|
|
// If first line is less than the length (minus newline), add ellipsis.
|
|
|
|
if (pos < static_cast<std::string::size_type>(message.size()) - 1)
|
|
|
|
{
|
|
|
|
return message.substr(0, pos) + "...";
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getAbbreviatedMessage(const T& message)
|
2016-05-01 19:31:40 -05:00
|
|
|
{
|
|
|
|
return getAbbreviatedMessage(message.data(), message.size());
|
|
|
|
}
|
2016-10-18 08:24:41 -05:00
|
|
|
|
2016-11-12 09:40:37 -06:00
|
|
|
// 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
|
2016-10-18 08:24:41 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2016-10-21 10:56:07 -05:00
|
|
|
result << " " << length << " bytes";
|
2016-10-18 08:24:41 -05:00
|
|
|
|
|
|
|
if (length > 0 &&
|
|
|
|
((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_TEXT ||
|
2016-12-02 06:32:27 -06:00
|
|
|
(flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_BINARY ||
|
|
|
|
(flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_PING ||
|
|
|
|
(flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_PONG))
|
2016-10-18 08:24:41 -05:00
|
|
|
result << ": '" << getAbbreviatedMessage(message, length) << "'";
|
|
|
|
return result.str();
|
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|