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 *****************************************************************************/
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 # include <netinet/in.h>
54 # ifdef HAVE_ARPA_INET_H
55 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
60 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
64 # define PF_INET AF_INET /* BeOS */
68 typedef struct httpd_t httpd_t;
70 typedef struct httpd_host_t httpd_host_t;
71 typedef struct httpd_url_t httpd_url_t;
72 typedef struct httpd_client_t httpd_client_t;
81 /* channel communication */
97 /* just to track the count of MSG */
110 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
122 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
124 /* for rtp over rtsp */
134 int64_t i_body_offset;
140 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
141 /* answer could be null, int this case no answer is requested */
142 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
145 /* create a new host */
146 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
148 void httpd_HostDelete( httpd_host_t * );
150 /* register a new url */
151 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
152 /* register callback on a url */
153 int httpd_UrlCatch( httpd_url_t *, int i_msg,
154 httpd_callback_t, httpd_callback_sys_t * );
156 void httpd_UrlDelete( httpd_url_t * );
159 void httpd_ClientModeStream( httpd_client_t *cl );
160 void httpd_ClientModeBidir( httpd_client_t *cl );
161 static void httpd_ClientClean( httpd_client_t *cl );
164 typedef struct httpd_file_t httpd_file_t;
165 typedef struct httpd_file_sys_t httpd_file_sys_t;
166 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
167 httpd_file_t *httpd_FileNew( httpd_host_t *,
168 char *psz_url, char *psz_mime,
169 char *psz_user, char *psz_password,
170 httpd_file_callback_t pf_fill,
171 httpd_file_sys_t *p_sys );
172 void httpd_FileDelete( httpd_file_t * );
174 typedef struct httpd_redirect_t httpd_redirect_t;
175 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
176 void httpd_RedirectDelete( httpd_redirect_t * );
179 typedef struct httpd_stream_t httpd_stream_t;
180 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
181 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
182 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
183 void httpd_StreamDelete( httpd_stream_t * );
186 /* Msg functions facilities */
187 void httpd_MsgInit( httpd_message_t * );
188 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
189 /* return "" if not found. The string is not allocated */
190 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
191 void httpd_MsgClean( httpd_message_t * );
199 /* vlc_mutex_t lock; */
205 static void httpd_ClientClean( httpd_client_t *cl );
207 /* each host run in his own thread */
217 /* address/port and socket for listening at connections */
218 struct sockaddr_storage sock;
224 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
225 * This will slow down the url research but make my live easier
226 * All url will have their cb trigger, but only the first one can answer
232 httpd_client_t **client;
251 httpd_callback_sys_t *p_sys;
252 } catch[HTTPD_MSG_MAX];
258 HTTPD_CLIENT_RECEIVING,
259 HTTPD_CLIENT_RECEIVE_DONE,
261 HTTPD_CLIENT_SENDING,
262 HTTPD_CLIENT_SEND_DONE,
264 HTTPD_CLIENT_WAITING,
271 HTTPD_CLIENT_FILE, /* default */
272 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
273 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
276 struct httpd_client_t
282 struct sockaddr_storage sock;
288 int b_read_waiting; /* stop as soon as possible sending */
290 mtime_t i_activity_date;
291 mtime_t i_activity_timeout;
293 /* buffer for reading header */
299 httpd_message_t query; /* client -> httpd */
300 httpd_message_t answer; /* httpd -> client */
303 tls_session_t *p_tls;
307 /*****************************************************************************
309 *****************************************************************************/
310 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
311 static void b64_decode( char *dest, char *src )
316 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
317 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
318 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
319 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
320 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
321 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
322 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
323 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
324 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
325 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
326 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
327 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
328 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
329 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
330 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
331 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
334 for( i_level = 0; *src != '\0'; src++ )
338 c = b64[(unsigned int)*src];
350 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
354 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
358 *dest++ = ( ( last &0x03 ) << 6 ) | c;
373 { ".htm", "text/html" },
374 { ".html", "text/html" },
375 { ".txt", "text/plain" },
376 { ".xml", "text/xml" },
377 { ".dtd", "text/dtd" },
379 { ".css", "text/css" },
382 { ".gif", "image/gif" },
383 { ".jpe", "image/jpeg" },
384 { ".jpg", "image/jpeg" },
385 { ".jpeg", "image/jpeg" },
386 { ".png", "image/png" },
387 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
390 { ".avi", "video/avi" },
391 { ".asf", "video/x-ms-asf" },
392 { ".m1a", "audio/mpeg" },
393 { ".m2a", "audio/mpeg" },
394 { ".m1v", "video/mpeg" },
395 { ".m2v", "video/mpeg" },
396 { ".mp2", "audio/mpeg" },
397 { ".mp3", "audio/mpeg" },
398 { ".mpa", "audio/mpeg" },
399 { ".mpg", "video/mpeg" },
400 { ".mpeg", "video/mpeg" },
401 { ".mpe", "video/mpeg" },
402 { ".mov", "video/quicktime" },
403 { ".moov", "video/quicktime" },
404 { ".ogg", "application/ogg" },
405 { ".ogm", "application/ogg" },
406 { ".wav", "audio/wav" },
407 { ".wma", "audio/x-ms-wma" },
408 { ".wmv", "video/x-ms-wmv" },
414 static char *httpd_MimeFromUrl( char *psz_url )
419 psz_ext = strrchr( psz_url, '.' );
424 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
426 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
428 return http_mime[i].psz_mime;
432 return "application/octet-stream";
435 /*****************************************************************************
436 * High Level Funtions: httpd_file_t
437 *****************************************************************************/
445 httpd_file_callback_t pf_fill;
446 httpd_file_sys_t *p_sys;
451 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
453 httpd_file_t *file = (httpd_file_t*)p_sys;
455 if( answer == NULL || query == NULL )
459 answer->i_proto = HTTPD_PROTO_HTTP;
460 answer->i_version= query->i_version;
461 answer->i_type = HTTPD_MSG_ANSWER;
463 answer->i_status = 200;
464 answer->psz_status = strdup( "OK" );
466 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
467 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
469 if( query->i_type != HTTPD_MSG_HEAD )
471 char *psz_args = query->psz_args;
472 if( query->i_type == HTTPD_MSG_POST )
475 psz_args = query->p_body;
477 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
480 /* We respect client request */
481 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
483 httpd_MsgAdd( answer, "Connection",
484 httpd_MsgGet( &cl->query, "Connection" ) );
487 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
493 httpd_file_t *httpd_FileNew( httpd_host_t *host,
494 char *psz_url, char *psz_mime,
495 char *psz_user, char *psz_password,
496 httpd_file_callback_t pf_fill,
497 httpd_file_sys_t *p_sys )
499 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
501 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
502 psz_password ) ) == NULL )
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 ) ) )
598 rdir->psz_dst = strdup( psz_url_dst );
600 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
601 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
602 (httpd_callback_sys_t*)rdir );
603 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
604 (httpd_callback_sys_t*)rdir );
605 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
606 (httpd_callback_sys_t*)rdir );
607 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
608 (httpd_callback_sys_t*)rdir );
612 void httpd_RedirectDelete( httpd_redirect_t *rdir )
614 httpd_UrlDelete( rdir->url );
615 free( rdir->psz_dst );
619 /*****************************************************************************
620 * High Level Funtions: httpd_stream_t
621 *****************************************************************************/
622 struct httpd_stream_t
629 /* Header to send as first packet */
633 /* circular buffer */
634 int i_buffer_size; /* buffer size, can't be reallocated smaller */
635 uint8_t *p_buffer; /* buffer */
636 int64_t i_buffer_pos; /* absolute position from begining */
637 int64_t i_buffer_last_pos; /* a new connection will start with that */
640 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
641 httpd_client_t *cl, httpd_message_t *answer,
642 httpd_message_t *query )
644 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
646 if( answer == NULL || query == NULL || cl == NULL )
650 if( answer->i_body_offset > 0 )
656 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
657 answer->i_body_offset );
660 if( answer->i_body_offset >= stream->i_buffer_pos )
662 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
663 return VLC_EGENERIC; /* wait, no data available */
665 if( answer->i_body_offset + stream->i_buffer_size <
666 stream->i_buffer_pos )
668 /* this client isn't fast enough */
669 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
670 answer->i_body_offset, stream->i_buffer_last_pos );
671 answer->i_body_offset = stream->i_buffer_last_pos;
674 i_pos = answer->i_body_offset % stream->i_buffer_size;
675 i_write = stream->i_buffer_pos - answer->i_body_offset;
676 if( i_write > 10000 )
680 else if( i_write <= 0 )
682 return VLC_EGENERIC; /* wait, no data available */
685 /* Don't go past the end of the circular buffer */
686 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
688 /* using HTTPD_MSG_ANSWER -> data available */
689 answer->i_proto = HTTPD_PROTO_HTTP;
690 answer->i_version= 0;
691 answer->i_type = HTTPD_MSG_ANSWER;
693 answer->i_body = i_write;
694 answer->p_body = malloc( i_write );
695 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
697 answer->i_body_offset += i_write;
703 answer->i_proto = HTTPD_PROTO_HTTP;
704 answer->i_version= 0;
705 answer->i_type = HTTPD_MSG_ANSWER;
707 answer->i_status = 200;
708 answer->psz_status = strdup( "OK" );
710 if( query->i_type != HTTPD_MSG_HEAD )
712 httpd_ClientModeStream( cl );
713 vlc_mutex_lock( &stream->lock );
714 /* Send the header */
715 if( stream->i_header > 0 )
717 answer->i_body = stream->i_header;
718 answer->p_body = malloc( stream->i_header );
719 memcpy( answer->p_body, stream->p_header, stream->i_header );
721 answer->i_body_offset = stream->i_buffer_last_pos;
722 vlc_mutex_unlock( &stream->lock );
726 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
727 answer->i_body_offset = 0;
730 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
732 vlc_bool_t b_xplaystream = VLC_FALSE;
735 httpd_MsgAdd( answer, "Content-type", "%s",
736 "application/octet-stream" );
737 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
738 httpd_MsgAdd( answer, "Pragma", "no-cache" );
739 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
740 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
742 /* Check if there is a xPlayStrm=1 */
743 for( i = 0; i < query->i_name; i++ )
745 if( !strcasecmp( query->name[i], "Pragma" ) &&
746 strstr( query->value[i], "xPlayStrm=1" ) )
748 b_xplaystream = VLC_TRUE;
754 answer->i_body_offset = 0;
759 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
761 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
766 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
767 char *psz_url, char *psz_mime,
768 char *psz_user, char *psz_password )
770 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
772 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
773 psz_password ) ) == NULL )
778 vlc_mutex_init( host, &stream->lock );
779 if( psz_mime && *psz_mime )
781 stream->psz_mime = strdup( psz_mime );
785 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
787 stream->i_header = 0;
788 stream->p_header = NULL;
789 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
790 stream->p_buffer = malloc( stream->i_buffer_size );
791 /* We set to 1 to make life simpler
792 * (this way i_body_offset can never be 0) */
793 stream->i_buffer_pos = 1;
794 stream->i_buffer_last_pos = 1;
796 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
797 (httpd_callback_sys_t*)stream );
798 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
799 (httpd_callback_sys_t*)stream );
800 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
801 (httpd_callback_sys_t*)stream );
806 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
808 vlc_mutex_lock( &stream->lock );
809 if( stream->p_header )
811 free( stream->p_header );
812 stream->p_header = NULL;
814 stream->i_header = i_data;
817 stream->p_header = malloc( i_data );
818 memcpy( stream->p_header, p_data, i_data );
820 vlc_mutex_unlock( &stream->lock );
825 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
830 if( i_data < 0 || p_data == NULL )
834 vlc_mutex_lock( &stream->lock );
836 /* save this pointer (to be used by new connection) */
837 stream->i_buffer_last_pos = stream->i_buffer_pos;
839 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
845 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
847 /* Ok, we can't go past the end of our buffer */
848 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
850 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
855 stream->i_buffer_pos += i_data;
857 vlc_mutex_unlock( &stream->lock );
861 void httpd_StreamDelete( httpd_stream_t *stream )
863 httpd_UrlDelete( stream->url );
864 vlc_mutex_destroy( &stream->lock );
865 if( stream->psz_mime ) free( stream->psz_mime );
866 if( stream->p_header ) free( stream->p_header );
867 if( stream->p_buffer ) free( stream->p_buffer );
872 /*****************************************************************************
874 *****************************************************************************/
875 #define LISTEN_BACKLOG 100
877 #if defined(HAVE_GETNAMEINFO) && !defined(HAVE_GETADDRINFO)
879 * For now, VLC's configure script does not check for getaddrinfo(),
880 * but it should be present if getnameinfo() is (the opposite is untrue, with
881 * Debian potato as an example)
883 # define HAVE_GETADDRINFO 1
886 static void httpd_HostThread( httpd_host_t * );
887 static int GetAddrPort( const struct sockaddr_storage *p_ss );
889 #ifndef HAVE_GETADDRINFO
890 struct httpd_addrinfo
896 struct sockaddr *ai_addr;
898 struct httpd_addrinfo *ai_next;
900 # define addrinfo httpd_addrinfo
902 static int BuildAddr( struct sockaddr_in * p_socket,
903 const char * psz_address, int i_port );
907 /* create a new host */
908 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host,
911 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL );
914 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, char *psz_host,
915 int i_port, tls_server_t *p_tls )
918 httpd_host_t *host = NULL;
921 struct addrinfo *res, *ptr;
924 #ifdef HAVE_GETADDRINFO
928 struct addrinfo hints;
930 memset( &hints, 0, sizeof( hints ) );
934 * For now, keep IPv4 by default. That said, it should be safe to use
935 * IPv6 by default *on the server side*, as, apart from NetBSD, most
936 * systems accept IPv4 clients on IPv6 listening sockets.
938 hints.ai_family = PF_INET;
942 /* Check if ipv4 or ipv6 were forced */
943 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
944 var_Get( p_this, "ipv4", &val );
946 hints.ai_family = PF_INET;
948 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
949 var_Get( p_this, "ipv6", &val );
951 hints.ai_family = PF_INET6;
953 hints.ai_socktype = SOCK_STREAM;
954 hints.ai_flags = AI_PASSIVE;
956 if (*psz_host == '\0')
959 snprintf( psz_port, sizeof( psz_port ), "%d", i_port );
960 psz_port[sizeof( psz_port ) - 1] = '\0';
962 if( getaddrinfo( psz_host, psz_port, &hints, &res ) )
964 msg_Err( p_this, "cannot resolve %s:%d", psz_host, i_port );
970 struct sockaddr_in sock;
971 struct httpd_addrinfo info;
973 info.ai_family = PF_INET;
974 info.ai_socktype = SOCK_STREAM;
975 info.ai_protocol = 0;
976 info.ai_addr = (struct sockaddr *)&sock;
977 info.ai_addrlen = sizeof( sock );
982 if( BuildAddr( &sock, psz_host, i_port ) )
984 msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
988 # define freeaddrinfo( r ) (void)0;
991 /* to be sure to avoid multiple creation */
992 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
993 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
994 vlc_mutex_lock( lockval.p_address );
996 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
998 msg_Info( p_this, "creating httpd" );
999 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
1001 vlc_mutex_unlock( lockval.p_address );
1002 freeaddrinfo( res );
1009 vlc_object_yield( httpd );
1010 vlc_object_attach( httpd, p_this->p_vlc );
1013 for( ptr = res; (ptr != NULL) && (fd == -1); ptr = ptr->ai_next )
1017 if( ((unsigned)ptr->ai_addrlen) > sizeof( struct sockaddr_storage ) )
1019 msg_Dbg( p_this, "socket address too big" );
1023 /* verify if it already exist */
1024 for( i = 0; i < httpd->i_host; i++ )
1026 if( GetAddrPort (&httpd->host[i]->sock) != i_port )
1029 /* Cannot re-use host if it uses TLS/SSL */
1030 if( httpd->host[i]->p_tls != NULL )
1034 if( httpd->host[i]->sock.ss_family == AF_INET6 )
1036 const struct sockaddr_in6 *p_hsock, *p_sock;
1038 p_hsock = (const struct sockaddr_in6 *)&httpd->host[i]->sock;
1039 p_sock = (const struct sockaddr_in6 *)ptr->ai_addr;
1041 if( memcmp( &p_hsock->sin6_addr, &in6addr_any,
1042 sizeof( struct in6_addr ) ) &&
1043 ( p_sock->sin6_family != AF_INET6 ||
1044 memcmp( &p_hsock->sin6_addr, &p_sock->sin6_addr,
1045 sizeof( struct in6_addr ) ) ) )
1046 continue; /* does not match */
1048 else if( ptr->ai_family == PF_INET6 )
1052 if( httpd->host[i]->sock.ss_family == AF_INET )
1054 const struct sockaddr_in *p_hsock, *p_sock;
1056 p_hsock = (const struct sockaddr_in *)&httpd->host[i]->sock;
1057 p_sock = (const struct sockaddr_in *)ptr->ai_addr;
1059 if( p_hsock->sin_addr.s_addr != INADDR_ANY &&
1060 ( p_sock->sin_family != AF_INET ||
1061 p_hsock->sin_addr.s_addr != p_sock->sin_addr.s_addr ) )
1062 continue; /* does not match */
1064 else if( ptr->ai_family == PF_INET )
1068 msg_Dbg( p_this, "host with unknown address family" );
1072 freeaddrinfo( res );
1075 host = httpd->host[i];
1078 vlc_mutex_unlock( lockval.p_address );
1080 msg_Dbg( p_this, "host already registered" );
1084 /* create the listening socket */
1085 fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
1092 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
1093 (void *)&dummy, sizeof( dummy ) ) < 0 )
1095 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
1100 if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
1102 msg_Err( p_this, "cannot bind socket" );
1105 /* set to non-blocking */
1106 #if defined( WIN32 ) || defined( UNDER_CE )
1108 unsigned long i_dummy = 1;
1109 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
1111 msg_Err( p_this, "cannot set socket to non-blocking mode" );
1117 unsigned int i_flags;
1118 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
1120 msg_Err( p_this, "cannot F_GETFL socket" );
1123 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
1125 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
1131 if( listen( fd, LISTEN_BACKLOG ) < 0 )
1133 msg_Err( p_this, "cannot listen socket" );
1147 freeaddrinfo( res );
1151 /* create the new host */
1152 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1153 host->httpd = httpd;
1154 vlc_mutex_init( httpd, &host->lock );
1158 memcpy( &host->sock, ptr->ai_addr, ptr->ai_addrlen );
1159 host->i_sock_size = ptr->ai_addrlen;
1163 host->client = NULL;
1165 freeaddrinfo( res );
1166 host->p_tls = p_tls;
1168 /* create the thread */
1169 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1170 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1172 msg_Err( p_this, "cannot spawn http host thread" );
1176 /* now add it to httpd */
1177 TAB_APPEND( httpd->i_host, httpd->host, host );
1178 vlc_mutex_unlock( lockval.p_address );
1183 vlc_mutex_unlock( lockval.p_address );
1190 vlc_mutex_destroy( &host->lock );
1191 vlc_object_destroy( host );
1194 /* TODO destroy no more used httpd TODO */
1195 vlc_object_release( httpd );
1200 void httpd_HostDelete( httpd_host_t *host )
1202 httpd_t *httpd = host->httpd;
1203 vlc_value_t lockval;
1206 msg_Dbg( host, "httpd_HostDelete" );
1208 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1209 vlc_mutex_lock( lockval.p_address );
1211 vlc_object_release( httpd );
1214 if( host->i_ref > 0 )
1217 vlc_mutex_unlock( lockval.p_address );
1218 msg_Dbg( host, "httpd_HostDelete: host still used" );
1221 TAB_REMOVE( httpd->i_host, httpd->host, host );
1223 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1226 vlc_thread_join( host );
1228 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1230 for( i = 0; i < host->i_url; i++ )
1232 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1234 for( i = 0; i < host->i_client; i++ )
1236 httpd_client_t *cl = host->client[i];
1237 msg_Warn( host, "client still connected" );
1238 httpd_ClientClean( cl );
1239 TAB_REMOVE( host->i_client, host->client, cl );
1245 if( host->p_tls != NULL)
1246 tls_ServerDelete( host->p_tls );
1247 net_Close( host->fd );
1248 vlc_mutex_destroy( &host->lock );
1249 vlc_object_destroy( host );
1251 if( httpd->i_host <= 0 )
1253 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1254 vlc_object_detach( httpd );
1255 vlc_object_destroy( httpd );
1257 vlc_mutex_unlock( lockval.p_address );
1260 /* register a new url */
1261 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1262 char *psz_user, char *psz_password,
1263 vlc_bool_t b_check )
1268 vlc_mutex_lock( &host->lock );
1271 for( i = 0; i < host->i_url; i++ )
1273 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1275 msg_Warn( host->httpd,
1276 "cannot add '%s' (url already defined)", psz_url );
1277 vlc_mutex_unlock( &host->lock );
1283 url = malloc( sizeof( httpd_url_t ) );
1286 vlc_mutex_init( host->httpd, &url->lock );
1287 url->psz_url = strdup( psz_url );
1288 url->psz_user = strdup( psz_user ? psz_user : "" );
1289 url->psz_password = strdup( psz_password ? psz_password : "" );
1290 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1292 url->catch[i].cb = NULL;
1293 url->catch[i].p_sys = NULL;
1296 TAB_APPEND( host->i_url, host->url, url );
1297 vlc_mutex_unlock( &host->lock );
1302 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1303 char *psz_user, char *psz_password )
1305 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1306 psz_password, VLC_FALSE );
1309 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1310 char *psz_user, char *psz_password )
1312 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1313 psz_password, VLC_TRUE );
1316 /* register callback on a url */
1317 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1318 httpd_callback_sys_t *p_sys )
1320 vlc_mutex_lock( &url->lock );
1321 url->catch[i_msg].cb = cb;
1322 url->catch[i_msg].p_sys= p_sys;
1323 vlc_mutex_unlock( &url->lock );
1330 void httpd_UrlDelete( httpd_url_t *url )
1332 httpd_host_t *host = url->host;
1335 vlc_mutex_lock( &host->lock );
1336 TAB_REMOVE( host->i_url, host->url, url );
1338 vlc_mutex_destroy( &url->lock );
1339 free( url->psz_url );
1340 free( url->psz_user );
1341 free( url->psz_password );
1343 for( i = 0; i < host->i_client; i++ )
1345 httpd_client_t *client = host->client[i];
1347 if( client->url == url )
1349 /* TODO complete it */
1350 msg_Warn( host, "force closing connections" );
1351 httpd_ClientClean( client );
1352 TAB_REMOVE( host->i_client, host->client, client );
1358 vlc_mutex_unlock( &host->lock );
1361 void httpd_MsgInit( httpd_message_t *msg )
1364 msg->i_type = HTTPD_MSG_NONE;
1365 msg->i_proto = HTTPD_PROTO_NONE;
1366 msg->i_version = -1;
1369 msg->psz_status = NULL;
1371 msg->psz_url = NULL;
1372 msg->psz_args = NULL;
1374 msg->i_channel = -1;
1381 msg->i_body_offset = 0;
1386 void httpd_MsgClean( httpd_message_t *msg )
1390 if( msg->psz_status )
1392 free( msg->psz_status );
1396 free( msg->psz_url );
1400 free( msg->psz_args );
1402 for( i = 0; i < msg->i_name; i++ )
1404 free( msg->name[i] );
1405 free( msg->value[i] );
1417 free( msg->p_body );
1419 httpd_MsgInit( msg );
1422 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1426 for( i = 0; i < msg->i_name; i++ )
1428 if( !strcasecmp( msg->name[i], name ))
1430 return msg->value[i];
1435 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1440 va_start( args, psz_value );
1441 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1442 vasprintf( &value, psz_value, args );
1445 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1446 value = calloc( i_size, sizeof( char ) );
1447 vsnprintf( value, i_size, psz_value, args );
1448 value[i_size - 1] = 0;
1453 name = strdup( name );
1455 TAB_APPEND( msg->i_name, msg->name, name );
1456 TAB_APPEND( msg->i_value, msg->value, value );
1459 static void httpd_ClientInit( httpd_client_t *cl )
1461 cl->i_state = HTTPD_CLIENT_RECEIVING;
1462 cl->i_activity_date = mdate();
1463 cl->i_activity_timeout = I64C(10000000);
1464 cl->i_buffer_size = 10000;
1466 cl->p_buffer = malloc( cl->i_buffer_size );
1467 cl->i_mode = HTTPD_CLIENT_FILE;
1468 cl->b_read_waiting = VLC_FALSE;
1470 httpd_MsgInit( &cl->query );
1471 httpd_MsgInit( &cl->answer );
1474 void httpd_ClientModeStream( httpd_client_t *cl )
1476 cl->i_mode = HTTPD_CLIENT_STREAM;
1479 void httpd_ClientModeBidir( httpd_client_t *cl )
1481 cl->i_mode = HTTPD_CLIENT_BIDIR;
1484 char* httpd_ClientIP( httpd_client_t *cl )
1486 #ifdef HAVE_GETNAMEINFO
1487 char sz_ip[INET6_ADDRSTRLEN + 2];
1490 if( (cl->sock.ss_family == AF_INET6) &&
1491 IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1492 &cl->sock)->sin6_addr) )
1494 /* If client is using IPv4 but server is using IPv6 */
1495 struct sockaddr_in a;
1497 memset( &a, 0, sizeof( a ) );
1498 a.sin_family = AF_INET;
1499 a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1500 a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1501 &cl->sock)->sin6_addr)[3];
1502 i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1503 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1506 i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1507 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1511 /* FIXME: msg_Err */
1514 if( strchr( &sz_ip[1], ':' ) != NULL )
1517 i = strlen( sz_ip );
1521 return strdup( sz_ip );
1524 return strdup( &sz_ip[1] );
1527 /* FIXME not thread safe */
1528 return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1532 static void httpd_ClientClean( httpd_client_t *cl )
1536 if( cl->p_tls != NULL )
1537 tls_SessionClose( cl->p_tls );
1538 net_Close( cl->fd );
1542 httpd_MsgClean( &cl->answer );
1543 httpd_MsgClean( &cl->query );
1547 free( cl->p_buffer );
1548 cl->p_buffer = NULL;
1552 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1554 tls_session_t *p_tls )
1556 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1559 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1560 cl->i_sock_size = i_sock_size;
1564 httpd_ClientInit( cl );
1570 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1572 tls_session_t *p_tls;
1576 return tls_Recv( p_tls, p, i_len );
1578 return recv( cl->fd, p, i_len, 0 );
1582 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1584 tls_session_t *p_tls;
1588 return tls_Send( p_tls, p, i_len );
1590 return send( cl->fd, p, i_len, 0 );
1594 static void httpd_ClientRecv( httpd_client_t *cl )
1598 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1600 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1601 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1605 cl->i_buffer += i_len;
1608 if( cl->i_buffer >= 4 )
1610 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1612 if( cl->p_buffer[0] == '$' )
1614 /* RTSP (rtp over rtsp) */
1615 cl->query.i_proto = HTTPD_PROTO_RTSP;
1616 cl->query.i_type = HTTPD_MSG_CHANNEL;
1617 cl->query.i_channel = cl->p_buffer[1];
1618 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1619 cl->query.p_body = malloc( cl->query.i_body );
1623 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1625 cl->query.i_proto = HTTPD_PROTO_HTTP;
1626 cl->query.i_type = HTTPD_MSG_ANSWER;
1628 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1630 cl->query.i_proto = HTTPD_PROTO_RTSP;
1631 cl->query.i_type = HTTPD_MSG_ANSWER;
1633 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1634 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1635 !strncmp( cl->p_buffer, "POST", 4 ) )
1637 cl->query.i_proto = HTTPD_PROTO_HTTP;
1638 cl->query.i_type = HTTPD_MSG_NONE;
1642 cl->query.i_proto = HTTPD_PROTO_RTSP;
1643 cl->query.i_type = HTTPD_MSG_NONE;
1647 else if( cl->query.i_body > 0 )
1649 /* we are reading the body of a request or a channel */
1650 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1651 cl->query.i_body - cl->i_buffer );
1654 cl->i_buffer += i_len;
1656 if( cl->i_buffer >= cl->query.i_body )
1658 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1663 /* we are reading a header -> char by char */
1666 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1673 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1675 cl->i_buffer_size += 1024;
1676 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1678 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1679 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1683 /* we have finished the header so parse it and set i_body */
1684 cl->p_buffer[cl->i_buffer] = '\0';
1686 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1688 cl->query.i_status =
1689 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1694 cl->query.psz_status = strdup( p );
1706 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1707 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1708 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1710 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1711 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1712 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1713 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1714 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1715 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1717 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1722 cl->query.i_type = HTTPD_MSG_NONE;
1724 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1726 for( i = 0; msg_type[i].name != NULL; i++ )
1728 if( !strncmp( cl->p_buffer, msg_type[i].name,
1729 strlen( msg_type[i].name ) ) )
1731 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1732 cl->query.i_type = msg_type[i].i_type;
1733 if( cl->query.i_proto != msg_type[i].i_proto )
1736 cl->query.i_proto = HTTPD_PROTO_NONE;
1737 cl->query.i_type = HTTPD_MSG_NONE;
1744 if( strstr( cl->p_buffer, "HTTP/1." ) )
1746 cl->query.i_proto = HTTPD_PROTO_HTTP;
1748 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1750 cl->query.i_proto = HTTPD_PROTO_RTSP;
1762 p2 = strchr( p, ' ' );
1767 if( !strncasecmp( p, "rtsp:", 5 ) )
1769 /* for rtsp url, you have rtsp://localhost:port/path */
1771 while( *p == '/' ) p++;
1772 while( *p && *p != '/' ) p++;
1774 cl->query.psz_url = strdup( p );
1775 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1778 cl->query.psz_args = strdup( p3 );
1786 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1788 cl->query.i_proto = HTTPD_PROTO_HTTP;
1789 cl->query.i_version = atoi( p2+7 );
1791 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1793 cl->query.i_proto = HTTPD_PROTO_RTSP;
1794 cl->query.i_version = atoi( p2+7 );
1802 p = strchr( p, '\n' );
1806 while( *p == '\n' || *p == '\r' )
1810 while( p && *p != '\0' )
1813 char *eol = p = strchr( p, '\n' );
1816 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1821 if( ( colon = strchr( line, ':' ) ) )
1827 while( *colon == ' ' )
1831 name = strdup( line );
1832 value = strdup( colon );
1834 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1835 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1837 if( !strcasecmp( name, "Content-Length" ) )
1839 cl->query.i_body = atol( value );
1846 while( *p == '\n' || *p == '\r' )
1853 if( cl->query.i_body > 0 )
1855 /* TODO Mhh, handle the case client will only send a request and close the connection
1856 * to mark and of body (probably only RTSP) */
1857 cl->query.p_body = malloc( cl->query.i_body );
1862 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1868 /* check if the client is to be set to dead */
1869 #if defined( WIN32 ) || defined( UNDER_CE )
1870 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1872 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1875 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1877 /* connection closed -> end of data */
1878 if( cl->query.i_body > 0 )
1880 cl->query.i_body = cl->i_buffer;
1882 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1886 cl->i_state = HTTPD_CLIENT_DEAD;
1889 cl->i_activity_date = mdate();
1891 /* XXX: for QT I have to disable timeout. Try to find why */
1892 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1893 cl->i_activity_timeout = 0;
1895 /* Debugging only */
1896 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1900 fprintf( stderr, "received new request\n" );
1901 fprintf( stderr, " - proto=%s\n",
1902 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1903 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1904 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1905 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1907 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1908 cl->query.psz_status );
1910 else if( cl->query.i_type != HTTPD_MSG_NONE )
1912 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1914 for( i = 0; i < cl->query.i_name; i++ )
1916 fprintf( stderr, " - option name='%s' value='%s'\n",
1917 cl->query.name[i], cl->query.value[i] );
1923 static void httpd_ClientSend( httpd_client_t *cl )
1928 if( cl->i_buffer < 0 )
1930 /* We need to create the header */
1934 i_size = strlen( "HTTP/1.") + 10 + 10 +
1935 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1936 for( i = 0; i < cl->answer.i_name; i++ )
1938 i_size += strlen( cl->answer.name[i] ) + 2 +
1939 strlen( cl->answer.value[i] ) + 2;
1942 if( cl->i_buffer_size < i_size )
1944 cl->i_buffer_size = i_size;
1945 free( cl->p_buffer );
1946 cl->p_buffer = malloc( i_size );
1950 p += sprintf( p, "%s/1.%d %d %s\r\n",
1951 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1952 cl->answer.i_version,
1953 cl->answer.i_status, cl->answer.psz_status );
1954 for( i = 0; i < cl->answer.i_name; i++ )
1956 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1957 cl->answer.value[i] );
1959 p += sprintf( p, "\r\n" );
1962 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1964 fprintf( stderr, "sending answer\n" );
1965 fprintf( stderr, "%s", cl->p_buffer );
1968 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1969 cl->i_buffer_size - cl->i_buffer );
1972 cl->i_activity_date = mdate();
1973 cl->i_buffer += i_len;
1975 if( cl->i_buffer >= cl->i_buffer_size )
1977 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1978 !cl->b_read_waiting )
1980 /* catch more body data */
1981 int i_msg = cl->query.i_type;
1982 int64_t i_offset = cl->answer.i_body_offset;
1984 httpd_MsgClean( &cl->answer );
1985 cl->answer.i_body_offset = i_offset;
1987 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1988 &cl->answer, &cl->query );
1991 if( cl->answer.i_body > 0 )
1993 /* send the body data */
1994 free( cl->p_buffer );
1995 cl->p_buffer = cl->answer.p_body;
1996 cl->i_buffer_size = cl->answer.i_body;
1999 cl->answer.i_body = 0;
2000 cl->answer.p_body = NULL;
2005 cl->i_state = HTTPD_CLIENT_SEND_DONE;
2011 #if defined( WIN32 ) || defined( UNDER_CE )
2012 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2014 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2018 cl->i_state = HTTPD_CLIENT_DEAD;
2023 static void httpd_HostThread( httpd_host_t *host )
2025 tls_session_t *p_tls = NULL;
2027 while( !host->b_die )
2029 struct timeval timeout;
2032 int i_handle_max = 0;
2035 int b_low_delay = 0;
2037 if( host->i_url <= 0 )
2044 /* built a set of handle to select */
2045 FD_ZERO( &fds_read );
2046 FD_ZERO( &fds_write );
2048 FD_SET( host->fd, &fds_read );
2049 i_handle_max = host->fd;
2051 /* prepare a new TLS session */
2052 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2053 p_tls = tls_ServerSessionPrepare( host->p_tls );
2055 /* add all socket that should be read/write and close dead connection */
2056 vlc_mutex_lock( &host->lock );
2057 for( i_client = 0; i_client < host->i_client; i_client++ )
2059 httpd_client_t *cl = host->client[i_client];
2061 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2062 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2063 ( cl->i_activity_timeout > 0 &&
2064 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2068 // FIXME: it sucks to allocate memory on the stack for debug
2069 ip = httpd_ClientIP( cl );
2070 msg_Dbg( host, "connection closed(%s)",
2071 (ip != NULL) ? ip : "unknown" );
2074 httpd_ClientClean( cl );
2075 TAB_REMOVE( host->i_client, host->client, cl );
2080 else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2082 FD_SET( cl->fd, &fds_read );
2083 i_handle_max = __MAX( i_handle_max, cl->fd );
2085 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2087 FD_SET( cl->fd, &fds_write );
2088 i_handle_max = __MAX( i_handle_max, cl->fd );
2090 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2092 httpd_message_t *answer = &cl->answer;
2093 httpd_message_t *query = &cl->query;
2094 int i_msg = query->i_type;
2096 httpd_MsgInit( answer );
2098 /* Handle what we received */
2099 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2100 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2102 /* we can only receive request from client when not
2105 cl->i_state = HTTPD_CLIENT_DEAD;
2107 else if( i_msg == HTTPD_MSG_ANSWER )
2109 /* We are in BIDIR mode, trigger the callback and then
2110 * check for new data */
2111 if( cl->url && cl->url->catch[i_msg].cb )
2113 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2116 cl->i_state = HTTPD_CLIENT_WAITING;
2118 else if( i_msg == HTTPD_MSG_CHANNEL )
2120 /* We are in BIDIR mode, trigger the callback and then
2121 * check for new data */
2122 if( cl->url && cl->url->catch[i_msg].cb )
2124 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2127 cl->i_state = HTTPD_CLIENT_WAITING;
2129 else if( i_msg == HTTPD_MSG_OPTIONS )
2134 answer->i_proto = query->i_proto ;
2135 answer->i_type = HTTPD_MSG_ANSWER;
2136 answer->i_version= 0;
2137 answer->i_status = 200;
2138 answer->psz_status = strdup( "Ok" );
2141 answer->p_body = NULL;
2143 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2144 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2145 httpd_MsgAdd( answer, "Server", "VLC Server" );
2146 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2147 "TEARDOWN, PLAY, PAUSE" );
2148 httpd_MsgAdd( answer, "Content-Length", "%d",
2151 cl->i_buffer = -1; /* Force the creation of the answer in
2152 * httpd_ClientSend */
2153 cl->i_state = HTTPD_CLIENT_SENDING;
2155 else if( i_msg == HTTPD_MSG_NONE )
2157 if( query->i_proto == HTTPD_PROTO_NONE )
2160 cl->i_state = HTTPD_CLIENT_DEAD;
2167 answer->i_proto = query->i_proto ;
2168 answer->i_type = HTTPD_MSG_ANSWER;
2169 answer->i_version= 0;
2170 answer->i_status = 501;
2171 answer->psz_status = strdup( "Unimplemented" );
2173 p = answer->p_body = malloc( 1000 );
2175 p += sprintf( p, "<html>\n" );
2176 p += sprintf( p, "<head>\n" );
2177 p += sprintf( p, "<title>Error 501</title>\n" );
2178 p += sprintf( p, "</head>\n" );
2179 p += sprintf( p, "<body>\n" );
2180 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2181 p += sprintf( p, "<hr />\n" );
2182 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2183 p += sprintf( p, "</body>\n" );
2184 p += sprintf( p, "</html>\n" );
2186 answer->i_body = p - answer->p_body;
2187 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2189 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2190 cl->i_state = HTTPD_CLIENT_SENDING;
2195 vlc_bool_t b_auth_failed = VLC_FALSE;
2198 /* Search the url and trigger callbacks */
2199 for( i = 0; i < host->i_url; i++ )
2201 httpd_url_t *url = host->url[i];
2203 if( !strcmp( url->psz_url, query->psz_url ) )
2205 if( url->catch[i_msg].cb )
2207 if( answer && ( *url->psz_user || *url->psz_password ) )
2209 /* create the headers */
2210 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2214 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2215 auth = malloc( strlen(b64) + 1 );
2217 if( !strncasecmp( b64, "BASIC", 5 ) )
2220 while( *b64 == ' ' )
2224 b64_decode( auth, b64 );
2230 if( strcmp( id, auth ) )
2232 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2233 /* We fail for all url */
2234 b_auth_failed = VLC_TRUE;
2244 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2246 /* only one url can answer */
2248 if( cl->url == NULL )
2260 answer->i_proto = query->i_proto;
2261 answer->i_type = HTTPD_MSG_ANSWER;
2262 answer->i_version= 0;
2263 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2267 answer->i_status = 401;
2268 answer->psz_status = strdup( "Authorization Required" );
2270 p += sprintf( p, "<html>\n" );
2271 p += sprintf( p, "<head>\n" );
2272 p += sprintf( p, "<title>Error 401</title>\n" );
2273 p += sprintf( p, "</head>\n" );
2274 p += sprintf( p, "<body>\n" );
2275 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2276 p += sprintf( p, "<hr />\n" );
2277 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2278 p += sprintf( p, "</body>\n" );
2279 p += sprintf( p, "</html>\n" );
2283 /* no url registered */
2284 answer->i_status = 404;
2285 answer->psz_status = strdup( "Not found" );
2287 p += sprintf( p, "<html>\n" );
2288 p += sprintf( p, "<head>\n" );
2289 p += sprintf( p, "<title>Error 404</title>\n" );
2290 p += sprintf( p, "</head>\n" );
2291 p += sprintf( p, "<body>\n" );
2292 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2293 p += sprintf( p, "<hr />\n" );
2294 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2295 p += sprintf( p, "</body>\n" );
2296 p += sprintf( p, "</html>\n" );
2299 answer->i_body = p - answer->p_body;
2300 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2302 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2303 cl->i_state = HTTPD_CLIENT_SENDING;
2306 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2308 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2311 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2312 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2313 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2314 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2315 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2316 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2318 httpd_MsgClean( &cl->query );
2319 httpd_MsgInit( &cl->query );
2322 cl->i_buffer_size = 1000;
2323 free( cl->p_buffer );
2324 cl->p_buffer = malloc( cl->i_buffer_size );
2325 cl->i_state = HTTPD_CLIENT_RECEIVING;
2329 cl->i_state = HTTPD_CLIENT_DEAD;
2331 httpd_MsgClean( &cl->answer );
2333 else if( cl->b_read_waiting )
2335 /* we have a message waiting for us to read it */
2336 httpd_MsgClean( &cl->answer );
2337 httpd_MsgClean( &cl->query );
2340 cl->i_buffer_size = 1000;
2341 free( cl->p_buffer );
2342 cl->p_buffer = malloc( cl->i_buffer_size );
2343 cl->i_state = HTTPD_CLIENT_RECEIVING;
2344 cl->b_read_waiting = VLC_FALSE;
2348 int64_t i_offset = cl->answer.i_body_offset;
2349 httpd_MsgClean( &cl->answer );
2351 cl->answer.i_body_offset = i_offset;
2352 free( cl->p_buffer );
2353 cl->p_buffer = NULL;
2355 cl->i_buffer_size = 0;
2357 cl->i_state = HTTPD_CLIENT_WAITING;
2360 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2362 int64_t i_offset = cl->answer.i_body_offset;
2363 int i_msg = cl->query.i_type;
2365 httpd_MsgInit( &cl->answer );
2366 cl->answer.i_body_offset = i_offset;
2368 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2369 &cl->answer, &cl->query );
2370 if( cl->answer.i_type != HTTPD_MSG_NONE )
2372 /* we have new data, so reenter send mode */
2374 cl->p_buffer = cl->answer.p_body;
2375 cl->i_buffer_size = cl->answer.i_body;
2376 cl->answer.p_body = NULL;
2377 cl->answer.i_body = 0;
2378 cl->i_state = HTTPD_CLIENT_SENDING;
2382 /* we shouldn't wait too long */
2383 b_low_delay = VLC_TRUE;
2387 /* Special for BIDIR mode we also check reading */
2388 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2389 cl->i_state == HTTPD_CLIENT_SENDING )
2391 FD_SET( cl->fd, &fds_read );
2392 i_handle_max = __MAX( i_handle_max, cl->fd );
2395 vlc_mutex_unlock( &host->lock );
2397 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2399 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2401 i_ret = select( i_handle_max + 1,
2402 &fds_read, &fds_write, NULL, &timeout );
2404 if( i_ret == -1 && errno != EINTR )
2406 msg_Warn( host, "cannot select sockets" );
2410 else if( i_ret <= 0 )
2415 /* accept new connections */
2416 if( FD_ISSET( host->fd, &fds_read ) )
2418 int i_sock_size = sizeof( struct sockaddr_storage );
2419 struct sockaddr_storage sock;
2422 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2425 /* set this new socket non-block */
2426 #if defined( WIN32 ) || defined( UNDER_CE )
2428 unsigned long i_dummy = 1;
2429 ioctlsocket( fd, FIONBIO, &i_dummy );
2432 fcntl( fd, F_SETFL, O_NONBLOCK );
2435 /* FIXME: that MUST be non-blocking */
2438 p_tls = tls_SessionHandshake( p_tls, fd );
2439 if ( p_tls == NULL )
2441 msg_Err( host, "Rejecting TLS connection" );
2452 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2454 vlc_mutex_lock( &host->lock );
2455 TAB_APPEND( host->i_client, host->client, cl );
2456 vlc_mutex_unlock( &host->lock );
2459 // FIXME: it sucks to allocate memory for debug
2460 ip = httpd_ClientIP( cl );
2461 msg_Dbg( host, "new connection (%s)",
2462 ip != NULL ? ip : "unknown" );
2468 /* now try all others socket */
2469 vlc_mutex_lock( &host->lock );
2470 for( i_client = 0; i_client < host->i_client; i_client++ )
2472 httpd_client_t *cl = host->client[i_client];
2473 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2475 httpd_ClientRecv( cl );
2477 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2479 httpd_ClientSend( cl );
2482 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2483 cl->i_state == HTTPD_CLIENT_SENDING &&
2484 FD_ISSET( cl->fd, &fds_read ) )
2486 cl->b_read_waiting = VLC_TRUE;
2489 vlc_mutex_unlock( &host->lock );
2493 #ifndef HAVE_GETADDRINFO
2494 static int BuildAddr( struct sockaddr_in * p_socket,
2495 const char * psz_address, int i_port )
2498 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2499 p_socket->sin_family = AF_INET; /* family */
2500 p_socket->sin_port = htons( (uint16_t)i_port );
2503 p_socket->sin_addr.s_addr = INADDR_ANY;
2507 struct hostent * p_hostent;
2509 /* Try to convert address directly from in_addr - this will work if
2510 * psz_address is dotted decimal. */
2511 #ifdef HAVE_ARPA_INET_H
2512 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2514 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2516 /* if( p_socket->sin_addr.s_addr == INADDR_NONE )*/
2517 if( p_socket->sin_addr.s_addr == INADDR_BROADCAST )
2520 /* We have a fqdn, try to find its address */
2521 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2526 /* Copy the first address of the host in the socket address */
2527 memcpy( &((struct sockaddr_in *)p_socket)->sin_addr, p_hostent->h_addr_list[0],
2528 p_hostent->h_length );
2535 static int GetAddrPort( const struct sockaddr_storage *p_ss )
2539 switch (p_ss->ss_family)
2543 i_port = ((const struct sockaddr_in6 *)p_ss)->sin6_port;
2548 i_port = ((const struct sockaddr_in *)p_ss)->sin_port;
2555 return ntohs( i_port );
2558 #else /* ENABLE_HTTPD */
2560 /* We just define an empty wrapper */
2561 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2564 msg_Err( a, "HTTP daemon support is disabled" );
2567 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2569 msg_Err( a, "HTTP daemon support is disabled" );
2572 void httpd_HostDelete( httpd_host_t *a ){}
2573 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2574 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2575 char *d ){ return 0; }
2576 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2577 httpd_callback_sys_t *d ){ return 0; }
2578 void httpd_UrlDelete( httpd_url_t *a ){}
2580 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2581 void httpd_ClientModeStream( httpd_client_t *a ){}
2582 void httpd_ClientModeBidir( httpd_client_t *a ){}
2584 void httpd_FileDelete( httpd_file_t *a ){}
2585 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2586 char *e, httpd_file_callback_t f,
2587 httpd_file_sys_t *g ){ return 0; }
2589 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2590 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2591 char *b, char *c ){ return 0; }
2593 void httpd_StreamDelete( httpd_stream_t *a ){}
2594 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2595 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2596 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2597 char *d, char *e ){ return 0; }
2599 void httpd_MsgInit ( httpd_message_t *a ){}
2600 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2601 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2602 void httpd_MsgClean( httpd_message_t *a ){}
2604 #endif /* ENABLE_HTTPD */