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;
1335 * FIXME: use vlc_getnameinfo
1337 char* httpd_ClientIP( httpd_client_t *cl )
1339 #ifdef HAVE_GETNAMEINFO
1340 char sz_ip[INET6_ADDRSTRLEN + 2];
1343 if( (cl->sock.ss_family == AF_INET6) &&
1344 IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1345 &cl->sock)->sin6_addr) )
1347 /* If client is using IPv4 but server is using IPv6 */
1348 struct sockaddr_in a;
1350 memset( &a, 0, sizeof( a ) );
1351 a.sin_family = AF_INET;
1352 a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1353 a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1354 &cl->sock)->sin6_addr)[3];
1355 i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1356 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1359 i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1360 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1364 /* FIXME: msg_Err */
1367 if( strchr( &sz_ip[1], ':' ) != NULL )
1370 i = strlen( sz_ip );
1374 return strdup( sz_ip );
1377 return strdup( &sz_ip[1] );
1380 /* FIXME not thread safe */
1381 return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1385 static void httpd_ClientClean( httpd_client_t *cl )
1389 if( cl->p_tls != NULL )
1390 tls_ServerSessionClose( cl->p_tls );
1391 net_Close( cl->fd );
1395 httpd_MsgClean( &cl->answer );
1396 httpd_MsgClean( &cl->query );
1400 free( cl->p_buffer );
1401 cl->p_buffer = NULL;
1405 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1407 tls_session_t *p_tls )
1409 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1412 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1413 cl->i_sock_size = i_sock_size;
1417 httpd_ClientInit( cl );
1423 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1425 tls_session_t *p_tls;
1429 return tls_Recv( p_tls, p, i_len );
1431 return recv( cl->fd, p, i_len, 0 );
1435 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1437 tls_session_t *p_tls;
1441 return tls_Send( p_tls, p, i_len );
1443 return send( cl->fd, p, i_len, 0 );
1447 static void httpd_ClientRecv( httpd_client_t *cl )
1451 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1453 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1454 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1458 cl->i_buffer += i_len;
1461 if( cl->i_buffer >= 4 )
1463 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1465 if( cl->p_buffer[0] == '$' )
1467 /* RTSP (rtp over rtsp) */
1468 cl->query.i_proto = HTTPD_PROTO_RTSP;
1469 cl->query.i_type = HTTPD_MSG_CHANNEL;
1470 cl->query.i_channel = cl->p_buffer[1];
1471 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1472 cl->query.p_body = malloc( cl->query.i_body );
1476 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1478 cl->query.i_proto = HTTPD_PROTO_HTTP;
1479 cl->query.i_type = HTTPD_MSG_ANSWER;
1481 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1483 cl->query.i_proto = HTTPD_PROTO_RTSP;
1484 cl->query.i_type = HTTPD_MSG_ANSWER;
1486 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1487 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1488 !strncmp( cl->p_buffer, "POST", 4 ) )
1490 cl->query.i_proto = HTTPD_PROTO_HTTP;
1491 cl->query.i_type = HTTPD_MSG_NONE;
1495 cl->query.i_proto = HTTPD_PROTO_RTSP;
1496 cl->query.i_type = HTTPD_MSG_NONE;
1500 else if( cl->query.i_body > 0 )
1502 /* we are reading the body of a request or a channel */
1503 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1504 cl->query.i_body - cl->i_buffer );
1507 cl->i_buffer += i_len;
1509 if( cl->i_buffer >= cl->query.i_body )
1511 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1516 /* we are reading a header -> char by char */
1519 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1526 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1528 cl->i_buffer_size += 1024;
1529 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1531 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1532 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1536 /* we have finished the header so parse it and set i_body */
1537 cl->p_buffer[cl->i_buffer] = '\0';
1539 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1541 cl->query.i_status =
1542 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1547 cl->query.psz_status = strdup( p );
1559 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1560 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1561 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1563 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1564 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1565 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1566 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1567 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1568 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1570 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1575 cl->query.i_type = HTTPD_MSG_NONE;
1577 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1579 for( i = 0; msg_type[i].name != NULL; i++ )
1581 if( !strncmp( cl->p_buffer, msg_type[i].name,
1582 strlen( msg_type[i].name ) ) )
1584 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1585 cl->query.i_type = msg_type[i].i_type;
1586 if( cl->query.i_proto != msg_type[i].i_proto )
1589 cl->query.i_proto = HTTPD_PROTO_NONE;
1590 cl->query.i_type = HTTPD_MSG_NONE;
1597 if( strstr( cl->p_buffer, "HTTP/1." ) )
1599 cl->query.i_proto = HTTPD_PROTO_HTTP;
1601 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1603 cl->query.i_proto = HTTPD_PROTO_RTSP;
1615 p2 = strchr( p, ' ' );
1620 if( !strncasecmp( p, "rtsp:", 5 ) )
1622 /* for rtsp url, you have rtsp://localhost:port/path */
1624 while( *p == '/' ) p++;
1625 while( *p && *p != '/' ) p++;
1627 cl->query.psz_url = strdup( p );
1628 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1631 cl->query.psz_args = strdup( p3 );
1639 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1641 cl->query.i_proto = HTTPD_PROTO_HTTP;
1642 cl->query.i_version = atoi( p2+7 );
1644 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1646 cl->query.i_proto = HTTPD_PROTO_RTSP;
1647 cl->query.i_version = atoi( p2+7 );
1655 p = strchr( p, '\n' );
1659 while( *p == '\n' || *p == '\r' )
1663 while( p && *p != '\0' )
1666 char *eol = p = strchr( p, '\n' );
1669 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1674 if( ( colon = strchr( line, ':' ) ) )
1680 while( *colon == ' ' )
1684 name = strdup( line );
1685 value = strdup( colon );
1687 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1688 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1690 if( !strcasecmp( name, "Content-Length" ) )
1692 cl->query.i_body = atol( value );
1699 while( *p == '\n' || *p == '\r' )
1706 if( cl->query.i_body > 0 )
1708 /* TODO Mhh, handle the case client will only send a request and close the connection
1709 * to mark and of body (probably only RTSP) */
1710 cl->query.p_body = malloc( cl->query.i_body );
1715 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1721 /* check if the client is to be set to dead */
1722 #if defined( WIN32 ) || defined( UNDER_CE )
1723 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1725 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1728 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1730 /* connection closed -> end of data */
1731 if( cl->query.i_body > 0 )
1733 cl->query.i_body = cl->i_buffer;
1735 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1739 cl->i_state = HTTPD_CLIENT_DEAD;
1742 cl->i_activity_date = mdate();
1744 /* XXX: for QT I have to disable timeout. Try to find why */
1745 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1746 cl->i_activity_timeout = 0;
1748 /* Debugging only */
1749 /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1753 fprintf( stderr, "received new request\n" );
1754 fprintf( stderr, " - proto=%s\n",
1755 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1756 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1757 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1758 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1760 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1761 cl->query.psz_status );
1763 else if( cl->query.i_type != HTTPD_MSG_NONE )
1765 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1767 for( i = 0; i < cl->query.i_name; i++ )
1769 fprintf( stderr, " - option name='%s' value='%s'\n",
1770 cl->query.name[i], cl->query.value[i] );
1776 static void httpd_ClientSend( httpd_client_t *cl )
1781 if( cl->i_buffer < 0 )
1783 /* We need to create the header */
1787 i_size = strlen( "HTTP/1.") + 10 + 10 +
1788 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1789 for( i = 0; i < cl->answer.i_name; i++ )
1791 i_size += strlen( cl->answer.name[i] ) + 2 +
1792 strlen( cl->answer.value[i] ) + 2;
1795 if( cl->i_buffer_size < i_size )
1797 cl->i_buffer_size = i_size;
1798 free( cl->p_buffer );
1799 cl->p_buffer = malloc( i_size );
1803 p += sprintf( p, "%s/1.%d %d %s\r\n",
1804 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1805 cl->answer.i_version,
1806 cl->answer.i_status, cl->answer.psz_status );
1807 for( i = 0; i < cl->answer.i_name; i++ )
1809 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1810 cl->answer.value[i] );
1812 p += sprintf( p, "\r\n" );
1815 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1817 /*fprintf( stderr, "sending answer\n" );
1818 fprintf( stderr, "%s", cl->p_buffer );*/
1821 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1822 cl->i_buffer_size - cl->i_buffer );
1825 cl->i_activity_date = mdate();
1826 cl->i_buffer += i_len;
1828 if( cl->i_buffer >= cl->i_buffer_size )
1830 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1831 !cl->b_read_waiting )
1833 /* catch more body data */
1834 int i_msg = cl->query.i_type;
1835 int64_t i_offset = cl->answer.i_body_offset;
1837 httpd_MsgClean( &cl->answer );
1838 cl->answer.i_body_offset = i_offset;
1840 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1841 &cl->answer, &cl->query );
1844 if( cl->answer.i_body > 0 )
1846 /* send the body data */
1847 free( cl->p_buffer );
1848 cl->p_buffer = cl->answer.p_body;
1849 cl->i_buffer_size = cl->answer.i_body;
1852 cl->answer.i_body = 0;
1853 cl->answer.p_body = NULL;
1858 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1864 #if defined( WIN32 ) || defined( UNDER_CE )
1865 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1867 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1871 cl->i_state = HTTPD_CLIENT_DEAD;
1876 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1878 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1881 cl->i_state = HTTPD_CLIENT_RECEIVING;
1885 cl->i_state = HTTPD_CLIENT_DEAD;
1890 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1894 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1896 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1899 cl->i_state = HTTPD_CLIENT_RECEIVING;
1903 cl->i_state = HTTPD_CLIENT_DEAD;
1908 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1913 static void httpd_HostThread( httpd_host_t *host )
1915 tls_session_t *p_tls = NULL;
1917 while( !host->b_die )
1919 struct timeval timeout;
1922 /* FIXME: (too) many int variables */
1924 int i_handle_max = 0;
1927 int b_low_delay = 0;
1929 if( host->i_url <= 0 )
1936 /* built a set of handle to select */
1937 FD_ZERO( &fds_read );
1938 FD_ZERO( &fds_write );
1942 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1944 FD_SET( fd, &fds_read );
1945 if( fd > i_handle_max )
1949 /* prepare a new TLS session */
1950 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1951 p_tls = tls_ServerSessionPrepare( host->p_tls );
1953 /* add all socket that should be read/write and close dead connection */
1954 vlc_mutex_lock( &host->lock );
1955 for( i_client = 0; i_client < host->i_client; i_client++ )
1957 httpd_client_t *cl = host->client[i_client];
1959 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1960 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1961 ( cl->i_activity_timeout > 0 &&
1962 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1966 // FIXME: it sucks to allocate memory on the stack for debug
1967 ip = httpd_ClientIP( cl );
1968 msg_Dbg( host, "connection closed(%s)",
1969 (ip != NULL) ? ip : "unknown" );
1972 httpd_ClientClean( cl );
1973 TAB_REMOVE( host->i_client, host->client, cl );
1978 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1979 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1981 FD_SET( cl->fd, &fds_read );
1982 i_handle_max = __MAX( i_handle_max, cl->fd );
1984 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1985 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1987 FD_SET( cl->fd, &fds_write );
1988 i_handle_max = __MAX( i_handle_max, cl->fd );
1990 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1992 httpd_message_t *answer = &cl->answer;
1993 httpd_message_t *query = &cl->query;
1994 int i_msg = query->i_type;
1996 httpd_MsgInit( answer );
1998 /* Handle what we received */
1999 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2000 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2002 /* we can only receive request from client when not
2005 cl->i_state = HTTPD_CLIENT_DEAD;
2007 else if( i_msg == HTTPD_MSG_ANSWER )
2009 /* We are in BIDIR mode, trigger the callback and then
2010 * check for new data */
2011 if( cl->url && cl->url->catch[i_msg].cb )
2013 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2016 cl->i_state = HTTPD_CLIENT_WAITING;
2018 else if( i_msg == HTTPD_MSG_CHANNEL )
2020 /* We are in BIDIR mode, trigger the callback and then
2021 * check for new data */
2022 if( cl->url && cl->url->catch[i_msg].cb )
2024 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2027 cl->i_state = HTTPD_CLIENT_WAITING;
2029 else if( i_msg == HTTPD_MSG_OPTIONS )
2034 answer->i_proto = query->i_proto ;
2035 answer->i_type = HTTPD_MSG_ANSWER;
2036 answer->i_version= 0;
2037 answer->i_status = 200;
2038 answer->psz_status = strdup( "Ok" );
2041 answer->p_body = NULL;
2043 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2044 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2045 httpd_MsgAdd( answer, "Server", "VLC Server" );
2046 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2047 "TEARDOWN, PLAY, PAUSE" );
2048 httpd_MsgAdd( answer, "Content-Length", "%d",
2051 cl->i_buffer = -1; /* Force the creation of the answer in
2052 * httpd_ClientSend */
2053 cl->i_state = HTTPD_CLIENT_SENDING;
2055 else if( i_msg == HTTPD_MSG_NONE )
2057 if( query->i_proto == HTTPD_PROTO_NONE )
2060 cl->i_state = HTTPD_CLIENT_DEAD;
2067 answer->i_proto = query->i_proto ;
2068 answer->i_type = HTTPD_MSG_ANSWER;
2069 answer->i_version= 0;
2070 answer->i_status = 501;
2071 answer->psz_status = strdup( "Unimplemented" );
2073 p = answer->p_body = malloc( 1000 );
2075 p += sprintf( p, "<html>\n" );
2076 p += sprintf( p, "<head>\n" );
2077 p += sprintf( p, "<title>Error 501</title>\n" );
2078 p += sprintf( p, "</head>\n" );
2079 p += sprintf( p, "<body>\n" );
2080 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2081 p += sprintf( p, "<hr />\n" );
2082 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2083 p += sprintf( p, "</body>\n" );
2084 p += sprintf( p, "</html>\n" );
2086 answer->i_body = p - answer->p_body;
2087 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2089 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2090 cl->i_state = HTTPD_CLIENT_SENDING;
2095 vlc_bool_t b_auth_failed = VLC_FALSE;
2096 vlc_bool_t b_hosts_failed = VLC_FALSE;
2099 /* Search the url and trigger callbacks */
2100 for( i = 0; i < host->i_url; i++ )
2102 httpd_url_t *url = host->url[i];
2104 if( !strcmp( url->psz_url, query->psz_url ) )
2106 if( url->catch[i_msg].cb )
2108 if( answer && url->i_hosts )
2110 char *ip = httpd_ClientIP( cl );
2113 if( net_CheckIP( host, ip,
2115 url->i_hosts ) <= 0 )
2117 b_hosts_failed = VLC_TRUE;
2125 if( answer && ( *url->psz_user || *url->psz_password ) )
2127 /* create the headers */
2128 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2132 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2133 auth = malloc( strlen(b64) + 1 );
2135 if( !strncasecmp( b64, "BASIC", 5 ) )
2138 while( *b64 == ' ' )
2142 b64_decode( auth, b64 );
2148 if( strcmp( id, auth ) )
2150 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2151 /* We fail for all url */
2152 b_auth_failed = VLC_TRUE;
2162 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2164 /* only one url can answer */
2166 if( cl->url == NULL )
2178 answer->i_proto = query->i_proto;
2179 answer->i_type = HTTPD_MSG_ANSWER;
2180 answer->i_version= 0;
2181 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2183 if( b_hosts_failed )
2185 answer->i_status = 403;
2186 answer->psz_status = strdup( "Forbidden" );
2188 p += sprintf( p, "<html>\n" );
2189 p += sprintf( p, "<head>\n" );
2190 p += sprintf( p, "<title>Error 403</title>\n" );
2191 p += sprintf( p, "</head>\n" );
2192 p += sprintf( p, "<body>\n" );
2193 p += sprintf( p, "<h1><center> 403 Forbidden (%s)</center></h1>\n", query->psz_url );
2194 p += sprintf( p, "<hr />\n" );
2195 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2196 p += sprintf( p, "</body>\n" );
2197 p += sprintf( p, "</html>\n" );
2199 else if( b_auth_failed )
2201 answer->i_status = 401;
2202 answer->psz_status = strdup( "Authorization Required" );
2204 p += sprintf( p, "<html>\n" );
2205 p += sprintf( p, "<head>\n" );
2206 p += sprintf( p, "<title>Error 401</title>\n" );
2207 p += sprintf( p, "</head>\n" );
2208 p += sprintf( p, "<body>\n" );
2209 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2210 p += sprintf( p, "<hr />\n" );
2211 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2212 p += sprintf( p, "</body>\n" );
2213 p += sprintf( p, "</html>\n" );
2217 /* no url registered */
2218 answer->i_status = 404;
2219 answer->psz_status = strdup( "Not found" );
2221 p += sprintf( p, "<html>\n" );
2222 p += sprintf( p, "<head>\n" );
2223 p += sprintf( p, "<title>Error 404</title>\n" );
2224 p += sprintf( p, "</head>\n" );
2225 p += sprintf( p, "<body>\n" );
2226 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2227 p += sprintf( p, "<hr />\n" );
2228 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2229 p += sprintf( p, "</body>\n" );
2230 p += sprintf( p, "</html>\n" );
2233 answer->i_body = p - answer->p_body;
2234 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2236 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2237 cl->i_state = HTTPD_CLIENT_SENDING;
2240 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2242 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2245 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2246 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2247 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2248 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2249 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2250 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2252 httpd_MsgClean( &cl->query );
2253 httpd_MsgInit( &cl->query );
2256 cl->i_buffer_size = 1000;
2257 free( cl->p_buffer );
2258 cl->p_buffer = malloc( cl->i_buffer_size );
2259 cl->i_state = HTTPD_CLIENT_RECEIVING;
2263 cl->i_state = HTTPD_CLIENT_DEAD;
2265 httpd_MsgClean( &cl->answer );
2267 else if( cl->b_read_waiting )
2269 /* we have a message waiting for us to read it */
2270 httpd_MsgClean( &cl->answer );
2271 httpd_MsgClean( &cl->query );
2274 cl->i_buffer_size = 1000;
2275 free( cl->p_buffer );
2276 cl->p_buffer = malloc( cl->i_buffer_size );
2277 cl->i_state = HTTPD_CLIENT_RECEIVING;
2278 cl->b_read_waiting = VLC_FALSE;
2282 int64_t i_offset = cl->answer.i_body_offset;
2283 httpd_MsgClean( &cl->answer );
2285 cl->answer.i_body_offset = i_offset;
2286 free( cl->p_buffer );
2287 cl->p_buffer = NULL;
2289 cl->i_buffer_size = 0;
2291 cl->i_state = HTTPD_CLIENT_WAITING;
2294 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2296 int64_t i_offset = cl->answer.i_body_offset;
2297 int i_msg = cl->query.i_type;
2299 httpd_MsgInit( &cl->answer );
2300 cl->answer.i_body_offset = i_offset;
2302 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2303 &cl->answer, &cl->query );
2304 if( cl->answer.i_type != HTTPD_MSG_NONE )
2306 /* we have new data, so reenter send mode */
2308 cl->p_buffer = cl->answer.p_body;
2309 cl->i_buffer_size = cl->answer.i_body;
2310 cl->answer.p_body = NULL;
2311 cl->answer.i_body = 0;
2312 cl->i_state = HTTPD_CLIENT_SENDING;
2316 /* we shouldn't wait too long */
2317 b_low_delay = VLC_TRUE;
2321 /* Special for BIDIR mode we also check reading */
2322 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2323 cl->i_state == HTTPD_CLIENT_SENDING )
2325 FD_SET( cl->fd, &fds_read );
2326 i_handle_max = __MAX( i_handle_max, cl->fd );
2329 vlc_mutex_unlock( &host->lock );
2331 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2333 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2335 i_ret = select( i_handle_max + 1,
2336 &fds_read, &fds_write, NULL, &timeout );
2338 if( i_ret == -1 && errno != EINTR )
2340 msg_Warn( host, "cannot select sockets" );
2344 else if( i_ret <= 0 )
2349 /* accept new connections */
2350 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2352 if( FD_ISSET( fd, &fds_read ) )
2354 int i_sock_size = sizeof( struct sockaddr_storage );
2355 struct sockaddr_storage sock;
2357 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2362 /* set this new socket non-block */
2363 #if defined( WIN32 ) || defined( UNDER_CE )
2365 unsigned long i_dummy = 1;
2366 ioctlsocket( fd, FIONBIO, &i_dummy );
2369 fcntl( fd, F_SETFL, O_NONBLOCK );
2374 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2377 msg_Err( host, "Rejecting TLS connection" );
2383 case 1: /* missing input - most likely */
2384 i_state = HTTPD_CLIENT_TLS_HS_IN;
2387 case 2: /* missing output */
2388 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2398 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2400 vlc_mutex_lock( &host->lock );
2401 TAB_APPEND( host->i_client, host->client, cl );
2402 vlc_mutex_unlock( &host->lock );
2405 cl->i_state = i_state; // override state for TLS
2407 // FIXME: it sucks to allocate memory for debug
2408 ip = httpd_ClientIP( cl );
2409 msg_Dbg( host, "new connection (%s)",
2410 ip != NULL ? ip : "unknown" );
2418 /* now try all others socket */
2419 vlc_mutex_lock( &host->lock );
2420 for( i_client = 0; i_client < host->i_client; i_client++ )
2422 httpd_client_t *cl = host->client[i_client];
2423 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2425 httpd_ClientRecv( cl );
2427 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2429 httpd_ClientSend( cl );
2431 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2433 httpd_ClientTlsHsIn( cl );
2435 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2437 httpd_ClientTlsHsOut( cl );
2440 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2441 cl->i_state == HTTPD_CLIENT_SENDING &&
2442 FD_ISSET( cl->fd, &fds_read ) )
2444 cl->b_read_waiting = VLC_TRUE;
2447 vlc_mutex_unlock( &host->lock );
2451 tls_ServerSessionClose( p_tls );
2454 #else /* ENABLE_HTTPD */
2456 /* We just define an empty wrapper */
2457 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2460 msg_Err( a, "HTTP daemon support is disabled" );
2463 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2465 msg_Err( a, "HTTP daemon support is disabled" );
2468 void httpd_HostDelete( httpd_host_t *a ){}
2469 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2470 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2471 char *d ){ return 0; }
2472 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2473 httpd_callback_sys_t *d ){ return 0; }
2474 void httpd_UrlDelete( httpd_url_t *a ){}
2476 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2477 void httpd_ClientModeStream( httpd_client_t *a ){}
2478 void httpd_ClientModeBidir( httpd_client_t *a ){}
2480 void httpd_FileDelete( httpd_file_t *a ){}
2481 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2482 char *e, httpd_file_callback_t f,
2483 httpd_file_sys_t *g ){ return 0; }
2485 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2486 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2487 char *b, char *c ){ return 0; }
2489 void httpd_StreamDelete( httpd_stream_t *a ){}
2490 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2491 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2492 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2493 char *d, char *e ){ return 0; }
2495 void httpd_MsgInit ( httpd_message_t *a ){}
2496 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2497 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2498 void httpd_MsgClean( httpd_message_t *a ){}
2500 #endif /* ENABLE_HTTPD */