-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessor.cpp
More file actions
292 lines (274 loc) · 8.2 KB
/
Processor.cpp
File metadata and controls
292 lines (274 loc) · 8.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "Processor.hpp"
#include "MimeTypes.hpp"
#include "Utils.hpp"
#include "Config.hpp"
#include "Location.hpp"
#include "Logger.hpp"
#include "CgiProcess.hpp"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <cstring>
#include <cerrno>
#include <cstdio>
using namespace std;
Processor::Processor(MyServer &server) : serv(server)
{
}
Response Processor::processGET(Request &r, const Location &loc)
{
Response res(serv);
string path = buildPath(r);
FileType ft = getFileType(path);
if (ft == Directory)
{
if (path[path.length() - 1] != '/')
{
path += "/";
}
path += loc.getIndex()[0];
ft = getFileType(path);
}
if (ft == DoesNotExist)
{
res.setStatus("404");
return res;
}
int fd = open(path.c_str(), O_RDONLY);
if (fd == -1)
{
logger.log(LogType::DEBUG, strerror(errno));
res.setStatus("503");
return res;
}
struct stat st;
fstat(fd, &st);
char *buff = new char[st.st_size];
read(fd, buff, st.st_size);
close(fd);
if (r.getMethod() == "GET")
res.setBody(string(buff, st.st_size));
res.setHeader("Last-Modified", timeToHTML(st.st_mtim.tv_sec));
res.setStatus("200");
delete[]buff;
return res;
}
Response Processor::processPOST(Request &r, const Location &loc)
{
size_t size = r.getBody().size();
if( size > loc.getMaxBody())
{
Response res(serv);
res.setStatus("413");
return res;
}
r.setRoot(getLanguageRoot(r));
string cgiName = loc.getCgiScript();
string cgiPath = getLanguageRoot(r) + cgiName;
CgiProcess cgi(serv, r, cgiPath, cgiName);
Response res = cgi.processCGI();
string methods = join(", ", loc.getMethods());
res.setHeader("Allow", methods);
// res.setStatus("201");
return res;
}
Response Processor::processPUT(Request &r, const Location &loc)
{
Response res(serv);
string path = buildPath(r);
FileType ft = getFileType(path);
if (ft == Directory)
{
res.setStatus("400");
return res;
}
int fd;
if (ft == DoesNotExist)
fd = open(path.c_str(), O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);
else
fd = open(path.c_str(), O_TRUNC | O_RDWR);
if (fd == -1)
{
logger.log(LogType::DEBUG, strerror(errno));
res.setStatus("503");
return res;
}
const char *body = r.getBody().c_str();
size_t bodySize = r.getBody().size();
int w = write(fd, body, bodySize);
if (w == -1)
{
logger.log(LogType::DEBUG, strerror(errno));
close(fd);
res.setStatus("503");
return res;
}
struct stat st;
fstat(fd, &st);
res.setHeader("Last-Modified", timeToHTML(st.st_mtim.tv_sec));
res.setHeader("Content-Location", r.getUri().getPath());
string methods = join(", ", loc.getMethods());
res.setHeader("Allow", methods);
setContType(res, path);
close(fd);
if (r.getBody().size() == 0)
res.setStatus("204");
else
res.setStatus("200");
return res;
}
Response Processor::processDELETE(Request &r, const Location &loc)
{
Response res(serv);
string path = buildPath(r);
FileType ft = getFileType(path);
if (ft == DoesNotExist)
{
res.setStatus("403");
return res;
}
else if (ft == Directory)
{
res.setStatus("400");
return res;
}
if (remove(path.c_str()) == -1)
{
logger.log(LogType::ERROR, strerror(errno));
res.setStatus("304");
return res;
}
string methods = join(", ", loc.getMethods());
res.setHeader("Allow", methods);
setContType(res, path);
if (r.getBody().size() == 0)
res.setStatus("204");
else
res.setStatus("200");
return res;
}
string Processor::getLanguageRoot(Request &r)
{
string path;
auto iAcceptLang = r.getHeaders().find("accept-language");
if (iAcceptLang != r.getHeaders().end())
{
for (const string &s : iAcceptLang->second)
{
auto iLang = serv.getConf().getLanguages().find(s);
if (iLang != serv.getConf().getLanguages().end())
return iLang->second;
}
}
auto iDefLang = serv.getConf().getLanguages().find(serv.getConf().getDefLanguage());
if (iDefLang != serv.getConf().getLanguages().end())
return iDefLang->second;
return serv.getConf().getRoot();
}
string Processor::buildPath(Request &r)
{
string path = getLanguageRoot(r) + r.getUri().getPath();
return path;
}
Response Processor::processRequest(Request &r)
{
bool methodMatched = false;
bool UriMatched = false;
vector<string> errStatus;
Response rsp(serv);
if (r.getHttpVersion() != serv.getConf().getProtocol())
{
rsp.setStatus("505");
return rsp;
}
for (const auto &l : serv.getConf().getLocations())
{
vector<string> matchedUri = l.matchesUri(r.getUri().getPath());
if (matchedUri.empty())
continue;
for (const auto &path : matchedUri)
{
UriMatched = true;
r.setUri(RequestURI(path, r.getUri().getQuery(), r.getUri().getFragment()));
bool test = l.isMatchMethod(r.getMethod());
if (!test)
continue;
if (r.getMethod() == "GET" || r.getMethod() == "HEAD") // The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
{
methodMatched = true;
Response t = processGET(r, l);
if (t.getStatus()[0] == '2')
return t;
else
errStatus.push_back(t.getStatus());
}
else if (r.getMethod() == "POST") //The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.
{
methodMatched = true;
Response t = processPOST(r, l);
if (t.getStatus()[0] == '2')
return t;
else
errStatus.push_back(t.getStatus());
}
else if (r.getMethod() == "PUT") // The PUT method replaces all current representations of the target resource with the request payload.
{
methodMatched = true;
Response t = processPUT(r, l);
if (t.getStatus()[0] == '2')
return t;
else
errStatus.push_back(t.getStatus());
}
else if (r.getMethod() == "DELETE")
{
methodMatched = true;
Response t = processDELETE(r, l);
if (t.getStatus()[0] == '2')
return t;
else
errStatus.push_back(t.getStatus());
}
}
}
if (UriMatched && !methodMatched)
{
rsp.setStatus("405");
return rsp;
}
if (!errStatus.empty())
rsp.setStatus(errStatus.back());
else
rsp.setStatus("404");
return rsp;
}
// // else if (r.getMethod() == "CONNECT")
// // {
// // // The CONNECT method establishes a tunnel to the server identified by the target resource.
// // }
// // else if (r.getMethod() == "OPTIONS")
// // {
// // // The OPTIONS method is used to describe the communication options for the target resource.
// // }
// // else if (r.getMethod() == "TRACE")
// // {
// // // The TRACE method performs a message loop-back test along the path to the target resource.
// // }
// // else if (r.getMethod() == "PATCH")
// // {
// // // The PATCH method is used to apply partial modifications to a resource.
// // }
void Processor::setContType(Response &res, const string &path)
{
string ext = getFileExt(path);
if (mimeTypes.find(ext) != mimeTypes.end())
{
string mimeType = mimeTypes[ext];
res.setHeader("Content-Type", mimeType);
}
else
{
res.setHeader("Content-Type", "application/octet-stream");
}
}