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, char *psz_host,
877 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL );
880 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, char *psz_host,
881 int i_port, tls_server_t *p_tls )
888 psz_host = strdup( psz_host );
889 if( psz_host == NULL )
891 msg_Err( p_this, "memory error" );
895 /* to be sure to avoid multiple creation */
896 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
897 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
898 vlc_mutex_lock( lockval.p_address );
900 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
902 msg_Info( p_this, "creating httpd" );
903 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
905 vlc_mutex_unlock( lockval.p_address );
912 vlc_object_yield( httpd );
913 vlc_object_attach( httpd, p_this->p_vlc );
916 /* verify if it already exist */
917 for( i = httpd->i_host - 1; i >= 0; i-- )
919 host = httpd->host[i];
921 /* FIXME : Cannot re-use host if it uses TLS/SSL */
922 if( httpd->host[i]->p_tls != NULL )
925 if( ( host->i_port != i_port )
926 || strcmp( host->psz_hostname, psz_host ) )
932 vlc_mutex_unlock( lockval.p_address );
936 /* create the new host */
937 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
939 vlc_mutex_init( httpd, &host->lock );
942 host->fd = net_ListenTCP( p_this, psz_host, i_port );
943 if( host->fd == NULL )
945 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
949 host->i_port = i_port;
950 host->psz_hostname = psz_host;
959 /* create the thread */
960 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
961 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
963 msg_Err( p_this, "cannot spawn http host thread" );
967 /* now add it to httpd */
968 TAB_APPEND( httpd->i_host, httpd->host, host );
969 vlc_mutex_unlock( lockval.p_address );
974 if( httpd->i_host <= 0 )
976 vlc_object_release( httpd );
977 vlc_object_detach( httpd );
978 vlc_object_destroy( httpd );
980 vlc_mutex_unlock( lockval.p_address );
984 net_ListenClose( host->fd );
985 vlc_mutex_destroy( &host->lock );
986 vlc_object_destroy( host );
993 void httpd_HostDelete( httpd_host_t *host )
995 httpd_t *httpd = host->httpd;
999 msg_Dbg( host, "httpd_HostDelete" );
1001 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1002 vlc_mutex_lock( lockval.p_address );
1005 if( host->i_ref > 0 )
1008 vlc_mutex_unlock( lockval.p_address );
1009 msg_Dbg( host, "httpd_HostDelete: host still used" );
1012 TAB_REMOVE( httpd->i_host, httpd->host, host );
1014 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1017 vlc_thread_join( host );
1019 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1021 for( i = 0; i < host->i_url; i++ )
1023 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1025 for( i = 0; i < host->i_client; i++ )
1027 httpd_client_t *cl = host->client[i];
1028 msg_Warn( host, "client still connected" );
1029 httpd_ClientClean( cl );
1030 TAB_REMOVE( host->i_client, host->client, cl );
1036 if( host->p_tls != NULL)
1037 tls_ServerDelete( host->p_tls );
1039 net_ListenClose( host->fd );
1040 free( host->psz_hostname );
1042 vlc_mutex_destroy( &host->lock );
1043 vlc_object_destroy( host );
1045 if( httpd->i_host <= 0 )
1047 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1048 vlc_object_release( httpd );
1049 vlc_object_detach( httpd );
1050 vlc_object_destroy( httpd );
1052 vlc_mutex_unlock( lockval.p_address );
1055 /* register a new url */
1056 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1057 char *psz_user, char *psz_password,
1058 vlc_bool_t b_check )
1063 vlc_mutex_lock( &host->lock );
1066 for( i = 0; i < host->i_url; i++ )
1068 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1070 msg_Warn( host->httpd,
1071 "cannot add '%s' (url already defined)", psz_url );
1072 vlc_mutex_unlock( &host->lock );
1078 url = malloc( sizeof( httpd_url_t ) );
1081 vlc_mutex_init( host->httpd, &url->lock );
1082 url->psz_url = strdup( psz_url );
1083 url->psz_user = strdup( psz_user ? psz_user : "" );
1084 url->psz_password = strdup( psz_password ? psz_password : "" );
1085 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1087 url->catch[i].cb = NULL;
1088 url->catch[i].p_sys = NULL;
1091 TAB_APPEND( host->i_url, host->url, url );
1092 vlc_mutex_unlock( &host->lock );
1097 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1098 char *psz_user, char *psz_password )
1100 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1101 psz_password, VLC_FALSE );
1104 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1105 char *psz_user, char *psz_password )
1107 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1108 psz_password, VLC_TRUE );
1111 /* register callback on a url */
1112 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1113 httpd_callback_sys_t *p_sys )
1115 vlc_mutex_lock( &url->lock );
1116 url->catch[i_msg].cb = cb;
1117 url->catch[i_msg].p_sys= p_sys;
1118 vlc_mutex_unlock( &url->lock );
1125 void httpd_UrlDelete( httpd_url_t *url )
1127 httpd_host_t *host = url->host;
1130 vlc_mutex_lock( &host->lock );
1131 TAB_REMOVE( host->i_url, host->url, url );
1133 vlc_mutex_destroy( &url->lock );
1134 free( url->psz_url );
1135 free( url->psz_user );
1136 free( url->psz_password );
1138 for( i = 0; i < host->i_client; i++ )
1140 httpd_client_t *client = host->client[i];
1142 if( client->url == url )
1144 /* TODO complete it */
1145 msg_Warn( host, "force closing connections" );
1146 httpd_ClientClean( client );
1147 TAB_REMOVE( host->i_client, host->client, client );
1153 vlc_mutex_unlock( &host->lock );
1156 void httpd_MsgInit( httpd_message_t *msg )
1159 msg->i_type = HTTPD_MSG_NONE;
1160 msg->i_proto = HTTPD_PROTO_NONE;
1161 msg->i_version = -1;
1164 msg->psz_status = NULL;
1166 msg->psz_url = NULL;
1167 msg->psz_args = NULL;
1169 msg->i_channel = -1;
1176 msg->i_body_offset = 0;
1181 void httpd_MsgClean( httpd_message_t *msg )
1185 if( msg->psz_status )
1187 free( msg->psz_status );
1191 free( msg->psz_url );
1195 free( msg->psz_args );
1197 for( i = 0; i < msg->i_name; i++ )
1199 free( msg->name[i] );
1200 free( msg->value[i] );
1212 free( msg->p_body );
1214 httpd_MsgInit( msg );
1217 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1221 for( i = 0; i < msg->i_name; i++ )
1223 if( !strcasecmp( msg->name[i], name ))
1225 return msg->value[i];
1230 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1235 va_start( args, psz_value );
1236 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1237 vasprintf( &value, psz_value, args );
1240 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1241 value = calloc( i_size, sizeof( char ) );
1242 vsnprintf( value, i_size, psz_value, args );
1243 value[i_size - 1] = 0;
1248 name = strdup( name );
1250 TAB_APPEND( msg->i_name, msg->name, name );
1251 TAB_APPEND( msg->i_value, msg->value, value );
1254 static void httpd_ClientInit( httpd_client_t *cl )
1256 cl->i_state = HTTPD_CLIENT_RECEIVING;
1257 cl->i_activity_date = mdate();
1258 cl->i_activity_timeout = I64C(10000000);
1259 cl->i_buffer_size = 10000;
1261 cl->p_buffer = malloc( cl->i_buffer_size );
1262 cl->i_mode = HTTPD_CLIENT_FILE;
1263 cl->b_read_waiting = VLC_FALSE;
1265 httpd_MsgInit( &cl->query );
1266 httpd_MsgInit( &cl->answer );
1269 void httpd_ClientModeStream( httpd_client_t *cl )
1271 cl->i_mode = HTTPD_CLIENT_STREAM;
1274 void httpd_ClientModeBidir( httpd_client_t *cl )
1276 cl->i_mode = HTTPD_CLIENT_BIDIR;
1280 * FIXME: use vlc_getnameinfo
1282 char* httpd_ClientIP( httpd_client_t *cl )
1284 #ifdef HAVE_GETNAMEINFO
1285 char sz_ip[INET6_ADDRSTRLEN + 2];
1288 if( (cl->sock.ss_family == AF_INET6) &&
1289 IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1290 &cl->sock)->sin6_addr) )
1292 /* If client is using IPv4 but server is using IPv6 */
1293 struct sockaddr_in a;
1295 memset( &a, 0, sizeof( a ) );
1296 a.sin_family = AF_INET;
1297 a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1298 a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1299 &cl->sock)->sin6_addr)[3];
1300 i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1301 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1304 i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1305 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1309 /* FIXME: msg_Err */
1312 if( strchr( &sz_ip[1], ':' ) != NULL )
1315 i = strlen( sz_ip );
1319 return strdup( sz_ip );
1322 return strdup( &sz_ip[1] );
1325 /* FIXME not thread safe */
1326 return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1330 static void httpd_ClientClean( httpd_client_t *cl )
1334 if( cl->p_tls != NULL )
1335 tls_ServerSessionClose( cl->p_tls );
1336 net_Close( cl->fd );
1340 httpd_MsgClean( &cl->answer );
1341 httpd_MsgClean( &cl->query );
1345 free( cl->p_buffer );
1346 cl->p_buffer = NULL;
1350 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1352 tls_session_t *p_tls )
1354 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1357 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1358 cl->i_sock_size = i_sock_size;
1362 httpd_ClientInit( cl );
1368 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1370 tls_session_t *p_tls;
1374 return tls_Recv( p_tls, p, i_len );
1376 return recv( cl->fd, p, i_len, 0 );
1380 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1382 tls_session_t *p_tls;
1386 return tls_Send( p_tls, p, i_len );
1388 return send( cl->fd, p, i_len, 0 );
1392 static void httpd_ClientRecv( httpd_client_t *cl )
1396 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1398 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1399 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1403 cl->i_buffer += i_len;
1406 if( cl->i_buffer >= 4 )
1408 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1410 if( cl->p_buffer[0] == '$' )
1412 /* RTSP (rtp over rtsp) */
1413 cl->query.i_proto = HTTPD_PROTO_RTSP;
1414 cl->query.i_type = HTTPD_MSG_CHANNEL;
1415 cl->query.i_channel = cl->p_buffer[1];
1416 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1417 cl->query.p_body = malloc( cl->query.i_body );
1421 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1423 cl->query.i_proto = HTTPD_PROTO_HTTP;
1424 cl->query.i_type = HTTPD_MSG_ANSWER;
1426 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1428 cl->query.i_proto = HTTPD_PROTO_RTSP;
1429 cl->query.i_type = HTTPD_MSG_ANSWER;
1431 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1432 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1433 !strncmp( cl->p_buffer, "POST", 4 ) )
1435 cl->query.i_proto = HTTPD_PROTO_HTTP;
1436 cl->query.i_type = HTTPD_MSG_NONE;
1440 cl->query.i_proto = HTTPD_PROTO_RTSP;
1441 cl->query.i_type = HTTPD_MSG_NONE;
1445 else if( cl->query.i_body > 0 )
1447 /* we are reading the body of a request or a channel */
1448 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1449 cl->query.i_body - cl->i_buffer );
1452 cl->i_buffer += i_len;
1454 if( cl->i_buffer >= cl->query.i_body )
1456 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1461 /* we are reading a header -> char by char */
1464 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1471 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1473 cl->i_buffer_size += 1024;
1474 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1476 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1477 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1481 /* we have finished the header so parse it and set i_body */
1482 cl->p_buffer[cl->i_buffer] = '\0';
1484 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1486 cl->query.i_status =
1487 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1492 cl->query.psz_status = strdup( p );
1504 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1505 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1506 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1508 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1509 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1510 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1511 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1512 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1513 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1515 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1520 cl->query.i_type = HTTPD_MSG_NONE;
1522 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1524 for( i = 0; msg_type[i].name != NULL; i++ )
1526 if( !strncmp( cl->p_buffer, msg_type[i].name,
1527 strlen( msg_type[i].name ) ) )
1529 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1530 cl->query.i_type = msg_type[i].i_type;
1531 if( cl->query.i_proto != msg_type[i].i_proto )
1534 cl->query.i_proto = HTTPD_PROTO_NONE;
1535 cl->query.i_type = HTTPD_MSG_NONE;
1542 if( strstr( cl->p_buffer, "HTTP/1." ) )
1544 cl->query.i_proto = HTTPD_PROTO_HTTP;
1546 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1548 cl->query.i_proto = HTTPD_PROTO_RTSP;
1560 p2 = strchr( p, ' ' );
1565 if( !strncasecmp( p, "rtsp:", 5 ) )
1567 /* for rtsp url, you have rtsp://localhost:port/path */
1569 while( *p == '/' ) p++;
1570 while( *p && *p != '/' ) p++;
1572 cl->query.psz_url = strdup( p );
1573 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1576 cl->query.psz_args = strdup( p3 );
1584 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1586 cl->query.i_proto = HTTPD_PROTO_HTTP;
1587 cl->query.i_version = atoi( p2+7 );
1589 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1591 cl->query.i_proto = HTTPD_PROTO_RTSP;
1592 cl->query.i_version = atoi( p2+7 );
1600 p = strchr( p, '\n' );
1604 while( *p == '\n' || *p == '\r' )
1608 while( p && *p != '\0' )
1611 char *eol = p = strchr( p, '\n' );
1614 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1619 if( ( colon = strchr( line, ':' ) ) )
1625 while( *colon == ' ' )
1629 name = strdup( line );
1630 value = strdup( colon );
1632 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1633 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1635 if( !strcasecmp( name, "Content-Length" ) )
1637 cl->query.i_body = atol( value );
1644 while( *p == '\n' || *p == '\r' )
1651 if( cl->query.i_body > 0 )
1653 /* TODO Mhh, handle the case client will only send a request and close the connection
1654 * to mark and of body (probably only RTSP) */
1655 cl->query.p_body = malloc( cl->query.i_body );
1660 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1666 /* check if the client is to be set to dead */
1667 #if defined( WIN32 ) || defined( UNDER_CE )
1668 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1670 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1673 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1675 /* connection closed -> end of data */
1676 if( cl->query.i_body > 0 )
1678 cl->query.i_body = cl->i_buffer;
1680 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1684 cl->i_state = HTTPD_CLIENT_DEAD;
1687 cl->i_activity_date = mdate();
1689 /* XXX: for QT I have to disable timeout. Try to find why */
1690 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1691 cl->i_activity_timeout = 0;
1693 /* Debugging only */
1694 /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1698 fprintf( stderr, "received new request\n" );
1699 fprintf( stderr, " - proto=%s\n",
1700 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1701 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1702 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1703 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1705 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1706 cl->query.psz_status );
1708 else if( cl->query.i_type != HTTPD_MSG_NONE )
1710 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1712 for( i = 0; i < cl->query.i_name; i++ )
1714 fprintf( stderr, " - option name='%s' value='%s'\n",
1715 cl->query.name[i], cl->query.value[i] );
1721 static void httpd_ClientSend( httpd_client_t *cl )
1726 if( cl->i_buffer < 0 )
1728 /* We need to create the header */
1732 i_size = strlen( "HTTP/1.") + 10 + 10 +
1733 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1734 for( i = 0; i < cl->answer.i_name; i++ )
1736 i_size += strlen( cl->answer.name[i] ) + 2 +
1737 strlen( cl->answer.value[i] ) + 2;
1740 if( cl->i_buffer_size < i_size )
1742 cl->i_buffer_size = i_size;
1743 free( cl->p_buffer );
1744 cl->p_buffer = malloc( i_size );
1748 p += sprintf( p, "%s/1.%d %d %s\r\n",
1749 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1750 cl->answer.i_version,
1751 cl->answer.i_status, cl->answer.psz_status );
1752 for( i = 0; i < cl->answer.i_name; i++ )
1754 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1755 cl->answer.value[i] );
1757 p += sprintf( p, "\r\n" );
1760 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1762 /*fprintf( stderr, "sending answer\n" );
1763 fprintf( stderr, "%s", cl->p_buffer );*/
1766 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1767 cl->i_buffer_size - cl->i_buffer );
1770 cl->i_activity_date = mdate();
1771 cl->i_buffer += i_len;
1773 if( cl->i_buffer >= cl->i_buffer_size )
1775 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1776 !cl->b_read_waiting )
1778 /* catch more body data */
1779 int i_msg = cl->query.i_type;
1780 int64_t i_offset = cl->answer.i_body_offset;
1782 httpd_MsgClean( &cl->answer );
1783 cl->answer.i_body_offset = i_offset;
1785 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1786 &cl->answer, &cl->query );
1789 if( cl->answer.i_body > 0 )
1791 /* send the body data */
1792 free( cl->p_buffer );
1793 cl->p_buffer = cl->answer.p_body;
1794 cl->i_buffer_size = cl->answer.i_body;
1797 cl->answer.i_body = 0;
1798 cl->answer.p_body = NULL;
1803 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1809 #if defined( WIN32 ) || defined( UNDER_CE )
1810 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1812 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1816 cl->i_state = HTTPD_CLIENT_DEAD;
1821 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1823 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1826 cl->i_state = HTTPD_CLIENT_RECEIVING;
1830 cl->i_state = HTTPD_CLIENT_DEAD;
1835 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1839 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1841 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1844 cl->i_state = HTTPD_CLIENT_RECEIVING;
1848 cl->i_state = HTTPD_CLIENT_DEAD;
1853 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1858 static void httpd_HostThread( httpd_host_t *host )
1860 tls_session_t *p_tls = NULL;
1862 while( !host->b_die )
1864 struct timeval timeout;
1867 /* FIXME: (too) many int variables */
1869 int i_handle_max = 0;
1872 int b_low_delay = 0;
1874 if( host->i_url <= 0 )
1881 /* built a set of handle to select */
1882 FD_ZERO( &fds_read );
1883 FD_ZERO( &fds_write );
1887 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1889 FD_SET( fd, &fds_read );
1890 if( fd > i_handle_max )
1894 /* prepare a new TLS session */
1895 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1896 p_tls = tls_ServerSessionPrepare( host->p_tls );
1898 /* add all socket that should be read/write and close dead connection */
1899 vlc_mutex_lock( &host->lock );
1900 for( i_client = 0; i_client < host->i_client; i_client++ )
1902 httpd_client_t *cl = host->client[i_client];
1904 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1905 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1906 ( cl->i_activity_timeout > 0 &&
1907 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1911 // FIXME: it sucks to allocate memory on the stack for debug
1912 ip = httpd_ClientIP( cl );
1913 msg_Dbg( host, "connection closed(%s)",
1914 (ip != NULL) ? ip : "unknown" );
1917 httpd_ClientClean( cl );
1918 TAB_REMOVE( host->i_client, host->client, cl );
1923 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1924 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1926 FD_SET( cl->fd, &fds_read );
1927 i_handle_max = __MAX( i_handle_max, cl->fd );
1929 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1930 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1932 FD_SET( cl->fd, &fds_write );
1933 i_handle_max = __MAX( i_handle_max, cl->fd );
1935 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1937 httpd_message_t *answer = &cl->answer;
1938 httpd_message_t *query = &cl->query;
1939 int i_msg = query->i_type;
1941 httpd_MsgInit( answer );
1943 /* Handle what we received */
1944 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
1945 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
1947 /* we can only receive request from client when not
1950 cl->i_state = HTTPD_CLIENT_DEAD;
1952 else if( i_msg == HTTPD_MSG_ANSWER )
1954 /* We are in BIDIR mode, trigger the callback and then
1955 * check for new data */
1956 if( cl->url && cl->url->catch[i_msg].cb )
1958 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1961 cl->i_state = HTTPD_CLIENT_WAITING;
1963 else if( i_msg == HTTPD_MSG_CHANNEL )
1965 /* We are in BIDIR mode, trigger the callback and then
1966 * check for new data */
1967 if( cl->url && cl->url->catch[i_msg].cb )
1969 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1972 cl->i_state = HTTPD_CLIENT_WAITING;
1974 else if( i_msg == HTTPD_MSG_OPTIONS )
1979 answer->i_proto = query->i_proto ;
1980 answer->i_type = HTTPD_MSG_ANSWER;
1981 answer->i_version= 0;
1982 answer->i_status = 200;
1983 answer->psz_status = strdup( "Ok" );
1986 answer->p_body = NULL;
1988 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
1989 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1990 httpd_MsgAdd( answer, "Server", "VLC Server" );
1991 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
1992 "TEARDOWN, PLAY, PAUSE" );
1993 httpd_MsgAdd( answer, "Content-Length", "%d",
1996 cl->i_buffer = -1; /* Force the creation of the answer in
1997 * httpd_ClientSend */
1998 cl->i_state = HTTPD_CLIENT_SENDING;
2000 else if( i_msg == HTTPD_MSG_NONE )
2002 if( query->i_proto == HTTPD_PROTO_NONE )
2005 cl->i_state = HTTPD_CLIENT_DEAD;
2012 answer->i_proto = query->i_proto ;
2013 answer->i_type = HTTPD_MSG_ANSWER;
2014 answer->i_version= 0;
2015 answer->i_status = 501;
2016 answer->psz_status = strdup( "Unimplemented" );
2018 p = answer->p_body = malloc( 1000 );
2020 p += sprintf( p, "<html>\n" );
2021 p += sprintf( p, "<head>\n" );
2022 p += sprintf( p, "<title>Error 501</title>\n" );
2023 p += sprintf( p, "</head>\n" );
2024 p += sprintf( p, "<body>\n" );
2025 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2026 p += sprintf( p, "<hr />\n" );
2027 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2028 p += sprintf( p, "</body>\n" );
2029 p += sprintf( p, "</html>\n" );
2031 answer->i_body = p - answer->p_body;
2032 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2034 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2035 cl->i_state = HTTPD_CLIENT_SENDING;
2040 vlc_bool_t b_auth_failed = VLC_FALSE;
2043 /* Search the url and trigger callbacks */
2044 for( i = 0; i < host->i_url; i++ )
2046 httpd_url_t *url = host->url[i];
2048 if( !strcmp( url->psz_url, query->psz_url ) )
2050 if( url->catch[i_msg].cb )
2052 if( answer && ( *url->psz_user || *url->psz_password ) )
2054 /* create the headers */
2055 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2059 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2060 auth = malloc( strlen(b64) + 1 );
2062 if( !strncasecmp( b64, "BASIC", 5 ) )
2065 while( *b64 == ' ' )
2069 b64_decode( auth, b64 );
2075 if( strcmp( id, auth ) )
2077 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2078 /* We fail for all url */
2079 b_auth_failed = VLC_TRUE;
2089 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2091 /* only one url can answer */
2093 if( cl->url == NULL )
2105 answer->i_proto = query->i_proto;
2106 answer->i_type = HTTPD_MSG_ANSWER;
2107 answer->i_version= 0;
2108 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2112 answer->i_status = 401;
2113 answer->psz_status = strdup( "Authorization Required" );
2115 p += sprintf( p, "<html>\n" );
2116 p += sprintf( p, "<head>\n" );
2117 p += sprintf( p, "<title>Error 401</title>\n" );
2118 p += sprintf( p, "</head>\n" );
2119 p += sprintf( p, "<body>\n" );
2120 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2121 p += sprintf( p, "<hr />\n" );
2122 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2123 p += sprintf( p, "</body>\n" );
2124 p += sprintf( p, "</html>\n" );
2128 /* no url registered */
2129 answer->i_status = 404;
2130 answer->psz_status = strdup( "Not found" );
2132 p += sprintf( p, "<html>\n" );
2133 p += sprintf( p, "<head>\n" );
2134 p += sprintf( p, "<title>Error 404</title>\n" );
2135 p += sprintf( p, "</head>\n" );
2136 p += sprintf( p, "<body>\n" );
2137 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2138 p += sprintf( p, "<hr />\n" );
2139 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2140 p += sprintf( p, "</body>\n" );
2141 p += sprintf( p, "</html>\n" );
2144 answer->i_body = p - answer->p_body;
2145 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2147 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2148 cl->i_state = HTTPD_CLIENT_SENDING;
2151 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2153 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2156 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2157 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2158 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2159 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2160 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2161 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2163 httpd_MsgClean( &cl->query );
2164 httpd_MsgInit( &cl->query );
2167 cl->i_buffer_size = 1000;
2168 free( cl->p_buffer );
2169 cl->p_buffer = malloc( cl->i_buffer_size );
2170 cl->i_state = HTTPD_CLIENT_RECEIVING;
2174 cl->i_state = HTTPD_CLIENT_DEAD;
2176 httpd_MsgClean( &cl->answer );
2178 else if( cl->b_read_waiting )
2180 /* we have a message waiting for us to read it */
2181 httpd_MsgClean( &cl->answer );
2182 httpd_MsgClean( &cl->query );
2185 cl->i_buffer_size = 1000;
2186 free( cl->p_buffer );
2187 cl->p_buffer = malloc( cl->i_buffer_size );
2188 cl->i_state = HTTPD_CLIENT_RECEIVING;
2189 cl->b_read_waiting = VLC_FALSE;
2193 int64_t i_offset = cl->answer.i_body_offset;
2194 httpd_MsgClean( &cl->answer );
2196 cl->answer.i_body_offset = i_offset;
2197 free( cl->p_buffer );
2198 cl->p_buffer = NULL;
2200 cl->i_buffer_size = 0;
2202 cl->i_state = HTTPD_CLIENT_WAITING;
2205 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2207 int64_t i_offset = cl->answer.i_body_offset;
2208 int i_msg = cl->query.i_type;
2210 httpd_MsgInit( &cl->answer );
2211 cl->answer.i_body_offset = i_offset;
2213 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2214 &cl->answer, &cl->query );
2215 if( cl->answer.i_type != HTTPD_MSG_NONE )
2217 /* we have new data, so reenter send mode */
2219 cl->p_buffer = cl->answer.p_body;
2220 cl->i_buffer_size = cl->answer.i_body;
2221 cl->answer.p_body = NULL;
2222 cl->answer.i_body = 0;
2223 cl->i_state = HTTPD_CLIENT_SENDING;
2227 /* we shouldn't wait too long */
2228 b_low_delay = VLC_TRUE;
2232 /* Special for BIDIR mode we also check reading */
2233 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2234 cl->i_state == HTTPD_CLIENT_SENDING )
2236 FD_SET( cl->fd, &fds_read );
2237 i_handle_max = __MAX( i_handle_max, cl->fd );
2240 vlc_mutex_unlock( &host->lock );
2242 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2244 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2246 i_ret = select( i_handle_max + 1,
2247 &fds_read, &fds_write, NULL, &timeout );
2249 if( i_ret == -1 && errno != EINTR )
2251 msg_Warn( host, "cannot select sockets" );
2255 else if( i_ret <= 0 )
2260 /* accept new connections */
2261 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2263 if( FD_ISSET( fd, &fds_read ) )
2265 int i_sock_size = sizeof( struct sockaddr_storage );
2266 struct sockaddr_storage sock;
2268 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2273 /* set this new socket non-block */
2274 #if defined( WIN32 ) || defined( UNDER_CE )
2276 unsigned long i_dummy = 1;
2277 ioctlsocket( fd, FIONBIO, &i_dummy );
2280 fcntl( fd, F_SETFL, O_NONBLOCK );
2285 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2288 msg_Err( host, "Rejecting TLS connection" );
2294 case 1: /* missing input - most likely */
2295 i_state = HTTPD_CLIENT_TLS_HS_IN;
2298 case 2: /* missing output */
2299 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2309 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2311 vlc_mutex_lock( &host->lock );
2312 TAB_APPEND( host->i_client, host->client, cl );
2313 vlc_mutex_unlock( &host->lock );
2316 cl->i_state = i_state; // override state for TLS
2318 // FIXME: it sucks to allocate memory for debug
2319 ip = httpd_ClientIP( cl );
2320 msg_Dbg( host, "new connection (%s)",
2321 ip != NULL ? ip : "unknown" );
2329 /* now try all others socket */
2330 vlc_mutex_lock( &host->lock );
2331 for( i_client = 0; i_client < host->i_client; i_client++ )
2333 httpd_client_t *cl = host->client[i_client];
2334 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2336 httpd_ClientRecv( cl );
2338 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2340 httpd_ClientSend( cl );
2342 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2344 httpd_ClientTlsHsIn( cl );
2346 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2348 httpd_ClientTlsHsOut( cl );
2351 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2352 cl->i_state == HTTPD_CLIENT_SENDING &&
2353 FD_ISSET( cl->fd, &fds_read ) )
2355 cl->b_read_waiting = VLC_TRUE;
2358 vlc_mutex_unlock( &host->lock );
2362 tls_ServerSessionClose( p_tls );
2365 #else /* ENABLE_HTTPD */
2367 /* We just define an empty wrapper */
2368 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2371 msg_Err( a, "HTTP daemon support is disabled" );
2374 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2376 msg_Err( a, "HTTP daemon support is disabled" );
2379 void httpd_HostDelete( httpd_host_t *a ){}
2380 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2381 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2382 char *d ){ return 0; }
2383 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2384 httpd_callback_sys_t *d ){ return 0; }
2385 void httpd_UrlDelete( httpd_url_t *a ){}
2387 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2388 void httpd_ClientModeStream( httpd_client_t *a ){}
2389 void httpd_ClientModeBidir( httpd_client_t *a ){}
2391 void httpd_FileDelete( httpd_file_t *a ){}
2392 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2393 char *e, httpd_file_callback_t f,
2394 httpd_file_sys_t *g ){ return 0; }
2396 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2397 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2398 char *b, char *c ){ return 0; }
2400 void httpd_StreamDelete( httpd_stream_t *a ){}
2401 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2402 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2403 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2404 char *d, char *e ){ return 0; }
2406 void httpd_MsgInit ( httpd_message_t *a ){}
2407 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2408 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2409 void httpd_MsgClean( httpd_message_t *a ){}
2411 #endif /* ENABLE_HTTPD */