forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect.cpp
245 lines (213 loc) · 7.37 KB
/
Connect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* 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 <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <mutex>
#include <sysexits.h>
#include <thread>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/Context.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/InvalidCertificateHandler.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/SharedPtr.h>
#include <Poco/TemporaryFile.h>
#include <Poco/URI.h>
#include <Poco/Util/Application.h>
#include <Common.hpp>
#include <Protocol.hpp>
#include "COOLWebSocket.hpp"
#include <Log.hpp>
#include <Util.hpp>
using namespace COOLProtocol;
using Poco::Net::AcceptCertificateHandler;
using Poco::Net::Context;
#if ENABLE_SSL
using Poco::Net::HTTPSClientSession;
#else
using Poco::Net::HTTPClientSession;
#endif
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::InvalidCertificateHandler;
using Poco::Net::SSLManager;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
using Poco::Runnable;
using Poco::SharedPtr;
using Poco::TemporaryFile;
using Poco::URI;
using Poco::Util::Application;
static bool closeExpected = false;
static std::mutex coutMutex;
constexpr auto Name = "connect ";
/// Prints incoming data from a COOLWebSocket.
class Output : public Runnable
{
public:
Output(COOLWebSocket& ws) :
_ws(ws)
{
}
void run() override
{
int flags;
int n;
try
{
do
{
char buffer[100000];
n = _ws.receiveFrame(buffer, sizeof(buffer), flags, Name);
if (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE)
{
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Got " << COOLWebSocket::getAbbreviatedFrameDump(buffer, n, flags) << std::endl;
}
std::string firstLine = getFirstLine(buffer, n);
StringVector tokens(StringVector::tokenize(firstLine, ' '));
if (std::getenv("DISPLAY") != nullptr && tokens.equals(0, "tile:"))
{
TemporaryFile pngFile;
std::ofstream pngStream(pngFile.path(), std::ios::binary);
pngStream.write(buffer + firstLine.size() + 1, n - firstLine.size() - 1);
pngStream.close();
if (std::system((std::string("display ") + pngFile.path()).c_str()) == -1)
{
// Not worth it to display a warning, this is just a throwaway test program, and
// the developer running it surely notices if nothing shows up...
}
}
}
}
while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "CLOSE frame received" << std::endl;
}
if (!closeExpected)
Util::forcedExit(EX_SOFTWARE);
}
catch (WebSocketException& exc)
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Got exception " << exc.message() << std::endl;
}
}
private:
COOLWebSocket& _ws;
};
/// Program for interactive or scripted testing of a cool server.
class Connect: public Poco::Util::Application
{
public:
Connect() :
#if ENABLE_SSL
_uri("https://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER) + "/ws")
#else
_uri("http://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER) + "/ws")
#endif
{
}
protected:
int main(const std::vector<std::string>& args) override
{
if (args.size() < 1)
{
LOG_ERR("Usage: connect documentURI [serverURI]");
return EX_USAGE;
}
if (args.size() > 1)
_uri = URI(args[1]);
#if ENABLE_SSL
Poco::Net::initializeSSL();
SharedPtr<InvalidCertificateHandler> invalidCertHandler = new AcceptCertificateHandler(false);
Context::Params sslParams;
Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext);
HTTPSClientSession cs(_uri.getHost(), _uri.getPort());
#else
HTTPClientSession cs(_uri.getHost(), _uri.getPort());
#endif
std::string encodedUri;
URI::encode(args[0], ":/?", encodedUri);
HTTPRequest request(HTTPRequest::HTTP_GET, "/cool/" + encodedUri + "/ws");
HTTPResponse response;
COOLWebSocket ws(cs, request, response);
ws.setReceiveTimeout(0);
std::thread thread([&ws]{Output(ws).run();});
while (true)
{
std::string line;
std::getline(std::cin, line);
if (std::cin.eof())
{
break;
}
else if (line.find("sleep ") == 0)
{
// Accept an input line "sleep <n>" that makes us sleep a number of seconds.
long sleepTime = std::stol(line.substr(std::string("sleep").length()));
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Sleeping " << sleepTime << " seconds" << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
}
else if (line == "exit")
{
// While hacking on COOL and editing input files for this program back and forth it
// is a good idea to be able to add an enforced exit in the middle of the input
// file.
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Exiting" << std::endl;
}
break;
}
else if (line.find('#') == 0)
{
// Also comments can be useful in input files for this program
}
else
{
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Sending: '" << line << '\'' << std::endl;
}
ws.sendFrame(line.c_str(), line.size());
}
}
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Shutting down websocket" << std::endl;
}
closeExpected = true;
ws.shutdown(Name);
thread.join();
return EX_OK;
}
private:
URI _uri;
};
namespace Util
{
void alertAllUsers(const std::string& cmd, const std::string& kind)
{
std::cout << "error: cmd=" << cmd << " kind=" << kind << std::endl;
}
}
POCO_APP_MAIN(Connect)
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */