-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.cpp
242 lines (205 loc) · 6.78 KB
/
http.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
#include "http.h"
#include <iostream>
using namespace std;
const char* FORBIDDEN_MSG = "405 Forbidden";
const int FORBIDDEN_LEN = 13;
const char* NOT_FOUND_MSG = "404 Not found";
const int NOT_FOUND_LEN = 13;
const char* CRLF = "\r\n";
const RequestInfo RequestInfo::BAD_REQUEST = RequestInfo(UNSUPPORTED, "");
std::map<std::string, const char*> CONTENTS = {{"html","text/html"}, {"css", "text/css"}, {"js", "application/javascript"},
{"jpeg", "image/jpeg"}, {"jpg", "image/jpeg"}, {"png", "image/png"},
{"gif", "image/gif"}, {"swf", "application/x-shockwave-flash"}
};
constexpr const char* ContentString[] = {"text/html", "text/css", "application/javascript", "image/jpeg",
"image/jpeg","image/png", "image/gif", "application/x-shockwave-flash"
"unknown"};
bool check_path_security(const string& path);
void urlDecode(char *dst, const char *src)
{
char a, b;
while (*src) {
if ((*src == '%') &&
((a = src[1]) && (b = src[2])) &&
(isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a'-'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a'-'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
*dst++ = 16*a+b;
src+=3;
} else {
*dst++ = *src++;
}
}
*dst++ = '\0';
}
RequestMethod stringToRequestMethod(const string& s) {
if(s == "GET") {
return GET;
} else if (s == "HEAD") {
return HEAD;
} else {
return UNSUPPORTED;
}
}
RequestInfo getRequestInfo(const string& line)
{
size_t mp = line.find(" ");
string method = string(line, 0, mp);
size_t pp = line.find(" ", mp+1);
string path = string(line, mp+1, pp-mp-1);
if (line.find(" ", pp+1) != string::npos) { // extra spaces in request
return RequestInfo::BAD_REQUEST;
} else {
return RequestInfo(stringToRequestMethod(method), path);
}
}
const char* statusMessgae(const Status& s)
{
switch(s) {
case OK:
return "200 OK";
case FORBIDDEN:
return "403 Forbidden";
case NOT_FOUND:
return "404 Not Found";
case BAD_REQUEST:
return "405 Method Not Allowed";
}
return "500 internal error";
}
inline const char* contentTypeString(const ContentType& t)
{
return ContentString[t];
}
ContentType getContentType(const string& path)
{
int point_pos = path.find_last_of('.');
string ext = path.substr(point_pos+1);
return ContentType(0);
}
const char* getMappedContentType(const string& path)
{
int point_pos = path.find_last_of('.');
string ext = path.substr(point_pos+1);
return CONTENTS[ext]; //to lower case!
}
inline void writeHeader(bufferevent *bev, Status s, const ContentType& t, int len)
{
evbuffer *output = bufferevent_get_output(bev);
char headers[]= "HTTP/1.1 %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Server: %s\r\n"
"Connection: %s\r\n"
"Date: %s\r\n";
const time_t timer = time(NULL);
evbuffer_add_printf(output, headers, statusMessgae(s), contentTypeString(t),
len, SERVER, CONNECTION, ctime(&timer));
}
inline void writeHeader(bufferevent *bev, Status s, const char* t, int len)
{
evbuffer *output = bufferevent_get_output(bev);
char headers[]= "HTTP/1.1 %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Server: %s\r\n"
"Connection: %s\r\n"
"Date: %s\r\n";
const time_t timer = time(NULL);
evbuffer_add_printf(output, headers, statusMessgae(s), t,
len, SERVER, CONNECTION, ctime(&timer));
}
void writeResponse(bufferevent *bev, const char* path, RequestMethod method)
{
evbuffer *output = bufferevent_get_output(bev);
string full_path = string(DOCUMENT_ROOT);
full_path.append(string(path));
if(!check_path_security(path)) {
writeHeader(bev, FORBIDDEN, HTML, FORBIDDEN_LEN);
evbuffer_add(output, FORBIDDEN_MSG, FORBIDDEN_LEN);
return;
}
bool index = false;
if (full_path[full_path.length()-1] == '/') { // directory
full_path.append("index.html");
index = true;
}
int fd = open(full_path.c_str(), O_NONBLOCK|O_RDONLY);
if (fd == -1) {
if (index) {
writeHeader(bev, FORBIDDEN, HTML, FORBIDDEN_LEN);
evbuffer_add(output, FORBIDDEN_MSG, FORBIDDEN_LEN);
} else {
writeHeader(bev, NOT_FOUND, HTML, NOT_FOUND_LEN);
evbuffer_add(output, NOT_FOUND_MSG, NOT_FOUND_LEN);
}
return;
}
struct stat st;
fstat(fd, &st);
writeHeader(bev, OK, getMappedContentType(full_path), st.st_size);
if(method == GET) {
evbuffer_add_file(output, fd, 0, st.st_size);
} else {
// ctime gives only /n so we'll add proper CRLF
evbuffer_add(output, CRLF, 2);
}
}
void writeBadRequest(bufferevent *bev)
{
evbuffer *output = bufferevent_get_output(bev);
char body[] = "405 Not Allowed\r\n";
writeHeader(bev, BAD_REQUEST, HTML, 17);
evbuffer_add(output, body, strlen(body));
}
void createResponse(bufferevent *bev)
{
evbuffer *in = bufferevent_get_input(bev);
size_t sz = 0;
RequestInfo rinf = getRequestInfo(string(evbuffer_readln(in, &sz, EVBUFFER_EOL_CRLF)));
size_t param_delim = rinf.path.find('?');
string raw_path = rinf.path.substr(0, param_delim);
char* path = new char[raw_path.length()+1]; // at most the same size + term\0
urlDecode(path, raw_path.c_str());
if(sz !=0) {
switch (rinf.method) {
case HEAD: case GET:
writeResponse(bev, path, rinf.method);
break;
case UNSUPPORTED :
writeBadRequest(bev);
}
}
delete[] path;
}
bool check_path_security(const string& path)
{
int len = path.length();
int i = 0;
int level = 0;
while(i < len) {
if(path[i] == '/'){
if(i < len - 2 && path[i+1] == '.' && path[i+2] == '.') {
--level;
i += 3;
} else {
++level;
++i;
}
} else {
++i;
}
if (level < 0) return false;
}
return true;
}