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