1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
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"
46 #if defined( UNDER_CE )
48 #elif defined( WIN32 )
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # include <netdb.h> /* hostent ... */
53 # include <sys/socket.h>
54 /* FIXME: should not be needed */
55 # include <netinet/in.h>
56 # ifdef HAVE_ARPA_INET_H
57 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
62 typedef struct httpd_t httpd_t;
64 typedef struct httpd_host_t httpd_host_t;
65 typedef struct httpd_url_t httpd_url_t;
66 typedef struct httpd_client_t httpd_client_t;
75 /* channel communication */
91 /* just to track the count of MSG */
104 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
116 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
118 /* for rtp over rtsp */
128 int64_t i_body_offset;
134 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
135 /* answer could be null, int this case no answer is requested */
136 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
139 /* create a new host */
140 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
142 void httpd_HostDelete( httpd_host_t * );
144 /* register a new url */
145 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
146 /* register callback on a url */
147 int httpd_UrlCatch( httpd_url_t *, int i_msg,
148 httpd_callback_t, httpd_callback_sys_t * );
150 void httpd_UrlDelete( httpd_url_t * );
153 void httpd_ClientModeStream( httpd_client_t *cl );
154 void httpd_ClientModeBidir( httpd_client_t *cl );
155 static void httpd_ClientClean( httpd_client_t *cl );
158 typedef struct httpd_file_t httpd_file_t;
159 typedef struct httpd_file_sys_t httpd_file_sys_t;
160 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
161 httpd_file_t *httpd_FileNew( httpd_host_t *,
162 char *psz_url, char *psz_mime,
163 char *psz_user, char *psz_password,
164 httpd_file_callback_t pf_fill,
165 httpd_file_sys_t *p_sys );
166 void httpd_FileDelete( httpd_file_t * );
168 typedef struct httpd_redirect_t httpd_redirect_t;
169 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
170 void httpd_RedirectDelete( httpd_redirect_t * );
173 typedef struct httpd_stream_t httpd_stream_t;
174 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
175 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
176 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
177 void httpd_StreamDelete( httpd_stream_t * );
180 /* Msg functions facilities */
181 void httpd_MsgInit( httpd_message_t * );
182 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
183 /* return "" if not found. The string is not allocated */
184 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
185 void httpd_MsgClean( httpd_message_t * );
193 /* vlc_mutex_t lock; */
199 static void httpd_ClientClean( httpd_client_t *cl );
201 /* each host run in his own thread */
211 /* address/port and socket for listening at connections */
218 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
219 * This will slow down the url research but make my live easier
220 * All url will have their cb trigger, but only the first one can answer
226 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 uint8_t *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 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
495 httpd_file_sys_t *p_sys )
497 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
499 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
500 psz_password, p_acl )
507 file->psz_url = strdup( psz_url );
508 if( psz_mime && *psz_mime )
510 file->psz_mime = strdup( psz_mime );
514 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
517 file->pf_fill = pf_fill;
520 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
521 (httpd_callback_sys_t*)file );
522 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
523 (httpd_callback_sys_t*)file );
524 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
525 (httpd_callback_sys_t*)file );
530 void httpd_FileDelete( httpd_file_t *file )
532 httpd_UrlDelete( file->url );
534 free( file->psz_url );
535 free( file->psz_mime );
540 /*****************************************************************************
541 * High Level Funtions: httpd_redirect_t
542 *****************************************************************************/
543 struct httpd_redirect_t
549 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
550 httpd_client_t *cl, httpd_message_t *answer,
551 httpd_message_t *query )
553 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
556 if( answer == NULL || query == NULL )
560 answer->i_proto = query->i_proto;
561 answer->i_version= query->i_version;
562 answer->i_type = HTTPD_MSG_ANSWER;
563 answer->i_status = 301;
564 answer->psz_status = strdup( "Moved Permanently" );
566 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
567 p += sprintf( (char *)p,
568 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
569 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
570 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
573 "<title>Redirection</title>\n"
577 "<a href=\"%s\">redirected</a></h1>\n"
579 "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
582 "</html>\n", rdir->psz_dst );
583 answer->i_body = p - answer->p_body;
585 /* XXX check if it's ok or we need to set an absolute url */
586 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
588 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
593 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
596 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
598 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
603 rdir->psz_dst = strdup( psz_url_dst );
605 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
606 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
607 (httpd_callback_sys_t*)rdir );
608 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
609 (httpd_callback_sys_t*)rdir );
610 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
611 (httpd_callback_sys_t*)rdir );
612 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
613 (httpd_callback_sys_t*)rdir );
617 void httpd_RedirectDelete( httpd_redirect_t *rdir )
619 httpd_UrlDelete( rdir->url );
620 free( rdir->psz_dst );
624 /*****************************************************************************
625 * High Level Funtions: httpd_stream_t
626 *****************************************************************************/
627 struct httpd_stream_t
634 /* Header to send as first packet */
638 /* circular buffer */
639 int i_buffer_size; /* buffer size, can't be reallocated smaller */
640 uint8_t *p_buffer; /* buffer */
641 int64_t i_buffer_pos; /* absolute position from begining */
642 int64_t i_buffer_last_pos; /* a new connection will start with that */
645 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
646 httpd_client_t *cl, httpd_message_t *answer,
647 httpd_message_t *query )
649 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
651 if( answer == NULL || query == NULL || cl == NULL )
655 if( answer->i_body_offset > 0 )
661 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
662 answer->i_body_offset );
665 if( answer->i_body_offset >= stream->i_buffer_pos )
667 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
668 return VLC_EGENERIC; /* wait, no data available */
670 if( answer->i_body_offset + stream->i_buffer_size <
671 stream->i_buffer_pos )
673 /* this client isn't fast enough */
674 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
675 answer->i_body_offset, stream->i_buffer_last_pos );
676 answer->i_body_offset = stream->i_buffer_last_pos;
679 i_pos = answer->i_body_offset % stream->i_buffer_size;
680 i_write = stream->i_buffer_pos - answer->i_body_offset;
681 if( i_write > 10000 )
685 else if( i_write <= 0 )
687 return VLC_EGENERIC; /* wait, no data available */
690 /* Don't go past the end of the circular buffer */
691 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
693 /* using HTTPD_MSG_ANSWER -> data available */
694 answer->i_proto = HTTPD_PROTO_HTTP;
695 answer->i_version= 0;
696 answer->i_type = HTTPD_MSG_ANSWER;
698 answer->i_body = i_write;
699 answer->p_body = malloc( i_write );
700 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
702 answer->i_body_offset += i_write;
708 answer->i_proto = HTTPD_PROTO_HTTP;
709 answer->i_version= 0;
710 answer->i_type = HTTPD_MSG_ANSWER;
712 answer->i_status = 200;
713 answer->psz_status = strdup( "OK" );
715 if( query->i_type != HTTPD_MSG_HEAD )
717 httpd_ClientModeStream( cl );
718 vlc_mutex_lock( &stream->lock );
719 /* Send the header */
720 if( stream->i_header > 0 )
722 answer->i_body = stream->i_header;
723 answer->p_body = malloc( stream->i_header );
724 memcpy( answer->p_body, stream->p_header, stream->i_header );
726 answer->i_body_offset = stream->i_buffer_last_pos;
727 vlc_mutex_unlock( &stream->lock );
731 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
732 answer->i_body_offset = 0;
735 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
737 vlc_bool_t b_xplaystream = VLC_FALSE;
740 httpd_MsgAdd( answer, "Content-type", "%s",
741 "application/octet-stream" );
742 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
743 httpd_MsgAdd( answer, "Pragma", "no-cache" );
744 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
745 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
747 /* Check if there is a xPlayStrm=1 */
748 for( i = 0; i < query->i_name; i++ )
750 if( !strcasecmp( query->name[i], "Pragma" ) &&
751 strstr( query->value[i], "xPlayStrm=1" ) )
753 b_xplaystream = VLC_TRUE;
759 answer->i_body_offset = 0;
764 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
766 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
771 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
772 char *psz_url, char *psz_mime,
773 char *psz_user, char *psz_password,
774 const vlc_acl_t *p_acl )
776 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
778 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
779 psz_password, p_acl )
785 vlc_mutex_init( host, &stream->lock );
786 if( psz_mime && *psz_mime )
788 stream->psz_mime = strdup( psz_mime );
792 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
794 stream->i_header = 0;
795 stream->p_header = NULL;
796 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
797 stream->p_buffer = malloc( stream->i_buffer_size );
798 /* We set to 1 to make life simpler
799 * (this way i_body_offset can never be 0) */
800 stream->i_buffer_pos = 1;
801 stream->i_buffer_last_pos = 1;
803 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
804 (httpd_callback_sys_t*)stream );
805 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
806 (httpd_callback_sys_t*)stream );
807 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
808 (httpd_callback_sys_t*)stream );
813 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
815 vlc_mutex_lock( &stream->lock );
816 if( stream->p_header )
818 free( stream->p_header );
819 stream->p_header = NULL;
821 stream->i_header = i_data;
824 stream->p_header = malloc( i_data );
825 memcpy( stream->p_header, p_data, i_data );
827 vlc_mutex_unlock( &stream->lock );
832 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
837 if( i_data < 0 || p_data == NULL )
841 vlc_mutex_lock( &stream->lock );
843 /* save this pointer (to be used by new connection) */
844 stream->i_buffer_last_pos = stream->i_buffer_pos;
846 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
852 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
854 /* Ok, we can't go past the end of our buffer */
855 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
857 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
862 stream->i_buffer_pos += i_data;
864 vlc_mutex_unlock( &stream->lock );
868 void httpd_StreamDelete( httpd_stream_t *stream )
870 httpd_UrlDelete( stream->url );
871 vlc_mutex_destroy( &stream->lock );
872 if( stream->psz_mime ) free( stream->psz_mime );
873 if( stream->p_header ) free( stream->p_header );
874 if( stream->p_buffer ) free( stream->p_buffer );
879 /*****************************************************************************
881 *****************************************************************************/
882 static void httpd_HostThread( httpd_host_t * );
884 /* create a new host */
885 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
888 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
892 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
894 const char *psz_cert, const char *psz_key,
895 const char *psz_ca, const char *psz_crl )
904 psz_host = strdup( psz_hostname );
905 if( psz_host == NULL )
907 msg_Err( p_this, "memory error" );
911 /* to be sure to avoid multiple creation */
912 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
913 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
914 vlc_mutex_lock( lockval.p_address );
916 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
918 msg_Info( p_this, "creating httpd" );
919 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
921 vlc_mutex_unlock( lockval.p_address );
929 vlc_object_yield( httpd );
930 vlc_object_attach( httpd, p_this->p_vlc );
933 /* verify if it already exist */
934 for( i = httpd->i_host - 1; i >= 0; i-- )
936 host = httpd->host[i];
938 /* cannot mix TLS and non-TLS hosts */
939 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
940 || ( host->i_port != i_port )
941 || strcmp( host->psz_hostname, psz_hostname ) )
947 vlc_mutex_unlock( lockval.p_address );
953 /* determine TLS configuration */
954 if ( psz_cert != NULL )
956 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
959 msg_Err( p_this, "TLS initialization error" );
963 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
965 msg_Err( p_this, "TLS CA error" );
969 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
971 msg_Err( p_this, "TLS CRL error" );
978 /* create the new host */
979 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
981 vlc_mutex_init( httpd, &host->lock );
984 host->fd = net_ListenTCP( p_this, psz_host, i_port );
985 if( host->fd == NULL )
987 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
991 host->i_port = i_port;
992 host->psz_hostname = psz_host;
1001 /* create the thread */
1002 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1003 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1005 msg_Err( p_this, "cannot spawn http host thread" );
1009 /* now add it to httpd */
1010 TAB_APPEND( httpd->i_host, httpd->host, host );
1011 vlc_mutex_unlock( lockval.p_address );
1017 if( httpd->i_host <= 0 )
1019 vlc_object_release( httpd );
1020 vlc_object_detach( httpd );
1021 vlc_object_destroy( httpd );
1023 vlc_mutex_unlock( lockval.p_address );
1027 net_ListenClose( host->fd );
1028 vlc_mutex_destroy( &host->lock );
1029 vlc_object_destroy( host );
1033 tls_ServerDelete( p_tls );
1039 void httpd_HostDelete( httpd_host_t *host )
1041 httpd_t *httpd = host->httpd;
1042 vlc_value_t lockval;
1045 msg_Dbg( host, "httpd_HostDelete" );
1047 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1048 vlc_mutex_lock( lockval.p_address );
1051 if( host->i_ref > 0 )
1054 vlc_mutex_unlock( lockval.p_address );
1055 msg_Dbg( host, "httpd_HostDelete: host still used" );
1058 TAB_REMOVE( httpd->i_host, httpd->host, host );
1060 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1063 vlc_thread_join( host );
1065 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1067 for( i = 0; i < host->i_url; i++ )
1069 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1071 for( i = 0; i < host->i_client; i++ )
1073 httpd_client_t *cl = host->client[i];
1074 msg_Warn( host, "client still connected" );
1075 httpd_ClientClean( cl );
1076 TAB_REMOVE( host->i_client, host->client, cl );
1082 if( host->p_tls != NULL)
1083 tls_ServerDelete( host->p_tls );
1085 net_ListenClose( host->fd );
1086 free( host->psz_hostname );
1088 vlc_mutex_destroy( &host->lock );
1089 vlc_object_destroy( host );
1091 if( httpd->i_host <= 0 )
1093 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1094 vlc_object_release( httpd );
1095 vlc_object_detach( httpd );
1096 vlc_object_destroy( httpd );
1098 vlc_mutex_unlock( lockval.p_address );
1101 /* register a new url */
1102 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1103 char *psz_user, char *psz_password,
1104 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1109 vlc_mutex_lock( &host->lock );
1112 for( i = 0; i < host->i_url; i++ )
1114 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1116 msg_Warn( host->httpd,
1117 "cannot add '%s' (url already defined)", psz_url );
1118 vlc_mutex_unlock( &host->lock );
1124 url = malloc( sizeof( httpd_url_t ) );
1127 vlc_mutex_init( host->httpd, &url->lock );
1128 url->psz_url = strdup( psz_url );
1129 url->psz_user = strdup( psz_user ? psz_user : "" );
1130 url->psz_password = strdup( psz_password ? psz_password : "" );
1131 url->p_acl = ACL_Duplicate( host, p_acl );
1132 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1134 url->catch[i].cb = NULL;
1135 url->catch[i].p_sys = NULL;
1138 TAB_APPEND( host->i_url, host->url, url );
1139 vlc_mutex_unlock( &host->lock );
1144 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1145 char *psz_user, char *psz_password,
1146 const vlc_acl_t *p_acl )
1148 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1149 psz_password, p_acl, VLC_FALSE );
1152 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1153 char *psz_user, char *psz_password,
1154 const vlc_acl_t *p_acl )
1156 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1157 psz_password, p_acl, VLC_TRUE );
1160 /* register callback on a url */
1161 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1162 httpd_callback_sys_t *p_sys )
1164 vlc_mutex_lock( &url->lock );
1165 url->catch[i_msg].cb = cb;
1166 url->catch[i_msg].p_sys= p_sys;
1167 vlc_mutex_unlock( &url->lock );
1174 void httpd_UrlDelete( httpd_url_t *url )
1176 httpd_host_t *host = url->host;
1179 vlc_mutex_lock( &host->lock );
1180 TAB_REMOVE( host->i_url, host->url, url );
1182 vlc_mutex_destroy( &url->lock );
1183 free( url->psz_url );
1184 free( url->psz_user );
1185 free( url->psz_password );
1186 ACL_Destroy( url->p_acl );
1188 for( i = 0; i < host->i_client; i++ )
1190 httpd_client_t *client = host->client[i];
1192 if( client->url == url )
1194 /* TODO complete it */
1195 msg_Warn( host, "force closing connections" );
1196 httpd_ClientClean( client );
1197 TAB_REMOVE( host->i_client, host->client, client );
1203 vlc_mutex_unlock( &host->lock );
1206 void httpd_MsgInit( httpd_message_t *msg )
1209 msg->i_type = HTTPD_MSG_NONE;
1210 msg->i_proto = HTTPD_PROTO_NONE;
1211 msg->i_version = -1;
1214 msg->psz_status = NULL;
1216 msg->psz_url = NULL;
1217 msg->psz_args = NULL;
1219 msg->i_channel = -1;
1226 msg->i_body_offset = 0;
1231 void httpd_MsgClean( httpd_message_t *msg )
1235 if( msg->psz_status )
1237 free( msg->psz_status );
1241 free( msg->psz_url );
1245 free( msg->psz_args );
1247 for( i = 0; i < msg->i_name; i++ )
1249 free( msg->name[i] );
1250 free( msg->value[i] );
1262 free( msg->p_body );
1264 httpd_MsgInit( msg );
1267 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1271 for( i = 0; i < msg->i_name; i++ )
1273 if( !strcasecmp( msg->name[i], name ))
1275 return msg->value[i];
1280 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1285 va_start( args, psz_value );
1286 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1287 vasprintf( &value, psz_value, args );
1290 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1291 value = calloc( i_size, sizeof( char ) );
1292 vsnprintf( value, i_size, psz_value, args );
1293 value[i_size - 1] = 0;
1298 name = strdup( name );
1300 TAB_APPEND( msg->i_name, msg->name, name );
1301 TAB_APPEND( msg->i_value, msg->value, value );
1304 static void httpd_ClientInit( httpd_client_t *cl )
1306 cl->i_state = HTTPD_CLIENT_RECEIVING;
1307 cl->i_activity_date = mdate();
1308 cl->i_activity_timeout = I64C(10000000);
1309 cl->i_buffer_size = 10000;
1311 cl->p_buffer = malloc( cl->i_buffer_size );
1312 cl->i_mode = HTTPD_CLIENT_FILE;
1313 cl->b_read_waiting = VLC_FALSE;
1315 httpd_MsgInit( &cl->query );
1316 httpd_MsgInit( &cl->answer );
1319 void httpd_ClientModeStream( httpd_client_t *cl )
1321 cl->i_mode = HTTPD_CLIENT_STREAM;
1324 void httpd_ClientModeBidir( httpd_client_t *cl )
1326 cl->i_mode = HTTPD_CLIENT_BIDIR;
1329 char* httpd_ClientIP( httpd_client_t *cl )
1332 char sz_ip[NI_MAXNUMERICHOST + 2];
1334 i = vlc_getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1335 sz_ip+1, NI_MAXNUMERICHOST, NULL, NI_NUMERICHOST );
1340 /* semi-colon in address => must add bracket for HTTP */
1341 if( strchr( sz_ip + 1, ':' ) != NULL )
1344 i = strlen( sz_ip );
1348 return strdup(sz_ip);
1351 return strdup(sz_ip + 1);
1354 static void httpd_ClientClean( httpd_client_t *cl )
1358 if( cl->p_tls != NULL )
1359 tls_ServerSessionClose( cl->p_tls );
1360 net_Close( cl->fd );
1364 httpd_MsgClean( &cl->answer );
1365 httpd_MsgClean( &cl->query );
1369 free( cl->p_buffer );
1370 cl->p_buffer = NULL;
1374 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1376 tls_session_t *p_tls )
1378 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1381 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1382 cl->i_sock_size = i_sock_size;
1386 httpd_ClientInit( cl );
1392 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1394 tls_session_t *p_tls;
1398 return tls_Recv( p_tls, p, i_len );
1400 return recv( cl->fd, p, i_len, 0 );
1404 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1406 tls_session_t *p_tls;
1410 return tls_Send( p_tls, p, i_len );
1412 return send( cl->fd, p, i_len, 0 );
1416 static void httpd_ClientRecv( httpd_client_t *cl )
1420 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1422 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1423 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1427 cl->i_buffer += i_len;
1430 if( cl->i_buffer >= 4 )
1432 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1434 if( cl->p_buffer[0] == '$' )
1436 /* RTSP (rtp over rtsp) */
1437 cl->query.i_proto = HTTPD_PROTO_RTSP;
1438 cl->query.i_type = HTTPD_MSG_CHANNEL;
1439 cl->query.i_channel = cl->p_buffer[1];
1440 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1441 cl->query.p_body = malloc( cl->query.i_body );
1445 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1447 cl->query.i_proto = HTTPD_PROTO_HTTP;
1448 cl->query.i_type = HTTPD_MSG_ANSWER;
1450 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1452 cl->query.i_proto = HTTPD_PROTO_RTSP;
1453 cl->query.i_type = HTTPD_MSG_ANSWER;
1455 else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
1456 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1457 !memcmp( cl->p_buffer, "POST", 4 ) )
1459 cl->query.i_proto = HTTPD_PROTO_HTTP;
1460 cl->query.i_type = HTTPD_MSG_NONE;
1464 cl->query.i_proto = HTTPD_PROTO_RTSP;
1465 cl->query.i_type = HTTPD_MSG_NONE;
1469 else if( cl->query.i_body > 0 )
1471 /* we are reading the body of a request or a channel */
1472 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1473 cl->query.i_body - cl->i_buffer );
1476 cl->i_buffer += i_len;
1478 if( cl->i_buffer >= cl->query.i_body )
1480 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1485 /* we are reading a header -> char by char */
1488 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1495 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1497 cl->i_buffer_size += 1024;
1498 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1500 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1501 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1505 /* we have finished the header so parse it and set i_body */
1506 cl->p_buffer[cl->i_buffer] = '\0';
1508 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1511 * assume strlen( "HTTP/1.x" ) = 8
1513 cl->query.i_status =
1514 strtol( (char *)&cl->p_buffer[8],
1520 cl->query.psz_status = strdup( p );
1532 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1533 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1534 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1536 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1537 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1538 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1539 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1540 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1541 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1543 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1548 cl->query.i_type = HTTPD_MSG_NONE;
1550 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1552 for( i = 0; msg_type[i].name != NULL; i++ )
1554 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1555 strlen( msg_type[i].name ) ) )
1557 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1558 cl->query.i_type = msg_type[i].i_type;
1559 if( cl->query.i_proto != msg_type[i].i_proto )
1562 cl->query.i_proto = HTTPD_PROTO_NONE;
1563 cl->query.i_type = HTTPD_MSG_NONE;
1570 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1572 cl->query.i_proto = HTTPD_PROTO_HTTP;
1574 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1576 cl->query.i_proto = HTTPD_PROTO_RTSP;
1588 p2 = strchr( p, ' ' );
1593 if( !strncasecmp( p, "rtsp:", 5 ) )
1595 /* for rtsp url, you have rtsp://localhost:port/path */
1597 while( *p == '/' ) p++;
1598 while( *p && *p != '/' ) p++;
1600 cl->query.psz_url = strdup( p );
1601 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1604 cl->query.psz_args = (uint8_t *)strdup( p3 );
1612 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1614 cl->query.i_proto = HTTPD_PROTO_HTTP;
1615 cl->query.i_version = atoi( p2+7 );
1617 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1619 cl->query.i_proto = HTTPD_PROTO_RTSP;
1620 cl->query.i_version = atoi( p2+7 );
1628 p = strchr( p, '\n' );
1632 while( *p == '\n' || *p == '\r' )
1636 while( p && *p != '\0' )
1639 char *eol = p = strchr( p, '\n' );
1642 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1647 if( ( colon = strchr( line, ':' ) ) )
1653 while( *colon == ' ' )
1657 name = strdup( line );
1658 value = strdup( colon );
1660 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1661 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1663 if( !strcasecmp( name, "Content-Length" ) )
1665 cl->query.i_body = atol( value );
1672 while( *p == '\n' || *p == '\r' )
1679 if( cl->query.i_body > 0 )
1681 /* TODO Mhh, handle the case client will only send a request and close the connection
1682 * to mark and of body (probably only RTSP) */
1683 cl->query.p_body = malloc( cl->query.i_body );
1688 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1694 /* check if the client is to be set to dead */
1695 #if defined( WIN32 ) || defined( UNDER_CE )
1696 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1698 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1701 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1703 /* connection closed -> end of data */
1704 if( cl->query.i_body > 0 )
1706 cl->query.i_body = cl->i_buffer;
1708 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1712 cl->i_state = HTTPD_CLIENT_DEAD;
1715 cl->i_activity_date = mdate();
1717 /* XXX: for QT I have to disable timeout. Try to find why */
1718 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1719 cl->i_activity_timeout = 0;
1721 /* Debugging only */
1722 /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1726 fprintf( stderr, "received new request\n" );
1727 fprintf( stderr, " - proto=%s\n",
1728 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1729 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1730 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1731 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1733 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1734 cl->query.psz_status );
1736 else if( cl->query.i_type != HTTPD_MSG_NONE )
1738 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1740 for( i = 0; i < cl->query.i_name; i++ )
1742 fprintf( stderr, " - option name='%s' value='%s'\n",
1743 cl->query.name[i], cl->query.value[i] );
1749 static void httpd_ClientSend( httpd_client_t *cl )
1754 if( cl->i_buffer < 0 )
1756 /* We need to create the header */
1760 i_size = strlen( "HTTP/1.") + 10 + 10 +
1761 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1762 for( i = 0; i < cl->answer.i_name; i++ )
1764 i_size += strlen( cl->answer.name[i] ) + 2 +
1765 strlen( cl->answer.value[i] ) + 2;
1768 if( cl->i_buffer_size < i_size )
1770 cl->i_buffer_size = i_size;
1771 free( cl->p_buffer );
1772 cl->p_buffer = malloc( i_size );
1774 p = (char *)cl->p_buffer;
1776 p += sprintf( p, "%s/1.%d %d %s\r\n",
1777 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1778 cl->answer.i_version,
1779 cl->answer.i_status, cl->answer.psz_status );
1780 for( i = 0; i < cl->answer.i_name; i++ )
1782 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1783 cl->answer.value[i] );
1785 p += sprintf( p, "\r\n" );
1788 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1790 /*fprintf( stderr, "sending answer\n" );
1791 fprintf( stderr, "%s", cl->p_buffer );*/
1794 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1795 cl->i_buffer_size - cl->i_buffer );
1798 cl->i_activity_date = mdate();
1799 cl->i_buffer += i_len;
1801 if( cl->i_buffer >= cl->i_buffer_size )
1803 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1804 !cl->b_read_waiting )
1806 /* catch more body data */
1807 int i_msg = cl->query.i_type;
1808 int64_t i_offset = cl->answer.i_body_offset;
1810 httpd_MsgClean( &cl->answer );
1811 cl->answer.i_body_offset = i_offset;
1813 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1814 &cl->answer, &cl->query );
1817 if( cl->answer.i_body > 0 )
1819 /* send the body data */
1820 free( cl->p_buffer );
1821 cl->p_buffer = cl->answer.p_body;
1822 cl->i_buffer_size = cl->answer.i_body;
1825 cl->answer.i_body = 0;
1826 cl->answer.p_body = NULL;
1831 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1837 #if defined( WIN32 ) || defined( UNDER_CE )
1838 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1840 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1844 cl->i_state = HTTPD_CLIENT_DEAD;
1849 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1851 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1854 cl->i_state = HTTPD_CLIENT_RECEIVING;
1858 cl->i_state = HTTPD_CLIENT_DEAD;
1863 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1867 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1869 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1872 cl->i_state = HTTPD_CLIENT_RECEIVING;
1876 cl->i_state = HTTPD_CLIENT_DEAD;
1881 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1886 static void httpd_HostThread( httpd_host_t *host )
1888 tls_session_t *p_tls = NULL;
1890 while( !host->b_die )
1892 struct timeval timeout;
1895 /* FIXME: (too) many int variables */
1897 int i_handle_max = 0;
1900 int b_low_delay = 0;
1902 if( host->i_url <= 0 )
1909 /* built a set of handle to select */
1910 FD_ZERO( &fds_read );
1911 FD_ZERO( &fds_write );
1915 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1917 FD_SET( fd, &fds_read );
1918 if( fd > i_handle_max )
1922 /* prepare a new TLS session */
1923 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1924 p_tls = tls_ServerSessionPrepare( host->p_tls );
1926 /* add all socket that should be read/write and close dead connection */
1927 vlc_mutex_lock( &host->lock );
1928 for( i_client = 0; i_client < host->i_client; i_client++ )
1930 httpd_client_t *cl = host->client[i_client];
1932 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1933 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1934 ( cl->i_activity_timeout > 0 &&
1935 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1937 httpd_ClientClean( cl );
1938 TAB_REMOVE( host->i_client, host->client, cl );
1943 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1944 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1946 FD_SET( cl->fd, &fds_read );
1947 i_handle_max = __MAX( i_handle_max, cl->fd );
1949 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1950 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1952 FD_SET( cl->fd, &fds_write );
1953 i_handle_max = __MAX( i_handle_max, cl->fd );
1955 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1957 httpd_message_t *answer = &cl->answer;
1958 httpd_message_t *query = &cl->query;
1959 int i_msg = query->i_type;
1961 httpd_MsgInit( answer );
1963 /* Handle what we received */
1964 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
1965 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
1967 /* we can only receive request from client when not
1970 cl->i_state = HTTPD_CLIENT_DEAD;
1972 else if( i_msg == HTTPD_MSG_ANSWER )
1974 /* We are in BIDIR mode, trigger the callback and then
1975 * check for new data */
1976 if( cl->url && cl->url->catch[i_msg].cb )
1978 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1981 cl->i_state = HTTPD_CLIENT_WAITING;
1983 else if( i_msg == HTTPD_MSG_CHANNEL )
1985 /* We are in BIDIR mode, trigger the callback and then
1986 * check for new data */
1987 if( cl->url && cl->url->catch[i_msg].cb )
1989 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1992 cl->i_state = HTTPD_CLIENT_WAITING;
1994 else if( i_msg == HTTPD_MSG_OPTIONS )
1999 answer->i_proto = query->i_proto ;
2000 answer->i_type = HTTPD_MSG_ANSWER;
2001 answer->i_version= 0;
2002 answer->i_status = 200;
2003 answer->psz_status = strdup( "Ok" );
2006 answer->p_body = NULL;
2008 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2009 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2010 httpd_MsgAdd( answer, "Server", "VLC Server" );
2011 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2012 "TEARDOWN, PLAY, PAUSE" );
2013 httpd_MsgAdd( answer, "Content-Length", "%d",
2016 cl->i_buffer = -1; /* Force the creation of the answer in
2017 * httpd_ClientSend */
2018 cl->i_state = HTTPD_CLIENT_SENDING;
2020 else if( i_msg == HTTPD_MSG_NONE )
2022 if( query->i_proto == HTTPD_PROTO_NONE )
2025 cl->i_state = HTTPD_CLIENT_DEAD;
2032 answer->i_proto = query->i_proto ;
2033 answer->i_type = HTTPD_MSG_ANSWER;
2034 answer->i_version= 0;
2035 answer->i_status = 501;
2036 answer->psz_status = strdup( "Unimplemented" );
2038 p = answer->p_body = malloc( 1000 );
2040 p += sprintf( (char *)p,
2041 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
2042 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2043 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2046 "<title>Error 501</title>\n"
2049 "<h1>501 Unimplemented</h1>\n"
2051 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2055 answer->i_body = p - answer->p_body;
2056 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2058 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2059 cl->i_state = HTTPD_CLIENT_SENDING;
2064 vlc_bool_t b_auth_failed = VLC_FALSE;
2065 vlc_bool_t b_hosts_failed = VLC_FALSE;
2068 /* Search the url and trigger callbacks */
2069 for( i = 0; i < host->i_url; i++ )
2071 httpd_url_t *url = host->url[i];
2073 if( !strcmp( url->psz_url, query->psz_url ) )
2075 if( url->catch[i_msg].cb )
2077 if( answer && ( url->p_acl != NULL ) )
2079 char *ip = httpd_ClientIP( cl );
2082 if( ACL_Check( url->p_acl, ip ) )
2084 b_hosts_failed = VLC_TRUE;
2092 if( answer && ( *url->psz_user || *url->psz_password ) )
2094 /* create the headers */
2095 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2099 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2100 auth = malloc( strlen(b64) + 1 );
2102 if( !strncasecmp( b64, "BASIC", 5 ) )
2105 while( *b64 == ' ' )
2109 b64_decode( auth, b64 );
2115 if( strcmp( id, auth ) )
2117 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2118 /* We fail for all url */
2119 b_auth_failed = VLC_TRUE;
2129 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2131 /* only one url can answer */
2133 if( cl->url == NULL )
2145 answer->i_proto = query->i_proto;
2146 answer->i_type = HTTPD_MSG_ANSWER;
2147 answer->i_version= 0;
2148 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2150 if( b_hosts_failed )
2152 answer->i_status = 403;
2153 answer->psz_status = strdup( "Forbidden" );
2155 /* FIXME: lots of code duplication */
2156 p += sprintf( (char *)p,
2157 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
2158 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2159 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2162 "<title>Error 403</title>\n"
2165 "<h1>403 Forbidden (%s)</h1>\n"
2167 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2169 "</html>\n", query->psz_url );
2171 else if( b_auth_failed )
2173 answer->i_status = 401;
2174 answer->psz_status = strdup( "Authorization Required" );
2176 p += sprintf( (char *)p,
2177 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
2178 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2179 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2182 "<title>Error 401</title>\n"
2185 "<h1>401 Authorization Required (%s)</h1>\n"
2187 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2189 "</html>\n", query->psz_url );
2193 /* no url registered */
2194 answer->i_status = 404;
2195 answer->psz_status = strdup( "Not found" );
2197 p += sprintf( (char *)p,
2198 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
2199 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2200 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2203 "<title>Error 404</title>\n"
2206 "<h1>404 Resource not found(%s)</h1>\n"
2208 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2210 "</html>\n", query->psz_url );
2213 answer->i_body = p - answer->p_body;
2214 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2216 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2217 cl->i_state = HTTPD_CLIENT_SENDING;
2220 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2222 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2225 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2226 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2227 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2228 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2229 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2230 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2232 httpd_MsgClean( &cl->query );
2233 httpd_MsgInit( &cl->query );
2236 cl->i_buffer_size = 1000;
2237 free( cl->p_buffer );
2238 cl->p_buffer = malloc( cl->i_buffer_size );
2239 cl->i_state = HTTPD_CLIENT_RECEIVING;
2243 cl->i_state = HTTPD_CLIENT_DEAD;
2245 httpd_MsgClean( &cl->answer );
2247 else if( cl->b_read_waiting )
2249 /* we have a message waiting for us to read it */
2250 httpd_MsgClean( &cl->answer );
2251 httpd_MsgClean( &cl->query );
2254 cl->i_buffer_size = 1000;
2255 free( cl->p_buffer );
2256 cl->p_buffer = malloc( cl->i_buffer_size );
2257 cl->i_state = HTTPD_CLIENT_RECEIVING;
2258 cl->b_read_waiting = VLC_FALSE;
2262 int64_t i_offset = cl->answer.i_body_offset;
2263 httpd_MsgClean( &cl->answer );
2265 cl->answer.i_body_offset = i_offset;
2266 free( cl->p_buffer );
2267 cl->p_buffer = NULL;
2269 cl->i_buffer_size = 0;
2271 cl->i_state = HTTPD_CLIENT_WAITING;
2274 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2276 int64_t i_offset = cl->answer.i_body_offset;
2277 int i_msg = cl->query.i_type;
2279 httpd_MsgInit( &cl->answer );
2280 cl->answer.i_body_offset = i_offset;
2282 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2283 &cl->answer, &cl->query );
2284 if( cl->answer.i_type != HTTPD_MSG_NONE )
2286 /* we have new data, so reenter send mode */
2288 cl->p_buffer = cl->answer.p_body;
2289 cl->i_buffer_size = cl->answer.i_body;
2290 cl->answer.p_body = NULL;
2291 cl->answer.i_body = 0;
2292 cl->i_state = HTTPD_CLIENT_SENDING;
2296 /* we shouldn't wait too long */
2297 b_low_delay = VLC_TRUE;
2301 /* Special for BIDIR mode we also check reading */
2302 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2303 cl->i_state == HTTPD_CLIENT_SENDING )
2305 FD_SET( cl->fd, &fds_read );
2306 i_handle_max = __MAX( i_handle_max, cl->fd );
2309 vlc_mutex_unlock( &host->lock );
2311 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2313 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2315 i_ret = select( i_handle_max + 1,
2316 &fds_read, &fds_write, NULL, &timeout );
2318 if( i_ret == -1 && errno != EINTR )
2320 #if defined(WIN32) || defined(UNDER_CE)
2321 msg_Warn( host, "cannot select sockets (%d)", WSAGetLastError( ) );
2323 msg_Warn( host, "cannot select sockets : %s", strerror( errno ) );
2328 else if( i_ret <= 0 )
2333 /* accept new connections */
2334 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2336 if( FD_ISSET( fd, &fds_read ) )
2338 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2339 struct sockaddr_storage sock;
2341 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2346 /* set this new socket non-block */
2347 #if defined( WIN32 ) || defined( UNDER_CE )
2349 unsigned long i_dummy = 1;
2350 ioctlsocket( fd, FIONBIO, &i_dummy );
2353 fcntl( fd, F_SETFL, O_NONBLOCK );
2358 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2361 msg_Err( host, "Rejecting TLS connection" );
2367 case 1: /* missing input - most likely */
2368 i_state = HTTPD_CLIENT_TLS_HS_IN;
2371 case 2: /* missing output */
2372 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2381 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2383 vlc_mutex_lock( &host->lock );
2384 TAB_APPEND( host->i_client, host->client, cl );
2385 vlc_mutex_unlock( &host->lock );
2388 cl->i_state = i_state; // override state for TLS
2394 /* now try all others socket */
2395 vlc_mutex_lock( &host->lock );
2396 for( i_client = 0; i_client < host->i_client; i_client++ )
2398 httpd_client_t *cl = host->client[i_client];
2399 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2401 httpd_ClientRecv( cl );
2403 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2405 httpd_ClientSend( cl );
2407 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2409 httpd_ClientTlsHsIn( cl );
2411 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2413 httpd_ClientTlsHsOut( cl );
2416 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2417 cl->i_state == HTTPD_CLIENT_SENDING &&
2418 FD_ISSET( cl->fd, &fds_read ) )
2420 cl->b_read_waiting = VLC_TRUE;
2423 vlc_mutex_unlock( &host->lock );
2427 tls_ServerSessionClose( p_tls );
2430 #else /* ENABLE_HTTPD */
2432 /* We just define an empty wrapper */
2433 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2436 msg_Err( a, "HTTP daemon support is disabled" );
2439 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2441 msg_Err( a, "HTTP daemon support is disabled" );
2444 void httpd_HostDelete( httpd_host_t *a )
2447 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2448 char *psz_user, char *psz_password,
2449 const vlc_acl_t *p_acl )
2453 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2454 char *psz_user, char *psz_password,
2455 const vlc_acl_t *p_acl )
2459 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2460 httpd_callback_sys_t *d ){ return 0; }
2461 void httpd_UrlDelete( httpd_url_t *a ){}
2463 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2464 void httpd_ClientModeStream( httpd_client_t *a ){}
2465 void httpd_ClientModeBidir( httpd_client_t *a ){}
2467 void httpd_FileDelete( httpd_file_t *a ){}
2468 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2469 char *e, httpd_file_callback_t f,
2470 httpd_file_sys_t *g ){ return 0; }
2472 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2473 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2474 char *b, char *c ){ return 0; }
2476 void httpd_StreamDelete( httpd_stream_t *a ){}
2477 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2478 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2479 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2480 char *d, char *e ){ return 0; }
2482 void httpd_MsgInit ( httpd_message_t *a ){}
2483 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2484 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2485 void httpd_MsgClean( httpd_message_t *a ){}
2487 #endif /* ENABLE_HTTPD */