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-04 17:14:04 -06: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/.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <ftw.h>
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <cassert>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <cstring>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <map>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <memory>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <set>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKit.h>
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Buffer.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <Poco/File.h>
|
|
|
|
#include <Poco/Net/WebSocket.h>
|
|
|
|
#include <Poco/Path.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/Random.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/String.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-03-23 15:13:57 -05:00
|
|
|
#include <Poco/URI.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-28 06:22:15 -05:00
|
|
|
#include "LOKitHelper.hpp"
|
2015-03-26 09:49:07 -05:00
|
|
|
#include "LOOLProtocol.hpp"
|
2015-03-09 03:01:30 -05:00
|
|
|
#include "LOOLSession.hpp"
|
2015-03-17 18:56:15 -05:00
|
|
|
#include "LOOLWSD.hpp"
|
2015-03-12 09:18:35 -05:00
|
|
|
#include "TileCache.hpp"
|
2015-03-17 18:56:15 -05:00
|
|
|
#include "Util.hpp"
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Buffer;
|
2015-04-08 09:22:42 -05:00
|
|
|
using Poco::File;
|
2015-03-04 17:14:04 -06:00
|
|
|
using Poco::Net::WebSocket;
|
2015-04-08 09:22:42 -05:00
|
|
|
using Poco::Path;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Process;
|
|
|
|
using Poco::ProcessHandle;
|
|
|
|
using Poco::Random;
|
2015-03-04 17:14:04 -06:00
|
|
|
using Poco::StringTokenizer;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Thread;
|
|
|
|
using Poco::UInt64;
|
2015-03-23 15:13:57 -05:00
|
|
|
using Poco::URI;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Util::Application;
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
std::map<UInt64, LOOLSession*> LOOLSession::_childIdToChildSession;
|
|
|
|
std::set<UInt64> LOOLSession::_pendingPreForkedChildren;
|
|
|
|
std::set<LOOLSession*> LOOLSession::_availableChildSessions;
|
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
LOOLSession::LOOLSession(WebSocket& ws, LibreOfficeKit *loKit, UInt64 childId) :
|
2015-04-08 09:22:42 -05:00
|
|
|
_ws(&ws),
|
2015-03-17 18:56:15 -05:00
|
|
|
_toChildProcess(false),
|
2015-04-08 09:22:42 -05:00
|
|
|
_docURL(""),
|
2015-03-17 18:56:15 -05:00
|
|
|
_peerWs(nullptr),
|
2015-04-13 07:13:38 -05:00
|
|
|
_childId(childId),
|
2015-03-04 17:14:04 -06:00
|
|
|
_loKit(loKit),
|
|
|
|
_loKitDocument(NULL)
|
|
|
|
{
|
2015-04-08 09:22:42 -05:00
|
|
|
std::cout << Util::logPrefix() << "LOOLSesstion ctor this=" << this << " ws=" << _ws << std::endl;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
LOOLSession::~LOOLSession()
|
2015-03-07 05:23:46 -06:00
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
std::cout << Util::logPrefix() << "LOOLSesstion dtor this=" << this << std::endl;
|
2015-04-08 09:22:42 -05:00
|
|
|
_ws->shutdown();
|
2015-03-07 05:23:46 -06:00
|
|
|
if (_loKitDocument)
|
|
|
|
_loKitDocument->pClass->destroy(_loKitDocument);
|
|
|
|
}
|
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
const std::string LOOLSession::jailDocumentURL = "/user/thedocument";
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
bool LOOLSession::handleInput(char *buffer, int length)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
Application& app = Application::instance();
|
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
if (!isChildProcess() && haveSeparateProcess())
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
|
|
|
forwardRequest(buffer, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
char *endl = (char *) memchr(buffer, '\n', length);
|
|
|
|
std::string commandline;
|
2015-03-17 18:56:15 -05:00
|
|
|
if (endl == nullptr)
|
2015-03-04 17:14:04 -06:00
|
|
|
commandline = std::string(buffer, length);
|
|
|
|
else
|
|
|
|
commandline = std::string(buffer, endl-buffer);
|
2015-03-07 05:23:46 -06:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
app.logger().information(Util::logPrefix() + "Input: '" + commandline + "'" + (endl == nullptr ? "" : " ..."));
|
2015-03-04 17:14:04 -06:00
|
|
|
|
|
|
|
StringTokenizer tokens(commandline, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
if (toChildProcess())
|
|
|
|
{
|
|
|
|
assert(_peerWs != nullptr);
|
|
|
|
_peerWs->sendFrame(buffer, length, WebSocket::FRAME_BINARY);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "child")
|
|
|
|
{
|
|
|
|
if (_peerWs != nullptr ||
|
|
|
|
isChildProcess())
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=invalid");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (tokens.count() != 2)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
UInt64 childId = std::stoull(tokens[1]);
|
|
|
|
app.logger().information(Util::logPrefix() + "childId=" + std::to_string(childId));
|
2015-04-08 09:22:42 -05:00
|
|
|
if (_pendingPreForkedChildren.find(childId) == _pendingPreForkedChildren.end())
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=notfound");
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-08 09:22:42 -05:00
|
|
|
_pendingPreForkedChildren.erase(childId);
|
|
|
|
_availableChildSessions.insert(this);
|
|
|
|
_childIdToChildSession[childId] = this;
|
2015-03-17 18:56:15 -05:00
|
|
|
_toChildProcess = true;
|
2015-04-08 09:22:42 -05:00
|
|
|
_childId = childId;
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "load")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
if (_docURL != "")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-04-13 09:03:55 -05:00
|
|
|
sendTextFrame("error: cmd=load kind=docalreadyloaded");
|
2015-03-07 05:23:46 -06:00
|
|
|
return false;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
return loadDocument(buffer, length, tokens);
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
else if (_docURL == "")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-13 06:59:51 -05:00
|
|
|
sendTextFrame("error: cmd=" + tokens[0] + " kind=nodocloaded");
|
2015-03-07 05:23:46 -06:00
|
|
|
return false;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "status")
|
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
return getStatus(buffer, length);
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "tile")
|
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
sendTile(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
// All other commands are such that they always require a
|
|
|
|
// LibreOfficeKitDocument session, i.e. need to be handled in
|
|
|
|
// a child process.
|
|
|
|
|
|
|
|
if (!isChildProcess())
|
|
|
|
{
|
|
|
|
if (tokens[0] != "key" &&
|
|
|
|
tokens[0] != "mouse" &&
|
|
|
|
tokens[0] != "uno" &&
|
|
|
|
tokens[0] != "selecttext" &&
|
|
|
|
tokens[0] != "selectgraphic" &&
|
2015-03-23 15:13:57 -05:00
|
|
|
tokens[0] != "resetselection" &&
|
|
|
|
tokens[0] != "saveas")
|
2015-03-20 08:50:23 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=" + tokens[0] + " kind=unknown");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
dispatchChild();
|
2015-03-20 08:50:23 -05:00
|
|
|
forwardRequest(buffer, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens[0] == "key")
|
|
|
|
{
|
|
|
|
return keyEvent(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "mouse")
|
|
|
|
{
|
|
|
|
return mouseEvent(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "uno")
|
|
|
|
{
|
|
|
|
return unoCommand(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "selecttext")
|
|
|
|
{
|
|
|
|
return selectText(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "selectgraphic")
|
|
|
|
{
|
|
|
|
return selectGraphic(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "resetselection")
|
|
|
|
{
|
2015-03-23 15:13:57 -05:00
|
|
|
return resetSelection(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "saveas")
|
|
|
|
{
|
|
|
|
return saveAs(buffer, length, tokens);
|
2015-03-20 08:50:23 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(false);
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-07 05:23:46 -06:00
|
|
|
return true;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
bool LOOLSession::haveSeparateProcess() const
|
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
return _childId != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LOOLSession::toChildProcess() const
|
|
|
|
{
|
|
|
|
return _toChildProcess;
|
2015-03-12 18:34:42 -05:00
|
|
|
}
|
|
|
|
|
2015-03-26 04:25:22 -05:00
|
|
|
void LOOLSession::sendTextFrame(const std::string& text)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-04-08 09:22:42 -05:00
|
|
|
_ws->sendFrame(text.data(), text.size());
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
void LOOLSession::sendBinaryFrame(const char *buffer, int length)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-04-08 09:22:42 -05:00
|
|
|
_ws->sendFrame(buffer, length, WebSocket::FRAME_BINARY);
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-07 05:23:46 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
static void myCallback(int nType, const char* pPayload, void* pData)
|
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
LOOLSession *srv = reinterpret_cast<LOOLSession *>(pData);
|
2015-03-07 05:23:46 -06:00
|
|
|
|
|
|
|
switch ((LibreOfficeKitCallbackType) nType)
|
|
|
|
{
|
|
|
|
case LOK_CALLBACK_INVALIDATE_TILES:
|
|
|
|
srv->sendTextFrame("invalidatetiles: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
|
|
|
|
srv->sendTextFrame("invalidatecursor:");
|
2015-03-13 04:25:00 -05:00
|
|
|
break;
|
2015-03-07 05:23:46 -06:00
|
|
|
case LOK_CALLBACK_TEXT_SELECTION:
|
|
|
|
srv->sendTextFrame("textselection: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_TEXT_SELECTION_START:
|
|
|
|
srv->sendTextFrame("textselectionstart: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_TEXT_SELECTION_END:
|
|
|
|
srv->sendTextFrame("textselectionend: " + std::string(pPayload));
|
|
|
|
break;
|
2015-03-13 04:30:23 -05:00
|
|
|
case LOK_CALLBACK_CURSOR_VISIBLE:
|
|
|
|
srv->sendTextFrame("cursorvisible: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_GRAPHIC_SELECTION:
|
|
|
|
srv->sendTextFrame("graphicselection: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_HYPERLINK_CLICKED:
|
|
|
|
srv->sendTextFrame("hyperlinkclicked: " + std::string(pPayload));
|
|
|
|
break;
|
2015-04-14 06:51:36 -05:00
|
|
|
case LOK_CALLBACK_STATE_CHANGED:
|
|
|
|
srv->sendTextFrame("statechanged: " + std::string(pPayload));
|
|
|
|
break;
|
2015-03-07 05:23:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
bool LOOLSession::loadDocument(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
if (tokens.count() != 2)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=load kind=syntax");
|
2015-03-17 18:56:15 -05:00
|
|
|
return false;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
if (tokens[1].find("url=") == 0)
|
|
|
|
_docURL = tokens[1].substr(strlen("url="));
|
|
|
|
else
|
|
|
|
_docURL = tokens[1];
|
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
_tileCache.reset(new TileCache(_docURL));
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
if (isChildProcess())
|
2015-03-07 05:23:46 -06:00
|
|
|
{
|
2015-04-13 07:13:38 -05:00
|
|
|
// The URL in the request is the original one, not visible in the chroot jail.
|
|
|
|
// The child process uses the fixed name jailDocumentURL.
|
|
|
|
|
|
|
|
if ((_loKitDocument = _loKit->pClass->documentLoad(_loKit, jailDocumentURL.c_str())) == NULL)
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
2015-03-25 05:08:12 -05:00
|
|
|
sendTextFrame("error: cmd=load kind=failed");
|
|
|
|
return false;
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
2015-03-25 05:08:12 -05:00
|
|
|
|
2015-04-10 04:20:42 -05:00
|
|
|
_loKitDocument->pClass->initializeForRendering(_loKitDocument);
|
|
|
|
|
2015-03-25 05:08:12 -05:00
|
|
|
if (!getStatus(buffer, length))
|
|
|
|
return false;
|
|
|
|
_loKitDocument->pClass->registerCallback(_loKitDocument, myCallback, this);
|
2015-04-08 09:22:42 -05:00
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
|
|
|
|
return true;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
bool LOOLSession::getStatus(const char *buffer, int length)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-13 07:17:51 -05:00
|
|
|
std::string status = _tileCache->getStatus();
|
|
|
|
if (status.size() > 0)
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
|
|
|
sendTextFrame(status);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isChildProcess())
|
|
|
|
{
|
2015-04-08 09:22:42 -05:00
|
|
|
dispatchChild();
|
2015-03-17 18:56:15 -05:00
|
|
|
forwardRequest(buffer, length);
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-13 07:17:51 -05:00
|
|
|
|
2015-03-28 06:22:15 -05:00
|
|
|
status = "status: " + LOKitHelper::documentStatus(_loKitDocument);
|
2015-03-17 18:56:15 -05:00
|
|
|
_tileCache->saveStatus(status);
|
|
|
|
|
|
|
|
sendTextFrame(status);
|
|
|
|
|
|
|
|
return true;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
void LOOLSession::sendTile(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
int width, height, tilePosX, tilePosY, tileWidth, tileHeight;
|
|
|
|
|
|
|
|
if (tokens.count() != 7 ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[1], "width", width) ||
|
|
|
|
!getTokenInteger(tokens[2], "height", height) ||
|
|
|
|
!getTokenInteger(tokens[3], "tileposx", tilePosX) ||
|
|
|
|
!getTokenInteger(tokens[4], "tileposy", tilePosY) ||
|
|
|
|
!getTokenInteger(tokens[5], "tilewidth", tileWidth) ||
|
|
|
|
!getTokenInteger(tokens[6], "tileheight", tileHeight))
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=syntax");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-24 11:59:04 -05:00
|
|
|
if (width <= 0 ||
|
|
|
|
height <= 0 ||
|
|
|
|
tilePosX < 0 ||
|
|
|
|
tilePosY < 0 ||
|
|
|
|
tileWidth <= 0 ||
|
|
|
|
tileHeight <= 0)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:18:35 -05:00
|
|
|
std::string response = "tile: " + Poco::cat(std::string(" "), tokens.begin() + 1, tokens.end()) + "\n";
|
|
|
|
|
|
|
|
std::vector<char> output;
|
|
|
|
output.reserve(4 * width * height);
|
|
|
|
output.resize(response.size());
|
2015-04-09 17:25:48 -05:00
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2015-03-13 07:17:51 -05:00
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(width, height, tilePosX, tilePosY, tileWidth, tileHeight);
|
2015-03-12 18:34:42 -05:00
|
|
|
if (cachedTile && cachedTile->is_open())
|
2015-03-12 09:18:35 -05:00
|
|
|
{
|
|
|
|
cachedTile->seekg(0, std::ios_base::end);
|
|
|
|
size_t pos = output.size();
|
|
|
|
std::streamsize size = cachedTile->tellg();
|
|
|
|
output.resize(pos + size);
|
|
|
|
cachedTile->seekg(0, std::ios_base::beg);
|
|
|
|
cachedTile->read(output.data() + pos, size);
|
|
|
|
cachedTile->close();
|
|
|
|
|
|
|
|
sendBinaryFrame(output.data(), output.size());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
if (!isChildProcess())
|
|
|
|
{
|
2015-04-08 09:22:42 -05:00
|
|
|
dispatchChild();
|
2015-03-17 18:56:15 -05:00
|
|
|
forwardRequest(buffer, length);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *pixmap = new unsigned char[4 * width * height];
|
2015-03-20 06:16:41 -05:00
|
|
|
_loKitDocument->pClass->paintTile(_loKitDocument, pixmap, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
if (!Util::encodePNGAndAppendToBuffer(pixmap, width, height, output))
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=failure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
delete[] pixmap;
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-13 07:17:51 -05:00
|
|
|
_tileCache->saveTile(width, height, tilePosX, tilePosY, tileWidth, tileHeight, output.data() + response.size(), output.size() - response.size());
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
sendBinaryFrame(output.data(), output.size());
|
|
|
|
}
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
bool LOOLSession::keyEvent(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(isChildProcess());
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
int type, charcode, keycode;
|
|
|
|
|
|
|
|
if (tokens.count() != 4 ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[1], "type", type) ||
|
|
|
|
!getTokenInteger(tokens[2], "char", charcode) ||
|
|
|
|
!getTokenInteger(tokens[3], "key", keycode))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=key kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->postKeyEvent(_loKitDocument, type, charcode, keycode);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LOOLSession::mouseEvent(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(isChildProcess());
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
int type, x, y, count;
|
|
|
|
|
|
|
|
if (tokens.count() != 5 ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[1], "type", type) ||
|
|
|
|
!getTokenInteger(tokens[2], "x", x) ||
|
|
|
|
!getTokenInteger(tokens[3], "y", y) ||
|
|
|
|
!getTokenInteger(tokens[4], "count", count))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=mouse kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->postMouseEvent(_loKitDocument, type, x, y, count);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LOOLSession::unoCommand(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(isChildProcess());
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
if (tokens.count() == 1)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=uno kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-22 13:26:50 -05:00
|
|
|
_loKitDocument->pClass->postUnoCommand(_loKitDocument, tokens[1].c_str(), Poco::cat(std::string(" "), tokens.begin() + 2, tokens.end()).c_str());
|
2015-03-19 07:38:04 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LOOLSession::selectText(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(isChildProcess());
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
int type, x, y;
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
if (tokens.count() != 4 ||
|
|
|
|
!getTokenInteger(tokens[1], "type", type) ||
|
|
|
|
!getTokenInteger(tokens[2], "x", x) ||
|
|
|
|
!getTokenInteger(tokens[3], "y", y))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=selecttext kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->setTextSelection(_loKitDocument, type, x, y);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LOOLSession::selectGraphic(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(isChildProcess());
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
int type, x, y;
|
|
|
|
|
|
|
|
if (tokens.count() != 4 ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[1], "type", type) ||
|
|
|
|
!getTokenInteger(tokens[2], "x", x) ||
|
|
|
|
!getTokenInteger(tokens[3], "y", y))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=selectghraphic kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->setGraphicSelection(_loKitDocument, type, x, y);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-20 07:56:37 -05:00
|
|
|
bool LOOLSession::resetSelection(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(isChildProcess());
|
|
|
|
|
2015-03-20 07:56:37 -05:00
|
|
|
if (tokens.count() != 1)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=resetselection kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
_loKitDocument->pClass->resetSelection(_loKitDocument);
|
2015-03-20 07:56:37 -05:00
|
|
|
|
|
|
|
return true;
|
2015-03-19 07:38:04 -05:00
|
|
|
}
|
|
|
|
|
2015-03-23 15:13:57 -05:00
|
|
|
bool LOOLSession::saveAs(const char *buffer, int length, Poco::StringTokenizer& tokens)
|
|
|
|
{
|
|
|
|
assert(isChildProcess());
|
|
|
|
|
|
|
|
std::string url, format, filterOptions;
|
|
|
|
|
|
|
|
if (tokens.count() < 4 ||
|
|
|
|
!getTokenString(tokens[1], "url", url) ||
|
|
|
|
!getTokenString(tokens[2], "format", format) ||
|
|
|
|
!getTokenString(tokens[3], "options", filterOptions))
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=saveas kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI::decode(url, url, true);
|
|
|
|
URI::decode(format, format, true);
|
|
|
|
|
|
|
|
if (tokens.count() > 4)
|
|
|
|
filterOptions += Poco::cat(std::string(" "), tokens.begin() + 4, tokens.end());
|
|
|
|
|
|
|
|
_loKitDocument->pClass->saveAs(_loKitDocument, url.c_str(), format.c_str(), filterOptions.c_str());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
namespace
|
2015-03-12 18:34:42 -05:00
|
|
|
{
|
2015-04-08 09:22:42 -05:00
|
|
|
std::string sourceForLinkOrCopy;
|
|
|
|
Path destinationForLinkOrCopy;
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
int linkOrCopyFunction(const char *fpath,
|
|
|
|
const struct stat *sb,
|
|
|
|
int typeflag,
|
|
|
|
struct FTW *ftwbuf)
|
|
|
|
{
|
|
|
|
if (strcmp(fpath, sourceForLinkOrCopy.c_str()) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(fpath[strlen(sourceForLinkOrCopy.c_str())] == '/');
|
|
|
|
const char *relativeOldPath = fpath + strlen(sourceForLinkOrCopy.c_str()) + 1;
|
|
|
|
Path newPath(destinationForLinkOrCopy, relativeOldPath);
|
|
|
|
|
|
|
|
switch (typeflag)
|
|
|
|
{
|
|
|
|
case FTW_F:
|
|
|
|
if (link(fpath, newPath.toString().c_str()) == -1)
|
|
|
|
{
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"link(\"" + fpath + "\",\"" + newPath.toString() + "\") failed: " +
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FTW_D:
|
|
|
|
if (mkdir(newPath.toString().c_str(), 0700) == -1)
|
|
|
|
{
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"mkdir(\"" + newPath.toString() + "\") failed: " +
|
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FTW_DNR:
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"Cannot read directory '" + fpath + "'");
|
|
|
|
return 1;
|
|
|
|
case FTW_NS:
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"nftw: stat failed for '" + fpath + "'");
|
|
|
|
return 1;
|
|
|
|
case FTW_SLN:
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"nftw: symlink to nonexistent file: '" + fpath + "'");
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linkOrCopy(const std::string& source, const Path& destination)
|
|
|
|
{
|
|
|
|
sourceForLinkOrCopy = source;
|
|
|
|
destinationForLinkOrCopy = destination;
|
|
|
|
nftw(source.c_str(), linkOrCopyFunction, 10, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOOLSession::preFork()
|
|
|
|
{
|
|
|
|
// Create child-specific subtree that will become its chroot root
|
2015-03-17 18:56:15 -05:00
|
|
|
|
|
|
|
Random rng;
|
2015-04-08 09:22:42 -05:00
|
|
|
UInt64 childId = (((UInt64)rng.next()) << 32) | rng.next() | 1;
|
|
|
|
|
|
|
|
Path jail = getJailPath(childId);
|
|
|
|
File(jail).createDirectory();
|
|
|
|
|
|
|
|
Path jailLOInstallation(jail, LOOLWSD::loSubPath);
|
|
|
|
File(jailLOInstallation).createDirectory();
|
|
|
|
|
|
|
|
// Copy (link) LO installation and other necessary files into it from the template
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
linkOrCopy(LOOLWSD::sysTemplate, jail);
|
|
|
|
linkOrCopy(LOOLWSD::loTemplate, jailLOInstallation);
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
_pendingPreForkedChildren.insert(childId);
|
|
|
|
|
|
|
|
Process::Args args;
|
|
|
|
args.push_back("--child=" + std::to_string(childId));
|
|
|
|
args.push_back("--port=" + std::to_string(LOOLWSD::portNumber));
|
|
|
|
args.push_back("--jail=" + jail.toString());
|
|
|
|
args.push_back("--losubpath=" + LOOLWSD::loSubPath);
|
2015-03-17 18:56:15 -05:00
|
|
|
|
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Launching child: " + Poco::cat(std::string(" "), args.begin(), args.end()));
|
|
|
|
|
|
|
|
ProcessHandle child = Process::launch(Application::instance().commandPath(), args);
|
2015-04-08 09:22:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void LOOLSession::dispatchChild()
|
|
|
|
{
|
|
|
|
assert(!isChildProcess());
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
// Copy document into jail using the fixed name
|
2015-04-08 09:22:42 -05:00
|
|
|
|
|
|
|
assert(_availableChildSessions.size() > 0);
|
|
|
|
|
|
|
|
LOOLSession *childSession = *(_availableChildSessions.begin());
|
|
|
|
|
|
|
|
_availableChildSessions.erase(childSession);
|
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
assert(jailDocumentURL[0] == '/');
|
|
|
|
Path copy(getJailPath(childSession->_childId), jailDocumentURL.substr(1));
|
2015-04-08 09:22:42 -05:00
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Copying " + _docURL + " to " + copy.toString());
|
|
|
|
|
|
|
|
File(_docURL).copyTo(copy.toString());
|
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
_peerWs = childSession->_ws;
|
|
|
|
childSession->_peerWs = _ws;
|
|
|
|
|
|
|
|
std::string loadRequest = "load url=" + _docURL;
|
2015-03-17 18:56:15 -05:00
|
|
|
forwardRequest(loadRequest.c_str(), loadRequest.size());
|
2015-03-12 18:34:42 -05:00
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
void LOOLSession::forwardRequest(const char *buffer, int length)
|
|
|
|
{
|
2015-04-13 07:13:38 -05:00
|
|
|
Application::instance().logger().information(Util::logPrefix() + "forwardRequest(" + std::string(buffer, length) + ")");
|
2015-04-08 09:22:42 -05:00
|
|
|
assert(_peerWs != nullptr);
|
2015-03-17 18:56:15 -05:00
|
|
|
_peerWs->sendFrame(buffer, length, WebSocket::FRAME_BINARY);
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
Path LOOLSession::getJailPath(Poco::UInt64 childId)
|
|
|
|
{
|
|
|
|
return Path::forDirectory(LOOLWSD::childRoot + Path::separator() + std::to_string(childId));
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
bool LOOLSession::isChildProcess() const
|
|
|
|
{
|
|
|
|
return _loKit != nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|