2017-02-14 16:57:03 -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"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <Poco/Net/HTMLForm.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/FilePartSource.h>
|
|
|
|
#include <Poco/Net/SSLManager.h>
|
2017-02-16 08:14:12 -06:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2017-02-14 16:57:03 -06:00
|
|
|
#include <Poco/Net/KeyConsoleHandler.h>
|
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
#include <Poco/StreamCopier.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/Util/HelpFormatter.h>
|
|
|
|
#include <Poco/Util/Option.h>
|
|
|
|
#include <Poco/Util/OptionSet.h>
|
2017-02-16 05:43:30 -06:00
|
|
|
#include <Poco/Runnable.h>
|
|
|
|
#include <Poco/Thread.h>
|
2017-02-14 16:57:03 -06:00
|
|
|
|
2017-02-19 11:06:45 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2017-02-14 16:57:03 -06:00
|
|
|
using Poco::Net::HTTPClientSession;
|
|
|
|
using Poco::Net::HTTPRequest;
|
|
|
|
using Poco::Net::HTTPResponse;
|
2017-02-16 08:14:12 -06:00
|
|
|
using Poco::Net::WebSocket;
|
2017-02-14 16:57:03 -06:00
|
|
|
using Poco::Runnable;
|
|
|
|
using Poco::Thread;
|
|
|
|
using Poco::Util::Application;
|
|
|
|
|
2017-02-16 04:14:08 -06:00
|
|
|
const char *HostName = "127.0.0.1";
|
2017-02-17 17:55:10 -06:00
|
|
|
constexpr int HttpPortNumber = 9191;
|
|
|
|
constexpr int SslPortNumber = 9193;
|
2017-02-14 16:57:03 -06:00
|
|
|
|
2017-02-17 19:40:17 -06:00
|
|
|
static bool EnableHttps = false;
|
|
|
|
|
2017-02-16 04:14:08 -06:00
|
|
|
struct Session
|
2017-02-14 16:57:03 -06:00
|
|
|
{
|
2017-02-16 04:14:08 -06:00
|
|
|
std::string _session_name;
|
|
|
|
Poco::Net::HTTPClientSession *_session;
|
|
|
|
|
|
|
|
Session(const char *session_name, bool https = false)
|
|
|
|
: _session_name(session_name)
|
2017-02-14 16:57:03 -06:00
|
|
|
{
|
|
|
|
if (https)
|
2017-02-17 17:55:10 -06:00
|
|
|
_session = new Poco::Net::HTTPSClientSession(HostName, SslPortNumber);
|
2017-02-14 16:57:03 -06:00
|
|
|
else
|
2017-02-17 17:55:10 -06:00
|
|
|
_session = new Poco::Net::HTTPClientSession(HostName, HttpPortNumber);
|
2017-02-16 04:14:08 -06:00
|
|
|
}
|
2017-02-16 05:52:22 -06:00
|
|
|
~Session()
|
|
|
|
{
|
|
|
|
delete _session;
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:14:08 -06:00
|
|
|
void sendPing(int i)
|
|
|
|
{
|
|
|
|
Poco::Net::HTTPRequest request(
|
|
|
|
Poco::Net::HTTPRequest::HTTP_POST,
|
|
|
|
"/ping/" + _session_name + "/" + std::to_string(i));
|
2017-02-14 16:57:03 -06:00
|
|
|
try {
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
|
|
|
form.prepareSubmit(request);
|
2017-02-16 04:14:08 -06:00
|
|
|
form.write(_session->sendRequest(request));
|
2017-02-14 16:57:03 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to write data: " << e.name() <<
|
|
|
|
" " << e.message() << "\n";
|
2017-02-16 04:14:08 -06:00
|
|
|
throw;
|
2017-02-14 16:57:03 -06:00
|
|
|
}
|
2017-02-16 04:14:08 -06:00
|
|
|
}
|
2017-02-18 13:49:46 -06:00
|
|
|
|
|
|
|
std::string getResponseString()
|
2017-02-16 04:14:08 -06:00
|
|
|
{
|
2017-02-14 16:57:03 -06:00
|
|
|
Poco::Net::HTTPResponse response;
|
2017-02-18 13:49:46 -06:00
|
|
|
std::istream& responseStream = _session->receiveResponse(response);
|
|
|
|
const std::string result(std::istreambuf_iterator<char>(responseStream), {});
|
|
|
|
// std::cerr << "Got response '" << result << "'\n";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getResponseInt()
|
|
|
|
{
|
|
|
|
int number = 42;
|
2017-02-14 16:57:03 -06:00
|
|
|
|
|
|
|
try {
|
2017-02-16 05:43:30 -06:00
|
|
|
// std::cerr << "try to get response\n";
|
2017-02-18 13:49:46 -06:00
|
|
|
const std::string result = getResponseString();
|
2017-02-16 05:24:07 -06:00
|
|
|
number = std::stoi(result);
|
2017-02-14 16:57:03 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception converting: " << e.name() <<
|
|
|
|
" " << e.message() << "\n";
|
2017-02-16 04:14:08 -06:00
|
|
|
throw;
|
2017-02-14 16:57:03 -06:00
|
|
|
}
|
2017-02-16 04:14:08 -06:00
|
|
|
return number;
|
|
|
|
}
|
2017-02-16 08:14:12 -06:00
|
|
|
|
|
|
|
std::shared_ptr<WebSocket> getWebSocket()
|
|
|
|
{
|
|
|
|
_session->setTimeout(Poco::Timespan(10, 0));
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
|
|
|
|
HTTPResponse response;
|
|
|
|
return std::shared_ptr<WebSocket>(
|
|
|
|
new WebSocket(*_session, request, response));
|
|
|
|
}
|
2017-02-16 04:14:08 -06:00
|
|
|
};
|
|
|
|
|
2017-02-16 05:43:30 -06:00
|
|
|
struct ThreadWorker : public Runnable
|
|
|
|
{
|
|
|
|
const char *_domain;
|
2017-02-16 21:04:45 -06:00
|
|
|
ThreadWorker(const char *domain = nullptr)
|
|
|
|
: _domain(domain)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void run()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 100; ++i)
|
2017-02-16 05:43:30 -06:00
|
|
|
{
|
2017-02-17 19:40:17 -06:00
|
|
|
Session ping(_domain ? _domain : "init", EnableHttps);
|
2017-02-16 21:04:45 -06:00
|
|
|
ping.sendPing(i);
|
2017-04-05 04:57:28 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
int back =
|
|
|
|
#endif
|
|
|
|
ping.getResponseInt();
|
2017-02-16 21:04:45 -06:00
|
|
|
assert(back == i + 1);
|
2017-02-16 05:43:30 -06:00
|
|
|
}
|
2017-02-16 21:04:45 -06:00
|
|
|
}
|
2017-02-16 05:43:30 -06:00
|
|
|
};
|
|
|
|
|
2017-02-16 04:14:08 -06:00
|
|
|
struct Client : public Poco::Util::Application
|
|
|
|
{
|
2017-02-17 19:40:17 -06:00
|
|
|
void testPing()
|
|
|
|
{
|
|
|
|
std::cerr << "testPing\n";
|
|
|
|
Session first("init", EnableHttps);
|
|
|
|
Session second("init", EnableHttps);
|
|
|
|
|
|
|
|
int count = 42, back;
|
|
|
|
first.sendPing(count);
|
|
|
|
second.sendPing(count + 1);
|
|
|
|
|
2017-02-18 13:49:46 -06:00
|
|
|
back = first.getResponseInt();
|
2017-02-17 19:40:17 -06:00
|
|
|
std::cerr << "testPing: " << back << "\n";
|
|
|
|
assert (back == count + 1);
|
|
|
|
|
2017-02-18 13:49:46 -06:00
|
|
|
back = second.getResponseInt();
|
2017-02-17 19:40:17 -06:00
|
|
|
std::cerr << "testPing: " << back << "\n";
|
|
|
|
assert (back == count + 2);
|
|
|
|
}
|
|
|
|
|
2017-02-16 05:43:30 -06:00
|
|
|
void testLadder()
|
|
|
|
{
|
2017-02-17 19:40:17 -06:00
|
|
|
std::cerr << "testLadder\n";
|
2017-02-16 05:52:22 -06:00
|
|
|
ThreadWorker ladder;
|
2017-02-16 05:43:30 -06:00
|
|
|
Thread thread;
|
|
|
|
thread.start(ladder);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2017-02-16 05:52:22 -06:00
|
|
|
void testParallel()
|
|
|
|
{
|
2017-02-17 19:40:17 -06:00
|
|
|
std::cerr << "testParallel\n";
|
2017-02-16 05:52:22 -06:00
|
|
|
const int num = 10;
|
|
|
|
Thread snakes[num];
|
|
|
|
ThreadWorker ladders[num];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num; i++)
|
|
|
|
snakes[i].start(ladders[i]);
|
|
|
|
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
snakes[i].join();
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:06:45 -06:00
|
|
|
void testWebsocketPingPong()
|
2017-02-16 08:14:12 -06:00
|
|
|
{
|
2017-02-19 11:06:45 -06:00
|
|
|
std::cerr << "testWebsocketPingPong\n";
|
2017-02-17 19:40:17 -06:00
|
|
|
Session session("ws", EnableHttps);
|
2017-02-16 08:14:12 -06:00
|
|
|
std::shared_ptr<WebSocket> ws = session.getWebSocket();
|
2017-02-17 18:58:49 -06:00
|
|
|
|
|
|
|
std::string send = "hello there";
|
|
|
|
ws->sendFrame(&send[0], send.length(),
|
|
|
|
WebSocket::SendFlags::FRAME_TEXT);
|
|
|
|
|
2017-02-16 08:14:12 -06:00
|
|
|
for (size_t i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
ws->sendFrame(&i, sizeof(i), WebSocket::SendFlags::FRAME_BINARY);
|
|
|
|
size_t back[5];
|
|
|
|
int flags = 0;
|
2017-04-05 04:57:28 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
int recvd =
|
|
|
|
#endif
|
|
|
|
ws->receiveFrame((void *)back, sizeof(back), flags);
|
2017-02-16 08:14:12 -06:00
|
|
|
assert(recvd == sizeof(size_t));
|
|
|
|
assert(back[0] == i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:06:45 -06:00
|
|
|
void testWebsocketEcho()
|
|
|
|
{
|
|
|
|
std::cerr << "testwebsocketEcho\n";
|
|
|
|
Session session("ws", EnableHttps);
|
|
|
|
std::shared_ptr<WebSocket> ws = session.getWebSocket();
|
|
|
|
|
|
|
|
std::vector<char> res;
|
2017-02-24 16:18:38 -06:00
|
|
|
|
|
|
|
std::srand(std::time(0));
|
|
|
|
|
|
|
|
std::vector<char> data;
|
2017-02-19 11:06:45 -06:00
|
|
|
for (size_t i = 1; i < (1 << 14); ++i)
|
|
|
|
{
|
2017-02-24 16:18:38 -06:00
|
|
|
data.push_back((char)(std::rand() / (RAND_MAX/256)));
|
2017-02-19 11:06:45 -06:00
|
|
|
ws->sendFrame(data.data(), data.size(), WebSocket::SendFlags::FRAME_BINARY);
|
|
|
|
|
|
|
|
res.resize(i);
|
|
|
|
int flags;
|
2017-04-05 04:57:28 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
int recvd =
|
|
|
|
#endif
|
|
|
|
ws->receiveFrame(res.data(), res.size(), flags);
|
2017-02-19 11:06:45 -06:00
|
|
|
assert(recvd == static_cast<int>(i));
|
|
|
|
|
|
|
|
if (i == sizeof(size_t))
|
|
|
|
{
|
|
|
|
assert(*reinterpret_cast<const size_t*>(res.data()) ==
|
|
|
|
*reinterpret_cast<const size_t*>(data.data()) + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(res == data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:14:08 -06:00
|
|
|
public:
|
2017-02-17 17:55:10 -06:00
|
|
|
int main(const std::vector<std::string>& args) override
|
2017-02-16 04:14:08 -06:00
|
|
|
{
|
2017-02-17 19:40:17 -06:00
|
|
|
EnableHttps = (args.size() > 0 && args[0] == "ssl");
|
|
|
|
std::cerr << "Starting " << (EnableHttps ? "HTTPS" : "HTTP") << " client." << std::endl;
|
2017-02-17 17:55:10 -06:00
|
|
|
|
2017-02-17 20:09:43 -06:00
|
|
|
if (EnableHttps)
|
|
|
|
{
|
|
|
|
Poco::Net::initializeSSL();
|
|
|
|
// Just accept the certificate anyway for testing purposes
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
|
2017-02-18 12:01:43 -06:00
|
|
|
Poco::Net::Context::Params sslParams;
|
|
|
|
Poco::Net::Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
|
|
|
|
Poco::Net::SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext);
|
2017-02-17 20:09:43 -06:00
|
|
|
}
|
|
|
|
|
2017-02-19 11:06:45 -06:00
|
|
|
testWebsocketPingPong();
|
|
|
|
testWebsocketEcho();
|
2017-02-16 04:14:08 -06:00
|
|
|
|
2017-02-17 19:40:17 -06:00
|
|
|
testPing();
|
2017-02-17 18:58:49 -06:00
|
|
|
testLadder();
|
|
|
|
testParallel();
|
2017-02-16 05:43:30 -06:00
|
|
|
|
2017-02-14 16:57:03 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
POCO_APP_MAIN(Client)
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|