-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhttpserverLT.cpp
More file actions
207 lines (181 loc) · 5.75 KB
/
Copy pathhttpserverLT.cpp
File metadata and controls
207 lines (181 loc) · 5.75 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
#include <iostream>
#include <functional>
#include "hpserver/KInetAddress.h"
#include "hpserver/KSocket.h"
#include "hpserver/KBuffer.h"
#include "thread/KThreadPool.h"
// for http
#include "http/KHttpContext.h"
#include "http/KHttpRequest.h"
#include "http/KHttpResponse.h"
#include <sys/epoll.h>
#include <string.h>
#include <vector>
#include <unistd.h>
#include <assert.h>
#include "utils/KIcon.h"
using namespace kb;
/// Http server 版本-1 using LT ///
const int timeoutMs = 5000;
void selectResponse(const HttpRequest &req, HttpResponse *resp)
{
if (req.path() == "/")
{
resp->setStatusCode(HttpResponse::k200Ok);
resp->setStatusMessage("OK");
resp->setContentType("text/html");
resp->addHeader("Server", "Muduo");
string now = Timestamp::now().toFormattedString();
resp->setBody("<html><head><title>This is title</title></head>"
"<body><h1>Hello</h1>Now is " +
now +
"</body></html>");
}
else if (req.path() == "/favicon.ico")
{
resp->setStatusCode(HttpResponse::k200Ok);
resp->setStatusMessage("OK");
resp->setContentType("image/png");
resp->setBody(string(favicon, sizeof favicon));
}
else if (req.path() == "/hello")
{
resp->setStatusCode(HttpResponse::k200Ok);
resp->setStatusMessage("OK");
resp->setContentType("text/plain");
resp->addHeader("Server", "Muduo");
resp->setBody("hello, world!\n");
}
else
{
resp->setStatusCode(HttpResponse::k404NotFound);
resp->setStatusMessage("Not Found");
resp->setCloseConnection(true);
}
}
void httpOnRequest(int epfd, int clntfd)
{
std::cout << "tid: " << std::this_thread::get_id()
<< std::endl;
// 全部委托给新的线程,注意销毁
// 处理client即可
Buffer buffer_;
int saveErrno = 0;
int str_len = buffer_.readFd(clntfd, &saveErrno);
if (str_len == 0)
{
epoll_ctl(epfd, EPOLL_CTL_DEL, clntfd, NULL);
close(clntfd);
std::cout << "close client " << clntfd << std::endl;
clntfd = -1;
return;
}
else
{
// // echo事件
// int nw = ::write(clntfd, buffer_.peek(), buffer_.readableBytes());
// if (nw > 0)
// {
// buffer_.retrieve(nw);
// }
// std::cout << "write data " << clntfd << std::endl;
// 处理http请求
HttpContext *context = new HttpContext();
Timestamp receiveTime(Timestamp::now());
if (!context->parseRequest(&buffer_, receiveTime))
{
string badReq("HTTP/1.1 400 Bad Request\r\n\r\n");
int nwrote = ::write(clntfd, badReq.data(), badReq.size());
assert(nwrote == badReq.size());
}
if (context->gotAll())
{
HttpRequest req = context->request();
const string &connection = req.getHeader("Connection");
// 判断是否是长连接
bool close = connection == "close" ||
(req.getVersion() == HttpRequest::kHttp10 && connection != "Keep-Alive");
HttpResponse response(close);
selectResponse(req, &response);
Buffer buf;
response.appendToBuffer(&buf);
string temp = buf.retrieveAsString();
int nwrote = ::write(clntfd, temp.data(), temp.size());
assert(nwrote == temp.size());
if (response.closeConnection())
{
//
}
context->reset();
}
}
}
int main()
{
// 添加线程池 来处理http请求
ThreadPool pool("Test");
pool.start(5);
int sockfd = createTcpSocket();
InetAddress localaddr(9981);
Socket server(sockfd);
server.bindAddress(localaddr);
server.listen();
// 储存客户端地址和fd
int clntfd = -1;
InetAddress peeraddr(0);
//
int epfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event event;
memset(&event, 0, sizeof event);
// 然后设置所要关注事件的参数
event.data.fd = sockfd;
event.events = EPOLLIN;
if (::epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event) < 0)
{
std::cerr << "epoll_ctl error" << std::endl;
}
// 保存epoll_wait调用后的活动事件
std::vector<struct epoll_event> events(16);
for (;;)
{
int numEvents = ::epoll_wait(epfd, events.data(), events.size(), timeoutMs);
// 事件分发处理
if (numEvents > 0)
{
std::cout << numEvents << " events happended" << std::endl;
if (numEvents == events.size())
{
events.resize(events.size() * 2);
}
for (int i = 0; i < numEvents; ++i)
{
if (events[i].data.fd == sockfd)
{
clntfd = server.accept(&peeraddr);
event.data.fd = clntfd;
event.events = EPOLLIN;
if (::epoll_ctl(epfd, EPOLL_CTL_ADD, clntfd, &event) < 0)
{
std::cerr << "epoll_ctl error" << std::endl;
}
std::cout << "connect client " << clntfd << std::endl;
}
else if (events[i].data.fd >= 0)
{
std::cout << "assign task to client " << clntfd << std::endl;
httpOnRequest(epfd, events[i].data.fd);
}
}
}
else if (numEvents == 0)
{
std::cout << "nothing happened" << std::endl;
}
else
{
std::cout << "Error: epoll_wait" << std::endl;
}
}
close(epfd);
return 0;
}