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
;
42 http_connection_t
*connections
;
44 /* These variables only edited mutex locked */
47 thread_handle_t thread
;
48 mutex_handle_t run_mutex
;
50 /* Server fd for accepting connections */
55 httpd_init(logger_t
*logger
, httpd_callbacks_t
*callbacks
, int max_connections
, int use_rtsp
)
61 assert(max_connections
> 0);
63 /* Allocate the httpd_t structure */
64 httpd
= calloc(1, sizeof(httpd_t
));
69 httpd
->use_rtsp
= !!use_rtsp
;
70 httpd
->max_connections
= max_connections
;
71 httpd
->connections
= calloc(max_connections
, sizeof(http_connection_t
));
72 if (!httpd
->connections
) {
77 /* Use the logger provided */
78 httpd
->logger
= logger
;
80 /* Save callback pointers */
81 memcpy(&httpd
->callbacks
, callbacks
, sizeof(httpd_callbacks_t
));
83 /* Initial status joined */
91 httpd_destroy(httpd_t
*httpd
)
96 free(httpd
->connections
);
102 httpd_add_connection(httpd_t
*httpd
, int fd
, unsigned char *local
, int local_len
, unsigned char *remote
, int remote_len
)
106 for (i
=0; i
<httpd
->max_connections
; i
++) {
107 if (!httpd
->connections
[i
].connected
) {
111 if (i
== httpd
->max_connections
) {
112 logger_log(httpd
->logger
, LOGGER_INFO
, "Max connections reached\n");
113 shutdown(fd
, SHUT_RDWR
);
118 httpd
->connections
[i
].socket_fd
= fd
;
119 httpd
->connections
[i
].connected
= 1;
120 httpd
->connections
[i
].user_data
= httpd
->callbacks
.conn_init(httpd
->callbacks
.opaque
, local
, local_len
, remote
, remote_len
);
124 httpd_remove_connection(httpd_t
*httpd
, http_connection_t
*connection
)
126 if (connection
->request
) {
127 http_request_destroy(connection
->request
);
128 connection
->request
= NULL
;
130 httpd
->callbacks
.conn_destroy(connection
->user_data
);
131 shutdown(connection
->socket_fd
, SHUT_WR
);
132 closesocket(connection
->socket_fd
);
133 connection
->connected
= 0;
137 httpd_thread(void *arg
)
139 httpd_t
*httpd
= arg
;
151 MUTEX_LOCK(httpd
->run_mutex
);
152 if (!httpd
->running
) {
153 MUTEX_UNLOCK(httpd
->run_mutex
);
156 MUTEX_UNLOCK(httpd
->run_mutex
);
158 /* Set timeout value to 5ms */
162 /* Get the correct nfds value and set rfds */
164 FD_SET(httpd
->server_fd
, &rfds
);
165 nfds
= httpd
->server_fd
+1;
166 for (i
=0; i
<httpd
->max_connections
; i
++) {
168 if (!httpd
->connections
[i
].connected
) {
171 socket_fd
= httpd
->connections
[i
].socket_fd
;
172 FD_SET(socket_fd
, &rfds
);
173 if (nfds
<= socket_fd
) {
178 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
180 /* Timeout happened */
182 } else if (ret
== -1) {
183 /* FIXME: Error happened */
184 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in select\n");
188 if (FD_ISSET(httpd
->server_fd
, &rfds
)) {
189 struct sockaddr_storage remote_saddr
;
190 socklen_t remote_saddrlen
;
191 struct sockaddr_storage local_saddr
;
192 socklen_t local_saddrlen
;
193 unsigned char *local
, *remote
;
194 int local_len
, remote_len
;
197 remote_saddrlen
= sizeof(remote_saddr
);
198 fd
= accept(httpd
->server_fd
, (struct sockaddr
*)&remote_saddr
, &remote_saddrlen
);
200 /* FIXME: Error happened */
204 local_saddrlen
= sizeof(local_saddr
);
205 ret
= getsockname(fd
, (struct sockaddr
*)&local_saddr
, &local_saddrlen
);
211 logger_log(httpd
->logger
, LOGGER_INFO
, "Accepted client on socket %d\n", fd
);
212 local
= netutils_get_address(&local_saddr
, &local_len
);
213 remote
= netutils_get_address(&remote_saddr
, &remote_len
);
215 httpd_add_connection(httpd
, fd
, local
, local_len
, remote
, remote_len
);
217 for (i
=0; i
<httpd
->max_connections
; i
++) {
218 http_connection_t
*connection
= &httpd
->connections
[i
];
220 if (!connection
->connected
) {
223 if (!FD_ISSET(connection
->socket_fd
, &rfds
)) {
227 /* If not in the middle of request, allocate one */
228 if (!connection
->request
) {
229 connection
->request
= http_request_init(httpd
->use_rtsp
);
230 assert(connection
->request
);
233 logger_log(httpd
->logger
, LOGGER_DEBUG
, "Receiving on socket %d\n", httpd
->connections
[i
].socket_fd
);
234 ret
= recv(connection
->socket_fd
, buffer
, sizeof(buffer
), 0);
236 logger_log(httpd
->logger
, LOGGER_INFO
, "Connection closed\n");
237 httpd_remove_connection(httpd
, connection
);
241 /* Parse HTTP request from data read from connection */
242 http_request_add_data(connection
->request
, buffer
, ret
);
243 if (http_request_has_error(connection
->request
)) {
244 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in parsing: %s\n", http_request_get_error_name(connection
->request
));
245 httpd_remove_connection(httpd
, connection
);
249 /* If request is finished, process and deallocate */
250 if (http_request_is_complete(connection
->request
)) {
251 http_response_t
*response
= NULL
;
253 httpd
->callbacks
.conn_request(connection
->user_data
, connection
->request
, &response
);
254 http_request_destroy(connection
->request
);
255 connection
->request
= NULL
;
263 /* Get response data and datalen */
264 data
= http_response_get_data(response
, &datalen
);
267 while (written
< datalen
) {
268 ret
= send(connection
->socket_fd
, data
+written
, datalen
-written
, 0);
270 /* FIXME: Error happened */
271 logger_log(httpd
->logger
, LOGGER_INFO
, "Error in sending data\n");
277 logger_log(httpd
->logger
, LOGGER_INFO
, "Didn't get response\n");
279 http_response_destroy(response
);
284 /* Remove all connections that are still connected */
285 for (i
=0; i
<httpd
->max_connections
; i
++) {
286 http_connection_t
*connection
= &httpd
->connections
[i
];
288 if (!connection
->connected
) {
291 logger_log(httpd
->logger
, LOGGER_INFO
, "Removing connection\n");
292 httpd_remove_connection(httpd
, connection
);
295 logger_log(httpd
->logger
, LOGGER_INFO
, "Exiting thread\n");
301 httpd_start(httpd_t
*httpd
, unsigned short *port
)
306 MUTEX_LOCK(httpd
->run_mutex
);
307 if (httpd
->running
|| !httpd
->joined
) {
308 MUTEX_UNLOCK(httpd
->run_mutex
);
312 httpd
->server_fd
= netutils_init_socket(port
, 1, 0);
313 if (httpd
->server_fd
== -1) {
314 logger_log(httpd
->logger
, LOGGER_INFO
, "Error initialising socket %d\n", SOCKET_GET_ERROR());
315 MUTEX_UNLOCK(httpd
->run_mutex
);
318 if (listen(httpd
->server_fd
, 5) == -1) {
319 logger_log(httpd
->logger
, LOGGER_INFO
, "Error listening to socket\n");
320 MUTEX_UNLOCK(httpd
->run_mutex
);
323 logger_log(httpd
->logger
, LOGGER_INFO
, "Initialized server socket\n");
325 /* Set values correctly and create new thread */
328 THREAD_CREATE(httpd
->thread
, httpd_thread
, httpd
);
329 MUTEX_UNLOCK(httpd
->run_mutex
);
335 httpd_stop(httpd_t
*httpd
)
339 MUTEX_LOCK(httpd
->run_mutex
);
340 if (!httpd
->running
|| httpd
->joined
) {
341 MUTEX_UNLOCK(httpd
->run_mutex
);
345 MUTEX_UNLOCK(httpd
->run_mutex
);
347 THREAD_JOIN(httpd
->thread
);
349 MUTEX_LOCK(httpd
->run_mutex
);
351 MUTEX_UNLOCK(httpd
->run_mutex
);