12 , server_name_(server_name)
14 , close_connection_(0)
28 #ifndef ATOMIC_LONG_LOCK_FREE 29 #error "Missing ATOMIC_LONG_LOCK_FREE required by C++11 standard" 30 #endif // ATOMIC_LONG_LOCK_FREE 32 ATOMIC_LONG_LOCK_FREE == 2,
"Class requires lock-free std::atomic<long>");
37 std::lock_guard<std::mutex> guard(
mu_);
38 auto inserted =
handlers_.emplace(path, std::move(handler));
39 if (not inserted.second) {
40 throw std::runtime_error(std::string(
"duplicate handler path: ") + path);
46 if (not found.second) {
50 res.result(beast::http::status::ok);
51 res.version = req.version;
53 found.first(req, res);
56 }
catch (std::exception
const& e) {
59 JB_LOG(
info) <<
"std::exception raised while sending response: " << e.what();
64 std::ostringstream os;
65 os <<
"# HELP open_connection The number of HTTP connections opened\n" 66 <<
"# TYPE open_connection counter\n" 69 <<
"# HELP close_connection The number of HTTP connections closed\n" 70 <<
"# TYPE close_connection counter\n" 73 <<
"# HELP read_ok The number of HTTP request received successfully\n" 74 <<
"# TYPE read_ok counter\n" 77 <<
"# HELP read_error The number of errors reading HTTP requests\n" 78 <<
"# TYPE read_error counter\n" 81 <<
"# HELP response_by_code_range " 82 <<
"The number of HTTP responses within each response code range\n" 83 <<
"# TYPE response_by_code_range\n" 86 <<
"response_by_code_range{range=\"100\"} " <<
get_write_100() <<
"\n" 87 <<
"response_by_code_range{range=\"200\"} " <<
get_write_200() <<
"\n" 88 <<
"response_by_code_range{range=\"300\"} " <<
get_write_300() <<
"\n" 89 <<
"response_by_code_range{range=\"400\"} " <<
get_write_400() <<
"\n" 90 <<
"response_by_code_range{range=\"500\"} " <<
get_write_500() <<
"\n" 92 <<
"# HELP write_ok The number of HTTP responses received successfully\n" 93 <<
"# TYPE write_ok counter\n" 95 <<
"# HELP write_error The number of errors writing HTTP responses\n" 96 <<
"# TYPE write_error counter\n" 99 <<
"# HELP accept_ok The number of HTTP connections accepted\n" 100 <<
"# TYPE accept_ok counter\n" 102 <<
"# HELP accept_error The number of errors accepting HTTP connections\n" 103 <<
"# TYPE accept_error counter\n" 105 <<
"# HELP accept_closed The number accept() attempts on a closed " 107 <<
"# TYPE accept_closed counter\n" 110 res.body += os.str();
115 res.result(beast::http::status::internal_server_error);
116 res.version = req.version;
118 res.insert(
"content-type",
"text/plain");
119 res.body = std::string{
"An internal error occurred"};
126 res.result(beast::http::status::not_found);
127 res.version = req.version;
129 res.insert(
"content-type",
"text/plain");
131 std::string(
"path: ") + std::string(req.target()) +
" not found\r\n";
136 std::pair<request_handler, bool>
138 std::lock_guard<std::mutex> guard(
mu_);
139 auto location =
handlers_.find(std::string(path));
143 return std::make_pair(location->second,
true);
149 using sc = beast::http::status_class;
150 switch (beast::http::to_status_class(res.result())) {
154 case sc::informational:
160 case sc::redirection:
163 case sc::client_error:
166 case sc::server_error:
long get_open_connection() const
Returns the count of open connections.
response_type not_found(request_type const &request)
Create a 404 response.
std::map< std::string, request_handler > handlers_
The collection of handlers.
beast::http::request< beast::http::string_body > request_type
The request type used for JayBeams Embedded HTTP Servers.
long get_accept_error() const
long get_read_ok() const
Get the count of successful reads.
void count_write_100()
Count a write in the 100 range.
long get_accept_closed() const
long get_close_connection() const
Get the count of close connections.
void append_metrics(response_type &res) const
Append the internal metrics to the body of res.
void count_write_invalid()
Internally updated event counters.
long get_write_400() const
Get the count write in the 400 range.
beast::http::response< beast::http::string_body > response_type
The response type used for JayBeams Embedded HTTP Servers.
request_dispatcher(std::string const &server_name)
Create a new empty dispatcher.
void count_write_500()
Count a write in the 500 range.
response_type internal_error(request_type const &request)
Create a 500 response.
std::mutex mu_
Protect the critical sections.
long get_write_invalid() const
Get count of responses with invalid codes (outside the [100,600) range).
std::function< void(request_type const &, response_type &)> request_handler
Define the interface for request handlers.
long get_write_100() const
Get the count write in the 100 range.
std::string server_name_
The name of the server returned in all HTTP responses.
void count_write_200()
Count a write in the 200 range.
long get_write_300() const
Get the count write in the 300 range.
long get_accept_ok() const
long get_write_500() const
Get the count write in the 500 range.
long get_read_error() const
Get the count write errors.
void count_write_400()
Count a write in the 400 range.
long get_write_200() const
Get the count write in the 200 range.
void update_response_counter(response_type const &res)
Update the response code counters based on res.
long get_write_error() const
Get the count write errors.
response_type process(request_type const &request)
Process a new request using the right handler.
std::pair< request_handler, bool > find_handler(beast::string_view path) const
Find the request handler for the given path.
long get_write_ok() const
Get the count write successes.
void count_write_300()
Count a write in the 300 range.
The top-level namespace for the JayBeams library.
void add_handler(std::string const &path, request_handler &&handler)
Add a new handler.