-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsedrequest_impl.cpp
151 lines (116 loc) · 3.16 KB
/
parsedrequest_impl.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
#include "parsedrequest_impl.h"
ParsedRequest_impl::ParsedRequest_impl(std::shared_ptr<Request> request):valid(false),errorCode(Settings::errors::NO_ERROR), request(request)
{
parse();
}
void ParsedRequest_impl::parse() throw(WrongRequestFormat)
{
// std::string str = "GET /stat.html HTTP/1.1\nHOST: localhost";
std::shared_ptr<std::string> query =request->getRequestText();
std::string firstLine = query->substr(0,query->find("\n"));
boost::smatch splited;
boost::regex re("([^ ]*) (.*) HTTP/(1.[10])");
firstLine = UriDecode(firstLine);
if(!boost::regex_search (firstLine,splited,re))
{
valid = false;
errorCode = Settings::errors::WRONG_REQUEST_FORMAT;
return;
}
method = splited[1];
url = splited[2];
http_version = splited[3];
std::string body = query->substr(query->find("\n")+1);
re.assign("(.*): (.+?)$");
while (boost::regex_search (body,splited,re)) {
requestFields.insert({splited[1],splited[2]});
body = splited.suffix().str();
}
try
{
validate();
}
catch(ForbiddenSimbols ex)
{
}
}
void ParsedRequest_impl::validate() throw (BadMethod, ForbiddenSimbols)
{
{
boost::regex re("%[0-9a-fA-F]{2}");
if (boost::regex_search (url,re))
{
valid = false;
throw ForbiddenSimbols();
}
}
if(Settings::ALLOWED_METHODS.find(method) == Settings::ALLOWED_METHODS.end())
{
valid = false;
//throw BadMethod();
}
valid = true;
}
std::string ParsedRequest_impl::getField(std::string key)
{
auto keyValue = requestFields.find(key);
if (keyValue != requestFields.end())
{
return (*keyValue).second;
}
return "";
}
std::string ParsedRequest_impl::getMethod()
{
return method;
}
std::string ParsedRequest_impl::getUrl()
{
return url;
}
std::string ParsedRequest_impl::getDecodedUrl()
{
return UriDecode(url);
}
std::string ParsedRequest_impl::getHttpVersion()
{
return http_version;
}
std::shared_ptr<Request> ParsedRequest_impl::getRequest()
{
return request;
}
std::string ParsedRequest_impl::UriDecode(const std::string & sSrc)
{
// Note from RFC1630: "Sequences which start with a percent
// sign but are not followed by two hexadecimal characters
// (0-9, A-F) are reserved for future extension"
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const int SRC_LEN = sSrc.length();
const unsigned char * const SRC_END = pSrc + SRC_LEN;
// last decodable '%'
const unsigned char * const SRC_LAST_DEC = SRC_END - 2;
char * const pStart = new char[SRC_LEN];
char * pEnd = pStart;
while (pSrc < SRC_LAST_DEC)
{
if (*pSrc == '%')
{
char dec1, dec2;
if (-1 != (dec1 = HEX2DEC[*(pSrc + 1)])
&& -1 != (dec2 = HEX2DEC[*(pSrc + 2)]))
{
*pEnd++ = (dec1 << 4) + dec2;
pSrc += 3;
continue;
}
}
*pEnd++ = *pSrc++;
}
// the last 2- chars
while (pSrc < SRC_END)
*pEnd++ = *pSrc++;
std::string sResult(pStart, pEnd);
delete [] pStart;
return sResult;
}