-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpparser.cpp
184 lines (155 loc) · 4.82 KB
/
httpparser.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
#include "httpparser.h"
#include "configuration.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cstdlib>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
HttpParser::HttpParser()
{
}
evbuffer* HttpParser::setRequest(char *req)
{
//parse first line of request:
char* method;
method = strtok(req, " ");
if(method == NULL || (strcmp(method, "GET") != 0 && strcmp(method, "HEAD") != 0)) {
return (new Response(405))->getRawResponse();
}
char* file;
file = strtok(NULL, " ");
if(file == NULL) {
return (new Response(400))->getRawResponse();
}
char* decoded = (char*) calloc(1000, sizeof(char));
urldecode(decoded, 1000, file);
file = decoded;
//remove GET params
char* index = strrchr(file, '?');
if(index != NULL) {
index[0] = '\0';
}
char* path = (char *)malloc(1000);
strcpy(path, Configuration::DOCUMENT_ROOT);
strcat(path, file);
bool isindex = false;
if(file[strlen(file) - 1] == '/') {
strcat(path, Configuration::INDEX_FILE);
isindex = true;
}
//check extension
char* extension = 0;
int i = strlen(path) - 1;
while(path[i] != '/') {
--i;
if(path[i] == '.') {
extension = (char*) calloc(strlen(path) - i, sizeof(char));
strcpy(extension, path + i + 1);
break;
}
}
if(strstr(path, "/../") != NULL) {
free(path);
free(decoded);
return (new Response(404))->getRawResponse();
}
int fd = open (path, O_RDONLY);
if(fd < 0) {
free(path);
free(decoded);
if(isindex) {
return (new Response(403))->getRawResponse();
} else {
return (new Response(404))->getRawResponse();
}
}
struct stat stat_;
fstat (fd, &stat_);
Response* response = new Response(200);
response->addHeader("Content-type", getContentType(extension));
response->addHeader("Content-Length", stat_.st_size);
evbuffer *temp = response->getRawResponse();
if(strcmp(method, "GET") == 0) {
evbuffer_add_file(temp, fd, 0, stat_.st_size);
}
if(path != NULL)
free(path);
if(extension != NULL)
free(extension);
free(decoded);
return temp;
}
const char *HttpParser::getContentType(char *extension)
{
if(extension == NULL) {
return Configuration::ext["no_ext"];
} else {
const char* ext = Configuration::ext[extension];
if(ext == NULL) {
return Configuration::ext["default"];
} else {
return ext;
}
}
}
void HttpParser::urldecode(char *pszDecodedOut, size_t nBufferSize, char *pszEncodedIn)
{
memset(pszDecodedOut, 0, nBufferSize);
enum DecodeState_e
{
STATE_SEARCH = 0, ///< searching for an ampersand to convert
STATE_CONVERTING, ///< convert the two proceeding characters from hex
};
DecodeState_e state = STATE_SEARCH;
for(unsigned int i = 0; i <= strlen(pszEncodedIn)-1; ++i)
{
if(pszEncodedIn[i] == '+') {
pszEncodedIn[i] = ' ';
}
switch(state)
{
case STATE_SEARCH:
{
if(pszEncodedIn[i] != '%')
{
strncat(pszDecodedOut, &pszEncodedIn[i], 1);
assert(strlen(pszDecodedOut) < nBufferSize);
break;
}
// We are now converting
state = STATE_CONVERTING;
}
break;
case STATE_CONVERTING:
{
// Conversion complete (i.e. don't convert again next iter)
state = STATE_SEARCH;
// Create a buffer to hold the hex. For example, if %20, this
// buffer would hold 20 (in ASCII)
char pszTempNumBuf[3] = {0};
strncpy(pszTempNumBuf, &pszEncodedIn[i], 2);
// Ensure both characters are hexadecimal
bool bBothDigits = true;
for(int j = 0; j < 2; ++j)
{
if(!isxdigit(pszTempNumBuf[j]))
bBothDigits = false;
}
if(!bBothDigits)
break;
// Convert two hexadecimal characters into one character
int nAsciiCharacter;
sscanf(pszTempNumBuf, "%x", &nAsciiCharacter);
// Ensure we aren't going to overflow
assert(strlen(pszDecodedOut) < nBufferSize);
// Concatenate this character onto the output
strncat(pszDecodedOut, (char*)&nAsciiCharacter, 1);
// Skip the next character
i++;
}
break;
}
}
}