2016-04-07 15:59:27 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
2020-04-18 03:39:50 -05:00
|
|
|
|
|
|
|
#pragma once
|
2016-04-07 15:59:27 -05:00
|
|
|
|
2016-10-20 16:09:00 -05:00
|
|
|
#include <memory>
|
2016-08-17 01:39:10 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
2016-04-07 15:59:27 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerParams.h>
|
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
|
|
|
#include <Poco/Net/SocketAddress.h>
|
2016-10-29 20:15:00 -05:00
|
|
|
#include <Poco/Version.h>
|
2016-04-07 15:59:27 -05:00
|
|
|
|
|
|
|
#include "Common.hpp"
|
2016-11-10 02:47:25 -06:00
|
|
|
#include <LOOLWebSocket.hpp>
|
2016-04-07 15:59:27 -05:00
|
|
|
|
|
|
|
using Poco::Net::SocketAddress;
|
|
|
|
using Poco::Net::HTTPServerParams;
|
|
|
|
|
|
|
|
/// Unit test stub for a server response
|
|
|
|
class UnitHTTPServerResponse : public Poco::Net::HTTPServerResponse
|
|
|
|
{
|
|
|
|
bool _sent;
|
2016-08-17 01:39:10 -05:00
|
|
|
std::stringstream _dummyStream;
|
2016-10-29 20:15:00 -05:00
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
public:
|
2016-10-29 20:15:00 -05:00
|
|
|
UnitHTTPServerResponse() :
|
|
|
|
_sent(false)
|
|
|
|
{
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
virtual void sendContinue() override {}
|
|
|
|
virtual std::ostream& send() override
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
_sent = true;
|
|
|
|
return _dummyStream;
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
virtual void sendFile(const std::string& /* path */,
|
|
|
|
const std::string& /* mediaType */) override {}
|
2017-01-13 06:52:08 -06:00
|
|
|
virtual void sendBuffer(const void* /* buffer */,
|
2016-04-07 15:59:27 -05:00
|
|
|
std::size_t /* length */) override {}
|
|
|
|
virtual void redirect(const std::string& /* uri */,
|
|
|
|
HTTPStatus /* status = HTTP_FOUND */) override {}
|
|
|
|
virtual void requireAuthentication(const std::string& /* realm */) override {}
|
|
|
|
virtual bool sent() const override { return _sent; }
|
|
|
|
};
|
|
|
|
|
2016-08-17 01:39:10 -05:00
|
|
|
/// Unit test stub for server params with a public dtor
|
|
|
|
class UnitHTTPServerParams : public Poco::Net::HTTPServerParams
|
|
|
|
{
|
|
|
|
public:
|
2016-10-29 20:15:00 -05:00
|
|
|
~UnitHTTPServerParams() {}
|
2016-08-17 01:39:10 -05:00
|
|
|
};
|
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
/// Unit test stub for a server request
|
|
|
|
class UnitHTTPServerRequest : public Poco::Net::HTTPServerRequest
|
|
|
|
{
|
2018-12-17 02:14:56 -06:00
|
|
|
private:
|
2016-10-29 20:15:00 -05:00
|
|
|
UnitHTTPServerResponse& _response;
|
2016-04-07 15:59:27 -05:00
|
|
|
Poco::Net::SocketAddress _clientAddress;
|
|
|
|
Poco::Net::SocketAddress _serverAddress;
|
2016-08-17 01:39:10 -05:00
|
|
|
std::stringstream _dummyStream;
|
|
|
|
UnitHTTPServerParams _dummyParams;
|
2016-10-29 20:15:00 -05:00
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
public:
|
2016-10-29 20:15:00 -05:00
|
|
|
UnitHTTPServerRequest(UnitHTTPServerResponse& inResponse,
|
|
|
|
const std::string& uri) :
|
|
|
|
_response(inResponse),
|
2019-03-30 09:06:16 -05:00
|
|
|
_serverAddress(9981) // FIXME: Unix Sockets now ...
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
setURI(uri);
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
virtual std::istream& stream() override
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
return _dummyStream;
|
|
|
|
}
|
2017-11-13 13:20:00 -06:00
|
|
|
#if POCO_VERSION < 0x01080000
|
2016-04-08 01:29:45 -05:00
|
|
|
virtual bool expectContinue() const override
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-08 01:29:45 -05:00
|
|
|
#endif
|
2017-11-13 13:20:00 -06:00
|
|
|
#if POCO_VERSION >= 0x01080000
|
2016-09-21 08:54:47 -05:00
|
|
|
virtual bool secure() const override
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-21 09:03:34 -05:00
|
|
|
#endif
|
2016-10-29 20:15:00 -05:00
|
|
|
virtual const SocketAddress& clientAddress() const override
|
|
|
|
{
|
|
|
|
return _clientAddress;
|
|
|
|
}
|
|
|
|
virtual const SocketAddress& serverAddress() const override
|
|
|
|
{
|
|
|
|
return _serverAddress;
|
|
|
|
}
|
|
|
|
virtual const HTTPServerParams& serverParams() const override
|
|
|
|
{
|
|
|
|
return _dummyParams;
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
virtual Poco::Net::HTTPServerResponse& response() const override
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
return _response;
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
};
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
namespace UnitHTTP
|
|
|
|
{
|
2018-05-22 02:12:29 -05:00
|
|
|
inline Poco::Net::HTTPClientSession* createSession()
|
2018-04-17 10:40:55 -05:00
|
|
|
{
|
|
|
|
// HTTP forced in configure hook.
|
|
|
|
return new Poco::Net::HTTPClientSession ("127.0.0.1",
|
|
|
|
ClientPortNumber);
|
|
|
|
}
|
2016-10-20 16:09:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class UnitWebSocket
|
|
|
|
{
|
2016-10-29 20:15:00 -05:00
|
|
|
Poco::Net::HTTPClientSession* _session;
|
2016-11-10 02:47:25 -06:00
|
|
|
LOOLWebSocket* _socket;
|
2016-10-29 20:15:00 -05:00
|
|
|
|
|
|
|
public:
|
2016-10-20 16:09:00 -05:00
|
|
|
/// Get a websocket connected for a given URL
|
2018-04-17 10:40:55 -05:00
|
|
|
UnitWebSocket(const std::string& docURL)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
UnitHTTPServerResponse response;
|
|
|
|
UnitHTTPServerRequest request(response, docURL);
|
|
|
|
|
|
|
|
_session = UnitHTTP::createSession();
|
|
|
|
|
|
|
|
// FIXME: leaking the session - hey ho ... do we need a UnitSocket ?
|
|
|
|
_socket = new LOOLWebSocket(*_session, request, response);
|
|
|
|
} catch (const Poco::Exception &ex) {
|
|
|
|
std::cerr << "Exception creating websocket " << ex.displayText() << std::endl;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:09:00 -05:00
|
|
|
~UnitWebSocket()
|
|
|
|
{
|
|
|
|
delete _socket;
|
|
|
|
delete _session;
|
|
|
|
}
|
2016-12-16 01:46:31 -06:00
|
|
|
|
2018-04-17 10:40:55 -05:00
|
|
|
LOOLWebSocket* getLOOLWebSocket() const
|
|
|
|
{
|
|
|
|
return _socket;
|
|
|
|
}
|
2016-10-20 16:09:00 -05:00
|
|
|
};
|
|
|
|
|
2016-04-08 03:08:41 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|