This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlibhttp_v2.hpp
More file actions
570 lines (408 loc) · 16.3 KB
/
Copy pathlibhttp_v2.hpp
File metadata and controls
570 lines (408 loc) · 16.3 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
// #include "cpplibs/libsocket.hpp"
#include "../cpplibs/libstrmanip.hpp"
template<typename T>
static size_t vector_indexOf(const std::vector<T>& vec, const T& t) {
auto it = std::find(vec.begin(), vec.end(), t);
return it != vec.end() ? std::distance(vec.begin(), it) : -1;
}
namespace http {
enum HTTPMethod {
HTTP_GET,
HTTP_HEAD,
HTTP_POST,
HTTP_PUT,
HTTP_DELETE,
HTTP_CONNECT,
HTTP_PATCH,
HTTP_TRACE
};
std::map<std::string, std::string> contentType = { {"html", "text/html"}, {"htm", "text/html"}, {"txt", "text/plain"}, {"ico", "image/x-icon"}, {"css", "text/css"}, {"js", "text/javascript"}, {"jpg", "image/jpeg"},{"png", "image/png"}, {"gif", "image/gif"}, {"mp3", "audio/mpeg"}, {"ogg", "audio/ogg"}, {"wav", "audio/wav"}, {"opus", "audio/opus"}, {"m4a", "audio/mp4"}, {"mp4", "video/mp4"}, {"webm", "video/webm"}, {"pdf", "application/pdf"}, {"json", "text/json"}, {"xml", "text/xml"}, {"other", "application/octet-stream"} };
std::map<int, std::string> strcode = { {200, "OK"}, {206, "Partial Content"}, {400, "Bad Request"}, {403, "Forbidden"}, {404, "Not Found"}, {500, "Internal Server Error"}, {501, "Not Implemented"} };
vector_string implMethods = { "GET", "HEAD", "POST", "PUT" };
bool error_flag = false;
struct __http_param_seq {
std::string paramName;
bool is_named;
};
struct __http_param_arg {
std::string paramName;
std::string argName;
std::string argExtraValue;
bool extraValue;
};
class HTTPHeader {
std::string name;
vector_string positionalParams;
std::map<std::string, std::string> namedParams;
std::string nonStandardParams;
std::vector<__http_param_seq> paramSeq;
std::vector<__http_param_arg> paramArgs;
bool null;
public:
HTTPHeader() {};
HTTPHeader(bool _null) : null(_null) {}
std::string getName() {
return name;
}
void setName(std::string _name) {
name = _name;
}
bool hasParam(std::string param) {
return std::find(positionalParams.begin(), positionalParams.end(), param) != positionalParams.end();
}
bool hasNamedParam(std::string param) {
return namedParams.find(param) != namedParams.end();
}
std::string getNamedParam(std::string param) {
auto it = namedParams.find(param);
if (it != namedParams.end()) return it->second;
return {};
}
std::string operator[](int index) {
if (index < 0 || index >= positionalParams.size()) return {};
return positionalParams[index];
}
std::string operator[](std::string key) {
return getNamedParam(key);
}
void addNamedParam(std::string key, std::string value) {
for (auto i : value) namedParams[key].push_back(i);
paramSeq.push_back({key, true});
setNull(false);
}
void addParam(std::string param) {
replaceAll(param, ", ", ",");
replaceAll(param, "; ", ";");
vector_string param_arg = split(param, ";");
param = param_arg.front();
param_arg.erase(param_arg.begin());
vector_string tmp = split(param, '=');
if (tmp.size() > 1) addNamedParam(tmp.front(), tmp.back());
else {
positionalParams.push_back(param);
paramSeq.push_back({param, false});
setNull(false);
}
if (param_arg.size()) {
for (auto i : param_arg) {
__http_param_arg arg;
arg.paramName = tmp.front();
vector_string tmp2 = split(i, "=");
arg.argName = tmp2.front();
arg.argExtraValue = tmp2.back();
arg.extraValue = tmp2.size() > 1;
addParamArg(arg);
}
}
}
void addParamRaw(std::string param) {
nonStandardParams = param;
}
std::string getParamRaw() {
return nonStandardParams;
}
void addParamArg(__http_param_arg arg) {
paramArgs.push_back(arg);
}
std::string findParamArg(std::string paramName, std::string argName) {
for (auto i : paramArgs) if (i.paramName == paramName && i.argName == argName) return i.argExtraValue;
return {};
}
bool IsNull() {
return null;
}
void setNull(bool n) {
null = n;
}
std::vector<__http_param_seq> __get_param_seq() {
return paramSeq;
}
std::vector<__http_param_arg> __get_param_arg() {
return paramArgs;
}
};
using HTTPHeaders = std::vector<HTTPHeader>;
class HTTPRequest {
HTTPMethod method;
std::string uri;
std::string version;
HTTPHeaders headers;
std::string body;
public:
void setVersion(std::string v) { version = v; }
void setMethod(HTTPMethod m) { method = m; }
void setURI(std::string u) { uri = u; }
void setBody(std::string b) { body = b; }
void addHeader(std::string name, vector_string params) {
HTTPHeader header;
header.setName(name);
for (auto i : params) header.addParam(i);
headers.push_back(header);
}
void addHeader(std::string name, std::string params) {
addHeader(name, split(params, ","));
}
void addHeader(std::string header) {
vector_string tmp = split(header, ":");
addHeader(tmp.front(), tmp.back());
}
void addHeaderRaw(std::string name, std::string param) {
HTTPHeader header;
header.setName(name);
header.addParamRaw(param);
headers.push_back(header);
}
void removeHeader(std::string name) {
for (size_t i = 0; i < headers.size(); i++) if (toLower(headers[i].getName()) == toLower(name)) headers.erase(headers.begin() + i);
}
HTTPMethod getMethod() const {
return method;
}
std::string getURI() const {
return uri;
}
std::string getVersion() const {
return version;
}
std::string getBody() const {
return body;
}
HTTPHeader findHeader(std::string name) {
for (auto i : headers) if (toLower(i.getName()) == toLower(name)) return i;
return true;
}
HTTPHeaders& getHeaders() {
return headers;
}
};
class HTTPResponse {
int code;
std::string version;
HTTPHeaders headers;
std::string body;
public:
void setCode(int c) { code = c; }
void setVersion(std::string v) { version = v; }
void setBody(std::string b) { body = b; }
void addHeader(std::string name, vector_string params) {
HTTPHeader header;
header.setName(name);
for (auto i : params) header.addParam(i);
headers.push_back(header);
}
void addHeader(std::string name, std::string params) {
addHeader(name, split(params, ","));
}
void addHeader(std::string header) {
vector_string tmp = split(header, ":");
addHeader(tmp.front(), tmp.back());
}
void addHeaderRaw(std::string name, std::string param) {
HTTPHeader header;
header.setName(name);
header.addParamRaw(param);
headers.push_back(header);
}
void removeHeader(std::string name) {
for (size_t i = 0; i < headers.size(); i++) if (toLower(headers[i].getName()) == toLower(name)) headers.erase(headers.begin() + i);
}
int getCode() const {
return code;
}
std::string getVersion() const {
return version;
}
std::string getBody() const {
return body;
}
HTTPHeader findHeader(std::string name) {
for (auto i : headers) if (toLower(i.getName()) == toLower(name)) return i;
HTTPHeader ret;
ret.setNull(true);
return ret;
}
HTTPHeaders& getHeaders() {
return headers;
}
};
std::map<std::string, std::string> parseHttpUriParams(std::string str) {
std::map<std::string, std::string> ret;
std::string strparams = split(str, "?", 1)[1];
for (auto& i : split(strparams, "&")) {
auto tmp = split(i, "=", 1);
ret[tmp[0]] = tmp[1];
}
return ret;
}
// HTTPHeaderValue parse_http_header_params(std::string params) {
// HTTPHeaderValue ret;
// if (params.front() == '{') {
// HTTPHeaderParam param;
// param.value1 = params;
// param.valid = true;
// ret.push_back(param);
// return ret;
// }
// // while (params.find(' ') != std::string::npos) params.erase(params.find(' '), 1);
// vector_string str_args = split(params, ",");
// for (std::string& i : str_args) {
// HTTPHeaderParam param;
// param.valid = true;
// vector_string param_arg = split(i, ";", 1);
// vector_string _param_tmp = split(param_arg.back(), "=", 1);
// param.args[_param_tmp.front()] = _param_tmp.back();
// vector_string param_tmp = split(param_arg.front(), "=", 1);
// param.value1 = param_tmp.front();
// if (param_tmp.size() > 1) param.value2 = param_tmp.back();
// ret.push_back(param);
// }
// return ret;
// }
HTTPRequest parseHttpRequest(std::string str) {
error_flag = false;
HTTPRequest ret;
vector_string head_body = split(str, "\r\n\r\n", 1);
if (head_body.size() < 2) {
error_flag = true;
return {};
}
vector_string temp_head = split(head_body.front(), "\r\n", 1);
if (temp_head.size() < 2) {
error_flag = true;
return {};
}
vector_string head = split(temp_head.front(), " ");
if (head.size() < 3 || vector_indexOf(implMethods, head[0]) < 0 || split(head[2], "/").front() != "HTTP") {
error_flag = true;
return {};
}
ret.setMethod((HTTPMethod)vector_indexOf(implMethods, head[0]));
ret.setURI(head[1]);
ret.setVersion(head[2]);
if (head_body.size() > 1) ret.setBody(head_body.back());
vector_string headers = split(temp_head.back(), "\r\n");
for (std::string& i : headers) {
if (!i.size()) continue;
vector_string header_temp = split(i, ": ", 1);
if (header_temp.size() < 2 || !header_temp.back().size()) {
error_flag = true;
return {};
}
if (header_temp.front() == "User-Agent") ret.addHeaderRaw(header_temp.front(), header_temp.back());
else ret.addHeader(header_temp.front(), header_temp.back());
}
return ret;
}
HTTPResponse parseHttpResponse(std::string str) {
error_flag = false;
HTTPResponse ret;
vector_string head_body = split(str, "\r\n\r\n", 1);
if (head_body.size() < 2) {
error_flag = true;
return {};
}
vector_string temp_head = split(head_body.front(), "\r\n", 1);
if (temp_head.size() < 2) {
error_flag = true;
return {};
}
vector_string head = split(temp_head.front(), " ");
while (head.size() > 2) head.erase(head.end());
if (split(head.front(), "/").front() != "HTTP" || !std::all_of(head.back().begin(), head.back().end(), ::isdigit)) {
error_flag = true;
return {};
}
ret.setVersion(head.front());
ret.setCode(stoi(head.back()));
ret.setBody(head_body.back());
vector_string headers = split(temp_head.back(), "\r\n");
for (std::string& i : headers) {
if (!i.size()) continue;
vector_string header_temp = split(i, ": ", 1);
if (header_temp.size() < 2 || !header_temp.back().size()) {
error_flag = true;
return {};
}
if (header_temp.front() == "last-modified") ret.addHeaderRaw(header_temp.front(), header_temp.back());
else if (header_temp.front() == "date") ret.addHeaderRaw(header_temp.front(), header_temp.back());
else if (header_temp.back().front() == '{') ret.addHeaderRaw(header_temp.front(), header_temp.back());
else ret.addHeader(header_temp.front(), header_temp.back());
}
error_flag = false;
return ret;
}
std::string dumpHttpHeaderParams(HTTPHeader value) {
if (value.getParamRaw().size()) return value.getParamRaw();
std::string ret;
for (auto i : value.__get_param_seq()) {
ret += i.paramName;
if (i.is_named) ret += "=" + value.getNamedParam(i.paramName);
for (auto j : value.__get_param_arg()) {
if (j.paramName != i.paramName) continue;
if (!j.extraValue) ret += ";" + j.argName;
else ret += ";" + j.argName + "=" + j.argExtraValue;
}
ret += ",";
}
ret.erase(ret.end() - 1);
return ret;
}
std::string dumpHttpRequest(HTTPRequest request) {
std::string ret;
ret += strformat("%s %s %s\r\n", implMethods[request.getMethod()].c_str(), request.getURI().c_str(), request.getVersion().c_str());
for (auto i : request.getHeaders())ret += strformat("%s: %s\r\n", i.getName().c_str(), dumpHttpHeaderParams(i).c_str());
ret += "\r\n";
ret += request.getBody();
return ret;
}
std::string dumpHttpResponse(HTTPResponse response) {
std::string ret;
ret += strformat("%s %d %s\r\n", response.getVersion().c_str(), response.getCode(), strcode[response.getCode()].c_str());
for (auto i : response.getHeaders())ret += strformat("%s: %s\r\n", i.getName().c_str(), dumpHttpHeaderParams(i).c_str());
ret += "\r\n";
ret += response.getBody();
return ret;
}
// HTTPResponse http_request(std::string url) {
// // std::function<void(int, short)>
// return {};
// }
// struct HTTPFile {
// std::string name;
// std::string data;
// };
// struct HTTPMultipartFormData {
// std::map<std::string, std::string> formData;
// std::map<std::string, HTTPFile> formDataFiles;
// HTTPHeaders headers;
// };
// struct HTTPEndpointRequest : public HTTPRequest {
// std::map<std::string, std::string> endpointParams;
// std::map<std::string, HTTPFile> endpointFiles;
// };
// struct HTTPEndpointResponse : public HTTPResponse {
// std::map<std::string, std::string> endpointParams;
// };
// HTTPMultipartFormData parse_http_multipart_form_data(std::string data, std::string boundary) {
// HTTPMultipartFormData ret;
// vector_string parts = split(data, "--" + boundary);
// if (parts.size()) parts.pop_back();
// for (std::string& i : parts) {
// vector_string head = split(i, "\r\n\r\n", 1);
// vector_string part_headers = split(head.front(), "\r\n");
// for (std::string& i : part_headers) {
// vector_string header_temp = split(i, ": ", 1);
// ret.headers[header_temp.front()] = parse_http_header_params(header_temp.back());
// }
// if (!findHeaderParam("form-data", ret.headers.at("Content-Disposition")).valid) throw std::runtime_error("parse_http_multipart_form_data(): only requests supported");
// }
// }
// class HTTPEndpointServer {
// // Socket server_socket;
// };
};