-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultrule.cpp
189 lines (162 loc) · 6.93 KB
/
defaultrule.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
#include "defaultrule.h"
DefaultRule::DefaultRule()
{
}
DefaultRule::~DefaultRule()
{
}
bool DefaultRule::isMatching(std::string url)
{
return true;
}
bool DefaultRule::isFinal()
{
return true;
}
void DefaultRule::operateRequest(std::shared_ptr<ParsedRequest> parsedRequest)
{
boost::regex hasQueryString(".+[?].*");
std::string filePart;
boost::smatch match;
//removing query string
if(boost::regex_search(parsedRequest->getDecodedUrl(),match,hasQueryString))
{
boost::regex filePart_re("(^.+?)[?].*$");
boost::regex_search(parsedRequest->getDecodedUrl(),match,filePart_re);
filePart = match[1];
}
else
filePart = parsedRequest->getDecodedUrl();
if(filePart[0]!='/')
filePart = "/"+filePart;
//Removing /../ sequences
std::string filePath = Settings::ROOT + filePart;
boost::regex escape_re("\/?[^\/^(..)]*/../");
while(boost::regex_search(filePath,match,escape_re))
filePath=boost::regex_replace(filePath,escape_re,"/");
boost::filesystem::path path(filePath);
if(parsedRequest->getMethod()=="GET")
{
if(filePath.find(Settings::ROOT)==std::string::npos)
{
std::shared_ptr<ResponseBlank> response = getResponseBuilder()->getResponse403();
std::shared_ptr<std::string> content(new std::string("403. Distanation is Forbidden: " + parsedRequest->getUrl()));
response->setContentType("text/html");
response->setContent(content);
sendResponse(parsedRequest, response);
}
else if(boost::filesystem::is_directory(path))
{
auto last = filePath.end();
last--;
char s = *last;
if(s!='/')
path+= boost::filesystem::path("/index.html");
else
path+= boost::filesystem::path("index.html");
if(!boost::filesystem::exists(path))
{
std::shared_ptr<ResponseBlank> response = getResponseBuilder()->getResponse403();
std::shared_ptr<std::string> content(new std::string("403. Forbidden: "+ parsedRequest->getUrl()));
response->setContentType("text/html");
response->setContent(content);
sendResponse(parsedRequest, response);
}
sendFile(path, parsedRequest);
}
else
{
sendFile(path, parsedRequest);
}
}
else
{
std::shared_ptr<ResponseBlank> response = (getResponseBuilder())->getResponse405();
std::shared_ptr<std::string> content(new std::string("405. Method not allowed: "+ parsedRequest->getUrl()));
response->setContent(content);
response->setContentType("text/html");
sendResponse(parsedRequest, response);
}
}
void DefaultRule::sendResponse(std::shared_ptr<ParsedRequest> parsedRequest, std::shared_ptr<Response> response)
{
bufferevent_lock(parsedRequest->getRequest()->getOutputBuffer());
bufferevent_write(parsedRequest->getRequest()->getOutputBuffer(),response->getHeaderText()->c_str(),response->getHeaderText()->length());
bufferevent_write(parsedRequest->getRequest()->getOutputBuffer(),response->getContentText()->c_str(),response->getContentText()->length());
bufferevent_write(parsedRequest->getRequest()->getOutputBuffer(),"\r\n\r\n",4);
bufferevent_enable( parsedRequest->getRequest()->getOutputBuffer(), (EV_WRITE ) );
bufferevent_flush(parsedRequest->getRequest()->getOutputBuffer(),EV_WRITE,BEV_FINISHED);
bufferevent_unlock(parsedRequest->getRequest()->getOutputBuffer());
}
void DefaultRule::sendFile(boost::filesystem::path path, std::shared_ptr<ParsedRequest> parsedRequest)
{
if(boost::filesystem::exists(path))
{
if(boost::filesystem::is_regular_file(path))
{
if(boost::filesystem::file_size(path)<Settings::MAX_FILE_SIZE)
{
std::ifstream is(path.string(), std::ifstream::binary);
if(is)
{
std::shared_ptr<ResponseBlank> response = (getResponseBuilder())->getResponse200();
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
std::shared_ptr<std::string> content(new std::string);
content->reserve(length);
char* buffer = new char[length];
memset(buffer,0,length);
is.read (buffer, length);
*content = std::move(std::string(buffer,length));
response->setContent(content);
boost::regex ext_re("(\.[a-z]+)([?].*)?$");
boost::smatch matcher;
boost::regex_search(path.string(),matcher,ext_re);
std::string ext = matcher[0].str();
if(ext == ".html")
response->setContentType("text/html");
else if(ext == ".css")
response->setContentType("text/css");
else if(ext == ".js")
response->setContentType("text/javascript");
else if(ext == ".png")
response->setContentType("image/png");
else if(ext == ".gif")
response->setContentType("image/gif");
else if(ext == ".swf")
response->setContentType("application/x-shockwave-flash");
else if(ext == ".jpeg" || ext==".jpg")
response->setContentType("image/jpeg");
else
response->setContentType("text/plain");
sendResponse(parsedRequest, response);
}
}
else
{
std::shared_ptr<ResponseBlank> response = (getResponseBuilder())->getResponse405();
std::shared_ptr<std::string> content(new std::string("405. Method not allowed: "+ parsedRequest->getUrl()));
response->setContent(content);
response->setContentType("text/html");
sendResponse(parsedRequest, response);
}
}
else
{
std::shared_ptr<ResponseBlank> response = (getResponseBuilder())->getResponse405();
std::shared_ptr<std::string> content(new std::string("405. Method not allowed: "+ parsedRequest->getUrl()));
response->setContent(content);
response->setContentType("text/html");
sendResponse(parsedRequest, response);
}
}
else
{
std::shared_ptr<ResponseBlank> response = getResponseBuilder()->getResponse404();
std::shared_ptr<std::string> content(new std::string("404. Page not found: "+ parsedRequest->getUrl()));
response->setContentType("text/html");
response->setContent(content);
sendResponse(parsedRequest, response);
}
}