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-27 07:51:33 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <ftw.h>
|
2015-04-27 07:51:33 -05:00
|
|
|
#include <utime.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <cassert>
|
2015-05-07 08:29:36 -05:00
|
|
|
#include <condition_variable>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <cstring>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2015-04-30 07:58:13 -05:00
|
|
|
#include <iterator>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <map>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <memory>
|
2015-05-07 08:29:36 -05:00
|
|
|
#include <mutex>
|
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-09-09 12:23:49 -05:00
|
|
|
#include <Poco/Dynamic/Var.h>
|
2015-05-29 00:49:49 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <Poco/File.h>
|
2015-09-09 12:23:49 -05:00
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2015-04-30 07:58:13 -05:00
|
|
|
#include <Poco/Net/HTTPStreamFactory.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
#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-04-30 10:01:21 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/String.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-05-07 12:15:05 -05:00
|
|
|
#include <Poco/ThreadLocal.h>
|
2015-03-23 15:13:57 -05:00
|
|
|
#include <Poco/URI.h>
|
2015-04-30 07:58:13 -05:00
|
|
|
#include <Poco/URIStreamOpener.h>
|
2015-07-04 19:46:13 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-07-13 09:13:06 -05:00
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/Net/DialogSocket.h>
|
|
|
|
#include <Poco/Net/SocketAddress.h>
|
2015-08-05 19:20:05 -05:00
|
|
|
#include <Poco/FileStream.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#include "LOOLProtocol.hpp"
|
2015-03-09 03:01:30 -05:00
|
|
|
#include "LOOLSession.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-09-09 12:23:49 -05:00
|
|
|
using Poco::Dynamic::Var;
|
2015-04-08 09:22:42 -05:00
|
|
|
using Poco::File;
|
2015-05-29 00:49:49 -05:00
|
|
|
using Poco::IOException;
|
2015-09-09 12:23:49 -05:00
|
|
|
using Poco::JSON::Object;
|
|
|
|
using Poco::JSON::Parser;
|
2015-04-30 07:58:13 -05:00
|
|
|
using Poco::Net::HTTPStreamFactory;
|
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-04-30 10:01:21 -05:00
|
|
|
using Poco::StreamCopier;
|
2015-03-04 17:14:04 -06:00
|
|
|
using Poco::StringTokenizer;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Thread;
|
2015-05-07 12:15:05 -05:00
|
|
|
using Poco::ThreadLocal;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::UInt64;
|
2015-03-23 15:13:57 -05:00
|
|
|
using Poco::URI;
|
2015-04-30 07:58:13 -05:00
|
|
|
using Poco::URIStreamOpener;
|
2015-07-04 19:46:13 -05:00
|
|
|
using Poco::Exception;
|
2015-07-13 09:13:06 -05:00
|
|
|
using Poco::Net::DialogSocket;
|
|
|
|
using Poco::Net::SocketAddress;
|
|
|
|
using Poco::Net::WebSocketException;
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
LOOLSession::LOOLSession(const std::string& id, const Kind kind,
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> ws) :
|
2015-04-20 09:43:31 -05:00
|
|
|
_kind(kind),
|
2015-12-27 21:47:39 -06:00
|
|
|
_kindString(kind == Kind::ToClient ? "ToClient" :
|
|
|
|
kind == Kind::ToMaster ? "ToMaster" : "ToPrisoner"),
|
2015-05-28 08:42:38 -05:00
|
|
|
_ws(ws),
|
2015-12-28 15:05:01 -06:00
|
|
|
_docURL(""),
|
|
|
|
_bShutdown(false)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-12-27 21:47:39 -06:00
|
|
|
setId(id);
|
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-10-19 03:58:21 -05:00
|
|
|
if (_ws)
|
|
|
|
Util::shutdownWebSocket(*_ws);
|
2015-03-07 05:23:46 -06:00
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
void LOOLSession::sendTextFrame(const std::string& text)
|
|
|
|
{
|
2015-12-20 11:59:26 -06:00
|
|
|
if (!_ws)
|
2015-12-24 21:19:50 -06:00
|
|
|
{
|
|
|
|
Log::error("Error: No socket to send to.");
|
|
|
|
return;
|
|
|
|
}
|
2015-12-20 11:59:26 -06:00
|
|
|
else
|
2016-01-05 21:31:57 -06:00
|
|
|
Log::trace(getName() + " Send: " + getAbbreviatedMessage(text.c_str(), text.size()));
|
2015-12-20 11:59:26 -06:00
|
|
|
|
2015-06-05 08:12:06 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
_ws->sendFrame(text.data(), text.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOOLSession::sendBinaryFrame(const char *buffer, int length)
|
|
|
|
{
|
2015-12-20 11:59:26 -06:00
|
|
|
if (!_ws)
|
2015-12-24 21:19:50 -06:00
|
|
|
{
|
|
|
|
Log::error("Error: No socket to send to.");
|
|
|
|
return;
|
|
|
|
}
|
2015-12-20 11:59:26 -06:00
|
|
|
else
|
2016-01-05 21:31:57 -06:00
|
|
|
Log::trace(getName() + " Send: " + std::to_string(length) + " bytes");
|
2015-12-20 11:59:26 -06:00
|
|
|
|
2015-06-05 08:12:06 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (length > 1000)
|
|
|
|
{
|
2016-01-05 21:31:57 -06:00
|
|
|
const std::string nextmessage = "nextmessage: size=" + std::to_string(length);
|
2015-12-24 21:19:50 -06:00
|
|
|
_ws->sendFrame(nextmessage.data(), nextmessage.size());
|
2015-06-05 08:12:06 -05:00
|
|
|
}
|
|
|
|
|
2015-12-24 21:19:50 -06:00
|
|
|
_ws->sendFrame(buffer, length, WebSocket::FRAME_BINARY);
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
2015-04-13 07:13:38 -05:00
|
|
|
|
2015-11-19 03:30:00 -06:00
|
|
|
void LOOLSession::parseDocOptions(const StringTokenizer& tokens, int& part, std::string& timestamp)
|
|
|
|
{
|
|
|
|
// First token is the "load" command itself.
|
|
|
|
size_t offset = 1;
|
|
|
|
if (tokens.count() > 2 && tokens[1].find("part=") == 0)
|
|
|
|
{
|
|
|
|
getTokenInteger(tokens[1], "part", part);
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = offset; i < tokens.count(); ++i)
|
|
|
|
{
|
|
|
|
if (tokens[i].find("url=") == 0)
|
|
|
|
{
|
|
|
|
_docURL = tokens[i].substr(strlen("url="));
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
else if (tokens[i].find("timestamp=") == 0)
|
|
|
|
{
|
|
|
|
timestamp = tokens[i].substr(strlen("timestamp="));
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens.count() > offset)
|
|
|
|
{
|
|
|
|
if (getTokenString(tokens[offset], "options", _docOptions))
|
|
|
|
{
|
|
|
|
if (tokens.count() > offset + 1)
|
|
|
|
_docOptions += Poco::cat(std::string(" "), tokens.begin() + offset + 1, tokens.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 12:51:32 -06:00
|
|
|
bool LOOLSession::handleInput(const char *buffer, int length)
|
|
|
|
{
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
|
2016-01-05 21:31:57 -06:00
|
|
|
Log::trace(getName() + " Recv: " + getAbbreviatedMessage(buffer, length));
|
2015-12-25 12:51:32 -06:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return _handleInput(buffer, length);
|
|
|
|
}
|
|
|
|
catch (const Exception& exc)
|
|
|
|
{
|
2015-12-25 18:37:44 -06:00
|
|
|
Log::error() << "Error while handling [" + getFirstLine(buffer, length) + "]. "
|
|
|
|
<< exc.displayText()
|
|
|
|
<< (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
|
|
|
|
<< Log::end;
|
2015-12-25 12:51:32 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2015-12-25 18:37:44 -06:00
|
|
|
Log::error("Error while handling [" + getFirstLine(buffer, length) + "]. " +
|
|
|
|
std::string("Exception: ") + exc.what());
|
2015-12-25 12:51:32 -06:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Log::error("Unexpected Exception.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-27 16:23:43 -06:00
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|