2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
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.
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.
22 #include "http_request.h"
26 struct http_connection_s
{
31 http_request_t
*request
;
33 typedef struct http_connection_s http_connection_t
;
37 httpd_callbacks_t callbacks
;
43 http_connection_t
*connections
;
45 /* These variables only edited mutex locked */
48 thread_handle_t thread
;
49 mutex_handle_t run_mutex
;
51 /* Server fds for accepting connections */
57 httpd_init(logger_t
*logger
, httpd_callbacks_t
*callbacks
, int max_connections
, int use_rtsp
)
63 assert(max_connections
> 0);
65 /* Allocate the httpd_t structure */
66 httpd
= calloc(1, sizeof(httpd_t
));
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
) {
79 /* Use the logger provided */
80 httpd
->logger
= logger
;
82 /* Save callback pointers */
83 memcpy(&httpd
->callbacks
, callbacks
, sizeof(httpd_callbacks_t
));
85 /* Initial status joined */
93 httpd_destroy(httpd_t
*httpd
)
98 free(httpd
->connections
);
104 httpd_add_connection(httpd_t
*httpd
, int fd
, unsigned char *local
, int local_len
, unsigned char *remote
, int remote_len
)
108 for (i
=0; i
<httpd
->max_connections
; i
++) {
109 if (!httpd
->connections
[i
].connected
) {
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
);
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
);
128 httpd_accept_connection(httpd_t
*httpd
, int server_fd
, int is_ipv6
)
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
;
138 remote_saddrlen
= sizeof(remote_saddr
);
139 fd
= accept(server_fd
, (struct sockaddr
*)&remote_saddr
, &remote_saddrlen
);
141 /* FIXME: Error happened */
145 local_saddrlen
= sizeof(local_saddr
);
146 ret
= getsockname(fd
, (struct sockaddr
*)&local_saddr
, &local_saddrlen
);
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
);
157 httpd_add_connection(httpd
, fd
, local
, local_len
, remote
, remote_len
);
162 httpd_remove_connection(httpd_t
*httpd
, http_connection_t
*connection
)
164 if (connection
->request
) {
165 http_request_destroy(connection
->request
);
166 connection
->request
= NULL
;
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
--;
176 httpd_thread(void *arg
)
178 httpd_t
*httpd
= arg
;
190 MUTEX_LOCK(httpd
->run_mutex
);
191 if (!httpd
->running
) {
192 MUTEX_UNLOCK(httpd
->run_mutex
);
195 MUTEX_UNLOCK(httpd
->run_mutex
);
197 /* Set timeout value to 5ms */
201 /* Get the correct nfds value and set 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;
213 for (i
=0; i
<httpd
->max_connections
; i
++) {
215 if (!httpd
->connections
[i
].connected
) {
218 socket_fd
= httpd
->connections
[i
].socket_fd
;
219 FD_SET(socket_fd
, &rfds
);
220 if (nfds
<= socket_fd
) {
225 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
227 /* Timeout happened */
229 } else if (ret
== -1) {
230 /* FIXME: Error happened */
231 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in select");
235 if (FD_ISSET(httpd
->server_fd4
, &rfds
)) {
236 ret
= httpd_accept_connection(httpd
, httpd
->server_fd4
, 0);
239 } else if (ret
== 0) {
243 if (FD_ISSET(httpd
->server_fd6
, &rfds
)) {
244 ret
= httpd_accept_connection(httpd
, httpd
->server_fd6
, 1);
247 } else if (ret
== 0) {
251 for (i
=0; i
<httpd
->max_connections
; i
++) {
252 http_connection_t
*connection
= &httpd
->connections
[i
];
254 if (!connection
->connected
) {
257 if (!FD_ISSET(connection
->socket_fd
, &rfds
)) {
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
);
267 logger_log(httpd
->logger
, LOGGER_DEBUG
, "Receiving on socket %d", connection
->socket_fd
);
268 ret
= recv(connection
->socket_fd
, buffer
, sizeof(buffer
), 0);
270 logger_log(httpd
->logger
, LOGGER_INFO
, "Connection closed for socket %d", connection
->socket_fd
);
271 httpd_remove_connection(httpd
, connection
);
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
);
283 /* If request is finished, process and deallocate */
284 if (http_request_is_complete(connection
->request
)) {
285 http_response_t
*response
= NULL
;
287 httpd
->callbacks
.conn_request(connection
->user_data
, connection
->request
, &response
);
288 http_request_destroy(connection
->request
);
289 connection
->request
= NULL
;
297 /* Get response data and datalen */
298 data
= http_response_get_data(response
, &datalen
);
301 while (written
< datalen
) {
302 ret
= send(connection
->socket_fd
, data
+written
, datalen
-written
, 0);
304 /* FIXME: Error happened */
305 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in sending data");
311 logger_log(httpd
->logger
, LOGGER_INFO
, "Didn't get response");
313 http_response_destroy(response
);
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
];
322 if (!connection
->connected
) {
325 logger_log(httpd
->logger
, LOGGER_INFO
, "Removing connection for socket %d", connection
->socket_fd
);
326 httpd_remove_connection(httpd
, connection
);
329 logger_log(httpd
->logger
, LOGGER_INFO
, "Exiting HTTP thread");
335 httpd_start(httpd_t
*httpd
, unsigned short *port
)
340 MUTEX_LOCK(httpd
->run_mutex
);
341 if (httpd
->running
|| !httpd
->joined
) {
342 MUTEX_UNLOCK(httpd
->run_mutex
);
346 httpd
->server_fd4
= netutils_init_socket(port
, 0, 0);
347 if (httpd
->server_fd4
== -1) {
348 logger_log(httpd
->logger
, LOGGER_ERR
, "Error initialising socket %d", SOCKET_GET_ERROR());
349 MUTEX_UNLOCK(httpd
->run_mutex
);
352 httpd
->server_fd6
= netutils_init_socket(port
, 1, 0);
353 if (httpd
->server_fd6
== -1) {
354 logger_log(httpd
->logger
, LOGGER_WARNING
, "Error initialising IPv6 socket %d", SOCKET_GET_ERROR());
355 logger_log(httpd
->logger
, LOGGER_WARNING
, "Continuing without IPv6 support");
358 if (listen(httpd
->server_fd4
, 5) == -1) {
359 logger_log(httpd
->logger
, LOGGER_ERR
, "Error listening to IPv4 socket");
360 closesocket(httpd
->server_fd4
);
361 closesocket(httpd
->server_fd6
);
362 MUTEX_UNLOCK(httpd
->run_mutex
);
365 if (httpd
->server_fd6
!= -1 && listen(httpd
->server_fd6
, 5) == -1) {
366 logger_log(httpd
->logger
, LOGGER_ERR
, "Error listening to IPv6 socket");
367 closesocket(httpd
->server_fd4
);
368 closesocket(httpd
->server_fd6
);
369 MUTEX_UNLOCK(httpd
->run_mutex
);
372 logger_log(httpd
->logger
, LOGGER_INFO
, "Initialized server socket(s)");
374 /* Set values correctly and create new thread */
377 THREAD_CREATE(httpd
->thread
, httpd_thread
, httpd
);
378 MUTEX_UNLOCK(httpd
->run_mutex
);
384 httpd_is_running(httpd_t
*httpd
)
390 MUTEX_LOCK(httpd
->run_mutex
);
391 running
= httpd
->running
|| !httpd
->joined
;
392 MUTEX_UNLOCK(httpd
->run_mutex
);
398 httpd_stop(httpd_t
*httpd
)
402 MUTEX_LOCK(httpd
->run_mutex
);
403 if (!httpd
->running
|| httpd
->joined
) {
404 MUTEX_UNLOCK(httpd
->run_mutex
);
408 MUTEX_UNLOCK(httpd
->run_mutex
);
410 THREAD_JOIN(httpd
->thread
);
412 MUTEX_LOCK(httpd
->run_mutex
);
414 MUTEX_UNLOCK(httpd
->run_mutex
);