1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2004-2007 Rémi Denis-Courmont
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_httpd.h>
35 #include <vlc_network.h>
38 #include <vlc_strings.h>
39 #include "../libvlc.h"
56 #if defined( UNDER_CE )
58 #elif defined( WIN32 )
59 # include <winsock2.h>
61 # include <sys/socket.h>
65 /* We need HUGE buffer otherwise TCP throughput is very limited */
66 #define HTTPD_CL_BUFSIZE 1000000
68 #define HTTPD_CL_BUFSIZE 10000
71 static void httpd_ClientClean( httpd_client_t *cl );
82 /* each host run in his own thread */
92 /* address/port and socket for listening at connections */
100 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
101 * This will slow down the url research but make my live easier
102 * All url will have their cb trigger, but only the first one can answer
108 httpd_client_t **client;
129 httpd_callback_sys_t *p_sys;
130 } catch[HTTPD_MSG_MAX];
136 HTTPD_CLIENT_RECEIVING,
137 HTTPD_CLIENT_RECEIVE_DONE,
139 HTTPD_CLIENT_SENDING,
140 HTTPD_CLIENT_SEND_DONE,
142 HTTPD_CLIENT_WAITING,
146 HTTPD_CLIENT_TLS_HS_IN,
147 HTTPD_CLIENT_TLS_HS_OUT
153 HTTPD_CLIENT_FILE, /* default */
154 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
155 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
158 struct httpd_client_t
168 int b_read_waiting; /* stop as soon as possible sending */
170 mtime_t i_activity_date;
171 mtime_t i_activity_timeout;
173 /* buffer for reading header */
179 httpd_message_t query; /* client -> httpd */
180 httpd_message_t answer; /* httpd -> client */
183 tls_session_t *p_tls;
187 /*****************************************************************************
189 *****************************************************************************/
192 const char psz_ext[8];
193 const char *psz_mime;
196 { ".htm", "text/html" },
197 { ".html", "text/html" },
198 { ".txt", "text/plain" },
199 { ".xml", "text/xml" },
200 { ".dtd", "text/dtd" },
202 { ".css", "text/css" },
205 { ".gif", "image/gif" },
206 { ".jpe", "image/jpeg" },
207 { ".jpg", "image/jpeg" },
208 { ".jpeg", "image/jpeg" },
209 { ".png", "image/png" },
210 /* same as modules/mux/mpjpeg.c here: */
211 { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" },
214 { ".avi", "video/avi" },
215 { ".asf", "video/x-ms-asf" },
216 { ".m1a", "audio/mpeg" },
217 { ".m2a", "audio/mpeg" },
218 { ".m1v", "video/mpeg" },
219 { ".m2v", "video/mpeg" },
220 { ".mp2", "audio/mpeg" },
221 { ".mp3", "audio/mpeg" },
222 { ".mpa", "audio/mpeg" },
223 { ".mpg", "video/mpeg" },
224 { ".mpeg", "video/mpeg" },
225 { ".mpe", "video/mpeg" },
226 { ".mov", "video/quicktime" },
227 { ".moov", "video/quicktime" },
228 { ".ogg", "application/ogg" },
229 { ".ogm", "application/ogg" },
230 { ".wav", "audio/wav" },
231 { ".wma", "audio/x-ms-wma" },
232 { ".wmv", "video/x-ms-wmv" },
239 static const char *httpd_MimeFromUrl( const char *psz_url )
244 psz_ext = strrchr( psz_url, '.' );
249 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
251 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
253 return http_mime[i].psz_mime;
257 return "application/octet-stream";
264 const char psz_reason[36];
267 static const http_status_info http_reason[] =
269 /*{ 100, "Continue" },
270 { 101, "Switching Protocols" },*/
272 /*{ 201, "Created" },
274 { 203, "Non-authoritative information" },
275 { 204, "No content" },
276 { 205, "Reset content" },
277 { 206, "Partial content" },
278 { 250, "Low on storage space" },
279 { 300, "Multiple choices" },*/
280 { 301, "Moved permanently" },
281 /*{ 302, "Moved temporarily" },
282 { 303, "See other" },
283 { 304, "Not modified" },
284 { 305, "Use proxy" },
285 { 307, "Temporary redirect" },
286 { 400, "Bad request" },*/
287 { 401, "Unauthorized" },
288 /*{ 402, "Payment Required" },*/
289 { 403, "Forbidden" },
290 { 404, "Not found" },
291 { 405, "Method not allowed" },
292 /*{ 406, "Not acceptable" },
293 { 407, "Proxy authentication required" },
294 { 408, "Request time-out" },
297 { 411, "Length required" },
298 { 412, "Precondition failed" },
299 { 413, "Request entity too large" },
300 { 414, "Request-URI too large" },
301 { 415, "Unsupported media Type" },
302 { 416, "Requested range not satisfiable" },
303 { 417, "Expectation failed" },
304 { 451, "Parameter not understood" },
305 { 452, "Conference not found" },
306 { 453, "Not enough bandwidth" },*/
307 { 454, "Session not found" },
308 /*{ 455, "Method not valid in this State" },*/
309 { 456, "Header field not valid for resource" },
310 /*{ 457, "Invalid range" },
311 { 458, "Read-only parameter" },*/
312 { 459, "Aggregate operation not allowed" },
313 { 460, "Non-aggregate operation not allowed" },
314 { 461, "Unsupported transport" },
315 /*{ 462, "Destination unreachable" },*/
316 { 500, "Internal server error" },
317 { 501, "Not implemented" },
318 /*{ 502, "Bad gateway" },*/
319 { 503, "Service unavailable" },
320 /*{ 504, "Gateway time-out" },*/
321 { 505, "Protocol version not supported" },
322 { 551, "Option not supported" },
326 static const char psz_fallback_reason[5][16] =
327 { "Continue", "OK", "Found", "Client error", "Server error" };
329 static const char *httpd_ReasonFromCode( unsigned i_code )
331 const http_status_info *p;
333 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
335 for (p = http_reason; i_code > p->i_code; p++);
337 if( p->i_code == i_code )
338 return p->psz_reason;
340 return psz_fallback_reason[(i_code / 100) - 1];
344 static size_t httpd_HtmlError (char **body, int code, const char *url)
346 const char *errname = httpd_ReasonFromCode (code);
347 assert (errname != NULL);
349 int res = asprintf (body,
350 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
351 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
352 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
353 "<html lang=\"en\">\n"
355 "<title>%s</title>\n"
358 "<h1>%d %s%s%s%s</h1>\n"
360 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
362 "</html>\n", errname, code, errname,
363 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
375 /*****************************************************************************
376 * High Level Functions: httpd_file_t
377 *****************************************************************************/
385 httpd_file_callback_t pf_fill;
386 httpd_file_sys_t *p_sys;
391 httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
392 httpd_message_t *answer, const httpd_message_t *query )
394 httpd_file_t *file = (httpd_file_t*)p_sys;
395 uint8_t **pp_body, *p_body;
396 const char *psz_connection;
397 int *pi_body, i_body;
399 if( answer == NULL || query == NULL )
403 answer->i_proto = HTTPD_PROTO_HTTP;
404 answer->i_version= 1;
405 answer->i_type = HTTPD_MSG_ANSWER;
407 answer->i_status = 200;
409 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
410 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
412 if( query->i_type != HTTPD_MSG_HEAD )
414 pp_body = &answer->p_body;
415 pi_body = &answer->i_body;
419 /* The file still needs to be executed. */
426 if( query->i_type == HTTPD_MSG_POST )
428 /* msg_Warn not supported */
431 uint8_t *psz_args = query->psz_args;
432 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
434 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
439 /* We respect client request */
440 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
441 if( psz_connection != NULL )
443 httpd_MsgAdd( answer, "Connection", "%s", psz_connection );
446 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
451 httpd_file_t *httpd_FileNew( httpd_host_t *host,
452 const char *psz_url, const char *psz_mime,
453 const char *psz_user, const char *psz_password,
454 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
455 httpd_file_sys_t *p_sys )
457 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
459 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
460 psz_password, p_acl )
467 file->psz_url = strdup( psz_url );
468 if( psz_mime && *psz_mime )
470 file->psz_mime = strdup( psz_mime );
474 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
477 file->pf_fill = pf_fill;
480 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
481 (httpd_callback_sys_t*)file );
482 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
483 (httpd_callback_sys_t*)file );
484 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
485 (httpd_callback_sys_t*)file );
490 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
492 httpd_file_sys_t *p_sys = file->p_sys;
494 httpd_UrlDelete( file->url );
496 free( file->psz_url );
497 free( file->psz_mime );
504 /*****************************************************************************
505 * High Level Functions: httpd_handler_t (for CGIs)
506 *****************************************************************************/
507 struct httpd_handler_t
511 httpd_handler_callback_t pf_fill;
512 httpd_handler_sys_t *p_sys;
517 httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
518 httpd_message_t *answer, const httpd_message_t *query )
520 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
521 char psz_remote_addr[NI_MAXNUMERICHOST];
523 if( answer == NULL || query == NULL )
527 answer->i_proto = HTTPD_PROTO_NONE;
528 answer->i_type = HTTPD_MSG_ANSWER;
530 /* We do it ourselves, thanks */
531 answer->i_status = 0;
533 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
534 *psz_remote_addr = '\0';
536 uint8_t *psz_args = query->psz_args;
537 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
538 query->i_type, query->p_body, query->i_body,
539 psz_remote_addr, NULL,
540 &answer->p_body, &answer->i_body );
542 if( query->i_type == HTTPD_MSG_HEAD )
544 char *p = (char *)answer->p_body;
546 /* Looks for end of header (i.e. one empty line) */
547 while ( (p = strchr( p, '\r' )) != NULL )
549 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
550 && p[3] && p[3] == '\n' )
559 answer->i_body = strlen((char*)answer->p_body) + 1;
560 answer->p_body = realloc( answer->p_body, answer->i_body );
564 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
566 int i_status, i_headers;
567 char *psz_headers, *psz_new;
568 const char *psz_status;
570 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
573 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
574 if( *psz_headers ) psz_headers++;
575 if( *psz_headers ) psz_headers++;
576 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
581 psz_headers = (char *)answer->p_body;
582 i_headers = answer->i_body;
585 psz_status = httpd_ReasonFromCode( i_status );
586 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
587 + strlen(psz_status) + i_headers - 1;
588 psz_new = (char *)malloc( answer->i_body + 1);
589 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
590 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
591 free( answer->p_body );
592 answer->p_body = (uint8_t *)psz_new;
598 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
599 const char *psz_user,
600 const char *psz_password,
601 const vlc_acl_t *p_acl,
602 httpd_handler_callback_t pf_fill,
603 httpd_handler_sys_t *p_sys )
605 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
607 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
608 psz_password, p_acl )
615 handler->pf_fill = pf_fill;
616 handler->p_sys = p_sys;
618 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
619 (httpd_callback_sys_t*)handler );
620 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
621 (httpd_callback_sys_t*)handler );
622 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
623 (httpd_callback_sys_t*)handler );
628 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
630 httpd_handler_sys_t *p_sys = handler->p_sys;
631 httpd_UrlDelete( handler->url );
636 /*****************************************************************************
637 * High Level Functions: httpd_redirect_t
638 *****************************************************************************/
639 struct httpd_redirect_t
645 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
646 httpd_client_t *cl, httpd_message_t *answer,
647 const httpd_message_t *query )
649 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
653 if( answer == NULL || query == NULL )
657 answer->i_proto = HTTPD_PROTO_HTTP;
658 answer->i_version= 1;
659 answer->i_type = HTTPD_MSG_ANSWER;
660 answer->i_status = 301;
662 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
663 answer->p_body = (unsigned char *)p_body;
665 /* XXX check if it's ok or we need to set an absolute url */
666 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
668 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
673 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
674 const char *psz_url_src )
676 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
678 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
683 rdir->psz_dst = strdup( psz_url_dst );
685 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
686 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
687 (httpd_callback_sys_t*)rdir );
688 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
689 (httpd_callback_sys_t*)rdir );
690 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
691 (httpd_callback_sys_t*)rdir );
692 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
693 (httpd_callback_sys_t*)rdir );
697 void httpd_RedirectDelete( httpd_redirect_t *rdir )
699 httpd_UrlDelete( rdir->url );
700 free( rdir->psz_dst );
704 /*****************************************************************************
705 * High Level Funtions: httpd_stream_t
706 *****************************************************************************/
707 struct httpd_stream_t
714 /* Header to send as first packet */
718 /* circular buffer */
719 int i_buffer_size; /* buffer size, can't be reallocated smaller */
720 uint8_t *p_buffer; /* buffer */
721 int64_t i_buffer_pos; /* absolute position from begining */
722 int64_t i_buffer_last_pos; /* a new connection will start with that */
725 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
726 httpd_client_t *cl, httpd_message_t *answer,
727 const httpd_message_t *query )
729 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
731 if( answer == NULL || query == NULL || cl == NULL )
736 if( answer->i_body_offset > 0 )
742 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
743 answer->i_body_offset );
746 if( answer->i_body_offset >= stream->i_buffer_pos )
748 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
749 return VLC_EGENERIC; /* wait, no data available */
751 if( answer->i_body_offset + stream->i_buffer_size <
752 stream->i_buffer_pos )
754 /* this client isn't fast enough */
756 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
757 answer->i_body_offset, stream->i_buffer_last_pos );
759 answer->i_body_offset = stream->i_buffer_last_pos;
762 i_pos = answer->i_body_offset % stream->i_buffer_size;
763 i_write = stream->i_buffer_pos - answer->i_body_offset;
764 if( i_write > HTTPD_CL_BUFSIZE )
766 i_write = HTTPD_CL_BUFSIZE;
768 else if( i_write <= 0 )
770 return VLC_EGENERIC; /* wait, no data available */
773 /* Don't go past the end of the circular buffer */
774 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
776 /* using HTTPD_MSG_ANSWER -> data available */
777 answer->i_proto = HTTPD_PROTO_HTTP;
778 answer->i_version= 0;
779 answer->i_type = HTTPD_MSG_ANSWER;
781 answer->i_body = i_write;
782 answer->p_body = malloc( i_write );
783 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
785 answer->i_body_offset += i_write;
791 answer->i_proto = HTTPD_PROTO_HTTP;
792 answer->i_version= 0;
793 answer->i_type = HTTPD_MSG_ANSWER;
795 answer->i_status = 200;
797 if( query->i_type != HTTPD_MSG_HEAD )
799 httpd_ClientModeStream( cl );
800 vlc_mutex_lock( &stream->lock );
801 /* Send the header */
802 if( stream->i_header > 0 )
804 answer->i_body = stream->i_header;
805 answer->p_body = malloc( stream->i_header );
806 memcpy( answer->p_body, stream->p_header, stream->i_header );
808 answer->i_body_offset = stream->i_buffer_last_pos;
809 vlc_mutex_unlock( &stream->lock );
813 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
814 answer->i_body_offset = 0;
817 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
819 bool b_xplaystream = false;
822 httpd_MsgAdd( answer, "Content-type", "%s",
823 "application/octet-stream" );
824 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
825 httpd_MsgAdd( answer, "Pragma", "no-cache" );
826 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
827 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
829 /* Check if there is a xPlayStrm=1 */
830 for( i = 0; i < query->i_name; i++ )
832 if( !strcasecmp( query->name[i], "Pragma" ) &&
833 strstr( query->value[i], "xPlayStrm=1" ) )
835 b_xplaystream = true;
841 answer->i_body_offset = 0;
846 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
848 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
853 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
854 const char *psz_url, const char *psz_mime,
855 const char *psz_user, const char *psz_password,
856 const vlc_acl_t *p_acl )
858 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
860 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
861 psz_password, p_acl )
867 vlc_mutex_init( &stream->lock );
868 if( psz_mime && *psz_mime )
870 stream->psz_mime = strdup( psz_mime );
874 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
876 stream->i_header = 0;
877 stream->p_header = NULL;
878 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
879 stream->p_buffer = malloc( stream->i_buffer_size );
880 /* We set to 1 to make life simpler
881 * (this way i_body_offset can never be 0) */
882 stream->i_buffer_pos = 1;
883 stream->i_buffer_last_pos = 1;
885 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
886 (httpd_callback_sys_t*)stream );
887 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
888 (httpd_callback_sys_t*)stream );
889 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
890 (httpd_callback_sys_t*)stream );
895 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
897 vlc_mutex_lock( &stream->lock );
898 free( stream->p_header );
899 stream->p_header = NULL;
901 stream->i_header = i_data;
904 stream->p_header = malloc( i_data );
905 memcpy( stream->p_header, p_data, i_data );
907 vlc_mutex_unlock( &stream->lock );
912 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
917 if( i_data < 0 || p_data == NULL )
921 vlc_mutex_lock( &stream->lock );
923 /* save this pointer (to be used by new connection) */
924 stream->i_buffer_last_pos = stream->i_buffer_pos;
926 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
932 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
934 /* Ok, we can't go past the end of our buffer */
935 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
937 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
942 stream->i_buffer_pos += i_data;
944 vlc_mutex_unlock( &stream->lock );
948 void httpd_StreamDelete( httpd_stream_t *stream )
950 httpd_UrlDelete( stream->url );
951 vlc_mutex_destroy( &stream->lock );
952 free( stream->psz_mime );
953 free( stream->p_header );
954 free( stream->p_buffer );
958 /*****************************************************************************
960 *****************************************************************************/
961 static void* httpd_HostThread( vlc_object_t * );
963 /* create a new host */
964 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
967 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
971 static const char psz_object_type[] = "http server";
973 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
975 const char *psz_cert, const char *psz_key,
976 const char *psz_ca, const char *psz_crl )
982 vlc_value_t lockval, ptrval;
985 if( psz_hostname == NULL )
988 psz_host = strdup( psz_hostname );
989 if( psz_host == NULL )
992 /* to be sure to avoid multiple creation */
993 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
994 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
995 vlc_mutex_lock( lockval.p_address );
996 httpd = libvlc_priv (p_this->p_libvlc)->p_httpd;
1000 msg_Info( p_this, "creating httpd" );
1001 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1006 vlc_mutex_unlock( lockval.p_address );
1014 ptrval.p_address = httpd;
1015 libvlc_priv (p_this->p_libvlc)->p_httpd = httpd;
1016 vlc_object_hold( httpd );
1017 vlc_object_attach( httpd, p_this->p_libvlc );
1020 /* verify if it already exist */
1021 for( i = httpd->i_host - 1; i >= 0; i-- )
1023 host = httpd->host[i];
1025 /* cannot mix TLS and non-TLS hosts */
1026 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1027 || ( host->i_port != i_port )
1028 || strcmp( host->psz_hostname, psz_hostname ) )
1034 vlc_mutex_unlock( lockval.p_address );
1040 /* determine TLS configuration */
1041 if ( psz_cert != NULL )
1043 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1044 if ( p_tls == NULL )
1046 msg_Err( p_this, "TLS initialization error" );
1050 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1052 msg_Err( p_this, "TLS CA error" );
1056 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1058 msg_Err( p_this, "TLS CRL error" );
1065 /* create the new host */
1066 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1072 vlc_object_lock( host );
1073 if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
1075 msg_Err( host, "signaling pipe error: %m" );
1076 vlc_object_unlock( host );
1079 vlc_object_unlock( host );
1081 host->httpd = httpd;
1082 vlc_mutex_init( &host->lock );
1085 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1086 if( host->fds == NULL )
1088 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1091 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1093 host->i_port = i_port;
1094 host->psz_hostname = psz_host;
1099 host->client = NULL;
1101 host->p_tls = p_tls;
1103 /* create the thread */
1104 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1105 VLC_THREAD_PRIORITY_LOW, false ) )
1107 msg_Err( p_this, "cannot spawn http host thread" );
1111 /* now add it to httpd */
1112 TAB_APPEND( httpd->i_host, httpd->host, host );
1113 vlc_mutex_unlock( lockval.p_address );
1119 if( httpd->i_host <= 0 )
1121 libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
1122 vlc_object_release( httpd );
1123 vlc_object_detach( httpd );
1124 vlc_object_release( httpd );
1126 vlc_mutex_unlock( lockval.p_address );
1130 net_ListenClose( host->fds );
1131 vlc_mutex_destroy( &host->lock );
1132 vlc_object_release( host );
1136 tls_ServerDelete( p_tls );
1142 void httpd_HostDelete( httpd_host_t *host )
1144 httpd_t *httpd = host->httpd;
1145 vlc_value_t lockval;
1148 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1149 vlc_mutex_lock( lockval.p_address );
1152 if( host->i_ref > 0 )
1155 vlc_mutex_unlock( lockval.p_address );
1156 msg_Dbg( host, "httpd_HostDelete: host still used" );
1159 TAB_REMOVE( httpd->i_host, httpd->host, host );
1161 vlc_object_kill( host );
1162 vlc_thread_join( host );
1164 msg_Dbg( host, "HTTP host removed" );
1166 for( i = 0; i < host->i_url; i++ )
1168 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1170 for( i = 0; i < host->i_client; i++ )
1172 httpd_client_t *cl = host->client[i];
1173 msg_Warn( host, "client still connected" );
1174 httpd_ClientClean( cl );
1175 TAB_REMOVE( host->i_client, host->client, cl );
1181 if( host->p_tls != NULL)
1182 tls_ServerDelete( host->p_tls );
1184 net_ListenClose( host->fds );
1185 free( host->psz_hostname );
1187 vlc_mutex_destroy( &host->lock );
1188 vlc_object_release( host );
1190 vlc_object_release( httpd );
1191 if( httpd->i_host <= 0 )
1193 msg_Dbg( httpd, "no host left, stopping httpd" );
1195 libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
1196 vlc_object_detach( httpd );
1197 vlc_object_release( httpd );
1200 vlc_mutex_unlock( lockval.p_address );
1203 /* register a new url */
1204 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1205 const char *psz_user, const char *psz_password,
1206 const vlc_acl_t *p_acl, bool b_check )
1211 assert( psz_url != NULL );
1213 vlc_mutex_lock( &host->lock );
1216 for( i = 0; i < host->i_url; i++ )
1218 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1220 msg_Warn( host->httpd,
1221 "cannot add '%s' (url already defined)", psz_url );
1222 vlc_mutex_unlock( &host->lock );
1228 url = malloc( sizeof( httpd_url_t ) );
1231 vlc_mutex_init( &url->lock );
1232 url->psz_url = strdup( psz_url );
1233 url->psz_user = strdup( psz_user ? psz_user : "" );
1234 url->psz_password = strdup( psz_password ? psz_password : "" );
1235 url->p_acl = ACL_Duplicate( host, p_acl );
1236 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1238 url->catch[i].cb = NULL;
1239 url->catch[i].p_sys = NULL;
1242 TAB_APPEND( host->i_url, host->url, url );
1243 vlc_mutex_unlock( &host->lock );
1248 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1249 const char *psz_user, const char *psz_password,
1250 const vlc_acl_t *p_acl )
1252 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1253 psz_password, p_acl, false );
1256 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1257 const char *psz_user, const char *psz_password,
1258 const vlc_acl_t *p_acl )
1260 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1261 psz_password, p_acl, true );
1264 /* register callback on a url */
1265 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1266 httpd_callback_sys_t *p_sys )
1268 vlc_mutex_lock( &url->lock );
1269 url->catch[i_msg].cb = cb;
1270 url->catch[i_msg].p_sys= p_sys;
1271 vlc_mutex_unlock( &url->lock );
1277 void httpd_UrlDelete( httpd_url_t *url )
1279 httpd_host_t *host = url->host;
1282 vlc_mutex_lock( &host->lock );
1283 TAB_REMOVE( host->i_url, host->url, url );
1285 vlc_mutex_destroy( &url->lock );
1286 free( url->psz_url );
1287 free( url->psz_user );
1288 free( url->psz_password );
1289 ACL_Destroy( url->p_acl );
1291 for( i = 0; i < host->i_client; i++ )
1293 httpd_client_t *client = host->client[i];
1295 if( client->url == url )
1297 /* TODO complete it */
1298 msg_Warn( host, "force closing connections" );
1299 httpd_ClientClean( client );
1300 TAB_REMOVE( host->i_client, host->client, client );
1306 vlc_mutex_unlock( &host->lock );
1309 static void httpd_MsgInit( httpd_message_t *msg )
1312 msg->i_type = HTTPD_MSG_NONE;
1313 msg->i_proto = HTTPD_PROTO_NONE;
1314 msg->i_version = -1; /* FIXME */
1318 msg->psz_url = NULL;
1319 msg->psz_args = NULL;
1321 msg->i_channel = -1;
1328 msg->i_body_offset = 0;
1333 static void httpd_MsgClean( httpd_message_t *msg )
1337 free( msg->psz_url );
1338 free( msg->psz_args );
1339 for( i = 0; i < msg->i_name; i++ )
1341 free( msg->name[i] );
1342 free( msg->value[i] );
1346 free( msg->p_body );
1347 httpd_MsgInit( msg );
1350 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1354 for( i = 0; i < msg->i_name; i++ )
1356 if( !strcasecmp( msg->name[i], name ))
1358 return msg->value[i];
1364 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1369 va_start( args, psz_value );
1370 if( vasprintf( &value, psz_value, args ) == -1 )
1377 name = strdup( name );
1384 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1385 TAB_APPEND( msg->i_value, msg->value, value );
1388 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1390 cl->i_state = HTTPD_CLIENT_RECEIVING;
1391 cl->i_activity_date = now;
1392 cl->i_activity_timeout = INT64_C(10000000);
1393 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1395 cl->p_buffer = malloc( cl->i_buffer_size );
1396 cl->i_mode = HTTPD_CLIENT_FILE;
1397 cl->b_read_waiting = false;
1399 httpd_MsgInit( &cl->query );
1400 httpd_MsgInit( &cl->answer );
1403 void httpd_ClientModeStream( httpd_client_t *cl )
1405 cl->i_mode = HTTPD_CLIENT_STREAM;
1408 void httpd_ClientModeBidir( httpd_client_t *cl )
1410 cl->i_mode = HTTPD_CLIENT_BIDIR;
1413 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1415 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1418 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1420 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1423 static void httpd_ClientClean( httpd_client_t *cl )
1427 if( cl->p_tls != NULL )
1428 tls_ServerSessionClose( cl->p_tls );
1429 net_Close( cl->fd );
1433 httpd_MsgClean( &cl->answer );
1434 httpd_MsgClean( &cl->query );
1436 free( cl->p_buffer );
1437 cl->p_buffer = NULL;
1440 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1442 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1444 if( !cl ) return NULL;
1451 httpd_ClientInit( cl, now );
1457 ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
1459 tls_session_t *p_tls;
1464 val = p_tls ? tls_Recv (p_tls, p, i_len)
1465 : recv (cl->fd, p, i_len, 0);
1466 while (val == -1 && errno == EINTR);
1471 ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
1473 tls_session_t *p_tls;
1478 val = p_tls ? tls_Send( p_tls, p, i_len )
1479 : send (cl->fd, p, i_len, 0);
1480 while (val == -1 && errno == EINTR);
1487 const char name[16];
1493 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1494 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1495 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1496 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1497 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1498 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1499 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1500 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1501 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1502 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1503 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1507 static void httpd_ClientRecv( httpd_client_t *cl )
1511 /* ignore leading whites */
1512 if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
1513 ( cl->i_buffer == 0 ) )
1517 i_len = httpd_NetRecv( cl, &c, 1 );
1519 if( ( i_len > 0 ) && ( strchr( "\r\n\t ", c ) == NULL ) )
1521 cl->p_buffer[0] = c;
1526 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1528 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1529 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1533 cl->i_buffer += i_len;
1536 if( ( cl->i_buffer >= 4 ) && ( cl->p_buffer[0] == '$' ) )
1538 /* Interleaved RTP over RTSP */
1539 cl->query.i_proto = HTTPD_PROTO_RTSP;
1540 cl->query.i_type = HTTPD_MSG_CHANNEL;
1541 cl->query.i_channel = cl->p_buffer[1];
1542 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1543 cl->query.p_body = malloc( cl->query.i_body );
1545 memcpy( cl->query.p_body, cl->p_buffer + 4, cl->i_buffer );
1548 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1549 * this is the maximum we can ask at this point. */
1550 if( cl->i_buffer >= 7 )
1552 if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
1554 cl->query.i_proto = HTTPD_PROTO_HTTP;
1555 cl->query.i_type = HTTPD_MSG_ANSWER;
1557 else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
1559 cl->query.i_proto = HTTPD_PROTO_RTSP;
1560 cl->query.i_type = HTTPD_MSG_ANSWER;
1564 /* We need the full request line to determine the protocol. */
1565 cl->query.i_proto = HTTPD_PROTO_HTTP0;
1566 cl->query.i_type = HTTPD_MSG_NONE;
1570 else if( cl->query.i_body > 0 )
1572 /* we are reading the body of a request or a channel */
1573 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1574 cl->query.i_body - cl->i_buffer );
1577 cl->i_buffer += i_len;
1579 if( cl->i_buffer >= cl->query.i_body )
1581 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1586 /* we are reading a header -> char by char */
1589 if( cl->i_buffer == cl->i_buffer_size )
1591 uint8_t *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
1592 if( newbuf == NULL )
1598 cl->p_buffer = newbuf;
1599 cl->i_buffer_size += 1024;
1602 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1609 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP0 )
1610 && ( cl->p_buffer[cl->i_buffer - 1] == '\n' ) )
1612 /* Request line is now complete */
1613 const char *p = memchr( cl->p_buffer, ' ', cl->i_buffer );
1616 assert( cl->query.i_type == HTTPD_MSG_NONE );
1618 if( p == NULL ) /* no URI: evil guy */
1620 i_len = 0; /* drop connection */
1625 p++; /* skips extra spaces */
1628 p = memchr( p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p );
1629 if( p == NULL ) /* no explicit protocol: HTTP/0.9 */
1631 i_len = 0; /* not supported currently -> drop */
1636 p++; /* skips extra spaces ever again */
1639 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
1640 if( len < 7 ) /* foreign protocol */
1641 i_len = 0; /* I don't understand -> drop */
1643 if( memcmp( p, "HTTP/1.", 7 ) == 0 )
1645 cl->query.i_proto = HTTPD_PROTO_HTTP;
1646 cl->query.i_version = atoi( p + 7 );
1649 if( memcmp( p, "RTSP/1.", 7 ) == 0 )
1651 cl->query.i_proto = HTTPD_PROTO_RTSP;
1652 cl->query.i_version = atoi( p + 7 );
1655 if( memcmp( p, "HTTP/", 5 ) == 0 )
1657 const uint8_t sorry[] =
1658 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1659 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1660 i_len = 0; /* drop */
1663 if( memcmp( p, "RTSP/", 5 ) == 0 )
1665 const uint8_t sorry[] =
1666 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1667 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1668 i_len = 0; /* drop */
1670 else /* yet another foreign protocol */
1677 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1678 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1682 /* we have finished the header so parse it and set i_body */
1683 cl->p_buffer[cl->i_buffer] = '\0';
1685 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1688 * assume strlen( "HTTP/1.x" ) = 8
1690 cl->query.i_status =
1691 strtol( (char *)&cl->p_buffer[8],
1701 cl->query.i_type = HTTPD_MSG_NONE;
1703 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1705 for( i = 0; msg_type[i].name[0]; i++ )
1707 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1708 strlen( msg_type[i].name ) ) )
1710 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1711 cl->query.i_type = msg_type[i].i_type;
1712 if( cl->query.i_proto != msg_type[i].i_proto )
1715 cl->query.i_proto = HTTPD_PROTO_NONE;
1716 cl->query.i_type = HTTPD_MSG_NONE;
1723 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1725 cl->query.i_proto = HTTPD_PROTO_HTTP;
1727 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1729 cl->query.i_proto = HTTPD_PROTO_RTSP;
1741 p2 = strchr( p, ' ' );
1746 if( !strncasecmp( p, "rtsp:", 5 ) )
1748 /* for rtsp url, you have rtsp://localhost:port/path */
1750 while( *p == '/' ) p++;
1751 while( *p && *p != '/' ) p++;
1753 cl->query.psz_url = strdup( p );
1754 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1757 cl->query.psz_args = (uint8_t *)strdup( p3 );
1764 p = strchr( p, '\n' );
1768 while( *p == '\n' || *p == '\r' )
1772 while( p && *p != '\0' )
1775 char *eol = p = strchr( p, '\n' );
1778 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1783 if( ( colon = strchr( line, ':' ) ) )
1789 while( *colon == ' ' )
1793 name = strdup( line );
1794 value = strdup( colon );
1796 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1797 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1799 if( !strcasecmp( name, "Content-Length" ) )
1801 cl->query.i_body = atol( value );
1808 while( *p == '\n' || *p == '\r' )
1815 if( cl->query.i_body > 0 )
1817 /* TODO Mhh, handle the case client will only send a
1818 * request and close the connection
1819 * to mark and of body (probably only RTSP) */
1820 cl->query.p_body = malloc( cl->query.i_body );
1825 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1831 /* check if the client is to be set to dead */
1832 #if defined( WIN32 ) || defined( UNDER_CE )
1833 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1835 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1838 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1840 /* connection closed -> end of data */
1841 if( cl->query.i_body > 0 )
1843 cl->query.i_body = cl->i_buffer;
1845 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1849 cl->i_state = HTTPD_CLIENT_DEAD;
1853 /* XXX: for QT I have to disable timeout. Try to find why */
1854 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1855 cl->i_activity_timeout = 0;
1857 #if 0 /* Debugging only */
1858 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1862 fprintf( stderr, "received new request\n" );
1863 fprintf( stderr, " - proto=%s\n",
1864 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1865 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1866 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1867 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1869 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1870 cl->query.psz_status );
1872 else if( cl->query.i_type != HTTPD_MSG_NONE )
1874 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1876 for( i = 0; i < cl->query.i_name; i++ )
1878 fprintf( stderr, " - option name='%s' value='%s'\n",
1879 cl->query.name[i], cl->query.value[i] );
1885 static void httpd_ClientSend( httpd_client_t *cl )
1890 if( cl->i_buffer < 0 )
1892 /* We need to create the header */
1895 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1897 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1898 for( i = 0; i < cl->answer.i_name; i++ )
1900 i_size += strlen( cl->answer.name[i] ) + 2 +
1901 strlen( cl->answer.value[i] ) + 2;
1904 if( cl->i_buffer_size < i_size )
1906 cl->i_buffer_size = i_size;
1907 free( cl->p_buffer );
1908 cl->p_buffer = malloc( i_size );
1910 p = (char *)cl->p_buffer;
1912 p += sprintf( p, "%s.%u %d %s\r\n",
1913 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1914 cl->answer.i_version,
1915 cl->answer.i_status, psz_status );
1916 for( i = 0; i < cl->answer.i_name; i++ )
1918 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1919 cl->answer.value[i] );
1921 p += sprintf( p, "\r\n" );
1924 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1926 /*fprintf( stderr, "sending answer\n" );
1927 fprintf( stderr, "%s", cl->p_buffer );*/
1930 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1931 cl->i_buffer_size - cl->i_buffer );
1934 cl->i_buffer += i_len;
1936 if( cl->i_buffer >= cl->i_buffer_size )
1938 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1939 !cl->b_read_waiting )
1941 /* catch more body data */
1942 int i_msg = cl->query.i_type;
1943 int64_t i_offset = cl->answer.i_body_offset;
1945 httpd_MsgClean( &cl->answer );
1946 cl->answer.i_body_offset = i_offset;
1948 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1949 &cl->answer, &cl->query );
1952 if( cl->answer.i_body > 0 )
1954 /* send the body data */
1955 free( cl->p_buffer );
1956 cl->p_buffer = cl->answer.p_body;
1957 cl->i_buffer_size = cl->answer.i_body;
1960 cl->answer.i_body = 0;
1961 cl->answer.p_body = NULL;
1966 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1972 #if defined( WIN32 ) || defined( UNDER_CE )
1973 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1975 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1979 cl->i_state = HTTPD_CLIENT_DEAD;
1984 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1986 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1989 cl->i_state = HTTPD_CLIENT_RECEIVING;
1993 cl->i_state = HTTPD_CLIENT_DEAD;
1998 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2002 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2004 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2007 cl->i_state = HTTPD_CLIENT_RECEIVING;
2011 cl->i_state = HTTPD_CLIENT_DEAD;
2016 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2021 static void* httpd_HostThread( vlc_object_t *p_this )
2023 httpd_host_t *host = (httpd_host_t *)p_this;
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 );
2029 int canc = vlc_savecancel ();
2032 vlc_object_lock( host );
2033 evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
2034 b_die = !vlc_object_alive( host );
2035 vlc_object_unlock( host );
2039 if( host->i_url <= 0 )
2041 /* 0.2s (FIXME: use a condition variable) */
2046 /* prepare a new TLS session */
2047 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2048 p_tls = tls_ServerSessionPrepare( host->p_tls );
2050 struct pollfd ufd[host->nfd + host->i_client + 1];
2052 for( nfd = 0; nfd < host->nfd; nfd++ )
2054 ufd[nfd].fd = host->fds[nfd];
2055 ufd[nfd].events = POLLIN;
2056 ufd[nfd].revents = 0;
2059 /* add all socket that should be read/write and close dead connection */
2060 vlc_mutex_lock( &host->lock );
2061 mtime_t now = mdate();
2062 bool b_low_delay = false;
2064 for(int i_client = 0; i_client < host->i_client; i_client++ )
2066 httpd_client_t *cl = host->client[i_client];
2067 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2068 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2069 ( cl->i_activity_timeout > 0 &&
2070 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2072 httpd_ClientClean( cl );
2073 stats_UpdateInteger( host, p_active_counter, -1, NULL );
2074 TAB_REMOVE( host->i_client, host->client, cl );
2080 struct pollfd *pufd = ufd + nfd;
2081 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2084 pufd->events = pufd->revents = 0;
2086 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2087 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2089 pufd->events = POLLIN;
2091 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2092 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2094 pufd->events = POLLOUT;
2096 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2098 httpd_message_t *answer = &cl->answer;
2099 httpd_message_t *query = &cl->query;
2100 int i_msg = query->i_type;
2102 httpd_MsgInit( answer );
2104 /* Handle what we received */
2105 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2106 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2108 /* we can only receive request from client when not
2111 cl->i_state = HTTPD_CLIENT_DEAD;
2113 else if( i_msg == HTTPD_MSG_ANSWER )
2115 /* We are in BIDIR mode, trigger the callback and then
2116 * check for new data */
2117 if( cl->url && cl->url->catch[i_msg].cb )
2119 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2122 cl->i_state = HTTPD_CLIENT_WAITING;
2124 else if( i_msg == HTTPD_MSG_CHANNEL )
2126 /* We are in BIDIR mode, trigger the callback and then
2127 * check for new data */
2128 if( cl->url && cl->url->catch[i_msg].cb )
2130 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2133 cl->i_state = HTTPD_CLIENT_WAITING;
2135 else if( i_msg == HTTPD_MSG_OPTIONS )
2138 answer->i_type = HTTPD_MSG_ANSWER;
2139 answer->i_proto = query->i_proto;
2140 answer->i_status = 200;
2142 answer->p_body = NULL;
2144 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
2145 httpd_MsgAdd( answer, "Content-Length", "0" );
2147 switch( query->i_proto )
2149 case HTTPD_PROTO_HTTP:
2150 answer->i_version = 1;
2151 httpd_MsgAdd( answer, "Allow",
2152 "GET,HEAD,POST,OPTIONS" );
2155 case HTTPD_PROTO_RTSP:
2158 answer->i_version = 0;
2160 p = httpd_MsgGet( query, "Cseq" );
2162 httpd_MsgAdd( answer, "Cseq", "%s", p );
2163 p = httpd_MsgGet( query, "Timestamp" );
2165 httpd_MsgAdd( answer, "Timestamp", "%s", p );
2167 p = httpd_MsgGet( query, "Require" );
2170 answer->i_status = 551;
2171 httpd_MsgAdd( query, "Unsupported", "%s", p );
2174 httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
2175 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
2180 cl->i_buffer = -1; /* Force the creation of the answer in
2181 * httpd_ClientSend */
2182 cl->i_state = HTTPD_CLIENT_SENDING;
2184 else if( i_msg == HTTPD_MSG_NONE )
2186 if( query->i_proto == HTTPD_PROTO_NONE )
2189 cl->i_state = HTTPD_CLIENT_DEAD;
2196 answer->i_proto = query->i_proto ;
2197 answer->i_type = HTTPD_MSG_ANSWER;
2198 answer->i_version= 0;
2199 answer->i_status = 501;
2201 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2202 answer->p_body = (uint8_t *)p;
2203 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2205 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2206 cl->i_state = HTTPD_CLIENT_SENDING;
2211 bool b_auth_failed = false;
2212 bool b_hosts_failed = false;
2214 /* Search the url and trigger callbacks */
2215 for(int i = 0; i < host->i_url; i++ )
2217 httpd_url_t *url = host->url[i];
2219 if( !strcmp( url->psz_url, query->psz_url ) )
2221 if( url->catch[i_msg].cb )
2223 if( answer && ( url->p_acl != NULL ) )
2225 char ip[NI_MAXNUMERICHOST];
2227 if( ( httpd_ClientIP( cl, ip ) == NULL )
2228 || ACL_Check( url->p_acl, ip ) )
2230 b_hosts_failed = true;
2235 if( answer && ( *url->psz_user || *url->psz_password ) )
2237 /* create the headers */
2238 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2239 char *user = NULL, *pass = NULL;
2242 && !strncasecmp( b64, "BASIC", 5 ) )
2245 while( *b64 == ' ' )
2248 user = vlc_b64_decode( b64 );
2251 pass = strchr (user, ':');
2257 if ((user == NULL) || (pass == NULL)
2258 || strcmp (user, url->psz_user)
2259 || strcmp (pass, url->psz_password))
2261 httpd_MsgAdd( answer,
2263 "Basic realm=\"%s\"",
2265 /* We fail for all url */
2266 b_auth_failed = true;
2274 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2276 if( answer->i_proto == HTTPD_PROTO_NONE )
2278 /* Raw answer from a CGI */
2279 cl->i_buffer = cl->i_buffer_size;
2284 /* only one url can answer */
2286 if( cl->url == NULL )
2299 answer->i_proto = query->i_proto;
2300 answer->i_type = HTTPD_MSG_ANSWER;
2301 answer->i_version= 0;
2303 if( b_hosts_failed )
2305 answer->i_status = 403;
2307 else if( b_auth_failed )
2309 answer->i_status = 401;
2313 /* no url registered */
2314 answer->i_status = 404;
2317 answer->i_body = httpd_HtmlError (&p,
2320 answer->p_body = (uint8_t *)p;
2322 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2323 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2324 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2327 cl->i_state = HTTPD_CLIENT_SENDING;
2330 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2332 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2334 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2335 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2336 bool b_connection = false;
2337 bool b_keepalive = false;
2338 bool b_query = false;
2341 if( psz_connection )
2343 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2344 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2349 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2352 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2353 ( ( cl->query.i_version == 0 && b_keepalive ) ||
2354 ( cl->query.i_version == 1 && !b_connection ) ) ) ||
2355 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2356 !b_query && !b_connection ) )
2358 httpd_MsgClean( &cl->query );
2359 httpd_MsgInit( &cl->query );
2362 cl->i_buffer_size = 1000;
2363 free( cl->p_buffer );
2364 cl->p_buffer = malloc( cl->i_buffer_size );
2365 cl->i_state = HTTPD_CLIENT_RECEIVING;
2369 cl->i_state = HTTPD_CLIENT_DEAD;
2371 httpd_MsgClean( &cl->answer );
2373 else if( cl->b_read_waiting )
2375 /* we have a message waiting for us to read it */
2376 httpd_MsgClean( &cl->answer );
2377 httpd_MsgClean( &cl->query );
2380 cl->i_buffer_size = 1000;
2381 free( cl->p_buffer );
2382 cl->p_buffer = malloc( cl->i_buffer_size );
2383 cl->i_state = HTTPD_CLIENT_RECEIVING;
2384 cl->b_read_waiting = false;
2388 int64_t i_offset = cl->answer.i_body_offset;
2389 httpd_MsgClean( &cl->answer );
2391 cl->answer.i_body_offset = i_offset;
2392 free( cl->p_buffer );
2393 cl->p_buffer = NULL;
2395 cl->i_buffer_size = 0;
2397 cl->i_state = HTTPD_CLIENT_WAITING;
2400 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2402 int64_t i_offset = cl->answer.i_body_offset;
2403 int i_msg = cl->query.i_type;
2405 httpd_MsgInit( &cl->answer );
2406 cl->answer.i_body_offset = i_offset;
2408 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2409 &cl->answer, &cl->query );
2410 if( cl->answer.i_type != HTTPD_MSG_NONE )
2412 /* we have new data, so re-enter send mode */
2414 cl->p_buffer = cl->answer.p_body;
2415 cl->i_buffer_size = cl->answer.i_body;
2416 cl->answer.p_body = NULL;
2417 cl->answer.i_body = 0;
2418 cl->i_state = HTTPD_CLIENT_SENDING;
2422 /* Special for BIDIR mode we also check reading */
2423 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2424 cl->i_state == HTTPD_CLIENT_SENDING )
2426 pufd->events |= POLLIN;
2429 if (pufd->events != 0)
2434 vlc_mutex_unlock( &host->lock );
2437 ufd[nfd].events = POLLIN;
2438 ufd[nfd].revents = 0;
2441 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2442 switch( poll( ufd, nfd, b_low_delay ? 20 : -1) )
2447 /* Kernel on low memory or a bug: pace */
2448 msg_Err( host, "polling error: %m" );
2455 vlc_object_lock( host );
2456 if( ufd[nfd - 1].revents )
2458 b_die = !vlc_object_alive( host );
2460 vlc_object_wait( host );
2462 vlc_object_unlock( host );
2464 /* Handle client sockets */
2465 vlc_mutex_lock( &host->lock );
2467 for( int i_client = 0; i_client < host->i_client; i_client++ )
2469 httpd_client_t *cl = host->client[i_client];
2470 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2472 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2474 if( cl->fd != pufd->fd )
2475 continue; // we were not waiting for this client
2476 if( pufd->revents == 0 )
2477 continue; // no event received
2479 cl->i_activity_date = now;
2481 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2483 httpd_ClientRecv( cl );
2485 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2487 httpd_ClientSend( cl );
2489 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2491 httpd_ClientTlsHsIn( cl );
2493 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2495 httpd_ClientTlsHsOut( cl );
2498 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2499 cl->i_state == HTTPD_CLIENT_SENDING &&
2500 (pufd->revents & POLLIN) )
2502 cl->b_read_waiting = true;
2505 vlc_mutex_unlock( &host->lock );
2507 /* Handle server sockets (accept new connections) */
2508 for( nfd = 0; nfd < host->nfd; nfd++ )
2512 int fd = ufd[nfd].fd;
2514 assert (fd == host->fds[nfd]);
2516 if( ufd[nfd].revents == 0 )
2520 fd = accept (fd, NULL, NULL);
2524 net_SetupSocket (fd);
2527 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2530 msg_Err( host, "Rejecting TLS connection" );
2536 case 1: /* missing input - most likely */
2537 i_state = HTTPD_CLIENT_TLS_HS_IN;
2540 case 2: /* missing output */
2541 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2545 if( (p_tls == NULL) != (host->p_tls == NULL) )
2546 break; // wasted TLS session, cannot accept() anymore
2549 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2550 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2551 cl = httpd_ClientNew( fd, p_tls, now );
2553 vlc_mutex_lock( &host->lock );
2554 TAB_APPEND( host->i_client, host->client, cl );
2555 vlc_mutex_unlock( &host->lock );
2557 cl->i_state = i_state; // override state for TLS
2559 if (host->p_tls != NULL)
2560 break; // cannot accept further without new TLS session
2566 tls_ServerSessionClose( p_tls );
2567 if( p_total_counter )
2568 stats_CounterClean( p_total_counter );
2569 if( p_active_counter )
2570 stats_CounterClean( p_active_counter );
2571 vlc_restorecancel (canc);