-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathhttp.cpp
More file actions
147 lines (98 loc) · 3.45 KB
/
http.cpp
File metadata and controls
147 lines (98 loc) · 3.45 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
#include "http.hpp"
#include "modp_burl.h"
#include "modp_qsiter.h"
namespace msrv {
const char HttpHeader::CONTENT_TYPE[] = "Content-Type";
const char HttpHeader::CONTENT_LENGTH[] = "Content-Length";
const char HttpHeader::AUTHORIZATION[] = "Authorization";
const char HttpHeader::WWW_AUTHENTICATE[] = "Www-Authenticate";
const char HttpHeader::IF_NONE_MATCH[] = "If-None-Match";
const char HttpHeader::ETAG[] = "ETag";
const char HttpHeader::CACHE_CONTROL[] = "Cache-Control";
const char HttpHeader::ACCEPT_ENCODING[] = "Accept-Encoding";
const char HttpHeader::CONTENT_ENCODING[] = "Content-Encoding";
const char HttpHeader::LOCATION[] = "Location";
const char ContentType::APPLICATION_OCTET_STREAM[] = "application/octet-stream";
const char ContentType::APPLICATION_JSON[] = "application/json";
const char ContentType::APPLICATION_JAVASCRIPT[] = "application/javascript";
const char ContentType::TEXT_PLAIN_UTF8[] = "text/plain; charset=utf-8";
const char ContentType::TEXT_HTML_UTF8[] = "text/html; charset=utf-8";
const char ContentType::TEXT_CSS[] = "text/css";
const char ContentType::IMAGE_SVG[] = "image/svg+xml";
const char ContentType::IMAGE_JPEG[] = "image/jpeg";
const char ContentType::IMAGE_PNG[] = "image/png";
const char ContentType::IMAGE_GIF[] = "image/gif";
const char ContentType::IMAGE_BMP[] = "image/bmp";
const char ContentType::APPLICATION_WEB_MANIFEST[] = "application/manifest+json";
std::string toString(HttpMethod method)
{
switch (method)
{
case HttpMethod::UNDEFINED:
return "undefined";
case HttpMethod::GET:
return "GET";
case HttpMethod::POST:
return "POST";
default:
return "invalid";
}
}
std::string toString(HttpStatus status)
{
switch (status)
{
case HttpStatus::UNDEFINED:
return "Undefined";
case HttpStatus::S_200_OK:
return "200 OK";
case HttpStatus::S_202_ACCEPTED:
return "202 Accepted";
case HttpStatus::S_204_NO_CONTENT:
return "204 No content";
case HttpStatus::S_304_NOT_MODIFIED:
return "304 Not modified";
case HttpStatus::S_400_BAD_REQUEST:
return "400 Bad request";
case HttpStatus::S_401_UNAUTHORIZED:
return "401 Unauthorized";
case HttpStatus::S_403_FORBIDDEN:
return "403 Forbidden";
case HttpStatus::S_404_NOT_FOUND:
return "404 Not found";
case HttpStatus::S_405_METHOD_NOT_ALLOWED:
return "405 Method not allowed";
case HttpStatus::S_500_SERVER_ERROR:
return "500 Internal server error";
case HttpStatus::S_501_NOT_IMPLEMENTED:
return "501 Not implemented";
default:
return toString(static_cast<int>(status)) + "Status code";
}
}
std::string urlDecode(StringView str)
{
std::string output;
output.resize(modp_burl_decode_len(str.length()));
auto outputSize = modp_burl_decode(&output[0], str.data(), str.length());
output.resize(outputSize);
return output;
}
HttpKeyValueMap parseQueryString(StringView str)
{
HttpKeyValueMap map;
if (!str.empty() && str.front() == '?')
str = str.substr(1);
qsiter_t iter;
qsiter_reset(&iter, str.data(), str.length());
while (qsiter_next(&iter))
{
auto key = urlDecode(StringView(iter.key, iter.keylen));
if (key.empty())
continue;
auto value = urlDecode(StringView(iter.val, iter.vallen));
map.emplace(std::move(key), std::move(value));
}
return map;
}
}