linyacool / WebServer

A C++ High Performance Web Server
MIT License
7.78k stars 2.11k forks source link

你的旧版本实现 #92

Open GodXuebi opened 4 years ago

GodXuebi commented 4 years ago

如果服务器接收到的是一个没有keep-alive的包,你的服务器端是怎么关闭连接的,我说的是哪里调用close

linyacool commented 4 years ago

没有keep-alive也依然会放到定时器里,等定时器任务结束的时候 会顺带close

GodXuebi commented 4 years ago

其实我关注到了你的Timer,If Timer is overtime, your code only uses the epoll_modify to stop continuing to listen the fd and unbind the relative RequestData, but i can not find the place where you use to close. I am sorry my keyboard have some problems to type Chinese

int Epoll::epoll_del(int fd, __uint32_t events) { struct epoll_event event; event.data.fd = fd; event.events = events; if(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &event) < 0) { perror("epoll_del error"); return -1; } auto fd_ite = fd2req.find(fd); if (fd_ite != fd2req.end()) fd2req.erase(fd_ite); return 0; }

TimerNode::~TimerNode() { cout << "~TimerNode()" << endl; if (request_data) { Epoll::epoll_del(request_data->getFd()); } //request_data.reset(); // if (request_data) // { // cout << "request_data=" << request_data << endl; // delete request_data; // request_data = NULL; // } }

GodXuebi commented 4 years ago

And i have another question, if the HTTP request data uses keep-alive, and if the client sends two HTTP request, how can separate the two HTTP. I have notified you use the content-type as the judgement. But if the received data have some content of the second HTTP, how to deal with that. i can leave my Wechat to further discuss. Thanks for your patient reply. i learn a lot from your project.

GodXuebi commented 4 years ago

if (state == STATE_FINISH) { if (keep_alive) { //printf("ok\n"); this->reset(); } else { //delete this; return; } }

By example, here for keep-alive, you uses the reset() which can clear the content, but if the content has the a part of the second HTTP request(request line). And in fact, here you use the continue to continue the loop which means you continue to receive the data without using the epoll until the length of data > content-length. And in the latest version of your server code, you directly uses break, so the epoll can be used But i think the latest version also does not solve the "second HTTP request" problem.