-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathser.cpp
executable file
·485 lines (438 loc) · 10.5 KB
/
ser.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "ser.h"
Ser::Ser()
{
//INADDR_ANY 0.0.0.0
//Ser("0.0.0.0", SER_PORT);
if ((m_listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
ERR_EXIT("socket");
int on = 1;
if (setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");
m_addr_len = sizeof(m_local_addr);
bzero(&m_local_addr, m_addr_len);
m_local_addr.sin_family = AF_INET;
m_local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
m_local_addr.sin_port = htons(SER_PORT);
Bind(m_listenfd, &m_local_addr);
Listen(m_listenfd, LISTENQ);
if ((m_epoll_fd = epoll_create1(0)) < 0)
ERR_EXIT("epoll_create1");
add_event(m_listenfd, EPOLLIN);
struct epoll_event one;
bzero(&one, sizeof(one));
m_epoll_event.push_back(one);
}
Ser::Ser(const char* ip, unsigned int port)
{
if ((m_listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
ERR_EXIT("socket");
int on = 1;
if (setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");
m_addr_len = sizeof(m_local_addr);
bzero(&m_local_addr, m_addr_len);
m_local_addr.sin_family = AF_INET;
m_local_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &m_local_addr.sin_addr) < 0)
ERR_EXIT("inet_pton");
Bind(m_listenfd, &m_local_addr);
Listen(m_listenfd, LISTENQ);
if ((m_epoll_fd = epoll_create1(0)) < 0)
ERR_EXIT("epoll_create1");
add_event(m_listenfd, EPOLLIN);
struct epoll_event one;
bzero(&one, sizeof(one));
m_epoll_event.push_back(one);
}
void Ser::Bind(int sockfd, struct sockaddr_in* addr)
{
if (bind(sockfd, (SA*)addr, m_addr_len) < 0)
ERR_EXIT("bind");
}
void Ser::Listen(int sockfd, unsigned int num)
{
if (listen(sockfd, num) < 0)
ERR_EXIT("listen");
}
int Ser::wait_event()
{
int ret = epoll_wait(m_epoll_fd, &m_epoll_event[0], m_epoll_event.size(), -1);
if (ret < 0)
{
ERR_EXIT("epoll_wait");;
}
return ret;
}
void Ser::add_event(int fd, int state)
{
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
epoll_ctl(m_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
}
void Ser::delete_event(int fd, int state)
{
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
epoll_ctl(m_epoll_fd, EPOLL_CTL_DEL, fd, &ev);
}
void Ser::modify_event(int fd, int state)
{
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
epoll_ctl(m_epoll_fd, EPOLL_CTL_MOD, fd, &ev);
}
void Ser::do_accept()
{
int fd = accept(m_listenfd, nullptr, nullptr);
if (fd == -1)
{
cerr << "accept" << endl;
return;
}
cout << "new fd:" << fd << endl;
/*
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
bzero(&addr, len);
getsockname(m_listenfd, (SA*)&addr, &len);
cout << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << " :: ";
bzero(&addr, len);
getpeername(m_listenfd, (SA*)&addr, &len);
cout << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << endl;
bzero(&addr, len);
getsockname(fd, (SA*)&addr, &len);
cout << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << " :: ";
bzero(&addr, len);
getpeername(fd, (SA*)&addr, &len);
cout << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << endl;
//*/
m_connfd.push_back(fd);//添加到已连接队列
add_event(fd, EPOLLIN);//添加到epoll监听
struct epoll_event ev;
bzero(&ev, sizeof(ev));
m_epoll_event.push_back(ev);//扩大事件处理队列容量
//do_in(fd);//启动线程处理此请求事件
}
void Ser::do_in(int fd)
{
char head[HEADSIZE];
//读报文头
int headlen;
if ((headlen = readline(fd, head, HEADSIZE)) == 0)
{
do_close(fd);
return;
}
head[headlen] = 0;
//read返回0则链接断开,处理已链接列表,处理epoll监听队列,减小数组大小
//解析存储
char way[16] = {0};
char uri[1024] = {0};
char version[16] = {0};
cout << head;
sscanf(head, "%s %s %s\r\n", way, uri, version);
upchar(way, sizeof(way));//统一大小写
downchar(uri, sizeof(uri));
upchar(version, sizeof(version));
if (strcmp(way, "GET") == 0)//get方法请求
{
int ret;
char bodybuf[BODYSIZE] = {0};
while (1)//直到请求头结束
{
ret = readline(fd, bodybuf, BODYSIZE);
if (ret == 0)
{
do_close(fd);
return;
}
if ((ret == 2) && (bodybuf[0] == '\r') && (bodybuf[1] == '\n'))
break;
//cout << bodybuf;
bzero(bodybuf, BODYSIZE);
}
do_get(fd, uri, sizeof(uri));//解析数据并完成发送任务
}
else if (strcmp(way, "POST") == 0)//post方法请求
{
char bodybuf[BODYSIZE] = {0};
char keybuf[32] = {0};
char valuebuf[512] = {0};
size_t textlen = 0;
int ret;
while (1)//直到请求头结束
{
ret = readline(fd, bodybuf, BODYSIZE);
if (ret == 0)
{
do_close(fd);
return;
}
if ((ret == 2) && (bodybuf[0] == '\r') && (bodybuf[1] == '\n'))
break;
cout << bodybuf;
downchar(bodybuf, BODYSIZE);//统一转为小写
bzero(keybuf, sizeof(keybuf));
bzero(valuebuf, sizeof(valuebuf));
sscanf(bodybuf, "%s: %s\r\n", keybuf, valuebuf);//分解键值对
if (strcmp(keybuf, "content-length") == 0)
{
sscanf(valuebuf, "%lu", &textlen);//读取文本长度
}
bzero(bodybuf, BODYSIZE);
}
char *ptext = new char[textlen+1];
bzero(ptext, textlen+1);
if (readn(fd, ptext, textlen) == 0)//读取文本
{
delete[] ptext;
do_close(fd);
return;
}
ptext[textlen] = '\0';
do_post(fd, uri, sizeof(uri), ptext, textlen+1);//解析数据并完成发送任务
delete[] ptext;
}
else
{
cout << "Further Support." << endl;
return;
}
return;
}
void Ser::do_close(int fd)
{
//close()
close(fd);
cout << "close fd:" << fd << endl;
//从已连接队列中删除
m_connfd.remove(fd);
//从epoll中删除
delete_event(fd, EPOLLIN);
//结束当前线程
//pthread_exit(NULL);
}
void Ser::do_conf(const char* filename)
{
bzero(m_confpath, sizeof(m_confpath));
FILE* pconf = fopen(filename, "rb");
char line[1024] = {0};
while (fgets(line, sizeof(line), pconf) != NULL)
{
char key[32] = {0};
char path[1024] = {0};
//sscanf(line, "%s=%s\n", key, path);
char* p = line;
while (*(p++) != '=');
p[-1] = '\0';
p[strlen(p)-1] = '\0';//去掉换行符
strcpy(key, line);
strcpy(path, p);
upchar(key, sizeof(key));
//cout << key << path << endl;
if (strcmp(key, "PATH") == 0)
{
strncpy(m_confpath, path, sizeof(path));
if (m_confpath[strlen(m_confpath)-1] == '/')//去掉后缀/
{
m_confpath[strlen(m_confpath)-1] = '\0';
}
//cout << m_confpath << endl;
}
}
return;
}
void Ser::go()
{
do_conf("./.httpd.conf");//从配置文件加载配置项
//cout << "m_confpath:" << m_confpath << endl;
while (1)
{
int num = wait_event();
for (int i = 0; i < num; ++i)
{
int fd = m_epoll_event[i].data.fd;
int event = m_epoll_event[i].events;
if ((fd == m_listenfd) && (event & EPOLLIN))
{
do_accept();
//thread(&Ser::do_accept, this).detach();//启动分离的无名线程处理新连接
}
else if (event & EPOLLIN)
{
do_in(fd);
//thread(&Ser::do_in, this, fd).detach();//启动线程处理此请求事件
}
/*else if (event & EPOLLOUT)
//thread out(do_out, fd);
do_out(fd);*/
}
//由于连接关闭。当连接数大于100,且容器大于连接数的2倍时,适当减小事件处理容器
size_t sizelist = m_connfd.size();
size_t sizearr = m_epoll_event.size()/2;
if ((sizelist > 100) && (sizearr > sizelist))
m_epoll_event.resize(sizearr);
}
}
unsigned int Ser::readline(int fd, char* buf, size_t len)
{
unsigned int i = 0;
for (i = 0; i < len; ++i)
{
if (read(fd, buf+i, 1) != 1)
{
if (errno == EINTR)
continue;
else return 0;
}
if (*(buf+i) == '\n' && *(buf+i-1) == '\r')
break;
}
return i+1;
}
unsigned int Ser::writeline(int fd, const char* buf, size_t len)
{
unsigned int i = 0;
for (i = 0; i < len; ++i)
{
if (write(fd, buf+i, 1) != 1)
{
if (errno == EINTR)
{
--i;
continue;
}
else return 0;
}
if (*(buf+i) == '\n' && *(buf+i-1) == '\r')
break;
}
return i+1;
}
unsigned int Ser::readn(int fd, char* buf, size_t len)
{
char* now = buf;
size_t oready = 0;
size_t ward = len;
int ret = 0;
while (ward)
{
do {//中断恢复
ret = read(fd, now, ward);
} while ((ret == -1) && (errno == EINTR));
if (ret == -1)
ERR_EXIT("readn");
else if (ret == 0)//连接中断再也没有数据
return 0;
now += ret;
oready += ret;
ward -= ret;
}
return oready;
}
unsigned int Ser::writen(int fd, const char* buf, size_t len)
{
const char* now = buf;
size_t oready = 0;
size_t ward = len;
int ret = 0;
while (ward)
{
do {
ret = write(fd, buf, ward);
} while ((ret == -1) && (errno == EINTR));
if (ret <= 0)
ERR_EXIT("writen");
now += ret;
oready += ret;
ward -= len;
}
return oready;
}
void Ser::upchar(char* buf, size_t len)
{
for (size_t i = 0; i < len; ++i)
*(buf+i) = toupper(*(buf+i));
}
void Ser::downchar(char* buf, size_t len)
{
for (size_t i = 0; i < len; ++i)
*(buf+i) = tolower(*(buf+i));
}
long Ser::get_file_size(const char* path)
{
long sizefile = -1;
struct stat statbuf;
if (stat(path, &statbuf) < 0)
return sizefile;
sizefile = statbuf.st_size;
return sizefile;
}
void Ser::do_get(int fd, const char* uri, size_t len)
{
//解析uri指定的文件及参数
//cout << "do_get" << endl;
//静态文件则发送文件
char filepath[1024] = {0};
snprintf(filepath, sizeof(filepath), "%s%s", m_confpath, uri);
if (filepath[strlen(filepath)-1] == '/')
strcat(filepath, "index.html");
//cout << filepath << endl;
if (!writeline(fd, "HTTP/1.1 200 OK\r\n", sizeof("HTTP/1.1 200 OK\r\n")))
{
do_close(fd);
return;
}
if (!writeline(fd, "Content-Type: text/html\r\n", sizeof("Content-Type: text/html\r\n")))
{
do_close(fd);
return;
}
long filesize = get_file_size(filepath);
char contentlength_buf[128] = {0};
sprintf(contentlength_buf, "content-length: %lu\r\n", filesize);
if (writeline(fd, contentlength_buf, sizeof(contentlength_buf)) == 0)
{
do_close(fd);
return;
}
if (writeline(fd, "\r\n", sizeof("\r\n")) == 0)
{
do_close(fd);
return;
}
//cout << "begin send file:" << filesize << endl;
if (filesize <= 0) return;
FILE* pfile = fopen(filepath, "rb");
char buf[4096] = {0};
int ret = 0;
while (1)
{
ret = fread(buf, 1, sizeof(buf), pfile);
buf[ret] = 0;
if (ret < 0)
{
if (errno == EINTR)
continue;
else do_close(fd);
}
if (ret == 0)
break;
if (!writen(fd, buf, ret))
{
do_close(fd);
return;
}
}
//动态请求则执行程序并发送输出
}
void Ser::do_post(int fd, const char* uri, size_t urilen, const char* text, size_t textlen)
{
cout << "do_post" << endl;
//解析uri
//解析使用text报文体
//发送相应内容
}