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 ) );
1557 /* set this new socket non-block */
1558 #if defined( WIN32 ) || defined( UNDER_CE )
1560 unsigned long i_dummy = 1;
1561 ioctlsocket( fd, FIONBIO, &i_dummy );
1564 fcntl( fd, F_SETFL, O_NONBLOCK );
1568 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1569 cl->i_sock_size = i_sock_size;
1573 httpd_ClientInit( cl );
1579 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1581 tls_session_t *p_tls;
1585 return tls_Recv( p_tls, p, i_len );
1587 return recv( cl->fd, p, i_len, 0 );
1591 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1593 tls_session_t *p_tls;
1597 return tls_Send( p_tls, p, i_len );
1599 return send( cl->fd, p, i_len, 0 );
1603 static void httpd_ClientRecv( httpd_client_t *cl )
1607 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1609 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1610 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1614 cl->i_buffer += i_len;
1617 if( cl->i_buffer >= 4 )
1619 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1621 if( cl->p_buffer[0] == '$' )
1623 /* RTSP (rtp over rtsp) */
1624 cl->query.i_proto = HTTPD_PROTO_RTSP;
1625 cl->query.i_type = HTTPD_MSG_CHANNEL;
1626 cl->query.i_channel = cl->p_buffer[1];
1627 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1628 cl->query.p_body = malloc( cl->query.i_body );
1632 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1634 cl->query.i_proto = HTTPD_PROTO_HTTP;
1635 cl->query.i_type = HTTPD_MSG_ANSWER;
1637 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1639 cl->query.i_proto = HTTPD_PROTO_RTSP;
1640 cl->query.i_type = HTTPD_MSG_ANSWER;
1642 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1643 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1644 !strncmp( cl->p_buffer, "POST", 4 ) )
1646 cl->query.i_proto = HTTPD_PROTO_HTTP;
1647 cl->query.i_type = HTTPD_MSG_NONE;
1651 cl->query.i_proto = HTTPD_PROTO_RTSP;
1652 cl->query.i_type = HTTPD_MSG_NONE;
1656 else if( cl->query.i_body > 0 )
1658 /* we are reading the body of a request or a channel */
1659 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1660 cl->query.i_body - cl->i_buffer );
1663 cl->i_buffer += i_len;
1665 if( cl->i_buffer >= cl->query.i_body )
1667 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1672 /* we are reading a header -> char by char */
1675 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1682 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1684 cl->i_buffer_size += 1024;
1685 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1687 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1688 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1692 /* we have finished the header so parse it and set i_body */
1693 cl->p_buffer[cl->i_buffer] = '\0';
1695 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1697 cl->query.i_status =
1698 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1703 cl->query.psz_status = strdup( p );
1715 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1716 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1717 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1719 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1720 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1721 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1722 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1723 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1724 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1726 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1731 cl->query.i_type = HTTPD_MSG_NONE;
1733 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1735 for( i = 0; msg_type[i].name != NULL; i++ )
1737 if( !strncmp( cl->p_buffer, msg_type[i].name,
1738 strlen( msg_type[i].name ) ) )
1740 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1741 cl->query.i_type = msg_type[i].i_type;
1742 if( cl->query.i_proto != msg_type[i].i_proto )
1745 cl->query.i_proto = HTTPD_PROTO_NONE;
1746 cl->query.i_type = HTTPD_MSG_NONE;
1753 if( strstr( cl->p_buffer, "HTTP/1." ) )
1755 cl->query.i_proto = HTTPD_PROTO_HTTP;
1757 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1759 cl->query.i_proto = HTTPD_PROTO_RTSP;
1771 p2 = strchr( p, ' ' );
1776 if( !strncasecmp( p, "rtsp:", 5 ) )
1778 /* for rtsp url, you have rtsp://localhost:port/path */
1780 while( *p == '/' ) p++;
1781 while( *p && *p != '/' ) p++;
1783 cl->query.psz_url = strdup( p );
1784 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1787 cl->query.psz_args = strdup( p3 );
1795 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1797 cl->query.i_proto = HTTPD_PROTO_HTTP;
1798 cl->query.i_version = atoi( p2+7 );
1800 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1802 cl->query.i_proto = HTTPD_PROTO_RTSP;
1803 cl->query.i_version = atoi( p2+7 );
1811 p = strchr( p, '\n' );
1815 while( *p == '\n' || *p == '\r' )
1819 while( p && *p != '\0' )
1822 char *eol = p = strchr( p, '\n' );
1825 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1830 if( ( colon = strchr( line, ':' ) ) )
1836 while( *colon == ' ' )
1840 name = strdup( line );
1841 value = strdup( colon );
1843 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1844 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1846 if( !strcasecmp( name, "Content-Length" ) )
1848 cl->query.i_body = atol( value );
1855 while( *p == '\n' || *p == '\r' )
1862 if( cl->query.i_body > 0 )
1864 /* TODO Mhh, handle the case client will only send a request and close the connection
1865 * to mark and of body (probably only RTSP) */
1866 cl->query.p_body = malloc( cl->query.i_body );
1871 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1877 /* check if the client is to be set to dead */
1878 #if defined( WIN32 ) || defined( UNDER_CE )
1879 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1881 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1884 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1886 /* connection closed -> end of data */
1887 if( cl->query.i_body > 0 )
1889 cl->query.i_body = cl->i_buffer;
1891 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1895 cl->i_state = HTTPD_CLIENT_DEAD;
1898 cl->i_activity_date = mdate();
1900 /* XXX: for QT I have to disable timeout. Try to find why */
1901 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1902 cl->i_activity_timeout = 0;
1904 /* Debugging only */
1905 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1909 fprintf( stderr, "received new request\n" );
1910 fprintf( stderr, " - proto=%s\n",
1911 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1912 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1913 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1914 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1916 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1917 cl->query.psz_status );
1919 else if( cl->query.i_type != HTTPD_MSG_NONE )
1921 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1923 for( i = 0; i < cl->query.i_name; i++ )
1925 fprintf( stderr, " - option name='%s' value='%s'\n",
1926 cl->query.name[i], cl->query.value[i] );
1932 static void httpd_ClientSend( httpd_client_t *cl )
1937 if( cl->i_buffer < 0 )
1939 /* We need to create the header */
1943 i_size = strlen( "HTTP/1.") + 10 + 10 +
1944 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1945 for( i = 0; i < cl->answer.i_name; i++ )
1947 i_size += strlen( cl->answer.name[i] ) + 2 +
1948 strlen( cl->answer.value[i] ) + 2;
1951 if( cl->i_buffer_size < i_size )
1953 cl->i_buffer_size = i_size;
1954 free( cl->p_buffer );
1955 cl->p_buffer = malloc( i_size );
1959 p += sprintf( p, "%s/1.%d %d %s\r\n",
1960 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1961 cl->answer.i_version,
1962 cl->answer.i_status, cl->answer.psz_status );
1963 for( i = 0; i < cl->answer.i_name; i++ )
1965 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1966 cl->answer.value[i] );
1968 p += sprintf( p, "\r\n" );
1971 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1973 fprintf( stderr, "sending answer\n" );
1974 fprintf( stderr, "%s", cl->p_buffer );
1977 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1978 cl->i_buffer_size - cl->i_buffer );
1981 cl->i_activity_date = mdate();
1982 cl->i_buffer += i_len;
1984 if( cl->i_buffer >= cl->i_buffer_size )
1986 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1987 !cl->b_read_waiting )
1989 /* catch more body data */
1990 int i_msg = cl->query.i_type;
1991 int64_t i_offset = cl->answer.i_body_offset;
1993 httpd_MsgClean( &cl->answer );
1994 cl->answer.i_body_offset = i_offset;
1996 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1997 &cl->answer, &cl->query );
2000 if( cl->answer.i_body > 0 )
2002 /* send the body data */
2003 free( cl->p_buffer );
2004 cl->p_buffer = cl->answer.p_body;
2005 cl->i_buffer_size = cl->answer.i_body;
2008 cl->answer.i_body = 0;
2009 cl->answer.p_body = NULL;
2014 cl->i_state = HTTPD_CLIENT_SEND_DONE;
2020 #if defined( WIN32 ) || defined( UNDER_CE )
2021 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2023 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2027 cl->i_state = HTTPD_CLIENT_DEAD;
2032 static void httpd_HostThread( httpd_host_t *host )
2034 tls_session_t *p_tls = NULL;
2036 while( !host->b_die )
2038 struct timeval timeout;
2041 int i_handle_max = 0;
2044 int b_low_delay = 0;
2046 if( host->i_url <= 0 )
2053 /* built a set of handle to select */
2054 FD_ZERO( &fds_read );
2055 FD_ZERO( &fds_write );
2057 FD_SET( host->fd, &fds_read );
2058 i_handle_max = host->fd;
2060 /* prepare a new TLS session */
2061 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2062 p_tls = tls_ServerSessionPrepare( host->p_tls );
2064 /* add all socket that should be read/write and close dead connection */
2065 vlc_mutex_lock( &host->lock );
2066 for( i_client = 0; i_client < host->i_client; i_client++ )
2068 httpd_client_t *cl = host->client[i_client];
2070 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2071 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2072 ( cl->i_activity_timeout > 0 &&
2073 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2077 // FIXME: it sucks to allocate memory on the stack for debug
2078 ip = httpd_ClientIP( cl );
2079 msg_Dbg( host, "connection closed(%s)",
2080 (ip != NULL) ? ip : "unknown" );
2083 httpd_ClientClean( cl );
2084 TAB_REMOVE( host->i_client, host->client, cl );
2089 else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2091 FD_SET( cl->fd, &fds_read );
2092 i_handle_max = __MAX( i_handle_max, cl->fd );
2094 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2096 FD_SET( cl->fd, &fds_write );
2097 i_handle_max = __MAX( i_handle_max, cl->fd );
2099 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2101 httpd_message_t *answer = &cl->answer;
2102 httpd_message_t *query = &cl->query;
2103 int i_msg = query->i_type;
2105 httpd_MsgInit( answer );
2107 /* Handle what we received */
2108 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2109 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2111 /* we can only receive request from client when not
2114 cl->i_state = HTTPD_CLIENT_DEAD;
2116 else if( i_msg == HTTPD_MSG_ANSWER )
2118 /* We are in BIDIR mode, trigger the callback and then
2119 * check for new data */
2120 if( cl->url && cl->url->catch[i_msg].cb )
2122 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2125 cl->i_state = HTTPD_CLIENT_WAITING;
2127 else if( i_msg == HTTPD_MSG_CHANNEL )
2129 /* We are in BIDIR mode, trigger the callback and then
2130 * check for new data */
2131 if( cl->url && cl->url->catch[i_msg].cb )
2133 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2136 cl->i_state = HTTPD_CLIENT_WAITING;
2138 else if( i_msg == HTTPD_MSG_OPTIONS )
2143 answer->i_proto = query->i_proto ;
2144 answer->i_type = HTTPD_MSG_ANSWER;
2145 answer->i_version= 0;
2146 answer->i_status = 200;
2147 answer->psz_status = strdup( "Ok" );
2150 answer->p_body = NULL;
2152 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2153 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2154 httpd_MsgAdd( answer, "Server", "VLC Server" );
2155 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2156 "TEARDOWN, PLAY, PAUSE" );
2157 httpd_MsgAdd( answer, "Content-Length", "%d",
2160 cl->i_buffer = -1; /* Force the creation of the answer in
2161 * httpd_ClientSend */
2162 cl->i_state = HTTPD_CLIENT_SENDING;
2164 else if( i_msg == HTTPD_MSG_NONE )
2166 if( query->i_proto == HTTPD_PROTO_NONE )
2169 cl->i_state = HTTPD_CLIENT_DEAD;
2176 answer->i_proto = query->i_proto ;
2177 answer->i_type = HTTPD_MSG_ANSWER;
2178 answer->i_version= 0;
2179 answer->i_status = 501;
2180 answer->psz_status = strdup( "Unimplemented" );
2182 p = answer->p_body = malloc( 1000 );
2184 p += sprintf( p, "<html>\n" );
2185 p += sprintf( p, "<head>\n" );
2186 p += sprintf( p, "<title>Error 501</title>\n" );
2187 p += sprintf( p, "</head>\n" );
2188 p += sprintf( p, "<body>\n" );
2189 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2190 p += sprintf( p, "<hr />\n" );
2191 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2192 p += sprintf( p, "</body>\n" );
2193 p += sprintf( p, "</html>\n" );
2195 answer->i_body = p - answer->p_body;
2196 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2198 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2199 cl->i_state = HTTPD_CLIENT_SENDING;
2204 vlc_bool_t b_auth_failed = VLC_FALSE;
2207 /* Search the url and trigger callbacks */
2208 for( i = 0; i < host->i_url; i++ )
2210 httpd_url_t *url = host->url[i];
2212 if( !strcmp( url->psz_url, query->psz_url ) )
2214 if( url->catch[i_msg].cb )
2216 if( answer && ( *url->psz_user || *url->psz_password ) )
2218 /* create the headers */
2219 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2223 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2224 auth = malloc( strlen(b64) + 1 );
2226 if( !strncasecmp( b64, "BASIC", 5 ) )
2229 while( *b64 == ' ' )
2233 b64_decode( auth, b64 );
2239 if( strcmp( id, auth ) )
2241 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2242 /* We fail for all url */
2243 b_auth_failed = VLC_TRUE;
2253 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2255 /* only one url can answer */
2257 if( cl->url == NULL )
2269 answer->i_proto = query->i_proto;
2270 answer->i_type = HTTPD_MSG_ANSWER;
2271 answer->i_version= 0;
2272 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2276 answer->i_status = 401;
2277 answer->psz_status = strdup( "Authorization Required" );
2279 p += sprintf( p, "<html>\n" );
2280 p += sprintf( p, "<head>\n" );
2281 p += sprintf( p, "<title>Error 401</title>\n" );
2282 p += sprintf( p, "</head>\n" );
2283 p += sprintf( p, "<body>\n" );
2284 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2285 p += sprintf( p, "<hr />\n" );
2286 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2287 p += sprintf( p, "</body>\n" );
2288 p += sprintf( p, "</html>\n" );
2292 /* no url registered */
2293 answer->i_status = 404;
2294 answer->psz_status = strdup( "Not found" );
2296 p += sprintf( p, "<html>\n" );
2297 p += sprintf( p, "<head>\n" );
2298 p += sprintf( p, "<title>Error 404</title>\n" );
2299 p += sprintf( p, "</head>\n" );
2300 p += sprintf( p, "<body>\n" );
2301 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2302 p += sprintf( p, "<hr />\n" );
2303 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2304 p += sprintf( p, "</body>\n" );
2305 p += sprintf( p, "</html>\n" );
2308 answer->i_body = p - answer->p_body;
2309 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2311 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2312 cl->i_state = HTTPD_CLIENT_SENDING;
2315 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2317 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2320 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2321 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2322 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2323 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2324 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2325 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2327 httpd_MsgClean( &cl->query );
2328 httpd_MsgInit( &cl->query );
2331 cl->i_buffer_size = 1000;
2332 free( cl->p_buffer );
2333 cl->p_buffer = malloc( cl->i_buffer_size );
2334 cl->i_state = HTTPD_CLIENT_RECEIVING;
2338 cl->i_state = HTTPD_CLIENT_DEAD;
2340 httpd_MsgClean( &cl->answer );
2342 else if( cl->b_read_waiting )
2344 /* we have a message waiting for us to read it */
2345 httpd_MsgClean( &cl->answer );
2346 httpd_MsgClean( &cl->query );
2349 cl->i_buffer_size = 1000;
2350 free( cl->p_buffer );
2351 cl->p_buffer = malloc( cl->i_buffer_size );
2352 cl->i_state = HTTPD_CLIENT_RECEIVING;
2353 cl->b_read_waiting = VLC_FALSE;
2357 int64_t i_offset = cl->answer.i_body_offset;
2358 httpd_MsgClean( &cl->answer );
2360 cl->answer.i_body_offset = i_offset;
2361 free( cl->p_buffer );
2362 cl->p_buffer = NULL;
2364 cl->i_buffer_size = 0;
2366 cl->i_state = HTTPD_CLIENT_WAITING;
2369 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2371 int64_t i_offset = cl->answer.i_body_offset;
2372 int i_msg = cl->query.i_type;
2374 httpd_MsgInit( &cl->answer );
2375 cl->answer.i_body_offset = i_offset;
2377 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2378 &cl->answer, &cl->query );
2379 if( cl->answer.i_type != HTTPD_MSG_NONE )
2381 /* we have new data, so reenter send mode */
2383 cl->p_buffer = cl->answer.p_body;
2384 cl->i_buffer_size = cl->answer.i_body;
2385 cl->answer.p_body = NULL;
2386 cl->answer.i_body = 0;
2387 cl->i_state = HTTPD_CLIENT_SENDING;
2391 /* we shouldn't wait too long */
2392 b_low_delay = VLC_TRUE;
2396 /* Special for BIDIR mode we also check reading */
2397 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2398 cl->i_state == HTTPD_CLIENT_SENDING )
2400 FD_SET( cl->fd, &fds_read );
2401 i_handle_max = __MAX( i_handle_max, cl->fd );
2404 vlc_mutex_unlock( &host->lock );
2406 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2408 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2410 i_ret = select( i_handle_max + 1,
2411 &fds_read, &fds_write, NULL, &timeout );
2413 if( i_ret == -1 && errno != EINTR )
2415 msg_Warn( host, "cannot select sockets" );
2419 else if( i_ret <= 0 )
2424 /* accept new connections */
2425 if( FD_ISSET( host->fd, &fds_read ) )
2427 int i_sock_size = sizeof( struct sockaddr_storage );
2428 struct sockaddr_storage sock;
2431 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2436 p_tls = tls_SessionHandshake( p_tls, fd );
2437 if ( p_tls == NULL )
2439 msg_Err( host, "Rejecting TLS connection" );
2450 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2452 vlc_mutex_lock( &host->lock );
2453 TAB_APPEND( host->i_client, host->client, cl );
2454 vlc_mutex_unlock( &host->lock );
2457 // FIXME: it sucks to allocate memory for debug
2458 ip = httpd_ClientIP( cl );
2459 msg_Dbg( host, "new connection (%s)",
2460 ip != NULL ? ip : "unknown" );
2466 /* now try all others socket */
2467 vlc_mutex_lock( &host->lock );
2468 for( i_client = 0; i_client < host->i_client; i_client++ )
2470 httpd_client_t *cl = host->client[i_client];
2471 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2473 httpd_ClientRecv( cl );
2475 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2477 httpd_ClientSend( cl );
2480 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2481 cl->i_state == HTTPD_CLIENT_SENDING &&
2482 FD_ISSET( cl->fd, &fds_read ) )
2484 cl->b_read_waiting = VLC_TRUE;
2487 vlc_mutex_unlock( &host->lock );
2491 #ifndef HAVE_GETADDRINFO
2492 static int BuildAddr( struct sockaddr_in * p_socket,
2493 const char * psz_address, int i_port )
2496 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2497 p_socket->sin_family = AF_INET; /* family */
2498 p_socket->sin_port = htons( (uint16_t)i_port );
2501 p_socket->sin_addr.s_addr = INADDR_ANY;
2505 struct hostent * p_hostent;
2507 /* Try to convert address directly from in_addr - this will work if
2508 * psz_address is dotted decimal. */
2509 #ifdef HAVE_ARPA_INET_H
2510 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2512 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2514 /* if( p_socket->sin_addr.s_addr == INADDR_NONE )*/
2515 if( p_socket->sin_addr.s_addr == INADDR_BROADCAST )
2518 /* We have a fqdn, try to find its address */
2519 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2524 /* Copy the first address of the host in the socket address */
2525 memcpy( &((struct sockaddr_in *)p_socket)->sin_addr, p_hostent->h_addr_list[0],
2526 p_hostent->h_length );
2533 static int GetAddrPort( const struct sockaddr_storage *p_ss )
2537 switch (p_ss->ss_family)
2541 i_port = ((const struct sockaddr_in6 *)p_ss)->sin6_port;
2546 i_port = ((const struct sockaddr_in *)p_ss)->sin_port;
2553 return ntohs( i_port );
2556 #else /* ENABLE_HTTPD */
2558 /* We just define an empty wrapper */
2559 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2562 msg_Err( a, "HTTP daemon support is disabled" );
2565 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2567 msg_Err( a, "HTTP daemon support is disabled" );
2570 void httpd_HostDelete( httpd_host_t *a ){}
2571 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2572 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2573 char *d ){ return 0; }
2574 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2575 httpd_callback_sys_t *d ){ return 0; }
2576 void httpd_UrlDelete( httpd_url_t *a ){}
2578 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2579 void httpd_ClientModeStream( httpd_client_t *a ){}
2580 void httpd_ClientModeBidir( httpd_client_t *a ){}
2582 void httpd_FileDelete( httpd_file_t *a ){}
2583 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2584 char *e, httpd_file_callback_t f,
2585 httpd_file_sys_t *g ){ return 0; }
2587 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2588 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2589 char *b, char *c ){ return 0; }
2591 void httpd_StreamDelete( httpd_stream_t *a ){}
2592 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2593 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2594 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2595 char *d, char *e ){ return 0; }
2597 void httpd_MsgInit ( httpd_message_t *a ){}
2598 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2599 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2600 void httpd_MsgClean( httpd_message_t *a ){}
2602 #endif /* ENABLE_HTTPD */