-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.cpp
More file actions
557 lines (476 loc) · 15.9 KB
/
Copy pathhttp_server.cpp
File metadata and controls
557 lines (476 loc) · 15.9 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "http_server.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <algorithm>
#include <fstream>
#include <filesystem>
//HttpRequest 实现
bool HttpRequest::parse(const std::string& raw){
size_t headerEnd = raw.find("\r\n\r\n");
if (headerEnd == std::string::npos){
return false;
}
std::string headerPart = raw.substr(0, headerEnd);
std::istringstream iss(headerPart);
std::string line;
if (!std::getline(iss, line) || line.empty()){
return false;
}
if (line.back() == '\r') line.pop_back();
std::istringstream lineStream(line);
if (!(lineStream >> method >> path >> version)){
return false;
}
while (std::getline(iss, line) && !line.empty() && line != "\r"){
if (line.back() == '\r') line.pop_back();
size_t colonPos = line.find(':');
if (colonPos != std::string::npos){
std::string key = line.substr(0, colonPos);
std::string value = line.substr(colonPos + 1);
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1);
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
headers[key] = value;
}
}
auto it = headers.find("content-length");
if (it != headers.end()){
size_t contentLength = std::stoul(it->second);
size_t bodyStart = headerEnd + 4;
if (raw.length() >= bodyStart + contentLength){
body = raw.substr(bodyStart, contentLength);
}
}
parseComplete = true;
return true;
}
void HttpRequest::reset(){
method.clear();
path.clear();
version.clear();
headers.clear();
body.clear();
parseComplete = false;
}
//HttpResponse 实现
std::string HttpResponse::toString() const{
std::ostringstream oss;
oss << "HTTP/1.1 " << statusCode << " " << statusMessage << "\r\n";
for (const auto& [key, value] : headers){
oss << key << ": " << value << "\r\n";
}
oss << "\r\n";
oss << body;
return oss.str();
}
void HttpResponse::setKeepAlive(bool keep){
if (keep){
headers["Connection"] = "keep-alive";
headers["Keep-Alive"] = "timeout=5, max=100";
} else{
headers["Connection"] = "close";
}
}
//ThreadPool 实现
ThreadPool::ThreadPool(int threadNum){
for (int i = 0; i < threadNum; ++i){
workers_.emplace_back([this] {
while (true) {
Task task;
{
std::unique_lock<std::mutex> lock(mtx_);
cv_.wait(lock, [this] { return stop_ || !tasks_.empty(); });
if (stop_ && tasks_.empty()) return;
task = std::move(tasks_.front());
tasks_.pop();
}
task();
}
});
}
LOG_INFO("ThreadPool initialized with %d threads", threadNum);
}
ThreadPool::~ThreadPool(){
{
std::lock_guard<std::mutex> lock(mtx_);
stop_ = true;
}
cv_.notify_all();
for (auto& t : workers_) {
if (t.joinable()) t.join();
}
LOG_INFO("ThreadPool destroyed");
}
void ThreadPool::addTask(Task task){
{
std::lock_guard<std::mutex> lock(mtx_);
tasks_.push(std::move(task));
}
cv_.notify_one();
}
size_t ThreadPool::taskCount() const {
std::lock_guard<std::mutex> lock(mtx_);
return tasks_.size();
}
//httpserver实现
HttpServer::HttpServer(int port, int threadNum) : port_(port), pool_(threadNum){
listenFd_.reset(socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0));
if (listenFd_.get() < 0){
LOG_ERROR("Failed to create socket");
throw std::runtime_error("Socket creation failed");
}
int opt = 1;
if (setsockopt(listenFd_.get(), SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0){
LOG_ERROR("Failed to set SO_REUSEADDR");
}
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listenFd_.get(), (sockaddr*)&addr, sizeof(addr)) < 0){
LOG_ERROR("Failed to bind port %d", port);
throw std::runtime_error("Bind failed");
}
if (listen(listenFd_.get(), SOMAXCONN) < 0){
LOG_ERROR("Failed to listen");
throw std::runtime_error("Listen failed");
}
epollFd_.reset(epoll_create1(0));
if (epollFd_.get() < 0){
LOG_ERROR("Failed to create epoll");
throw std::runtime_error("Epoll creation failed");
}
epoll_event ev{};
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listenFd_.get();
if (epoll_ctl(epollFd_.get(), EPOLL_CTL_ADD, listenFd_.get(), &ev) < 0){
LOG_ERROR("Failed to add listen fd to epoll");
throw std::runtime_error("Epoll add failed");
}
LOG_INFO("HttpServer initialized on port %d", port);
}
HttpServer::~HttpServer(){
running_ = false;
if (cleanupThread_.joinable()){
cleanupThread_.join();
}
LOG_INFO("HttpServer destroyed");
}
int HttpServer::setNonBlock(int fd){
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1){
LOG_ERROR("fcntl F_GETFL failed");
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1){
LOG_ERROR("fcntl F_SETFL failed");
return -1;
}
return 0;
}
void HttpServer::addEpoll(int fd){
setNonBlock(fd);
epoll_event ev{};
ev.events = EPOLLIN | EPOLLONESHOT | EPOLLET;
ev.data.fd = fd;
if (epoll_ctl(epollFd_.get(), EPOLL_CTL_ADD, fd, &ev) < 0){
LOG_ERROR("Failed to add fd %d to epoll", fd);
}
}
void HttpServer::modEpoll(int fd, uint32_t events){
epoll_event ev{};
ev.events = events | EPOLLONESHOT | EPOLLET;
ev.data.fd = fd;
if (epoll_ctl(epollFd_.get(), EPOLL_CTL_MOD, fd, &ev) < 0){
LOG_ERROR("Failed to modify fd %d in epoll", fd);
}
}
void HttpServer::delEpoll(int fd){
epoll_ctl(epollFd_.get(), EPOLL_CTL_DEL, fd, nullptr);
}
void HttpServer::closeConn(int fd){
std::unique_lock<std::shared_mutex> lock(connMutex_);
auto it = conns_.find(fd);
if (it != conns_.end()){
delEpoll(fd);
conns_.erase(it);
LOG_INFO("Connection closed: fd=%d", fd);
}
}
void HttpServer::run(){
startCleanupTimer();
std::vector<epoll_event> events(1024);
LOG_INFO("HttpServer started, listening on port %d", port_);
while (running_){
int n = epoll_wait(epollFd_.get(), events.data(), events.size(), 1000);
if (n < 0) {
if (errno == EINTR) continue;
LOG_ERROR("epoll_wait failed");
break;
}
for (int i = 0; i < n; ++i){
int fd = events[i].data.fd;
uint32_t ev = events[i].events;
if (fd == listenFd_.get()){
handleAccept();
} else if (ev & (EPOLLERR | EPOLLHUP)) {
LOG_INFO("Connection error on fd %d", fd);
closeConn(fd);
} else if (ev & EPOLLIN){
handleRead(fd);
} else if (ev & EPOLLOUT){
handleWrite(fd);
}
}
}
}
void HttpServer::handleAccept(){
while (true){
sockaddr_in addr{};
socklen_t len = sizeof(addr);
int cfd = accept4(listenFd_.get(), (sockaddr*)&addr, &len, SOCK_NONBLOCK);
if(cfd < 0){
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
LOG_ERROR("accept failed");
break;
}
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr.sin_addr, ip, sizeof(ip));
LOG_INFO("New connection: fd=%d, ip=%s:%d", cfd, ip, ntohs(addr.sin_port));
addEpoll(cfd);
std::unique_lock<std::shared_mutex> lock(connMutex_);
conns_[cfd] = std::make_shared<Conn>(cfd);
}
}
void HttpServer::handleRead(int fd) {
ConnPtr conn;
{
std::shared_lock<std::shared_mutex> lock(connMutex_);
auto it = conns_.find(fd);
if (it == conns_.end()) return;
conn = it->second;
}
char buf[8192];
ssize_t n = 0;
size_t totalRead = 0;
while (true){
n = read(fd, buf, sizeof(buf) - 1);
if(n > 0){
conn->readBuf.append(buf, n);
totalRead += n;
conn->lastActive = time(nullptr);
}else if (n == 0){
LOG_INFO("Client closed connection: fd=%d", fd);
closeConn(fd);
return;
}else{
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
LOG_ERROR("read error on fd %d", fd);
closeConn(fd);
return;
}
}
if (totalRead > 0){
LOG_INFO("Read %zu bytes from fd=%d", totalRead, fd);
if(conn->request.parse(conn->readBuf)) {
conn->state = ConnState::PROCESSING;
pool_.addTask([this, fd, conn] {
handleProcess(fd, conn);
});
}else{
modEpoll(fd, EPOLLIN);
}
}else{
modEpoll(fd, EPOLLIN);
}
}
void HttpServer::handleProcess(int fd, ConnPtr conn){
processRequest(conn);
prepareResponse(conn);
size_t contentLength = 0;
auto it = conn->request.headers.find("content-length");
if(it != conn->request.headers.end()) {
contentLength = std::stoul(it->second);
}
size_t headerEnd = conn->readBuf.find("\r\n\r\n");
size_t totalLength = headerEnd + 4 + contentLength;
if(conn->readBuf.length() > totalLength){
conn->readBuf = conn->readBuf.substr(totalLength);
}else{
conn->readBuf.clear();
}
conn->request.reset();
conn->state = ConnState::WRITING;
modEpoll(fd, EPOLLOUT);
}
void HttpServer::handleWrite(int fd){
ConnPtr conn;
{
std::shared_lock<std::shared_mutex> lock(connMutex_);
auto it = conns_.find(fd);
if (it == conns_.end()) return;
conn = it->second;
}
if(conn->writeBuf.empty()){
conn->state = ConnState::READING;
modEpoll(fd, EPOLLIN);
return;
}
ssize_t n = 0;
size_t totalWritten = 0;
while (!conn->writeBuf.empty()){
n = write(fd, conn->writeBuf.data(), conn->writeBuf.size());
if(n > 0){
conn->writeBuf.erase(0, n);
totalWritten += n;
conn->lastActive = time(nullptr);
}else if(n == 0){
LOG_INFO("Write returned 0 on fd=%d", fd);
closeConn(fd);
return;
}else{
if(errno == EAGAIN || errno == EWOULDBLOCK){
modEpoll(fd, EPOLLOUT);
break;
}
LOG_ERROR("write error on fd %d", fd);
closeConn(fd);
return;
}
}
if (totalWritten > 0){
LOG_INFO("Wrote %zu bytes to fd=%d", totalWritten, fd);
}
if (conn->writeBuf.empty()){
if (!conn->keepAlive) {
LOG_INFO("Closing connection (keep-alive: false): fd=%d", fd);
closeConn(fd);
}else{
if (!conn->readBuf.empty() && conn->request.parse(conn->readBuf)){
pool_.addTask([this, fd, conn] {
handleProcess(fd, conn);
});
}else{
conn->state = ConnState::READING;
modEpoll(fd, EPOLLIN);
}
}
}
}
void HttpServer::processRequest(ConnPtr conn){
LOG_INFO("Processing request: %s %s",
conn->request.method.c_str(), conn->request.path.c_str());
auto it = conn->request.headers.find("connection");
if (it != conn->request.headers.end()) {
std::string connHeader = it->second;
std::transform(connHeader.begin(), connHeader.end(), connHeader.begin(), ::tolower);
conn->keepAlive = (connHeader == "keep-alive");
} else {
conn->keepAlive = (conn->request.version == "HTTP/1.1");
}
if (conn->request.path == "/" || conn->request.path == "/index.html"){
conn->response.statusCode = 200;
conn->response.statusMessage = "OK";
conn->response.body =
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <meta charset=\"UTF-8\">\n"
" <title>Mini HttpServer</title>\n"
" <style>\n"
" body { font-family: Arial, sans-serif; margin: 40px; }\n"
" h1 { color: #333; }\n"
" .info { background: #f0f0f0; padding: 20px; border-radius: 5px; }\n"
" </style>\n"
"</head>\n"
"<body>\n"
" <h1>🚀 Mini HttpServer</h1>\n"
" <div class=\"info\">\n"
" <p>Welcome to Mini HttpServer!</p>\n"
" <p>This server is built with:</p>\n"
" <ul>\n"
" <li>Epoll (Edge Triggered)</li>\n"
" <li>Non-blocking I/O</li>\n"
" <li>Thread Pool</li>\n"
" <li>HTTP/1.1 Keep-Alive</li>\n"
" </ul>\n"
" <p>Request Info:</p>\n"
" <ul>\n"
" <li>Method: " + conn->request.method + "</li>\n"
" <li>Path: " + conn->request.path + "</li>\n"
" <li>Version: " + conn->request.version + "</li>\n"
" </ul>\n"
" </div>\n"
"</body>\n"
"</html>";
} else if (conn->request.path == "/health"){
conn->response.statusCode = 200;
conn->response.statusMessage = "OK";
conn->response.body = "{\"status\":\"ok\",\"connections\":" +
std::to_string(conns_.size()) + "}";
} else {
handleNotFound(conn);
}
}
void HttpServer::prepareResponse(ConnPtr conn){
conn->response.headers["Content-Type"] = "text/html; charset=utf-8";
conn->response.headers["Content-Length"] = std::to_string(conn->response.body.size());
conn->response.headers["Server"] = "Mini-HttpServer/1.0";
conn->response.setKeepAlive(conn->keepAlive);
conn->writeBuf = conn->response.toString();
LOG_INFO("Prepared response: status=%d, size=%zu, keep-alive=%d",
conn->response.statusCode, conn->writeBuf.size(), conn->keepAlive);
}
void HttpServer::handleNotFound(ConnPtr conn){
conn->response.statusCode = 404;
conn->response.statusMessage = "Not Found";
conn->response.body =
"<!DOCTYPE html>\n"
"<html>\n"
"<head><title>404 Not Found</title></head>\n"
"<body>\n"
" <h1>404 Not Found</h1>\n"
" <p>The requested URL " + conn->request.path + " was not found on this server.</p>\n"
"</body>\n"
"</html>";
}
void HttpServer::handleBadRequest(ConnPtr conn){
conn->response.statusCode = 400;
conn->response.statusMessage = "Bad Request";
conn->response.body =
"<!DOCTYPE html>\n"
"<html>\n"
"<head><title>400 Bad Request</title></head>\n"
"<body>\n"
" <h1>400 Bad Request</h1>\n"
"</body>\n"
"</html>";
}
void HttpServer::startCleanupTimer(){
cleanupThread_ = std::thread([this]{
while(running_) {
std::this_thread::sleep_for(std::chrono::seconds(30));
cleanupIdleConnections();
}
});
}
void HttpServer::cleanupIdleConnections(){
std::unique_lock<std::shared_mutex> lock(connMutex_);
time_t now = time(nullptr);
std::vector<int> toClose;
for(auto& [fd, conn] : conns_){
if (now - conn->lastActive > 60) {
toClose.push_back(fd);
}
}
lock.unlock();
for(int fd : toClose){
LOG_INFO("Closing idle connection: fd=%d", fd);
closeConn(fd);
}
}