Make the maximum number of clients configurable
[deb_shairplay.git] / src / lib / httpd.c
index 5779c0646e6501c8b012ba815bf547c891bf99d0..2bed984203ebbdd907eb480daa8ea20749f2c682 100644 (file)
@@ -1,3 +1,17 @@
+/**
+ *  Copyright (C) 2011-2012  Juho Vähä-Herttua
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ */
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -25,6 +39,7 @@ struct httpd_s {
        int use_rtsp;
 
        int max_connections;
+       int open_connections;
        http_connection_t *connections;
 
        /* These variables only edited mutex locked */
@@ -95,12 +110,14 @@ httpd_add_connection(httpd_t *httpd, int fd, unsigned char *local, int local_len
                }
        }
        if (i == httpd->max_connections) {
-               logger_log(httpd->logger, LOGGER_INFO, "Max connections reached\n");
+               /* This code should never be reached, we do not select server_fd when full */
+               logger_log(httpd->logger, LOGGER_INFO, "Max connections reached");
                shutdown(fd, SHUT_RDWR);
                closesocket(fd);
                return;
        }
 
+       httpd->open_connections++;
        httpd->connections[i].socket_fd = fd;
        httpd->connections[i].connected = 1;
        httpd->connections[i].user_data = httpd->callbacks.conn_init(httpd->callbacks.opaque, local, local_len, remote, remote_len);
@@ -117,6 +134,7 @@ httpd_remove_connection(httpd_t *httpd, http_connection_t *connection)
        shutdown(connection->socket_fd, SHUT_WR);
        closesocket(connection->socket_fd);
        connection->connected = 0;
+       httpd->open_connections--;
 }
 
 static THREAD_RETVAL
@@ -147,8 +165,10 @@ httpd_thread(void *arg)
 
                /* Get the correct nfds value and set rfds */
                FD_ZERO(&rfds);
-               FD_SET(httpd->server_fd, &rfds);
-               nfds = httpd->server_fd+1;
+               if (httpd->open_connections < httpd->max_connections) {
+                       FD_SET(httpd->server_fd, &rfds);
+                       nfds = httpd->server_fd+1;
+               }
                for (i=0; i<httpd->max_connections; i++) {
                        int socket_fd;
                        if (!httpd->connections[i].connected) {
@@ -167,7 +187,7 @@ httpd_thread(void *arg)
                        continue;
                } else if (ret == -1) {
                        /* FIXME: Error happened */
-                       logger_log(httpd->logger, LOGGER_INFO, "Error in select\n");
+                       logger_log(httpd->logger, LOGGER_INFO, "Error in select");
                        break;
                }
 
@@ -194,7 +214,7 @@ httpd_thread(void *arg)
                                continue;
                        }
 
-                       logger_log(httpd->logger, LOGGER_INFO, "Accepted client on socket %d\n", fd);
+                       logger_log(httpd->logger, LOGGER_INFO, "Accepted client on socket %d", fd);
                        local = netutils_get_address(&local_saddr, &local_len);
                        remote = netutils_get_address(&remote_saddr, &remote_len);
 
@@ -216,10 +236,10 @@ httpd_thread(void *arg)
                                assert(connection->request);
                        }
 
-                       logger_log(httpd->logger, LOGGER_DEBUG, "Receiving on socket %d\n", httpd->connections[i].socket_fd);
+                       logger_log(httpd->logger, LOGGER_DEBUG, "Receiving on socket %d", connection->socket_fd);
                        ret = recv(connection->socket_fd, buffer, sizeof(buffer), 0);
                        if (ret == 0) {
-                               logger_log(httpd->logger, LOGGER_INFO, "Connection closed\n");
+                               logger_log(httpd->logger, LOGGER_INFO, "Connection closed for socket %d", connection->socket_fd);
                                httpd_remove_connection(httpd, connection);
                                continue;
                        }
