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/FileStream.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/URIStreamOpener.h>
|
|
|
|
|
|
|
|
#include "Common.hpp"
|
|
|
|
#include "LOOLProtocol.hpp"
|
|
|
|
#include "LOOLSession.hpp"
|
|
|
|
#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"
|
|
|
|
#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-05-16 20:03:45 -05:00
|
|
|
PrisonerSession::PrisonerSession(const std::string& id,
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> ws,
|
|
|
|
std::shared_ptr<DocumentBroker> docBroker) :
|
2016-05-16 21:48:33 -05:00
|
|
|
LOOLSession(id, Kind::ToPrisoner, ws),
|
2016-07-26 02:29:39 -05:00
|
|
|
_docBroker(std::move(docBroker)),
|
2016-05-16 20:03:45 -05:00
|
|
|
_curPart(0)
|
|
|
|
{
|
2016-05-17 17:38:56 -05:00
|
|
|
Log::info("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-05-16 18:05:22 -05:00
|
|
|
Log::info("~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
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(buffer, length);
|
|
|
|
StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
Log::trace(getName() + ": handling [" + firstLine + "].");
|
|
|
|
|
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-07-27 09:56:28 -05:00
|
|
|
bool isBinary = true;
|
2016-05-20 19:15:50 -05:00
|
|
|
if (tokens[0] == "unocommandresult:")
|
|
|
|
{
|
|
|
|
const std::string stringMsg(buffer, length);
|
2016-09-16 05:09:00 -05:00
|
|
|
Log::info(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();
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
if (!_docBroker->save(getId(), success, result))
|
2016-10-10 09:14:06 -05:00
|
|
|
peer->sendTextFrame("error: cmd=internal kind=diskfull");
|
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-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-07-27 09:56:28 -05:00
|
|
|
forwardToPeer(_peer, buffer, length, isBinary);
|
2016-10-10 05:15:15 -05:00
|
|
|
Log::warn("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-05-20 19:15:50 -05:00
|
|
|
Log::error("Bad syntax for: " + firstLine);
|
|
|
|
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.
|
|
|
|
Log::debug("SaveAs produced no output, producing blank url.");
|
|
|
|
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-09-20 02:46:39 -05:00
|
|
|
return forwardToPeer(_peer, buffer, length, isBinary);
|
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
|
|
|
|
{
|
|
|
|
Log::error("Unable to parse " + firstLine);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
else if (tokens[0] == "renderfont:")
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
2016-05-16 18:05:22 -05:00
|
|
|
std::string font;
|
|
|
|
if (tokens.count() < 2 ||
|
|
|
|
!getTokenString(tokens[1], "font", font))
|
2016-05-20 19:05:44 -05:00
|
|
|
{
|
|
|
|
Log::error("Bad syntax for: " + firstLine);
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 18:05:22 -05:00
|
|
|
|
|
|
|
assert(firstLine.size() < static_cast<std::string::size_type>(length));
|
|
|
|
_docBroker->tileCache().saveRendering(font, "font", buffer + firstLine.size() + 1, length - firstLine.size() - 1);
|
2016-05-16 06:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 14:30:59 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::info("Ignoring notification on password protected document: " + firstLine);
|
|
|
|
}
|
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.
|
|
|
|
isBinary = buffer[length - 1] != '}' && firstLine.find('{') == std::string::npos;
|
|
|
|
|
2016-05-20 19:05:44 -05:00
|
|
|
// Forward everything else.
|
2016-07-27 09:56:28 -05:00
|
|
|
forwardToPeer(_peer, buffer, length, isBinary);
|
2016-05-16 06:37:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-12 05:05:57 -05:00
|
|
|
bool PrisonerSession::shutdownPeer(Poco::UInt16 statusCode)
|
2016-05-16 21:48:33 -05:00
|
|
|
{
|
|
|
|
auto peer = _peer.lock();
|
|
|
|
if (peer && !peer->isCloseFrame())
|
|
|
|
{
|
2016-10-12 05:05:57 -05:00
|
|
|
peer->shutdown(statusCode);
|
2016-05-16 21:48:33 -05:00
|
|
|
}
|
2016-05-29 11:03:26 -05:00
|
|
|
|
2016-05-16 21:48:33 -05:00
|
|
|
return peer != nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|