| 12 | |
| 13 | std::string GetFile(); |
| 14 | void 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 | |
| 57 | std::string GetFile() { |
| 58 | std::ifstream t("logo.png"); |
no test coverage detected