FIx annoying button behaviour (resize when tab changing).
[vlc/davidf-public.git] / src / network / httpd.c
blob86c7745209ac3208eba141dc6028faa489597cae
1 /*****************************************************************************
2 * httpd.c
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2004-2007 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_httpd.h>
33 #ifdef ENABLE_HTTPD
35 #include <assert.h>
37 #include <vlc_network.h>
38 #include <vlc_tls.h>
39 #include <vlc_acl.h>
40 #include <vlc_strings.h>
41 #include "../libvlc.h"
43 #include <string.h>
44 #include <errno.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
50 #ifdef HAVE_FCNTL_H
51 # include <fcntl.h>
52 #endif
54 #ifdef HAVE_POLL
55 # include <poll.h>
56 #endif
58 #if defined( UNDER_CE )
59 # include <winsock.h>
60 #elif defined( WIN32 )
61 # include <winsock2.h>
62 #else
63 # include <sys/socket.h>
64 #endif
66 #if defined( WIN32 )
67 /* We need HUGE buffer otherwise TCP throughput is very limited */
68 #define HTTPD_CL_BUFSIZE 1000000
69 #else
70 #define HTTPD_CL_BUFSIZE 10000
71 #endif
73 static void httpd_ClientClean( httpd_client_t *cl );
75 struct httpd_t
77 VLC_COMMON_MEMBERS
79 int i_host;
80 httpd_host_t **host;
84 /* each host run in his own thread */
85 struct httpd_host_t
87 VLC_COMMON_MEMBERS
89 httpd_t *httpd;
91 /* ref count */
92 int i_ref;
94 /* address/port and socket for listening at connections */
95 char *psz_hostname;
96 int i_port;
97 int *fds;
98 unsigned nfd;
100 vlc_mutex_t lock;
102 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
103 * This will slow down the url research but make my live easier
104 * All url will have their cb trigger, but only the first one can answer
105 * */
106 int i_url;
107 httpd_url_t **url;
109 int i_client;
110 httpd_client_t **client;
112 /* TLS data */
113 tls_server_t *p_tls;
117 struct httpd_url_t
119 httpd_host_t *host;
121 vlc_mutex_t lock;
123 char *psz_url;
124 char *psz_user;
125 char *psz_password;
126 vlc_acl_t *p_acl;
128 struct
130 httpd_callback_t cb;
131 httpd_callback_sys_t *p_sys;
132 } catch[HTTPD_MSG_MAX];
135 /* status */
136 enum
138 HTTPD_CLIENT_RECEIVING,
139 HTTPD_CLIENT_RECEIVE_DONE,
141 HTTPD_CLIENT_SENDING,
142 HTTPD_CLIENT_SEND_DONE,
144 HTTPD_CLIENT_WAITING,
146 HTTPD_CLIENT_DEAD,
148 HTTPD_CLIENT_TLS_HS_IN,
149 HTTPD_CLIENT_TLS_HS_OUT
152 /* mode */
153 enum
155 HTTPD_CLIENT_FILE, /* default */
156 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
157 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
160 struct httpd_client_t
162 httpd_url_t *url;
164 int i_ref;
166 int fd;
168 int i_mode;
169 int i_state;
170 int b_read_waiting; /* stop as soon as possible sending */
172 mtime_t i_activity_date;
173 mtime_t i_activity_timeout;
175 /* buffer for reading header */
176 int i_buffer_size;
177 int i_buffer;
178 uint8_t *p_buffer;
180 /* */
181 httpd_message_t query; /* client -> httpd */
182 httpd_message_t answer; /* httpd -> client */
184 /* TLS data */
185 tls_session_t *p_tls;
189 /*****************************************************************************
190 * Various functions
191 *****************************************************************************/
192 static struct
194 const char psz_ext[8];
195 const char *psz_mime;
196 } http_mime[] =
198 { ".htm", "text/html" },
199 { ".html", "text/html" },
200 { ".txt", "text/plain" },
201 { ".xml", "text/xml" },
202 { ".dtd", "text/dtd" },
204 { ".css", "text/css" },
206 /* image mime */
207 { ".gif", "image/gif" },
208 { ".jpe", "image/jpeg" },
209 { ".jpg", "image/jpeg" },
210 { ".jpeg", "image/jpeg" },
211 { ".png", "image/png" },
212 /* same as modules/mux/mpjpeg.c here: */
213 { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" },
215 /* media mime */
216 { ".avi", "video/avi" },
217 { ".asf", "video/x-ms-asf" },
218 { ".m1a", "audio/mpeg" },
219 { ".m2a", "audio/mpeg" },
220 { ".m1v", "video/mpeg" },
221 { ".m2v", "video/mpeg" },
222 { ".mp2", "audio/mpeg" },
223 { ".mp3", "audio/mpeg" },
224 { ".mpa", "audio/mpeg" },
225 { ".mpg", "video/mpeg" },
226 { ".mpeg", "video/mpeg" },
227 { ".mpe", "video/mpeg" },
228 { ".mov", "video/quicktime" },
229 { ".moov", "video/quicktime" },
230 { ".ogg", "application/ogg" },
231 { ".ogm", "application/ogg" },
232 { ".wav", "audio/wav" },
233 { ".wma", "audio/x-ms-wma" },
234 { ".wmv", "video/x-ms-wmv" },
237 /* end */
238 { "", "" }
241 static const char *httpd_MimeFromUrl( const char *psz_url )
244 char *psz_ext;
246 psz_ext = strrchr( psz_url, '.' );
247 if( psz_ext )
249 int i;
251 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
253 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
255 return http_mime[i].psz_mime;
259 return "application/octet-stream";
263 typedef struct
265 unsigned i_code;
266 const char psz_reason[36];
267 } http_status_info;
269 static const http_status_info http_reason[] =
271 /*{ 100, "Continue" },
272 { 101, "Switching Protocols" },*/
273 { 200, "OK" },
274 /*{ 201, "Created" },
275 { 202, "Accepted" },
276 { 203, "Non-authoritative information" },
277 { 204, "No content" },
278 { 205, "Reset content" },
279 { 206, "Partial content" },
280 { 250, "Low on storage space" },
281 { 300, "Multiple choices" },*/
282 { 301, "Moved permanently" },
283 /*{ 302, "Moved temporarily" },
284 { 303, "See other" },
285 { 304, "Not modified" },
286 { 305, "Use proxy" },
287 { 307, "Temporary redirect" },
288 { 400, "Bad request" },*/
289 { 401, "Unauthorized" },
290 /*{ 402, "Payment Required" },*/
291 { 403, "Forbidden" },
292 { 404, "Not found" },
293 { 405, "Method not allowed" },
294 /*{ 406, "Not acceptable" },
295 { 407, "Proxy authentication required" },
296 { 408, "Request time-out" },
297 { 409, "Conflict" },
298 { 410, "Gone" },
299 { 411, "Length required" },
300 { 412, "Precondition failed" },
301 { 413, "Request entity too large" },
302 { 414, "Request-URI too large" },
303 { 415, "Unsupported media Type" },
304 { 416, "Requested range not satisfiable" },
305 { 417, "Expectation failed" },
306 { 451, "Parameter not understood" },
307 { 452, "Conference not found" },
308 { 453, "Not enough bandwidth" },*/
309 { 454, "Session not found" },
310 /*{ 455, "Method not valid in this State" },*/
311 { 456, "Header field not valid for resource" },
312 /*{ 457, "Invalid range" },
313 { 458, "Read-only parameter" },*/
314 { 459, "Aggregate operation not allowed" },
315 { 460, "Non-aggregate operation not allowed" },
316 { 461, "Unsupported transport" },
317 /*{ 462, "Destination unreachable" },*/
318 { 500, "Internal server error" },
319 { 501, "Not implemented" },
320 /*{ 502, "Bad gateway" },*/
321 { 503, "Service unavailable" },
322 /*{ 504, "Gateway time-out" },*/
323 { 505, "Protocol version not supported" },
324 { 551, "Option not supported" },
325 { 999, "" }
328 static const char psz_fallback_reason[5][16] =
329 { "Continue", "OK", "Found", "Client error", "Server error" };
331 static const char *httpd_ReasonFromCode( unsigned i_code )
333 const http_status_info *p;
335 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
337 for (p = http_reason; i_code > p->i_code; p++);
339 if( p->i_code == i_code )
340 return p->psz_reason;
342 return psz_fallback_reason[(i_code / 100) - 1];
346 static size_t httpd_HtmlError (char **body, int code, const char *url)
348 const char *errname = httpd_ReasonFromCode (code);
349 assert (errname != NULL);
351 int res = asprintf (body,
352 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
353 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
354 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
355 "<html lang=\"en\">\n"
356 "<head>\n"
357 "<title>%s</title>\n"
358 "</head>\n"
359 "<body>\n"
360 "<h1>%d %s%s%s%s</h1>\n"
361 "<hr />\n"
362 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
363 "</body>\n"
364 "</html>\n", errname, code, errname,
365 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
367 if (res == -1)
369 *body = NULL;
370 return 0;
373 return (size_t)res;
377 /*****************************************************************************
378 * High Level Functions: httpd_file_t
379 *****************************************************************************/
380 struct httpd_file_t
382 httpd_url_t *url;
384 char *psz_url;
385 char *psz_mime;
387 httpd_file_callback_t pf_fill;
388 httpd_file_sys_t *p_sys;
392 static int
393 httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
394 httpd_message_t *answer, const httpd_message_t *query )
396 httpd_file_t *file = (httpd_file_t*)p_sys;
397 uint8_t **pp_body, *p_body;
398 const char *psz_connection;
399 int *pi_body, i_body;
401 if( answer == NULL || query == NULL )
403 return VLC_SUCCESS;
405 answer->i_proto = HTTPD_PROTO_HTTP;
406 answer->i_version= 1;
407 answer->i_type = HTTPD_MSG_ANSWER;
409 answer->i_status = 200;
411 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
412 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
414 if( query->i_type != HTTPD_MSG_HEAD )
416 pp_body = &answer->p_body;
417 pi_body = &answer->i_body;
419 else
421 /* The file still needs to be executed. */
422 p_body = NULL;
423 i_body = 0;
424 pp_body = &p_body;
425 pi_body = &i_body;
428 if( query->i_type == HTTPD_MSG_POST )
430 /* msg_Warn not supported */
433 uint8_t *psz_args = query->psz_args;
434 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
436 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
438 free( p_body );
441 /* We respect client request */
442 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
443 if( psz_connection != NULL )
445 httpd_MsgAdd( answer, "Connection", "%s", psz_connection );
448 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
450 return VLC_SUCCESS;
453 httpd_file_t *httpd_FileNew( httpd_host_t *host,
454 const char *psz_url, const char *psz_mime,
455 const char *psz_user, const char *psz_password,
456 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
457 httpd_file_sys_t *p_sys )
459 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
461 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
462 psz_password, p_acl )
463 ) == NULL )
465 free( file );
466 return NULL;
469 file->psz_url = strdup( psz_url );
470 if( psz_mime && *psz_mime )
472 file->psz_mime = strdup( psz_mime );
474 else
476 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
479 file->pf_fill = pf_fill;
480 file->p_sys = p_sys;
482 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
483 (httpd_callback_sys_t*)file );
484 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
485 (httpd_callback_sys_t*)file );
486 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
487 (httpd_callback_sys_t*)file );
489 return file;
492 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
494 httpd_file_sys_t *p_sys = file->p_sys;
496 httpd_UrlDelete( file->url );
498 free( file->psz_url );
499 free( file->psz_mime );
501 free( file );
503 return p_sys;
506 /*****************************************************************************
507 * High Level Functions: httpd_handler_t (for CGIs)
508 *****************************************************************************/
509 struct httpd_handler_t
511 httpd_url_t *url;
513 httpd_handler_callback_t pf_fill;
514 httpd_handler_sys_t *p_sys;
518 static int
519 httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
520 httpd_message_t *answer, const httpd_message_t *query )
522 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
523 char psz_remote_addr[NI_MAXNUMERICHOST];
525 if( answer == NULL || query == NULL )
527 return VLC_SUCCESS;
529 answer->i_proto = HTTPD_PROTO_NONE;
530 answer->i_type = HTTPD_MSG_ANSWER;
532 /* We do it ourselves, thanks */
533 answer->i_status = 0;
535 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
536 *psz_remote_addr = '\0';
538 uint8_t *psz_args = query->psz_args;
539 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
540 query->i_type, query->p_body, query->i_body,
541 psz_remote_addr, NULL,
542 &answer->p_body, &answer->i_body );
544 if( query->i_type == HTTPD_MSG_HEAD )
546 char *p = (char *)answer->p_body;
548 /* Looks for end of header (i.e. one empty line) */
549 while ( (p = strchr( p, '\r' )) != NULL )
551 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
552 && p[3] && p[3] == '\n' )
554 break;
558 if( p != NULL )
560 p[4] = '\0';
561 answer->i_body = strlen((char*)answer->p_body) + 1;
562 answer->p_body = realloc( answer->p_body, answer->i_body );
566 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
568 int i_status, i_headers;
569 char *psz_headers, *psz_new;
570 const char *psz_status;
572 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
574 /* Apache-style */
575 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
576 if( *psz_headers ) psz_headers++;
577 if( *psz_headers ) psz_headers++;
578 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
580 else
582 i_status = 200;
583 psz_headers = (char *)answer->p_body;
584 i_headers = answer->i_body;
587 psz_status = httpd_ReasonFromCode( i_status );
588 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
589 + strlen(psz_status) + i_headers - 1;
590 psz_new = (char *)malloc( answer->i_body + 1);
591 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
592 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
593 free( answer->p_body );
594 answer->p_body = (uint8_t *)psz_new;
597 return VLC_SUCCESS;
600 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
601 const char *psz_user,
602 const char *psz_password,
603 const vlc_acl_t *p_acl,
604 httpd_handler_callback_t pf_fill,
605 httpd_handler_sys_t *p_sys )
607 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
609 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
610 psz_password, p_acl )
611 ) == NULL )
613 free( handler );
614 return NULL;
617 handler->pf_fill = pf_fill;
618 handler->p_sys = p_sys;
620 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
621 (httpd_callback_sys_t*)handler );
622 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
623 (httpd_callback_sys_t*)handler );
624 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
625 (httpd_callback_sys_t*)handler );
627 return handler;
630 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
632 httpd_handler_sys_t *p_sys = handler->p_sys;
633 httpd_UrlDelete( handler->url );
634 free( handler );
635 return p_sys;
638 /*****************************************************************************
639 * High Level Functions: httpd_redirect_t
640 *****************************************************************************/
641 struct httpd_redirect_t
643 httpd_url_t *url;
644 char *psz_dst;
647 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
648 httpd_client_t *cl, httpd_message_t *answer,
649 const httpd_message_t *query )
651 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
652 char *p_body;
653 (void)cl;
655 if( answer == NULL || query == NULL )
657 return VLC_SUCCESS;
659 answer->i_proto = HTTPD_PROTO_HTTP;
660 answer->i_version= 1;
661 answer->i_type = HTTPD_MSG_ANSWER;
662 answer->i_status = 301;
664 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
665 answer->p_body = (unsigned char *)p_body;
667 /* XXX check if it's ok or we need to set an absolute url */
668 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
670 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
672 return VLC_SUCCESS;
675 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
676 const char *psz_url_src )
678 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
680 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
682 free( rdir );
683 return NULL;
685 rdir->psz_dst = strdup( psz_url_dst );
687 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
688 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
689 (httpd_callback_sys_t*)rdir );
690 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
691 (httpd_callback_sys_t*)rdir );
692 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
693 (httpd_callback_sys_t*)rdir );
694 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
695 (httpd_callback_sys_t*)rdir );
697 return rdir;
699 void httpd_RedirectDelete( httpd_redirect_t *rdir )
701 httpd_UrlDelete( rdir->url );
702 free( rdir->psz_dst );
703 free( rdir );
706 /*****************************************************************************
707 * High Level Funtions: httpd_stream_t
708 *****************************************************************************/
709 struct httpd_stream_t
711 vlc_mutex_t lock;
712 httpd_url_t *url;
714 char *psz_mime;
716 /* Header to send as first packet */
717 uint8_t *p_header;
718 int i_header;
720 /* circular buffer */
721 int i_buffer_size; /* buffer size, can't be reallocated smaller */
722 uint8_t *p_buffer; /* buffer */
723 int64_t i_buffer_pos; /* absolute position from begining */
724 int64_t i_buffer_last_pos; /* a new connection will start with that */
727 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
728 httpd_client_t *cl, httpd_message_t *answer,
729 const httpd_message_t *query )
731 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
733 if( answer == NULL || query == NULL || cl == NULL )
735 return VLC_SUCCESS;
738 if( answer->i_body_offset > 0 )
740 int64_t i_write;
741 int i_pos;
743 #if 0
744 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
745 answer->i_body_offset );
746 #endif
748 if( answer->i_body_offset >= stream->i_buffer_pos )
750 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
751 return VLC_EGENERIC; /* wait, no data available */
753 if( answer->i_body_offset + stream->i_buffer_size <
754 stream->i_buffer_pos )
756 /* this client isn't fast enough */
757 #if 0
758 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
759 answer->i_body_offset, stream->i_buffer_last_pos );
760 #endif
761 answer->i_body_offset = stream->i_buffer_last_pos;
764 i_pos = answer->i_body_offset % stream->i_buffer_size;
765 i_write = stream->i_buffer_pos - answer->i_body_offset;
766 if( i_write > HTTPD_CL_BUFSIZE )
768 i_write = HTTPD_CL_BUFSIZE;
770 else if( i_write <= 0 )
772 return VLC_EGENERIC; /* wait, no data available */
775 /* Don't go past the end of the circular buffer */
776 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
778 /* using HTTPD_MSG_ANSWER -> data available */
779 answer->i_proto = HTTPD_PROTO_HTTP;
780 answer->i_version= 0;
781 answer->i_type = HTTPD_MSG_ANSWER;
783 answer->i_body = i_write;
784 answer->p_body = malloc( i_write );
785 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
787 answer->i_body_offset += i_write;
789 return VLC_SUCCESS;
791 else
793 answer->i_proto = HTTPD_PROTO_HTTP;
794 answer->i_version= 0;
795 answer->i_type = HTTPD_MSG_ANSWER;
797 answer->i_status = 200;
799 if( query->i_type != HTTPD_MSG_HEAD )
801 httpd_ClientModeStream( cl );
802 vlc_mutex_lock( &stream->lock );
803 /* Send the header */
804 if( stream->i_header > 0 )
806 answer->i_body = stream->i_header;
807 answer->p_body = malloc( stream->i_header );
808 memcpy( answer->p_body, stream->p_header, stream->i_header );
810 answer->i_body_offset = stream->i_buffer_last_pos;
811 vlc_mutex_unlock( &stream->lock );
813 else
815 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
816 answer->i_body_offset = 0;
819 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
821 bool b_xplaystream = false;
822 int i;
824 httpd_MsgAdd( answer, "Content-type", "%s",
825 "application/octet-stream" );
826 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
827 httpd_MsgAdd( answer, "Pragma", "no-cache" );
828 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
829 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
831 /* Check if there is a xPlayStrm=1 */
832 for( i = 0; i < query->i_name; i++ )
834 if( !strcasecmp( query->name[i], "Pragma" ) &&
835 strstr( query->value[i], "xPlayStrm=1" ) )
837 b_xplaystream = true;
841 if( !b_xplaystream )
843 answer->i_body_offset = 0;
846 else
848 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
850 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
851 return VLC_SUCCESS;
855 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
856 const char *psz_url, const char *psz_mime,
857 const char *psz_user, const char *psz_password,
858 const vlc_acl_t *p_acl )
860 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
862 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
863 psz_password, p_acl )
864 ) == NULL )
866 free( stream );
867 return NULL;
869 vlc_mutex_init( &stream->lock );
870 if( psz_mime && *psz_mime )
872 stream->psz_mime = strdup( psz_mime );
874 else
876 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
878 stream->i_header = 0;
879 stream->p_header = NULL;
880 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
881 stream->p_buffer = malloc( stream->i_buffer_size );
882 /* We set to 1 to make life simpler
883 * (this way i_body_offset can never be 0) */
884 stream->i_buffer_pos = 1;
885 stream->i_buffer_last_pos = 1;
887 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
888 (httpd_callback_sys_t*)stream );
889 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
890 (httpd_callback_sys_t*)stream );
891 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
892 (httpd_callback_sys_t*)stream );
894 return stream;
897 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
899 vlc_mutex_lock( &stream->lock );
900 free( stream->p_header );
901 stream->p_header = NULL;
903 stream->i_header = i_data;
904 if( i_data > 0 )
906 stream->p_header = malloc( i_data );
907 memcpy( stream->p_header, p_data, i_data );
909 vlc_mutex_unlock( &stream->lock );
911 return VLC_SUCCESS;
914 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
916 int i_count;
917 int i_pos;
919 if( i_data < 0 || p_data == NULL )
921 return VLC_SUCCESS;
923 vlc_mutex_lock( &stream->lock );
925 /* save this pointer (to be used by new connection) */
926 stream->i_buffer_last_pos = stream->i_buffer_pos;
928 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
929 i_count = i_data;
930 while( i_count > 0)
932 int i_copy;
934 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
936 /* Ok, we can't go past the end of our buffer */
937 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
939 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
940 i_count -= i_copy;
941 p_data += i_copy;
944 stream->i_buffer_pos += i_data;
946 vlc_mutex_unlock( &stream->lock );
947 return VLC_SUCCESS;
950 void httpd_StreamDelete( httpd_stream_t *stream )
952 httpd_UrlDelete( stream->url );
953 vlc_mutex_destroy( &stream->lock );
954 free( stream->psz_mime );
955 free( stream->p_header );
956 free( stream->p_buffer );
957 free( stream );
960 /*****************************************************************************
961 * Low level
962 *****************************************************************************/
963 static void httpd_HostThread( httpd_host_t * );
965 /* create a new host */
966 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
967 int i_port )
969 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
973 static const char psz_object_type[] = "http server";
975 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
976 int i_port,
977 const char *psz_cert, const char *psz_key,
978 const char *psz_ca, const char *psz_crl )
980 httpd_t *httpd;
981 httpd_host_t *host;
982 tls_server_t *p_tls;
983 char *psz_host;
984 vlc_value_t lockval, ptrval;
985 int i;
987 if( psz_hostname == NULL )
988 psz_hostname = "";
990 psz_host = strdup( psz_hostname );
991 if( psz_host == NULL )
992 return NULL;
994 /* to be sure to avoid multiple creation */
995 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
996 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
997 vlc_mutex_lock( lockval.p_address );
998 httpd = libvlc_priv (p_this->p_libvlc)->p_httpd;
1000 if( httpd == NULL )
1002 msg_Info( p_this, "creating httpd" );
1003 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1004 VLC_OBJECT_GENERIC,
1005 psz_object_type );
1006 if( httpd == NULL )
1008 vlc_mutex_unlock( lockval.p_address );
1009 free( psz_host );
1010 return NULL;
1013 httpd->i_host = 0;
1014 httpd->host = NULL;
1016 ptrval.p_address = httpd;
1017 libvlc_priv (p_this->p_libvlc)->p_httpd = httpd;
1018 vlc_object_yield( httpd );
1019 vlc_object_attach( httpd, p_this->p_libvlc );
1022 /* verify if it already exist */
1023 for( i = httpd->i_host - 1; i >= 0; i-- )
1025 host = httpd->host[i];
1027 /* cannot mix TLS and non-TLS hosts */
1028 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1029 || ( host->i_port != i_port )
1030 || strcmp( host->psz_hostname, psz_hostname ) )
1031 continue;
1033 /* yep found */
1034 host->i_ref++;
1036 vlc_mutex_unlock( lockval.p_address );
1037 return host;
1040 host = NULL;
1042 /* determine TLS configuration */
1043 if ( psz_cert != NULL )
1045 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1046 if ( p_tls == NULL )
1048 msg_Err( p_this, "TLS initialization error" );
1049 goto error;
1052 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1054 msg_Err( p_this, "TLS CA error" );
1055 goto error;
1058 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1060 msg_Err( p_this, "TLS CRL error" );
1061 goto error;
1064 else
1065 p_tls = NULL;
1067 /* create the new host */
1068 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1069 VLC_OBJECT_GENERIC,
1070 psz_object_type );
1071 if (host == NULL)
1072 goto error;
1074 vlc_object_lock( host );
1075 if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
1077 msg_Err( host, "signaling pipe error: %m" );
1078 vlc_object_unlock( host );
1079 goto error;
1081 vlc_object_unlock( host );
1083 host->httpd = httpd;
1084 vlc_mutex_init( &host->lock );
1085 host->i_ref = 1;
1087 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1088 if( host->fds == NULL )
1090 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1091 goto error;
1093 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1095 host->i_port = i_port;
1096 host->psz_hostname = psz_host;
1098 host->i_url = 0;
1099 host->url = NULL;
1100 host->i_client = 0;
1101 host->client = NULL;
1103 host->p_tls = p_tls;
1105 /* create the thread */
1106 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1107 VLC_THREAD_PRIORITY_LOW, false ) )
1109 msg_Err( p_this, "cannot spawn http host thread" );
1110 goto error;
1113 /* now add it to httpd */
1114 TAB_APPEND( httpd->i_host, httpd->host, host );
1115 vlc_mutex_unlock( lockval.p_address );
1117 return host;
1119 error:
1120 free( psz_host );
1121 if( httpd->i_host <= 0 )
1123 vlc_object_release( httpd );
1124 vlc_object_detach( httpd );
1125 vlc_object_release( httpd );
1127 vlc_mutex_unlock( lockval.p_address );
1129 if( host != NULL )
1131 net_ListenClose( host->fds );
1132 vlc_mutex_destroy( &host->lock );
1133 vlc_object_release( host );
1136 if( p_tls != NULL )
1137 tls_ServerDelete( p_tls );
1139 return NULL;
1142 /* delete a host */
1143 void httpd_HostDelete( httpd_host_t *host )
1145 httpd_t *httpd = host->httpd;
1146 vlc_value_t lockval;
1147 int i;
1149 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1150 vlc_mutex_lock( lockval.p_address );
1152 host->i_ref--;
1153 if( host->i_ref > 0 )
1155 /* still used */
1156 vlc_mutex_unlock( lockval.p_address );
1157 msg_Dbg( host, "httpd_HostDelete: host still used" );
1158 return;
1160 TAB_REMOVE( httpd->i_host, httpd->host, host );
1162 vlc_object_kill( host );
1163 vlc_thread_join( host );
1165 msg_Dbg( host, "HTTP host removed" );
1167 for( i = 0; i < host->i_url; i++ )
1169 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1171 for( i = 0; i < host->i_client; i++ )
1173 httpd_client_t *cl = host->client[i];
1174 msg_Warn( host, "client still connected" );
1175 httpd_ClientClean( cl );
1176 TAB_REMOVE( host->i_client, host->client, cl );
1177 free( cl );
1178 i--;
1179 /* TODO */
1182 if( host->p_tls != NULL)
1183 tls_ServerDelete( host->p_tls );
1185 net_ListenClose( host->fds );
1186 free( host->psz_hostname );
1188 vlc_mutex_destroy( &host->lock );
1189 vlc_object_release( host );
1191 vlc_object_release( httpd );
1192 if( httpd->i_host <= 0 )
1194 msg_Dbg( httpd, "no host left, stopping httpd" );
1196 libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
1197 vlc_object_detach( httpd );
1198 vlc_object_release( httpd );
1201 vlc_mutex_unlock( lockval.p_address );
1204 /* register a new url */
1205 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1206 const char *psz_user, const char *psz_password,
1207 const vlc_acl_t *p_acl, bool b_check )
1209 httpd_url_t *url;
1210 int i;
1212 assert( psz_url != NULL );
1214 vlc_mutex_lock( &host->lock );
1215 if( b_check )
1217 for( i = 0; i < host->i_url; i++ )
1219 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1221 msg_Warn( host->httpd,
1222 "cannot add '%s' (url already defined)", psz_url );
1223 vlc_mutex_unlock( &host->lock );
1224 return NULL;
1229 url = malloc( sizeof( httpd_url_t ) );
1230 url->host = host;
1232 vlc_mutex_init( &url->lock );
1233 url->psz_url = strdup( psz_url );
1234 url->psz_user = strdup( psz_user ? psz_user : "" );
1235 url->psz_password = strdup( psz_password ? psz_password : "" );
1236 url->p_acl = ACL_Duplicate( host, p_acl );
1237 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1239 url->catch[i].cb = NULL;
1240 url->catch[i].p_sys = NULL;
1243 TAB_APPEND( host->i_url, host->url, url );
1244 vlc_mutex_unlock( &host->lock );
1246 return url;
1249 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1250 const char *psz_user, const char *psz_password,
1251 const vlc_acl_t *p_acl )
1253 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1254 psz_password, p_acl, false );
1257 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1258 const char *psz_user, const char *psz_password,
1259 const vlc_acl_t *p_acl )
1261 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1262 psz_password, p_acl, true );
1265 /* register callback on a url */
1266 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1267 httpd_callback_sys_t *p_sys )
1269 vlc_mutex_lock( &url->lock );
1270 url->catch[i_msg].cb = cb;
1271 url->catch[i_msg].p_sys= p_sys;
1272 vlc_mutex_unlock( &url->lock );
1274 return VLC_SUCCESS;
1277 /* delete an url */
1278 void httpd_UrlDelete( httpd_url_t *url )
1280 httpd_host_t *host = url->host;
1281 int i;
1283 vlc_mutex_lock( &host->lock );
1284 TAB_REMOVE( host->i_url, host->url, url );
1286 vlc_mutex_destroy( &url->lock );
1287 free( url->psz_url );
1288 free( url->psz_user );
1289 free( url->psz_password );
1290 ACL_Destroy( url->p_acl );
1292 for( i = 0; i < host->i_client; i++ )
1294 httpd_client_t *client = host->client[i];
1296 if( client->url == url )
1298 /* TODO complete it */
1299 msg_Warn( host, "force closing connections" );
1300 httpd_ClientClean( client );
1301 TAB_REMOVE( host->i_client, host->client, client );
1302 free( client );
1303 i--;
1306 free( url );
1307 vlc_mutex_unlock( &host->lock );
1310 void httpd_MsgInit( httpd_message_t *msg )
1312 msg->cl = NULL;
1313 msg->i_type = HTTPD_MSG_NONE;
1314 msg->i_proto = HTTPD_PROTO_NONE;
1315 msg->i_version = -1; /* FIXME */
1317 msg->i_status = 0;
1319 msg->psz_url = NULL;
1320 msg->psz_args = NULL;
1322 msg->i_channel = -1;
1324 msg->i_name = 0;
1325 msg->name = NULL;
1326 msg->i_value = 0;
1327 msg->value = NULL;
1329 msg->i_body_offset = 0;
1330 msg->i_body = 0;
1331 msg->p_body = NULL;
1334 void httpd_MsgClean( httpd_message_t *msg )
1336 int i;
1338 free( msg->psz_url );
1339 free( msg->psz_args );
1340 for( i = 0; i < msg->i_name; i++ )
1342 free( msg->name[i] );
1343 free( msg->value[i] );
1345 free( msg->name );
1346 free( msg->value );
1347 free( msg->p_body );
1348 httpd_MsgInit( msg );
1351 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1353 int i;
1355 for( i = 0; i < msg->i_name; i++ )
1357 if( !strcasecmp( msg->name[i], name ))
1359 return msg->value[i];
1362 return NULL;
1365 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1367 va_list args;
1368 char *value = NULL;
1370 va_start( args, psz_value );
1371 if( vasprintf( &value, psz_value, args ) == -1 )
1372 value = NULL;
1373 va_end( args );
1375 if( value == NULL )
1376 return;
1378 name = strdup( name );
1379 if( name == NULL )
1381 free( value );
1382 return;
1385 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1386 TAB_APPEND( msg->i_value, msg->value, value );
1389 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1391 cl->i_state = HTTPD_CLIENT_RECEIVING;
1392 cl->i_activity_date = now;
1393 cl->i_activity_timeout = INT64_C(10000000);
1394 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1395 cl->i_buffer = 0;
1396 cl->p_buffer = malloc( cl->i_buffer_size );
1397 cl->i_mode = HTTPD_CLIENT_FILE;
1398 cl->b_read_waiting = false;
1400 httpd_MsgInit( &cl->query );
1401 httpd_MsgInit( &cl->answer );
1404 void httpd_ClientModeStream( httpd_client_t *cl )
1406 cl->i_mode = HTTPD_CLIENT_STREAM;
1409 void httpd_ClientModeBidir( httpd_client_t *cl )
1411 cl->i_mode = HTTPD_CLIENT_BIDIR;
1414 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1416 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1419 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1421 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1424 static void httpd_ClientClean( httpd_client_t *cl )
1426 if( cl->fd >= 0 )
1428 if( cl->p_tls != NULL )
1429 tls_ServerSessionClose( cl->p_tls );
1430 net_Close( cl->fd );
1431 cl->fd = -1;
1434 httpd_MsgClean( &cl->answer );
1435 httpd_MsgClean( &cl->query );
1437 free( cl->p_buffer );
1438 cl->p_buffer = NULL;
1441 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1443 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1445 if( !cl ) return NULL;
1447 cl->i_ref = 0;
1448 cl->fd = fd;
1449 cl->url = NULL;
1450 cl->p_tls = p_tls;
1452 httpd_ClientInit( cl, now );
1454 return cl;
1457 static
1458 ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
1460 tls_session_t *p_tls;
1461 ssize_t val;
1463 p_tls = cl->p_tls;
1465 val = p_tls ? tls_Recv (p_tls, p, i_len)
1466 : recv (cl->fd, p, i_len, 0);
1467 while (val == -1 && errno == EINTR);
1468 return val;
1471 static
1472 ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
1474 tls_session_t *p_tls;
1475 ssize_t val;
1477 p_tls = cl->p_tls;
1479 val = p_tls ? tls_Send( p_tls, p, i_len )
1480 : send (cl->fd, p, i_len, 0);
1481 while (val == -1 && errno == EINTR);
1482 return val;
1486 static const struct
1488 const char name[16];
1489 int i_type;
1490 int i_proto;
1492 msg_type[] =
1494 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1495 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1496 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1497 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1498 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1499 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1500 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1501 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1502 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1503 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1504 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1508 static void httpd_ClientRecv( httpd_client_t *cl )
1510 int i_len;
1512 /* ignore leading whites */
1513 if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
1514 ( cl->i_buffer == 0 ) )
1516 unsigned char c;
1518 i_len = httpd_NetRecv( cl, &c, 1 );
1520 if( ( i_len > 0 ) && ( strchr( "\r\n\t ", c ) == NULL ) )
1522 cl->p_buffer[0] = c;
1523 cl->i_buffer++;
1526 else
1527 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1529 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1530 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1531 7 - cl->i_buffer );
1532 if( i_len > 0 )
1534 cl->i_buffer += i_len;
1537 if( ( cl->i_buffer >= 4 ) && ( cl->p_buffer[0] == '$' ) )
1539 /* Interleaved RTP over RTSP */
1540 cl->query.i_proto = HTTPD_PROTO_RTSP;
1541 cl->query.i_type = HTTPD_MSG_CHANNEL;
1542 cl->query.i_channel = cl->p_buffer[1];
1543 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1544 cl->query.p_body = malloc( cl->query.i_body );
1545 cl->i_buffer -= 4;
1546 memcpy( cl->query.p_body, cl->p_buffer + 4, cl->i_buffer );
1548 else
1549 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1550 * this is the maximum we can ask at this point. */
1551 if( cl->i_buffer >= 7 )
1553 if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
1555 cl->query.i_proto = HTTPD_PROTO_HTTP;
1556 cl->query.i_type = HTTPD_MSG_ANSWER;
1558 else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
1560 cl->query.i_proto = HTTPD_PROTO_RTSP;
1561 cl->query.i_type = HTTPD_MSG_ANSWER;
1563 else
1565 /* We need the full request line to determine the protocol. */
1566 cl->query.i_proto = HTTPD_PROTO_HTTP0;
1567 cl->query.i_type = HTTPD_MSG_NONE;
1571 else if( cl->query.i_body > 0 )
1573 /* we are reading the body of a request or a channel */
1574 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1575 cl->query.i_body - cl->i_buffer );
1576 if( i_len > 0 )
1578 cl->i_buffer += i_len;
1580 if( cl->i_buffer >= cl->query.i_body )
1582 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1585 else
1587 /* we are reading a header -> char by char */
1588 for( ;; )
1590 if( cl->i_buffer == cl->i_buffer_size )
1592 uint8_t *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
1593 if( newbuf == NULL )
1595 i_len = 0;
1596 break;
1599 cl->p_buffer = newbuf;
1600 cl->i_buffer_size += 1024;
1603 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1604 if( i_len <= 0 )
1606 break;
1608 cl->i_buffer++;
1610 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP0 )
1611 && ( cl->p_buffer[cl->i_buffer - 1] == '\n' ) )
1613 /* Request line is now complete */
1614 const char *p = memchr( cl->p_buffer, ' ', cl->i_buffer );
1615 size_t len;
1617 assert( cl->query.i_type == HTTPD_MSG_NONE );
1619 if( p == NULL ) /* no URI: evil guy */
1621 i_len = 0; /* drop connection */
1622 break;
1626 p++; /* skips extra spaces */
1627 while( *p == ' ' );
1629 p = memchr( p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p );
1630 if( p == NULL ) /* no explicit protocol: HTTP/0.9 */
1632 i_len = 0; /* not supported currently -> drop */
1633 break;
1637 p++; /* skips extra spaces ever again */
1638 while( *p == ' ' );
1640 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
1641 if( len < 7 ) /* foreign protocol */
1642 i_len = 0; /* I don't understand -> drop */
1643 else
1644 if( memcmp( p, "HTTP/1.", 7 ) == 0 )
1646 cl->query.i_proto = HTTPD_PROTO_HTTP;
1647 cl->query.i_version = atoi( p + 7 );
1649 else
1650 if( memcmp( p, "RTSP/1.", 7 ) == 0 )
1652 cl->query.i_proto = HTTPD_PROTO_RTSP;
1653 cl->query.i_version = atoi( p + 7 );
1655 else
1656 if( memcmp( p, "HTTP/", 5 ) == 0 )
1658 const uint8_t sorry[] =
1659 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1660 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1661 i_len = 0; /* drop */
1663 else
1664 if( memcmp( p, "RTSP/", 5 ) == 0 )
1666 const uint8_t sorry[] =
1667 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1668 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1669 i_len = 0; /* drop */
1671 else /* yet another foreign protocol */
1672 i_len = 0;
1674 if( i_len == 0 )
1675 break;
1678 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1679 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1681 char *p;
1683 /* we have finished the header so parse it and set i_body */
1684 cl->p_buffer[cl->i_buffer] = '\0';
1686 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1688 /* FIXME:
1689 * assume strlen( "HTTP/1.x" ) = 8
1691 cl->query.i_status =
1692 strtol( (char *)&cl->p_buffer[8],
1693 &p, 0 );
1694 while( *p == ' ' )
1695 p++;
1697 else
1699 unsigned i;
1701 p = NULL;
1702 cl->query.i_type = HTTPD_MSG_NONE;
1704 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1706 for( i = 0; msg_type[i].name[0]; i++ )
1708 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1709 strlen( msg_type[i].name ) ) )
1711 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1712 cl->query.i_type = msg_type[i].i_type;
1713 if( cl->query.i_proto != msg_type[i].i_proto )
1715 p = NULL;
1716 cl->query.i_proto = HTTPD_PROTO_NONE;
1717 cl->query.i_type = HTTPD_MSG_NONE;
1719 break;
1722 if( p == NULL )
1724 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1726 cl->query.i_proto = HTTPD_PROTO_HTTP;
1728 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1730 cl->query.i_proto = HTTPD_PROTO_RTSP;
1733 else
1735 char *p2;
1736 char *p3;
1738 while( *p == ' ' )
1740 p++;
1742 p2 = strchr( p, ' ' );
1743 if( p2 )
1745 *p2++ = '\0';
1747 if( !strncasecmp( p, "rtsp:", 5 ) )
1749 /* for rtsp url, you have rtsp://localhost:port/path */
1750 p += 5;
1751 while( *p == '/' ) p++;
1752 while( *p && *p != '/' ) p++;
1754 cl->query.psz_url = strdup( p );
1755 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1757 *p3++ = '\0';
1758 cl->query.psz_args = (uint8_t *)strdup( p3 );
1760 p = p2;
1763 if( p )
1765 p = strchr( p, '\n' );
1767 if( p )
1769 while( *p == '\n' || *p == '\r' )
1771 p++;
1773 while( p && *p != '\0' )
1775 char *line = p;
1776 char *eol = p = strchr( p, '\n' );
1777 char *colon;
1779 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1781 *eol-- = '\0';
1784 if( ( colon = strchr( line, ':' ) ) )
1786 char *name;
1787 char *value;
1789 *colon++ = '\0';
1790 while( *colon == ' ' )
1792 colon++;
1794 name = strdup( line );
1795 value = strdup( colon );
1797 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1798 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1800 if( !strcasecmp( name, "Content-Length" ) )
1802 cl->query.i_body = atol( value );
1806 if( p )
1808 p++;
1809 while( *p == '\n' || *p == '\r' )
1811 p++;
1816 if( cl->query.i_body > 0 )
1818 /* TODO Mhh, handle the case client will only send a
1819 * request and close the connection
1820 * to mark and of body (probably only RTSP) */
1821 cl->query.p_body = malloc( cl->query.i_body );
1822 cl->i_buffer = 0;
1824 else
1826 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1832 /* check if the client is to be set to dead */
1833 #if defined( WIN32 ) || defined( UNDER_CE )
1834 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1835 #else
1836 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1837 #endif
1839 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1841 /* connection closed -> end of data */
1842 if( cl->query.i_body > 0 )
1844 cl->query.i_body = cl->i_buffer;
1846 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1848 else
1850 cl->i_state = HTTPD_CLIENT_DEAD;
1854 /* XXX: for QT I have to disable timeout. Try to find why */
1855 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1856 cl->i_activity_timeout = 0;
1858 #if 0 /* Debugging only */
1859 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1861 int i;
1863 fprintf( stderr, "received new request\n" );
1864 fprintf( stderr, " - proto=%s\n",
1865 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1866 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1867 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1868 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1870 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1871 cl->query.psz_status );
1873 else if( cl->query.i_type != HTTPD_MSG_NONE )
1875 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1877 for( i = 0; i < cl->query.i_name; i++ )
1879 fprintf( stderr, " - option name='%s' value='%s'\n",
1880 cl->query.name[i], cl->query.value[i] );
1883 #endif
1886 static void httpd_ClientSend( httpd_client_t *cl )
1888 int i;
1889 int i_len;
1891 if( cl->i_buffer < 0 )
1893 /* We need to create the header */
1894 int i_size = 0;
1895 char *p;
1896 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1898 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1899 for( i = 0; i < cl->answer.i_name; i++ )
1901 i_size += strlen( cl->answer.name[i] ) + 2 +
1902 strlen( cl->answer.value[i] ) + 2;
1905 if( cl->i_buffer_size < i_size )
1907 cl->i_buffer_size = i_size;
1908 free( cl->p_buffer );
1909 cl->p_buffer = malloc( i_size );
1911 p = (char *)cl->p_buffer;
1913 p += sprintf( p, "%s.%u %d %s\r\n",
1914 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1915 cl->answer.i_version,
1916 cl->answer.i_status, psz_status );
1917 for( i = 0; i < cl->answer.i_name; i++ )
1919 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1920 cl->answer.value[i] );
1922 p += sprintf( p, "\r\n" );
1924 cl->i_buffer = 0;
1925 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1927 /*fprintf( stderr, "sending answer\n" );
1928 fprintf( stderr, "%s", cl->p_buffer );*/
1931 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1932 cl->i_buffer_size - cl->i_buffer );
1933 if( i_len >= 0 )
1935 cl->i_buffer += i_len;
1937 if( cl->i_buffer >= cl->i_buffer_size )
1939 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1940 !cl->b_read_waiting )
1942 /* catch more body data */
1943 int i_msg = cl->query.i_type;
1944 int64_t i_offset = cl->answer.i_body_offset;
1946 httpd_MsgClean( &cl->answer );
1947 cl->answer.i_body_offset = i_offset;
1949 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1950 &cl->answer, &cl->query );
1953 if( cl->answer.i_body > 0 )
1955 /* send the body data */
1956 free( cl->p_buffer );
1957 cl->p_buffer = cl->answer.p_body;
1958 cl->i_buffer_size = cl->answer.i_body;
1959 cl->i_buffer = 0;
1961 cl->answer.i_body = 0;
1962 cl->answer.p_body = NULL;
1964 else
1966 /* send finished */
1967 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1971 else
1973 #if defined( WIN32 ) || defined( UNDER_CE )
1974 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1975 #else
1976 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1977 #endif
1979 /* error */
1980 cl->i_state = HTTPD_CLIENT_DEAD;
1985 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1987 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1989 case 0:
1990 cl->i_state = HTTPD_CLIENT_RECEIVING;
1991 break;
1993 case -1:
1994 cl->i_state = HTTPD_CLIENT_DEAD;
1995 cl->p_tls = NULL;
1996 break;
1998 case 2:
1999 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2003 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2005 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2007 case 0:
2008 cl->i_state = HTTPD_CLIENT_RECEIVING;
2009 break;
2011 case -1:
2012 cl->i_state = HTTPD_CLIENT_DEAD;
2013 cl->p_tls = NULL;
2014 break;
2016 case 1:
2017 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2018 break;
2022 static void httpd_HostThread( httpd_host_t *host )
2024 tls_session_t *p_tls = NULL;
2025 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
2026 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
2027 int evfd;
2028 bool b_die;
2030 retry:
2031 vlc_object_lock( host );
2032 evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
2033 b_die = !vlc_object_alive( host );
2034 vlc_object_unlock( host );
2036 while( !b_die )
2038 if( host->i_url <= 0 )
2040 /* 0.2s (FIXME: use a condition variable) */
2041 msleep( 200000 );
2042 goto retry;
2045 /* prepare a new TLS session */
2046 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2047 p_tls = tls_ServerSessionPrepare( host->p_tls );
2049 struct pollfd ufd[host->nfd + host->i_client + 1];
2050 unsigned nfd;
2051 for( nfd = 0; nfd < host->nfd; nfd++ )
2053 ufd[nfd].fd = host->fds[nfd];
2054 ufd[nfd].events = POLLIN;
2055 ufd[nfd].revents = 0;
2058 /* add all socket that should be read/write and close dead connection */
2059 vlc_mutex_lock( &host->lock );
2060 mtime_t now = mdate();
2061 bool b_low_delay = false;
2063 for(int i_client = 0; i_client < host->i_client; i_client++ )
2065 httpd_client_t *cl = host->client[i_client];
2066 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2067 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2068 ( cl->i_activity_timeout > 0 &&
2069 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2071 httpd_ClientClean( cl );
2072 stats_UpdateInteger( host, p_active_counter, -1, NULL );
2073 TAB_REMOVE( host->i_client, host->client, cl );
2074 free( cl );
2075 i_client--;
2076 continue;
2079 struct pollfd *pufd = ufd + nfd;
2080 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2082 pufd->fd = cl->fd;
2083 pufd->events = pufd->revents = 0;
2085 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2086 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2088 pufd->events = POLLIN;
2090 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2091 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2093 pufd->events = POLLOUT;
2095 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2097 httpd_message_t *answer = &cl->answer;
2098 httpd_message_t *query = &cl->query;
2099 int i_msg = query->i_type;
2101 httpd_MsgInit( answer );
2103 /* Handle what we received */
2104 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2105 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2107 /* we can only receive request from client when not
2108 * in BIDIR mode */
2109 cl->url = NULL;
2110 cl->i_state = HTTPD_CLIENT_DEAD;
2112 else if( i_msg == HTTPD_MSG_ANSWER )
2114 /* We are in BIDIR mode, trigger the callback and then
2115 * check for new data */
2116 if( cl->url && cl->url->catch[i_msg].cb )
2118 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2119 cl, NULL, query );
2121 cl->i_state = HTTPD_CLIENT_WAITING;
2123 else if( i_msg == HTTPD_MSG_CHANNEL )
2125 /* We are in BIDIR mode, trigger the callback and then
2126 * check for new data */
2127 if( cl->url && cl->url->catch[i_msg].cb )
2129 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2130 cl, NULL, query );
2132 cl->i_state = HTTPD_CLIENT_WAITING;
2134 else if( i_msg == HTTPD_MSG_OPTIONS )
2137 answer->i_type = HTTPD_MSG_ANSWER;
2138 answer->i_proto = query->i_proto;
2139 answer->i_status = 200;
2140 answer->i_body = 0;
2141 answer->p_body = NULL;
2143 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
2144 httpd_MsgAdd( answer, "Content-Length", "0" );
2146 switch( query->i_proto )
2148 case HTTPD_PROTO_HTTP:
2149 answer->i_version = 1;
2150 httpd_MsgAdd( answer, "Allow",
2151 "GET,HEAD,POST,OPTIONS" );
2152 break;
2154 case HTTPD_PROTO_RTSP:
2156 const char *p;
2157 answer->i_version = 0;
2159 p = httpd_MsgGet( query, "Cseq" );
2160 if( p != NULL )
2161 httpd_MsgAdd( answer, "Cseq", "%s", p );
2162 p = httpd_MsgGet( query, "Timestamp" );
2163 if( p != NULL )
2164 httpd_MsgAdd( answer, "Timestamp", "%s", p );
2166 p = httpd_MsgGet( query, "Require" );
2167 if( p != NULL )
2169 answer->i_status = 551;
2170 httpd_MsgAdd( query, "Unsupported", "%s", p );
2173 httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
2174 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
2175 break;
2179 cl->i_buffer = -1; /* Force the creation of the answer in
2180 * httpd_ClientSend */
2181 cl->i_state = HTTPD_CLIENT_SENDING;
2183 else if( i_msg == HTTPD_MSG_NONE )
2185 if( query->i_proto == HTTPD_PROTO_NONE )
2187 cl->url = NULL;
2188 cl->i_state = HTTPD_CLIENT_DEAD;
2190 else
2192 char *p;
2194 /* unimplemented */
2195 answer->i_proto = query->i_proto ;
2196 answer->i_type = HTTPD_MSG_ANSWER;
2197 answer->i_version= 0;
2198 answer->i_status = 501;
2200 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2201 answer->p_body = (uint8_t *)p;
2202 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2204 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2205 cl->i_state = HTTPD_CLIENT_SENDING;
2208 else
2210 bool b_auth_failed = false;
2211 bool b_hosts_failed = false;
2213 /* Search the url and trigger callbacks */
2214 for(int i = 0; i < host->i_url; i++ )
2216 httpd_url_t *url = host->url[i];
2218 if( !strcmp( url->psz_url, query->psz_url ) )
2220 if( url->catch[i_msg].cb )
2222 if( answer && ( url->p_acl != NULL ) )
2224 char ip[NI_MAXNUMERICHOST];
2226 if( ( httpd_ClientIP( cl, ip ) == NULL )
2227 || ACL_Check( url->p_acl, ip ) )
2229 b_hosts_failed = true;
2230 break;
2234 if( answer && ( *url->psz_user || *url->psz_password ) )
2236 /* create the headers */
2237 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2238 char *user = NULL, *pass = NULL;
2240 if( b64 != NULL
2241 && !strncasecmp( b64, "BASIC", 5 ) )
2243 b64 += 5;
2244 while( *b64 == ' ' )
2245 b64++;
2247 user = vlc_b64_decode( b64 );
2248 if (user != NULL)
2250 pass = strchr (user, ':');
2251 if (pass != NULL)
2252 *pass++ = '\0';
2256 if ((user == NULL) || (pass == NULL)
2257 || strcmp (user, url->psz_user)
2258 || strcmp (pass, url->psz_password))
2260 httpd_MsgAdd( answer,
2261 "WWW-Authenticate",
2262 "Basic realm=\"%s\"",
2263 url->psz_user );
2264 /* We fail for all url */
2265 b_auth_failed = true;
2266 free( user );
2267 break;
2270 free( user );
2273 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2275 if( answer->i_proto == HTTPD_PROTO_NONE )
2277 /* Raw answer from a CGI */
2278 cl->i_buffer = cl->i_buffer_size;
2280 else
2281 cl->i_buffer = -1;
2283 /* only one url can answer */
2284 answer = NULL;
2285 if( cl->url == NULL )
2287 cl->url = url;
2294 if( answer )
2296 char *p;
2298 answer->i_proto = query->i_proto;
2299 answer->i_type = HTTPD_MSG_ANSWER;
2300 answer->i_version= 0;
2302 if( b_hosts_failed )
2304 answer->i_status = 403;
2306 else if( b_auth_failed )
2308 answer->i_status = 401;
2310 else
2312 /* no url registered */
2313 answer->i_status = 404;
2316 answer->i_body = httpd_HtmlError (&p,
2317 answer->i_status,
2318 query->psz_url);
2319 answer->p_body = (uint8_t *)p;
2321 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2322 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2323 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2326 cl->i_state = HTTPD_CLIENT_SENDING;
2329 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2331 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2333 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2334 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2335 bool b_connection = false;
2336 bool b_keepalive = false;
2337 bool b_query = false;
2339 cl->url = NULL;
2340 if( psz_connection )
2342 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2343 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2346 if( psz_query )
2348 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2351 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2352 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2353 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2354 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2355 !b_query && !b_connection ) )
2357 httpd_MsgClean( &cl->query );
2358 httpd_MsgInit( &cl->query );
2360 cl->i_buffer = 0;
2361 cl->i_buffer_size = 1000;
2362 free( cl->p_buffer );
2363 cl->p_buffer = malloc( cl->i_buffer_size );
2364 cl->i_state = HTTPD_CLIENT_RECEIVING;
2366 else
2368 cl->i_state = HTTPD_CLIENT_DEAD;
2370 httpd_MsgClean( &cl->answer );
2372 else if( cl->b_read_waiting )
2374 /* we have a message waiting for us to read it */
2375 httpd_MsgClean( &cl->answer );
2376 httpd_MsgClean( &cl->query );
2378 cl->i_buffer = 0;
2379 cl->i_buffer_size = 1000;
2380 free( cl->p_buffer );
2381 cl->p_buffer = malloc( cl->i_buffer_size );
2382 cl->i_state = HTTPD_CLIENT_RECEIVING;
2383 cl->b_read_waiting = false;
2385 else
2387 int64_t i_offset = cl->answer.i_body_offset;
2388 httpd_MsgClean( &cl->answer );
2390 cl->answer.i_body_offset = i_offset;
2391 free( cl->p_buffer );
2392 cl->p_buffer = NULL;
2393 cl->i_buffer = 0;
2394 cl->i_buffer_size = 0;
2396 cl->i_state = HTTPD_CLIENT_WAITING;
2399 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2401 int64_t i_offset = cl->answer.i_body_offset;
2402 int i_msg = cl->query.i_type;
2404 httpd_MsgInit( &cl->answer );
2405 cl->answer.i_body_offset = i_offset;
2407 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2408 &cl->answer, &cl->query );
2409 if( cl->answer.i_type != HTTPD_MSG_NONE )
2411 /* we have new data, so re-enter send mode */
2412 cl->i_buffer = 0;
2413 cl->p_buffer = cl->answer.p_body;
2414 cl->i_buffer_size = cl->answer.i_body;
2415 cl->answer.p_body = NULL;
2416 cl->answer.i_body = 0;
2417 cl->i_state = HTTPD_CLIENT_SENDING;
2421 /* Special for BIDIR mode we also check reading */
2422 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2423 cl->i_state == HTTPD_CLIENT_SENDING )
2425 pufd->events |= POLLIN;
2428 if (pufd->events != 0)
2429 nfd++;
2430 else
2431 b_low_delay = true;
2433 vlc_mutex_unlock( &host->lock );
2435 ufd[nfd].fd = evfd;
2436 ufd[nfd].events = POLLIN;
2437 ufd[nfd].revents = 0;
2438 nfd++;
2440 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2441 switch( poll( ufd, nfd, b_low_delay ? 20 : -1) )
2443 case -1:
2444 if (errno != EINTR)
2446 /* Kernel on low memory or a bug: pace */
2447 msg_Err( host, "polling error: %m" );
2448 msleep( 100000 );
2450 case 0:
2451 continue;
2454 vlc_object_lock( host );
2455 if( ufd[nfd - 1].revents )
2457 b_die = !vlc_object_alive( host );
2458 if( !b_die )
2459 vlc_object_wait( host );
2461 vlc_object_unlock( host );
2463 /* Handle client sockets */
2464 vlc_mutex_lock( &host->lock );
2465 now = mdate();
2466 for( int i_client = 0; i_client < host->i_client; i_client++ )
2468 httpd_client_t *cl = host->client[i_client];
2469 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2471 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2473 if( cl->fd != pufd->fd )
2474 continue; // we were not waiting for this client
2475 if( pufd->revents == 0 )
2476 continue; // no event received
2478 cl->i_activity_date = now;
2480 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2482 httpd_ClientRecv( cl );
2484 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2486 httpd_ClientSend( cl );
2488 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2490 httpd_ClientTlsHsIn( cl );
2492 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2494 httpd_ClientTlsHsOut( cl );
2497 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2498 cl->i_state == HTTPD_CLIENT_SENDING &&
2499 (pufd->revents & POLLIN) )
2501 cl->b_read_waiting = true;
2504 vlc_mutex_unlock( &host->lock );
2506 /* Handle server sockets (accept new connections) */
2507 for( nfd = 0; nfd < host->nfd; nfd++ )
2509 httpd_client_t *cl;
2510 int i_state = -1;
2511 int fd = ufd[nfd].fd;
2513 assert (fd == host->fds[nfd]);
2515 if( ufd[nfd].revents == 0 )
2516 continue;
2518 /* */
2519 fd = accept (fd, NULL, NULL);
2520 if (fd == -1)
2521 continue;
2523 net_SetupSocket (fd);
2524 if( p_tls != NULL )
2526 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2528 case -1:
2529 msg_Err( host, "Rejecting TLS connection" );
2530 net_Close( fd );
2531 fd = -1;
2532 p_tls = NULL;
2533 break;
2535 case 1: /* missing input - most likely */
2536 i_state = HTTPD_CLIENT_TLS_HS_IN;
2537 break;
2539 case 2: /* missing output */
2540 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2541 break;
2544 if( (p_tls == NULL) != (host->p_tls == NULL) )
2545 break; // wasted TLS session, cannot accept() anymore
2548 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2549 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2550 cl = httpd_ClientNew( fd, p_tls, now );
2551 p_tls = NULL;
2552 vlc_mutex_lock( &host->lock );
2553 TAB_APPEND( host->i_client, host->client, cl );
2554 vlc_mutex_unlock( &host->lock );
2555 if( i_state != -1 )
2556 cl->i_state = i_state; // override state for TLS
2558 if (host->p_tls != NULL)
2559 break; // cannot accept further without new TLS session
2564 if( p_tls != NULL )
2565 tls_ServerSessionClose( p_tls );
2566 if( p_total_counter )
2567 stats_CounterClean( p_total_counter );
2568 if( p_active_counter )
2569 stats_CounterClean( p_active_counter );
2572 #else /* ENABLE_HTTPD */
2574 /* We just define an empty wrapper */
2575 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, const char *b,
2576 int c,
2577 const char *e, const char *f,
2578 const char *g, const char* h)
2580 msg_Err( a, "HTTP daemon support is disabled" );
2581 return NULL;
2584 httpd_host_t *httpd_HostNew( vlc_object_t *a, const char *b,
2585 int c )
2587 msg_Err( a, "HTTP daemon support is disabled" );
2588 return NULL;
2591 void httpd_HostDelete( httpd_host_t *a )
2595 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
2596 const char *psz_user, const char *psz_password,
2597 const vlc_acl_t *p_acl )
2599 return NULL;
2602 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
2603 const char *psz_user, const char *psz_password,
2604 const vlc_acl_t *p_acl )
2606 return NULL;
2609 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2610 httpd_callback_sys_t *d )
2612 return 0;
2615 void httpd_UrlDelete( httpd_url_t *a )
2619 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
2621 return NULL;
2624 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
2626 return NULL;
2629 void httpd_ClientModeStream( httpd_client_t *a )
2633 void httpd_ClientModeBidir( httpd_client_t *a )
2637 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
2639 return NULL;
2642 httpd_file_t *httpd_FileNew( httpd_host_t *host,
2643 const char *psz_url, const char *psz_mime,
2644 const char *psz_user, const char *psz_password,
2645 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
2646 httpd_file_sys_t *p_sys )
2648 return NULL;
2651 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2652 const char *psz_user,
2653 const char *psz_password,
2654 const vlc_acl_t *p_acl,
2655 httpd_handler_callback_t pf_fill,
2656 httpd_handler_sys_t *p_sys )
2658 return NULL;
2661 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
2663 return NULL;
2666 void httpd_RedirectDelete( httpd_redirect_t *a )
2670 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
2671 const char *psz_url_src )
2673 return NULL;
2676 void httpd_StreamDelete( httpd_stream_t *a )
2680 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2682 return 0;
2685 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2687 return 0;
2690 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
2691 const char *psz_url, const char *psz_mime,
2692 const char *psz_user, const char *psz_password,
2693 const vlc_acl_t *p_acl )
2695 return NULL;
2698 void httpd_MsgInit ( httpd_message_t *a )
2702 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2706 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
2708 return "";
2711 void httpd_MsgClean( httpd_message_t *a )
2715 #endif /* ENABLE_HTTPD */