1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
30 #include "vlc_httpd.h"
45 #if defined( UNDER_CE )
47 #elif defined( WIN32 )
48 # include <winsock2.h>
49 # include <ws2tcpip.h>
51 # include <netdb.h> /* hostent ... */
52 # include <sys/socket.h>
53 /* FIXME: should not be needed */
54 # include <netinet/in.h>
55 # ifdef HAVE_ARPA_INET_H
56 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
61 typedef struct httpd_t httpd_t;
63 typedef struct httpd_host_t httpd_host_t;
64 typedef struct httpd_url_t httpd_url_t;
65 typedef struct httpd_client_t httpd_client_t;
74 /* channel communication */
90 /* just to track the count of MSG */
103 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
115 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
117 /* for rtp over rtsp */
127 int64_t i_body_offset;
133 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
134 /* answer could be null, int this case no answer is requested */
135 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
138 /* create a new host */
139 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
141 void httpd_HostDelete( httpd_host_t * );
143 /* register a new url */
144 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
145 /* register callback on a url */
146 int httpd_UrlCatch( httpd_url_t *, int i_msg,
147 httpd_callback_t, httpd_callback_sys_t * );
149 void httpd_UrlDelete( httpd_url_t * );
152 void httpd_ClientModeStream( httpd_client_t *cl );
153 void httpd_ClientModeBidir( httpd_client_t *cl );
154 static void httpd_ClientClean( httpd_client_t *cl );
157 typedef struct httpd_file_t httpd_file_t;
158 typedef struct httpd_file_sys_t httpd_file_sys_t;
159 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
160 httpd_file_t *httpd_FileNew( httpd_host_t *,
161 char *psz_url, char *psz_mime,
162 char *psz_user, char *psz_password,
163 httpd_file_callback_t pf_fill,
164 httpd_file_sys_t *p_sys );
165 void httpd_FileDelete( httpd_file_t * );
167 typedef struct httpd_redirect_t httpd_redirect_t;
168 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
169 void httpd_RedirectDelete( httpd_redirect_t * );
172 typedef struct httpd_stream_t httpd_stream_t;
173 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
174 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
175 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
176 void httpd_StreamDelete( httpd_stream_t * );
179 /* Msg functions facilities */
180 void httpd_MsgInit( httpd_message_t * );
181 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
182 /* return "" if not found. The string is not allocated */
183 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
184 void httpd_MsgClean( httpd_message_t * );
192 /* vlc_mutex_t lock; */
198 static void httpd_ClientClean( httpd_client_t *cl );
200 /* each host run in his own thread */
210 /* address/port and socket for listening at connections */
217 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
218 * This will slow down the url research but make my live easier
219 * All url will have their cb trigger, but only the first one can answer
225 httpd_client_t **client;
244 httpd_callback_sys_t *p_sys;
245 } catch[HTTPD_MSG_MAX];
251 HTTPD_CLIENT_RECEIVING,
252 HTTPD_CLIENT_RECEIVE_DONE,
254 HTTPD_CLIENT_SENDING,
255 HTTPD_CLIENT_SEND_DONE,
257 HTTPD_CLIENT_WAITING,
261 HTTPD_CLIENT_TLS_HS_IN,
262 HTTPD_CLIENT_TLS_HS_OUT
267 HTTPD_CLIENT_FILE, /* default */
268 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
269 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
272 struct httpd_client_t
278 struct sockaddr_storage sock;
284 int b_read_waiting; /* stop as soon as possible sending */
286 mtime_t i_activity_date;
287 mtime_t i_activity_timeout;
289 /* buffer for reading header */
295 httpd_message_t query; /* client -> httpd */
296 httpd_message_t answer; /* httpd -> client */
299 tls_session_t *p_tls;
303 /*****************************************************************************
305 *****************************************************************************/
306 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
307 static void b64_decode( char *dest, char *src )
312 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
313 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
314 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
315 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
316 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
317 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
318 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
319 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
320 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
321 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
322 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
323 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
324 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
325 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
326 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
327 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
330 for( i_level = 0; *src != '\0'; src++ )
334 c = b64[(unsigned int)*src];
346 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
350 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
354 *dest++ = ( ( last &0x03 ) << 6 ) | c;
369 { ".htm", "text/html" },
370 { ".html", "text/html" },
371 { ".txt", "text/plain" },
372 { ".xml", "text/xml" },
373 { ".dtd", "text/dtd" },
375 { ".css", "text/css" },
378 { ".gif", "image/gif" },
379 { ".jpe", "image/jpeg" },
380 { ".jpg", "image/jpeg" },
381 { ".jpeg", "image/jpeg" },
382 { ".png", "image/png" },
383 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
386 { ".avi", "video/avi" },
387 { ".asf", "video/x-ms-asf" },
388 { ".m1a", "audio/mpeg" },
389 { ".m2a", "audio/mpeg" },
390 { ".m1v", "video/mpeg" },
391 { ".m2v", "video/mpeg" },
392 { ".mp2", "audio/mpeg" },
393 { ".mp3", "audio/mpeg" },
394 { ".mpa", "audio/mpeg" },
395 { ".mpg", "video/mpeg" },
396 { ".mpeg", "video/mpeg" },
397 { ".mpe", "video/mpeg" },
398 { ".mov", "video/quicktime" },
399 { ".moov", "video/quicktime" },
400 { ".ogg", "application/ogg" },
401 { ".ogm", "application/ogg" },
402 { ".wav", "audio/wav" },
403 { ".wma", "audio/x-ms-wma" },
404 { ".wmv", "video/x-ms-wmv" },
410 static char *httpd_MimeFromUrl( char *psz_url )
415 psz_ext = strrchr( psz_url, '.' );
420 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
422 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
424 return http_mime[i].psz_mime;
428 return "application/octet-stream";
431 /*****************************************************************************
432 * High Level Funtions: httpd_file_t
433 *****************************************************************************/
441 httpd_file_callback_t pf_fill;
442 httpd_file_sys_t *p_sys;
447 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
449 httpd_file_t *file = (httpd_file_t*)p_sys;
451 if( answer == NULL || query == NULL )
455 answer->i_proto = HTTPD_PROTO_HTTP;
456 answer->i_version= query->i_version;
457 answer->i_type = HTTPD_MSG_ANSWER;
459 answer->i_status = 200;
460 answer->psz_status = strdup( "OK" );
462 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
463 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
465 if( query->i_type != HTTPD_MSG_HEAD )
467 char *psz_args = query->psz_args;
468 if( query->i_type == HTTPD_MSG_POST )
471 psz_args = query->p_body;
473 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
476 /* We respect client request */
477 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
479 httpd_MsgAdd( answer, "Connection",
480 httpd_MsgGet( &cl->query, "Connection" ) );
483 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
489 httpd_file_t *httpd_FileNew( httpd_host_t *host,
490 char *psz_url, char *psz_mime,
491 char *psz_user, char *psz_password,
492 httpd_file_callback_t pf_fill,
493 httpd_file_sys_t *p_sys )
495 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
497 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
498 psz_password ) ) == NULL )
504 file->psz_url = strdup( psz_url );
505 if( psz_mime && *psz_mime )
507 file->psz_mime = strdup( psz_mime );
511 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
514 file->pf_fill = pf_fill;
517 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
518 (httpd_callback_sys_t*)file );
519 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
520 (httpd_callback_sys_t*)file );
521 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
522 (httpd_callback_sys_t*)file );
527 void httpd_FileDelete( httpd_file_t *file )
529 httpd_UrlDelete( file->url );
531 free( file->psz_url );
532 free( file->psz_mime );
537 /*****************************************************************************
538 * High Level Funtions: httpd_redirect_t
539 *****************************************************************************/
540 struct httpd_redirect_t
546 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
547 httpd_client_t *cl, httpd_message_t *answer,
548 httpd_message_t *query )
550 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
553 if( answer == NULL || query == NULL )
557 answer->i_proto = query->i_proto;
558 answer->i_version= query->i_version;
559 answer->i_type = HTTPD_MSG_ANSWER;
560 answer->i_status = 301;
561 answer->psz_status = strdup( "Moved Permanently" );
563 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
564 p += sprintf( p, "<html>\n" );
565 p += sprintf( p, "<head>\n" );
566 p += sprintf( p, "<title>Redirection</title>\n" );
567 p += sprintf( p, "</head>\n" );
568 p += sprintf( p, "<body>\n" );
569 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
570 p += sprintf( p, "<hr />\n" );
571 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
572 p += sprintf( p, "</body>\n" );
573 p += sprintf( p, "</html>\n" );
574 answer->i_body = p - answer->p_body;
576 /* XXX check if it's ok or we need to set an absolute url */
577 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
579 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
584 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
587 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
589 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) )
594 rdir->psz_dst = strdup( psz_url_dst );
596 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
597 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
598 (httpd_callback_sys_t*)rdir );
599 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
600 (httpd_callback_sys_t*)rdir );
601 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
602 (httpd_callback_sys_t*)rdir );
603 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
604 (httpd_callback_sys_t*)rdir );
608 void httpd_RedirectDelete( httpd_redirect_t *rdir )
610 httpd_UrlDelete( rdir->url );
611 free( rdir->psz_dst );
615 /*****************************************************************************
616 * High Level Funtions: httpd_stream_t
617 *****************************************************************************/
618 struct httpd_stream_t
625 /* Header to send as first packet */
629 /* circular buffer */
630 int i_buffer_size; /* buffer size, can't be reallocated smaller */
631 uint8_t *p_buffer; /* buffer */
632 int64_t i_buffer_pos; /* absolute position from begining */
633 int64_t i_buffer_last_pos; /* a new connection will start with that */
636 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
637 httpd_client_t *cl, httpd_message_t *answer,
638 httpd_message_t *query )
640 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
642 if( answer == NULL || query == NULL || cl == NULL )
646 if( answer->i_body_offset > 0 )
652 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
653 answer->i_body_offset );
656 if( answer->i_body_offset >= stream->i_buffer_pos )
658 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
659 return VLC_EGENERIC; /* wait, no data available */
661 if( answer->i_body_offset + stream->i_buffer_size <
662 stream->i_buffer_pos )
664 /* this client isn't fast enough */
665 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
666 answer->i_body_offset, stream->i_buffer_last_pos );
667 answer->i_body_offset = stream->i_buffer_last_pos;
670 i_pos = answer->i_body_offset % stream->i_buffer_size;
671 i_write = stream->i_buffer_pos - answer->i_body_offset;
672 if( i_write > 10000 )
676 else if( i_write <= 0 )
678 return VLC_EGENERIC; /* wait, no data available */
681 /* Don't go past the end of the circular buffer */
682 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
684 /* using HTTPD_MSG_ANSWER -> data available */
685 answer->i_proto = HTTPD_PROTO_HTTP;
686 answer->i_version= 0;
687 answer->i_type = HTTPD_MSG_ANSWER;
689 answer->i_body = i_write;
690 answer->p_body = malloc( i_write );
691 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
693 answer->i_body_offset += i_write;
699 answer->i_proto = HTTPD_PROTO_HTTP;
700 answer->i_version= 0;
701 answer->i_type = HTTPD_MSG_ANSWER;
703 answer->i_status = 200;
704 answer->psz_status = strdup( "OK" );
706 if( query->i_type != HTTPD_MSG_HEAD )
708 httpd_ClientModeStream( cl );
709 vlc_mutex_lock( &stream->lock );
710 /* Send the header */
711 if( stream->i_header > 0 )
713 answer->i_body = stream->i_header;
714 answer->p_body = malloc( stream->i_header );
715 memcpy( answer->p_body, stream->p_header, stream->i_header );
717 answer->i_body_offset = stream->i_buffer_last_pos;
718 vlc_mutex_unlock( &stream->lock );
722 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
723 answer->i_body_offset = 0;
726 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
728 vlc_bool_t b_xplaystream = VLC_FALSE;
731 httpd_MsgAdd( answer, "Content-type", "%s",
732 "application/octet-stream" );
733 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
734 httpd_MsgAdd( answer, "Pragma", "no-cache" );
735 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
736 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
738 /* Check if there is a xPlayStrm=1 */
739 for( i = 0; i < query->i_name; i++ )
741 if( !strcasecmp( query->name[i], "Pragma" ) &&
742 strstr( query->value[i], "xPlayStrm=1" ) )
744 b_xplaystream = VLC_TRUE;
750 answer->i_body_offset = 0;
755 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
757 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
762 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
763 char *psz_url, char *psz_mime,
764 char *psz_user, char *psz_password )
766 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
768 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
769 psz_password ) ) == NULL )
774 vlc_mutex_init( host, &stream->lock );
775 if( psz_mime && *psz_mime )
777 stream->psz_mime = strdup( psz_mime );
781 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
783 stream->i_header = 0;
784 stream->p_header = NULL;
785 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
786 stream->p_buffer = malloc( stream->i_buffer_size );
787 /* We set to 1 to make life simpler
788 * (this way i_body_offset can never be 0) */
789 stream->i_buffer_pos = 1;
790 stream->i_buffer_last_pos = 1;
792 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
793 (httpd_callback_sys_t*)stream );
794 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
795 (httpd_callback_sys_t*)stream );
796 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
797 (httpd_callback_sys_t*)stream );
802 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
804 vlc_mutex_lock( &stream->lock );
805 if( stream->p_header )
807 free( stream->p_header );
808 stream->p_header = NULL;
810 stream->i_header = i_data;
813 stream->p_header = malloc( i_data );
814 memcpy( stream->p_header, p_data, i_data );
816 vlc_mutex_unlock( &stream->lock );
821 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
826 if( i_data < 0 || p_data == NULL )
830 vlc_mutex_lock( &stream->lock );
832 /* save this pointer (to be used by new connection) */
833 stream->i_buffer_last_pos = stream->i_buffer_pos;
835 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
841 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
843 /* Ok, we can't go past the end of our buffer */
844 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
846 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
851 stream->i_buffer_pos += i_data;
853 vlc_mutex_unlock( &stream->lock );
857 void httpd_StreamDelete( httpd_stream_t *stream )
859 httpd_UrlDelete( stream->url );
860 vlc_mutex_destroy( &stream->lock );
861 if( stream->psz_mime ) free( stream->psz_mime );
862 if( stream->p_header ) free( stream->p_header );
863 if( stream->p_buffer ) free( stream->p_buffer );
868 /*****************************************************************************
870 *****************************************************************************/
871 static void httpd_HostThread( httpd_host_t * );
873 /* create a new host */
874 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
877 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
881 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
883 const char *psz_cert, const char *psz_key,
884 const char *psz_ca, const char *psz_crl )
893 psz_host = strdup( psz_hostname );
894 if( psz_host == NULL )
896 msg_Err( p_this, "memory error" );
900 /* to be sure to avoid multiple creation */
901 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
902 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
903 vlc_mutex_lock( lockval.p_address );
905 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
907 msg_Info( p_this, "creating httpd" );
908 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
910 vlc_mutex_unlock( lockval.p_address );
918 vlc_object_yield( httpd );
919 vlc_object_attach( httpd, p_this->p_vlc );
922 /* verify if it already exist */
923 for( i = httpd->i_host - 1; i >= 0; i-- )
925 host = httpd->host[i];
927 /* cannot mix TLS and non-TLS hosts */
928 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
929 || ( host->i_port != i_port )
930 || strcmp( host->psz_hostname, psz_hostname ) )
936 vlc_mutex_unlock( lockval.p_address );
942 /* determine TLS configuration */
943 if ( psz_cert != NULL )
945 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
948 msg_Err( p_this, "TLS initialization error" );
952 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
954 msg_Err( p_this, "TLS CA error" );
958 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
960 msg_Err( p_this, "TLS CRL error" );
967 /* create the new host */
968 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
970 vlc_mutex_init( httpd, &host->lock );
973 host->fd = net_ListenTCP( p_this, psz_host, i_port );
974 if( host->fd == NULL )
976 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
980 host->i_port = i_port;
981 host->psz_hostname = psz_host;
990 /* create the thread */
991 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
992 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
994 msg_Err( p_this, "cannot spawn http host thread" );
998 /* now add it to httpd */
999 TAB_APPEND( httpd->i_host, httpd->host, host );
1000 vlc_mutex_unlock( lockval.p_address );
1006 if( httpd->i_host <= 0 )
1008 vlc_object_release( httpd );
1009 vlc_object_detach( httpd );
1010 vlc_object_destroy( httpd );
1012 vlc_mutex_unlock( lockval.p_address );
1016 net_ListenClose( host->fd );
1017 vlc_mutex_destroy( &host->lock );
1018 vlc_object_destroy( host );
1022 tls_ServerDelete( p_tls );
1028 void httpd_HostDelete( httpd_host_t *host )
1030 httpd_t *httpd = host->httpd;
1031 vlc_value_t lockval;
1034 msg_Dbg( host, "httpd_HostDelete" );
1036 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1037 vlc_mutex_lock( lockval.p_address );
1040 if( host->i_ref > 0 )
1043 vlc_mutex_unlock( lockval.p_address );
1044 msg_Dbg( host, "httpd_HostDelete: host still used" );
1047 TAB_REMOVE( httpd->i_host, httpd->host, host );
1049 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1052 vlc_thread_join( host );
1054 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1056 for( i = 0; i < host->i_url; i++ )
1058 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1060 for( i = 0; i < host->i_client; i++ )
1062 httpd_client_t *cl = host->client[i];
1063 msg_Warn( host, "client still connected" );
1064 httpd_ClientClean( cl );
1065 TAB_REMOVE( host->i_client, host->client, cl );
1071 if( host->p_tls != NULL)
1072 tls_ServerDelete( host->p_tls );
1074 net_ListenClose( host->fd );
1075 free( host->psz_hostname );
1077 vlc_mutex_destroy( &host->lock );
1078 vlc_object_destroy( host );
1080 if( httpd->i_host <= 0 )
1082 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1083 vlc_object_release( httpd );
1084 vlc_object_detach( httpd );
1085 vlc_object_destroy( httpd );
1087 vlc_mutex_unlock( lockval.p_address );
1090 /* register a new url */
1091 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1092 char *psz_user, char *psz_password,
1093 vlc_bool_t b_check )
1098 vlc_mutex_lock( &host->lock );
1101 for( i = 0; i < host->i_url; i++ )
1103 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1105 msg_Warn( host->httpd,
1106 "cannot add '%s' (url already defined)", psz_url );
1107 vlc_mutex_unlock( &host->lock );
1113 url = malloc( sizeof( httpd_url_t ) );
1116 vlc_mutex_init( host->httpd, &url->lock );
1117 url->psz_url = strdup( psz_url );
1118 url->psz_user = strdup( psz_user ? psz_user : "" );
1119 url->psz_password = strdup( psz_password ? psz_password : "" );
1120 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1122 url->catch[i].cb = NULL;
1123 url->catch[i].p_sys = NULL;
1126 TAB_APPEND( host->i_url, host->url, url );
1127 vlc_mutex_unlock( &host->lock );
1132 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1133 char *psz_user, char *psz_password )
1135 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1136 psz_password, VLC_FALSE );
1139 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1140 char *psz_user, char *psz_password )
1142 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1143 psz_password, VLC_TRUE );
1146 /* register callback on a url */
1147 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1148 httpd_callback_sys_t *p_sys )
1150 vlc_mutex_lock( &url->lock );
1151 url->catch[i_msg].cb = cb;
1152 url->catch[i_msg].p_sys= p_sys;
1153 vlc_mutex_unlock( &url->lock );
1160 void httpd_UrlDelete( httpd_url_t *url )
1162 httpd_host_t *host = url->host;
1165 vlc_mutex_lock( &host->lock );
1166 TAB_REMOVE( host->i_url, host->url, url );
1168 vlc_mutex_destroy( &url->lock );
1169 free( url->psz_url );
1170 free( url->psz_user );
1171 free( url->psz_password );
1173 for( i = 0; i < host->i_client; i++ )
1175 httpd_client_t *client = host->client[i];
1177 if( client->url == url )
1179 /* TODO complete it */
1180 msg_Warn( host, "force closing connections" );
1181 httpd_ClientClean( client );
1182 TAB_REMOVE( host->i_client, host->client, client );
1188 vlc_mutex_unlock( &host->lock );
1191 void httpd_MsgInit( httpd_message_t *msg )
1194 msg->i_type = HTTPD_MSG_NONE;
1195 msg->i_proto = HTTPD_PROTO_NONE;
1196 msg->i_version = -1;
1199 msg->psz_status = NULL;
1201 msg->psz_url = NULL;
1202 msg->psz_args = NULL;
1204 msg->i_channel = -1;
1211 msg->i_body_offset = 0;
1216 void httpd_MsgClean( httpd_message_t *msg )
1220 if( msg->psz_status )
1222 free( msg->psz_status );
1226 free( msg->psz_url );
1230 free( msg->psz_args );
1232 for( i = 0; i < msg->i_name; i++ )
1234 free( msg->name[i] );
1235 free( msg->value[i] );
1247 free( msg->p_body );
1249 httpd_MsgInit( msg );
1252 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1256 for( i = 0; i < msg->i_name; i++ )
1258 if( !strcasecmp( msg->name[i], name ))
1260 return msg->value[i];
1265 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1270 va_start( args, psz_value );
1271 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1272 vasprintf( &value, psz_value, args );
1275 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1276 value = calloc( i_size, sizeof( char ) );
1277 vsnprintf( value, i_size, psz_value, args );
1278 value[i_size - 1] = 0;
1283 name = strdup( name );
1285 TAB_APPEND( msg->i_name, msg->name, name );
1286 TAB_APPEND( msg->i_value, msg->value, value );
1289 static void httpd_ClientInit( httpd_client_t *cl )
1291 cl->i_state = HTTPD_CLIENT_RECEIVING;
1292 cl->i_activity_date = mdate();
1293 cl->i_activity_timeout = I64C(10000000);
1294 cl->i_buffer_size = 10000;
1296 cl->p_buffer = malloc( cl->i_buffer_size );
1297 cl->i_mode = HTTPD_CLIENT_FILE;
1298 cl->b_read_waiting = VLC_FALSE;
1300 httpd_MsgInit( &cl->query );
1301 httpd_MsgInit( &cl->answer );
1304 void httpd_ClientModeStream( httpd_client_t *cl )
1306 cl->i_mode = HTTPD_CLIENT_STREAM;
1309 void httpd_ClientModeBidir( httpd_client_t *cl )
1311 cl->i_mode = HTTPD_CLIENT_BIDIR;
1315 * FIXME: use vlc_getnameinfo
1317 char* httpd_ClientIP( httpd_client_t *cl )
1319 #ifdef HAVE_GETNAMEINFO
1320 char sz_ip[INET6_ADDRSTRLEN + 2];
1323 if( (cl->sock.ss_family == AF_INET6) &&
1324 IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1325 &cl->sock)->sin6_addr) )
1327 /* If client is using IPv4 but server is using IPv6 */
1328 struct sockaddr_in a;
1330 memset( &a, 0, sizeof( a ) );
1331 a.sin_family = AF_INET;
1332 a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1333 a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1334 &cl->sock)->sin6_addr)[3];
1335 i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1336 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1339 i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1340 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1344 /* FIXME: msg_Err */
1347 if( strchr( &sz_ip[1], ':' ) != NULL )
1350 i = strlen( sz_ip );
1354 return strdup( sz_ip );
1357 return strdup( &sz_ip[1] );
1360 /* FIXME not thread safe */
1361 return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1365 static void httpd_ClientClean( httpd_client_t *cl )
1369 if( cl->p_tls != NULL )
1370 tls_ServerSessionClose( cl->p_tls );
1371 net_Close( cl->fd );
1375 httpd_MsgClean( &cl->answer );
1376 httpd_MsgClean( &cl->query );
1380 free( cl->p_buffer );
1381 cl->p_buffer = NULL;
1385 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1387 tls_session_t *p_tls )
1389 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1392 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1393 cl->i_sock_size = i_sock_size;
1397 httpd_ClientInit( cl );
1403 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1405 tls_session_t *p_tls;
1409 return tls_Recv( p_tls, p, i_len );
1411 return recv( cl->fd, p, i_len, 0 );
1415 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1417 tls_session_t *p_tls;
1421 return tls_Send( p_tls, p, i_len );
1423 return send( cl->fd, p, i_len, 0 );
1427 static void httpd_ClientRecv( httpd_client_t *cl )
1431 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1433 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1434 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1438 cl->i_buffer += i_len;
1441 if( cl->i_buffer >= 4 )
1443 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1445 if( cl->p_buffer[0] == '$' )
1447 /* RTSP (rtp over rtsp) */
1448 cl->query.i_proto = HTTPD_PROTO_RTSP;
1449 cl->query.i_type = HTTPD_MSG_CHANNEL;
1450 cl->query.i_channel = cl->p_buffer[1];
1451 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1452 cl->query.p_body = malloc( cl->query.i_body );
1456 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1458 cl->query.i_proto = HTTPD_PROTO_HTTP;
1459 cl->query.i_type = HTTPD_MSG_ANSWER;
1461 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1463 cl->query.i_proto = HTTPD_PROTO_RTSP;
1464 cl->query.i_type = HTTPD_MSG_ANSWER;
1466 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1467 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1468 !strncmp( cl->p_buffer, "POST", 4 ) )
1470 cl->query.i_proto = HTTPD_PROTO_HTTP;
1471 cl->query.i_type = HTTPD_MSG_NONE;
1475 cl->query.i_proto = HTTPD_PROTO_RTSP;
1476 cl->query.i_type = HTTPD_MSG_NONE;
1480 else if( cl->query.i_body > 0 )
1482 /* we are reading the body of a request or a channel */
1483 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1484 cl->query.i_body - cl->i_buffer );
1487 cl->i_buffer += i_len;
1489 if( cl->i_buffer >= cl->query.i_body )
1491 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1496 /* we are reading a header -> char by char */
1499 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1506 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1508 cl->i_buffer_size += 1024;
1509 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1511 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1512 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1516 /* we have finished the header so parse it and set i_body */
1517 cl->p_buffer[cl->i_buffer] = '\0';
1519 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1521 cl->query.i_status =
1522 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1527 cl->query.psz_status = strdup( p );
1539 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1540 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1541 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1543 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1544 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1545 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1546 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1547 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1548 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1550 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1555 cl->query.i_type = HTTPD_MSG_NONE;
1557 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1559 for( i = 0; msg_type[i].name != NULL; i++ )
1561 if( !strncmp( cl->p_buffer, msg_type[i].name,
1562 strlen( msg_type[i].name ) ) )
1564 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1565 cl->query.i_type = msg_type[i].i_type;
1566 if( cl->query.i_proto != msg_type[i].i_proto )
1569 cl->query.i_proto = HTTPD_PROTO_NONE;
1570 cl->query.i_type = HTTPD_MSG_NONE;
1577 if( strstr( cl->p_buffer, "HTTP/1." ) )
1579 cl->query.i_proto = HTTPD_PROTO_HTTP;
1581 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1583 cl->query.i_proto = HTTPD_PROTO_RTSP;
1595 p2 = strchr( p, ' ' );
1600 if( !strncasecmp( p, "rtsp:", 5 ) )
1602 /* for rtsp url, you have rtsp://localhost:port/path */
1604 while( *p == '/' ) p++;
1605 while( *p && *p != '/' ) p++;
1607 cl->query.psz_url = strdup( p );
1608 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1611 cl->query.psz_args = strdup( p3 );
1619 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1621 cl->query.i_proto = HTTPD_PROTO_HTTP;
1622 cl->query.i_version = atoi( p2+7 );
1624 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1626 cl->query.i_proto = HTTPD_PROTO_RTSP;
1627 cl->query.i_version = atoi( p2+7 );
1635 p = strchr( p, '\n' );
1639 while( *p == '\n' || *p == '\r' )
1643 while( p && *p != '\0' )
1646 char *eol = p = strchr( p, '\n' );
1649 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1654 if( ( colon = strchr( line, ':' ) ) )
1660 while( *colon == ' ' )
1664 name = strdup( line );
1665 value = strdup( colon );
1667 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1668 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1670 if( !strcasecmp( name, "Content-Length" ) )
1672 cl->query.i_body = atol( value );
1679 while( *p == '\n' || *p == '\r' )
1686 if( cl->query.i_body > 0 )
1688 /* TODO Mhh, handle the case client will only send a request and close the connection
1689 * to mark and of body (probably only RTSP) */
1690 cl->query.p_body = malloc( cl->query.i_body );
1695 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1701 /* check if the client is to be set to dead */
1702 #if defined( WIN32 ) || defined( UNDER_CE )
1703 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1705 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1708 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1710 /* connection closed -> end of data */
1711 if( cl->query.i_body > 0 )
1713 cl->query.i_body = cl->i_buffer;
1715 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1719 cl->i_state = HTTPD_CLIENT_DEAD;
1722 cl->i_activity_date = mdate();
1724 /* XXX: for QT I have to disable timeout. Try to find why */
1725 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1726 cl->i_activity_timeout = 0;
1728 /* Debugging only */
1729 /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1733 fprintf( stderr, "received new request\n" );
1734 fprintf( stderr, " - proto=%s\n",
1735 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1736 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1737 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1738 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1740 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1741 cl->query.psz_status );
1743 else if( cl->query.i_type != HTTPD_MSG_NONE )
1745 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1747 for( i = 0; i < cl->query.i_name; i++ )
1749 fprintf( stderr, " - option name='%s' value='%s'\n",
1750 cl->query.name[i], cl->query.value[i] );
1756 static void httpd_ClientSend( httpd_client_t *cl )
1761 if( cl->i_buffer < 0 )
1763 /* We need to create the header */
1767 i_size = strlen( "HTTP/1.") + 10 + 10 +
1768 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1769 for( i = 0; i < cl->answer.i_name; i++ )
1771 i_size += strlen( cl->answer.name[i] ) + 2 +
1772 strlen( cl->answer.value[i] ) + 2;
1775 if( cl->i_buffer_size < i_size )
1777 cl->i_buffer_size = i_size;
1778 free( cl->p_buffer );
1779 cl->p_buffer = malloc( i_size );
1783 p += sprintf( p, "%s/1.%d %d %s\r\n",
1784 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1785 cl->answer.i_version,
1786 cl->answer.i_status, cl->answer.psz_status );
1787 for( i = 0; i < cl->answer.i_name; i++ )
1789 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1790 cl->answer.value[i] );
1792 p += sprintf( p, "\r\n" );
1795 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1797 /*fprintf( stderr, "sending answer\n" );
1798 fprintf( stderr, "%s", cl->p_buffer );*/
1801 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1802 cl->i_buffer_size - cl->i_buffer );
1805 cl->i_activity_date = mdate();
1806 cl->i_buffer += i_len;
1808 if( cl->i_buffer >= cl->i_buffer_size )
1810 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1811 !cl->b_read_waiting )
1813 /* catch more body data */
1814 int i_msg = cl->query.i_type;
1815 int64_t i_offset = cl->answer.i_body_offset;
1817 httpd_MsgClean( &cl->answer );
1818 cl->answer.i_body_offset = i_offset;
1820 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1821 &cl->answer, &cl->query );
1824 if( cl->answer.i_body > 0 )
1826 /* send the body data */
1827 free( cl->p_buffer );
1828 cl->p_buffer = cl->answer.p_body;
1829 cl->i_buffer_size = cl->answer.i_body;
1832 cl->answer.i_body = 0;
1833 cl->answer.p_body = NULL;
1838 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1844 #if defined( WIN32 ) || defined( UNDER_CE )
1845 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1847 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1851 cl->i_state = HTTPD_CLIENT_DEAD;
1856 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1858 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1861 cl->i_state = HTTPD_CLIENT_RECEIVING;
1865 cl->i_state = HTTPD_CLIENT_DEAD;
1870 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1874 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1876 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1879 cl->i_state = HTTPD_CLIENT_RECEIVING;
1883 cl->i_state = HTTPD_CLIENT_DEAD;
1888 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1893 static void httpd_HostThread( httpd_host_t *host )
1895 tls_session_t *p_tls = NULL;
1897 while( !host->b_die )
1899 struct timeval timeout;
1902 /* FIXME: (too) many int variables */
1904 int i_handle_max = 0;
1907 int b_low_delay = 0;
1909 if( host->i_url <= 0 )
1916 /* built a set of handle to select */
1917 FD_ZERO( &fds_read );
1918 FD_ZERO( &fds_write );
1922 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1924 FD_SET( fd, &fds_read );
1925 if( fd > i_handle_max )
1929 /* prepare a new TLS session */
1930 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1931 p_tls = tls_ServerSessionPrepare( host->p_tls );
1933 /* add all socket that should be read/write and close dead connection */
1934 vlc_mutex_lock( &host->lock );
1935 for( i_client = 0; i_client < host->i_client; i_client++ )
1937 httpd_client_t *cl = host->client[i_client];
1939 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1940 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1941 ( cl->i_activity_timeout > 0 &&
1942 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1946 // FIXME: it sucks to allocate memory on the stack for debug
1947 ip = httpd_ClientIP( cl );
1948 msg_Dbg( host, "connection closed(%s)",
1949 (ip != NULL) ? ip : "unknown" );
1952 httpd_ClientClean( cl );
1953 TAB_REMOVE( host->i_client, host->client, cl );
1958 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1959 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1961 FD_SET( cl->fd, &fds_read );
1962 i_handle_max = __MAX( i_handle_max, cl->fd );
1964 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1965 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1967 FD_SET( cl->fd, &fds_write );
1968 i_handle_max = __MAX( i_handle_max, cl->fd );
1970 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1972 httpd_message_t *answer = &cl->answer;
1973 httpd_message_t *query = &cl->query;
1974 int i_msg = query->i_type;
1976 httpd_MsgInit( answer );
1978 /* Handle what we received */
1979 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
1980 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
1982 /* we can only receive request from client when not
1985 cl->i_state = HTTPD_CLIENT_DEAD;
1987 else if( i_msg == HTTPD_MSG_ANSWER )
1989 /* We are in BIDIR mode, trigger the callback and then
1990 * check for new data */
1991 if( cl->url && cl->url->catch[i_msg].cb )
1993 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1996 cl->i_state = HTTPD_CLIENT_WAITING;
1998 else if( i_msg == HTTPD_MSG_CHANNEL )
2000 /* We are in BIDIR mode, trigger the callback and then
2001 * check for new data */
2002 if( cl->url && cl->url->catch[i_msg].cb )
2004 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2007 cl->i_state = HTTPD_CLIENT_WAITING;
2009 else if( i_msg == HTTPD_MSG_OPTIONS )
2014 answer->i_proto = query->i_proto ;
2015 answer->i_type = HTTPD_MSG_ANSWER;
2016 answer->i_version= 0;
2017 answer->i_status = 200;
2018 answer->psz_status = strdup( "Ok" );
2021 answer->p_body = NULL;
2023 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2024 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2025 httpd_MsgAdd( answer, "Server", "VLC Server" );
2026 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2027 "TEARDOWN, PLAY, PAUSE" );
2028 httpd_MsgAdd( answer, "Content-Length", "%d",
2031 cl->i_buffer = -1; /* Force the creation of the answer in
2032 * httpd_ClientSend */
2033 cl->i_state = HTTPD_CLIENT_SENDING;
2035 else if( i_msg == HTTPD_MSG_NONE )
2037 if( query->i_proto == HTTPD_PROTO_NONE )
2040 cl->i_state = HTTPD_CLIENT_DEAD;
2047 answer->i_proto = query->i_proto ;
2048 answer->i_type = HTTPD_MSG_ANSWER;
2049 answer->i_version= 0;
2050 answer->i_status = 501;
2051 answer->psz_status = strdup( "Unimplemented" );
2053 p = answer->p_body = malloc( 1000 );
2055 p += sprintf( p, "<html>\n" );
2056 p += sprintf( p, "<head>\n" );
2057 p += sprintf( p, "<title>Error 501</title>\n" );
2058 p += sprintf( p, "</head>\n" );
2059 p += sprintf( p, "<body>\n" );
2060 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2061 p += sprintf( p, "<hr />\n" );
2062 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2063 p += sprintf( p, "</body>\n" );
2064 p += sprintf( p, "</html>\n" );
2066 answer->i_body = p - answer->p_body;
2067 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2069 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2070 cl->i_state = HTTPD_CLIENT_SENDING;
2075 vlc_bool_t b_auth_failed = VLC_FALSE;
2078 /* Search the url and trigger callbacks */
2079 for( i = 0; i < host->i_url; i++ )
2081 httpd_url_t *url = host->url[i];
2083 if( !strcmp( url->psz_url, query->psz_url ) )
2085 if( url->catch[i_msg].cb )
2087 if( answer && ( *url->psz_user || *url->psz_password ) )
2089 /* create the headers */
2090 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2094 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2095 auth = malloc( strlen(b64) + 1 );
2097 if( !strncasecmp( b64, "BASIC", 5 ) )
2100 while( *b64 == ' ' )
2104 b64_decode( auth, b64 );
2110 if( strcmp( id, auth ) )
2112 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2113 /* We fail for all url */
2114 b_auth_failed = VLC_TRUE;
2124 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2126 /* only one url can answer */
2128 if( cl->url == NULL )
2140 answer->i_proto = query->i_proto;
2141 answer->i_type = HTTPD_MSG_ANSWER;
2142 answer->i_version= 0;
2143 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2147 answer->i_status = 401;
2148 answer->psz_status = strdup( "Authorization Required" );
2150 p += sprintf( p, "<html>\n" );
2151 p += sprintf( p, "<head>\n" );
2152 p += sprintf( p, "<title>Error 401</title>\n" );
2153 p += sprintf( p, "</head>\n" );
2154 p += sprintf( p, "<body>\n" );
2155 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2156 p += sprintf( p, "<hr />\n" );
2157 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2158 p += sprintf( p, "</body>\n" );
2159 p += sprintf( p, "</html>\n" );
2163 /* no url registered */
2164 answer->i_status = 404;
2165 answer->psz_status = strdup( "Not found" );
2167 p += sprintf( p, "<html>\n" );
2168 p += sprintf( p, "<head>\n" );
2169 p += sprintf( p, "<title>Error 404</title>\n" );
2170 p += sprintf( p, "</head>\n" );
2171 p += sprintf( p, "<body>\n" );
2172 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2173 p += sprintf( p, "<hr />\n" );
2174 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2175 p += sprintf( p, "</body>\n" );
2176 p += sprintf( p, "</html>\n" );
2179 answer->i_body = p - answer->p_body;
2180 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2182 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2183 cl->i_state = HTTPD_CLIENT_SENDING;
2186 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2188 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2191 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2192 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2193 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2194 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2195 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2196 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2198 httpd_MsgClean( &cl->query );
2199 httpd_MsgInit( &cl->query );
2202 cl->i_buffer_size = 1000;
2203 free( cl->p_buffer );
2204 cl->p_buffer = malloc( cl->i_buffer_size );
2205 cl->i_state = HTTPD_CLIENT_RECEIVING;
2209 cl->i_state = HTTPD_CLIENT_DEAD;
2211 httpd_MsgClean( &cl->answer );
2213 else if( cl->b_read_waiting )
2215 /* we have a message waiting for us to read it */
2216 httpd_MsgClean( &cl->answer );
2217 httpd_MsgClean( &cl->query );
2220 cl->i_buffer_size = 1000;
2221 free( cl->p_buffer );
2222 cl->p_buffer = malloc( cl->i_buffer_size );
2223 cl->i_state = HTTPD_CLIENT_RECEIVING;
2224 cl->b_read_waiting = VLC_FALSE;
2228 int64_t i_offset = cl->answer.i_body_offset;
2229 httpd_MsgClean( &cl->answer );
2231 cl->answer.i_body_offset = i_offset;
2232 free( cl->p_buffer );
2233 cl->p_buffer = NULL;
2235 cl->i_buffer_size = 0;
2237 cl->i_state = HTTPD_CLIENT_WAITING;
2240 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2242 int64_t i_offset = cl->answer.i_body_offset;
2243 int i_msg = cl->query.i_type;
2245 httpd_MsgInit( &cl->answer );
2246 cl->answer.i_body_offset = i_offset;
2248 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2249 &cl->answer, &cl->query );
2250 if( cl->answer.i_type != HTTPD_MSG_NONE )
2252 /* we have new data, so reenter send mode */
2254 cl->p_buffer = cl->answer.p_body;
2255 cl->i_buffer_size = cl->answer.i_body;
2256 cl->answer.p_body = NULL;
2257 cl->answer.i_body = 0;
2258 cl->i_state = HTTPD_CLIENT_SENDING;
2262 /* we shouldn't wait too long */
2263 b_low_delay = VLC_TRUE;
2267 /* Special for BIDIR mode we also check reading */
2268 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2269 cl->i_state == HTTPD_CLIENT_SENDING )
2271 FD_SET( cl->fd, &fds_read );
2272 i_handle_max = __MAX( i_handle_max, cl->fd );
2275 vlc_mutex_unlock( &host->lock );
2277 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2279 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2281 i_ret = select( i_handle_max + 1,
2282 &fds_read, &fds_write, NULL, &timeout );
2284 if( i_ret == -1 && errno != EINTR )
2286 msg_Warn( host, "cannot select sockets" );
2290 else if( i_ret <= 0 )
2295 /* accept new connections */
2296 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2298 if( FD_ISSET( fd, &fds_read ) )
2300 int i_sock_size = sizeof( struct sockaddr_storage );
2301 struct sockaddr_storage sock;
2303 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2308 /* set this new socket non-block */
2309 #if defined( WIN32 ) || defined( UNDER_CE )
2311 unsigned long i_dummy = 1;
2312 ioctlsocket( fd, FIONBIO, &i_dummy );
2315 fcntl( fd, F_SETFL, O_NONBLOCK );
2320 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2323 msg_Err( host, "Rejecting TLS connection" );
2329 case 1: /* missing input - most likely */
2330 i_state = HTTPD_CLIENT_TLS_HS_IN;
2333 case 2: /* missing output */
2334 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2344 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2346 vlc_mutex_lock( &host->lock );
2347 TAB_APPEND( host->i_client, host->client, cl );
2348 vlc_mutex_unlock( &host->lock );
2351 cl->i_state = i_state; // override state for TLS
2353 // FIXME: it sucks to allocate memory for debug
2354 ip = httpd_ClientIP( cl );
2355 msg_Dbg( host, "new connection (%s)",
2356 ip != NULL ? ip : "unknown" );
2364 /* now try all others socket */
2365 vlc_mutex_lock( &host->lock );
2366 for( i_client = 0; i_client < host->i_client; i_client++ )
2368 httpd_client_t *cl = host->client[i_client];
2369 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2371 httpd_ClientRecv( cl );
2373 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2375 httpd_ClientSend( cl );
2377 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2379 httpd_ClientTlsHsIn( cl );
2381 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2383 httpd_ClientTlsHsOut( cl );
2386 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2387 cl->i_state == HTTPD_CLIENT_SENDING &&
2388 FD_ISSET( cl->fd, &fds_read ) )
2390 cl->b_read_waiting = VLC_TRUE;
2393 vlc_mutex_unlock( &host->lock );
2397 tls_ServerSessionClose( p_tls );
2400 #else /* ENABLE_HTTPD */
2402 /* We just define an empty wrapper */
2403 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2406 msg_Err( a, "HTTP daemon support is disabled" );
2409 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2411 msg_Err( a, "HTTP daemon support is disabled" );
2414 void httpd_HostDelete( httpd_host_t *a ){}
2415 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2416 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2417 char *d ){ return 0; }
2418 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2419 httpd_callback_sys_t *d ){ return 0; }
2420 void httpd_UrlDelete( httpd_url_t *a ){}
2422 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2423 void httpd_ClientModeStream( httpd_client_t *a ){}
2424 void httpd_ClientModeBidir( httpd_client_t *a ){}
2426 void httpd_FileDelete( httpd_file_t *a ){}
2427 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2428 char *e, httpd_file_callback_t f,
2429 httpd_file_sys_t *g ){ return 0; }
2431 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2432 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2433 char *b, char *c ){ return 0; }
2435 void httpd_StreamDelete( httpd_stream_t *a ){}
2436 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2437 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2438 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2439 char *d, char *e ){ return 0; }
2441 void httpd_MsgInit ( httpd_message_t *a ){}
2442 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2443 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2444 void httpd_MsgClean( httpd_message_t *a ){}
2446 #endif /* ENABLE_HTTPD */