X-Git-Url: https://git.piment-noir.org/?p=deb_shairplay.git;a=blobdiff_plain;f=src%2Flib%2Fhttpd.c;h=7cb6fdea61d4f322778f3407ba9f056f6afc54b7;hp=0f82b261fe34c12e81717fb447acaa33f4170dcb;hb=1e8b64fdcd07264fcb0577d4fad13428b6cfdd7c;hpb=20e91d6c2ce2072e35508666d5df2cfcc6e159c8 diff --git a/src/lib/httpd.c b/src/lib/httpd.c index 0f82b26..7cb6fde 100644 --- a/src/lib/httpd.c +++ b/src/lib/httpd.c @@ -36,8 +36,6 @@ struct httpd_s { logger_t *logger; httpd_callbacks_t callbacks; - int use_rtsp; - int max_connections; int open_connections; http_connection_t *connections; @@ -54,7 +52,7 @@ struct httpd_s { }; httpd_t * -httpd_init(logger_t *logger, httpd_callbacks_t *callbacks, int max_connections, int use_rtsp) +httpd_init(logger_t *logger, httpd_callbacks_t *callbacks, int max_connections) { httpd_t *httpd; @@ -68,7 +66,6 @@ httpd_init(logger_t *logger, httpd_callbacks_t *callbacks, int max_connections, return NULL; } - httpd->use_rtsp = !!use_rtsp; httpd->max_connections = max_connections; httpd->connections = calloc(max_connections, sizeof(http_connection_t)); if (!httpd->connections) { @@ -201,8 +198,12 @@ httpd_thread(void *arg) /* Get the correct nfds value and set rfds */ FD_ZERO(&rfds); if (httpd->open_connections < httpd->max_connections) { - FD_SET(httpd->server_fd4, &rfds); - nfds = httpd->server_fd4+1; + if (httpd->server_fd4 != -1) { + FD_SET(httpd->server_fd4, &rfds); + if (nfds <= httpd->server_fd4) { + nfds = httpd->server_fd4+1; + } + } if (httpd->server_fd6 != -1) { FD_SET(httpd->server_fd6, &rfds); if (nfds <= httpd->server_fd6) { @@ -232,7 +233,8 @@ httpd_thread(void *arg) break; } - if (FD_ISSET(httpd->server_fd4, &rfds)) { + if (httpd->open_connections < httpd->max_connections && + httpd->server_fd4 != -1 && FD_ISSET(httpd->server_fd4, &rfds)) { ret = httpd_accept_connection(httpd, httpd->server_fd4, 0); if (ret == -1) { break; @@ -240,7 +242,8 @@ httpd_thread(void *arg) continue; } } - if (httpd->server_fd6 != -1 && FD_ISSET(httpd->server_fd6, &rfds)) { + if (httpd->open_connections < httpd->max_connections && + httpd->server_fd6 != -1 && FD_ISSET(httpd->server_fd6, &rfds)) { ret = httpd_accept_connection(httpd, httpd->server_fd6, 1); if (ret == -1) { break; @@ -260,7 +263,7 @@ httpd_thread(void *arg) /* If not in the middle of request, allocate one */ if (!connection->request) { - connection->request = http_request_init(httpd->use_rtsp); + connection->request = http_request_init(); assert(connection->request); } @@ -307,6 +310,11 @@ httpd_thread(void *arg) } written += ret; } + + if (http_response_get_disconnect(response)) { + logger_log(httpd->logger, LOGGER_INFO, "Disconnecting on software request"); + httpd_remove_connection(httpd, connection); + } } else { logger_log(httpd->logger, LOGGER_INFO, "Didn't get response"); } @@ -346,6 +354,9 @@ httpd_thread(void *arg) int httpd_start(httpd_t *httpd, unsigned short *port) { + /* How many connection attempts are kept in queue */ + int backlog = 5; + assert(httpd); assert(port); @@ -367,14 +378,14 @@ httpd_start(httpd_t *httpd, unsigned short *port) logger_log(httpd->logger, LOGGER_WARNING, "Continuing without IPv6 support"); } - if (listen(httpd->server_fd4, 5) == -1) { + if (httpd->server_fd4 != -1 && listen(httpd->server_fd4, backlog) == -1) { logger_log(httpd->logger, LOGGER_ERR, "Error listening to IPv4 socket"); closesocket(httpd->server_fd4); closesocket(httpd->server_fd6); MUTEX_UNLOCK(httpd->run_mutex); return -2; } - if (httpd->server_fd6 != -1 && listen(httpd->server_fd6, 5) == -1) { + if (httpd->server_fd6 != -1 && listen(httpd->server_fd6, backlog) == -1) { logger_log(httpd->logger, LOGGER_ERR, "Error listening to IPv6 socket"); closesocket(httpd->server_fd4); closesocket(httpd->server_fd6);