forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigParser.cpp
More file actions
526 lines (401 loc) · 12.6 KB
/
ConfigParser.cpp
File metadata and controls
526 lines (401 loc) · 12.6 KB
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "ConfigParser.h"
#include "ServerApplicationSettings.h"
#include "Utils.h"
#include <iostream>
#include <fstream>
namespace HttpServer
{
/**
* Config - include file
*/
bool ConfigParser::includeConfigFile(const std::string &fileName, std::string &strBuf, const std::size_t offset)
{
std::ifstream file(fileName);
if ( ! file)
{
file.close();
std::cout << "Error: " << fileName << " - cannot be open;" << std::endl;
return false;
}
file.seekg(0, std::ifstream::end);
std::streamsize file_size = file.tellg();
file.seekg(0, std::ifstream::beg);
const std::streamsize file_size_max = 2048 * 1024;
if (file_size_max < file_size)
{
file.close();
std::cout << "Error: " << fileName << " - is too large; max include file size = " << file_size_max << " bytes;" << std::endl;
return false;
}
if (file_size)
{
std::vector<std::string::value_type> buf(file_size);
file.read(reinterpret_cast<char *>(buf.data() ), file_size);
strBuf.insert(strBuf.begin() + offset, buf.cbegin(), buf.cend() );
}
file.close();
return true;
}
/**
* Config - add application
*/
bool ConfigParser::addApplication(
const std::unordered_map<std::string, std::string> &app,
const ServerApplicationDefaultSettings &defaults,
std::vector<Module> &modules,
ServerApplicationsTree &apps_tree
)
{
auto it_name = app.find("server_name");
if (app.cend() == it_name)
{
std::cout << "Error: application parameter 'server_name' is not specified;" << std::endl;
return false;
}
std::vector<std::string> names;
const std::string whitespace(" \t\n\v\f\r");
const std::string &app_name = it_name->second;
size_t delimiter = app_name.find_first_of(whitespace);
if (delimiter)
{
size_t cur_pos = 0;
while (std::string::npos != delimiter)
{
std::string name = app_name.substr(cur_pos, delimiter - cur_pos);
names.emplace_back(std::move(name) );
cur_pos = app_name.find_first_not_of(whitespace, delimiter + 1);
delimiter = app_name.find_first_of(whitespace, cur_pos);
}
std::string name = app_name.substr(cur_pos);
names.emplace_back(std::move(name) );
}
auto it_port = app.find("listen");
if (app.cend() == it_port)
{
std::cout << "Error: application port is not set;" << std::endl;
return false;
}
auto it_root_dir = app.find("root_dir");
if (app.cend() == it_root_dir || it_root_dir->second.empty() )
{
std::cout << "Error: application parameter 'root_dir' is not specified;" << std::endl;
return false;
}
auto it_module = app.find("server_module");
if (app.cend() == it_module)
{
std::cout << "Error: application parameter 'server_module' is not specified;" << std::endl;
return false;
}
// TODO: get module realpath
Module module(it_module->second);
if (false == module.is_open() )
{
std::cout << "Error: module '" << it_module->second << "' cannot be open;" << std::endl;
return false;
}
void *(*addr)(void *) = nullptr;
if (false == module.find("application_call", &addr) )
{
std::cout << "Error: function 'application_call' not found in module '" << it_module->second << "';" << std::endl;
return false;
}
std::function<int(server_request *, server_response *)> app_call = reinterpret_cast<int(*)(server_request *, server_response *)>(addr);
if ( ! app_call)
{
std::cout << "Error: invalid function 'application_call' in module '" << it_module->second << "';" << std::endl;
return false;
}
if (false == module.find("application_clear", &addr) )
{
std::cout << "Error: function 'application_clear' not found in module '" << it_module->second << "';" << std::endl;
return false;
}
std::function<void(Utils::raw_pair [], const size_t)> app_clear = reinterpret_cast<void(*)(Utils::raw_pair [], const size_t)>(addr);
std::function<bool()> app_init = std::function<bool()>();
if (module.find("application_init", &addr) )
{
app_init = reinterpret_cast<bool(*)()>(addr);
}
std::function<void()> app_final = std::function<void()>();
if (module.find("application_final", &addr) )
{
app_final = reinterpret_cast<void(*)()>(addr);
}
bool success = true;
try
{
if (app_init)
{
success = app_init();
}
}
catch (...)
{
success = false;
}
if (false == success)
{
std::cout << "Warning: error when initializing application '" << it_module->second << "';" << std::endl;
return false;
}
auto it_temp_dir = app.find("temp_dir");
const std::string temp_dir = app.cend() != it_temp_dir ? it_temp_dir->second : defaults.temp_dir;
auto it_request_max_size = app.find("request_max_size");
const size_t request_max_size = app.cend() != it_request_max_size ? std::strtoull(it_request_max_size->second.c_str(), nullptr, 10) : defaults.request_max_size;
auto it_module_update = app.find("server_module_update");
const std::string module_update = app.cend() != it_module_update ? it_module_update->second : "";
// Calculate module index
size_t module_index = std::numeric_limits<size_t>::max();
for (size_t i = 0; i < modules.size(); ++i)
{
if (modules[i] == module)
{
module_index = i;
break;
}
}
if (std::numeric_limits<size_t>::max() == module_index)
{
module_index = modules.size();
modules.emplace_back(std::move(module) );
}
std::string root_dir = it_root_dir->second;
#ifdef WIN32
if ('\\' == root_dir.back() )
{
root_dir.pop_back();
}
#endif
// Remove back slash from root_dir
if ('/' == root_dir.back() )
{
root_dir.pop_back();
}
// Create application settings struct
ServerApplicationSettings *sets = new ServerApplicationSettings {
std::strtol(it_port->second.c_str(), nullptr, 10),
root_dir,
temp_dir,
request_max_size,
module_index,
it_module->second,
module_update,
app_call,
app_clear,
app_init,
app_final
};
// Add application names in tree
if (names.empty() )
{
apps_tree.addApplication(app_name, sets);
}
else
{
for (size_t i = 0; i < names.size(); ++i)
{
apps_tree.addApplication(names[i], sets);
}
}
return true;
}
/**
* @brief ConfigParser::parseMimes
* @param fileName
* @param mimes_types
* @return bool
*/
bool ConfigParser::parseMimes(const std::string &fileName, std::unordered_map<std::string, std::string> &mimes_types)
{
std::ifstream file(fileName);
if ( ! file)
{
file.close();
std::cout << "Error: " << fileName << " - cannot be open;" << std::endl;
return false;
}
file.seekg(0, std::ifstream::end);
std::streamsize file_size = file.tellg();
file.seekg(0, std::ifstream::beg);
const std::streamsize file_size_max = 2048 * 1024;
if (file_size_max < file_size)
{
file.close();
std::cout << "Error: " << fileName << " - is too large; max file size = " << file_size_max << " bytes;" << std::endl;
return false;
}
const std::string whitespace(" \t\v\f\r");
std::vector<std::string::value_type> buf(file_size);
file.read(reinterpret_cast<char *>(buf.data() ), file_size);
const std::string str_buf(buf.cbegin(), buf.cend() );
size_t cur_pos = 0;
size_t end_pos = str_buf.find('\n', cur_pos);
while (std::string::npos != end_pos)
{
cur_pos = str_buf.find_first_not_of(whitespace, cur_pos);
size_t delimiter = str_buf.find_first_of(whitespace, cur_pos);
if (delimiter < end_pos)
{
std::string mime_type = str_buf.substr(cur_pos, delimiter - cur_pos);
if ('#' != mime_type.front() )
{
delimiter = str_buf.find_first_not_of(whitespace, delimiter);
if (delimiter < end_pos)
{
std::string ext = str_buf.substr(delimiter, end_pos - delimiter);
delimiter = ext.find_first_of(whitespace);
if (std::string::npos != delimiter)
{
for (size_t ext_pos = 0; std::string::npos != ext_pos; )
{
std::string ext_unit = ext.substr(ext_pos, std::string::npos != delimiter ? delimiter - ext_pos : std::string::npos);
if (false == ext_unit.empty() )
{
mimes_types.emplace(std::move(ext_unit), mime_type);
}
ext_pos = ext.find_first_not_of(whitespace, delimiter);
delimiter = ext.find_first_of(whitespace, ext_pos);
}
}
else
{
mimes_types.emplace(std::move(ext), std::move(mime_type) );
}
}
}
}
cur_pos = end_pos + 1;
end_pos = str_buf.find('\n', cur_pos);
}
return true;
}
/**
* Config - parse
*/
bool ConfigParser::loadConfig(
const std::string &conf_file_name,
std::unordered_map<std::string, std::string> &settings,
std::unordered_map<std::string, std::string> &mimes_types,
std::vector<Module> &modules,
ServerApplicationsTree &apps_tree
)
{
std::string str_buf;
if (false == includeConfigFile(conf_file_name, str_buf) )
{
return false;
}
std::vector<std::unordered_map<std::string, std::string> > applications;
const std::string whitespace(" \t\n\v\f\r");
size_t cur_pos = 0;
size_t end_pos = str_buf.find(';', cur_pos);
size_t block_pos = 0;
while (std::string::npos != end_pos)
{
block_pos = str_buf.find('{', cur_pos);
if (end_pos < block_pos)
{
cur_pos = str_buf.find_first_not_of(whitespace, cur_pos);
size_t delimiter = str_buf.find_first_of(whitespace, cur_pos);
if (delimiter < end_pos)
{
std::string param_name = str_buf.substr(cur_pos, delimiter - cur_pos);
cur_pos = str_buf.find_first_not_of(whitespace, delimiter + 1);
delimiter = str_buf.find_last_not_of(whitespace, end_pos);
std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos);
if ("include" == param_name)
{
this->includeConfigFile(param_value, str_buf, end_pos + 1);
}
else
{
settings.emplace(std::move(param_name), std::move(param_value) );
}
}
cur_pos = end_pos + 1;
}
else if (std::string::npos != block_pos)
{
cur_pos = str_buf.find_first_not_of(whitespace, cur_pos);
size_t delimiter = str_buf.find_first_of(whitespace, cur_pos);
const std::string block_type_name = str_buf.substr(cur_pos, delimiter - cur_pos);
delimiter = str_buf.find_first_not_of(whitespace, delimiter);
cur_pos = block_pos + 1;
size_t block_end = str_buf.find('}', cur_pos);
if (delimiter == block_pos)
{
if ("server" == block_type_name)
{
std::unordered_map<std::string, std::string> app;
end_pos = str_buf.find(';', cur_pos);
while (block_end > end_pos)
{
cur_pos = str_buf.find_first_not_of(whitespace, cur_pos);
delimiter = str_buf.find_first_of(whitespace, cur_pos);
if (delimiter < end_pos)
{
std::string param_name = str_buf.substr(cur_pos, delimiter - cur_pos);
cur_pos = str_buf.find_first_not_of(whitespace, delimiter + 1);
delimiter = str_buf.find_last_not_of(whitespace, end_pos);
std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos);
if ("include" == param_name)
{
cur_pos = end_pos + 1;
this->includeConfigFile(param_value, str_buf, cur_pos);
block_end = str_buf.find('}', cur_pos);
}
else
{
app.emplace(std::move(param_name), std::move(param_value) );
}
}
cur_pos = end_pos + 1;
end_pos = str_buf.find(';', cur_pos);
}
applications.emplace_back(std::move(app) );
}
else
{
std::cout << "Warning: " << block_type_name << " - unknown block type;" << std::endl;
}
}
else
{
std::cout << "Warning: after " << block_type_name << " expected '{' ;" << std::endl;
}
cur_pos = block_end + 1;
}
end_pos = str_buf.find(';', cur_pos);
}
auto it_mimes = settings.find("mimes");
if (settings.cend() != it_mimes)
{
this->parseMimes(it_mimes->second, mimes_types);
}
else
{
std::cout << "Warning: mime types file is not set in configuration;" << std::endl;
}
if (false == applications.empty() )
{
auto it_default_temp_dir = settings.find("default_temp_dir");
const std::string default_temp_dir = settings.cend() != it_default_temp_dir ? it_default_temp_dir->second : System::getTempDir();
auto it_default_request_max_size = settings.find("request_max_size");
const size_t default_request_max_size = settings.cend() != it_default_request_max_size ? std::strtoull(it_default_request_max_size->second.c_str(), nullptr, 10) : 0;
ServerApplicationDefaultSettings defaults {
default_temp_dir,
default_request_max_size
};
for (auto &app : applications)
{
this->addApplication(app, defaults, modules, apps_tree);
}
}
if (apps_tree.empty() )
{
std::cout << "Notice: server does not contain applications;" << std::endl;
}
return true;
}
};