MCPcopy Create free account
hub / github.com/caozhiyi/CppNet / OnRequest

Function OnRequest

test/http/HttpServerTest.cpp:14–55  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13std::string GetFile();
14void OnRequest(const CHttpRequest& req, CHttpResponse& resp) {
15 //std::cout << "Headers " << req.GetMethodString() << " " << req.GetPath() << std::endl;
16 if (!benchmark) {
17 const std::map<std::string, std::string>& headers = req.GetHeaders();
18 for (const auto& header : headers) {
19 std::cout << header.first << ": " << header.second << std::endl;
20 }
21 }
22
23 if (req.GetPath() == "/") {
24 resp.SetStatusCode(k200Ok);
25 resp.SetStatusMessage("OK");
26 resp.SetContentType("text/html");
27 resp.AddHeader("Server", "CppNet");
28 CHttpServer::_time_tool.Now();
29 std::string now = std::to_string(CHttpServer::_time_tool.GetMsec());
30 resp.SetBody("<html><head><title>This is title</title></head>"
31 "<body><h1>Hello</h1>Now is " + now +
32 "</body></html>");
33
34 } else if (req.GetPath() == "/favicon.ico") {
35 resp.SetStatusCode(k200Ok);
36 resp.SetStatusMessage("OK");
37 resp.SetContentType("image/png");
38 if (image.empty()) {
39 image = GetFile();
40 }
41 resp.SetBody(image);
42
43 } else if (req.GetPath() == "/hello") {
44 resp.SetStatusCode(k200Ok);
45 resp.SetStatusMessage("OK");
46 resp.SetContentType("text/plain");
47 resp.AddHeader("Server", "CppNet");
48 resp.SetBody("hello, world!\n");
49
50 } else {
51 resp.SetStatusCode(k404NotFound);
52 resp.SetStatusMessage("Not Found");
53 resp.SetCloseConnection(true);
54 }
55}
56
57std::string GetFile() {
58 std::ifstream t("logo.png");

Callers 1

OnMessageMethod · 0.85

Calls 9

GetFileFunction · 0.85
SetStatusCodeMethod · 0.80
SetStatusMessageMethod · 0.80
SetContentTypeMethod · 0.80
NowMethod · 0.80
GetMsecMethod · 0.80
SetBodyMethod · 0.80
SetCloseConnectionMethod · 0.80
AddHeaderMethod · 0.45

Tested by

no test coverage detected