-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.cpp
391 lines (335 loc) · 10.8 KB
/
http.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
#include "weblegends.h"
#include "resource.h"
#include <functional>
#include <iostream>
#include <sstream>
#include "df/viewscreen_adopt_regionst.h"
#include "df/viewscreen_loadgamest.h"
#include "modules/Gui.h"
bool WebLegends::http(CActiveSocket *sock, std::string & request)
{
size_t ending = request.find('\n');
if (ending == std::string::npos)
{
return false;
}
size_t ending_size = 1;
if (ending != 0 && request.at(ending - 1) == '\r')
{
ending--;
ending_size++;
}
if (ending == 0)
{
request.erase(0, ending_size);
return true;
}
const static size_t min_length = strlen("GET / HTTP/1.0");
if (ending < min_length)
{
sock->Close();
return false;
}
std::string url;
std::string method = request.substr(0, 5) == "GET /" ? "GET" : request.substr(0, 6) == "HEAD /" ? "HEAD" : "";
char http1Point = request.at(ending - 1);
bool keepAlive = http1Point == '1';
if (!method.empty() && request.substr(method.length(), 2) == " /" && request.substr(ending - 9, 8) == " HTTP/1." && (http1Point == '0' || http1Point == '1'))
{
url = request.substr(method.length() + 1, ending - 10 - method.length());
}
while (true)
{
size_t start = ending + ending_size;
ending = request.find('\n', start);
if (ending == std::string::npos)
{
return false;
}
ending_size = 1;
if (request.at(ending - 1) == '\r')
{
ending--;
ending_size++;
}
if (request.substr(start, ending - start) == "Connection: keep-alive")
{
keepAlive = true;
}
else if (request.substr(start, ending - start) == "Connection: close")
{
keepAlive = false;
}
if (request.at(ending - 1) == '\n')
{
break;
}
}
request.erase(0, ending + ending_size);
if (url.empty())
{
sock->Close();
return false;
}
try
{
handle(sock, method, url, http1Point, keepAlive);
}
catch (...)
{
WEBLEGENDS_DEBUG_LOG << "[weblegends] exception in request: " << method << " " << url << " (" << sock->GetClientAddr() << ")" << std::endl;
throw;
}
if (!keepAlive)
{
sock->Close();
return false;
}
return true;
}
static void http_error(CActiveSocket *sock, const std::string & method, const std::string & url, int status_code, const std::string & status_phrase, const std::string & body, char http1Point, bool keepAlive, const std::string & extra_headers = std::string())
{
std::string header = stl_sprintf("HTTP/1.%c %d %s\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: %zu\r\nConnection: %s\r\n%s\r\n", http1Point, status_code, status_phrase.c_str(), body.length(), keepAlive ? "keep-alive" : "close", extra_headers.c_str());
std::string payload = method == "HEAD" ? header : (header + body);
if (sock->Send((const uint8_t *)payload.c_str(), payload.length()) != int32_t(payload.length()))
{
std::cerr << "weblegends send " << status_code << " " << url << ": " << sock->GetSocketError() << std::endl;
std::cerr << sock->DescribeError() << std::endl;
}
}
static void not_found(CActiveSocket *sock, const std::string & method, const std::string & url, char http1Point, bool keepAlive)
{
http_error(sock, method, url, 404, "Not Found", "not found: " + url, http1Point, keepAlive);
}
static bool check_page(Layout & l, const std::string & url, const std::string & prefix, std::function<bool(Layout &, int32_t)> handler)
{
if (url.length() <= prefix.length() || url.substr(0, prefix.length()) != prefix)
{
return false;
}
size_t end = 0;
int32_t page = -1;
try
{
page = std::stoi(url.substr(prefix.length()), &end);
}
catch (...)
{
return false;
}
if (end != url.length() - prefix.length())
{
return false;
}
if (page < 0)
{
return false;
}
return handler(l, page);
}
static bool check_id(Layout & l, const std::string & url, const std::string & prefix, std::function<bool(Layout &, int32_t, int32_t)> handler)
{
if (url.length() <= prefix.length() || url.substr(0, prefix.length()) != prefix)
{
return false;
}
size_t end = 0;
int32_t id = -1;
try
{
id = std::stoi(url.substr(prefix.length()), &end);
}
catch (...)
{
return false;
}
if (end == url.length() - prefix.length())
{
return handler(l, id, 0);
}
return check_page(l, url.substr(prefix.length() + end), "?page=", [handler, id](Layout & l2, int32_t page) -> bool
{
if (page <= 0)
{
return false;
}
return handler(l2, id, page);
});
}
static bool check_id_2(Layout & l, const std::string & url, const std::string & prefix1, const std::string & prefix2, std::function<bool(Layout &, int32_t, int32_t, int32_t)> handler)
{
if (url.length() <= prefix1.length() + prefix2.length() || url.substr(0, prefix1.length()) != prefix1)
{
return false;
}
size_t end = 0;
int32_t id1 = -1;
try
{
id1 = std::stoi(url.substr(prefix1.length()), &end);
}
catch (...)
{
return false;
}
return check_id(l, url.substr(prefix1.length() + end), prefix2, [handler, id1](Layout & l2, int32_t id2, int32_t page) -> bool
{
return handler(l2, id1, id2, page);
});
}
bool WebLegends::is_world_loaded()
{
if (!Core::getInstance().isWorldLoaded())
{
return false;
}
auto view = Gui::getCurViewscreen(true);
if (virtual_cast<df::viewscreen_adopt_regionst>(view))
{
return false;
}
if (virtual_cast<df::viewscreen_loadgamest>(view))
{
return false;
}
return true;
}
DECLARE_RESOURCE(faux_wikipedia_css);
bool WebLegends::request(weblegends_handler_v1 & response, const std::string & url)
{
Layout l;
bool builtin = true;
render_sidebar(l);
if (url == "/") { render_home(l); }
else if (check_page(l, url, "/ents-", render_entity_list)) {}
else if (check_id(l, url, "/ent-", render_entity)) {}
else if (check_page(l, url, "/figs-", render_figure_list)) {}
else if (check_id(l, url, "/fig-", render_figure)) {}
else if (check_page(l, url, "/items-", render_item_list)) {}
else if (check_id(l, url, "/item-", render_item)) {}
else if (check_page(l, url, "/regions-", render_region_list)) {}
else if (check_id(l, url, "/region-", render_region)) {}
else if (check_page(l, url, "/sites-", render_site_list)) {}
else if (check_id(l, url, "/site-", render_site)) {}
else if (check_id_2(l, url, "/site-", "/bld-", render_structure)) {}
else if (check_page(l, url, "/layers-", render_layer_list)) {}
else if (check_id(l, url, "/layer-", render_layer)) {}
else if (check_id(l, url, "/era-", render_era)) {}
else if (check_page(l, url, "/eventcols-", render_eventcol_list)) {}
else if (check_id(l, url, "/eventcol-", render_eventcol)) {}
else { builtin = false; }
if (builtin)
{
if (l.content.str().empty())
return false;
l.write_to(response);
return true;
}
if (url == "/style.css")
{
response.status_code() = 410;
response.status_description() = "Gone";
response.headers()["Content-Type"] = "text/css; charset=utf-8";
return true;
}
if (url == "/faux-wikipedia.css")
{
response.headers()["Content-Type"] = "text/css; charset=utf-8";
response.raw_out().write(faux_wikipedia_css.data(), faux_wikipedia_css.size());
return true;
}
if (url == "/region.png")
{
void render_region_map(std::ostream & s);
response.headers()["Content-Type"] = "image/png";
render_region_map(response.raw_out());
return true;
}
size_t pos = url.find_first_of("/?", 1);
std::string prefix, rest;
if (pos == std::string::npos)
{
prefix = url.substr(1);
rest = "";
}
else
{
prefix = url.substr(1, pos - 1);
rest = url.substr(pos);
}
CoreSuspender suspend;
auto handlers_1 = get_handlers_v1();
if (handlers_1 != nullptr)
{
auto handler = handlers_1->find(prefix);
if (handler != handlers_1->end())
{
return handler->second.second(response, rest);
}
}
auto handlers_0 = get_handlers_v0();
if (handlers_0 != nullptr)
{
auto handler = handlers_0->find(prefix);
if (handler != handlers_0->end())
{
std::ostringstream s;
handler->second.second(s, rest);
auto str = s.str();
if (str.empty())
return false;
response.cp437_out() << str;
return true;
}
}
return false;
}
void WebLegends::handle(CActiveSocket *sock, const std::string & method, const std::string & url, char http1Point, bool keepAlive)
{
if (method != "GET" && method != "HEAD")
{
http_error(sock, method, url, 405, "Method Not Allowed", "Method \"" + method + "\" not allowed.", http1Point, keepAlive, "Allow: GET, HEAD\r\n");
return;
}
if (!is_world_loaded())
{
http_error(sock, method, url, 503, "Service Unavailable", "No world loaded.", http1Point, keepAlive, "Retry-After: 5\r\nRefresh: 5\r\n");
return;
}
weblegends_handler_v1_impl response;
try
{
if (!request(response, url))
{
not_found(sock, method, url, http1Point, keepAlive);
return;
}
}
catch (...)
{
http_error(sock, method, url, 500, "Internal Server Error", "Error processing page.", http1Point, keepAlive);
throw;
}
std::ostringstream transport;
transport << "HTTP/1." << http1Point << " " << response.current_status_code << " " << response.current_status_description << "\r\n";
response.current_headers.erase("Content-Length");
response.current_headers.erase("Connection");
for (auto h : response.current_headers)
{
transport << h.first << ": " << h.second << "\r\n";
}
auto body = response.body_raw.str();
transport << "Content-Length: " << body.length() << "\r\n";
transport << "Connection: " << (keepAlive ? "keep-alive" : "close") << "\r\n";
transport << "\r\n";
if (method != "HEAD")
{
transport << body;
}
std::string transport_str = transport.str();
if (size_t(sock->Send((const uint8_t *)transport_str.c_str(), transport_str.length())) != transport_str.length())
{
std::cerr << "weblegends send " << response.current_status_code << " " << url << ": " << sock->GetSocketError() << std::endl;
std::cerr << sock->DescribeError() << std::endl;
}
}