| 299 | } |
| 300 | |
| 301 | bool server_http_context::start() { |
| 302 | // Bind and listen |
| 303 | |
| 304 | auto & srv = pimpl->srv; |
| 305 | bool was_bound = false; |
| 306 | bool is_sock = false; |
| 307 | if (string_ends_with(std::string(hostname), ".sock")) { |
| 308 | is_sock = true; |
| 309 | LOG_INF("%s: setting address family to AF_UNIX\n", __func__); |
| 310 | srv->set_address_family(AF_UNIX); |
| 311 | // bind_to_port requires a second arg, any value other than 0 should |
| 312 | // simply get ignored |
| 313 | was_bound = srv->bind_to_port(hostname, 8080); |
| 314 | } else { |
| 315 | LOG_INF("%s: binding port with default address family\n", __func__); |
| 316 | // bind HTTP listen port |
| 317 | if (port == 0) { |
| 318 | int bound_port = srv->bind_to_any_port(hostname); |
| 319 | was_bound = (bound_port >= 0); |
| 320 | if (was_bound) { |
| 321 | port = bound_port; |
| 322 | } |
| 323 | } else { |
| 324 | was_bound = srv->bind_to_port(hostname, port); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (!was_bound) { |
| 329 | LOG_ERR("%s: couldn't bind HTTP server socket, hostname: %s, port: %d\n", __func__, hostname.c_str(), port); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | // run the HTTP server in a thread |
| 334 | thread = std::thread([this]() { pimpl->srv->listen_after_bind(); }); |
| 335 | srv->wait_until_ready(); |
| 336 | |
| 337 | listening_address = is_sock ? string_format("unix://%s", hostname.c_str()) |
| 338 | : string_format("http://%s:%d", hostname.c_str(), port); |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | void server_http_context::stop() const { |
| 343 | if (pimpl->srv) { |
no test coverage detected