| 1 | /** |
| 2 | * Copyright (C) 2011-2012 Juho Vähä-Herttua |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <stdio.h> |
| 18 | #include <assert.h> |
| 19 | |
| 20 | #include "httpd.h" |
| 21 | #include "netutils.h" |
| 22 | #include "http_request.h" |
| 23 | #include "compat.h" |
| 24 | #include "logger.h" |
| 25 | |
| 26 | struct http_connection_s { |
| 27 | int connected; |
| 28 | |
| 29 | int socket_fd; |
| 30 | void *user_data; |
| 31 | http_request_t *request; |
| 32 | }; |
| 33 | typedef struct http_connection_s http_connection_t; |
| 34 | |
| 35 | struct httpd_s { |
| 36 | logger_t *logger; |
| 37 | httpd_callbacks_t callbacks; |
| 38 | |
| 39 | int use_rtsp; |
| 40 | |
| 41 | int max_connections; |
| 42 | int open_connections; |
| 43 | http_connection_t *connections; |
| 44 | |
| 45 | /* These variables only edited mutex locked */ |
| 46 | int running; |
| 47 | int joined; |
| 48 | thread_handle_t thread; |
| 49 | mutex_handle_t run_mutex; |
| 50 | |
| 51 | /* Server fds for accepting connections */ |
| 52 | int server_fd4; |
| 53 | int server_fd6; |
| 54 | }; |
| 55 | |
| 56 | httpd_t * |
| 57 | httpd_init(logger_t *logger, httpd_callbacks_t *callbacks, int max_connections, int use_rtsp) |
| 58 | { |
| 59 | httpd_t *httpd; |
| 60 | |
| 61 | assert(logger); |
| 62 | assert(callbacks); |
| 63 | assert(max_connections > 0); |
| 64 | |
| 65 | /* Allocate the httpd_t structure */ |
| 66 | httpd = calloc(1, sizeof(httpd_t)); |
| 67 | if (!httpd) { |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | httpd->use_rtsp = !!use_rtsp; |
| 72 | httpd->max_connections = max_connections; |
| 73 | httpd->connections = calloc(max_connections, sizeof(http_connection_t)); |
| 74 | if (!httpd->connections) { |
| 75 | free(httpd); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | /* Use the logger provided */ |
| 80 | httpd->logger = logger; |
| 81 | |
| 82 | /* Save callback pointers */ |
| 83 | memcpy(&httpd->callbacks, callbacks, sizeof(httpd_callbacks_t)); |
| 84 | |
| 85 | /* Initial status joined */ |
| 86 | httpd->running = 0; |
| 87 | httpd->joined = 1; |
| 88 | |
| 89 | return httpd; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | httpd_destroy(httpd_t *httpd) |
| 94 | { |
| 95 | if (httpd) { |
| 96 | httpd_stop(httpd); |
| 97 | |
| 98 | free(httpd->connections); |
| 99 | free(httpd); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | httpd_add_connection(httpd_t *httpd, int fd, unsigned char *local, int local_len, unsigned char *remote, int remote_len) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | for (i=0; i<httpd->max_connections; i++) { |
| 109 | if (!httpd->connections[i].connected) { |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (i == httpd->max_connections) { |
| 114 | /* This code should never be reached, we do not select server_fds when full */ |
| 115 | logger_log(httpd->logger, LOGGER_INFO, "Max connections reached"); |
| 116 | shutdown(fd, SHUT_RDWR); |
| 117 | closesocket(fd); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | httpd->open_connections++; |
| 122 | httpd->connections[i].socket_fd = fd; |
| 123 | httpd->connections[i].connected = 1; |
| 124 | httpd->connections[i].user_data = httpd->callbacks.conn_init(httpd->callbacks.opaque, local, local_len, remote, remote_len); |
| 125 | } |
| 126 | |
| 127 | static int |
| 128 | httpd_accept_connection(httpd_t *httpd, int server_fd, int is_ipv6) |
| 129 | { |
| 130 | struct sockaddr_storage remote_saddr; |
| 131 | socklen_t remote_saddrlen; |
| 132 | struct sockaddr_storage local_saddr; |
| 133 | socklen_t local_saddrlen; |
| 134 | unsigned char *local, *remote; |
| 135 | int local_len, remote_len; |
| 136 | int ret, fd; |
| 137 | |
| 138 | remote_saddrlen = sizeof(remote_saddr); |
| 139 | fd = accept(server_fd, (struct sockaddr *)&remote_saddr, &remote_saddrlen); |
| 140 | if (fd == -1) { |
| 141 | /* FIXME: Error happened */ |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | local_saddrlen = sizeof(local_saddr); |
| 146 | ret = getsockname(fd, (struct sockaddr *)&local_saddr, &local_saddrlen); |
| 147 | if (ret == -1) { |
| 148 | closesocket(fd); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | logger_log(httpd->logger, LOGGER_INFO, "Accepted %s client on socket %d", |
| 153 | (is_ipv6 ? "IPv6" : "IPv4"), fd); |
| 154 | local = netutils_get_address(&local_saddr, &local_len); |
| 155 | remote = netutils_get_address(&remote_saddr, &remote_len); |
| 156 | |
| 157 | httpd_add_connection(httpd, fd, local, local_len, remote, remote_len); |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | httpd_remove_connection(httpd_t *httpd, http_connection_t *connection) |
| 163 | { |
| 164 | if (connection->request) { |
| 165 | http_request_destroy(connection->request); |
| 166 | connection->request = NULL; |
| 167 | } |
| 168 | httpd->callbacks.conn_destroy(connection->user_data); |
| 169 | shutdown(connection->socket_fd, SHUT_WR); |
| 170 | closesocket(connection->socket_fd); |
| 171 | connection->connected = 0; |
| 172 | httpd->open_connections--; |
| 173 | } |
| 174 | |
| 175 | static THREAD_RETVAL |
| 176 | httpd_thread(void *arg) |
| 177 | { |
| 178 | httpd_t *httpd = arg; |
| 179 | char buffer[1024]; |
| 180 | int i; |
| 181 | |
| 182 | assert(httpd); |
| 183 | |
| 184 | while (1) { |
| 185 | fd_set rfds; |
| 186 | struct timeval tv; |
| 187 | int nfds=0; |
| 188 | int ret; |
| 189 | |
| 190 | MUTEX_LOCK(httpd->run_mutex); |
| 191 | if (!httpd->running) { |
| 192 | MUTEX_UNLOCK(httpd->run_mutex); |
| 193 | break; |
| 194 | } |
| 195 | MUTEX_UNLOCK(httpd->run_mutex); |
| 196 | |
| 197 | /* Set timeout value to 5ms */ |
| 198 | tv.tv_sec = 1; |
| 199 | tv.tv_usec = 5000; |
| 200 | |
| 201 | /* Get the correct nfds value and set rfds */ |
| 202 | FD_ZERO(&rfds); |
| 203 | if (httpd->open_connections < httpd->max_connections) { |
| 204 | FD_SET(httpd->server_fd4, &rfds); |
| 205 | nfds = httpd->server_fd4+1; |
| 206 | if (httpd->server_fd6 != -1) { |
| 207 | FD_SET(httpd->server_fd6, &rfds); |
| 208 | if (nfds <= httpd->server_fd6) { |
| 209 | nfds = httpd->server_fd6+1; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | for (i=0; i<httpd->max_connections; i++) { |
| 214 | int socket_fd; |
| 215 | if (!httpd->connections[i].connected) { |
| 216 | continue; |
| 217 | } |
| 218 | socket_fd = httpd->connections[i].socket_fd; |
| 219 | FD_SET(socket_fd, &rfds); |
| 220 | if (nfds <= socket_fd) { |
| 221 | nfds = socket_fd+1; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | ret = select(nfds, &rfds, NULL, NULL, &tv); |
| 226 | if (ret == 0) { |
| 227 | /* Timeout happened */ |
| 228 | continue; |
| 229 | } else if (ret == -1) { |
| 230 | /* FIXME: Error happened */ |
| 231 | logger_log(httpd->logger, LOGGER_INFO, "Error in select"); |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | if (FD_ISSET(httpd->server_fd4, &rfds)) { |
| 236 | ret = httpd_accept_connection(httpd, httpd->server_fd4, 0); |
| 237 | if (ret == -1) { |
| 238 | break; |
| 239 | } else if (ret == 0) { |
| 240 | continue; |
| 241 | } |
| 242 | } |
| 243 | if (httpd->server_fd6 != -1 && FD_ISSET(httpd->server_fd6, &rfds)) { |
| 244 | ret = httpd_accept_connection(httpd, httpd->server_fd6, 1); |
| 245 | if (ret == -1) { |
| 246 | break; |
| 247 | } else if (ret == 0) { |
| 248 | continue; |
| 249 | } |
| 250 | } |
| 251 | for (i=0; i<httpd->max_connections; i++) { |
| 252 | http_connection_t *connection = &httpd->connections[i]; |
| 253 | |
| 254 | if (!connection->connected) { |
| 255 | continue; |
| 256 | } |
| 257 | if (!FD_ISSET(connection->socket_fd, &rfds)) { |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | /* If not in the middle of request, allocate one */ |
| 262 | if (!connection->request) { |
| 263 | connection->request = http_request_init(httpd->use_rtsp); |
| 264 | assert(connection->request); |
| 265 | } |
| 266 | |
| 267 | logger_log(httpd->logger, LOGGER_DEBUG, "Receiving on socket %d", connection->socket_fd); |
| 268 | ret = recv(connection->socket_fd, buffer, sizeof(buffer), 0); |
| 269 | if (ret == 0) { |
| 270 | logger_log(httpd->logger, LOGGER_INFO, "Connection closed for socket %d", connection->socket_fd); |
| 271 | httpd_remove_connection(httpd, connection); |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | /* Parse HTTP request from data read from connection */ |
| 276 | http_request_add_data(connection->request, buffer, ret); |
| 277 | if (http_request_has_error(connection->request)) { |
| 278 | logger_log(httpd->logger, LOGGER_INFO, "Error in parsing: %s", http_request_get_error_name(connection->request)); |
| 279 | httpd_remove_connection(httpd, connection); |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | /* If request is finished, process and deallocate */ |
| 284 | if (http_request_is_complete(connection->request)) { |
| 285 | http_response_t *response = NULL; |
| 286 | |
| 287 | httpd->callbacks.conn_request(connection->user_data, connection->request, &response); |
| 288 | http_request_destroy(connection->request); |
| 289 | connection->request = NULL; |
| 290 | |
| 291 | if (response) { |
| 292 | const char *data; |
| 293 | int datalen; |
| 294 | int written; |
| 295 | int ret; |
| 296 | |
| 297 | /* Get response data and datalen */ |
| 298 | data = http_response_get_data(response, &datalen); |
| 299 | |
| 300 | written = 0; |
| 301 | while (written < datalen) { |
| 302 | ret = send(connection->socket_fd, data+written, datalen-written, 0); |
| 303 | if (ret == -1) { |
| 304 | /* FIXME: Error happened */ |
| 305 | logger_log(httpd->logger, LOGGER_INFO, "Error in sending data"); |
| 306 | break; |
| 307 | } |
| 308 | written += ret; |
| 309 | } |
| 310 | } else { |
| 311 | logger_log(httpd->logger, LOGGER_INFO, "Didn't get response"); |
| 312 | } |
| 313 | http_response_destroy(response); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* Remove all connections that are still connected */ |
| 319 | for (i=0; i<httpd->max_connections; i++) { |
| 320 | http_connection_t *connection = &httpd->connections[i]; |
| 321 | |
| 322 | if (!connection->connected) { |
| 323 | continue; |
| 324 | } |
| 325 | logger_log(httpd->logger, LOGGER_INFO, "Removing connection for socket %d", connection->socket_fd); |
| 326 | httpd_remove_connection(httpd, connection); |
| 327 | } |
| 328 | |
| 329 | /* Close server sockets since they are not used any more */ |
| 330 | if (httpd->server_fd4 != -1) { |
| 331 | closesocket(httpd->server_fd4); |
| 332 | httpd->server_fd4 = -1; |
| 333 | } |
| 334 | if (httpd->server_fd6 != -1) { |
| 335 | closesocket(httpd->server_fd6); |
| 336 | httpd->server_fd6 = -1; |
| 337 | } |
| 338 | |
| 339 | logger_log(httpd->logger, LOGGER_INFO, "Exiting HTTP thread"); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | int |
| 345 | httpd_start(httpd_t *httpd, unsigned short *port) |
| 346 | { |
| 347 | assert(httpd); |
| 348 | assert(port); |
| 349 | |
| 350 | MUTEX_LOCK(httpd->run_mutex); |
| 351 | if (httpd->running || !httpd->joined) { |
| 352 | MUTEX_UNLOCK(httpd->run_mutex); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | httpd->server_fd4 = netutils_init_socket(port, 0, 0); |
| 357 | if (httpd->server_fd4 == -1) { |
| 358 | logger_log(httpd->logger, LOGGER_ERR, "Error initialising socket %d", SOCKET_GET_ERROR()); |
| 359 | MUTEX_UNLOCK(httpd->run_mutex); |
| 360 | return -1; |
| 361 | } |
| 362 | httpd->server_fd6 = netutils_init_socket(port, 1, 0); |
| 363 | if (httpd->server_fd6 == -1) { |
| 364 | logger_log(httpd->logger, LOGGER_WARNING, "Error initialising IPv6 socket %d", SOCKET_GET_ERROR()); |
| 365 | logger_log(httpd->logger, LOGGER_WARNING, "Continuing without IPv6 support"); |
| 366 | } |
| 367 | |
| 368 | if (listen(httpd->server_fd4, 5) == -1) { |
| 369 | logger_log(httpd->logger, LOGGER_ERR, "Error listening to IPv4 socket"); |
| 370 | closesocket(httpd->server_fd4); |
| 371 | closesocket(httpd->server_fd6); |
| 372 | MUTEX_UNLOCK(httpd->run_mutex); |
| 373 | return -2; |
| 374 | } |
| 375 | if (httpd->server_fd6 != -1 && listen(httpd->server_fd6, 5) == -1) { |
| 376 | logger_log(httpd->logger, LOGGER_ERR, "Error listening to IPv6 socket"); |
| 377 | closesocket(httpd->server_fd4); |
| 378 | closesocket(httpd->server_fd6); |
| 379 | MUTEX_UNLOCK(httpd->run_mutex); |
| 380 | return -2; |
| 381 | } |
| 382 | logger_log(httpd->logger, LOGGER_INFO, "Initialized server socket(s)"); |
| 383 | |
| 384 | /* Set values correctly and create new thread */ |
| 385 | httpd->running = 1; |
| 386 | httpd->joined = 0; |
| 387 | THREAD_CREATE(httpd->thread, httpd_thread, httpd); |
| 388 | MUTEX_UNLOCK(httpd->run_mutex); |
| 389 | |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | int |
| 394 | httpd_is_running(httpd_t *httpd) |
| 395 | { |
| 396 | int running; |
| 397 | |
| 398 | assert(httpd); |
| 399 | |
| 400 | MUTEX_LOCK(httpd->run_mutex); |
| 401 | running = httpd->running || !httpd->joined; |
| 402 | MUTEX_UNLOCK(httpd->run_mutex); |
| 403 | |
| 404 | return running; |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | httpd_stop(httpd_t *httpd) |
| 409 | { |
| 410 | assert(httpd); |
| 411 | |
| 412 | MUTEX_LOCK(httpd->run_mutex); |
| 413 | if (!httpd->running || httpd->joined) { |
| 414 | MUTEX_UNLOCK(httpd->run_mutex); |
| 415 | return; |
| 416 | } |
| 417 | httpd->running = 0; |
| 418 | MUTEX_UNLOCK(httpd->run_mutex); |
| 419 | |
| 420 | THREAD_JOIN(httpd->thread); |
| 421 | |
| 422 | MUTEX_LOCK(httpd->run_mutex); |
| 423 | httpd->joined = 1; |
| 424 | MUTEX_UNLOCK(httpd->run_mutex); |
| 425 | } |
| 426 | |