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 fd for accepting connections */
56 httpd_init(logger_t
*logger
, httpd_callbacks_t
*callbacks
, int max_connections
, int use_rtsp
)
62 assert(max_connections
> 0);
64 /* Allocate the httpd_t structure */
65 httpd
= calloc(1, sizeof(httpd_t
));
70 httpd
->use_rtsp
= !!use_rtsp
;
71 httpd
->max_connections
= max_connections
;
72 httpd
->connections
= calloc(max_connections
, sizeof(http_connection_t
));
73 if (!httpd
->connections
) {
78 /* Use the logger provided */
79 httpd
->logger
= logger
;
81 /* Save callback pointers */
82 memcpy(&httpd
->callbacks
, callbacks
, sizeof(httpd_callbacks_t
));
84 /* Initial status joined */
92 httpd_destroy(httpd_t
*httpd
)
97 free(httpd
->connections
);
103 httpd_add_connection(httpd_t
*httpd
, int fd
, unsigned char *local
, int local_len
, unsigned char *remote
, int remote_len
)
107 for (i
=0; i
<httpd
->max_connections
; i
++) {
108 if (!httpd
->connections
[i
].connected
) {
112 if (i
== httpd
->max_connections
) {
113 logger_log(httpd
->logger
, LOGGER_INFO
, "Max connections reached");
114 shutdown(fd
, SHUT_RDWR
);
119 httpd
->open_connections
++;
120 httpd
->connections
[i
].socket_fd
= fd
;
121 httpd
->connections
[i
].connected
= 1;
122 httpd
->connections
[i
].user_data
= httpd
->callbacks
.conn_init(httpd
->callbacks
.opaque
, local
, local_len
, remote
, remote_len
);
126 httpd_remove_connection(httpd_t
*httpd
, http_connection_t
*connection
)
128 if (connection
->request
) {
129 http_request_destroy(connection
->request
);
130 connection
->request
= NULL
;
132 httpd
->callbacks
.conn_destroy(connection
->user_data
);
133 shutdown(connection
->socket_fd
, SHUT_WR
);
134 closesocket(connection
->socket_fd
);
135 connection
->connected
= 0;
136 httpd
->open_connections
--;
140 httpd_thread(void *arg
)
142 httpd_t
*httpd
= arg
;
154 MUTEX_LOCK(httpd
->run_mutex
);
155 if (!httpd
->running
) {
156 MUTEX_UNLOCK(httpd
->run_mutex
);
159 MUTEX_UNLOCK(httpd
->run_mutex
);
161 /* Set timeout value to 5ms */
165 /* Get the correct nfds value and set rfds */
167 if (httpd
->open_connections
< httpd
->max_connections
) {
168 FD_SET(httpd
->server_fd
, &rfds
);
169 nfds
= httpd
->server_fd
+1;
171 for (i
=0; i
<httpd
->max_connections
; i
++) {
173 if (!httpd
->connections
[i
].connected
) {
176 socket_fd
= httpd
->connections
[i
].socket_fd
;
177 FD_SET(socket_fd
, &rfds
);
178 if (nfds
<= socket_fd
) {
183 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
185 /* Timeout happened */
187 } else if (ret
== -1) {
188 /* FIXME: Error happened */
189 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in select");
193 if (FD_ISSET(httpd
->server_fd
, &rfds
)) {
194 struct sockaddr_storage remote_saddr
;
195 socklen_t remote_saddrlen
;
196 struct sockaddr_storage local_saddr
;
197 socklen_t local_saddrlen
;
198 unsigned char *local
, *remote
;
199 int local_len
, remote_len
;
202 remote_saddrlen
= sizeof(remote_saddr
);
203 fd
= accept(httpd
->server_fd
, (struct sockaddr
*)&remote_saddr
, &remote_saddrlen
);
205 /* FIXME: Error happened */
209 local_saddrlen
= sizeof(local_saddr
);
210 ret
= getsockname(fd
, (struct sockaddr
*)&local_saddr
, &local_saddrlen
);
216 logger_log(httpd
->logger
, LOGGER_INFO
, "Accepted client on socket %d", fd
);
217 local
= netutils_get_address(&local_saddr
, &local_len
);
218 remote
= netutils_get_address(&remote_saddr
, &remote_len
);
220 httpd_add_connection(httpd
, fd
, local
, local_len
, remote
, remote_len
);
222 for (i
=0; i
<httpd
->max_connections
; i
++) {
223 http_connection_t
*connection
= &httpd
->connections
[i
];
225 if (!connection
->connected
) {
228 if (!FD_ISSET(connection
->socket_fd
, &rfds
)) {
232 /* If not in the middle of request, allocate one */
233 if (!connection
->request
) {
234 connection
->request
= http_request_init(httpd
->use_rtsp
);
235 assert(connection
->request
);
238 logger_log(httpd
->logger
, LOGGER_DEBUG
, "Receiving on socket %d", connection
->socket_fd
);
239 ret
= recv(connection
->socket_fd
, buffer
, sizeof(buffer
), 0);
241 logger_log(httpd
->logger
, LOGGER_INFO
, "Connection closed for socket %d", connection
->socket_fd
);
242 httpd_remove_connection(httpd
, connection
);
246 /* Parse HTTP request from data read from connection */
247 http_request_add_data(connection
->request
, buffer
, ret
);
248 if (http_request_has_error(connection
->request
)) {
249 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in parsing: %s", http_request_get_error_name(connection
->request
));
250 httpd_remove_connection(httpd
, connection
);
254 /* If request is finished, process and deallocate */
255 if (http_request_is_complete(connection
->request
)) {
256 http_response_t
*response
= NULL
;
258 httpd
->callbacks
.conn_request(connection
->user_data
, connection
->request
, &response
);
259 http_request_destroy(connection
->request
);
260 connection
->request
= NULL
;
268 /* Get response data and datalen */
269 data
= http_response_get_data(response
, &datalen
);
272 while (written
< datalen
) {
273 ret
= send(connection
->socket_fd
, data
+written
, datalen
-written
, 0);
275 /* FIXME: Error happened */
276 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in sending data");
282 logger_log(httpd
->logger
, LOGGER_INFO
, "Didn't get response");
284 http_response_destroy(response
);
289 /* Remove all connections that are still connected */
290 for (i
=0; i
<httpd
->max_connections
; i
++) {
291 http_connection_t
*connection
= &httpd
->connections
[i
];
293 if (!connection
->connected
) {
296 logger_log(httpd
->logger
, LOGGER_INFO
, "Removing connection for socket %d", connection
->socket_fd
);
297 httpd_remove_connection(httpd
, connection
);
300 logger_log(httpd
->logger
, LOGGER_INFO
, "Exiting HTTP thread");
306 httpd_start(httpd_t
*httpd
, unsigned short *port
)
311 MUTEX_LOCK(httpd
->run_mutex
);
312 if (httpd
->running
|| !httpd
->joined
) {
313 MUTEX_UNLOCK(httpd
->run_mutex
);
317 httpd
->server_fd
= netutils_init_socket(port
, 1, 0);
318 if (httpd
->server_fd
== -1) {
319 logger_log(httpd
->logger
, LOGGER_INFO
, "Error initialising IPv6 socket %d", SOCKET_GET_ERROR());
320 logger_log(httpd
->logger
, LOGGER_INFO
, "Attempting to fall back to IPv4");
321 httpd
->server_fd
= netutils_init_socket(port
, 0, 0);
323 if (httpd
->server_fd
== -1) {
324 logger_log(httpd
->logger
, LOGGER_INFO
, "Error initialising socket %d", SOCKET_GET_ERROR());
325 MUTEX_UNLOCK(httpd
->run_mutex
);
328 if (listen(httpd
->server_fd
, 5) == -1) {
329 logger_log(httpd
->logger
, LOGGER_INFO
, "Error listening to socket");
330 MUTEX_UNLOCK(httpd
->run_mutex
);
333 logger_log(httpd
->logger
, LOGGER_INFO
, "Initialized server socket");
335 /* Set values correctly and create new thread */
338 THREAD_CREATE(httpd
->thread
, httpd_thread
, httpd
);
339 MUTEX_UNLOCK(httpd
->run_mutex
);
345 httpd_is_running(httpd_t
*httpd
)
351 MUTEX_LOCK(httpd
->run_mutex
);
352 running
= httpd
->running
|| !httpd
->joined
;
353 MUTEX_UNLOCK(httpd
->run_mutex
);
359 httpd_stop(httpd_t
*httpd
)
363 MUTEX_LOCK(httpd
->run_mutex
);
364 if (!httpd
->running
|| httpd
->joined
) {
365 MUTEX_UNLOCK(httpd
->run_mutex
);
369 MUTEX_UNLOCK(httpd
->run_mutex
);
371 THREAD_JOIN(httpd
->thread
);
373 MUTEX_LOCK(httpd
->run_mutex
);
375 MUTEX_UNLOCK(httpd
->run_mutex
);