-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.cpp
More file actions
261 lines (235 loc) · 7.55 KB
/
http.cpp
File metadata and controls
261 lines (235 loc) · 7.55 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
*
* @file server.c
* @author ajhende4 Alec Henderson
*/
#include <sstream>
#include <memory>
#include <map>
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <filesystem>
#include <unistd.h>
#define BUFFER_SIZE 1024
#define IS_NEWLINE (buffer[i] == '\n' || buffer[i] == '\r')
#define IS_NOT_NEWLINE (buffer[i] != '\n' && buffer[i] != '\r')
typedef enum methods {GET, POST, HEAD, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH, NIL} httpMethod;
std::map<std::string, std::string> ContentTypes = {
{"html", "text/html"}, {"css", "text/css"}, {"jpeg", "image/jpeg"}, {"jpg", "image/jpg"},
{"png", "image/png"}, {"ico", "image/ico"}, {"js", "text/javascript"}
};
class httpRequest {
private:
static inline ulong scanUntilCRLF(const char buffer[BUFFER_SIZE], size_t n = BUFFER_SIZE) {
ulong i = 0;
while(i < n && buffer[i] != '\r') {
++i;
}
return i;
}
public:
int socket = -1;
httpMethod method;
std::string target;
std::string version;
std::map<std::string, std::string> headers;
httpRequest(char *buffer, ssize_t n) {
using namespace std;
stringstream ss;
//parse header
ssize_t i = 0;
while(i < n && buffer[i] != ' ' && IS_NOT_NEWLINE) {
ss << buffer[i];
i++;
}
string m(ss.str());
ss.str("");
ss.clear();
i++;
while(i < n && buffer[i] != ' ' && IS_NOT_NEWLINE) {
ss << buffer[i];
i++;
}
target = string(ss.str());
ss.str("");
ss.clear();
i++;
while(i < n && buffer[i] != ' ' && IS_NOT_NEWLINE) {
ss << buffer[i];
i++;
}
version = string(ss.str());
if(m == "GET") {
method = GET;
} else if(m == "POST") {
method = POST;
} else {
cout << "HTTP method " << m << " requested but not supported\n";
method = NIL;
return;
}
while(i < n && buffer[i] != EOF && buffer[i] != '\0') {
short newlineCount = 0;
while(i < n && IS_NEWLINE) {
newlineCount++;
i++;
}
string key, value;
while(i < n && buffer[i] != ':') {
key += buffer[i];
i++;
}
i += 2;
while(i < n && buffer[i] && IS_NOT_NEWLINE) {
value += buffer[i];
i++;
}
i++;
if(!key.empty() && !value.empty()) {
headers[key] = value;
}
}
}
explicit httpRequest(int socket) {
using namespace std;
char buffer[BUFFER_SIZE];
read(socket, buffer, BUFFER_SIZE);
char meth[9], reso[BUFFER_SIZE], ver[11];
if(sscanf(buffer, "%8s %1023s %10s\r\n", meth, reso, ver) != 3) {
cerr << "something went wrong reading the header\n" ;
}
if(strcmp(meth, "GET") == 0) {
method = GET;
} else if(strcmp(meth, "POST") == 0) {
method = POST;
} else {
cout << "HTTP method " << meth << " requested but not supported\n";
method = NIL;
return;
}
version = string(ver);
if(strcmp(reso, "/") == 0) {
target = "/index.html";
} else {
target = string(reso);
}
long min = scanUntilCRLF(buffer) + 2;
long max = min + scanUntilCRLF(buffer + min, BUFFER_SIZE - min);
while(buffer[min] != '\r') {
while (max != BUFFER_SIZE && buffer[min] != '\r') {
ulong j = min;
string key, value;
while (j < BUFFER_SIZE && buffer[j] != ':') {
key += buffer[j];
j++;
}
j += 2;
while (j < BUFFER_SIZE && buffer[j] && (buffer[j] != '\n' && buffer[j] != '\r')) {
value += buffer[j];
j++;
}
if (!key.empty() && !value.empty()) {
headers[key] = value;
}
min = max + 2;
max = min + scanUntilCRLF(buffer + min, BUFFER_SIZE - min);
}
if(max == BUFFER_SIZE) {
for(ulong i = min; i < BUFFER_SIZE; i++) {
buffer[i - min] = buffer[i];
}
read(socket, buffer + min, BUFFER_SIZE - min);
min = 2;
max = scanUntilCRLF(buffer + min, BUFFER_SIZE - min);
}
}
long contentLen = 0;
if(auto x = headers.find("Content-Length"); x != headers.end()) {
contentLen = atol(x->second.c_str());
}
if(!contentLen) {
return;
}
min += 2;
for(ulong i = min; i < BUFFER_SIZE; i++) {
buffer[i - min] = buffer[i];
}
std::vector<char> binary;
while(contentLen > 0) {
long m = contentLen < (BUFFER_SIZE - min) ? contentLen : BUFFER_SIZE - min;
binary.insert(binary.end(), buffer, buffer + m);
contentLen -= (BUFFER_SIZE - min);
if(contentLen > 0) {
read(socket, buffer, BUFFER_SIZE);
min = 0;
}
}
cout << "binary size: " << binary.size() << '\n';
for(char c : binary) {
cout << c;
}
cout << '\n';
}
};
class httpResponse {
private:
const std::string CRLF = "\r\n";
const std::string notFound = "404 Not found";
public:
std::string responseCode; //ex. 200 OK, 404 Not Found
std::string version = "HTTP/3"; //for images, use HTTP/3
std::string filepath;
std::map<std::string, std::string> headers;
std::string header;
//constructor generates response
httpResponse(const std::set<std::string>& allowedResources, const httpRequest& req, std::string resourcePath) {
using namespace std;
const string& t = req.target;
if(req.method != GET || allowedResources.find(t) == allowedResources.end()) {
responseCode = notFound;
cerr << "client requested illegal resource " << t << '\n';
return;
}
responseCode = "200 OK";
auto idx = t.find('.');
auto seek = ContentTypes.find(t.substr(idx + 1));
if(seek == ContentTypes.end()) {
responseCode = notFound;
cerr << "content " << t << " not allowed\n";
return;
}
headers["Content-Type:"] = seek->second;
if(!filesystem::exists(resourcePath + t)) {
responseCode = notFound;
cerr << "could not find file " << resourcePath + t << '\n';
return;
}
filepath = resourcePath + t;
headers["Content-Length:"] = to_string(filesystem::file_size(filepath));
}
/**
* assembles getHeader with blank lines at the end so that the file may
* be printed immediately after
* @return the complete getHeader as a string
*/
std::string getHeader() {
if(!header.empty()) {
return header;
}
using namespace std;
stringstream ss;
ss << version << ' ';
ss << responseCode << CRLF;
if(responseCode == notFound) {
return ss.str();
}
for(const auto& it : headers) {
ss << it.first << ' ';
ss << it.second << CRLF;
}
ss << CRLF;
return ss.str();
}
};