2016-05-16 06:37:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
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 "PrisonerSession.hpp"
|
2016-05-16 06:37:02 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
|
|
|
|
|
|
|
#include "Common.hpp"
|
2016-12-21 14:37:22 -06:00
|
|
|
#include "DocumentBroker.hpp"
|
2016-11-24 08:56:06 -06:00
|
|
|
#include "Protocol.hpp"
|
|
|
|
#include "Session.hpp"
|
2016-05-16 06:37:02 -05:00
|
|
|
#include "LOOLWSD.hpp"
|
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 "Log.hpp"
|
2016-05-16 18:05:22 -05:00
|
|
|
#include "ClientSession.hpp"
|
2016-05-16 06:37:02 -05:00
|
|
|
#include "Rectangle.hpp"
|
2016-12-11 17:56:55 -06:00
|
|
|
#include "SenderQueue.hpp"
|
2016-05-16 06:37:02 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
|
|
|
#include "IoUtil.hpp"
|
|
|
|
#include "Util.hpp"
|
2016-05-16 18:05:22 -05:00
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
|
|
|
using Poco::Path;
|
|
|
|
using Poco::StringTokenizer;
|
|
|
|
|
2016-11-13 11:26:00 -06:00
|
|
|
PrisonerSession::PrisonerSession(std::shared_ptr<ClientSession> clientSession,
|
2016-05-16 20:03:45 -05:00
|
|
|
std::shared_ptr<DocumentBroker> docBroker) :
|
2016-12-12 19:18:25 -06:00
|
|
|
Session("ToPrisoner-" + clientSession->getId(), clientSession->getId(), nullptr),
|
2016-07-26 02:29:39 -05:00
|
|
|
_docBroker(std::move(docBroker)),
|
2016-11-13 11:26:00 -06:00
|
|
|
_peer(clientSession),
|
2016-05-16 20:03:45 -05:00
|
|
|
_curPart(0)
|
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_INF("PrisonerSession ctor [" << getName() << "].");
|
2016-05-16 20:03:45 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
PrisonerSession::~PrisonerSession()
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_INF("~PrisonerSession dtor [" << getName() << "].");
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
bool PrisonerSession::_handleInput(const char *buffer, int length)
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_TRC(getName() + ": handling [" << getAbbreviatedMessage(buffer, length) << "].");
|
2016-05-16 06:37:02 -05:00
|
|
|
const std::string firstLine = getFirstLine(buffer, length);
|
|
|
|
StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
2016-08-03 07:15:04 -05:00
|
|
|
LOOLWSD::dumpOutgoingTrace(_docBroker->getJailId(), getId(), firstLine);
|
2016-07-31 06:54:47 -05:00
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
auto peer = _peer.lock();
|
2016-05-20 19:15:50 -05:00
|
|
|
if (!peer)
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-20 19:15:50 -05:00
|
|
|
throw Poco::ProtocolException("The session has not been assigned a peer.");
|
|
|
|
}
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2016-05-20 19:15:50 -05:00
|
|
|
if (tokens[0] == "unocommandresult:")
|
|
|
|
{
|
|
|
|
const std::string stringMsg(buffer, length);
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_INF(getName() << ": Command: " << stringMsg);
|
2016-05-20 19:15:50 -05:00
|
|
|
const auto index = stringMsg.find_first_of('{');
|
|
|
|
if (index != std::string::npos)
|
2016-05-16 18:05:22 -05:00
|
|
|
{
|
2016-05-20 19:15:50 -05:00
|
|
|
const std::string stringJSON = stringMsg.substr(index);
|
|
|
|
Poco::JSON::Parser parser;
|
2016-07-14 04:49:21 -05:00
|
|
|
const auto parsedJSON = parser.parse(stringJSON);
|
|
|
|
const auto& object = parsedJSON.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
if (object->get("commandName").toString() == ".uno:Save")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-07-14 04:49:21 -05:00
|
|
|
bool success = object->get("success").toString() == "true";
|
|
|
|
std::string result;
|
|
|
|
if (object->has("result"))
|
|
|
|
{
|
|
|
|
const auto parsedResultJSON = object->get("result");
|
|
|
|
const auto& resultObj = parsedResultJSON.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
if (resultObj->get("type").toString() == "string")
|
|
|
|
result = resultObj->get("value").toString();
|
|
|
|
}
|
|
|
|
|
2017-01-01 11:06:05 -06:00
|
|
|
// Save to Storage and log result.
|
|
|
|
_docBroker->save(getId(), success, result);
|
2016-05-20 19:15:50 -05:00
|
|
|
return true;
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
}
|
2016-11-05 16:48:36 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_WRN("Expected json unocommandresult. Ignoring: " << stringMsg);
|
|
|
|
}
|
2016-05-20 19:15:50 -05:00
|
|
|
}
|
2016-10-03 10:45:58 -05:00
|
|
|
else if (tokens[0] == "error:")
|
2016-05-20 19:15:50 -05:00
|
|
|
{
|
|
|
|
std::string errorCommand;
|
|
|
|
std::string errorKind;
|
|
|
|
if (getTokenString(tokens[1], "cmd", errorCommand) &&
|
|
|
|
getTokenString(tokens[2], "kind", errorKind) )
|
2016-05-16 18:05:22 -05:00
|
|
|
{
|
2016-05-20 19:15:50 -05:00
|
|
|
if (errorCommand == "load")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-20 19:15:50 -05:00
|
|
|
if (errorKind == "passwordrequired:to-view" ||
|
|
|
|
errorKind == "passwordrequired:to-modify" ||
|
|
|
|
errorKind == "wrongpassword")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-12-21 20:07:55 -06:00
|
|
|
forwardToPeer(peer, buffer, length, false);
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_WRN("Document load failed: " << errorKind);
|
2016-05-20 19:15:50 -05:00
|
|
|
return false;
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
}
|
2016-05-20 19:15:50 -05:00
|
|
|
}
|
2016-10-03 10:45:58 -05:00
|
|
|
else if (tokens[0] == "curpart:" &&
|
|
|
|
tokens.count() == 2 &&
|
|
|
|
getTokenInteger(tokens[1], "part", _curPart))
|
2016-05-20 19:15:50 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-03 10:45:58 -05:00
|
|
|
else if (tokens.count() == 2 && tokens[0] == "saveas:")
|
2016-05-20 19:15:50 -05:00
|
|
|
{
|
|
|
|
std::string url;
|
|
|
|
if (!getTokenString(tokens[1], "url", url))
|
2016-05-16 18:05:22 -05:00
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_ERR("Bad syntax for: " << firstLine);
|
2016-05-20 19:15:50 -05:00
|
|
|
return false;
|
2016-05-16 18:05:22 -05:00
|
|
|
}
|
|
|
|
|
2016-10-02 14:30:59 -05:00
|
|
|
// Save-as completed, inform the ClientSession.
|
2016-05-20 19:15:50 -05:00
|
|
|
const std::string filePrefix("file:///");
|
|
|
|
if (url.find(filePrefix) == 0)
|
|
|
|
{
|
|
|
|
// Rewrite file:// URLs, as they are visible to the outside world.
|
|
|
|
const Path path(_docBroker->getJailRoot(), url.substr(filePrefix.length()));
|
2016-05-29 11:00:09 -05:00
|
|
|
if (Poco::File(path).exists())
|
|
|
|
{
|
|
|
|
url = filePrefix + path.toString().substr(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Blank for failure.
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_DBG("SaveAs produced no output, producing blank url.");
|
2016-05-29 11:00:09 -05:00
|
|
|
url.clear();
|
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
}
|
2016-05-20 19:15:50 -05:00
|
|
|
|
|
|
|
peer->setSaveAsUrl(url);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (tokens.count() == 2 && tokens[0] == "statechanged:")
|
|
|
|
{
|
|
|
|
if (_docBroker)
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-16 18:05:22 -05:00
|
|
|
StringTokenizer stateTokens(tokens[1], "=", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
if (stateTokens.count() == 2 && stateTokens[0] == ".uno:ModifiedStatus")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-20 19:15:50 -05:00
|
|
|
_docBroker->setModified(stateTokens[1] == "true");
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 19:05:44 -05:00
|
|
|
if (!_isDocPasswordProtected)
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-16 18:05:22 -05:00
|
|
|
if (tokens[0] == "tile:")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-07-26 02:29:39 -05:00
|
|
|
assert(false && "Tile traffic should go through the DocumentBroker-LoKit WS.");
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
else if (tokens[0] == "status:")
|
|
|
|
{
|
|
|
|
_docBroker->setLoaded();
|
|
|
|
|
|
|
|
// Forward the status response to the client.
|
2016-12-21 20:07:55 -06:00
|
|
|
return forwardToPeer(peer, buffer, length, false);
|
2016-05-16 18:05:22 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "commandvalues:")
|
|
|
|
{
|
|
|
|
const std::string stringMsg(buffer, length);
|
|
|
|
const auto index = stringMsg.find_first_of('{');
|
|
|
|
if (index != std::string::npos)
|
|
|
|
{
|
|
|
|
const std::string stringJSON = stringMsg.substr(index);
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
const auto result = parser.parse(stringJSON);
|
|
|
|
const auto& object = result.extract<Poco::JSON::Object::Ptr>();
|
2016-08-28 07:41:24 -05:00
|
|
|
const std::string commandName = object->has("commandName") ? object->get("commandName").toString() : "";
|
2016-05-16 18:05:22 -05:00
|
|
|
if (commandName.find(".uno:CharFontName") != std::string::npos ||
|
|
|
|
commandName.find(".uno:StyleApply") != std::string::npos)
|
|
|
|
{
|
|
|
|
// other commands should not be cached
|
|
|
|
_docBroker->tileCache().saveTextFile(stringMsg, "cmdValues" + commandName + ".txt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "partpagerectangles:")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-16 18:05:22 -05:00
|
|
|
if (tokens.count() > 1 && !tokens[1].empty())
|
2016-05-20 19:05:44 -05:00
|
|
|
{
|
2016-05-16 18:05:22 -05:00
|
|
|
_docBroker->tileCache().saveTextFile(std::string(buffer, length), "partpagerectangles.txt");
|
2016-05-20 19:05:44 -05:00
|
|
|
}
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
else if (tokens[0] == "invalidatetiles:")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-16 18:05:22 -05:00
|
|
|
assert(firstLine.size() == static_cast<std::string::size_type>(length));
|
2016-09-20 21:19:52 -05:00
|
|
|
_docBroker->invalidateTiles(firstLine);
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
2016-05-22 15:47:22 -05:00
|
|
|
else if (tokens[0] == "invalidatecursor:")
|
|
|
|
{
|
|
|
|
assert(firstLine.size() == static_cast<std::string::size_type>(length));
|
2016-05-23 02:09:11 -05:00
|
|
|
StringTokenizer firstLineTokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
2016-09-01 15:15:13 -05:00
|
|
|
int x = 0, y = 0, w = 0, h = 0;
|
2016-05-23 02:09:11 -05:00
|
|
|
if (firstLineTokens.count() > 2 &&
|
|
|
|
stringToInteger(firstLineTokens[1], x) &&
|
|
|
|
stringToInteger(firstLineTokens[2], y))
|
2016-05-22 15:47:22 -05:00
|
|
|
{
|
2016-09-01 15:15:13 -05:00
|
|
|
if (firstLineTokens.count() > 3)
|
|
|
|
{
|
|
|
|
stringToInteger(firstLineTokens[3], w);
|
|
|
|
stringToInteger(firstLineTokens[4], h);
|
|
|
|
}
|
|
|
|
_docBroker->invalidateCursor(x, y, w, h);
|
2016-05-22 15:47:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_ERR("Unable to parse " << firstLine);
|
2016-05-22 15:47:22 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
else if (tokens[0] == "renderfont:")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-11-27 17:49:17 -06:00
|
|
|
std::string font, text;
|
2016-11-11 11:24:13 -06:00
|
|
|
if (tokens.count() < 3 ||
|
2016-05-16 18:05:22 -05:00
|
|
|
!getTokenString(tokens[1], "font", font))
|
2016-05-20 19:05:44 -05:00
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_ERR("Bad syntax for: " << firstLine);
|
2016-05-20 19:05:44 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
|
2016-11-11 11:24:13 -06:00
|
|
|
getTokenString(tokens[2], "char", text);
|
2016-05-16 18:05:22 -05:00
|
|
|
assert(firstLine.size() < static_cast<std::string::size_type>(length));
|
2016-11-27 17:49:17 -06:00
|
|
|
_docBroker->tileCache().saveRendering(font+text, "font", buffer + firstLine.size() + 1, length - firstLine.size() - 1);
|
2016-12-11 17:50:32 -06:00
|
|
|
forwardToPeer(peer, buffer, length, true);
|
2016-11-27 20:39:50 -06:00
|
|
|
return true;
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 14:30:59 -05:00
|
|
|
else
|
|
|
|
{
|
2016-11-05 16:48:36 -05:00
|
|
|
LOG_INF("Ignoring notification on password protected document: " << firstLine);
|
2016-10-02 14:30:59 -05:00
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
|
2016-07-27 09:56:28 -05:00
|
|
|
// Detect json messages, since we must send those as text even though they are multiline.
|
|
|
|
// If not, the UI will read the first line of a binary payload, assuming that's the only
|
|
|
|
// text part and the rest is binary.
|
2016-12-21 20:07:55 -06:00
|
|
|
const bool isBinary = buffer[length - 1] != '}' && firstLine.find('{') == std::string::npos;
|
2016-07-27 09:56:28 -05:00
|
|
|
|
2016-05-20 19:05:44 -05:00
|
|
|
// Forward everything else.
|
2016-12-11 17:50:32 -06:00
|
|
|
forwardToPeer(peer, buffer, length, isBinary);
|
2016-05-16 06:37:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-11 17:50:32 -06:00
|
|
|
bool PrisonerSession::forwardToPeer(const std::shared_ptr<ClientSession>& clientSession,
|
|
|
|
const char* buffer, int length, const bool binary)
|
|
|
|
{
|
|
|
|
const auto message = LOOLProtocol::getAbbreviatedMessage(buffer, length);
|
|
|
|
|
|
|
|
if (clientSession->isCloseFrame())
|
|
|
|
{
|
|
|
|
LOG_TRC(getName() << ": peer began the closing handshake. Dropping forward message [" << message << "].");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (clientSession->isHeadless())
|
|
|
|
{
|
|
|
|
// Fail silently and return as there is no actual websocket
|
|
|
|
// connection in this case.
|
|
|
|
LOG_INF(getName() << ": Headless peer, not forwarding message [" << message << "].");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_TRC(getName() << " -> " << clientSession->getName() << ": " << message);
|
2016-12-11 17:56:55 -06:00
|
|
|
|
2016-12-17 13:28:11 -06:00
|
|
|
auto payload = std::make_shared<MessagePayload>(buffer, length,
|
|
|
|
binary ? MessagePayload::Type::Binary
|
|
|
|
: MessagePayload::Type::Text);
|
2016-12-13 18:20:05 -06:00
|
|
|
clientSession->enqueueSendMessage(payload);
|
2016-12-11 17:56:55 -06:00
|
|
|
|
|
|
|
return true;
|
2016-12-11 17:50:32 -06:00
|
|
|
}
|
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|