-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.cpp
246 lines (230 loc) · 5.43 KB
/
request.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
// junky parsing
#include "request.hpp"
#include <cstddef>
#include <cstdlib>
#include <exception>
#include <map>
#include <sys/_types/_size_t.h>
#include <utility>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include "server.hpp"
extern std::string err;
std::map<std::string, std::string> encode;
size_t toHex(std::string hex){
std::stringstream tmp;
size_t res;
tmp << std::hex << hex;
tmp >> res;
return res;
}
// remove boundary + headers from the body
// handle chunked req && convert size from hex to int and recv the body && remove the boundary
void initializeEncode(){
encode["%20"] = " ";
encode["%21"] = "!";
encode["%22"] = "\"";
encode["%23"] = "#";
encode["%24"] = "$";
encode["%25"] = "%";
encode["%26"] = "&";
encode["%27"] = "\'";
encode["%28"] = "(";
encode["%29"] = ")";
encode["%2A"] = "*";
encode["%2B"] = "+";
encode["%2C"] = ",";
encode["%2F"] = "/";
encode["%3A"] = ":";
encode["%3B"] = ";";
encode["%3D"] = "=";
encode["%3F"] = "?";
encode["%40"] = "@";
encode["%5B"] = "[";
encode["%5D"] = "]";
}
request::request(const std::string &req){
if (err != "")
return;
rawReq = req;
}
request::~request(){}
std::string request::getMethod(){
return method;
}
std::string request::getPath(){
return path;
}
std::string request::getVersion(){
return version;
}
std::multimap<std::string, std::string>& request::getHeader(){
return headers;
}
std::string request::getBody(){
return body;
}
std::string request::getQuery(){
return query;
}
bool request::checkVerion(){
if(version != "HTTP/1.1"){
err = "505";
return true;
}
return false;
}
void request::urlDecoding(){
size_t start = 0;
while ((start = path.find("%", start)) != path.npos) {
path.replace(start, 3, encode[path.substr(start, 3)]);
}
}
bool request::checkPath(){
if (path.size() > 2048)
return true; //status code 414
for (size_t i = 0; path[i]; ++i){
if ((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z') || (path[i] >= '0' && path[i] <= '9'))
continue; //400
if (path[i] == '~' || path[i] == '!' || (path[i] >= '#' && path[i] <= '/') || path[i] == ':' || path[i] == ';' || path[i] == '=' || path[i] == '?' || path[i] == '@')
continue;
if (path[i] == '[' || path[i] == ']' || path[i] == '_')
continue;
err = "400";
return true;
}
size_t start = path.find("?");
if (start != path.npos){
query = path.substr(start + 1, path.size() - (start + 1));
path = path.substr(0, start);
}
return false;
}
bool request::checkMethod(){
if (method != "GET" && method != "POST" && method != "DELETE"){
err = "501";
return true;
}
return false;
}
bool request::checkHeaders(){
if (headers.find("Transfer-Encoding") != headers.end()){
if (headers.find("Transfer-Encoding")->second.find("chunked") == std::string::npos){
err = "501";
return true;
}
}
if (headers.find("Host") == headers.end()){
err = "400";
return true;
}
if (method == "POST"){
if (headers.find("Content-Length") == headers.end() && headers.find("Transfer-Encoding") == headers.end()){
err = "400";
return true;
}
}
return false;
}
void request::parseBody(){
std::string boundary;
std::multimap<std::string, std::string>::iterator it = headers.find("Content-Type");
if (it == headers.end())
return;
std::string header = it->second;
size_t start = header.find("boundary");
if (start != std::string::npos){
boundary = header.substr(start + 9, header.find("\r", start + 9));
it = headers.find("Transfer-Encoding");
if (it == headers.end())
return;
header = it->second;
if (header.find("chunked") != std::string::npos){
std::string tmpBody = "";
std::string tmp;
long long chunk = 0;
size_t start = 0;
long long end =0;
while (1) {
start = body.find("\r\n", end);
if (start == std::string::npos){
err = "500";
return;
}
tmp = body.substr(end, start - end);
chunk = toHex(tmp.c_str());
if (chunk == 0)
break;
tmpBody.append(body.substr(start + 2, chunk));//
end += chunk + tmp.size() + 4;
}
body = tmpBody;
}
}
}
size_t request::parseHeaders(size_t start){
size_t end, ret;
std::string tmp;
end = rawReq.find("\r\n\r\n", start);
ret = end;
tmp = rawReq.substr(start, end);
std::stringstream tab(tmp);
std::string str;
while (getline(tab, tmp))
{
end = tmp.find(":", 0);
if (end == tmp.npos)
break;
headers.insert(std::make_pair(tmp.substr(0, end) , tmp.substr(end + 2, tmp.size() - (end + 3))));
}
return ret;
}
void request::parse(Server &serv){
if (err != "")
return;
try {
size_t i = 0, start = 0;
std::string tmp;
i = rawReq.find(" ", 0);
if (i == rawReq.npos){
err = "400";
return;
}
method = rawReq.substr(0, i);
start = rawReq.find(" ", i + 1);
if (start == rawReq.npos){
err = "400";
return;
}
path = rawReq.substr(i + 1, start - i - 1);
i = rawReq.find("\r", start + 1);
if (i == rawReq.npos){
err = "400";
return;
}
version = rawReq.substr(start + 1, i - start - 1);
if (checkVerion())
return;
if (checkMethod())
return;
if (checkPath())
return;
urlDecoding();
start = parseHeaders(i + 2);
if (checkHeaders())
return;
body = rawReq.substr(start+ 4, rawReq.size() - start);
if((long long)body.size() > serv.maxBodySize){
err = "413";
return;
}
parseBody();
} catch (std::exception &) {
err = "500";
return;
}
}