-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatus_handler.cpp
61 lines (45 loc) · 2.02 KB
/
status_handler.cpp
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
// Based Off http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/http/server/request_handler.cpp
#include "status_handler.hpp"
#include "server_stats.hpp"
#include <fstream>
#include <sstream>
namespace http {
namespace server {
RequestHandler::Status StatusHandler::HandleRequest(const Request &request, Response* response) {
std::string body = setBodyString();
response->SetStatus(Response::ok);
response->SetBody(body);
response->AddHeader("Content-Length", std::to_string(body.size()));
response->AddHeader("Content-Type", "text/html");
return RequestHandler::OK;
}
RequestHandler::Status StatusHandler::Init(const std::string& uri_prefix, const NginxConfig& config) {
return RequestHandler::OK;
}
//sets body of string used for response->SetBody()
std::string StatusHandler::setBodyString() {
int numReq = ServerStats::getInstance().getNumRequests();
std::vector<std::pair<std::string, std::string>> handlers = ServerStats::getInstance().getHandlers();
std::vector<std::pair<std::string, Response::ResponseCode>> requests = ServerStats::getInstance().getRequests();
std::string status_page = "<html>";
status_page += "<head><title>Status Page</title></head>";
status_page += "<h1>Server Information</h1>";
status_page += "<h2>Handlers</h2>";
status_page += "<table><tr><th>URL Prefix</th><th>Handler</th></tr>";
for (unsigned int i = 0; i < handlers.size(); i++)
{
status_page += "<tr><td>" + handlers[i].first + "</td><td>"+ handlers[i].second + "</td></tr>";
}
status_page += "</table><br>";
status_page += "<h2>Requests</h2>";
status_page += "<p>Number of Requests: " + std::to_string(numReq) + "</p>" ;
status_page += "<table><tr><th>URL Requested</th><th>Response Code</th></tr>";
for (unsigned int i = 0; i < requests.size(); i++)
{
status_page += "<tr><td>" + requests[i].first + "</td><td>"+ std::to_string(requests[i].second) + "</td></tr>";
}
status_page += "</table></html>";
return status_page;
}
}
}