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;
246 httpd_callback_sys_t *p_sys;
247 } catch[HTTPD_MSG_MAX];
253 HTTPD_CLIENT_RECEIVING,
254 HTTPD_CLIENT_RECEIVE_DONE,
256 HTTPD_CLIENT_SENDING,
257 HTTPD_CLIENT_SEND_DONE,
259 HTTPD_CLIENT_WAITING,
263 HTTPD_CLIENT_TLS_HS_IN,
264 HTTPD_CLIENT_TLS_HS_OUT
269 HTTPD_CLIENT_FILE, /* default */
270 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
271 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
274 struct httpd_client_t
280 struct sockaddr_storage sock;
286 int b_read_waiting; /* stop as soon as possible sending */
288 mtime_t i_activity_date;
289 mtime_t i_activity_timeout;
291 /* buffer for reading header */
297 httpd_message_t query; /* client -> httpd */
298 httpd_message_t answer; /* httpd -> client */
301 tls_session_t *p_tls;
305 /*****************************************************************************
307 *****************************************************************************/
308 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
309 static void b64_decode( char *dest, char *src )
314 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
315 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
316 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
317 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
318 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
319 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
320 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
321 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
322 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
323 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
324 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
325 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
326 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
327 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
328 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
329 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
332 for( i_level = 0; *src != '\0'; src++ )
336 c = b64[(unsigned int)*src];
348 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
352 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
356 *dest++ = ( ( last &0x03 ) << 6 ) | c;
371 { ".htm", "text/html" },
372 { ".html", "text/html" },
373 { ".txt", "text/plain" },
374 { ".xml", "text/xml" },
375 { ".dtd", "text/dtd" },
377 { ".css", "text/css" },
380 { ".gif", "image/gif" },
381 { ".jpe", "image/jpeg" },
382 { ".jpg", "image/jpeg" },
383 { ".jpeg", "image/jpeg" },
384 { ".png", "image/png" },
385 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
388 { ".avi", "video/avi" },
389 { ".asf", "video/x-ms-asf" },
390 { ".m1a", "audio/mpeg" },
391 { ".m2a", "audio/mpeg" },
392 { ".m1v", "video/mpeg" },
393 { ".m2v", "video/mpeg" },
394 { ".mp2", "audio/mpeg" },
395 { ".mp3", "audio/mpeg" },
396 { ".mpa", "audio/mpeg" },
397 { ".mpg", "video/mpeg" },
398 { ".mpeg", "video/mpeg" },
399 { ".mpe", "video/mpeg" },
400 { ".mov", "video/quicktime" },
401 { ".moov", "video/quicktime" },
402 { ".ogg", "application/ogg" },
403 { ".ogm", "application/ogg" },
404 { ".wav", "audio/wav" },
405 { ".wma", "audio/x-ms-wma" },
406 { ".wmv", "video/x-ms-wmv" },
412 static char *httpd_MimeFromUrl( char *psz_url )
417 psz_ext = strrchr( psz_url, '.' );
422 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
424 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
426 return http_mime[i].psz_mime;
430 return "application/octet-stream";
433 /*****************************************************************************
434 * High Level Funtions: httpd_file_t
435 *****************************************************************************/
443 httpd_file_callback_t pf_fill;
444 httpd_file_sys_t *p_sys;
449 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
451 httpd_file_t *file = (httpd_file_t*)p_sys;
453 if( answer == NULL || query == NULL )
457 answer->i_proto = HTTPD_PROTO_HTTP;
458 answer->i_version= query->i_version;
459 answer->i_type = HTTPD_MSG_ANSWER;
461 answer->i_status = 200;
462 answer->psz_status = strdup( "OK" );
464 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
465 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
467 if( query->i_type != HTTPD_MSG_HEAD )
469 char *psz_args = query->psz_args;
470 if( query->i_type == HTTPD_MSG_POST )
473 psz_args = query->p_body;
475 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
478 /* We respect client request */
479 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
481 httpd_MsgAdd( answer, "Connection",
482 httpd_MsgGet( &cl->query, "Connection" ) );
485 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
491 httpd_file_t *httpd_FileNew( httpd_host_t *host,
492 char *psz_url, char *psz_mime,
493 char *psz_user, char *psz_password,
494 char **ppsz_hosts, int i_hosts,
495 httpd_file_callback_t pf_fill,
496 httpd_file_sys_t *p_sys )
498 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
500 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
501 psz_password, ppsz_hosts, i_hosts )
508 file->psz_url = strdup( psz_url );
509 if( psz_mime && *psz_mime )
511 file->psz_mime = strdup( psz_mime );
515 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
518 file->pf_fill = pf_fill;
521 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
522 (httpd_callback_sys_t*)file );
523 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
524 (httpd_callback_sys_t*)file );
525 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
526 (httpd_callback_sys_t*)file );
531 void httpd_FileDelete( httpd_file_t *file )
533 httpd_UrlDelete( file->url );
535 free( file->psz_url );
536 free( file->psz_mime );
541 /*****************************************************************************
542 * High Level Funtions: httpd_redirect_t
543 *****************************************************************************/
544 struct httpd_redirect_t
550 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
551 httpd_client_t *cl, httpd_message_t *answer,
552 httpd_message_t *query )
554 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
557 if( answer == NULL || query == NULL )
561 answer->i_proto = query->i_proto;
562 answer->i_version= query->i_version;
563 answer->i_type = HTTPD_MSG_ANSWER;
564 answer->i_status = 301;
565 answer->psz_status = strdup( "Moved Permanently" );
567 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
568 p += sprintf( p, "<html>\n" );
569 p += sprintf( p, "<head>\n" );
570 p += sprintf( p, "<title>Redirection</title>\n" );
571 p += sprintf( p, "</head>\n" );
572 p += sprintf( p, "<body>\n" );
573 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
574 p += sprintf( p, "<hr />\n" );
575 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
576 p += sprintf( p, "</body>\n" );
577 p += sprintf( p, "</html>\n" );
578 answer->i_body = p - answer->p_body;
580 /* XXX check if it's ok or we need to set an absolute url */
581 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
583 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
588 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
591 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
593 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL,
599 rdir->psz_dst = strdup( psz_url_dst );
601 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
602 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
603 (httpd_callback_sys_t*)rdir );
604 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
605 (httpd_callback_sys_t*)rdir );
606 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
607 (httpd_callback_sys_t*)rdir );
608 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
609 (httpd_callback_sys_t*)rdir );
613 void httpd_RedirectDelete( httpd_redirect_t *rdir )
615 httpd_UrlDelete( rdir->url );
616 free( rdir->psz_dst );
620 /*****************************************************************************
621 * High Level Funtions: httpd_stream_t
622 *****************************************************************************/
623 struct httpd_stream_t
630 /* Header to send as first packet */
634 /* circular buffer */
635 int i_buffer_size; /* buffer size, can't be reallocated smaller */
636 uint8_t *p_buffer; /* buffer */
637 int64_t i_buffer_pos; /* absolute position from begining */
638 int64_t i_buffer_last_pos; /* a new connection will start with that */
641 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
642 httpd_client_t *cl, httpd_message_t *answer,
643 httpd_message_t *query )
645 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
647 if( answer == NULL || query == NULL || cl == NULL )
651 if( answer->i_body_offset > 0 )
657 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
658 answer->i_body_offset );
661 if( answer->i_body_offset >= stream->i_buffer_pos )
663 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
664 return VLC_EGENERIC; /* wait, no data available */
666 if( answer->i_body_offset + stream->i_buffer_size <
667 stream->i_buffer_pos )
669 /* this client isn't fast enough */
670 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
671 answer->i_body_offset, stream->i_buffer_last_pos );
672 answer->i_body_offset = stream->i_buffer_last_pos;
675 i_pos = answer->i_body_offset % stream->i_buffer_size;
676 i_write = stream->i_buffer_pos - answer->i_body_offset;
677 if( i_write > 10000 )
681 else if( i_write <= 0 )
683 return VLC_EGENERIC; /* wait, no data available */
686 /* Don't go past the end of the circular buffer */
687 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
689 /* using HTTPD_MSG_ANSWER -> data available */
690 answer->i_proto = HTTPD_PROTO_HTTP;
691 answer->i_version= 0;
692 answer->i_type = HTTPD_MSG_ANSWER;
694 answer->i_body = i_write;
695 answer->p_body = malloc( i_write );
696 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
698 answer->i_body_offset += i_write;
704 answer->i_proto = HTTPD_PROTO_HTTP;
705 answer->i_version= 0;
706 answer->i_type = HTTPD_MSG_ANSWER;
708 answer->i_status = 200;
709 answer->psz_status = strdup( "OK" );
711 if( query->i_type != HTTPD_MSG_HEAD )
713 httpd_ClientModeStream( cl );
714 vlc_mutex_lock( &stream->lock );
715 /* Send the header */
716 if( stream->i_header > 0 )
718 answer->i_body = stream->i_header;
719 answer->p_body = malloc( stream->i_header );
720 memcpy( answer->p_body, stream->p_header, stream->i_header );
722 answer->i_body_offset = stream->i_buffer_last_pos;
723 vlc_mutex_unlock( &stream->lock );
727 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
728 answer->i_body_offset = 0;
731 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
733 vlc_bool_t b_xplaystream = VLC_FALSE;
736 httpd_MsgAdd( answer, "Content-type", "%s",
737 "application/octet-stream" );
738 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
739 httpd_MsgAdd( answer, "Pragma", "no-cache" );
740 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
741 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
743 /* Check if there is a xPlayStrm=1 */
744 for( i = 0; i < query->i_name; i++ )
746 if( !strcasecmp( query->name[i], "Pragma" ) &&
747 strstr( query->value[i], "xPlayStrm=1" ) )
749 b_xplaystream = VLC_TRUE;
755 answer->i_body_offset = 0;
760 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
762 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
767 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
768 char *psz_url, char *psz_mime,
769 char *psz_user, char *psz_password,
770 char **ppsz_hosts, int i_hosts )
772 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
774 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
775 psz_password, ppsz_hosts, i_hosts )
781 vlc_mutex_init( host, &stream->lock );
782 if( psz_mime && *psz_mime )
784 stream->psz_mime = strdup( psz_mime );
788 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
790 stream->i_header = 0;
791 stream->p_header = NULL;
792 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
793 stream->p_buffer = malloc( stream->i_buffer_size );
794 /* We set to 1 to make life simpler
795 * (this way i_body_offset can never be 0) */
796 stream->i_buffer_pos = 1;
797 stream->i_buffer_last_pos = 1;
799 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
800 (httpd_callback_sys_t*)stream );
801 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
802 (httpd_callback_sys_t*)stream );
803 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
804 (httpd_callback_sys_t*)stream );
809 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
811 vlc_mutex_lock( &stream->lock );
812 if( stream->p_header )
814 free( stream->p_header );
815 stream->p_header = NULL;
817 stream->i_header = i_data;
820 stream->p_header = malloc( i_data );
821 memcpy( stream->p_header, p_data, i_data );
823 vlc_mutex_unlock( &stream->lock );
828 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
833 if( i_data < 0 || p_data == NULL )
837 vlc_mutex_lock( &stream->lock );
839 /* save this pointer (to be used by new connection) */
840 stream->i_buffer_last_pos = stream->i_buffer_pos;
842 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
848 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
850 /* Ok, we can't go past the end of our buffer */
851 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
853 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
858 stream->i_buffer_pos += i_data;
860 vlc_mutex_unlock( &stream->lock );
864 void httpd_StreamDelete( httpd_stream_t *stream )
866 httpd_UrlDelete( stream->url );
867 vlc_mutex_destroy( &stream->lock );
868 if( stream->psz_mime ) free( stream->psz_mime );
869 if( stream->p_header ) free( stream->p_header );
870 if( stream->p_buffer ) free( stream->p_buffer );
875 /*****************************************************************************
877 *****************************************************************************/
878 static void httpd_HostThread( httpd_host_t * );
880 /* create a new host */
881 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
884 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
888 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
890 const char *psz_cert, const char *psz_key,
891 const char *psz_ca, const char *psz_crl )
900 psz_host = strdup( psz_hostname );
901 if( psz_host == NULL )
903 msg_Err( p_this, "memory error" );
907 /* to be sure to avoid multiple creation */
908 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
909 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
910 vlc_mutex_lock( lockval.p_address );
912 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
914 msg_Info( p_this, "creating httpd" );
915 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
917 vlc_mutex_unlock( lockval.p_address );
925 vlc_object_yield( httpd );
926 vlc_object_attach( httpd, p_this->p_vlc );
929 /* verify if it already exist */
930 for( i = httpd->i_host - 1; i >= 0; i-- )
932 host = httpd->host[i];
934 /* cannot mix TLS and non-TLS hosts */
935 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
936 || ( host->i_port != i_port )
937 || strcmp( host->psz_hostname, psz_hostname ) )
943 vlc_mutex_unlock( lockval.p_address );
949 /* determine TLS configuration */
950 if ( psz_cert != NULL )
952 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
955 msg_Err( p_this, "TLS initialization error" );
959 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
961 msg_Err( p_this, "TLS CA error" );
965 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
967 msg_Err( p_this, "TLS CRL error" );
974 /* create the new host */
975 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
977 vlc_mutex_init( httpd, &host->lock );
980 host->fd = net_ListenTCP( p_this, psz_host, i_port );
981 if( host->fd == NULL )
983 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
987 host->i_port = i_port;
988 host->psz_hostname = psz_host;
997 /* create the thread */
998 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
999 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1001 msg_Err( p_this, "cannot spawn http host thread" );
1005 /* now add it to httpd */
1006 TAB_APPEND( httpd->i_host, httpd->host, host );
1007 vlc_mutex_unlock( lockval.p_address );
1013 if( httpd->i_host <= 0 )
1015 vlc_object_release( httpd );
1016 vlc_object_detach( httpd );
1017 vlc_object_destroy( httpd );
1019 vlc_mutex_unlock( lockval.p_address );
1023 net_ListenClose( host->fd );
1024 vlc_mutex_destroy( &host->lock );
1025 vlc_object_destroy( host );
1029 tls_ServerDelete( p_tls );
1035 void httpd_HostDelete( httpd_host_t *host )
1037 httpd_t *httpd = host->httpd;
1038 vlc_value_t lockval;
1041 msg_Dbg( host, "httpd_HostDelete" );
1043 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1044 vlc_mutex_lock( lockval.p_address );
1047 if( host->i_ref > 0 )
1050 vlc_mutex_unlock( lockval.p_address );
1051 msg_Dbg( host, "httpd_HostDelete: host still used" );
1054 TAB_REMOVE( httpd->i_host, httpd->host, host );
1056 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1059 vlc_thread_join( host );
1061 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1063 for( i = 0; i < host->i_url; i++ )
1065 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1067 for( i = 0; i < host->i_client; i++ )
1069 httpd_client_t *cl = host->client[i];
1070 msg_Warn( host, "client still connected" );
1071 httpd_ClientClean( cl );
1072 TAB_REMOVE( host->i_client, host->client, cl );
1078 if( host->p_tls != NULL)
1079 tls_ServerDelete( host->p_tls );
1081 net_ListenClose( host->fd );
1082 free( host->psz_hostname );
1084 vlc_mutex_destroy( &host->lock );
1085 vlc_object_destroy( host );
1087 if( httpd->i_host <= 0 )
1089 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1090 vlc_object_release( httpd );
1091 vlc_object_detach( httpd );
1092 vlc_object_destroy( httpd );
1094 vlc_mutex_unlock( lockval.p_address );
1097 /* register a new url */
1098 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1099 char *psz_user, char *psz_password,
1100 char **ppsz_hosts, int i_hosts,
1101 vlc_bool_t b_check )
1106 vlc_mutex_lock( &host->lock );
1109 for( i = 0; i < host->i_url; i++ )
1111 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1113 msg_Warn( host->httpd,
1114 "cannot add '%s' (url already defined)", psz_url );
1115 vlc_mutex_unlock( &host->lock );
1121 url = malloc( sizeof( httpd_url_t ) );
1124 vlc_mutex_init( host->httpd, &url->lock );
1125 url->psz_url = strdup( psz_url );
1126 url->psz_user = strdup( psz_user ? psz_user : "" );
1127 url->psz_password = strdup( psz_password ? psz_password : "" );
1129 url->ppsz_hosts = NULL;
1130 for( i = 0; i < i_hosts; i++ )
1132 TAB_APPEND( url->i_hosts, url->ppsz_hosts, strdup(ppsz_hosts[i]) );
1134 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1136 url->catch[i].cb = NULL;
1137 url->catch[i].p_sys = NULL;
1140 TAB_APPEND( host->i_url, host->url, url );
1141 vlc_mutex_unlock( &host->lock );
1146 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1147 char *psz_user, char *psz_password,
1148 char **ppsz_hosts, int i_hosts )
1150 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1151 psz_password, ppsz_hosts, i_hosts, VLC_FALSE );
1154 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1155 char *psz_user, char *psz_password,
1156 char **ppsz_hosts, int i_hosts )
1158 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1159 psz_password, ppsz_hosts, i_hosts, VLC_TRUE );
1162 /* register callback on a url */
1163 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1164 httpd_callback_sys_t *p_sys )
1166 vlc_mutex_lock( &url->lock );
1167 url->catch[i_msg].cb = cb;
1168 url->catch[i_msg].p_sys= p_sys;
1169 vlc_mutex_unlock( &url->lock );
1176 void httpd_UrlDelete( httpd_url_t *url )
1178 httpd_host_t *host = url->host;
1181 vlc_mutex_lock( &host->lock );
1182 TAB_REMOVE( host->i_url, host->url, url );
1184 vlc_mutex_destroy( &url->lock );
1185 free( url->psz_url );
1186 free( url->psz_user );
1187 free( url->psz_password );
1188 for( i = 0; i < url->i_hosts; i++ )
1190 TAB_REMOVE( url->i_hosts, url->ppsz_hosts, url->ppsz_hosts[0] );
1193 for( i = 0; i < host->i_client; i++ )
1195 httpd_client_t *client = host->client[i];
1197 if( client->url == url )
1199 /* TODO complete it */
1200 msg_Warn( host, "force closing connections" );
1201 httpd_ClientClean( client );
1202 TAB_REMOVE( host->i_client, host->client, client );
1208 vlc_mutex_unlock( &host->lock );
1211 void httpd_MsgInit( httpd_message_t *msg )
1214 msg->i_type = HTTPD_MSG_NONE;
1215 msg->i_proto = HTTPD_PROTO_NONE;
1216 msg->i_version = -1;
1219 msg->psz_status = NULL;
1221 msg->psz_url = NULL;
1222 msg->psz_args = NULL;
1224 msg->i_channel = -1;
1231 msg->i_body_offset = 0;
1236 void httpd_MsgClean( httpd_message_t *msg )
1240 if( msg->psz_status )
1242 free( msg->psz_status );
1246 free( msg->psz_url );
1250 free( msg->psz_args );
1252 for( i = 0; i < msg->i_name; i++ )
1254 free( msg->name[i] );
1255 free( msg->value[i] );
1267 free( msg->p_body );
1269 httpd_MsgInit( msg );
1272 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1276 for( i = 0; i < msg->i_name; i++ )
1278 if( !strcasecmp( msg->name[i], name ))
1280 return msg->value[i];
1285 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1290 va_start( args, psz_value );
1291 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1292 vasprintf( &value, psz_value, args );
1295 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1296 value = calloc( i_size, sizeof( char ) );
1297 vsnprintf( value, i_size, psz_value, args );
1298 value[i_size - 1] = 0;
1303 name = strdup( name );
1305 TAB_APPEND( msg->i_name, msg->name, name );
1306 TAB_APPEND( msg->i_value, msg->value, value );
1309 static void httpd_ClientInit( httpd_client_t *cl )
1311 cl->i_state = HTTPD_CLIENT_RECEIVING;
1312 cl->i_activity_date = mdate();
1313 cl->i_activity_timeout = I64C(10000000);
1314 cl->i_buffer_size = 10000;
1316 cl->p_buffer = malloc( cl->i_buffer_size );
1317 cl->i_mode = HTTPD_CLIENT_FILE;
1318 cl->b_read_waiting = VLC_FALSE;
1320 httpd_MsgInit( &cl->query );
1321 httpd_MsgInit( &cl->answer );
1324 void httpd_ClientModeStream( httpd_client_t *cl )
1326 cl->i_mode = HTTPD_CLIENT_STREAM;
1329 void httpd_ClientModeBidir( httpd_client_t *cl )
1331 cl->i_mode = HTTPD_CLIENT_BIDIR;
1334 char* httpd_ClientIP( httpd_client_t *cl )
1337 char sz_ip[NI_MAXNUMERICHOST + 2];
1339 i = vlc_getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1340 sz_ip+1, NI_MAXNUMERICHOST, NULL, NI_NUMERICHOST );
1345 /* semi-colon in address => must add bracket for HTTP */
1346 if( strchr( sz_ip + 1, ':' ) != NULL )
1349 i = strlen( sz_ip );
1353 return strdup(sz_ip);
1356 return strdup(sz_ip + 1);
1359 static void httpd_ClientClean( httpd_client_t *cl )
1363 if( cl->p_tls != NULL )
1364 tls_ServerSessionClose( cl->p_tls );
1365 net_Close( cl->fd );
1369 httpd_MsgClean( &cl->answer );
1370 httpd_MsgClean( &cl->query );
1374 free( cl->p_buffer );
1375 cl->p_buffer = NULL;
1379 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1381 tls_session_t *p_tls )
1383 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1386 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1387 cl->i_sock_size = i_sock_size;
1391 httpd_ClientInit( cl );
1397 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1399 tls_session_t *p_tls;
1403 return tls_Recv( p_tls, p, i_len );
1405 return recv( cl->fd, p, i_len, 0 );
1409 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1411 tls_session_t *p_tls;
1415 return tls_Send( p_tls, p, i_len );
1417 return send( cl->fd, p, i_len, 0 );
1421 static void httpd_ClientRecv( httpd_client_t *cl )
1425 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1427 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1428 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1432 cl->i_buffer += i_len;
1435 if( cl->i_buffer >= 4 )
1437 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1439 if( cl->p_buffer[0] == '$' )
1441 /* RTSP (rtp over rtsp) */
1442 cl->query.i_proto = HTTPD_PROTO_RTSP;
1443 cl->query.i_type = HTTPD_MSG_CHANNEL;
1444 cl->query.i_channel = cl->p_buffer[1];
1445 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1446 cl->query.p_body = malloc( cl->query.i_body );
1450 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1452 cl->query.i_proto = HTTPD_PROTO_HTTP;
1453 cl->query.i_type = HTTPD_MSG_ANSWER;
1455 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1457 cl->query.i_proto = HTTPD_PROTO_RTSP;
1458 cl->query.i_type = HTTPD_MSG_ANSWER;
1460 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1461 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1462 !strncmp( cl->p_buffer, "POST", 4 ) )
1464 cl->query.i_proto = HTTPD_PROTO_HTTP;
1465 cl->query.i_type = HTTPD_MSG_NONE;
1469 cl->query.i_proto = HTTPD_PROTO_RTSP;
1470 cl->query.i_type = HTTPD_MSG_NONE;
1474 else if( cl->query.i_body > 0 )
1476 /* we are reading the body of a request or a channel */
1477 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1478 cl->query.i_body - cl->i_buffer );
1481 cl->i_buffer += i_len;
1483 if( cl->i_buffer >= cl->query.i_body )
1485 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1490 /* we are reading a header -> char by char */
1493 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1500 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1502 cl->i_buffer_size += 1024;
1503 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1505 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1506 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1510 /* we have finished the header so parse it and set i_body */
1511 cl->p_buffer[cl->i_buffer] = '\0';
1513 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1515 cl->query.i_status =
1516 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1521 cl->query.psz_status = strdup( p );
1533 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1534 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1535 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1537 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1538 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1539 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1540 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1541 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1542 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1544 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1549 cl->query.i_type = HTTPD_MSG_NONE;
1551 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1553 for( i = 0; msg_type[i].name != NULL; i++ )
1555 if( !strncmp( cl->p_buffer, msg_type[i].name,
1556 strlen( msg_type[i].name ) ) )
1558 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1559 cl->query.i_type = msg_type[i].i_type;
1560 if( cl->query.i_proto != msg_type[i].i_proto )
1563 cl->query.i_proto = HTTPD_PROTO_NONE;
1564 cl->query.i_type = HTTPD_MSG_NONE;
1571 if( strstr( cl->p_buffer, "HTTP/1." ) )
1573 cl->query.i_proto = HTTPD_PROTO_HTTP;
1575 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1577 cl->query.i_proto = HTTPD_PROTO_RTSP;
1589 p2 = strchr( p, ' ' );
1594 if( !strncasecmp( p, "rtsp:", 5 ) )
1596 /* for rtsp url, you have rtsp://localhost:port/path */
1598 while( *p == '/' ) p++;
1599 while( *p && *p != '/' ) p++;
1601 cl->query.psz_url = strdup( p );
1602 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1605 cl->query.psz_args = strdup( p3 );
1613 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1615 cl->query.i_proto = HTTPD_PROTO_HTTP;
1616 cl->query.i_version = atoi( p2+7 );
1618 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1620 cl->query.i_proto = HTTPD_PROTO_RTSP;
1621 cl->query.i_version = atoi( p2+7 );
1629 p = strchr( p, '\n' );
1633 while( *p == '\n' || *p == '\r' )
1637 while( p && *p != '\0' )
1640 char *eol = p = strchr( p, '\n' );
1643 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1648 if( ( colon = strchr( line, ':' ) ) )
1654 while( *colon == ' ' )
1658 name = strdup( line );
1659 value = strdup( colon );
1661 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1662 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1664 if( !strcasecmp( name, "Content-Length" ) )
1666 cl->query.i_body = atol( value );
1673 while( *p == '\n' || *p == '\r' )
1680 if( cl->query.i_body > 0 )
1682 /* TODO Mhh, handle the case client will only send a request and close the connection
1683 * to mark and of body (probably only RTSP) */
1684 cl->query.p_body = malloc( cl->query.i_body );
1689 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1695 /* check if the client is to be set to dead */
1696 #if defined( WIN32 ) || defined( UNDER_CE )
1697 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1699 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1702 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1704 /* connection closed -> end of data */
1705 if( cl->query.i_body > 0 )
1707 cl->query.i_body = cl->i_buffer;
1709 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1713 cl->i_state = HTTPD_CLIENT_DEAD;
1716 cl->i_activity_date = mdate();
1718 /* XXX: for QT I have to disable timeout. Try to find why */
1719 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1720 cl->i_activity_timeout = 0;
1722 /* Debugging only */
1723 /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1727 fprintf( stderr, "received new request\n" );
1728 fprintf( stderr, " - proto=%s\n",
1729 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1730 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1731 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1732 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1734 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1735 cl->query.psz_status );
1737 else if( cl->query.i_type != HTTPD_MSG_NONE )
1739 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1741 for( i = 0; i < cl->query.i_name; i++ )
1743 fprintf( stderr, " - option name='%s' value='%s'\n",
1744 cl->query.name[i], cl->query.value[i] );
1750 static void httpd_ClientSend( httpd_client_t *cl )
1755 if( cl->i_buffer < 0 )
1757 /* We need to create the header */
1761 i_size = strlen( "HTTP/1.") + 10 + 10 +
1762 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1763 for( i = 0; i < cl->answer.i_name; i++ )
1765 i_size += strlen( cl->answer.name[i] ) + 2 +
1766 strlen( cl->answer.value[i] ) + 2;
1769 if( cl->i_buffer_size < i_size )
1771 cl->i_buffer_size = i_size;
1772 free( cl->p_buffer );
1773 cl->p_buffer = malloc( i_size );
1777 p += sprintf( p, "%s/1.%d %d %s\r\n",
1778 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1779 cl->answer.i_version,
1780 cl->answer.i_status, cl->answer.psz_status );
1781 for( i = 0; i < cl->answer.i_name; i++ )
1783 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1784 cl->answer.value[i] );
1786 p += sprintf( p, "\r\n" );
1789 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1791 /*fprintf( stderr, "sending answer\n" );
1792 fprintf( stderr, "%s", cl->p_buffer );*/
1795 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1796 cl->i_buffer_size - cl->i_buffer );
1799 cl->i_activity_date = mdate();
1800 cl->i_buffer += i_len;
1802 if( cl->i_buffer >= cl->i_buffer_size )
1804 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1805 !cl->b_read_waiting )
1807 /* catch more body data */
1808 int i_msg = cl->query.i_type;
1809 int64_t i_offset = cl->answer.i_body_offset;
1811 httpd_MsgClean( &cl->answer );
1812 cl->answer.i_body_offset = i_offset;
1814 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1815 &cl->answer, &cl->query );
1818 if( cl->answer.i_body > 0 )
1820 /* send the body data */
1821 free( cl->p_buffer );
1822 cl->p_buffer = cl->answer.p_body;
1823 cl->i_buffer_size = cl->answer.i_body;
1826 cl->answer.i_body = 0;
1827 cl->answer.p_body = NULL;
1832 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1838 #if defined( WIN32 ) || defined( UNDER_CE )
1839 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1841 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1845 cl->i_state = HTTPD_CLIENT_DEAD;
1850 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1852 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1855 cl->i_state = HTTPD_CLIENT_RECEIVING;
1859 cl->i_state = HTTPD_CLIENT_DEAD;
1864 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1868 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1870 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1873 cl->i_state = HTTPD_CLIENT_RECEIVING;
1877 cl->i_state = HTTPD_CLIENT_DEAD;
1882 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1887 static void httpd_HostThread( httpd_host_t *host )
1889 tls_session_t *p_tls = NULL;
1891 while( !host->b_die )
1893 struct timeval timeout;
1896 /* FIXME: (too) many int variables */
1898 int i_handle_max = 0;
1901 int b_low_delay = 0;
1903 if( host->i_url <= 0 )
1910 /* built a set of handle to select */
1911 FD_ZERO( &fds_read );
1912 FD_ZERO( &fds_write );
1916 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1918 FD_SET( fd, &fds_read );
1919 if( fd > i_handle_max )
1923 /* prepare a new TLS session */
1924 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1925 p_tls = tls_ServerSessionPrepare( host->p_tls );
1927 /* add all socket that should be read/write and close dead connection */
1928 vlc_mutex_lock( &host->lock );
1929 for( i_client = 0; i_client < host->i_client; i_client++ )
1931 httpd_client_t *cl = host->client[i_client];
1933 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1934 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1935 ( cl->i_activity_timeout > 0 &&
1936 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1938 httpd_ClientClean( cl );
1939 TAB_REMOVE( host->i_client, host->client, cl );
1944 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1945 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1947 FD_SET( cl->fd, &fds_read );
1948 i_handle_max = __MAX( i_handle_max, cl->fd );
1950 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1951 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1953 FD_SET( cl->fd, &fds_write );
1954 i_handle_max = __MAX( i_handle_max, cl->fd );
1956 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1958 httpd_message_t *answer = &cl->answer;
1959 httpd_message_t *query = &cl->query;
1960 int i_msg = query->i_type;
1962 httpd_MsgInit( answer );
1964 /* Handle what we received */
1965 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
1966 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
1968 /* we can only receive request from client when not
1971 cl->i_state = HTTPD_CLIENT_DEAD;
1973 else if( i_msg == HTTPD_MSG_ANSWER )
1975 /* We are in BIDIR mode, trigger the callback and then
1976 * check for new data */
1977 if( cl->url && cl->url->catch[i_msg].cb )
1979 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1982 cl->i_state = HTTPD_CLIENT_WAITING;
1984 else if( i_msg == HTTPD_MSG_CHANNEL )
1986 /* We are in BIDIR mode, trigger the callback and then
1987 * check for new data */
1988 if( cl->url && cl->url->catch[i_msg].cb )
1990 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1993 cl->i_state = HTTPD_CLIENT_WAITING;
1995 else if( i_msg == HTTPD_MSG_OPTIONS )
2000 answer->i_proto = query->i_proto ;
2001 answer->i_type = HTTPD_MSG_ANSWER;
2002 answer->i_version= 0;
2003 answer->i_status = 200;
2004 answer->psz_status = strdup( "Ok" );
2007 answer->p_body = NULL;
2009 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2010 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2011 httpd_MsgAdd( answer, "Server", "VLC Server" );
2012 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2013 "TEARDOWN, PLAY, PAUSE" );
2014 httpd_MsgAdd( answer, "Content-Length", "%d",
2017 cl->i_buffer = -1; /* Force the creation of the answer in
2018 * httpd_ClientSend */
2019 cl->i_state = HTTPD_CLIENT_SENDING;
2021 else if( i_msg == HTTPD_MSG_NONE )
2023 if( query->i_proto == HTTPD_PROTO_NONE )
2026 cl->i_state = HTTPD_CLIENT_DEAD;
2033 answer->i_proto = query->i_proto ;
2034 answer->i_type = HTTPD_MSG_ANSWER;
2035 answer->i_version= 0;
2036 answer->i_status = 501;
2037 answer->psz_status = strdup( "Unimplemented" );
2039 p = answer->p_body = malloc( 1000 );
2041 p += sprintf( p, "<html>\n" );
2042 p += sprintf( p, "<head>\n" );
2043 p += sprintf( p, "<title>Error 501</title>\n" );
2044 p += sprintf( p, "</head>\n" );
2045 p += sprintf( p, "<body>\n" );
2046 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2047 p += sprintf( p, "<hr />\n" );
2048 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2049 p += sprintf( p, "</body>\n" );
2050 p += sprintf( p, "</html>\n" );
2052 answer->i_body = p - answer->p_body;
2053 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2055 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2056 cl->i_state = HTTPD_CLIENT_SENDING;
2061 vlc_bool_t b_auth_failed = VLC_FALSE;
2062 vlc_bool_t b_hosts_failed = VLC_FALSE;
2065 /* Search the url and trigger callbacks */
2066 for( i = 0; i < host->i_url; i++ )
2068 httpd_url_t *url = host->url[i];
2070 if( !strcmp( url->psz_url, query->psz_url ) )
2072 if( url->catch[i_msg].cb )
2074 if( answer && url->i_hosts )
2076 char *ip = httpd_ClientIP( cl );
2079 if( net_CheckIP( host, ip,
2081 url->i_hosts ) <= 0 )
2083 b_hosts_failed = VLC_TRUE;
2091 if( answer && ( *url->psz_user || *url->psz_password ) )
2093 /* create the headers */
2094 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2098 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2099 auth = malloc( strlen(b64) + 1 );
2101 if( !strncasecmp( b64, "BASIC", 5 ) )
2104 while( *b64 == ' ' )
2108 b64_decode( auth, b64 );
2114 if( strcmp( id, auth ) )
2116 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2117 /* We fail for all url */
2118 b_auth_failed = VLC_TRUE;
2128 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2130 /* only one url can answer */
2132 if( cl->url == NULL )
2144 answer->i_proto = query->i_proto;
2145 answer->i_type = HTTPD_MSG_ANSWER;
2146 answer->i_version= 0;
2147 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2149 if( b_hosts_failed )
2151 answer->i_status = 403;
2152 answer->psz_status = strdup( "Forbidden" );
2154 p += sprintf( p, "<html>\n" );
2155 p += sprintf( p, "<head>\n" );
2156 p += sprintf( p, "<title>Error 403</title>\n" );
2157 p += sprintf( p, "</head>\n" );
2158 p += sprintf( p, "<body>\n" );
2159 p += sprintf( p, "<h1><center> 403 Forbidden (%s)</center></h1>\n", query->psz_url );
2160 p += sprintf( p, "<hr />\n" );
2161 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2162 p += sprintf( p, "</body>\n" );
2163 p += sprintf( p, "</html>\n" );
2165 else if( b_auth_failed )
2167 answer->i_status = 401;
2168 answer->psz_status = strdup( "Authorization Required" );
2170 p += sprintf( p, "<html>\n" );
2171 p += sprintf( p, "<head>\n" );
2172 p += sprintf( p, "<title>Error 401</title>\n" );
2173 p += sprintf( p, "</head>\n" );
2174 p += sprintf( p, "<body>\n" );
2175 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2176 p += sprintf( p, "<hr />\n" );
2177 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2178 p += sprintf( p, "</body>\n" );
2179 p += sprintf( p, "</html>\n" );
2183 /* no url registered */
2184 answer->i_status = 404;
2185 answer->psz_status = strdup( "Not found" );
2187 p += sprintf( p, "<html>\n" );
2188 p += sprintf( p, "<head>\n" );
2189 p += sprintf( p, "<title>Error 404</title>\n" );
2190 p += sprintf( p, "</head>\n" );
2191 p += sprintf( p, "<body>\n" );
2192 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2193 p += sprintf( p, "<hr />\n" );
2194 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2195 p += sprintf( p, "</body>\n" );
2196 p += sprintf( p, "</html>\n" );
2199 answer->i_body = p - answer->p_body;
2200 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2202 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2203 cl->i_state = HTTPD_CLIENT_SENDING;
2206 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2208 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2211 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2212 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2213 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2214 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2215 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2216 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2218 httpd_MsgClean( &cl->query );
2219 httpd_MsgInit( &cl->query );
2222 cl->i_buffer_size = 1000;
2223 free( cl->p_buffer );
2224 cl->p_buffer = malloc( cl->i_buffer_size );
2225 cl->i_state = HTTPD_CLIENT_RECEIVING;
2229 cl->i_state = HTTPD_CLIENT_DEAD;
2231 httpd_MsgClean( &cl->answer );
2233 else if( cl->b_read_waiting )
2235 /* we have a message waiting for us to read it */
2236 httpd_MsgClean( &cl->answer );
2237 httpd_MsgClean( &cl->query );
2240 cl->i_buffer_size = 1000;
2241 free( cl->p_buffer );
2242 cl->p_buffer = malloc( cl->i_buffer_size );
2243 cl->i_state = HTTPD_CLIENT_RECEIVING;
2244 cl->b_read_waiting = VLC_FALSE;
2248 int64_t i_offset = cl->answer.i_body_offset;
2249 httpd_MsgClean( &cl->answer );
2251 cl->answer.i_body_offset = i_offset;
2252 free( cl->p_buffer );
2253 cl->p_buffer = NULL;
2255 cl->i_buffer_size = 0;
2257 cl->i_state = HTTPD_CLIENT_WAITING;
2260 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2262 int64_t i_offset = cl->answer.i_body_offset;
2263 int i_msg = cl->query.i_type;
2265 httpd_MsgInit( &cl->answer );
2266 cl->answer.i_body_offset = i_offset;
2268 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2269 &cl->answer, &cl->query );
2270 if( cl->answer.i_type != HTTPD_MSG_NONE )
2272 /* we have new data, so reenter send mode */
2274 cl->p_buffer = cl->answer.p_body;
2275 cl->i_buffer_size = cl->answer.i_body;
2276 cl->answer.p_body = NULL;
2277 cl->answer.i_body = 0;
2278 cl->i_state = HTTPD_CLIENT_SENDING;
2282 /* we shouldn't wait too long */
2283 b_low_delay = VLC_TRUE;
2287 /* Special for BIDIR mode we also check reading */
2288 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2289 cl->i_state == HTTPD_CLIENT_SENDING )
2291 FD_SET( cl->fd, &fds_read );
2292 i_handle_max = __MAX( i_handle_max, cl->fd );
2295 vlc_mutex_unlock( &host->lock );
2297 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2299 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2301 i_ret = select( i_handle_max + 1,
2302 &fds_read, &fds_write, NULL, &timeout );
2304 if( i_ret == -1 && errno != EINTR )
2306 msg_Warn( host, "cannot select sockets" );
2310 else if( i_ret <= 0 )
2315 /* accept new connections */
2316 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2318 if( FD_ISSET( fd, &fds_read ) )
2320 int i_sock_size = sizeof( struct sockaddr_storage );
2321 struct sockaddr_storage sock;
2323 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2328 /* set this new socket non-block */
2329 #if defined( WIN32 ) || defined( UNDER_CE )
2331 unsigned long i_dummy = 1;
2332 ioctlsocket( fd, FIONBIO, &i_dummy );
2335 fcntl( fd, F_SETFL, O_NONBLOCK );
2340 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2343 msg_Err( host, "Rejecting TLS connection" );
2349 case 1: /* missing input - most likely */
2350 i_state = HTTPD_CLIENT_TLS_HS_IN;
2353 case 2: /* missing output */
2354 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2363 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2365 vlc_mutex_lock( &host->lock );
2366 TAB_APPEND( host->i_client, host->client, cl );
2367 vlc_mutex_unlock( &host->lock );
2370 cl->i_state = i_state; // override state for TLS
2376 /* now try all others socket */
2377 vlc_mutex_lock( &host->lock );
2378 for( i_client = 0; i_client < host->i_client; i_client++ )
2380 httpd_client_t *cl = host->client[i_client];
2381 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2383 httpd_ClientRecv( cl );
2385 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2387 httpd_ClientSend( cl );
2389 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2391 httpd_ClientTlsHsIn( cl );
2393 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2395 httpd_ClientTlsHsOut( cl );
2398 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2399 cl->i_state == HTTPD_CLIENT_SENDING &&
2400 FD_ISSET( cl->fd, &fds_read ) )
2402 cl->b_read_waiting = VLC_TRUE;
2405 vlc_mutex_unlock( &host->lock );
2409 tls_ServerSessionClose( p_tls );
2412 #else /* ENABLE_HTTPD */
2414 /* We just define an empty wrapper */
2415 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2418 msg_Err( a, "HTTP daemon support is disabled" );
2421 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2423 msg_Err( a, "HTTP daemon support is disabled" );
2426 void httpd_HostDelete( httpd_host_t *a ){}
2427 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2428 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2429 char *d ){ return 0; }
2430 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2431 httpd_callback_sys_t *d ){ return 0; }
2432 void httpd_UrlDelete( httpd_url_t *a ){}
2434 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2435 void httpd_ClientModeStream( httpd_client_t *a ){}
2436 void httpd_ClientModeBidir( httpd_client_t *a ){}
2438 void httpd_FileDelete( httpd_file_t *a ){}
2439 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2440 char *e, httpd_file_callback_t f,
2441 httpd_file_sys_t *g ){ return 0; }
2443 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2444 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2445 char *b, char *c ){ return 0; }
2447 void httpd_StreamDelete( httpd_stream_t *a ){}
2448 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2449 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2450 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2451 char *d, char *e ){ return 0; }
2453 void httpd_MsgInit ( httpd_message_t *a ){}
2454 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2455 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2456 void httpd_MsgClean( httpd_message_t *a ){}
2458 #endif /* ENABLE_HTTPD */