-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpConn.cpp
More file actions
165 lines (136 loc) · 4.65 KB
/
HttpConn.cpp
File metadata and controls
165 lines (136 loc) · 4.65 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
#include "HttpConn.h"
#include "EpollOps.h"
#include "Logger.h"
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/uio.h>
#include <string.h>
#include <mysql.h>
using namespace std;
int HttpConn::epollFd = -1;
void HttpConn::initResponse(ConnectionPool* connPool, char* docRoot) {
httpResponse_.initResponse(connPool, docRoot);
}
void HttpConn::init(int fd, struct sockaddr_in addr) {
fd_ = fd;
addr_ = addr;
reset();
}
void HttpConn::reset() {
bytesToSend_ = 0;
bytesHaveSend_ = 0;
inputBuf_.retrieveAll(); // buffer是否应该shrink
outputBuf_.retrieveAll();
if (fileAddress_) {
munmap(fileAddress_, httpResponse_.getFileStat().st_size);
fileAddress_ = NULL;
}
HttpRequest dummy;
httpRequest_.reset(dummy);
httpResponse_.reset();
}
void HttpConn::process() {
// 业务线程执行
if (!httpRequest_.parse(inputBuf_)) { // 解析失败
LOG_INFO("parse failed!");
outputBuf_.append("HTTP/1.1 400 Bad Request\r\n", 26);
outputBuf_.append("\r\n", 2);
outputBuf_.append("Your request has bad syntax or is inherently impossible to staisfy.\n", 68);
iv_[0].iov_base = outputBuf_.beginRead();
iv_[0].iov_len = outputBuf_.readableBytes();
ivCount_ = 1;
bytesToSend_ = iv_[0].iov_len;
}
if (httpRequest_.gotAll()) { // 解析完成
doRequest();
modFd(epollFd, fd_, EPOLLOUT);
}
LOG_INFO("fd = %d process done!", fd_);
}
void HttpConn::doRequest() {
httpResponse_.doResponse(httpRequest_, inputBuf_, outputBuf_); // 根据request做出响应,响应状态反映了响应情况
if(httpResponse_.getHttpCode() == HttpResponse::HttpCode::k200Ok) {
int fd = open(httpResponse_.getRealFile(), O_RDONLY);
fileAddress_ = static_cast<char*>(mmap(0, httpResponse_.getFileStat().st_size, PROT_READ, MAP_PRIVATE, fd, 0));
close(fd);
iv_[0].iov_base = outputBuf_.beginRead();
iv_[0].iov_len = outputBuf_.readableBytes();
iv_[1].iov_base = fileAddress_;
iv_[1].iov_len = httpResponse_.getFileStat().st_size;
ivCount_ = 2;
bytesToSend_ = iv_[0].iov_len + iv_[1].iov_len;
} else {
iv_[0].iov_base = outputBuf_.beginRead();
iv_[0].iov_len = outputBuf_.readableBytes();
ivCount_ = 1;
bytesToSend_ = iv_[0].iov_len;
}
}
void HttpConn::closeConn() {
delFd(epollFd, fd_);
close(fd_);
}
ssize_t HttpConn::recvMsg() {
ssize_t n = inputBuf_.readFd(fd_);
return n;
}
bool HttpConn::sendMsg() {
// 主线程执行
while (1) { // 未发送完,考虑大文件
ssize_t temp = writev(fd_, iv_, ivCount_);
if (temp < 0) {
if (errno == EAGAIN) { // 发送缓冲区满了,继续监听可写
LOG_INFO("fd = %d write EAGAIN", fd_);
LOG_INFO("fd = %d remaining %d bytes to send (%d already success)", fd_, (int)bytesToSend_, (int)bytesHaveSend_);
modFd(epollFd, fd_, EPOLLOUT);
return false;
}
// 其他错误
if (fileAddress_) {
munmap(fileAddress_, httpResponse_.getFileStat().st_size);
fileAddress_ = NULL;
}
return true;
}
bytesToSend_ -= temp;
bytesHaveSend_ += temp;
if (bytesHaveSend_ > iv_[0].iov_len) { // 第一部分已发完,手动调整
iv_[0].iov_len = 0;
iv_[1].iov_base = fileAddress_ + (bytesHaveSend_ - outputBuf_.readableBytes());
iv_[1].iov_len = bytesToSend_;
} else {
iv_[0].iov_base = outputBuf_.beginRead() + bytesHaveSend_;
iv_[0].iov_len = iv_[0].iov_len - bytesHaveSend_;
}
if (bytesToSend_ <= 0) { // 发送完毕
LOG_INFO("fd = %d remaining %d bytes to send (%d already success)", fd_, (int)bytesToSend_, (int)bytesHaveSend_);
if (httpResponse_.getIsCloseConn()) { // 短连接
if (fileAddress_) {
munmap(fileAddress_, httpResponse_.getFileStat().st_size);
fileAddress_ = NULL;
}
return true;
} else { // 长连接
reset();
modFd(epollFd, fd_, EPOLLIN);
return false;
}
}
}
}
int HttpConn::getFd() {
return fd_;
}
void HttpConn::setFd(int fd) {
fd_ = fd;
}
int HttpConn::getBytesToSend() const {
return bytesToSend_;
}
bool HttpConn::isWriting() const {
return bytesToSend_ == 0;
}