-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSemanticHTTPServer.cpp
428 lines (364 loc) · 12.7 KB
/
SemanticHTTPServer.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#import "SemanticHTTPServer.hpp"
#import "Logging.hpp"
#import "SwiftCompleter.hpp"
#import "file_body.hpp"
#import <beast/core/handler_helpers.hpp>
#import <beast/core/handler_ptr.hpp>
#import <beast/core/placeholders.hpp>
#import <beast/core/streambuf.hpp>
#import <beast/http.hpp>
#import <boost/asio.hpp>
#import <boost/property_tree/json_parser.hpp>
#import <boost/property_tree/ptree.hpp>
#import <dispatch/dispatch.h>
#import <cstddef>
#import <cstdio>
#import <functional>
#import <iostream>
#import <map>
#import <memory>
#import <mutex>
#import <sstream>
#import <string>
#import <thread>
#import <utility>
using namespace ssvim;
static auto HeaderValueContentTypeJSON = "application/json";
static auto HeaderKeyContentType = "Content-Type";
static auto HeaderKeyServer = "Server";
static auto HeaderValueServer = "SSVIM";
using namespace beast::http;
namespace ssvim {
namespace http {
using socket_type = boost::asio::ip::tcp::socket;
using req_type = request<string_body>;
using resp_type = response<file_body>;
/**
* Session is an instance of an HTTP Session.
*
* The server will allocate a new instance for each accepted
* request.
*/
class Session;
using EndpointFn = std::function<void(std::shared_ptr<Session>)>;
class EndpointImpl : public std::enable_shared_from_this<EndpointImpl> {
EndpointFn _start;
public:
EndpointImpl(EndpointFn start);
void handleRequest(std::shared_ptr<Session> session);
};
using namespace ssvim;
EndpointImpl makeSlowTestEndpoint();
EndpointImpl makeStatusEndpoint();
EndpointImpl makeShutdownEndpoint();
EndpointImpl makeCompletionsEndpoint();
EndpointImpl makeDiagnosticsEndpoint();
response<string_body> notFoundResponse(req_type request);
response<string_body> errorResponse(req_type request, std::string message);
class Session : public std::enable_shared_from_this<Session> {
streambuf _streambuf;
socket_type _socket;
ServiceContext _context;
boost::asio::io_service::strand _strand;
req_type _request;
std::map<std::string, EndpointImpl> _endpoints;
EndpointImpl *_endpoint;
Logger _logger;
public:
Session(Session &&) = default;
Session(Session const &) = default;
Session &operator=(Session &&) = delete;
Session &operator=(Session const &) = delete;
Session(socket_type &&sock, ServiceContext ctx)
: _socket(std::move(sock)), _context(ctx),
_strand(_socket.get_io_service()), _logger(ctx.logLevel, "HTTP") {
_endpoint = NULL;
_logger.log(LogLevelInfo, "Secret:", _context.secret);
// Setup Endpoints.
// TODO: Perhaps this can be done statically
_endpoints = std::map<std::string, EndpointImpl>();
auto insert_endpoint = [&](std::string named, EndpointImpl impl) {
_endpoints.insert(std::pair<std::string, EndpointImpl>(named, impl));
};
insert_endpoint("/status", makeStatusEndpoint());
insert_endpoint("/shutdown", makeShutdownEndpoint());
insert_endpoint("/completions", makeCompletionsEndpoint());
insert_endpoint("/diagnostics", makeDiagnosticsEndpoint());
insert_endpoint("/slow_test", makeSlowTestEndpoint());
}
public:
void start() {
doRead();
}
Logger logger() {
return _logger;
}
std::shared_ptr<Session> detach() {
return shared_from_this();
}
void doRead() {
async_read(_socket, _streambuf, _request,
_strand.wrap(std::bind(&Session::onRead, shared_from_this(),
asio::placeholders::error)));
}
void onRead(error_code const &ec) {
_logger << "ONREAD";
if (ec)
return fail(ec, "read");
auto path = _request.url;
// Typical flow of handling a response
// - Detach and retain - necessary to keep this alive.
// - Quickly return to prevent from blocking acceptor loop.
// - Perform a long running task.
// - Schedule write for the response body
auto detachedSession = detach();
_logger << "WILL_READ: " << path;
auto endpointImpl = _endpoints.find(std::string(path));
if (endpointImpl != _endpoints.end()) {
_logger << "GOTEP:";
_endpoint = &endpointImpl->second;
_endpoint->handleRequest(detachedSession);
return;
}
// Schedule not found response
detachedSession->write(notFoundResponse(_request));
}
void onWrite(error_code ec) {
if (ec)
fail(ec, "write");
doRead();
}
#pragma mark - State
req_type request() {
return _request;
}
#pragma mark - Writing messages
// Schedule a write
void write(response<string_body> res) {
async_write(_socket, std::move(res),
std::bind(&Session::onWrite, shared_from_this(),
asio::placeholders::error));
}
void fail(error_code ec, std::string what) {
auto message = what + " and: " + ec.message();
_logger << message;
}
// Schedule an error message
void error(std::string message) {
auto res = errorResponse(_request, message);
async_write(_socket, std::move(res),
std::bind(&Session::onWrite, shared_from_this(),
asio::placeholders::error));
}
};
#pragma mark - Server
void SemanticHTTPServer::onAccept(error_code ec) {
if (!_acceptor.is_open()) {
return;
}
if (ec) {
std::cerr << ec.message() << "accept";
return;
}
socket_type sock(std::move(_socket));
_acceptor.async_accept(_socket, std::bind(&SemanticHTTPServer::onAccept, this,
asio::placeholders::error));
// Start a new Session.
auto session = std::make_shared<Session>(std::move(sock), _context);
session->start();
}
#pragma mark - Endpoint impl
EndpointImpl makeStatusEndpoint() {
return EndpointImpl([&](std::shared_ptr<Session> session) {
response<string_body> res;
res.status = 200;
res.version = session->request().version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType, HeaderValueContentTypeJSON);
prepare(res);
session->write(res);
});
}
EndpointImpl makeShutdownEndpoint() {
return EndpointImpl([&](std::shared_ptr<Session> session) {
session->logger() << "Recieved Shutdown Request";
response<string_body> res;
res.status = 200;
res.version = session->request().version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType, HeaderValueContentTypeJSON);
prepare(res);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
session->logger() << "Shutting down...";
exit(0);
});
session->write(res);
});
}
EndpointImpl::EndpointImpl(EndpointFn start) : _start(start) {
}
void EndpointImpl::handleRequest(std::shared_ptr<Session> session) {
auto logger = session->logger();
logger << "HANDLE_REQUEST";
logger << session->request().url;
// Assume we have a dispatch main queue running.
//
// Run the endpoint on the background thread
auto strongSelf = this;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
session->logger() << "_START_BACKGROUND";
// TODO: Exception safety
// Assume we have a dispatch main queue running.
strongSelf->_start(session);
});
}
using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;
ptree readJSONPostBody(std::string body) {
ptree pt;
std::istringstream is(body);
read_json(is, pt);
return pt;
}
template <typename T>
const std::vector<T> as_vector(ptree const &pt, ptree::key_type const &key) {
std::vector<T> r;
for (auto &item : pt.get_child(key))
r.push_back(item.second.get_value<T>());
return r;
}
// Make completions endpoint returns an endpoint that
// handles basic completion requests
//
// @param flags: an array of string flags
// @param contents: the current files
// @param line: the users line
// @param column: the users column
// @param file_name: the name of the users file
EndpointImpl makeCompletionsEndpoint() {
return EndpointImpl([&](std::shared_ptr<Session> session) {
// Parse in data
auto logger = session->logger();
auto bodyString = session->request().body;
logger << bodyString;
auto bodyJSON = readJSONPostBody(bodyString);
auto fileName = bodyJSON.get<std::string>("file_name");
auto column = bodyJSON.get<int>("column");
auto line = bodyJSON.get<int>("line");
auto contents = bodyJSON.get<std::string>("contents");
auto flags = as_vector<std::string>(bodyJSON, "flags");
logger << "file_name:" << fileName;
logger << "column:" << column;
logger << "line:" << line;
for (auto &f : flags) {
logger << "flags:" << f;
}
using namespace ssvim;
SwiftCompleter completer(session->logger().level());
auto files = std::vector<UnsavedFile>();
auto unsaved = UnsavedFile();
unsaved.contents = contents;
unsaved.fileName = fileName;
files.push_back(unsaved);
logger << "SEND_REQ";
auto candidates = completer.CandidatesForLocationInFile(
fileName, line, column, files, flags);
logger << "GOT_CANDIDATES";
session->logger().log(LogLevelExtreme, candidates);
// Build out response
response<string_body> res;
res.status = 200;
res.version = session->request().version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType, HeaderValueContentTypeJSON);
res.body = candidates;
prepare(res);
session->write(res);
});
}
// Make completions endpoint returns an endpoint that
// handles basic completion requests
//
// @param flags: an array of string flags
// @param contents: the current files
// @param file_name: the name of the users file
EndpointImpl makeDiagnosticsEndpoint() {
return EndpointImpl([&](std::shared_ptr<Session> session) {
// Parse in data
auto bodyString = session->request().body;
session->logger() << bodyString;
auto bodyJSON = readJSONPostBody(bodyString);
auto fileName = bodyJSON.get<std::string>("file_name");
auto contents = bodyJSON.get<std::string>("contents");
auto flags = as_vector<std::string>(bodyJSON, "flags");
session->logger() << "file_name:" << fileName;
for (auto &f : flags) {
session->logger().log(LogLevelInfo, "flags:", f);
}
using namespace ssvim;
SwiftCompleter completer(session->logger().level());
auto files = std::vector<UnsavedFile>();
auto unsaved = UnsavedFile();
unsaved.contents = contents;
unsaved.fileName = fileName;
files.push_back(unsaved);
session->logger() << "SEND_REQ";
auto diagnostics = completer.DiagnosticsForFile(fileName, files, flags);
session->logger() << "GOT_DIAGNOSTICS";
session->logger().log(LogLevelExtreme, diagnostics);
// Build out response
response<string_body> res;
res.status = 200;
res.version = session->request().version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType, HeaderValueContentTypeJSON);
res.body = diagnostics;
prepare(res);
session->write(res);
});
}
EndpointImpl makeSlowTestEndpoint() {
return EndpointImpl([](std::shared_ptr<Session> session) {
// Wait for 10 seconds to write hello world.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
session->logger() << "Enter main: ";
session->logger() << session->request().url;
response<string_body> res;
res.status = 200;
res.version = session->request().version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType,
HeaderValueContentTypeJSON);
res.body = "Hello World";
prepare(res);
session->write(res);
});
});
}
response<string_body> errorResponse(req_type request, std::string message) {
response<string_body> res;
res.status = 500;
res.reason = "Internal Error";
res.version = request.version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType, HeaderValueContentTypeJSON);
res.body = std::string{"An internal error occurred"} + message;
prepare(res);
return res;
}
response<string_body> notFoundResponse(req_type request) {
response<string_body> res;
res.status = 404;
res.reason = "Not Found";
res.version = request.version;
res.fields.insert(HeaderKeyServer, HeaderValueServer);
res.fields.insert(HeaderKeyContentType, HeaderValueContentTypeJSON);
res.body = "Endpoint: '" + request.url + "' not found";
prepare(res);
return res;
}
} // namespace http
} // namespace ssvim