-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinyWeb.cpp
More file actions
367 lines (323 loc) · 7.84 KB
/
TinyWeb.cpp
File metadata and controls
367 lines (323 loc) · 7.84 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <netdb.h>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <sys/stat.h>
#include <map>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include "Buffer.h"
#include "fmt/format.h"
constexpr socklen_t MAXLINE = 1024;
constexpr socklen_t MAXBUFFER = 4096;
class RioT
{
public:
explicit RioT(int fd):
fd_(fd),
buffer_()
{
}
/**
* 从RioT读取一行到buffer
* 同时会读取\r\n 或者\n 到buffer中
* 读取结果末尾不增加\0
* @param buffer 存储字符串的缓冲区
* @param buffer_size 缓冲区长度
* @return 读取成功 返回读取长度, 数据不足返回 0 读满后依旧无换行或参数错误 返回-1
*/
ssize_t ReadLine(char* buffer, size_t buffer_size)
{
if (!buffer)
{
return -1;
}
char* line_begin = buffer_.ReadBegin();
char* line_end = buffer_.Find('\n');
if (!line_end)
{
/**
* 没有读取到换行符 移除已读取内容 并读取后再次查询
*/
buffer_.RemoveReadData();
buffer_.ReadDataFromFd(fd_);
line_end = buffer_.Find('\n');
if (!line_end)
{
/**
* 如果缓冲区已经读满 但仍然没有换行则返回 -1 否则返回0
*/
return buffer_.Writeable() == 0 ? -1 : 0;
}
}
int line_length = line_end - line_begin + 1;
if (line_length + 1 > buffer_size)
{
return -1;
}
memcpy(buffer, line_begin, line_length);
buffer_.AddReadIdx(line_length);
buffer[line_length] = '\0';
return line_length;
}
private:
int fd_;
Buffer buffer_;
};
int OpenListenFd(const char* port)
{
struct addrinfo hints{};
hints.ai_socktype = SOCK_STREAM;
// Socket address is intended for `bind'
// Use configuration of this host to choose
// returned address type..
hints.ai_flags |= AI_PASSIVE | AI_ADDRCONFIG | AI_NUMERICSERV;
struct addrinfo* result;
getaddrinfo(nullptr, port, &hints, &result);
int listenfd;
int on = 1;
struct addrinfo* p;
for (p = result; p; p = p->ai_next)
{
if ((listenfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) < 0)
{
fprintf(stderr, "socket error: %s\n", strerror(errno));
continue;
}
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
static_cast<const void*>(&on),
static_cast<socklen_t>(sizeof on));
if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0)
{
break;
}
close(listenfd);
}
freeaddrinfo(result);
if (!p)
{
return -1;
}
if (listen(listenfd, 100) < 0)
{
return -1;
}
return listenfd;
}
void ClientError(int fd, const char* cause, const char* errnum, const char* shortmsg,
const char* longmsg)
{
std::string body;
body.reserve(MAXBUFFER);
body += "<html><title>Tiny Error</title>";
body += "<body bgcolor=""ffffff"">\r\n";
body += fmt::format("{}: {}\r\n", errnum, shortmsg);
body += fmt::format("<p>{}: {}\r\n", longmsg, cause);
body += "<hr><em>The Tiny Web server</em>\r\n";
std::string header;
header.reserve(MAXLINE);
header += fmt::format("HTTP/1.0 {} {}\r\n", errnum, shortmsg);
header += "Content-type: text/html\r\n";
header += fmt::format("Content-length: {}\r\n\r\n", body.length());
write(fd, header.c_str(), header.size());
write(fd, body.c_str(), body.size());
}
void ReadRequestHeaders(RioT* rio_t)
{
char buffer[MAXLINE];
rio_t->ReadLine(buffer, sizeof buffer);
while (strcmp(buffer, "\r\n") != 0)
{
rio_t->ReadLine(buffer, sizeof buffer);
}
}
/**
* 解析uri 填充参数 并返回是否是静态网页请求
* @param uri 传入的uri参数
* @param filename 保存文件名 静态网页默认文件名 index.html 动态文件名 .cginame
* @param cgiargs 保存agiargs 不存在置为 \0
* @return true 静态网页请求 false cgi请求
*/
bool ParseUri(char* uri, char* filename, char* cgiargs)
{
if (!strstr(uri, "cgi-bin"))
{
strcpy(cgiargs, ""); // copy terminating null byte
strcpy(filename, ".");
strcat(filename, uri);
if (uri[strlen(uri) - 1] == '/')
{
strcat(filename, "index.html");
}
return true;
}
else
{
char* ptr = strchr(uri, '?'); // change index to strchr
if (ptr)
{
strcpy(cgiargs, ptr + 1);
*ptr = '\0'; // remove '?' and cgiargs
}
else
{
strcpy(cgiargs, ""); // copy terminating null byte
}
strcpy(filename, ".");
strcat(filename, uri);
return false;
}
}
std::map<std::string, std::string> file_type_map{
{"html", "text/html"},
{"gif", "image/gif"},
{"png", "image/png"},
{"jpg", "image/ipeg"}};
void GetFileType(char* filename, std::string* filetype)
{
std::string suffix(strrchr(filename, '.') + 1);
auto iter = file_type_map.find(suffix);
if (iter != file_type_map.end())
{
*filetype = iter->second;
}
else
{
filetype->append("text/plain");
}
}
void ServerStatic(int fd, char* filename, size_t file_size)
{
std::string file_type;
GetFileType(filename, &file_type);
std::string header;
header.reserve(MAXBUFFER);
header += "HTTP/1.0 200 OK\r\n";
header += "Server: Tiny Web Server\r\n";
header += "Connection: close\r\n";
header += fmt::format("Content-length: {}\r\n", file_size);
header += fmt::format("Content-type: {}\r\n\r\n", file_type);
write(fd, header.c_str(), header.length());
std::cout << "Response headers:\n";
std::cout << header;
int file_fd = open(filename, O_RDONLY, 0);
char* srcp = static_cast<char*>(
mmap(0, file_size, PROT_READ, MAP_PRIVATE,
file_fd, 0));
close(file_fd);
write(fd, srcp, file_size);
munmap(srcp, file_size);
}
void ServerDynamic(int fd, char* filename, char* cgiargs)
{
std::string header;
header.reserve(MAXBUFFER);
header += "HTTP/1.0 200 OK\r\n";
header += "Server: Tiny Web Server\r\n";
write(fd, header.c_str(), header.length());
if (fork() == 0)
{
setenv("QUERY_STRING", cgiargs, 1);
dup2(fd, STDOUT_FILENO);
execl(filename, nullptr, environ);
}
wait(nullptr);
}
void Doit(int connfd)
{
RioT rio_t(connfd);
char buffer[MAXLINE];
rio_t.ReadLine(buffer, sizeof buffer);
printf("Request headers:\n%s", buffer);
char method[MAXLINE];
char uri[MAXLINE];
char version[MAXLINE];
sscanf(buffer, "%s %s %s", method, uri, version);
if (strcasecmp(method, "GET") != 0)
{
ClientError(connfd, method, "501", "Not Implemented",
"Tiny does not implement this method");
return;
}
ReadRequestHeaders(&rio_t);
char filename[MAXLINE];
char cgiargs[MAXLINE];
bool is_static = ParseUri(uri, filename, cgiargs);
struct stat status;
if (stat(filename, &status) < 0)
{
ClientError(connfd, filename, "404", "Not found",
"Tiny couldn't find this file");
return;
}
if (is_static)
{
/**
* S_ISREG 是否是常规文件
* S_IRUSR read by owner
*/
if (!S_ISREG(status.st_mode) ||
!(S_IRUSR & status.st_mode))
{
ClientError(connfd, filename, "403", "Forbidden",
"Tiny couldn't read the file");
return;
}
ServerStatic(connfd, filename, status.st_size);
}
else
{
if (!S_ISREG(status.st_mode) ||
!(S_IRUSR & status.st_mode))
{
ClientError(connfd, filename, "403", "Forbidden",
"Tiny couldn't run the CGI program");
return;
}
ServerDynamic(connfd, filename, cgiargs);
}
}
int main(int argc, char* argv[])
{
int opt;
char* port;
while ((opt = getopt(argc, argv, "p:")) != -1)
{
switch (opt)
{
case 'p':
port = optarg;
break;
default:
fprintf(stderr, "Usage: %s [-p port]", argv[0]);
exit(-1);
}
}
int listenfd = OpenListenFd(port);
struct sockaddr_storage clientaddr{};
socklen_t clientaddr_len =
static_cast<socklen_t>(sizeof clientaddr);
char client_hostname[MAXLINE];
char client_port[MAXLINE];
while (true)
{
int connfd = accept(listenfd,
reinterpret_cast<sockaddr*>(&clientaddr),
&clientaddr_len);
if (connfd < 0)
{
break;
}
getnameinfo(reinterpret_cast<sockaddr*>(&clientaddr), clientaddr_len,
client_hostname, MAXLINE,
client_port, MAXLINE, NI_NUMERICHOST | NI_NUMERICSERV);
printf("Accept connection from (%s:%s)\n", client_hostname, client_port);
Doit(connfd);
close(connfd);
}
}