| 268 | } |
| 269 | |
| 270 | bool server_http_context::start() { |
| 271 | // Bind and listen |
| 272 | |
| 273 | auto & srv = pimpl->srv; |
| 274 | bool was_bound = false; |
| 275 | bool is_sock = false; |
| 276 | if (string_ends_with(std::string(hostname), ".sock")) { |
| 277 | is_sock = true; |
| 278 | LOG_INF("%s: setting address family to AF_UNIX\n", __func__); |
| 279 | srv->set_address_family(AF_UNIX); |
| 280 | // bind_to_port requires a second arg, any value other than 0 should |
| 281 | // simply get ignored |
| 282 | was_bound = srv->bind_to_port(hostname, 8080); |
| 283 | } else { |
| 284 | LOG_INF("%s: binding port with default address family\n", __func__); |
| 285 | // bind HTTP listen port |
| 286 | if (port == 0) { |
| 287 | int bound_port = srv->bind_to_any_port(hostname); |
| 288 | was_bound = (bound_port >= 0); |
| 289 | if (was_bound) { |
| 290 | port = bound_port; |
| 291 | } |
| 292 | } else { |
| 293 | was_bound = srv->bind_to_port(hostname, port); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (!was_bound) { |
| 298 | LOG_ERR("%s: couldn't bind HTTP server socket, hostname: %s, port: %d\n", __func__, hostname.c_str(), port); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // run the HTTP server in a thread |
| 303 | thread = std::thread([this]() { pimpl->srv->listen_after_bind(); }); |
| 304 | srv->wait_until_ready(); |
| 305 | |
| 306 | listening_address = is_sock ? string_format("unix://%s", hostname.c_str()) |
| 307 | : string_format("http://%s:%d", hostname.c_str(), port); |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | void server_http_context::stop() const { |
| 312 | if (pimpl->srv) { |
no test coverage detected