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/.
|
|
|
|
*/
|
|
|
|
|
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 "LOOLSession.hpp"
|
|
|
|
#include "config.h"
|
|
|
|
|
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-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
|
|
|
|
2016-03-07 11:58:26 -06:00
|
|
|
#include <Poco/Exception.h>
|
2016-04-12 15:13:19 -05:00
|
|
|
#include <Poco/Net/Socket.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
|
|
|
#include <Poco/Path.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/String.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
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 "ChildSession.hpp"
|
2016-01-27 03:05:54 -06:00
|
|
|
#include "Common.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 "IoUtil.hpp"
|
2015-03-26 09:49:07 -05:00
|
|
|
#include "LOOLProtocol.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"
|
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;
|
|
|
|
|
2016-03-07 11:58:26 -06:00
|
|
|
using Poco::Exception;
|
|
|
|
using Poco::IOException;
|
2016-04-12 15:13:19 -05:00
|
|
|
using Poco::Net::Socket;
|
2015-03-04 17:14:04 -06:00
|
|
|
using Poco::Net::WebSocket;
|
|
|
|
using Poco::StringTokenizer;
|
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,
|
2016-03-07 11:58:26 -06:00
|
|
|
std::shared_ptr<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),
|
2016-02-04 11:35:26 -06:00
|
|
|
_docPassword(""),
|
2016-04-27 19:51:54 -05:00
|
|
|
_haveDocPassword(false),
|
2016-02-04 11:35:26 -06:00
|
|
|
_isDocLoaded(false),
|
|
|
|
_isDocPasswordProtected(false),
|
2016-04-18 18:12:26 -05:00
|
|
|
_isCloseFrame(false),
|
2016-04-09 16:47:23 -05:00
|
|
|
_disconnected(false),
|
2016-04-21 00:14:27 -05:00
|
|
|
_isActive(true),
|
2016-04-09 16:47:23 -05:00
|
|
|
_lastActivityTime(std::chrono::steady_clock::now())
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2016-01-21 08:26:34 -06:00
|
|
|
// Only a post request can have a null ws.
|
|
|
|
if (_kind != Kind::ToClient)
|
|
|
|
assert(_ws);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-20 17:52:12 -05:00
|
|
|
bool LOOLSession::sendTextFrame(const std::string& text)
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
2016-04-12 15:13:19 -05:00
|
|
|
if (!_ws || _ws->poll(Poco::Timespan(0), Socket::SelectMode::SELECT_ERROR))
|
2015-12-24 21:19:50 -06:00
|
|
|
{
|
2016-05-19 19:28:02 -05:00
|
|
|
Log::error(getName() + ": Bad socket while sending [" + getAbbreviatedMessage(text.c_str(), text.size()) + "].");
|
2016-05-20 17:52:12 -05:00
|
|
|
return false;
|
2015-12-24 21:19:50 -06:00
|
|
|
}
|
2016-05-19 19:28:02 -05:00
|
|
|
|
|
|
|
Log::trace(getName() + " Send: " + getAbbreviatedMessage(text.c_str(), text.size()));
|
2016-04-12 15:13:19 -05:00
|
|
|
try
|
2016-01-27 03:05:54 -06:00
|
|
|
{
|
2016-05-20 17:52:12 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-04-12 15:13:19 -05:00
|
|
|
const int length = text.size();
|
|
|
|
if ( length > SMALL_MESSAGE_SIZE )
|
|
|
|
{
|
|
|
|
const std::string nextmessage = "nextmessage: size=" + std::to_string(length);
|
|
|
|
_ws->sendFrame(nextmessage.data(), nextmessage.size());
|
|
|
|
}
|
2015-06-05 08:12:06 -05:00
|
|
|
|
2016-04-12 15:13:19 -05:00
|
|
|
_ws->sendFrame(text.data(), length);
|
2016-05-20 17:52:12 -05:00
|
|
|
return true;
|
2016-04-12 15:13:19 -05:00
|
|
|
}
|
|
|
|
catch (const Exception& exc)
|
|
|
|
{
|
2016-05-20 17:52:12 -05:00
|
|
|
Log::error() << "LOOLSession::sendTextFrame: "
|
|
|
|
<< "Exception: " << exc.displayText()
|
|
|
|
<< (exc.nested() ? "( " + exc.nested()->displayText() + ")" : "");
|
2016-04-12 15:13:19 -05:00
|
|
|
}
|
2016-05-20 17:52:12 -05:00
|
|
|
|
|
|
|
return false;
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
|
2016-05-20 17:52:12 -05:00
|
|
|
bool LOOLSession::sendBinaryFrame(const char *buffer, int length)
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
2016-04-12 15:13:19 -05:00
|
|
|
if (!_ws || _ws->poll(Poco::Timespan(0), Socket::SelectMode::SELECT_ERROR))
|
2015-12-24 21:19:50 -06:00
|
|
|
{
|
2016-05-19 19:28:02 -05:00
|
|
|
Log::error(getName() + ": Bad socket while sending binary frame of " + std::to_string(length) + " bytes.");
|
2016-05-20 17:52:12 -05:00
|
|
|
return false;
|
2015-12-24 21:19:50 -06:00
|
|
|
}
|
2016-05-19 19:28:02 -05:00
|
|
|
|
|
|
|
Log::trace(getName() + " Send: " + std::to_string(length) + " bytes");
|
2016-04-12 15:13:19 -05:00
|
|
|
try
|
2015-06-05 08:12:06 -05:00
|
|
|
{
|
2016-05-20 17:52:12 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-04-12 15:13:19 -05:00
|
|
|
if ( length > SMALL_MESSAGE_SIZE )
|
|
|
|
{
|
|
|
|
const std::string nextmessage = "nextmessage: size=" + std::to_string(length);
|
|
|
|
_ws->sendFrame(nextmessage.data(), nextmessage.size());
|
|
|
|
}
|
2015-06-05 08:12:06 -05:00
|
|
|
|
2016-04-12 15:13:19 -05:00
|
|
|
_ws->sendFrame(buffer, length, WebSocket::FRAME_BINARY);
|
2016-05-20 17:52:12 -05:00
|
|
|
return true;
|
2016-04-12 15:13:19 -05:00
|
|
|
}
|
|
|
|
catch (const Exception& exc)
|
|
|
|
{
|
2016-05-20 17:52:12 -05:00
|
|
|
Log::error() << "LOOLSession::sendBinaryFrame: "
|
|
|
|
<< "Exception: " << exc.displayText()
|
|
|
|
<< (exc.nested() ? "( " + exc.nested()->displayText() + ")" : "");
|
2016-04-12 15:13:19 -05:00
|
|
|
}
|
2016-05-20 17:52:12 -05:00
|
|
|
|
|
|
|
return false;
|
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;
|
|
|
|
}
|
2016-01-06 23:33:54 -06:00
|
|
|
else if (tokens[i].find("jail=") == 0)
|
|
|
|
{
|
|
|
|
_jailedFilePath = tokens[i].substr(strlen("jail="));
|
|
|
|
++offset;
|
|
|
|
}
|
2015-11-19 03:30:00 -06:00
|
|
|
else if (tokens[i].find("timestamp=") == 0)
|
|
|
|
{
|
|
|
|
timestamp = tokens[i].substr(strlen("timestamp="));
|
|
|
|
++offset;
|
|
|
|
}
|
2016-02-04 11:35:26 -06:00
|
|
|
else if (tokens[i].find("password=") == 0)
|
|
|
|
{
|
|
|
|
_docPassword = tokens[i].substr(strlen("password="));
|
2016-04-27 19:51:54 -05:00
|
|
|
_haveDocPassword = true;
|
2016-02-04 11:35:26 -06:00
|
|
|
++offset;
|
|
|
|
}
|
2015-11-19 03:30:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:56:23 -05:00
|
|
|
void LOOLSession::disconnect()
|
2016-01-21 08:26:34 -06:00
|
|
|
{
|
2016-03-07 11:55:00 -06:00
|
|
|
try
|
2016-01-21 08:26:34 -06:00
|
|
|
{
|
2016-03-07 11:55:00 -06:00
|
|
|
if (!_disconnected)
|
|
|
|
{
|
|
|
|
_disconnected = true;
|
2016-03-27 14:22:24 -05:00
|
|
|
IoUtil::shutdownWebSocket(_ws);
|
2016-03-07 11:55:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const IOException& exc)
|
|
|
|
{
|
|
|
|
Log::error("LOOLSession::disconnect: Exception: " + exc.displayText() + (exc.nested() ? " (" + exc.nested()->displayText() + ")" : ""));
|
2016-01-21 08:26:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:56:23 -05:00
|
|
|
bool LOOLSession::handleDisconnect()
|
2016-01-21 08:26:34 -06:00
|
|
|
{
|
|
|
|
_disconnected = true;
|
2016-03-27 14:22:24 -05:00
|
|
|
IoUtil::shutdownWebSocket(_ws);
|
2016-01-21 08:26:34 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-25 12:51:32 -06:00
|
|
|
bool LOOLSession::handleInput(const char *buffer, int length)
|
|
|
|
{
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
|
2016-03-27 13:51:54 -05:00
|
|
|
const auto summary = getAbbreviatedMessage(buffer, length);
|
2015-12-25 12:51:32 -06:00
|
|
|
try
|
|
|
|
{
|
2016-03-26 08:17:08 -05:00
|
|
|
Log::trace(getName() + " Recv: " + summary);
|
|
|
|
if (TerminationFlag)
|
|
|
|
{
|
|
|
|
Log::warn("Input while terminating: [" + summary + "].");
|
|
|
|
}
|
2016-01-23 16:32:09 -06:00
|
|
|
|
2015-12-25 12:51:32 -06:00
|
|
|
return _handleInput(buffer, length);
|
|
|
|
}
|
|
|
|
catch (const Exception& exc)
|
|
|
|
{
|
2016-03-27 13:51:54 -05:00
|
|
|
Log::error() << "LOOLSession::handleInput: Exception while handling [" + summary + "] in "
|
2016-03-07 10:22:39 -06:00
|
|
|
<< getName() << ": "
|
2015-12-25 18:37:44 -06:00
|
|
|
<< exc.displayText()
|
|
|
|
<< (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
|
|
|
|
<< Log::end;
|
2015-12-25 12:51:32 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2016-03-27 13:51:54 -05:00
|
|
|
Log::error("LOOLSession::handleInput: Exception while handling [" + summary + "]: " + exc.what());
|
2015-12-25 12:51:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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: */
|