-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.cc
More file actions
58 lines (48 loc) · 1.8 KB
/
HttpServer.cc
File metadata and controls
58 lines (48 loc) · 1.8 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
#include "HttpServer.h"
#include "HttpContext.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
#include "base/Logging.h"
void defaultHttpCallback(const HttpRequest &, HttpResponse *resp) {
resp->setStatusCode(HttpResponse::NotFound);
resp->setStatusMessage("Not Found");
resp->setCloseConnection(true);
}
HttpServer::HttpServer(EventLoop *loop, const InetAddress &listen_addr, const std::string &name, TcpServer::Option option)
: server_(loop, listen_addr, name, option),
http_callback_(defaultHttpCallback) {
server_.setConnectionCallback([this](const TcpConnectionPtr &conn) {
onConnection(conn);
});
server_.setMessageCallback([this](const TcpConnectionPtr &conn, Buffer *buf, Timestamp t) {
onMessage(conn, buf, t);
});
}
void HttpServer::onConnection(const TcpConnectionPtr &conn) {
if (conn->connect()) {
conn->setContext(HttpContext());
}
}
void HttpServer::onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp receive_time) {
auto *context = std::any_cast<HttpContext>(conn->getMutableContext());
if (!context->parseRequest(buf, receive_time)) {
conn->send("HTTP/1.1 400 Bad Request\r\n");
conn->shutdown();
}
if (context->gotAll()) {
onRequest(conn, context->request());
context->reset();
}
}
void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequest &req) {
const std::string &connection = req.getHeader("Connection");
bool close = connection == "close" || (req.getVersion() == HttpRequest::Http10 && connection != "Keep-Alive");
HttpResponse response(close);
http_callback_(req, &response);
Buffer buf;
response.appendToBuffer(&buf);
conn->send(buf.retrieveAllAsString());
if (response.closeConnection()) {
conn->shutdown();
}
}