1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: httpd.c,v 1.19 2003/07/01 09:00:50 adn Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <sys/types.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
47 #if defined( UNDER_CE )
49 #elif defined( WIN32 )
50 # include <winsock2.h>
51 # include <ws2tcpip.h>
53 # define IN_MULTICAST(a) IN_CLASSD(a)
56 # include <netdb.h> /* hostent ... */
57 # include <sys/socket.h>
58 # include <netinet/in.h>
59 # ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
67 # define INADDR_ANY 0x00000000
70 # define INADDR_NONE 0xFFFFFFFF
73 #define LISTEN_BACKLOG 100
74 #define HTTPD_MAX_CONNECTION 512
75 #define HTTPD_CONNECTION_MAX_UNUSED 10000000
77 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
79 #if defined( WIN32 ) || defined( UNDER_CE )
80 #define SOCKET_CLOSE(a) closesocket(a)
82 #define SOCKET_CLOSE(a) close(a)
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Open ( vlc_object_t * );
89 static void Close ( vlc_object_t * );
91 /*****************************************************************************
93 *****************************************************************************/
95 set_description( _("HTTP 1.0 daemon") );
96 set_capability( "httpd", 42 );
97 set_callbacks( Open, Close );
100 /*****************************************************************************
102 *****************************************************************************/
103 static httpd_host_t *RegisterHost ( httpd_t *, char *, int );
104 static void UnregisterHost ( httpd_t *, httpd_host_t * );
106 static httpd_file_t *RegisterFile ( httpd_t *,
107 char *psz_file, char *psz_mime,
108 char *psz_user, char *psz_password,
109 httpd_file_callback pf_get,
110 httpd_file_callback pf_post,
111 httpd_file_callback_args_t *p_args );
112 static void UnregisterFile ( httpd_t *, httpd_file_t * );
114 //#define httpd_stream_t httpd_file_t
115 static httpd_stream_t *RegisterStream ( httpd_t *,
116 char *psz_file, char *psz_mime,
117 char *psz_user, char *psz_password );
118 static int SendStream ( httpd_t *, httpd_stream_t *, uint8_t *, int );
119 static int HeaderStream ( httpd_t *, httpd_stream_t *, uint8_t *, int );
120 static void UnregisterStream( httpd_t *, httpd_stream_t* );
122 /*****************************************************************************
123 * Internal definitions
124 *****************************************************************************/
132 struct sockaddr_in sock;
137 #define HTTPD_AUTHENTICATE_NONE 0
138 #define HTTPD_AUTHENTICATE_BASIC 1
140 //typedef httpd_file_t httpd_stream_t;
150 int i_authenticate_method;
151 char *psz_user; /* NULL if no auth */
152 char *psz_password; /* NULL if no auth */
154 vlc_bool_t b_stream; /* if false: httpd will retreive data by a callback
155 true: it's up to the program to give data to httpd */
156 void *p_sys; /* provided for user */
157 httpd_file_callback pf_get; /* it should allocate and fill *pp_data and *pi_data */
158 httpd_file_callback pf_post; /* it should allocate and fill *pp_data and *pi_data */
162 /* circular buffer for stream only */
163 int i_buffer_size; /* buffer size, can't be reallocated smaller */
164 uint8_t *p_buffer; /* buffer */
165 int64_t i_buffer_pos; /* absolute position from begining */
166 int i_buffer_last_pos; /* a new connection will start with that */
168 /* data to be send at connection time (if any) */
174 #define HTTPD_CONNECTION_RECEIVING_REQUEST 1
175 #define HTTPD_CONNECTION_SENDING_HEADER 2
176 #define HTTPD_CONNECTION_SENDING_FILE 3
177 #define HTTPD_CONNECTION_SENDING_STREAM 4
178 #define HTTPD_CONNECTION_TO_BE_CLOSED 5
180 #define HTTPD_CONNECTION_METHOD_GET 1
181 #define HTTPD_CONNECTION_METHOD_POST 2
182 typedef struct httpd_connection_s
184 struct httpd_connection_s *p_next;
185 struct httpd_connection_s *p_prev;
187 struct sockaddr_in sock;
189 mtime_t i_last_activity_date;
192 int i_method; /* get/post */
194 char *psz_file; // file to be send
195 int i_http_error; // error to be send with the file
196 char *psz_user; // if Authorization in the request header
199 uint8_t *p_request; // whith get: ?<*>, with post: main data
202 httpd_file_t *p_file;
204 /* used while sending header and file */
207 int i_buffer; /* private */
209 /* used for stream */
210 int64_t i_stream_pos; /* absolute pos in stream */
211 } httpd_connection_t;
213 /* Linked List of banned IP */
214 typedef struct httpd_banned_ip_s
216 struct httpd_banned_ip_s *p_next;
217 struct httpd_banned_ip_s *p_prev;
229 vlc_mutex_t host_lock;
230 volatile int i_host_count;
233 vlc_mutex_t file_lock;
237 vlc_mutex_t connection_lock;
238 int i_connection_count;
239 httpd_connection_t *p_first_connection;
241 vlc_mutex_t ban_lock;
242 int i_banned_ip_count;
243 httpd_banned_ip_t *p_first_banned_ip;
246 static void httpd_Thread( httpd_sys_t *p_httpt );
247 static void httpd_ConnnectionNew( httpd_sys_t *, int , struct sockaddr_in * );
248 static void httpd_ConnnectionClose( httpd_sys_t *, httpd_connection_t * );
249 static int httpd_UnbanIP( httpd_sys_t *, httpd_banned_ip_t *);
250 static int httpd_BanIP( httpd_sys_t *, char *);
251 static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *, char * );
252 /*****************************************************************************
254 *****************************************************************************/
256 static int Open( vlc_object_t *p_this )
258 httpd_t *p_httpd = (httpd_t*)p_this;
259 httpd_sys_t *p_httpt;
261 /* Launch httpt thread */
262 if( !( p_httpt = vlc_object_create( p_this, sizeof( httpd_sys_t ) ) ) )
264 msg_Err( p_this, "out of memory" );
265 return( VLC_EGENERIC );
271 /* init httpt_t structure */
272 vlc_mutex_init( p_httpd, &p_httpt->host_lock );
273 p_httpt->i_host_count = 0;
274 p_httpt->host = NULL;
276 vlc_mutex_init( p_httpd, &p_httpt->file_lock );
277 p_httpt->i_file_count = 0;
278 p_httpt->file = NULL;
280 vlc_mutex_init( p_httpd, &p_httpt->connection_lock );
281 p_httpt->i_connection_count = 0;
282 p_httpt->p_first_connection = NULL;
284 vlc_mutex_init( p_httpd, &p_httpt->ban_lock );
285 p_httpt->i_banned_ip_count = 0;
286 p_httpt->p_first_banned_ip = NULL;
288 /* start the thread */
289 if( vlc_thread_create( p_httpt, "httpd thread",
290 httpd_Thread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
292 msg_Err( p_this, "cannot spawn http thread" );
294 vlc_mutex_destroy( &p_httpt->host_lock );
295 vlc_mutex_destroy( &p_httpt->file_lock );
296 vlc_mutex_destroy( &p_httpt->connection_lock );
297 vlc_mutex_destroy( &p_httpt->ban_lock );
299 vlc_object_destroy( p_httpt );
300 return( VLC_EGENERIC );
303 msg_Info( p_httpd, "http thread launched" );
305 p_httpd->p_sys = p_httpt;
306 p_httpd->pf_register_host = RegisterHost;
307 p_httpd->pf_unregister_host = UnregisterHost;
308 p_httpd->pf_register_file = RegisterFile;
309 p_httpd->pf_unregister_file = UnregisterFile;
310 p_httpd->pf_register_stream = RegisterStream;
311 p_httpd->pf_header_stream = HeaderStream;
312 p_httpd->pf_send_stream = SendStream;
313 p_httpd->pf_unregister_stream=UnregisterStream;
315 return( VLC_SUCCESS );
318 /*****************************************************************************
319 * Close: close the target
320 *****************************************************************************/
321 static void Close( vlc_object_t * p_this )
323 httpd_t *p_httpd = (httpd_t*)p_this;
324 httpd_sys_t *p_httpt = p_httpd->p_sys;
326 httpd_connection_t *p_con;
327 httpd_banned_ip_t *p_banned_ip;
332 vlc_thread_join( p_httpt );
334 /* first close all host */
335 vlc_mutex_destroy( &p_httpt->host_lock );
336 if( p_httpt->i_host_count )
338 msg_Err( p_httpd, "still have %d hosts registered !", p_httpt->i_host_count );
340 for( i = 0; i < p_httpt->i_host_count; i++ )
342 #define p_host p_httpt->host[i]
343 FREE( p_host->psz_host_addr );
344 SOCKET_CLOSE( p_host->fd );
349 FREE( p_httpt->host );
352 vlc_mutex_destroy( &p_httpt->file_lock );
353 if( p_httpt->i_file_count )
355 msg_Err( p_httpd, "still have %d files registered !", p_httpt->i_file_count );
357 for( i = 0; i < p_httpt->i_file_count; i++ )
359 #define p_file p_httpt->file[i]
360 FREE( p_file->psz_file );
361 FREE( p_file->psz_mime );
362 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
364 FREE( p_file->psz_user );
365 FREE( p_file->psz_password );
367 FREE( p_file->p_buffer );
372 FREE( p_httpt->file );
374 /* andd close all connection */
375 vlc_mutex_destroy( &p_httpt->connection_lock );
376 if( p_httpt->i_connection_count )
378 msg_Warn( p_httpd, "%d connections still in use", p_httpt->i_connection_count );
380 while( ( p_con = p_httpt->p_first_connection ) )
382 httpd_ConnnectionClose( p_httpt, p_con );
385 /* Free all banned IP */
386 vlc_mutex_destroy( &p_httpt->ban_lock );
387 while( ( p_banned_ip = p_httpt->p_first_banned_ip))
389 httpd_UnbanIP(p_httpt,p_banned_ip);
392 msg_Info( p_httpd, "httpd instance closed" );
393 vlc_object_destroy( p_httpt );
397 /****************************************************************************
398 ****************************************************************************
401 ****************************************************************************
402 ****************************************************************************/
403 static int BuildAddr( struct sockaddr_in * p_socket,
404 const char * psz_address, int i_port )
407 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
408 p_socket->sin_family = AF_INET; /* family */
409 p_socket->sin_port = htons( (uint16_t)i_port );
412 p_socket->sin_addr.s_addr = INADDR_ANY;
416 struct hostent * p_hostent;
418 /* Try to convert address directly from in_addr - this will work if
419 * psz_address is dotted decimal. */
420 #ifdef HAVE_ARPA_INET_H
421 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
423 p_socket->sin_addr.s_addr = inet_addr( psz_address );
424 if( p_socket->sin_addr.s_addr == INADDR_NONE )
427 /* We have a fqdn, try to find its address */
428 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
433 /* Copy the first address of the host in the socket address */
434 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
435 p_hostent->h_length );
443 * listen on a host for a httpd instance
446 static httpd_host_t *_RegisterHost( httpd_sys_t *p_httpt, char *psz_host_addr, int i_port )
448 httpd_host_t *p_host;
449 struct sockaddr_in sock;
453 #if !defined( WIN32 ) && !defined( UNDER_CE )
457 if( BuildAddr( &sock, psz_host_addr, i_port ) )
459 msg_Err( p_httpt, "cannot build address for %s:%d", psz_host_addr, i_port );
463 /* is it already declared ? */
464 vlc_mutex_lock( &p_httpt->host_lock );
465 for( i = 0; i < p_httpt->i_host_count; i++ )
467 if( p_httpt->host[i]->sock.sin_port == sock.sin_port &&
468 ( p_httpt->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
469 p_httpt->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
475 if( i < p_httpt->i_host_count )
477 /* yes, increment ref count and succed */
478 p_httpt->host[i]->i_ref++;
479 vlc_mutex_unlock( &p_httpt->host_lock );
480 return( p_httpt->host[i] );
483 /* need to add a new listening socket */
486 fd = socket( AF_INET, SOCK_STREAM, 0 );
489 msg_Err( p_httpt, "cannot open socket" );
494 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
495 (void *) &i_opt, sizeof( i_opt ) ) < 0 )
497 msg_Warn( p_httpt, "cannot configure socket (SO_REUSEADDR)" );
500 if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
502 msg_Err( p_httpt, "cannot bind socket" );
505 /* set to non-blocking */
506 #if defined( WIN32 ) || defined( UNDER_CE )
508 unsigned long i_dummy = 1;
509 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
511 msg_Err( p_httpt, "cannot set socket to non-blocking mode" );
516 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
518 msg_Err( p_httpt, "cannot F_GETFL socket" );
521 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
523 msg_Err( p_httpt, "cannot F_SETFL O_NONBLOCK" );
528 if( listen( fd, LISTEN_BACKLOG ) < 0 )
530 msg_Err( p_httpt, "cannot listen socket" );
536 p_httpt->host = realloc( p_httpt->host, sizeof( httpd_host_t *) * ( p_httpt->i_host_count + 1 ) );
540 p_httpt->host = malloc( sizeof( httpd_host_t *) );
542 p_host = malloc( sizeof( httpd_host_t ) );
544 p_host->psz_host_addr = strdup( psz_host_addr );
545 p_host->i_port = i_port;
549 p_httpt->host[p_httpt->i_host_count++] = p_host;
550 vlc_mutex_unlock( &p_httpt->host_lock );
555 vlc_mutex_unlock( &p_httpt->host_lock );
562 static httpd_host_t *RegisterHost( httpd_t *p_httpd, char *psz_host_addr, int i_port )
564 return( _RegisterHost( p_httpd->p_sys, psz_host_addr, i_port ) );
568 * remove a listening host for an httpd instance
570 static void _UnregisterHost( httpd_sys_t *p_httpt, httpd_host_t *p_host )
574 vlc_mutex_lock( &p_httpt->host_lock );
575 for( i = 0; i < p_httpt->i_host_count; i++ )
577 if( p_httpt->host[i] == p_host )
582 if( i >= p_httpt->i_host_count )
584 vlc_mutex_unlock( &p_httpt->host_lock );
585 msg_Err( p_httpt, "cannot unregister host" );
591 if( p_host->i_ref > 0 )
594 vlc_mutex_unlock( &p_httpt->host_lock );
599 FREE( p_host->psz_host_addr );
600 SOCKET_CLOSE( p_host->fd );
604 if( p_httpt->i_host_count <= 1 )
606 FREE( p_httpt->host );
607 p_httpt->i_host_count = 0;
613 i_move = p_httpt->i_host_count - i - 1;
617 memmove( &p_httpt->host[i],
619 i_move * sizeof( httpd_host_t * ) );
622 p_httpt->i_host_count--;
623 p_httpt->host = realloc( p_httpt->host,
624 p_httpt->i_host_count * sizeof( httpd_host_t * ) );
627 vlc_mutex_unlock( &p_httpt->host_lock );
629 static void UnregisterHost( httpd_t *p_httpd, httpd_host_t *p_host )
631 _UnregisterHost( p_httpd->p_sys, p_host );
635 static void __RegisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
638 if( p_httpt->i_file_count )
640 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * ( p_httpt->i_file_count + 1 ) );
644 p_httpt->file = malloc( sizeof( httpd_file_t *) );
647 p_httpt->file[p_httpt->i_file_count++] = p_file;
650 static httpd_file_t *_RegisterFile( httpd_sys_t *p_httpt,
651 char *psz_file, char *psz_mime,
652 char *psz_user, char *psz_password,
653 httpd_file_callback pf_get,
654 httpd_file_callback pf_post,
655 httpd_file_callback_args_t *p_args )
657 httpd_file_t *p_file;
660 vlc_mutex_lock( &p_httpt->file_lock );
661 for( i = 0; i < p_httpt->i_file_count; i++ )
663 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
668 if( i < p_httpt->i_file_count )
670 vlc_mutex_unlock( &p_httpt->file_lock );
671 msg_Err( p_httpt, "%s already registered", psz_file );
675 p_file = malloc( sizeof( httpd_file_t ) );
677 p_file->psz_file = strdup( psz_file );
678 p_file->psz_mime = strdup( psz_mime );
679 if( psz_user && *psz_user )
681 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
682 p_file->psz_user = strdup( psz_user );
683 p_file->psz_password = strdup( psz_password );
687 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
688 p_file->psz_user = NULL;
689 p_file->psz_password = NULL;
692 p_file->b_stream = VLC_FALSE;
693 p_file->p_sys = p_args;
694 p_file->pf_get = pf_get;
695 p_file->pf_post = pf_post;
697 p_file->i_buffer_size = 0;
698 p_file->i_buffer_last_pos = 0;
699 p_file->i_buffer_pos = 0;
700 p_file->p_buffer = NULL;
702 p_file->i_header_size = 0;
703 p_file->p_header = NULL;
705 __RegisterFile( p_httpt, p_file );
707 vlc_mutex_unlock( &p_httpt->file_lock );
711 static httpd_file_t *RegisterFile( httpd_t *p_httpd,
712 char *psz_file, char *psz_mime,
713 char *psz_user, char *psz_password,
714 httpd_file_callback pf_get,
715 httpd_file_callback pf_post,
716 httpd_file_callback_args_t *p_args )
718 return( _RegisterFile( p_httpd->p_sys,
719 psz_file, psz_mime, psz_user, psz_password,
720 pf_get, pf_post, p_args ) );
723 static httpd_stream_t *_RegisterStream( httpd_sys_t *p_httpt,
724 char *psz_file, char *psz_mime,
725 char *psz_user, char *psz_password )
727 httpd_stream_t *p_stream;
730 vlc_mutex_lock( &p_httpt->file_lock );
731 for( i = 0; i < p_httpt->i_file_count; i++ )
733 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
738 if( i < p_httpt->i_file_count )
740 vlc_mutex_unlock( &p_httpt->file_lock );
741 msg_Err( p_httpt, "%s already registered", psz_file );
745 p_stream = malloc( sizeof( httpd_stream_t ) );
747 p_stream->psz_file = strdup( psz_file );
748 p_stream->psz_mime = strdup( psz_mime );
749 if( psz_user && *psz_user )
751 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
752 p_stream->psz_user = strdup( psz_user );
753 p_stream->psz_password = strdup( psz_password );
757 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
758 p_stream->psz_user = NULL;
759 p_stream->psz_password = NULL;
762 p_stream->b_stream = VLC_TRUE;
763 p_stream->p_sys = NULL;
764 p_stream->pf_get = NULL;
765 p_stream->pf_post = NULL;
767 p_stream->i_buffer_size = 5*1024*1024;
768 p_stream->i_buffer_pos = 0;
769 p_stream->i_buffer_last_pos = 0;
770 p_stream->p_buffer = malloc( p_stream->i_buffer_size );
772 p_stream->i_header_size = 0;
773 p_stream->p_header = NULL;
775 __RegisterFile( p_httpt, p_stream );
777 vlc_mutex_unlock( &p_httpt->file_lock );
781 static httpd_stream_t *RegisterStream( httpd_t *p_httpd,
782 char *psz_file, char *psz_mime,
783 char *psz_user, char *psz_password )
785 return( _RegisterStream( p_httpd->p_sys,
786 psz_file, psz_mime, psz_user, psz_password ) );
789 static void _UnregisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
793 vlc_mutex_lock( &p_httpt->file_lock );
794 for( i = 0; i < p_httpt->i_file_count; i++ )
796 if( !strcmp( p_file->psz_file, p_httpt->file[i]->psz_file ) )
801 if( i >= p_httpt->i_file_count )
803 vlc_mutex_unlock( &p_httpt->file_lock );
804 msg_Err( p_httpt, "cannot unregister file" );
808 if( p_file->i_ref > 0 )
810 httpd_connection_t *p_con;
811 /* force closing all connection for this file */
812 msg_Err( p_httpt, "closing all client connection" );
814 vlc_mutex_lock( &p_httpt->connection_lock );
815 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
817 httpd_connection_t *p_next;
819 p_next = p_con->p_next;
820 if( p_con->p_file == p_file )
822 httpd_ConnnectionClose( p_httpt, p_con );
826 vlc_mutex_unlock( &p_httpt->connection_lock );
829 FREE( p_file->psz_file );
830 FREE( p_file->psz_mime );
831 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
833 FREE( p_file->psz_user );
834 FREE( p_file->psz_password );
836 FREE( p_file->p_buffer );
837 FREE( p_file->p_header );
842 if( p_httpt->i_file_count == 1 )
844 FREE( p_httpt->file );
845 p_httpt->i_file_count = 0;
851 i_move = p_httpt->i_file_count - i - 1;
854 memmove( &p_httpt->file[i], &p_httpt->file[i + 1], sizeof( httpd_file_t *) * i_move );
856 p_httpt->i_file_count--;
857 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * p_httpt->i_file_count );
860 vlc_mutex_unlock( &p_httpt->file_lock );
862 static void UnregisterFile( httpd_t *p_httpd, httpd_file_t *p_file )
864 _UnregisterFile( p_httpd->p_sys, p_file );
867 static void UnregisterStream( httpd_t *p_httpd, httpd_stream_t *p_stream )
869 _UnregisterFile( p_httpd->p_sys, p_stream );
874 static int _SendStream( httpd_sys_t *p_httpt, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
879 if( i_data <= 0 || p_data == NULL )
881 return( VLC_SUCCESS );
883 //fprintf( stderr, "## i_data=%d pos=%lld\n", i_data, p_stream->i_buffer_pos );
885 vlc_mutex_lock( &p_httpt->file_lock );
887 /* save this pointer (to be used by new connection) */
888 p_stream->i_buffer_last_pos = p_stream->i_buffer_pos;
890 i_pos = p_stream->i_buffer_pos % p_stream->i_buffer_size;
896 i_copy = __MIN( i_count, p_stream->i_buffer_size - i_pos );
898 memcpy( &p_stream->p_buffer[i_pos],
902 i_pos = ( i_pos + i_copy ) % p_stream->i_buffer_size;
907 p_stream->i_buffer_pos += i_data;
908 vlc_mutex_unlock( &p_httpt->file_lock );
910 return( VLC_SUCCESS );
912 static int SendStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
914 return( _SendStream( p_httpd->p_sys, p_stream, p_data, i_data ) );
917 static int HeaderStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
919 httpd_sys_t *p_httpt = p_httpd->p_sys;
921 vlc_mutex_lock( &p_httpt->file_lock );
923 FREE( p_stream->p_header );
924 if( p_data == NULL || i_data <= 0 )
926 p_stream->i_header_size = 0;
930 p_stream->i_header_size = i_data;
931 p_stream->p_header = malloc( i_data );
932 memcpy( p_stream->p_header,
936 vlc_mutex_unlock( &p_httpt->file_lock );
938 return( VLC_SUCCESS );
941 /****************************************************************************/
942 /****************************************************************************/
943 /****************************************************************************/
944 /****************************************************************************/
945 /****************************************************************************/
947 static int httpd_page_401_get( httpd_file_callback_args_t *p_args,
948 uint8_t *p_request, int i_request,
949 uint8_t **pp_data, int *pi_data )
953 p = *pp_data = malloc( 1024 );
955 p += sprintf( p, "<html>\n" );
956 p += sprintf( p, "<head>\n" );
957 p += sprintf( p, "<title>Error 401</title>\n" );
958 p += sprintf( p, "</head>\n" );
959 p += sprintf( p, "<body>\n" );
960 p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
961 p += sprintf( p, "<hr />\n" );
962 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
963 p += sprintf( p, "</body>\n" );
964 p += sprintf( p, "</html>\n" );
966 *pi_data = strlen( *pp_data ) + 1;
970 static int httpd_page_404_get( httpd_file_callback_args_t *p_args,
971 uint8_t *p_request, int i_request,
972 uint8_t **pp_data, int *pi_data )
976 p = *pp_data = malloc( 1024 );
978 p += sprintf( p, "<html>\n" );
979 p += sprintf( p, "<head>\n" );
980 p += sprintf( p, "<title>Error 404</title>\n" );
981 p += sprintf( p, "</head>\n" );
982 p += sprintf( p, "<body>\n" );
983 p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
984 p += sprintf( p, "<hr />\n" );
985 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
986 p += sprintf( p, "</body>\n" );
987 p += sprintf( p, "</html>\n" );
989 *pi_data = strlen( *pp_data ) + 1;
995 static int _httpd_page_admin_get_status( httpd_file_callback_args_t *p_args,
996 uint8_t **pp_data, int *pi_data )
998 httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
999 httpd_connection_t *p_con;
1000 httpd_banned_ip_t *p_ip;
1005 /* FIXME FIXME do not use static size FIXME FIXME*/
1006 p = *pp_data = malloc( 8096 );
1008 p += sprintf( p, "<html>\n" );
1009 p += sprintf( p, "<head>\n" );
1010 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1011 p += sprintf( p, "</head>\n" );
1012 p += sprintf( p, "<body>\n" );
1013 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1014 p += sprintf( p, "<h2><center>Admin page</center></h2>\n" );
1017 p += sprintf( p, "<h3>General state</h3>\n" );
1018 p += sprintf( p, "<ul>\n" );
1019 p += sprintf( p, "<li>Connection count: %d</li>\n", p_httpt->i_connection_count );
1020 //p += sprintf( p, "<li>Total bandwith: %d</li>\n", -1 );
1021 /*p += sprintf( p, "<li></li>\n" );*/
1022 p += sprintf( p, "<li>Ban count: %d</li>\n", p_httpt->i_banned_ip_count );
1023 p += sprintf( p, "</ul>\n" );
1026 /* XXX do not lock on ban_lock */
1027 p += sprintf( p, "<h3>Ban list</h3>\n" );
1028 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1029 p += sprintf( p, "<tr>\n<th>IP</th>\n<th>Action</th></tr>\n" );
1030 for( p_ip = p_httpt->p_first_banned_ip;p_ip != NULL; p_ip = p_ip->p_next )
1032 p += sprintf( p, "<tr>\n" );
1033 p += sprintf( p, "<td>%s</td>\n", p_ip->psz_ip );
1034 p += sprintf( p, "<td><form method=\"get\" action=\"\">"
1035 "<select name=\"action\">"
1036 "<option selected>unban_ip</option>"
1038 "<input type=\"hidden\" name=\"id\" value=\"%s\"/>"
1039 "<input type=\"submit\" value=\"Do it\" />"
1040 "</form></td>\n", p_ip->psz_ip);
1041 p += sprintf( p, "</tr>\n" );
1043 p += sprintf( p, "</table>\n" );
1048 vlc_mutex_lock( &p_httpt->host_lock );
1049 p += sprintf( p, "<h3>Host list</h3>\n" );
1050 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1051 p += sprintf( p, "<tr>\n<th>Host</th><th>Port</th><th>IP</th>\n</tr>\n" );
1053 for( i = 0; i < p_httpt->i_host_count; i++ )
1055 p += sprintf( p, "<tr>\n" );
1056 p += sprintf( p, "<td>%s</td>\n", p_httpt->host[i]->psz_host_addr );
1057 p += sprintf( p, "<td>%d</td>\n", p_httpt->host[i]->i_port );
1058 p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_httpt->host[i]->sock.sin_addr ) );
1059 p += sprintf( p, "</tr>\n" );
1061 p += sprintf( p, "</table>\n" );
1062 vlc_mutex_unlock( &p_httpt->host_lock );
1065 /* XXX do not take lock on file_lock */
1066 p += sprintf( p, "<h3>File list</h3>\n" );
1067 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1068 p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1070 for( i = 0; i < p_httpt->i_file_count; i++ )
1072 if( !p_httpt->file[i]->b_stream )
1074 p += sprintf( p, "<tr>\n" );
1075 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1076 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1077 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1078 p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1079 p += sprintf( p, "</tr>\n" );
1082 p += sprintf( p, "</table>\n" );
1085 /* XXX do not take lock on file_lock */
1086 p += sprintf( p, "<h3>Stream list</h3>\n" );
1087 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1088 p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1090 for( i = 0; i < p_httpt->i_file_count; i++ )
1092 if( p_httpt->file[i]->b_stream )
1094 p += sprintf( p, "<tr>\n" );
1095 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1096 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1097 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1098 p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1099 p += sprintf( p, "</tr>\n" );
1102 p += sprintf( p, "</table>\n" );
1104 /* connection list */
1105 /* XXX do not take lock on connection_lock */
1106 p += sprintf( p, "<h3>Connection list</h3>\n" );
1107 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1108 p += sprintf( p, "<tr>\n<th>IP</th><th>Requested File</th><th>Status</th><th>Action</th>\n</tr>\n" );
1110 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1112 p += sprintf( p, "<tr>\n" );
1113 p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_con->sock.sin_addr ) );
1114 p += sprintf( p, "<td>%s</td>\n", p_con->psz_file );
1115 p += sprintf( p, "<td>%d</td>\n", p_con->i_http_error );
1116 p += sprintf( p, "<td><form method=\"get\" action=\"\">"
1117 "<select name=\"action\">"
1118 "<option selected>close_connection</option>"
1119 "<option>ban_ip</option>"
1120 "<option>close_connection_and_ban_ip</option>"
1122 "<input type=\"hidden\" name=\"id\" value=\"%p\"/>"
1123 "<input type=\"submit\" value=\"Do it\" />"
1124 "</form></td>\n", p_con);
1125 p += sprintf( p, "</tr>\n" );
1127 p += sprintf( p, "</table>\n" );
1130 /* www.videolan.org */
1131 p += sprintf( p, "<hr />\n" );
1132 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1133 p += sprintf( p, "</body>\n" );
1134 p += sprintf( p, "</html>\n" );
1136 *pi_data = strlen( *pp_data ) + 1;
1138 return( VLC_SUCCESS );
1141 static int _httpd_page_admin_get_success( httpd_file_callback_args_t *p_args,
1142 uint8_t **pp_data, int *pi_data,
1147 p = *pp_data = malloc( 8096 );
1149 p += sprintf( p, "<html>\n" );
1150 p += sprintf( p, "<head>\n" );
1151 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1152 p += sprintf( p, "</head>\n" );
1153 p += sprintf( p, "<body>\n" );
1154 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1156 p += sprintf( p, "<p>Success=`%s'</p>", psz_msg );
1157 p += sprintf( p, "<a href=\"admin.html\">Retour a la page d'administration</a>\n" );
1159 p += sprintf( p, "<hr />\n" );
1160 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1161 p += sprintf( p, "</body>\n" );
1162 p += sprintf( p, "</html>\n" );
1164 *pi_data = strlen( *pp_data ) + 1;
1166 return( VLC_SUCCESS );
1169 static int _httpd_page_admin_get_error( httpd_file_callback_args_t *p_args,
1170 uint8_t **pp_data, int *pi_data,
1175 p = *pp_data = malloc( 8096 );
1177 p += sprintf( p, "<html>\n" );
1178 p += sprintf( p, "<head>\n" );
1179 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1180 p += sprintf( p, "</head>\n" );
1181 p += sprintf( p, "<body>\n" );
1182 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1184 p += sprintf( p, "<p>Error=`%s'</p>", psz_error );
1185 p += sprintf( p, "<a href=\"admin.html\">Retour a la page d'administration</a>\n" );
1187 p += sprintf( p, "<hr />\n" );
1188 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1189 p += sprintf( p, "</body>\n" );
1190 p += sprintf( p, "</html>\n" );
1192 *pi_data = strlen( *pp_data ) + 1;
1194 return( VLC_SUCCESS );
1197 static void _httpd_uri_extract_value( char *psz_uri, char *psz_name, char *psz_value, int i_value_max )
1201 p = strstr( psz_uri, psz_name );
1206 p += strlen( psz_name );
1207 if( *p == '=' ) p++;
1209 if( strchr( p, '&' ) )
1211 i_len = strchr( p, '&' ) - p;
1215 i_len = strlen( p );
1217 i_len = __MIN( i_value_max - 1, i_len );
1220 strncpy( psz_value, p, i_len );
1221 psz_value[i_len] = '\0';
1225 strncpy( psz_value, "", i_value_max );
1230 strncpy( psz_value, "", i_value_max );
1235 static int httpd_page_admin_get( httpd_file_callback_args_t *p_args,
1236 uint8_t *p_request, int i_request,
1237 uint8_t **pp_data, int *pi_data )
1239 httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
1240 httpd_connection_t *p_con;
1246 _httpd_uri_extract_value( p_request, "action", action, 512 );
1248 if( !strcmp( action, "close_connection" ) )
1253 _httpd_uri_extract_value( p_request, "id", id, 512 );
1254 i_id = (void*)strtol( id, NULL, 0 );
1255 msg_Dbg( p_httpt, "requested closing connection id=%s %p", id, i_id );
1256 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1258 if( (void*)p_con == i_id )
1260 /* XXX don't free p_con as it could be the one that it is sending ... */
1261 p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
1262 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "connection closed" ) );
1265 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
1267 else if( !strcmp( action, "ban_ip" ) )
1272 _httpd_uri_extract_value( p_request, "id", id, 512 );
1273 i_id = (void*)strtol( id, NULL, 0 );
1275 msg_Dbg( p_httpt, "requested banning ip id=%s %p", id, i_id );
1277 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1279 if( (void*)p_con == i_id )
1281 if( httpd_BanIP( p_httpt,inet_ntoa( p_con->sock.sin_addr ) ) == 0)
1282 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "IP banned" ) );
1288 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1290 else if( !strcmp( action, "unban_ip" ) )
1294 _httpd_uri_extract_value( p_request, "id", id, 512 );
1295 msg_Dbg( p_httpt, "requested unbanning ip %s", id);
1297 if( httpd_UnbanIP( p_httpt, httpd_GetbannedIP ( p_httpt, id ) ) == 0)
1298 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "IP Unbanned" ) );
1300 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1302 else if( !strcmp( action, "close_connection_and_ban_ip" ) )
1307 _httpd_uri_extract_value( p_request, "id", id, 512 );
1308 i_id = (void*)strtol( id, NULL, 0 );
1309 msg_Dbg( p_httpt, "requested closing connection and banning ip id=%s %p", id, i_id );
1310 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1312 if( (void*)p_con == i_id )
1314 /* XXX don't free p_con as it could be the one that it is sending ... */
1315 p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
1317 if( httpd_BanIP( p_httpt,inet_ntoa( p_con->sock.sin_addr ) ) == 0)
1318 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "Connection closed and IP banned" ) );
1324 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
1327 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1331 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1336 return( _httpd_page_admin_get_status( p_args, pp_data, pi_data ) );
1342 static int httpd_BanIP( httpd_sys_t *p_httpt, char * psz_new_banned_ip)
1344 httpd_banned_ip_t *p_new_banned_ip ;
1346 p_new_banned_ip = malloc( sizeof( httpd_banned_ip_t ) );
1347 if( !p_new_banned_ip )
1351 p_new_banned_ip->p_next=NULL;
1352 p_new_banned_ip->psz_ip = malloc( strlen( psz_new_banned_ip ) + 1 );
1353 if( !p_new_banned_ip->psz_ip )
1358 strcpy( p_new_banned_ip->psz_ip, psz_new_banned_ip );
1360 msg_Dbg( p_httpt, "Banning IP %s", psz_new_banned_ip );
1362 if( p_httpt->p_first_banned_ip )
1364 httpd_banned_ip_t *p_last;
1366 p_last = p_httpt->p_first_banned_ip;
1367 while( p_last->p_next )
1369 p_last = p_last->p_next;
1372 p_last->p_next = p_new_banned_ip;
1373 p_new_banned_ip->p_prev = p_last;
1377 p_new_banned_ip->p_prev = NULL;
1379 p_httpt->p_first_banned_ip = p_new_banned_ip;
1382 p_httpt->i_banned_ip_count++;
1386 static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *p_httpt, char *psz_ip )
1388 httpd_banned_ip_t *p_ip;
1390 p_ip = p_httpt->p_first_banned_ip;
1394 if( strcmp( psz_ip, p_ip->psz_ip ) == 0 )
1398 p_ip = p_ip->p_next;
1404 static int httpd_UnbanIP( httpd_sys_t *p_httpt, httpd_banned_ip_t *p_banned_ip )
1411 msg_Dbg( p_httpt, "Unbanning IP %s",p_banned_ip->psz_ip);
1413 /* first cut out from list */
1414 if( p_banned_ip->p_prev )
1416 p_banned_ip->p_prev->p_next = p_banned_ip->p_next;
1420 p_httpt->p_first_banned_ip = p_banned_ip->p_next;
1423 if( p_banned_ip->p_next )
1425 p_banned_ip->p_next->p_prev = p_banned_ip->p_prev;
1428 FREE( p_banned_ip->psz_ip );
1429 FREE( p_banned_ip );
1431 p_httpt->i_banned_ip_count--;
1437 static void httpd_ConnnectionNew( httpd_sys_t *p_httpt, int fd, struct sockaddr_in *p_sock )
1439 httpd_connection_t *p_con;
1441 msg_Dbg( p_httpt, "new connection from %s", inet_ntoa( p_sock->sin_addr ) );
1443 /* verify if it's a banned ip */
1444 if(httpd_GetbannedIP( p_httpt,inet_ntoa( p_sock->sin_addr ) ) )
1446 msg_Dbg( p_httpt, "Ip %s banned : closing connection", inet_ntoa( p_sock->sin_addr ) );
1451 /* create a new connection and link it */
1452 p_con = malloc( sizeof( httpd_connection_t ) );
1453 p_con->i_state = HTTPD_CONNECTION_RECEIVING_REQUEST;
1455 p_con->i_last_activity_date = mdate();
1457 p_con->sock = *p_sock;
1458 p_con->psz_file = NULL;
1459 p_con->i_http_error = 0;
1460 p_con->psz_user = NULL;
1461 p_con->psz_password = NULL;
1462 p_con->p_file = NULL;
1464 p_con->i_request_size = 0;
1465 p_con->p_request = NULL;
1467 p_con->i_buffer = 0;
1468 p_con->i_buffer_size = 8096;
1469 p_con->p_buffer = malloc( p_con->i_buffer_size );
1471 p_con->i_stream_pos = 0; // updated by httpd_thread */
1472 p_con->p_next = NULL;
1474 if( p_httpt->p_first_connection )
1476 httpd_connection_t *p_last;
1478 p_last = p_httpt->p_first_connection;
1479 while( p_last->p_next )
1481 p_last = p_last->p_next;
1484 p_last->p_next = p_con;
1485 p_con->p_prev = p_last;
1489 p_con->p_prev = NULL;
1491 p_httpt->p_first_connection = p_con;
1494 p_httpt->i_connection_count++;
1497 static void httpd_ConnnectionClose( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1499 msg_Dbg( p_httpt, "close connection from %s", inet_ntoa( p_con->sock.sin_addr ) );
1501 p_httpt->i_connection_count--;
1502 /* first cut out from list */
1505 p_con->p_prev->p_next = p_con->p_next;
1509 p_httpt->p_first_connection = p_con->p_next;
1514 p_con->p_next->p_prev = p_con->p_prev;
1517 if( p_con->p_file ) p_con->p_file->i_ref--;
1518 FREE( p_con->psz_file );
1520 FREE( p_con->p_buffer );
1521 SOCKET_CLOSE( p_con->fd );
1523 FREE( p_con->psz_user );
1524 FREE( p_con->psz_password );
1526 FREE( p_con->p_request );
1530 static void httpd_RequestGetWord( char *word, int i_word_max, char **pp_buffer, char *p_end )
1532 char *p = *pp_buffer;
1535 while( p < p_end && *p && ( *p == ' ' || *p == '\t' ) )
1541 for( i = 0; i < i_word_max && p < p_end && *p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r'; i++,p++)
1546 word[__MIN( i, i_word_max -1 )] = '\0';
1551 static int httpd_RequestNextLine( char **pp_buffer, char *p_end )
1555 for( p = *pp_buffer; p < p_end; p++ )
1557 if( p + 1 < p_end && *p == '\n' )
1562 if( p + 2 < p_end && p[0] == '\r' && p[1] == '\n' )
1569 return VLC_EGENERIC;
1572 //char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1573 static void b64_decode( char *dest, char *src )
1578 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1579 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1580 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1581 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1582 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1583 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1584 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1585 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1586 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1587 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1588 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1589 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1590 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1591 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1592 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1593 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1596 for( i_level = 0; *src != '\0'; src++ )
1600 c = b64[(unsigned int)*src];
1613 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1617 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1621 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1630 static void httpd_ConnectionParseRequest( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1639 char user[512] = "";
1640 char password[512] = "";
1642 //msg_Dbg( p_httpt, "new request=\n%s", p_con->p_buffer );
1645 p = p_con->p_buffer;
1646 p_end = p + strlen( p ) + 1;
1648 httpd_RequestGetWord( command, 32, &p, p_end );
1649 httpd_RequestGetWord( url, 1024, &p, p_end );
1650 httpd_RequestGetWord( version, 32, &p, p_end );
1651 //msg_Dbg( p_httpt, "ask =%s= =%s= =%s=", command, url, version );
1653 p_con->p_request = NULL;
1654 p_con->i_request_size = 0;
1655 if( !strcmp( command, "GET" ) )
1657 p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1659 else if( !strcmp( command, "POST" ))
1661 p_con->i_method = HTTPD_CONNECTION_METHOD_POST;
1666 p_con->psz_file = strdup( "/501.html" );
1667 p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1668 p_con->i_http_error = 501;
1672 if( strcmp( version, "HTTP/1.0" ) && strcmp( version, "HTTP/1.1" ) )
1674 p_con->psz_file = strdup( "/505.html" );
1675 p_con->i_http_error = 505;
1685 if( httpd_RequestNextLine( &p, p_end ) )
1687 //msg_Dbg( p_httpt, "failled new line" );
1690 //msg_Dbg( p_httpt, "new line=%s", p );
1692 httpd_RequestGetWord( header, 1024, &p, p_end );
1693 if( !strcmp( header, "\r\n" ) || !strcmp( header, "\n" ) )
1698 if( !strcmp( header, "Authorization:" ) )
1702 httpd_RequestGetWord( method, 32, &p, p_end );
1703 if( !strcasecmp( method, "BASIC" ) )
1708 httpd_RequestGetWord( basic, 1024, &p, p_end );
1709 //msg_Dbg( p_httpt, "Authorization: basic:%s", basic );
1710 b64_decode( decoded, basic );
1712 //msg_Dbg( p_httpt, "Authorization: decoded:%s", decoded );
1713 if( strchr( decoded, ':' ) )
1715 char *p = strchr( decoded, ':' );
1718 strcpy( user, decoded );
1719 strcpy( password, p );
1725 if( strchr( url, '?' ) )
1727 char *p_request = strchr( url, '?' );
1728 *p_request++ = '\0';
1729 p_con->psz_file = strdup( url );
1730 p_con->p_request = strdup( p_request );
1731 p_con->i_request_size = strlen( p_con->p_request );
1735 p_con->psz_file = strdup( url );
1739 if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1742 if( strstr( p_con->p_buffer, "\r\n\r\n" ) )
1744 p_request = strstr( p_con->p_buffer, "\r\n\r\n" ) + 4;
1746 else if( strstr( p_con->p_buffer, "\n\n" ) )
1748 p_request = strstr( p_con->p_buffer, "\n\n" ) + 2;
1754 if( p_request && p_request < p_end )
1756 p_con->i_request_size = p_end - p_request;
1757 p_con->p_request = malloc( p_con->i_request_size + 1);
1759 memcpy( p_con->p_request,
1761 p_con->i_request_size );
1763 p_con->p_request[p_con->i_request_size] = '\0';
1766 p_con->i_http_error = 200;
1769 //msg_Dbg( p_httpt, "ask %s %s %d", command, p_con->psz_file, p_con->i_http_error );
1770 FREE( p_con->p_buffer );
1771 p_con->i_buffer = 0;
1772 p_con->i_buffer_size = 0;
1774 //vlc_mutex_lock( &p_httpt->file_lock );
1777 p_con->p_file = NULL;
1778 for( i = 0; i < p_httpt->i_file_count; i++ )
1780 if( !strcmp( p_httpt->file[i]->psz_file, p_con->psz_file ) )
1782 if( p_httpt->file[i]->b_stream ||
1783 ( p_con->i_method == HTTPD_CONNECTION_METHOD_GET && p_httpt->file[i]->pf_get ) ||
1784 ( p_con->i_method == HTTPD_CONNECTION_METHOD_POST && p_httpt->file[i]->pf_post ) )
1786 p_con->p_file = p_httpt->file[i];
1792 if( !p_con->p_file )
1794 p_con->psz_file = strdup( "/404.html" );
1795 p_con->i_http_error = 404;
1797 /* XXX be sure that "/404.html" exist else ... */
1801 if( p_con->p_file->i_authenticate_method == HTTPD_AUTHENTICATE_BASIC )
1803 if( strcmp( user, p_con->p_file->psz_user ) || strcmp( password, p_con->p_file->psz_password ) )
1805 p_con->psz_file = strdup( "/401.html" );
1806 strcpy( user, p_con->p_file->psz_user );
1807 p_con->i_http_error = 401;
1809 /* XXX do not put password on 404 else ... */
1814 p_con->p_file->i_ref++;
1815 // vlc_mutex_unlock( &p_httpt->file_lock );
1817 switch( p_con->i_http_error )
1824 psz_status = "Authorization Required";
1827 psz_status = "Unknown";
1831 p_con->i_state = HTTPD_CONNECTION_SENDING_HEADER;
1833 p_con->i_buffer_size = 4096;
1834 p_con->i_buffer = 0;
1836 /* we send stream header with this one */
1837 if( p_con->i_http_error == 200 && p_con->p_file->b_stream )
1839 p_con->i_buffer_size += p_con->p_file->i_header_size;
1842 p = p_con->p_buffer = malloc( p_con->i_buffer_size );
1844 p += sprintf( p, "HTTP/1.0 %d %s\r\n", p_con->i_http_error, psz_status );
1845 p += sprintf( p, "Content-type: %s\r\n", p_con->p_file->psz_mime );
1846 if( p_con->i_http_error == 401 )
1848 p += sprintf( p, "WWW-Authenticate: Basic realm=\"%s\"\r\n", user );
1850 p += sprintf( p, "Cache-Control: no-cache\r\n" );
1851 p += sprintf( p, "\r\n" );
1853 p_con->i_buffer_size = strlen( p_con->p_buffer );// + 1;
1855 if( p_con->i_http_error == 200 && p_con->p_file->b_stream && p_con->p_file->i_header_size > 0 )
1857 /* add stream header */
1858 memcpy( &p_con->p_buffer[p_con->i_buffer_size],
1859 p_con->p_file->p_header,
1860 p_con->p_file->i_header_size );
1861 p_con->i_buffer_size += p_con->p_file->i_header_size;
1864 //msg_Dbg( p_httpt, "answer=\n%s", p_con->p_buffer );
1866 #define HTTPD_STREAM_PACKET 10000
1867 static void httpd_Thread( httpd_sys_t *p_httpt )
1869 httpd_file_t *p_page_admin;
1870 httpd_file_t *p_page_401;
1871 httpd_file_t *p_page_404;
1873 httpd_connection_t *p_con;
1875 msg_Info( p_httpt, "httpd started" );
1877 p_page_401 = _RegisterFile( p_httpt,
1878 "/401.html", "text/html",
1882 (httpd_file_callback_args_t*)NULL );
1883 p_page_404 = _RegisterFile( p_httpt,
1884 "/404.html", "text/html",
1888 (httpd_file_callback_args_t*)NULL );
1889 p_page_admin = _RegisterFile( p_httpt,
1890 "/admin.html", "text/html",
1892 httpd_page_admin_get,
1894 (httpd_file_callback_args_t*)p_httpt );
1896 while( !p_httpt->b_die )
1898 struct timeval timeout;
1901 int i_handle_max = 0;
1904 if( p_httpt->i_host_count <= 0 )
1906 msleep( 100 * 1000 );
1910 /* we will create a socket set with host and connection */
1911 FD_ZERO( &fds_read );
1912 FD_ZERO( &fds_write );
1914 vlc_mutex_lock( &p_httpt->host_lock );
1915 vlc_mutex_lock( &p_httpt->connection_lock );
1916 for( i = 0; i < p_httpt->i_host_count; i++ )
1918 FD_SET( p_httpt->host[i]->fd, &fds_read );
1919 i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd );
1921 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1923 /* no more than 10s of inactivity */
1924 if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ||
1925 p_con->i_state == HTTPD_CONNECTION_TO_BE_CLOSED)
1927 httpd_connection_t *p_next = p_con->p_next;
1929 msg_Dbg( p_httpt, "close unused connection" );
1930 httpd_ConnnectionClose( p_httpt, p_con );
1935 if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos )
1937 p_con = p_con->p_next;
1941 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1943 FD_SET( p_con->fd, &fds_read );
1947 FD_SET( p_con->fd, &fds_write );
1949 i_handle_max = __MAX( i_handle_max, p_con->fd );
1951 p_con = p_con->p_next;
1953 vlc_mutex_unlock( &p_httpt->host_lock );
1954 vlc_mutex_unlock( &p_httpt->connection_lock );
1956 /* we will wait 0.5s */
1958 timeout.tv_usec = 500*1000;
1960 i_ret = select( i_handle_max + 1,
1965 if( i_ret == -1 && errno != EINTR )
1967 msg_Warn( p_httpt, "cannot select sockets" );
1973 // msg_Dbg( p_httpt, "waiting..." );
1977 vlc_mutex_lock( &p_httpt->host_lock );
1978 /* accept/refuse new connection */
1979 for( i = 0; i < p_httpt->i_host_count; i++ )
1981 int i_sock_size = sizeof( struct sockaddr_in );
1982 struct sockaddr_in sock;
1985 fd = accept( p_httpt->host[i]->fd, (struct sockaddr *)&sock,
1989 #if defined( WIN32 ) || defined( UNDER_CE )
1991 unsigned long i_dummy = 1;
1992 ioctlsocket( fd, FIONBIO, &i_dummy );
1995 fcntl( fd, F_SETFL, O_NONBLOCK );
1998 if( p_httpt->i_connection_count >= HTTPD_MAX_CONNECTION )
2000 msg_Warn( p_httpt, "max connection reached" );
2004 /* create a new connection and link it */
2005 httpd_ConnnectionNew( p_httpt, fd, &sock );
2009 vlc_mutex_unlock( &p_httpt->host_lock );
2011 vlc_mutex_lock( &p_httpt->file_lock );
2012 /* now do work for all connections */
2013 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
2015 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
2019 i_len = recv( p_con->fd,
2020 p_con->p_buffer + p_con->i_buffer,
2021 p_con->i_buffer_size - p_con->i_buffer, 0 );
2024 #if defined( WIN32 ) || defined( UNDER_CE )
2025 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) ||
2027 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) ||
2031 httpd_connection_t *p_next = p_con->p_next;
2033 httpd_ConnnectionClose( p_httpt, p_con );
2036 else if( i_len > 0 )
2039 p_con->i_last_activity_date = mdate();
2040 p_con->i_buffer += i_len;
2042 ptr = p_con->p_buffer + p_con->i_buffer;
2044 if( ( p_con->i_buffer >= 2 && !strncmp( ptr - 2, "\n\n", 2 ) )||
2045 ( p_con->i_buffer >= 4 && !strncmp( ptr - 4, "\r\n\r\n", 4 ) ) ||
2046 p_con->i_buffer >= p_con->i_buffer_size )
2048 p_con->p_buffer[__MIN( p_con->i_buffer, p_con->i_buffer_size - 1 )] = '\0';
2049 httpd_ConnectionParseRequest( p_httpt, p_con );
2052 p_con = p_con->p_next;
2056 p_con = p_con->p_next;
2058 continue; /* just for clarity */
2060 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER || p_con->i_state == HTTPD_CONNECTION_SENDING_FILE )
2065 if( p_con->i_buffer_size - p_con->i_buffer > 0 )
2067 i_len = send( p_con->fd, p_con->p_buffer + p_con->i_buffer, p_con->i_buffer_size - p_con->i_buffer, 0 );
2073 // msg_Warn( p_httpt, "on %d send %d bytes %s", p_con->i_buffer_size, i_len, p_con->p_buffer + p_con->i_buffer );
2075 #if defined( WIN32 ) || defined( UNDER_CE )
2076 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) ||
2078 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) ||
2082 httpd_connection_t *p_next = p_con->p_next;
2084 httpd_ConnnectionClose( p_httpt, p_con );
2087 else if( i_len > 0 )
2089 p_con->i_last_activity_date = mdate();
2090 p_con->i_buffer += i_len;
2092 if( p_con->i_buffer >= p_con->i_buffer_size )
2094 if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER )
2096 p_con->i_buffer_size = 0;
2097 p_con->i_buffer = 0;
2098 FREE( p_con->p_buffer );
2100 if( !p_con->p_file->b_stream )
2102 p_con->i_state = HTTPD_CONNECTION_SENDING_FILE; // be sure to out from HTTPD_CONNECTION_SENDING_HEADER
2103 if( p_con->i_method == HTTPD_CONNECTION_METHOD_GET )
2105 p_con->p_file->pf_get( p_con->p_file->p_sys,
2106 p_con->p_request, p_con->i_request_size,
2107 &p_con->p_buffer, &p_con->i_buffer_size );
2109 else if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
2111 p_con->p_file->pf_post( p_con->p_file->p_sys,
2112 p_con->p_request, p_con->i_request_size,
2113 &p_con->p_buffer, &p_con->i_buffer_size );
2117 p_con->p_buffer = NULL;
2118 p_con->i_buffer_size = 0;
2123 p_con->i_state = HTTPD_CONNECTION_SENDING_STREAM;
2124 p_con->i_stream_pos = p_con->p_file->i_buffer_last_pos;
2126 p_con = p_con->p_next;
2130 httpd_connection_t *p_next = p_con->p_next;
2132 httpd_ConnnectionClose( p_httpt, p_con );
2138 p_con = p_con->p_next;
2143 p_con = p_con->p_next;
2145 continue; /* just for clarity */
2147 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM )
2149 httpd_stream_t *p_stream = p_con->p_file;
2153 if( p_con->i_stream_pos < p_stream->i_buffer_pos )
2156 /* check if this p_con aren't to late */
2157 if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
2159 fprintf(stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n", p_con->i_stream_pos, p_stream->i_buffer_pos );
2160 p_con->i_stream_pos = p_stream->i_buffer_last_pos;
2163 i_pos = p_con->i_stream_pos % p_stream->i_buffer_size;
2164 /* size until end of buffer */
2165 i_write = p_stream->i_buffer_size - i_pos;
2166 /* is it more than valid data */
2167 if( i_write >= p_stream->i_buffer_pos - p_con->i_stream_pos )
2169 i_write = p_stream->i_buffer_pos - p_con->i_stream_pos;
2171 /* limit to HTTPD_STREAM_PACKET */
2172 if( i_write > HTTPD_STREAM_PACKET )
2174 i_write = HTTPD_STREAM_PACKET;
2176 i_send = send( p_con->fd, &p_stream->p_buffer[i_pos], i_write, 0 );
2178 #if defined( WIN32 ) || defined( UNDER_CE )
2179 if( ( i_send < 0 && WSAGetLastError() != WSAEWOULDBLOCK )||
2181 if( ( i_send < 0 && errno != EAGAIN && errno != EINTR )||
2185 httpd_connection_t *p_next = p_con->p_next;
2187 httpd_ConnnectionClose( p_httpt, p_con );
2191 else if( i_send > 0 )
2193 msg_Dbg( p_httpt, "Sending %d bytes",i_send );
2194 p_con->i_last_activity_date = mdate();
2195 p_con->i_stream_pos += i_send;
2198 p_con = p_con->p_next;
2199 continue; /* just for clarity */
2201 else if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
2203 msg_Warn( p_httpt, "cannot occur (Invalid p_con->i_state)" );
2204 p_con = p_con->p_next;
2206 } /* for over connection */
2208 vlc_mutex_unlock( &p_httpt->file_lock );
2210 msg_Info( p_httpt, "httpd stopped" );
2212 _UnregisterFile( p_httpt, p_page_401 );
2213 _UnregisterFile( p_httpt, p_page_404 );
2214 _UnregisterFile( p_httpt, p_page_admin );