1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Remi Denis-Courmont <courmisch@via.ecp.fr>
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 *****************************************************************************/
28 #include "vlc_httpd.h"
38 #if defined( UNDER_CE )
40 #elif defined( WIN32 )
41 # include <winsock2.h>
42 # include <ws2tcpip.h>
44 # include <netdb.h> /* hostent ... */
45 # include <sys/socket.h>
46 # include <netinet/in.h>
47 # ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
53 typedef struct httpd_t httpd_t;
55 typedef struct httpd_host_t httpd_host_t;
56 typedef struct httpd_url_t httpd_url_t;
57 typedef struct httpd_client_t httpd_client_t;
66 /* channel communication */
82 /* just to track the count of MSG */
95 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
107 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
109 /* for rtp over rtsp */
119 int64_t i_body_offset;
125 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
126 /* answer could be null, int this case no answer is requested */
127 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
130 /* create a new host */
131 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
133 void httpd_HostDelete( httpd_host_t * );
135 /* register a new url */
136 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
137 /* register callback on a url */
138 int httpd_UrlCatch( httpd_url_t *, int i_msg,
139 httpd_callback_t, httpd_callback_sys_t * );
141 void httpd_UrlDelete( httpd_url_t * );
144 void httpd_ClientModeStream( httpd_client_t *cl );
145 void httpd_ClientModeBidir( httpd_client_t *cl );
146 static void httpd_ClientClean( httpd_client_t *cl );
149 typedef struct httpd_file_t httpd_file_t;
150 typedef struct httpd_file_sys_t httpd_file_sys_t;
151 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
152 httpd_file_t *httpd_FileNew( httpd_host_t *,
153 char *psz_url, char *psz_mime,
154 char *psz_user, char *psz_password,
155 httpd_file_callback_t pf_fill,
156 httpd_file_sys_t *p_sys );
157 void httpd_FileDelete( httpd_file_t * );
159 typedef struct httpd_redirect_t httpd_redirect_t;
160 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
161 void httpd_RedirectDelete( httpd_redirect_t * );
164 typedef struct httpd_stream_t httpd_stream_t;
165 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
166 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
167 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
168 void httpd_StreamDelete( httpd_stream_t * );
171 /* Msg functions facilities */
172 void httpd_MsgInit( httpd_message_t * );
173 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
174 /* return "" if not found. The string is not allocated */
175 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
176 void httpd_MsgClean( httpd_message_t * );
184 /* vlc_mutex_t lock; */
190 static void httpd_ClientClean( httpd_client_t *cl );
192 /* each host run in his own thread */
202 /* address/port and socket for listening at connections */
203 struct sockaddr_storage sock;
209 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
210 * This will slow down the url research but make my live easier
211 * All url will have their cb trigger, but only the first one can answer
217 httpd_client_t **client;
233 httpd_callback_sys_t *p_sys;
234 } catch[HTTPD_MSG_MAX];
240 HTTPD_CLIENT_RECEIVING,
241 HTTPD_CLIENT_RECEIVE_DONE,
243 HTTPD_CLIENT_SENDING,
244 HTTPD_CLIENT_SEND_DONE,
246 HTTPD_CLIENT_WAITING,
253 HTTPD_CLIENT_FILE, /* default */
254 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
255 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
258 struct httpd_client_t
264 struct sockaddr_storage sock;
270 int b_read_waiting; /* stop as soon as possible sending */
272 mtime_t i_activity_date;
273 mtime_t i_activity_timeout;
275 /* buffer for reading header */
281 httpd_message_t query; /* client -> httpd */
282 httpd_message_t answer; /* httpd -> client */
286 /*****************************************************************************
288 *****************************************************************************/
289 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
290 static void b64_decode( char *dest, char *src )
295 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
296 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
297 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
298 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
299 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
300 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
301 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
302 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
303 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
304 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
305 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
306 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
307 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
308 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
309 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
310 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
313 for( i_level = 0; *src != '\0'; src++ )
317 c = b64[(unsigned int)*src];
329 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
333 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
337 *dest++ = ( ( last &0x03 ) << 6 ) | c;
352 { ".htm", "text/html" },
353 { ".html", "text/html" },
354 { ".txt", "text/plain" },
355 { ".xml", "text/xml" },
356 { ".dtd", "text/dtd" },
358 { ".css", "text/css" },
361 { ".gif", "image/gif" },
362 { ".jpe", "image/jpeg" },
363 { ".jpg", "image/jpeg" },
364 { ".jpeg", "image/jpeg" },
365 { ".png", "image/png" },
368 { ".avi", "video/avi" },
369 { ".asf", "video/x-ms-asf" },
370 { ".m1a", "audio/mpeg" },
371 { ".m2a", "audio/mpeg" },
372 { ".m1v", "video/mpeg" },
373 { ".m2v", "video/mpeg" },
374 { ".mp2", "audio/mpeg" },
375 { ".mp3", "audio/mpeg" },
376 { ".mpa", "audio/mpeg" },
377 { ".mpg", "video/mpeg" },
378 { ".mpeg", "video/mpeg" },
379 { ".mpe", "video/mpeg" },
380 { ".mov", "video/quicktime" },
381 { ".moov", "video/quicktime" },
382 { ".ogg", "application/ogg" },
383 { ".ogm", "application/ogg" },
384 { ".wav", "audio/wav" },
385 { ".wma", "audio/x-ms-wma" },
386 { ".wmv", "video/x-ms-wmv" },
392 static char *httpd_MimeFromUrl( char *psz_url )
397 psz_ext = strrchr( psz_url, '.' );
402 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
404 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
406 return http_mime[i].psz_mime;
410 return "application/octet-stream";
413 /*****************************************************************************
414 * High Level Funtions: httpd_file_t
415 *****************************************************************************/
423 httpd_file_callback_t pf_fill;
424 httpd_file_sys_t *p_sys;
429 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
431 httpd_file_t *file = (httpd_file_t*)p_sys;
433 if( answer == NULL || query == NULL )
437 answer->i_proto = HTTPD_PROTO_HTTP;
438 answer->i_version= query->i_version;
439 answer->i_type = HTTPD_MSG_ANSWER;
441 answer->i_status = 200;
442 answer->psz_status = strdup( "OK" );
444 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
445 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
447 if( query->i_type != HTTPD_MSG_HEAD )
449 char *psz_args = query->psz_args;
450 if( query->i_type == HTTPD_MSG_POST )
453 psz_args = query->p_body;
455 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
458 /* We respect client request */
459 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
461 httpd_MsgAdd( answer, "Connection",
462 httpd_MsgGet( &cl->query, "Connection" ) );
465 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
471 httpd_file_t *httpd_FileNew( httpd_host_t *host,
472 char *psz_url, char *psz_mime,
473 char *psz_user, char *psz_password,
474 httpd_file_callback_t pf_fill,
475 httpd_file_sys_t *p_sys )
477 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
479 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
480 psz_password ) ) == NULL )
486 file->psz_url = strdup( psz_url );
487 if( psz_mime && *psz_mime )
489 file->psz_mime = strdup( psz_mime );
493 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
496 file->pf_fill = pf_fill;
499 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
500 (httpd_callback_sys_t*)file );
501 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
502 (httpd_callback_sys_t*)file );
503 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
504 (httpd_callback_sys_t*)file );
509 void httpd_FileDelete( httpd_file_t *file )
511 httpd_UrlDelete( file->url );
513 free( file->psz_url );
514 free( file->psz_mime );
519 /*****************************************************************************
520 * High Level Funtions: httpd_redirect_t
521 *****************************************************************************/
522 struct httpd_redirect_t
528 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
529 httpd_client_t *cl, httpd_message_t *answer,
530 httpd_message_t *query )
532 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
535 if( answer == NULL || query == NULL )
539 answer->i_proto = query->i_proto;
540 answer->i_version= query->i_version;
541 answer->i_type = HTTPD_MSG_ANSWER;
542 answer->i_status = 301;
543 answer->psz_status = strdup( "Moved Permanently" );
545 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
546 p += sprintf( p, "<html>\n" );
547 p += sprintf( p, "<head>\n" );
548 p += sprintf( p, "<title>Redirection</title>\n" );
549 p += sprintf( p, "</head>\n" );
550 p += sprintf( p, "<body>\n" );
551 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
552 p += sprintf( p, "<hr />\n" );
553 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
554 p += sprintf( p, "</body>\n" );
555 p += sprintf( p, "</html>\n" );
556 answer->i_body = p - answer->p_body;
558 /* XXX check if it's ok or we need to set an absolute url */
559 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
561 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
566 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
569 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
571 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) )
576 rdir->psz_dst = strdup( psz_url_dst );
578 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
579 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
580 (httpd_callback_sys_t*)rdir );
581 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
582 (httpd_callback_sys_t*)rdir );
583 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
584 (httpd_callback_sys_t*)rdir );
585 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
586 (httpd_callback_sys_t*)rdir );
590 void httpd_RedirectDelete( httpd_redirect_t *rdir )
592 httpd_UrlDelete( rdir->url );
593 free( rdir->psz_dst );
597 /*****************************************************************************
598 * High Level Funtions: httpd_stream_t
599 *****************************************************************************/
600 struct httpd_stream_t
607 /* Header to send as first packet */
611 /* circular buffer */
612 int i_buffer_size; /* buffer size, can't be reallocated smaller */
613 uint8_t *p_buffer; /* buffer */
614 int64_t i_buffer_pos; /* absolute position from begining */
615 int64_t i_buffer_last_pos; /* a new connection will start with that */
618 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
619 httpd_client_t *cl, httpd_message_t *answer,
620 httpd_message_t *query )
622 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
624 if( answer == NULL || query == NULL || cl == NULL )
628 if( answer->i_body_offset > 0 )
634 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
635 answer->i_body_offset );
638 if( answer->i_body_offset >= stream->i_buffer_pos )
640 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
641 return VLC_EGENERIC; /* wait, no data available */
643 if( answer->i_body_offset + stream->i_buffer_size <
644 stream->i_buffer_pos )
646 /* this client isn't fast enough */
647 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
648 answer->i_body_offset, stream->i_buffer_last_pos );
649 answer->i_body_offset = stream->i_buffer_last_pos;
652 i_pos = answer->i_body_offset % stream->i_buffer_size;
653 i_write = stream->i_buffer_pos - answer->i_body_offset;
654 if( i_write > 10000 )
658 else if( i_write <= 0 )
660 return VLC_EGENERIC; /* wait, no data available */
663 /* Don't go past the end of the circular buffer */
664 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
666 /* using HTTPD_MSG_ANSWER -> data available */
667 answer->i_proto = HTTPD_PROTO_HTTP;
668 answer->i_version= 0;
669 answer->i_type = HTTPD_MSG_ANSWER;
671 answer->i_body = i_write;
672 answer->p_body = malloc( i_write );
673 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
675 answer->i_body_offset += i_write;
681 answer->i_proto = HTTPD_PROTO_HTTP;
682 answer->i_version= 0;
683 answer->i_type = HTTPD_MSG_ANSWER;
685 answer->i_status = 200;
686 answer->psz_status = strdup( "OK" );
688 if( query->i_type != HTTPD_MSG_HEAD )
690 httpd_ClientModeStream( cl );
691 vlc_mutex_lock( &stream->lock );
692 /* Send the header */
693 if( stream->i_header > 0 )
695 answer->i_body = stream->i_header;
696 answer->p_body = malloc( stream->i_header );
697 memcpy( answer->p_body, stream->p_header, stream->i_header );
699 answer->i_body_offset = stream->i_buffer_last_pos;
700 vlc_mutex_unlock( &stream->lock );
704 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
705 answer->i_body_offset = 0;
708 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
710 vlc_bool_t b_xplaystream = VLC_FALSE;
713 httpd_MsgAdd( answer, "Content-type", "%s",
714 "application/octet-stream" );
715 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
716 httpd_MsgAdd( answer, "Pragma", "no-cache" );
717 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
718 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
720 /* Check if there is a xPlayStrm=1 */
721 for( i = 0; i < query->i_name; i++ )
723 if( !strcasecmp( query->name[i], "Pragma" ) &&
724 strstr( query->value[i], "xPlayStrm=1" ) )
726 b_xplaystream = VLC_TRUE;
732 answer->i_body_offset = 0;
737 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
739 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
744 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
745 char *psz_url, char *psz_mime,
746 char *psz_user, char *psz_password )
748 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
750 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
751 psz_password ) ) == NULL )
756 vlc_mutex_init( host, &stream->lock );
757 if( psz_mime && *psz_mime )
759 stream->psz_mime = strdup( psz_mime );
763 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
765 stream->i_header = 0;
766 stream->p_header = NULL;
767 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
768 stream->p_buffer = malloc( stream->i_buffer_size );
769 /* We set to 1 to make life simpler
770 * (this way i_body_offset can never be 0) */
771 stream->i_buffer_pos = 1;
772 stream->i_buffer_last_pos = 1;
774 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
775 (httpd_callback_sys_t*)stream );
776 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
777 (httpd_callback_sys_t*)stream );
778 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
779 (httpd_callback_sys_t*)stream );
784 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
786 vlc_mutex_lock( &stream->lock );
787 if( stream->p_header )
789 free( stream->p_header );
790 stream->p_header = NULL;
792 stream->i_header = i_data;
795 stream->p_header = malloc( i_data );
796 memcpy( stream->p_header, p_data, i_data );
798 vlc_mutex_unlock( &stream->lock );
803 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
808 if( i_data < 0 || p_data == NULL )
812 vlc_mutex_lock( &stream->lock );
814 /* save this pointer (to be used by new connection) */
815 stream->i_buffer_last_pos = stream->i_buffer_pos;
817 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
823 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
825 /* Ok, we can't go past the end of our buffer */
826 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
828 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
833 stream->i_buffer_pos += i_data;
835 vlc_mutex_unlock( &stream->lock );
839 void httpd_StreamDelete( httpd_stream_t *stream )
841 httpd_UrlDelete( stream->url );
842 vlc_mutex_destroy( &stream->lock );
843 if( stream->psz_mime ) free( stream->psz_mime );
844 if( stream->p_header ) free( stream->p_header );
845 if( stream->p_buffer ) free( stream->p_buffer );
850 /*****************************************************************************
852 *****************************************************************************/
853 #define LISTEN_BACKLOG 100
855 #if defined(HAVE_GETNAMEINFO) && !defined(HAVE_GETADDRINFO)
857 * For now, VLC's configure script does not check for getaddrinfo(),
858 * but it should be present if getnameinfo() is (the opposite is untrue, with
859 * Debian potato as an example)
861 # define HAVE_GETADDRINFO 1
864 static void httpd_HostThread( httpd_host_t * );
865 static int GetAddrPort( const struct sockaddr_storage *p_ss );
867 #ifndef HAVE_GETADDRINFO
868 struct httpd_addrinfo
874 struct sockaddr *ai_addr;
876 struct httpd_addrinfo *ai_next;
878 # define addrinfo httpd_addrinfo
880 static int BuildAddr( struct sockaddr_in * p_socket,
881 const char * psz_address, int i_port );
885 /* create a new host */
886 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
889 httpd_host_t *host = NULL;
892 struct addrinfo *res, *ptr;
895 #ifdef HAVE_GETADDRINFO
899 struct addrinfo hints;
902 memset( &hints, 0, sizeof( hints ) );
903 /* Check if we have force ipv4 or ipv6 */
904 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
905 var_Get( p_this, "ipv4", &val );
907 hints.ai_family = PF_INET;
910 * For now, keep IPv4 by default. That said, it should be safe to use
911 * IPv6 by default *on the server side*, as, apart from NetBSD, most
912 * systems accept IPv4 clients on IPv6 listening sockets.
916 hints.ai_family = PF_INET;
918 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
919 var_Get( p_this, "ipv6", &val );
921 hints.ai_family = 0; // try IPv6, then IPv4
924 hints.ai_socktype = SOCK_STREAM;
925 hints.ai_flags = AI_PASSIVE;
927 if (*psz_host == '\0')
930 snprintf( psz_port, sizeof( psz_port ), "%d", i_port );
931 psz_port[sizeof( psz_port ) - 1] = '\0';
933 if( getaddrinfo( psz_host, psz_port, &hints, &res ) )
935 msg_Err( p_this, "cannot resolve %s:%d", psz_host, i_port );
940 struct sockaddr_in sock;
941 struct httpd_addrinfo info;
943 info.ai_family = PF_INET;
944 info.ai_socktype = SOCK_STREAM;
945 info.ai_protocol = 0;
946 info.ai_addr = (struct sockaddr *)&sock;
947 info.ai_addrlen = sizeof( sock );
952 if( BuildAddr( &sock, psz_host, i_port ) )
954 msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
958 # define freeaddrinfo( r ) (void)0;
961 /* to be sure to avoid multiple creation */
962 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
963 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
964 vlc_mutex_lock( lockval.p_address );
966 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
968 msg_Info( p_this, "creating httpd" );
969 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
971 vlc_mutex_unlock( lockval.p_address );
979 vlc_object_yield( httpd );
980 vlc_object_attach( httpd, p_this->p_vlc );
983 for( ptr = res; (ptr != NULL) && (fd == -1); ptr = ptr->ai_next )
987 if( ((unsigned)ptr->ai_addrlen) > sizeof( struct sockaddr_storage ) )
989 msg_Dbg( p_this, "socket address too big" );
993 /* verify if it already exist */
994 for( i = 0; i < httpd->i_host; i++ )
996 if (GetAddrPort (&httpd->host[i]->sock) != i_port)
1000 if( httpd->host[i]->sock.ss_family == AF_INET6 )
1002 const struct sockaddr_in6 *p_hsock, *p_sock;
1004 p_hsock = (const struct sockaddr_in6 *)&httpd->host[i]->sock;
1005 p_sock = (const struct sockaddr_in6 *)res->ai_addr;
1007 if( memcmp( &p_hsock->sin6_addr, &in6addr_any,
1008 sizeof( struct in6_addr ) ) &&
1009 ( p_sock->sin6_family != AF_INET6 ||
1010 memcmp( &p_hsock->sin6_addr, &p_sock->sin6_addr,
1011 sizeof( struct in6_addr ) ) ) )
1012 continue; /* does not match */
1015 if( res->ai_family == PF_INET6 )
1019 if( httpd->host[i]->sock.ss_family == AF_INET )
1021 const struct sockaddr_in *p_hsock, *p_sock;
1023 p_hsock = (const struct sockaddr_in *)&httpd->host[i]->sock;
1024 p_sock = (const struct sockaddr_in *)res->ai_addr;
1026 if( p_hsock->sin_addr.s_addr != INADDR_ANY &&
1027 ( p_sock->sin_family != AF_INET ||
1028 p_hsock->sin_addr.s_addr != p_sock->sin_addr.s_addr ) )
1029 continue; /* does not match */
1032 if( res->ai_family == PF_INET )
1036 msg_Dbg( p_this, "host with unknown address family" );
1040 freeaddrinfo( res );
1043 host = httpd->host[i];
1046 vlc_mutex_unlock( lockval.p_address );
1048 msg_Dbg( p_this, "host already registered" );
1052 /* create the listening socket */
1053 fd = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
1060 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
1061 (void *)&dummy, sizeof( dummy ) ) < 0 )
1063 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
1068 if( bind( fd, res->ai_addr, res->ai_addrlen ) )
1070 msg_Err( p_this, "cannot bind socket" );
1073 /* set to non-blocking */
1074 #if defined( WIN32 ) || defined( UNDER_CE )
1076 unsigned long i_dummy = 1;
1077 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
1079 msg_Err( p_this, "cannot set socket to non-blocking mode" );
1085 unsigned int i_flags;
1086 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
1088 msg_Err( p_this, "cannot F_GETFL socket" );
1091 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
1093 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
1099 if( listen( fd, LISTEN_BACKLOG ) < 0 )
1101 msg_Err( p_this, "cannot listen socket" );
1113 freeaddrinfo( res );
1118 /* create the new host */
1119 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1120 host->httpd = httpd;
1121 vlc_mutex_init( httpd, &host->lock );
1125 memcpy( &host->sock, res->ai_addr, res->ai_addrlen );
1126 host->i_sock_size = res->ai_addrlen;
1130 host->client = NULL;
1132 /* create the thread */
1133 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1134 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1136 msg_Err( p_this, "cannot spawn http host thread" );
1140 /* now add it to httpd */
1141 TAB_APPEND( httpd->i_host, httpd->host, host );
1142 vlc_mutex_unlock( lockval.p_address );
1147 vlc_mutex_unlock( lockval.p_address );
1154 vlc_mutex_destroy( &host->lock );
1155 vlc_object_destroy( host );
1158 /* TODO destroy no more used httpd TODO */
1159 vlc_object_release( httpd );
1164 void httpd_HostDelete( httpd_host_t *host )
1166 httpd_t *httpd = host->httpd;
1167 vlc_value_t lockval;
1170 msg_Dbg( host, "httpd_HostDelete" );
1172 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1173 vlc_mutex_lock( lockval.p_address );
1175 vlc_object_release( httpd );
1178 if( host->i_ref > 0 )
1181 vlc_mutex_unlock( lockval.p_address );
1182 msg_Dbg( host, "httpd_HostDelete: host still used" );
1185 TAB_REMOVE( httpd->i_host, httpd->host, host );
1187 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1190 vlc_thread_join( host );
1192 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1194 for( i = 0; i < host->i_url; i++ )
1196 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1198 for( i = 0; i < host->i_client; i++ )
1200 httpd_client_t *cl = host->client[i];
1201 msg_Warn( host, "client still connected" );
1202 httpd_ClientClean( cl );
1203 TAB_REMOVE( host->i_client, host->client, cl );
1209 net_Close( host->fd );
1210 vlc_mutex_destroy( &host->lock );
1211 vlc_object_destroy( host );
1213 if( httpd->i_host <= 0 )
1215 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1216 vlc_object_detach( httpd );
1217 vlc_object_destroy( httpd );
1219 vlc_mutex_unlock( lockval.p_address );
1222 /* register a new url */
1223 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1224 char *psz_user, char *psz_password,
1225 vlc_bool_t b_check )
1230 vlc_mutex_lock( &host->lock );
1233 for( i = 0; i < host->i_url; i++ )
1235 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1237 msg_Warn( host->httpd,
1238 "cannot add '%s' (url already defined)", psz_url );
1239 vlc_mutex_unlock( &host->lock );
1245 url = malloc( sizeof( httpd_url_t ) );
1248 vlc_mutex_init( host->httpd, &url->lock );
1249 url->psz_url = strdup( psz_url );
1250 url->psz_user = strdup( psz_user ? psz_user : "" );
1251 url->psz_password = strdup( psz_password ? psz_password : "" );
1252 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1254 url->catch[i].cb = NULL;
1255 url->catch[i].p_sys = NULL;
1258 TAB_APPEND( host->i_url, host->url, url );
1259 vlc_mutex_unlock( &host->lock );
1264 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1265 char *psz_user, char *psz_password )
1267 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1268 psz_password, VLC_FALSE );
1271 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1272 char *psz_user, char *psz_password )
1274 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1275 psz_password, VLC_TRUE );
1278 /* register callback on a url */
1279 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1280 httpd_callback_sys_t *p_sys )
1282 vlc_mutex_lock( &url->lock );
1283 url->catch[i_msg].cb = cb;
1284 url->catch[i_msg].p_sys= p_sys;
1285 vlc_mutex_unlock( &url->lock );
1292 void httpd_UrlDelete( httpd_url_t *url )
1294 httpd_host_t *host = url->host;
1297 vlc_mutex_lock( &host->lock );
1298 TAB_REMOVE( host->i_url, host->url, url );
1300 vlc_mutex_destroy( &url->lock );
1301 free( url->psz_url );
1302 free( url->psz_user );
1303 free( url->psz_password );
1305 for( i = 0; i < host->i_client; i++ )
1307 httpd_client_t *client = host->client[i];
1309 if( client->url == url )
1311 /* TODO complete it */
1312 msg_Warn( host, "force closing connections" );
1313 httpd_ClientClean( client );
1314 TAB_REMOVE( host->i_client, host->client, client );
1320 vlc_mutex_unlock( &host->lock );
1323 void httpd_MsgInit( httpd_message_t *msg )
1326 msg->i_type = HTTPD_MSG_NONE;
1327 msg->i_proto = HTTPD_PROTO_NONE;
1328 msg->i_version = -1;
1331 msg->psz_status = NULL;
1333 msg->psz_url = NULL;
1334 msg->psz_args = NULL;
1336 msg->i_channel = -1;
1343 msg->i_body_offset = 0;
1348 void httpd_MsgClean( httpd_message_t *msg )
1352 if( msg->psz_status )
1354 free( msg->psz_status );
1358 free( msg->psz_url );
1362 free( msg->psz_args );
1364 for( i = 0; i < msg->i_name; i++ )
1366 free( msg->name[i] );
1367 free( msg->value[i] );
1379 free( msg->p_body );
1381 httpd_MsgInit( msg );
1384 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1388 for( i = 0; i < msg->i_name; i++ )
1390 if( !strcasecmp( msg->name[i], name ))
1392 return msg->value[i];
1397 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1402 va_start( args, psz_value );
1403 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1404 vasprintf( &value, psz_value, args );
1407 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1408 value = calloc( i_size, sizeof( char ) );
1409 vsnprintf( value, i_size, psz_value, args );
1410 value[i_size - 1] = 0;
1415 name = strdup( name );
1417 TAB_APPEND( msg->i_name, msg->name, name );
1418 TAB_APPEND( msg->i_value, msg->value, value );
1421 static void httpd_ClientInit( httpd_client_t *cl )
1423 cl->i_state = HTTPD_CLIENT_RECEIVING;
1424 cl->i_activity_date = mdate();
1425 cl->i_activity_timeout = 50000000;
1426 cl->i_buffer_size = 10000;
1428 cl->p_buffer = malloc( cl->i_buffer_size );
1429 cl->i_mode = HTTPD_CLIENT_FILE;
1430 cl->b_read_waiting = VLC_FALSE;
1432 httpd_MsgInit( &cl->query );
1433 httpd_MsgInit( &cl->answer );
1436 void httpd_ClientModeStream( httpd_client_t *cl )
1438 cl->i_mode = HTTPD_CLIENT_STREAM;
1441 void httpd_ClientModeBidir( httpd_client_t *cl )
1443 cl->i_mode = HTTPD_CLIENT_BIDIR;
1446 char* httpd_ClientIP( httpd_client_t *cl )
1448 #ifdef HAVE_GETNAMEINFO
1449 char sz_ip[INET6_ADDRSTRLEN + 2];
1452 if( (cl->sock.ss_family == AF_INET6) &&
1453 IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1454 &cl->sock)->sin6_addr) )
1456 /* If client is using IPv4 but server is using IPv6 */
1457 struct sockaddr_in a;
1459 memset( &a, 0, sizeof( a ) );
1460 a.sin_family = AF_INET;
1461 a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1462 a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1463 &cl->sock)->sin6_addr)[3];
1464 i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1465 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1468 i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1469 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1473 /* FIXME: msg_Err */
1476 fprintf( stderr, "ClientIP = %s\n", &sz_ip[1]);
1478 if( strchr( &sz_ip[1], ':' ) != NULL )
1481 i = strlen( sz_ip );
1485 fprintf( stderr, "ClientIP (with []) = %s\n", sz_ip);
1486 return strdup( sz_ip );
1489 return strdup( &sz_ip[1] );
1491 /* FIXME not thread safe */
1492 return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1496 static void httpd_ClientClean( httpd_client_t *cl )
1500 net_Close( cl->fd );
1504 httpd_MsgClean( &cl->answer );
1505 httpd_MsgClean( &cl->query );
1509 free( cl->p_buffer );
1510 cl->p_buffer = NULL;
1514 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1517 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1518 /* set this new socket non-block */
1519 #if defined( WIN32 ) || defined( UNDER_CE )
1521 unsigned long i_dummy = 1;
1522 ioctlsocket( fd, FIONBIO, &i_dummy );
1525 fcntl( fd, F_SETFL, O_NONBLOCK );
1529 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1530 cl->i_sock_size = i_sock_size;
1533 httpd_ClientInit( cl );
1538 static void httpd_ClientRecv( httpd_client_t *cl )
1542 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1544 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1545 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 4 - cl->i_buffer, 0 );
1549 cl->i_buffer += i_len;
1552 if( cl->i_buffer >= 4 )
1554 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1556 if( cl->p_buffer[0] == '$' )
1558 /* RTSP (rtp over rtsp) */
1559 cl->query.i_proto = HTTPD_PROTO_RTSP;
1560 cl->query.i_type = HTTPD_MSG_CHANNEL;
1561 cl->query.i_channel = cl->p_buffer[1];
1562 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1563 cl->query.p_body = malloc( cl->query.i_body );
1567 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1569 cl->query.i_proto = HTTPD_PROTO_HTTP;
1570 cl->query.i_type = HTTPD_MSG_ANSWER;
1572 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1574 cl->query.i_proto = HTTPD_PROTO_RTSP;
1575 cl->query.i_type = HTTPD_MSG_ANSWER;
1577 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1578 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1579 !strncmp( cl->p_buffer, "POST", 4 ) )
1581 cl->query.i_proto = HTTPD_PROTO_HTTP;
1582 cl->query.i_type = HTTPD_MSG_NONE;
1586 cl->query.i_proto = HTTPD_PROTO_RTSP;
1587 cl->query.i_type = HTTPD_MSG_NONE;
1591 else if( cl->query.i_body > 0 )
1593 /* we are reading the body of a request or a channel */
1594 i_len = recv( cl->fd, &cl->query.p_body[cl->i_buffer],
1595 cl->query.i_body - cl->i_buffer, 0 );
1598 cl->i_buffer += i_len;
1600 if( cl->i_buffer >= cl->query.i_body )
1602 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1607 /* we are reading a header -> char by char */
1610 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 1, 0 );
1617 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1619 cl->i_buffer_size += 1024;
1620 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1622 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1623 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1627 /* we have finished the header so parse it and set i_body */
1628 cl->p_buffer[cl->i_buffer] = '\0';
1630 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1632 cl->query.i_status =
1633 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1638 cl->query.psz_status = strdup( p );
1650 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1651 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1652 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1654 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1655 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1656 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1657 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1658 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1659 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1661 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1666 cl->query.i_type = HTTPD_MSG_NONE;
1668 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1670 for( i = 0; msg_type[i].name != NULL; i++ )
1672 if( !strncmp( cl->p_buffer, msg_type[i].name,
1673 strlen( msg_type[i].name ) ) )
1675 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1676 cl->query.i_type = msg_type[i].i_type;
1677 if( cl->query.i_proto != msg_type[i].i_proto )
1680 cl->query.i_proto = HTTPD_PROTO_NONE;
1681 cl->query.i_type = HTTPD_MSG_NONE;
1688 if( strstr( cl->p_buffer, "HTTP/1." ) )
1690 cl->query.i_proto = HTTPD_PROTO_HTTP;
1692 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1694 cl->query.i_proto = HTTPD_PROTO_RTSP;
1706 p2 = strchr( p, ' ' );
1711 if( !strncasecmp( p, "rtsp:", 5 ) )
1713 /* for rtsp url, you have rtsp://localhost:port/path */
1715 while( *p == '/' ) p++;
1716 while( *p && *p != '/' ) p++;
1718 cl->query.psz_url = strdup( p );
1719 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1722 cl->query.psz_args = strdup( p3 );
1730 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1732 cl->query.i_proto = HTTPD_PROTO_HTTP;
1733 cl->query.i_version = atoi( p2+7 );
1735 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1737 cl->query.i_proto = HTTPD_PROTO_RTSP;
1738 cl->query.i_version = atoi( p2+7 );
1746 p = strchr( p, '\n' );
1750 while( *p == '\n' || *p == '\r' )
1754 while( p && *p != '\0' )
1757 char *eol = p = strchr( p, '\n' );
1760 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1765 if( ( colon = strchr( line, ':' ) ) )
1771 while( *colon == ' ' )
1775 name = strdup( line );
1776 value = strdup( colon );
1778 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1779 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1781 if( !strcasecmp( name, "Content-Length" ) )
1783 cl->query.i_body = atol( value );
1790 while( *p == '\n' || *p == '\r' )
1797 if( cl->query.i_body > 0 )
1799 /* TODO Mhh, handle the case client will only send a request and close the connection
1800 * to mark and of body (probably only RTSP) */
1801 cl->query.p_body = malloc( cl->query.i_body );
1806 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1812 /* check if the client is to be set to dead */
1813 #if defined( WIN32 ) || defined( UNDER_CE )
1814 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1816 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1819 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1821 /* connection closed -> end of data */
1822 if( cl->query.i_body > 0 )
1824 cl->query.i_body = cl->i_buffer;
1826 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1830 cl->i_state = HTTPD_CLIENT_DEAD;
1833 cl->i_activity_date = mdate();
1835 /* Debugging only */
1836 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1840 fprintf( stderr, "received new request\n" );
1841 fprintf( stderr, " - proto=%s\n",
1842 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1843 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1844 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1845 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1847 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1848 cl->query.psz_status );
1850 else if( cl->query.i_type != HTTPD_MSG_NONE )
1852 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1854 for( i = 0; i < cl->query.i_name; i++ )
1856 fprintf( stderr, " - option name='%s' value='%s'\n",
1857 cl->query.name[i], cl->query.value[i] );
1862 static void httpd_ClientSend( httpd_client_t *cl )
1867 if( cl->i_buffer < 0 )
1869 /* We need to create the header */
1873 i_size = strlen( "HTTP/1.") + 10 + 10 +
1874 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1875 for( i = 0; i < cl->answer.i_name; i++ )
1877 i_size += strlen( cl->answer.name[i] ) + 2 +
1878 strlen( cl->answer.value[i] ) + 2;
1881 if( cl->i_buffer_size < i_size )
1883 cl->i_buffer_size = i_size;
1884 free( cl->p_buffer );
1885 cl->p_buffer = malloc( i_size );
1889 p += sprintf( p, "%s/1.%d %d %s\r\n",
1890 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1891 cl->answer.i_version,
1892 cl->answer.i_status, cl->answer.psz_status );
1893 for( i = 0; i < cl->answer.i_name; i++ )
1895 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1896 cl->answer.value[i] );
1898 p += sprintf( p, "\r\n" );
1901 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1903 fprintf( stderr, "sending answer\n" );
1904 fprintf( stderr, "%s", cl->p_buffer );
1907 i_len = send( cl->fd, &cl->p_buffer[cl->i_buffer],
1908 cl->i_buffer_size - cl->i_buffer, 0 );
1911 cl->i_activity_date = mdate();
1912 cl->i_buffer += i_len;
1914 if( cl->i_buffer >= cl->i_buffer_size )
1916 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1917 !cl->b_read_waiting )
1919 /* catch more body data */
1920 int i_msg = cl->query.i_type;
1921 int64_t i_offset = cl->answer.i_body_offset;
1923 httpd_MsgClean( &cl->answer );
1924 cl->answer.i_body_offset = i_offset;
1926 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1927 &cl->answer, &cl->query );
1930 if( cl->answer.i_body > 0 )
1932 /* send the body data */
1933 free( cl->p_buffer );
1934 cl->p_buffer = cl->answer.p_body;
1935 cl->i_buffer_size = cl->answer.i_body;
1938 cl->answer.i_body = 0;
1939 cl->answer.p_body = NULL;
1944 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1950 #if defined( WIN32 ) || defined( UNDER_CE )
1951 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1953 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1957 cl->i_state = HTTPD_CLIENT_DEAD;
1962 static void httpd_HostThread( httpd_host_t *host )
1964 while( !host->b_die )
1966 struct timeval timeout;
1969 int i_handle_max = 0;
1972 int b_low_delay = 0;
1974 if( host->i_url <= 0 )
1981 /* built a set of handle to select */
1982 FD_ZERO( &fds_read );
1983 FD_ZERO( &fds_write );
1985 FD_SET( host->fd, &fds_read );
1986 i_handle_max = host->fd;
1988 /* add all socket that should be read/write and close dead connection */
1989 vlc_mutex_lock( &host->lock );
1990 for( i_client = 0; i_client < host->i_client; i_client++ )
1992 httpd_client_t *cl = host->client[i_client];
1994 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1995 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1996 cl->i_activity_date + cl->i_activity_timeout < mdate() ) ) )
2000 // FIXME: it sucks to allocate memory on the stack for debug
2001 ip = httpd_ClientIP( cl );
2002 msg_Dbg( host, "connection closed(%s)",
2003 (ip != NULL) ? ip : "unknown" );
2006 httpd_ClientClean( cl );
2007 TAB_REMOVE( host->i_client, host->client, cl );
2012 else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2014 FD_SET( cl->fd, &fds_read );
2015 i_handle_max = __MAX( i_handle_max, cl->fd );
2017 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2019 FD_SET( cl->fd, &fds_write );
2020 i_handle_max = __MAX( i_handle_max, cl->fd );
2022 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2024 httpd_message_t *answer = &cl->answer;
2025 httpd_message_t *query = &cl->query;
2026 int i_msg = query->i_type;
2028 httpd_MsgInit( answer );
2030 /* Handle what we received */
2031 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2032 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2034 /* we can only receive request from client when not
2037 cl->i_state = HTTPD_CLIENT_DEAD;
2039 else if( i_msg == HTTPD_MSG_ANSWER )
2041 /* We are in BIDIR mode, trigger the callback and then
2042 * check for new data */
2043 if( cl->url && cl->url->catch[i_msg].cb )
2045 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2048 cl->i_state = HTTPD_CLIENT_WAITING;
2050 else if( i_msg == HTTPD_MSG_CHANNEL )
2052 /* We are in BIDIR mode, trigger the callback and then
2053 * check for new data */
2054 if( cl->url && cl->url->catch[i_msg].cb )
2056 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2059 cl->i_state = HTTPD_CLIENT_WAITING;
2061 else if( i_msg == HTTPD_MSG_OPTIONS )
2066 answer->i_proto = query->i_proto ;
2067 answer->i_type = HTTPD_MSG_ANSWER;
2068 answer->i_version= 0;
2069 answer->i_status = 200;
2070 answer->psz_status = strdup( "Ok" );
2073 answer->p_body = NULL;
2075 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2076 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2077 httpd_MsgAdd( answer, "Server", "VLC Server" );
2078 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2079 "TEARDOWN, PLAY, PAUSE" );
2080 httpd_MsgAdd( answer, "Content-Length", "%d",
2083 cl->i_buffer = -1; /* Force the creation of the answer in
2084 * httpd_ClientSend */
2085 cl->i_state = HTTPD_CLIENT_SENDING;
2087 else if( i_msg == HTTPD_MSG_NONE )
2089 if( query->i_proto == HTTPD_PROTO_NONE )
2092 cl->i_state = HTTPD_CLIENT_DEAD;
2099 answer->i_proto = query->i_proto ;
2100 answer->i_type = HTTPD_MSG_ANSWER;
2101 answer->i_version= 0;
2102 answer->i_status = 501;
2103 answer->psz_status = strdup( "Unimplemented" );
2105 p = answer->p_body = malloc( 1000 );
2107 p += sprintf( p, "<html>\n" );
2108 p += sprintf( p, "<head>\n" );
2109 p += sprintf( p, "<title>Error 501</title>\n" );
2110 p += sprintf( p, "</head>\n" );
2111 p += sprintf( p, "<body>\n" );
2112 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2113 p += sprintf( p, "<hr />\n" );
2114 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2115 p += sprintf( p, "</body>\n" );
2116 p += sprintf( p, "</html>\n" );
2118 answer->i_body = p - answer->p_body;
2119 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2121 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2122 cl->i_state = HTTPD_CLIENT_SENDING;
2127 vlc_bool_t b_auth_failed = VLC_FALSE;
2130 /* Search the url and trigger callbacks */
2131 for( i = 0; i < host->i_url; i++ )
2133 httpd_url_t *url = host->url[i];
2135 if( !strcmp( url->psz_url, query->psz_url ) )
2137 if( url->catch[i_msg].cb )
2139 if( answer && ( *url->psz_user || *url->psz_password ) )
2141 /* create the headers */
2142 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2146 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2147 auth = malloc( strlen(b64) + 1 );
2149 if( !strncasecmp( b64, "BASIC", 5 ) )
2152 while( *b64 == ' ' )
2156 b64_decode( auth, b64 );
2162 if( strcmp( id, auth ) )
2164 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2165 /* We fail for all url */
2166 b_auth_failed = VLC_TRUE;
2176 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2178 /* only one url can answer */
2180 if( cl->url == NULL )
2192 answer->i_proto = query->i_proto;
2193 answer->i_type = HTTPD_MSG_ANSWER;
2194 answer->i_version= 0;
2195 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2199 answer->i_status = 401;
2200 answer->psz_status = strdup( "Authorization Required" );
2202 p += sprintf( p, "<html>\n" );
2203 p += sprintf( p, "<head>\n" );
2204 p += sprintf( p, "<title>Error 401</title>\n" );
2205 p += sprintf( p, "</head>\n" );
2206 p += sprintf( p, "<body>\n" );
2207 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2208 p += sprintf( p, "<hr />\n" );
2209 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2210 p += sprintf( p, "</body>\n" );
2211 p += sprintf( p, "</html>\n" );
2215 /* no url registered */
2216 answer->i_status = 404;
2217 answer->psz_status = strdup( "Not found" );
2219 p += sprintf( p, "<html>\n" );
2220 p += sprintf( p, "<head>\n" );
2221 p += sprintf( p, "<title>Error 404</title>\n" );
2222 p += sprintf( p, "</head>\n" );
2223 p += sprintf( p, "<body>\n" );
2224 p += sprintf( p, "<h1><center> 404 Ressource not found(%s)</center></h1>\n", query->psz_url );
2225 p += sprintf( p, "<hr />\n" );
2226 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2227 p += sprintf( p, "</body>\n" );
2228 p += sprintf( p, "</html>\n" );
2231 answer->i_body = p - answer->p_body;
2232 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2234 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2235 cl->i_state = HTTPD_CLIENT_SENDING;
2238 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2240 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2243 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2244 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2245 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2246 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2247 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2248 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2250 httpd_MsgClean( &cl->query );
2251 httpd_MsgInit( &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;
2261 cl->i_state = HTTPD_CLIENT_DEAD;
2263 httpd_MsgClean( &cl->answer );
2265 else if( cl->b_read_waiting )
2267 /* we have a message waiting for us to read it */
2268 httpd_MsgClean( &cl->answer );
2269 httpd_MsgClean( &cl->query );
2272 cl->i_buffer_size = 1000;
2273 free( cl->p_buffer );
2274 cl->p_buffer = malloc( cl->i_buffer_size );
2275 cl->i_state = HTTPD_CLIENT_RECEIVING;
2276 cl->b_read_waiting = VLC_FALSE;
2280 int64_t i_offset = cl->answer.i_body_offset;
2281 httpd_MsgClean( &cl->answer );
2283 cl->answer.i_body_offset = i_offset;
2284 free( cl->p_buffer );
2285 cl->p_buffer = NULL;
2287 cl->i_buffer_size = 0;
2289 cl->i_state = HTTPD_CLIENT_WAITING;
2292 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2294 int64_t i_offset = cl->answer.i_body_offset;
2295 int i_msg = cl->query.i_type;
2297 httpd_MsgInit( &cl->answer );
2298 cl->answer.i_body_offset = i_offset;
2300 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2301 &cl->answer, &cl->query );
2302 if( cl->answer.i_type != HTTPD_MSG_NONE )
2304 /* we have new data, so reenter send mode */
2306 cl->p_buffer = cl->answer.p_body;
2307 cl->i_buffer_size = cl->answer.i_body;
2308 cl->answer.p_body = NULL;
2309 cl->answer.i_body = 0;
2310 cl->i_state = HTTPD_CLIENT_SENDING;
2314 /* we shouldn't wait too long */
2315 b_low_delay = VLC_TRUE;
2319 /* Special for BIDIR mode we also check reading */
2320 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2321 cl->i_state == HTTPD_CLIENT_SENDING )
2323 FD_SET( cl->fd, &fds_read );
2324 i_handle_max = __MAX( i_handle_max, cl->fd );
2327 vlc_mutex_unlock( &host->lock );
2329 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2331 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2333 i_ret = select( i_handle_max + 1,
2334 &fds_read, &fds_write, NULL, &timeout );
2336 if( i_ret == -1 && errno != EINTR )
2338 msg_Warn( host, "cannot select sockets" );
2342 else if( i_ret <= 0 )
2347 /* accept new connections */
2348 if( FD_ISSET( host->fd, &fds_read ) )
2350 int i_sock_size = sizeof( struct sockaddr_storage );
2351 struct sockaddr_storage sock;
2354 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2359 httpd_client_t *cl = httpd_ClientNew( fd, &sock, i_sock_size );
2361 vlc_mutex_lock( &host->lock );
2362 TAB_APPEND( host->i_client, host->client, cl );
2363 vlc_mutex_unlock( &host->lock );
2365 // FIXME: it sucks to allocate heap memory for a debug message
2366 ip = httpd_ClientIP( cl );
2367 msg_Dbg( host, "new connection (%s)",
2368 ip != NULL ? ip : "unknown" );
2372 /* now try all others socket */
2373 vlc_mutex_lock( &host->lock );
2374 for( i_client = 0; i_client < host->i_client; i_client++ )
2376 httpd_client_t *cl = host->client[i_client];
2377 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2379 httpd_ClientRecv( cl );
2381 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2383 httpd_ClientSend( cl );
2386 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2387 cl->i_state == HTTPD_CLIENT_SENDING &&
2388 FD_ISSET( cl->fd, &fds_read ) )
2390 cl->b_read_waiting = VLC_TRUE;
2393 vlc_mutex_unlock( &host->lock );
2397 #ifndef HAVE_GETADDRINFO
2398 static int BuildAddr( struct sockaddr_in * p_socket,
2399 const char * psz_address, int i_port )
2402 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2403 p_socket->sin_family = AF_INET; /* family */
2404 p_socket->sin_port = htons( (uint16_t)i_port );
2407 p_socket->sin_addr.s_addr = INADDR_ANY;
2411 struct hostent * p_hostent;
2413 /* Try to convert address directly from in_addr - this will work if
2414 * psz_address is dotted decimal. */
2415 #ifdef HAVE_ARPA_INET_H
2416 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2418 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2420 /* if( p_socket->sin_addr.s_addr == INADDR_NONE )*/
2421 if( p_socket->sin_addr.s_addr == INADDR_BROADCAST )
2424 /* We have a fqdn, try to find its address */
2425 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2430 /* Copy the first address of the host in the socket address */
2431 memcpy( &((struct sockaddr_in *)p_socket)->sin_addr, p_hostent->h_addr_list[0],
2432 p_hostent->h_length );
2439 static int GetAddrPort( const struct sockaddr_storage *p_ss )
2443 switch (p_ss->ss_family)
2447 i_port = ((const struct sockaddr_in6 *)p_ss)->sin6_port;
2452 i_port = ((const struct sockaddr_in *)p_ss)->sin_port;
2459 return ntohs( i_port );