2017-02-11 11:13:31 -06: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-02-12 14:54:49 -06:00
|
|
|
#include <atomic>
|
2017-02-11 11:13:31 -06:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2017-02-14 05:10:52 -06:00
|
|
|
#include <assert.h>
|
2017-02-11 11:13:31 -06:00
|
|
|
|
2017-02-16 05:00:38 -06:00
|
|
|
#include <Poco/MemoryStream.h>
|
2017-02-11 11:13:31 -06:00
|
|
|
#include <Poco/Net/SocketAddress.h>
|
2017-02-24 10:45:31 -06:00
|
|
|
#include <Poco/Util/ServerApplication.h>
|
2017-02-16 05:00:38 -06:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2017-02-16 05:52:22 -06:00
|
|
|
#include <Poco/Runnable.h>
|
|
|
|
#include <Poco/Thread.h>
|
2017-02-16 05:00:38 -06:00
|
|
|
|
2017-02-21 19:43:51 -06:00
|
|
|
#include "Socket.hpp"
|
2017-02-21 20:49:29 -06:00
|
|
|
#include "ServerSocket.hpp"
|
2017-02-26 23:01:17 -06:00
|
|
|
#if ENABLE_SSL
|
2017-02-21 19:54:13 -06:00
|
|
|
#include "SslSocket.hpp"
|
2017-02-26 23:01:17 -06:00
|
|
|
#endif
|
2017-02-23 10:57:59 -06:00
|
|
|
#include "WebSocketHandler.hpp"
|
|
|
|
|
|
|
|
using Poco::MemoryInputStream;
|
|
|
|
using Poco::StringTokenizer;
|
2017-02-11 11:13:31 -06:00
|
|
|
|
2017-02-17 17:13:52 -06:00
|
|
|
constexpr int HttpPortNumber = 9191;
|
|
|
|
constexpr int SslPortNumber = 9193;
|
2017-02-11 14:13:14 -06:00
|
|
|
|
2017-02-23 06:14:01 -06:00
|
|
|
class SimpleResponseClient : public WebSocketHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SimpleResponseClient() : WebSocketHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-02 03:38:49 -06:00
|
|
|
virtual void handleIncomingMessage() override
|
|
|
|
{
|
|
|
|
LOG_TRC("incoming WebSocket message");
|
|
|
|
if (_wsState == WSState::HTTP)
|
|
|
|
{
|
|
|
|
auto socket = _socket.lock();
|
|
|
|
|
|
|
|
int number = 0;
|
|
|
|
Poco::MemoryInputStream message(&socket->_inBuffer[0], socket->_inBuffer.size());
|
|
|
|
Poco::Net::HTTPRequest req;
|
|
|
|
req.read(message);
|
|
|
|
|
|
|
|
// if we succeeded - remove that from our input buffer
|
|
|
|
// FIXME: We should check if this is GET or POST. For GET, we only
|
|
|
|
// can have a single request (headers only). For POST, we can/should
|
|
|
|
// use Poco HTMLForm to parse the post message properly.
|
|
|
|
// Otherwise, we should catch exceptions from the previous read/parse
|
|
|
|
// and assume we don't have sufficient data, so we wait some more.
|
|
|
|
socket->_inBuffer.clear();
|
|
|
|
|
|
|
|
LOG_DBG("URI: " << req.getURI());
|
|
|
|
|
|
|
|
Poco::StringTokenizer tokens(req.getURI(), "/?");
|
|
|
|
if (tokens.count() == 4)
|
|
|
|
{
|
|
|
|
std::string subpool = tokens[2];
|
|
|
|
number = std::stoi(tokens[3]);
|
|
|
|
|
|
|
|
// complex algorithmic core:
|
|
|
|
number = number + 1;
|
|
|
|
|
|
|
|
std::string numberString = std::to_string(number);
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "HTTP/1.1 200 OK\r\n"
|
|
|
|
<< "Date: Once, Upon a time GMT\r\n" // Mon, 27 Jul 2009 12:28:53 GMT
|
|
|
|
<< "Server: madeup string (Linux)\r\n"
|
|
|
|
<< "Content-Length: " << numberString.size() << "\r\n"
|
|
|
|
<< "Content-Type: text/plain\r\n"
|
|
|
|
<< "Connection: Closed\r\n"
|
|
|
|
<< "\r\n"
|
|
|
|
<< numberString;
|
2017-03-12 17:34:06 -05:00
|
|
|
|
2017-03-02 03:38:49 -06:00
|
|
|
std::string str = oss.str();
|
|
|
|
socket->_outBuffer.insert(socket->_outBuffer.end(), str.begin(), str.end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tokens.count() == 2 && tokens[1] == "ws")
|
|
|
|
{
|
|
|
|
|
|
|
|
upgradeToWebSocket(req);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketHandler::handleIncomingMessage();
|
|
|
|
}
|
|
|
|
|
2017-02-26 10:52:06 -06:00
|
|
|
virtual void handleMessage(const bool fin, const WSOpCode code, std::vector<char> &data) override
|
2017-02-16 12:49:31 -06:00
|
|
|
{
|
2017-02-17 18:58:49 -06:00
|
|
|
std::cerr << "Message: fin? " << fin << " code " << code << " data size " << data.size();
|
|
|
|
if (code == WSOpCode::Text)
|
|
|
|
{
|
|
|
|
std::string text(data.begin(), data.end());
|
|
|
|
std::cerr << " text is '" << text << "'\n";
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
std::cerr << " binary\n";
|
2017-02-16 15:57:18 -06:00
|
|
|
|
|
|
|
std::vector<char> reply;
|
2017-02-19 11:06:45 -06:00
|
|
|
if (data.size() == sizeof(size_t))
|
|
|
|
{
|
|
|
|
// ping pong test
|
|
|
|
assert (data.size() >= sizeof(size_t));
|
|
|
|
size_t *countPtr = reinterpret_cast<size_t *>(&data[0]);
|
|
|
|
size_t count = *countPtr;
|
|
|
|
count++;
|
|
|
|
std::cerr << "count is " << count << "\n";
|
|
|
|
reply.insert(reply.end(), reinterpret_cast<char *>(&count),
|
|
|
|
reinterpret_cast<char *>(&count) + sizeof(count));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// echo tests
|
|
|
|
reply.insert(reply.end(), data.begin(), data.end());
|
|
|
|
}
|
|
|
|
|
2017-02-27 00:45:18 -06:00
|
|
|
sendMessage(reply.data(), reply.size(), code);
|
2017-02-16 12:49:31 -06:00
|
|
|
}
|
2017-02-14 17:45:24 -06:00
|
|
|
};
|
|
|
|
|
2017-02-16 05:52:22 -06:00
|
|
|
// FIXME: use Poco Thread instead (?)
|
|
|
|
|
2017-02-12 14:54:49 -06:00
|
|
|
/// Generic thread class.
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Thread(const std::function<void(std::atomic<bool>&)>& cb) :
|
|
|
|
_cb(cb),
|
|
|
|
_stop(false)
|
|
|
|
{
|
|
|
|
_thread = std::thread([this]() { _cb(_stop); });
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread(Thread&& other) = delete;
|
|
|
|
const Thread& operator=(Thread&& other) = delete;
|
|
|
|
|
|
|
|
~Thread()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
if (_thread.joinable())
|
|
|
|
{
|
|
|
|
_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
_stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::function<void(std::atomic<bool>&)> _cb;
|
|
|
|
std::atomic<bool> _stop;
|
|
|
|
std::thread _thread;
|
|
|
|
};
|
|
|
|
|
2017-02-17 17:13:52 -06:00
|
|
|
Poco::Net::SocketAddress addrHttp("127.0.0.1", HttpPortNumber);
|
|
|
|
Poco::Net::SocketAddress addrSsl("127.0.0.1", SslPortNumber);
|
2017-02-13 19:55:01 -06:00
|
|
|
|
2017-02-21 20:49:29 -06:00
|
|
|
void server(const Poco::Net::SocketAddress& addr, SocketPoll& clientPoller,
|
|
|
|
std::unique_ptr<SocketFactory> sockFactory)
|
2017-02-13 19:55:01 -06:00
|
|
|
{
|
|
|
|
// Start server.
|
2017-02-21 20:49:29 -06:00
|
|
|
auto server = std::make_shared<ServerSocket>(clientPoller, std::move(sockFactory));
|
2017-02-13 19:55:01 -06:00
|
|
|
if (!server->bind(addr))
|
|
|
|
{
|
|
|
|
const std::string msg = "Failed to bind. (errno: ";
|
|
|
|
throw std::runtime_error(msg + std::strerror(errno) + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!server->listen())
|
|
|
|
{
|
|
|
|
const std::string msg = "Failed to listen. (errno: ";
|
|
|
|
throw std::runtime_error(msg + std::strerror(errno) + ")");
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:09:09 -06:00
|
|
|
SocketPoll serverPoll("srv_poll");
|
2017-02-15 08:48:48 -06:00
|
|
|
|
|
|
|
serverPoll.insertNewSocket(server);
|
|
|
|
|
2017-02-13 19:55:01 -06:00
|
|
|
std::cout << "Listening." << std::endl;
|
2017-03-06 22:09:09 -06:00
|
|
|
while (true)
|
|
|
|
sleep(1);
|
2017-02-13 19:55:01 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 10:45:31 -06:00
|
|
|
class LOOLNB : public Poco::Util::ServerApplication
|
2017-02-13 20:25:54 -06:00
|
|
|
{
|
2017-02-24 10:45:31 -06:00
|
|
|
public:
|
|
|
|
int main(const std::vector<std::string>& args) override
|
2017-02-12 14:54:49 -06:00
|
|
|
{
|
2017-02-24 10:45:31 -06:00
|
|
|
const char* logLevel = std::getenv("LOOL_LOGLEVEL");
|
|
|
|
std::map<std::string, std::string> props;
|
|
|
|
if (logLevel)
|
|
|
|
Log::initialize("loolnb", logLevel ? logLevel : "",
|
|
|
|
false, false, props);
|
|
|
|
|
2017-02-26 23:01:17 -06:00
|
|
|
#if ENABLE_SSL
|
2017-02-24 10:45:31 -06:00
|
|
|
// TODO: These would normally come from config.
|
|
|
|
SslContext::initialize("/etc/loolwsd/cert.pem",
|
|
|
|
"/etc/loolwsd/key.pem",
|
|
|
|
"/etc/loolwsd/ca-chain.cert.pem");
|
2017-02-26 23:01:17 -06:00
|
|
|
#endif
|
2017-02-24 10:45:31 -06:00
|
|
|
|
|
|
|
// Used to poll client sockets.
|
2017-03-06 22:09:09 -06:00
|
|
|
SocketPoll poller("client_poll");
|
2017-02-24 10:45:31 -06:00
|
|
|
|
|
|
|
class PlainSocketFactory : public SocketFactory
|
2017-02-16 08:14:12 -06:00
|
|
|
{
|
2017-02-24 10:45:31 -06:00
|
|
|
std::shared_ptr<Socket> create(const int fd) override
|
2017-02-25 13:08:03 -06:00
|
|
|
{
|
2017-03-02 03:38:49 -06:00
|
|
|
return StreamSocket::create<StreamSocket>(fd, std::unique_ptr<SocketHandlerInterface>{ new SimpleResponseClient });
|
2017-02-25 13:08:03 -06:00
|
|
|
}
|
2017-02-24 10:45:31 -06:00
|
|
|
};
|
2017-02-12 14:54:49 -06:00
|
|
|
|
2017-02-26 23:01:17 -06:00
|
|
|
#if ENABLE_SSL
|
2017-02-24 10:45:31 -06:00
|
|
|
class SslSocketFactory : public SocketFactory
|
2017-02-21 20:49:29 -06:00
|
|
|
{
|
2017-02-24 10:45:31 -06:00
|
|
|
std::shared_ptr<Socket> create(const int fd) override
|
2017-02-25 13:08:03 -06:00
|
|
|
{
|
2017-03-02 03:38:49 -06:00
|
|
|
return StreamSocket::create<SslStreamSocket>(fd, std::unique_ptr<SocketHandlerInterface>{ new SimpleResponseClient });
|
2017-02-25 13:08:03 -06:00
|
|
|
}
|
2017-02-24 10:45:31 -06:00
|
|
|
};
|
2017-02-21 20:49:29 -06:00
|
|
|
|
2017-02-24 10:45:31 -06:00
|
|
|
// Start the server.
|
2017-02-27 05:22:14 -06:00
|
|
|
if (!args.empty() && args.back() == "ssl")
|
2017-02-24 10:45:31 -06:00
|
|
|
server(addrSsl, poller, std::unique_ptr<SocketFactory>{new SslSocketFactory});
|
|
|
|
else
|
2017-02-26 23:01:17 -06:00
|
|
|
#endif
|
2017-02-24 10:45:31 -06:00
|
|
|
server(addrHttp, poller, std::unique_ptr<SocketFactory>{new PlainSocketFactory});
|
2017-02-21 20:49:29 -06:00
|
|
|
|
2017-02-24 10:45:31 -06:00
|
|
|
std::cout << "Shutting down server." << std::endl;
|
2017-02-11 17:21:57 -06:00
|
|
|
|
2017-03-06 22:09:09 -06:00
|
|
|
poller.stop();
|
2017-02-12 14:54:49 -06:00
|
|
|
|
2017-02-26 23:01:17 -06:00
|
|
|
#if ENABLE_SSL
|
2017-02-24 10:45:31 -06:00
|
|
|
SslContext::uninitialize();
|
2017-02-26 23:01:17 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
(void)args;
|
2017-02-24 10:45:31 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2017-02-12 13:44:14 -06:00
|
|
|
|
2017-02-24 10:45:31 -06:00
|
|
|
POCO_SERVER_MAIN(LOOLNB)
|
2017-02-11 11:13:31 -06:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|