@@ -227,7 +247,7 @@ httpd_thread(void *arg)
                        /* Parse HTTP request from data read from connection */
                        http_request_add_data(connection->request, buffer, ret);
                        if (http_request_has_error(connection->request)) {
-                               logger_log(httpd->logger, LOGGER_INFO, "Error in parsing: %s\n", http_request_get_error_name(connection->request));
+                               logger_log(httpd->logger, LOGGER_INFO, "Error in parsing: %s", http_request_get_error_name(connection->request));
                                httpd_remove_connection(httpd, connection);
                                continue;
                        }
@@ -254,13 +274,13 @@ httpd_thread(void *arg)
                                                ret = send(connection->socket_fd, data+written, datalen-written, 0);
                                                if (ret == -1) {
                                                        /* FIXME: Error happened */
-                                                       logger_log(httpd->logger, LOGGER_INFO, "Error in sending data\n");
+                                                       logger_log(httpd->logger, LOGGER_INFO, "Error in sending data");
                                                        break;
                                                }
                                                written += ret;
                                        }
                                } else {
-                                       logger_log(httpd->logger, LOGGER_INFO, "Didn't get response\n");
+                                       logger_log(httpd->logger, LOGGER_INFO, "Didn't get response");
                                }
                                http_response_destroy(response);
                        }
@@ -274,11 +294,11 @@ httpd_thread(void *arg)
                if (!connection->connected) {
                        continue;
                }
-               logger_log(httpd->logger, LOGGER_INFO, "Removing connection\n");
+               logger_log(httpd->logger, LOGGER_INFO, "Removing connection for socket %d", connection->socket_fd);
                httpd_remove_connection(httpd, connection);
        }
 
-       logger_log(httpd->logger, LOGGER_INFO, "Exiting thread\n");
+       logger_log(httpd->logger, LOGGER_INFO, "Exiting HTTP thread");
 
        return 0;
 }
@@ -297,16 +317,21 @@ httpd_start(httpd_t *httpd, unsigned short *port)
 
        httpd->server_fd = netutils_init_socket(port, 1, 0);
        if (httpd->server_fd == -1) {
-               logger_log(httpd->logger, LOGGER_INFO, "Error initialising socket %d\n", SOCKET_GET_ERROR());
+               logger_log(httpd->logger, LOGGER_INFO, "Error initialising IPv6 socket %d", SOCKET_GET_ERROR());
+               logger_log(httpd->logger, LOGGER_INFO, "Attempting to fall back to IPv4");
+               httpd->server_fd = netutils_init_socket(port, 0, 0);
+       }
+       if (httpd->server_fd == -1) {
+               logger_log(httpd->logger, LOGGER_INFO, "Error initialising socket %d", SOCKET_GET_ERROR());
                MUTEX_UNLOCK(httpd->run_mutex);
                return -1;
        }
        if (listen(httpd->server_fd, 5) == -1) {
-               logger_log(httpd->logger, LOGGER_INFO, "Error listening to socket\n");
+               logger_log(httpd->logger, LOGGER_INFO, "Error listening to socket");
                MUTEX_UNLOCK(httpd->run_mutex);
                return -2;
        }
-       logger_log(httpd->logger, LOGGER_INFO, "Initialized server socket\n");
+       logger_log(httpd->logger, LOGGER_INFO, "Initialized server socket");
 
        /* Set values correctly and create new thread */
        httpd->running = 1;
@@ -317,6 +342,20 @@ httpd_start(httpd_t *httpd, unsigned short *port)
        return 1;
 }
 
+int
+httpd_is_running(httpd_t *httpd)
+{
+       int running;
+
+       assert(httpd);
+
+       MUTEX_LOCK(httpd->run_mutex);
+       running = httpd->running || !httpd->joined;
+       MUTEX_UNLOCK(httpd->run_mutex);
+
+       return running;
+}
+
 void
 httpd_stop(httpd_t *httpd)
 {