1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: httpd.c,v 1.16 2003/06/28 23:56:30 fenrir 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;
220 vlc_mutex_t host_lock;
221 volatile int i_host_count;
224 vlc_mutex_t file_lock;
228 vlc_mutex_t connection_lock;
229 int i_connection_count;
230 httpd_connection_t *p_first_connection;
233 static void httpd_Thread( httpd_sys_t *p_httpt );
234 static void httpd_ConnnectionNew( httpd_sys_t *, int , struct sockaddr_in * );
235 static void httpd_ConnnectionClose( httpd_sys_t *, httpd_connection_t * );
237 /*****************************************************************************
239 *****************************************************************************/
241 static int Open( vlc_object_t *p_this )
243 httpd_t *p_httpd = (httpd_t*)p_this;
244 httpd_sys_t *p_httpt;
246 /* Launch httpt thread */
247 if( !( p_httpt = vlc_object_create( p_this, sizeof( httpd_sys_t ) ) ) )
249 msg_Err( p_this, "out of memory" );
250 return( VLC_EGENERIC );
256 /* init httpt_t structure */
257 vlc_mutex_init( p_httpd, &p_httpt->host_lock );
258 p_httpt->i_host_count = 0;
259 p_httpt->host = NULL;
261 vlc_mutex_init( p_httpd, &p_httpt->file_lock );
262 p_httpt->i_file_count = 0;
263 p_httpt->file = NULL;
265 vlc_mutex_init( p_httpd, &p_httpt->connection_lock );
266 p_httpt->i_connection_count = 0;
267 p_httpt->p_first_connection = NULL;
269 /* start the thread */
270 if( vlc_thread_create( p_httpt, "httpd thread",
271 httpd_Thread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
273 msg_Err( p_this, "cannot spawn http thread" );
275 vlc_mutex_destroy( &p_httpt->host_lock );
276 vlc_mutex_destroy( &p_httpt->file_lock );
277 vlc_mutex_destroy( &p_httpt->connection_lock );
279 vlc_object_destroy( p_httpt );
280 return( VLC_EGENERIC );
283 msg_Info( p_httpd, "http thread launched" );
285 p_httpd->p_sys = p_httpt;
286 p_httpd->pf_register_host = RegisterHost;
287 p_httpd->pf_unregister_host = UnregisterHost;
288 p_httpd->pf_register_file = RegisterFile;
289 p_httpd->pf_unregister_file = UnregisterFile;
290 p_httpd->pf_register_stream = RegisterStream;
291 p_httpd->pf_header_stream = HeaderStream;
292 p_httpd->pf_send_stream = SendStream;
293 p_httpd->pf_unregister_stream=UnregisterStream;
295 return( VLC_SUCCESS );
298 /*****************************************************************************
299 * Close: close the target
300 *****************************************************************************/
301 static void Close( vlc_object_t * p_this )
303 httpd_t *p_httpd = (httpd_t*)p_this;
304 httpd_sys_t *p_httpt = p_httpd->p_sys;
306 httpd_connection_t *p_con;
310 vlc_thread_join( p_httpt );
312 /* first close all host */
313 vlc_mutex_destroy( &p_httpt->host_lock );
314 if( p_httpt->i_host_count )
316 msg_Err( p_httpd, "still have %d hosts registered !", p_httpt->i_host_count );
318 for( i = 0; i < p_httpt->i_host_count; i++ )
320 #define p_host p_httpt->host[i]
321 FREE( p_host->psz_host_addr );
322 SOCKET_CLOSE( p_host->fd );
327 FREE( p_httpt->host );
330 vlc_mutex_destroy( &p_httpt->file_lock );
331 if( p_httpt->i_file_count )
333 msg_Err( p_httpd, "still have %d files registered !", p_httpt->i_file_count );
335 for( i = 0; i < p_httpt->i_file_count; i++ )
337 #define p_file p_httpt->file[i]
338 FREE( p_file->psz_file );
339 FREE( p_file->psz_mime );
340 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
342 FREE( p_file->psz_user );
343 FREE( p_file->psz_password );
345 FREE( p_file->p_buffer );
350 FREE( p_httpt->file );
352 /* andd close all connection */
353 vlc_mutex_destroy( &p_httpt->connection_lock );
354 if( p_httpt->i_connection_count )
356 msg_Warn( p_httpd, "%d connections still in use", p_httpt->i_connection_count );
358 while( ( p_con = p_httpt->p_first_connection ) )
360 httpd_ConnnectionClose( p_httpt, p_con );
363 msg_Info( p_httpd, "httpd instance closed" );
364 vlc_object_destroy( p_httpt );
368 /****************************************************************************
369 ****************************************************************************
372 ****************************************************************************
373 ****************************************************************************/
374 static int BuildAddr( struct sockaddr_in * p_socket,
375 const char * psz_address, int i_port )
378 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
379 p_socket->sin_family = AF_INET; /* family */
380 p_socket->sin_port = htons( (uint16_t)i_port );
383 p_socket->sin_addr.s_addr = INADDR_ANY;
387 struct hostent * p_hostent;
389 /* Try to convert address directly from in_addr - this will work if
390 * psz_address is dotted decimal. */
391 #ifdef HAVE_ARPA_INET_H
392 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
394 p_socket->sin_addr.s_addr = inet_addr( psz_address );
395 if( p_socket->sin_addr.s_addr == INADDR_NONE )
398 /* We have a fqdn, try to find its address */
399 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
404 /* Copy the first address of the host in the socket address */
405 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
406 p_hostent->h_length );
414 * listen on a host for a httpd instance
417 static httpd_host_t *_RegisterHost( httpd_sys_t *p_httpt, char *psz_host_addr, int i_port )
419 httpd_host_t *p_host;
420 struct sockaddr_in sock;
424 #if !defined( WIN32 ) && !defined( UNDER_CE )
428 if( BuildAddr( &sock, psz_host_addr, i_port ) )
430 msg_Err( p_httpt, "cannot build address for %s:%d", psz_host_addr, i_port );
434 /* is it already declared ? */
435 vlc_mutex_lock( &p_httpt->host_lock );
436 for( i = 0; i < p_httpt->i_host_count; i++ )
438 if( p_httpt->host[i]->sock.sin_port == sock.sin_port &&
439 ( p_httpt->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
440 p_httpt->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
446 if( i < p_httpt->i_host_count )
448 /* yes, increment ref count and succed */
449 p_httpt->host[i]->i_ref++;
450 vlc_mutex_unlock( &p_httpt->host_lock );
451 return( p_httpt->host[i] );
454 /* need to add a new listening socket */
457 fd = socket( AF_INET, SOCK_STREAM, 0 );
460 msg_Err( p_httpt, "cannot open socket" );
465 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
466 (void *) &i_opt, sizeof( i_opt ) ) < 0 )
468 msg_Warn( p_httpt, "cannot configure socket (SO_REUSEADDR)" );
471 if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
473 msg_Err( p_httpt, "cannot bind socket" );
476 /* set to non-blocking */
477 #if defined( WIN32 ) || defined( UNDER_CE )
479 unsigned long i_dummy = 1;
480 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
482 msg_Err( p_httpt, "cannot set socket to non-blocking mode" );
487 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
489 msg_Err( p_httpt, "cannot F_GETFL socket" );
492 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
494 msg_Err( p_httpt, "cannot F_SETFL O_NONBLOCK" );
499 if( listen( fd, LISTEN_BACKLOG ) < 0 )
501 msg_Err( p_httpt, "cannot listen socket" );
507 p_httpt->host = realloc( p_httpt->host, sizeof( httpd_host_t *) * ( p_httpt->i_host_count + 1 ) );
511 p_httpt->host = malloc( sizeof( httpd_host_t *) );
513 p_host = malloc( sizeof( httpd_host_t ) );
515 p_host->psz_host_addr = strdup( psz_host_addr );
516 p_host->i_port = i_port;
520 p_httpt->host[p_httpt->i_host_count++] = p_host;
521 vlc_mutex_unlock( &p_httpt->host_lock );
526 vlc_mutex_unlock( &p_httpt->host_lock );
533 static httpd_host_t *RegisterHost( httpd_t *p_httpd, char *psz_host_addr, int i_port )
535 return( _RegisterHost( p_httpd->p_sys, psz_host_addr, i_port ) );
539 * remove a listening host for an httpd instance
541 static void _UnregisterHost( httpd_sys_t *p_httpt, httpd_host_t *p_host )
545 vlc_mutex_lock( &p_httpt->host_lock );
546 for( i = 0; i < p_httpt->i_host_count; i++ )
548 if( p_httpt->host[i] == p_host )
553 if( i >= p_httpt->i_host_count )
555 vlc_mutex_unlock( &p_httpt->host_lock );
556 msg_Err( p_httpt, "cannot unregister host" );
562 if( p_host->i_ref > 0 )
565 vlc_mutex_unlock( &p_httpt->host_lock );
570 FREE( p_host->psz_host_addr );
571 SOCKET_CLOSE( p_host->fd );
575 if( p_httpt->i_host_count <= 1 )
577 FREE( p_httpt->host );
578 p_httpt->i_host_count = 0;
584 i_move = p_httpt->i_host_count - i - 1;
588 memmove( &p_httpt->host[i],
590 i_move * sizeof( httpd_host_t * ) );
593 p_httpt->i_host_count--;
594 p_httpt->host = realloc( p_httpt->host,
595 p_httpt->i_host_count * sizeof( httpd_host_t * ) );
598 vlc_mutex_unlock( &p_httpt->host_lock );
600 static void UnregisterHost( httpd_t *p_httpd, httpd_host_t *p_host )
602 _UnregisterHost( p_httpd->p_sys, p_host );
606 static void __RegisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
609 if( p_httpt->i_file_count )
611 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * ( p_httpt->i_file_count + 1 ) );
615 p_httpt->file = malloc( sizeof( httpd_file_t *) );
618 p_httpt->file[p_httpt->i_file_count++] = p_file;
621 static httpd_file_t *_RegisterFile( httpd_sys_t *p_httpt,
622 char *psz_file, char *psz_mime,
623 char *psz_user, char *psz_password,
624 httpd_file_callback pf_get,
625 httpd_file_callback pf_post,
626 httpd_file_callback_args_t *p_args )
628 httpd_file_t *p_file;
631 vlc_mutex_lock( &p_httpt->file_lock );
632 for( i = 0; i < p_httpt->i_file_count; i++ )
634 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
639 if( i < p_httpt->i_file_count )
641 vlc_mutex_unlock( &p_httpt->file_lock );
642 msg_Err( p_httpt, "%s already registered", psz_file );
646 p_file = malloc( sizeof( httpd_file_t ) );
648 p_file->psz_file = strdup( psz_file );
649 p_file->psz_mime = strdup( psz_mime );
650 if( psz_user && *psz_user )
652 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
653 p_file->psz_user = strdup( psz_user );
654 p_file->psz_password = strdup( psz_password );
658 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
659 p_file->psz_user = NULL;
660 p_file->psz_password = NULL;
663 p_file->b_stream = VLC_FALSE;
664 p_file->p_sys = p_args;
665 p_file->pf_get = pf_get;
666 p_file->pf_post = pf_post;
668 p_file->i_buffer_size = 0;
669 p_file->i_buffer_last_pos = 0;
670 p_file->i_buffer_pos = 0;
671 p_file->p_buffer = NULL;
673 p_file->i_header_size = 0;
674 p_file->p_header = NULL;
676 __RegisterFile( p_httpt, p_file );
678 vlc_mutex_unlock( &p_httpt->file_lock );
682 static httpd_file_t *RegisterFile( httpd_t *p_httpd,
683 char *psz_file, char *psz_mime,
684 char *psz_user, char *psz_password,
685 httpd_file_callback pf_get,
686 httpd_file_callback pf_post,
687 httpd_file_callback_args_t *p_args )
689 return( _RegisterFile( p_httpd->p_sys,
690 psz_file, psz_mime, psz_user, psz_password,
691 pf_get, pf_post, p_args ) );
694 static httpd_stream_t *_RegisterStream( httpd_sys_t *p_httpt,
695 char *psz_file, char *psz_mime,
696 char *psz_user, char *psz_password )
698 httpd_stream_t *p_stream;
701 vlc_mutex_lock( &p_httpt->file_lock );
702 for( i = 0; i < p_httpt->i_file_count; i++ )
704 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
709 if( i < p_httpt->i_file_count )
711 vlc_mutex_unlock( &p_httpt->file_lock );
712 msg_Err( p_httpt, "%s already registered", psz_file );
716 p_stream = malloc( sizeof( httpd_stream_t ) );
718 p_stream->psz_file = strdup( psz_file );
719 p_stream->psz_mime = strdup( psz_mime );
720 if( psz_user && *psz_user )
722 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
723 p_stream->psz_user = strdup( psz_user );
724 p_stream->psz_password = strdup( psz_password );
728 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
729 p_stream->psz_user = NULL;
730 p_stream->psz_password = NULL;
733 p_stream->b_stream = VLC_TRUE;
734 p_stream->p_sys = NULL;
735 p_stream->pf_get = NULL;
736 p_stream->pf_post = NULL;
738 p_stream->i_buffer_size = 5*1024*1024;
739 p_stream->i_buffer_pos = 0;
740 p_stream->i_buffer_last_pos = 0;
741 p_stream->p_buffer = malloc( p_stream->i_buffer_size );
743 p_stream->i_header_size = 0;
744 p_stream->p_header = NULL;
746 __RegisterFile( p_httpt, p_stream );
748 vlc_mutex_unlock( &p_httpt->file_lock );
752 static httpd_stream_t *RegisterStream( httpd_t *p_httpd,
753 char *psz_file, char *psz_mime,
754 char *psz_user, char *psz_password )
756 return( _RegisterStream( p_httpd->p_sys,
757 psz_file, psz_mime, psz_user, psz_password ) );
760 static void _UnregisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
764 vlc_mutex_lock( &p_httpt->file_lock );
765 for( i = 0; i < p_httpt->i_file_count; i++ )
767 if( !strcmp( p_file->psz_file, p_httpt->file[i]->psz_file ) )
772 if( i >= p_httpt->i_file_count )
774 vlc_mutex_unlock( &p_httpt->file_lock );
775 msg_Err( p_httpt, "cannot unregister file" );
779 if( p_file->i_ref > 0 )
781 httpd_connection_t *p_con;
782 /* force closing all connection for this file */
783 msg_Err( p_httpt, "closing all client connection" );
785 vlc_mutex_lock( &p_httpt->connection_lock );
786 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
788 httpd_connection_t *p_next;
790 p_next = p_con->p_next;
791 if( p_con->p_file == p_file )
793 httpd_ConnnectionClose( p_httpt, p_con );
797 vlc_mutex_unlock( &p_httpt->connection_lock );
800 FREE( p_file->psz_file );
801 FREE( p_file->psz_mime );
802 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
804 FREE( p_file->psz_user );
805 FREE( p_file->psz_password );
807 FREE( p_file->p_buffer );
808 FREE( p_file->p_header );
813 if( p_httpt->i_file_count == 1 )
815 FREE( p_httpt->file );
816 p_httpt->i_file_count = 0;
822 i_move = p_httpt->i_file_count - i - 1;
825 memmove( &p_httpt->file[i], &p_httpt->file[i + 1], sizeof( httpd_file_t *) * i_move );
827 p_httpt->i_file_count--;
828 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * p_httpt->i_file_count );
831 vlc_mutex_unlock( &p_httpt->file_lock );
833 static void UnregisterFile( httpd_t *p_httpd, httpd_file_t *p_file )
835 _UnregisterFile( p_httpd->p_sys, p_file );
838 static void UnregisterStream( httpd_t *p_httpd, httpd_stream_t *p_stream )
840 _UnregisterFile( p_httpd->p_sys, p_stream );
845 static int _SendStream( httpd_sys_t *p_httpt, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
850 if( i_data <= 0 || p_data == NULL )
852 return( VLC_SUCCESS );
854 //fprintf( stderr, "## i_data=%d pos=%lld\n", i_data, p_stream->i_buffer_pos );
856 vlc_mutex_lock( &p_httpt->file_lock );
858 /* save this pointer (to be used by new connection) */
859 p_stream->i_buffer_last_pos = p_stream->i_buffer_pos;
861 i_pos = p_stream->i_buffer_pos % p_stream->i_buffer_size;
867 i_copy = __MIN( i_count, p_stream->i_buffer_size - i_pos );
869 memcpy( &p_stream->p_buffer[i_pos],
873 i_pos = ( i_pos + i_copy ) % p_stream->i_buffer_size;
878 p_stream->i_buffer_pos += i_data;
879 vlc_mutex_unlock( &p_httpt->file_lock );
881 return( VLC_SUCCESS );
883 static int SendStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
885 return( _SendStream( p_httpd->p_sys, p_stream, p_data, i_data ) );
888 static int HeaderStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
890 httpd_sys_t *p_httpt = p_httpd->p_sys;
892 vlc_mutex_lock( &p_httpt->file_lock );
894 FREE( p_stream->p_header );
895 if( p_data == NULL || i_data <= 0 )
897 p_stream->i_header_size = 0;
901 p_stream->i_header_size = i_data;
902 p_stream->p_header = malloc( i_data );
903 memcpy( p_stream->p_header,
907 vlc_mutex_unlock( &p_httpt->file_lock );
909 return( VLC_SUCCESS );
912 /****************************************************************************/
913 /****************************************************************************/
914 /****************************************************************************/
915 /****************************************************************************/
916 /****************************************************************************/
918 static int httpd_page_401_get( httpd_file_callback_args_t *p_args,
919 uint8_t *p_request, int i_request,
920 uint8_t **pp_data, int *pi_data )
924 p = *pp_data = malloc( 1024 );
926 p += sprintf( p, "<html>\n" );
927 p += sprintf( p, "<head>\n" );
928 p += sprintf( p, "<title>Error 401</title>\n" );
929 p += sprintf( p, "</head>\n" );
930 p += sprintf( p, "<body>\n" );
931 p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
932 p += sprintf( p, "<hr />\n" );
933 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
934 p += sprintf( p, "</body>\n" );
935 p += sprintf( p, "</html>\n" );
937 *pi_data = strlen( *pp_data ) + 1;
941 static int httpd_page_404_get( httpd_file_callback_args_t *p_args,
942 uint8_t *p_request, int i_request,
943 uint8_t **pp_data, int *pi_data )
947 p = *pp_data = malloc( 1024 );
949 p += sprintf( p, "<html>\n" );
950 p += sprintf( p, "<head>\n" );
951 p += sprintf( p, "<title>Error 404</title>\n" );
952 p += sprintf( p, "</head>\n" );
953 p += sprintf( p, "<body>\n" );
954 p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
955 p += sprintf( p, "<hr />\n" );
956 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
957 p += sprintf( p, "</body>\n" );
958 p += sprintf( p, "</html>\n" );
960 *pi_data = strlen( *pp_data ) + 1;
966 static int _httpd_page_admin_get_status( httpd_file_callback_args_t *p_args,
967 uint8_t **pp_data, int *pi_data )
969 httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
970 httpd_connection_t *p_con;
974 /* FIXME FIXME do not use static size FIXME FIXME*/
975 p = *pp_data = malloc( 8096 );
977 p += sprintf( p, "<html>\n" );
978 p += sprintf( p, "<head>\n" );
979 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
980 p += sprintf( p, "</head>\n" );
981 p += sprintf( p, "<body>\n" );
982 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
983 p += sprintf( p, "<h2><center>Admin page</center></h2>\n" );
986 p += sprintf( p, "<h3>General state</h3>\n" );
987 p += sprintf( p, "<ul>\n" );
988 p += sprintf( p, "<li>Connection count: %d</li>\n", p_httpt->i_connection_count );
989 //p += sprintf( p, "<li>Total bandwith: %d</li>\n", -1 );
990 /*p += sprintf( p, "<li></li>\n" );*/
991 p += sprintf( p, "</ul>\n" );
993 vlc_mutex_lock( &p_httpt->host_lock );
994 p += sprintf( p, "<h3>Host list</h3>\n" );
995 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
996 p += sprintf( p, "<tr>\n<th>Host</th><th>Port</th><th>IP</th>\n</tr>\n" );
998 for( i = 0; i < p_httpt->i_host_count; i++ )
1000 p += sprintf( p, "<tr>\n" );
1001 p += sprintf( p, "<td>%s</td>\n", p_httpt->host[i]->psz_host_addr );
1002 p += sprintf( p, "<td>%d</td>\n", p_httpt->host[i]->i_port );
1003 p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_httpt->host[i]->sock.sin_addr ) );
1004 p += sprintf( p, "</tr>\n" );
1006 p += sprintf( p, "</table>\n" );
1007 vlc_mutex_unlock( &p_httpt->host_lock );
1010 /* XXX do not take lock on file_lock */
1011 p += sprintf( p, "<h3>File list</h3>\n" );
1012 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1013 p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1015 for( i = 0; i < p_httpt->i_file_count; i++ )
1017 if( !p_httpt->file[i]->b_stream )
1019 p += sprintf( p, "<tr>\n" );
1020 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1021 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1022 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1023 p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1024 p += sprintf( p, "</tr>\n" );
1027 p += sprintf( p, "</table>\n" );
1030 /* XXX do not take lock on file_lock */
1031 p += sprintf( p, "<h3>Stream list</h3>\n" );
1032 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1033 p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1035 for( i = 0; i < p_httpt->i_file_count; i++ )
1037 if( p_httpt->file[i]->b_stream )
1039 p += sprintf( p, "<tr>\n" );
1040 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1041 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1042 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1043 p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1044 p += sprintf( p, "</tr>\n" );
1047 p += sprintf( p, "</table>\n" );
1049 /* connection list */
1050 /* XXX do not take lock on connection_lock */
1051 p += sprintf( p, "<h3>Connection list</h3>\n" );
1052 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1053 p += sprintf( p, "<tr>\n<th>IP</th><th>Requested File</th><th>Status</th><th>Action</th>\n</tr>\n" );
1055 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1057 p += sprintf( p, "<tr>\n" );
1058 p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_con->sock.sin_addr ) );
1059 p += sprintf( p, "<td>%s</td>\n", p_con->psz_file );
1060 p += sprintf( p, "<td>%d</td>\n", p_con->i_http_error );
1061 p += sprintf( p, "<td><form method=\"get\" action=\"\">"
1062 "<select name=\"action\">"
1063 "<option selected>close_connection</option>"
1064 "<option>ban_ip</option>"
1066 "<input type=\"hidden\" name=\"id\" value=\"%p\"/>"
1067 "<input type=\"submit\" value=\"Do it\" />"
1068 "</form></td>\n", p_con);
1069 p += sprintf( p, "</tr>\n" );
1071 p += sprintf( p, "</table>\n" );
1074 /* www.videolan.org */
1075 p += sprintf( p, "<hr />\n" );
1076 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1077 p += sprintf( p, "</body>\n" );
1078 p += sprintf( p, "</html>\n" );
1080 *pi_data = strlen( *pp_data ) + 1;
1082 return( VLC_SUCCESS );
1085 static int _httpd_page_admin_get_success( httpd_file_callback_args_t *p_args,
1086 uint8_t **pp_data, int *pi_data,
1091 p = *pp_data = malloc( 8096 );
1093 p += sprintf( p, "<html>\n" );
1094 p += sprintf( p, "<head>\n" );
1095 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1096 p += sprintf( p, "</head>\n" );
1097 p += sprintf( p, "<body>\n" );
1098 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1100 p += sprintf( p, "<p>Success=`%s'</p>", psz_msg );
1102 p += sprintf( p, "<hr />\n" );
1103 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1104 p += sprintf( p, "</body>\n" );
1105 p += sprintf( p, "</html>\n" );
1107 *pi_data = strlen( *pp_data ) + 1;
1109 return( VLC_SUCCESS );
1112 static int _httpd_page_admin_get_error( httpd_file_callback_args_t *p_args,
1113 uint8_t **pp_data, int *pi_data,
1118 p = *pp_data = malloc( 8096 );
1120 p += sprintf( p, "<html>\n" );
1121 p += sprintf( p, "<head>\n" );
1122 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1123 p += sprintf( p, "</head>\n" );
1124 p += sprintf( p, "<body>\n" );
1125 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1127 p += sprintf( p, "<p>Error=`%s'</p>", psz_error );
1129 p += sprintf( p, "<hr />\n" );
1130 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1131 p += sprintf( p, "</body>\n" );
1132 p += sprintf( p, "</html>\n" );
1134 *pi_data = strlen( *pp_data ) + 1;
1136 return( VLC_SUCCESS );
1139 static void _httpd_uri_extract_value( char *psz_uri, char *psz_name, char *psz_value, int i_value_max )
1143 p = strstr( psz_uri, psz_name );
1148 p += strlen( psz_name );
1149 if( *p == '=' ) p++;
1151 if( strchr( p, '&' ) )
1153 i_len = strchr( p, '&' ) - p;
1157 i_len = strlen( p );
1159 i_len = __MIN( i_value_max - 1, i_len );
1162 strncpy( psz_value, p, i_len );
1163 psz_value[i_len] = '\0';
1167 strncpy( psz_value, "", i_value_max );
1172 strncpy( psz_value, "", i_value_max );
1177 static int httpd_page_admin_get( httpd_file_callback_args_t *p_args,
1178 uint8_t *p_request, int i_request,
1179 uint8_t **pp_data, int *pi_data )
1181 httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
1182 httpd_connection_t *p_con;
1188 _httpd_uri_extract_value( p_request, "action", action, 512 );
1190 if( !strcmp( action, "close_connection" ) )
1195 _httpd_uri_extract_value( p_request, "id", id, 512 );
1196 i_id = (void*)strtol( id, NULL, 0 );
1197 msg_Dbg( p_httpt, "requested closing connection id=%s %p", id, i_id );
1198 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1200 if( (void*)p_con == i_id )
1202 /* XXX don't free p_con as it could be the one that it is sending ... */
1203 p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
1204 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "connection closed" ) );
1207 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
1209 else if( !strcmp( action, "ban_ip" ) )
1211 msg_Dbg( p_httpt, "requested banning ip" );
1212 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "ip banned" ) );
1216 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1221 return( _httpd_page_admin_get_status( p_args, pp_data, pi_data ) );
1228 static void httpd_ConnnectionNew( httpd_sys_t *p_httpt, int fd, struct sockaddr_in *p_sock )
1230 httpd_connection_t *p_con;
1232 msg_Dbg( p_httpt, "new connection from %s", inet_ntoa( p_sock->sin_addr ) );
1234 /* create a new connection and link it */
1235 p_con = malloc( sizeof( httpd_connection_t ) );
1236 p_con->i_state = HTTPD_CONNECTION_RECEIVING_REQUEST;
1238 p_con->i_last_activity_date = mdate();
1240 p_con->sock = *p_sock;
1241 p_con->psz_file = NULL;
1242 p_con->i_http_error = 0;
1243 p_con->psz_user = NULL;
1244 p_con->psz_password = NULL;
1245 p_con->p_file = NULL;
1247 p_con->i_request_size = 0;
1248 p_con->p_request = NULL;
1250 p_con->i_buffer = 0;
1251 p_con->i_buffer_size = 8096;
1252 p_con->p_buffer = malloc( p_con->i_buffer_size );
1254 p_con->i_stream_pos = 0; // updated by httpd_thread */
1255 p_con->p_next = NULL;
1257 if( p_httpt->p_first_connection )
1259 httpd_connection_t *p_last;
1261 p_last = p_httpt->p_first_connection;
1262 while( p_last->p_next )
1264 p_last = p_last->p_next;
1267 p_last->p_next = p_con;
1268 p_con->p_prev = p_last;
1272 p_con->p_prev = NULL;
1274 p_httpt->p_first_connection = p_con;
1277 p_httpt->i_connection_count++;
1280 static void httpd_ConnnectionClose( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1282 msg_Dbg( p_httpt, "close connection from %s", inet_ntoa( p_con->sock.sin_addr ) );
1284 p_httpt->i_connection_count--;
1285 /* first cut out from list */
1288 p_con->p_prev->p_next = p_con->p_next;
1292 p_httpt->p_first_connection = p_con->p_next;
1297 p_con->p_next->p_prev = p_con->p_prev;
1300 if( p_con->p_file ) p_con->p_file->i_ref--;
1301 FREE( p_con->psz_file );
1303 FREE( p_con->p_buffer );
1304 SOCKET_CLOSE( p_con->fd );
1306 FREE( p_con->psz_user );
1307 FREE( p_con->psz_password );
1309 FREE( p_con->p_request );
1313 static void httpd_RequestGetWord( char *word, int i_word_max, char **pp_buffer, char *p_end )
1315 char *p = *pp_buffer;
1318 while( p < p_end && *p && ( *p == ' ' || *p == '\t' ) )
1324 for( i = 0; i < i_word_max && p < p_end && *p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r'; i++,p++)
1329 word[__MIN( i, i_word_max -1 )] = '\0';
1334 static int httpd_RequestNextLine( char **pp_buffer, char *p_end )
1338 for( p = *pp_buffer; p < p_end; p++ )
1340 if( p + 1 < p_end && *p == '\n' )
1345 if( p + 2 < p_end && p[0] == '\r' && p[1] == '\n' )
1352 return VLC_EGENERIC;
1355 //char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1356 static void b64_decode( char *dest, char *src )
1361 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1362 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1363 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1364 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1365 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1366 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1367 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1368 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1369 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1370 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1371 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1372 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1373 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1374 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1375 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1376 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1379 for( i_level = 0; *src != '\0'; src++ )
1383 c = b64[(unsigned int)*src];
1396 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1400 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1404 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1413 static void httpd_ConnectionParseRequest( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1422 char user[512] = "";
1423 char password[512] = "";
1425 //msg_Dbg( p_httpt, "new request=\n%s", p_con->p_buffer );
1428 p = p_con->p_buffer;
1429 p_end = p + strlen( p ) + 1;
1431 httpd_RequestGetWord( command, 32, &p, p_end );
1432 httpd_RequestGetWord( url, 1024, &p, p_end );
1433 httpd_RequestGetWord( version, 32, &p, p_end );
1434 //msg_Dbg( p_httpt, "ask =%s= =%s= =%s=", command, url, version );
1436 p_con->p_request = NULL;
1437 p_con->i_request_size = 0;
1438 if( !strcmp( command, "GET" ) )
1440 p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1442 else if( !strcmp( command, "POST" ))
1444 p_con->i_method = HTTPD_CONNECTION_METHOD_POST;
1449 p_con->psz_file = strdup( "/501.html" );
1450 p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1451 p_con->i_http_error = 501;
1455 if( strcmp( version, "HTTP/1.0" ) && strcmp( version, "HTTP/1.1" ) )
1457 p_con->psz_file = strdup( "/505.html" );
1458 p_con->i_http_error = 505;
1468 if( httpd_RequestNextLine( &p, p_end ) )
1470 //msg_Dbg( p_httpt, "failled new line" );
1473 //msg_Dbg( p_httpt, "new line=%s", p );
1475 httpd_RequestGetWord( header, 1024, &p, p_end );
1476 if( !strcmp( header, "\r\n" ) || !strcmp( header, "\n" ) )
1481 if( !strcmp( header, "Authorization:" ) )
1485 httpd_RequestGetWord( method, 32, &p, p_end );
1486 if( !strcasecmp( method, "BASIC" ) )
1491 httpd_RequestGetWord( basic, 1024, &p, p_end );
1492 //msg_Dbg( p_httpt, "Authorization: basic:%s", basic );
1493 b64_decode( decoded, basic );
1495 //msg_Dbg( p_httpt, "Authorization: decoded:%s", decoded );
1496 if( strchr( decoded, ':' ) )
1498 char *p = strchr( decoded, ':' );
1501 strcpy( user, decoded );
1502 strcpy( password, p );
1508 if( strchr( url, '?' ) )
1510 char *p_request = strchr( url, '?' );
1511 *p_request++ = '\0';
1512 p_con->psz_file = strdup( url );
1513 p_con->p_request = strdup( p_request );
1514 p_con->i_request_size = strlen( p_con->p_request );
1518 p_con->psz_file = strdup( url );
1522 if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1525 if( strstr( p_con->p_buffer, "\r\n\r\n" ) )
1527 p_request = strstr( p_con->p_buffer, "\r\n\r\n" ) + 4;
1529 else if( strstr( p_con->p_buffer, "\n\n" ) )
1531 p_request = strstr( p_con->p_buffer, "\n\n" ) + 2;
1537 if( p_request && p_request < p_end )
1539 p_con->i_request_size = p_end - p_request;
1540 p_con->p_request = malloc( p_con->i_request_size + 1);
1542 memcpy( p_con->p_request,
1544 p_con->i_request_size );
1546 p_con->p_request[p_con->i_request_size] = '\0';
1549 p_con->i_http_error = 200;
1552 //msg_Dbg( p_httpt, "ask %s %s %d", command, p_con->psz_file, p_con->i_http_error );
1553 FREE( p_con->p_buffer );
1554 p_con->i_buffer = 0;
1555 p_con->i_buffer_size = 0;
1557 //vlc_mutex_lock( &p_httpt->file_lock );
1560 p_con->p_file = NULL;
1561 for( i = 0; i < p_httpt->i_file_count; i++ )
1563 if( !strcmp( p_httpt->file[i]->psz_file, p_con->psz_file ) )
1565 if( p_httpt->file[i]->b_stream ||
1566 ( p_con->i_method == HTTPD_CONNECTION_METHOD_GET && p_httpt->file[i]->pf_get ) ||
1567 ( p_con->i_method == HTTPD_CONNECTION_METHOD_POST && p_httpt->file[i]->pf_post ) )
1569 p_con->p_file = p_httpt->file[i];
1575 if( !p_con->p_file )
1577 p_con->psz_file = strdup( "/404.html" );
1578 p_con->i_http_error = 404;
1580 /* XXX be sure that "/404.html" exist else ... */
1584 if( p_con->p_file->i_authenticate_method == HTTPD_AUTHENTICATE_BASIC )
1586 if( strcmp( user, p_con->p_file->psz_user ) || strcmp( password, p_con->p_file->psz_password ) )
1588 p_con->psz_file = strdup( "/401.html" );
1589 strcpy( user, p_con->p_file->psz_user );
1590 p_con->i_http_error = 401;
1592 /* XXX do not put password on 404 else ... */
1597 p_con->p_file->i_ref++;
1598 // vlc_mutex_unlock( &p_httpt->file_lock );
1600 switch( p_con->i_http_error )
1607 psz_status = "Authorization Required";
1610 psz_status = "Unknown";
1614 p_con->i_state = HTTPD_CONNECTION_SENDING_HEADER;
1616 p_con->i_buffer_size = 4096;
1617 p_con->i_buffer = 0;
1619 /* we send stream header with this one */
1620 if( p_con->i_http_error == 200 && p_con->p_file->b_stream )
1622 p_con->i_buffer_size += p_con->p_file->i_header_size;
1625 p = p_con->p_buffer = malloc( p_con->i_buffer_size );
1627 p += sprintf( p, "HTTP/1.0 %d %s\r\n", p_con->i_http_error, psz_status );
1628 p += sprintf( p, "Content-type: %s\r\n", p_con->p_file->psz_mime );
1629 if( p_con->i_http_error == 401 )
1631 p += sprintf( p, "WWW-Authenticate: Basic realm=\"%s\"\r\n", user );
1633 p += sprintf( p, "Cache-Control: no-cache\r\n" );
1634 p += sprintf( p, "\r\n" );
1636 p_con->i_buffer_size = strlen( p_con->p_buffer );// + 1;
1638 if( p_con->i_http_error == 200 && p_con->p_file->b_stream &&
1639 p_con->p_file->i_header_size > 0 )
1641 /* add stream header */
1642 memcpy( &p_con->p_buffer[p_con->i_buffer_size],
1643 p_con->p_file->p_header,
1644 p_con->p_file->i_header_size );
1645 p_con->i_buffer_size += p_con->p_file->i_header_size;
1648 //msg_Dbg( p_httpt, "answer=\n%s", p_con->p_buffer );
1650 #define HTTPD_STREAM_PACKET 10000
1651 static void httpd_Thread( httpd_sys_t *p_httpt )
1653 httpd_file_t *p_page_admin;
1654 httpd_file_t *p_page_401;
1655 httpd_file_t *p_page_404;
1657 httpd_connection_t *p_con;
1659 msg_Info( p_httpt, "httpd started" );
1661 p_page_401 = _RegisterFile( p_httpt,
1662 "/401.html", "text/html",
1666 (httpd_file_callback_args_t*)NULL );
1667 p_page_404 = _RegisterFile( p_httpt,
1668 "/404.html", "text/html",
1672 (httpd_file_callback_args_t*)NULL );
1673 p_page_admin = _RegisterFile( p_httpt,
1674 "/admin.html", "text/html",
1676 httpd_page_admin_get,
1678 (httpd_file_callback_args_t*)p_httpt );
1680 while( !p_httpt->b_die )
1682 struct timeval timeout;
1685 int i_handle_max = 0;
1688 if( p_httpt->i_host_count <= 0 )
1690 msleep( 100 * 1000 );
1694 /* we will create a socket set with host and connection */
1695 FD_ZERO( &fds_read );
1696 FD_ZERO( &fds_write );
1698 vlc_mutex_lock( &p_httpt->host_lock );
1699 vlc_mutex_lock( &p_httpt->connection_lock );
1700 for( i = 0; i < p_httpt->i_host_count; i++ )
1702 FD_SET( p_httpt->host[i]->fd, &fds_read );
1703 i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd );
1705 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1707 /* no more than 10s of inactivity */
1708 if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ||
1709 p_con->i_state == HTTPD_CONNECTION_TO_BE_CLOSED)
1711 httpd_connection_t *p_next = p_con->p_next;
1713 msg_Dbg( p_httpt, "close unused connection" );
1714 httpd_ConnnectionClose( p_httpt, p_con );
1719 if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos )
1721 p_con = p_con->p_next;
1725 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1727 FD_SET( p_con->fd, &fds_read );
1731 FD_SET( p_con->fd, &fds_write );
1733 i_handle_max = __MAX( i_handle_max, p_con->fd );
1735 p_con = p_con->p_next;
1737 vlc_mutex_unlock( &p_httpt->host_lock );
1738 vlc_mutex_unlock( &p_httpt->connection_lock );
1740 /* we will wait 0.5s */
1742 timeout.tv_usec = 500*1000;
1744 i_ret = select( i_handle_max + 1,
1749 if( i_ret == -1 && errno != EINTR )
1751 msg_Warn( p_httpt, "cannot select sockets" );
1757 // msg_Dbg( p_httpt, "waiting..." );
1761 vlc_mutex_lock( &p_httpt->host_lock );
1762 /* accept/refuse new connection */
1763 for( i = 0; i < p_httpt->i_host_count; i++ )
1765 int i_sock_size = sizeof( struct sockaddr_in );
1766 struct sockaddr_in sock;
1769 fd = accept( p_httpt->host[i]->fd, (struct sockaddr *)&sock,
1773 #if defined( WIN32 ) || defined( UNDER_CE )
1775 unsigned long i_dummy = 1;
1776 ioctlsocket( fd, FIONBIO, &i_dummy );
1779 fcntl( fd, F_SETFL, O_NONBLOCK );
1782 if( p_httpt->i_connection_count >= HTTPD_MAX_CONNECTION )
1784 msg_Warn( p_httpt, "max connection reached" );
1788 /* create a new connection and link it */
1789 httpd_ConnnectionNew( p_httpt, fd, &sock );
1793 vlc_mutex_unlock( &p_httpt->host_lock );
1795 vlc_mutex_lock( &p_httpt->file_lock );
1796 /* now do work for all connections */
1797 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1799 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1803 i_len = recv( p_con->fd,
1804 p_con->p_buffer + p_con->i_buffer,
1805 p_con->i_buffer_size - p_con->i_buffer, 0 );
1808 #if defined( WIN32 ) || defined( UNDER_CE )
1809 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) ||
1811 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) ||
1815 httpd_connection_t *p_next = p_con->p_next;
1817 httpd_ConnnectionClose( p_httpt, p_con );
1820 else if( i_len > 0 )
1823 p_con->i_last_activity_date = mdate();
1824 p_con->i_buffer += i_len;
1826 ptr = p_con->p_buffer + p_con->i_buffer;
1828 if( ( p_con->i_buffer >= 2 && !strncmp( ptr - 2, "\n\n", 2 ) )||
1829 ( p_con->i_buffer >= 4 && !strncmp( ptr - 4, "\r\n\r\n", 4 ) ) ||
1830 p_con->i_buffer >= p_con->i_buffer_size )
1832 p_con->p_buffer[__MIN( p_con->i_buffer, p_con->i_buffer_size - 1 )] = '\0';
1833 httpd_ConnectionParseRequest( p_httpt, p_con );
1836 p_con = p_con->p_next;
1840 p_con = p_con->p_next;
1842 continue; /* just for clarity */
1844 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER || p_con->i_state == HTTPD_CONNECTION_SENDING_FILE )
1849 if( p_con->i_buffer_size - p_con->i_buffer > 0 )
1851 i_len = send( p_con->fd, p_con->p_buffer + p_con->i_buffer, p_con->i_buffer_size - p_con->i_buffer, 0 );
1857 // 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 );
1859 #if defined( WIN32 ) || defined( UNDER_CE )
1860 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) ||
1862 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) ||
1866 httpd_connection_t *p_next = p_con->p_next;
1868 httpd_ConnnectionClose( p_httpt, p_con );
1871 else if( i_len > 0 )
1873 p_con->i_last_activity_date = mdate();
1874 p_con->i_buffer += i_len;
1876 if( p_con->i_buffer >= p_con->i_buffer_size )
1878 if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER )
1880 p_con->i_buffer_size = 0;
1881 p_con->i_buffer = 0;
1882 FREE( p_con->p_buffer );
1884 if( !p_con->p_file->b_stream )
1886 p_con->i_state = HTTPD_CONNECTION_SENDING_FILE; // be sure to out from HTTPD_CONNECTION_SENDING_HEADER
1887 if( p_con->i_method == HTTPD_CONNECTION_METHOD_GET )
1889 p_con->p_file->pf_get( p_con->p_file->p_sys,
1890 p_con->p_request, p_con->i_request_size,
1891 &p_con->p_buffer, &p_con->i_buffer_size );
1893 else if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1895 p_con->p_file->pf_post( p_con->p_file->p_sys,
1896 p_con->p_request, p_con->i_request_size,
1897 &p_con->p_buffer, &p_con->i_buffer_size );
1901 p_con->p_buffer = NULL;
1902 p_con->i_buffer_size = 0;
1907 p_con->i_state = HTTPD_CONNECTION_SENDING_STREAM;
1908 p_con->i_stream_pos = p_con->p_file->i_buffer_last_pos;
1910 p_con = p_con->p_next;
1914 httpd_connection_t *p_next = p_con->p_next;
1916 httpd_ConnnectionClose( p_httpt, p_con );
1922 p_con = p_con->p_next;
1927 p_con = p_con->p_next;
1929 continue; /* just for clarity */
1931 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM )
1933 httpd_stream_t *p_stream = p_con->p_file;
1937 if( p_con->i_stream_pos < p_stream->i_buffer_pos )
1940 /* check if this p_con aren't to late */
1941 if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
1943 fprintf(stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n", p_con->i_stream_pos, p_stream->i_buffer_pos );
1944 p_con->i_stream_pos = p_stream->i_buffer_last_pos;
1947 i_pos = p_con->i_stream_pos % p_stream->i_buffer_size;
1948 /* size until end of buffer */
1949 i_write = p_stream->i_buffer_size - i_pos;
1950 /* is it more than valid data */
1951 if( i_write >= p_stream->i_buffer_pos - p_con->i_stream_pos )
1953 i_write = p_stream->i_buffer_pos - p_con->i_stream_pos;
1955 /* limit to HTTPD_STREAM_PACKET */
1956 if( i_write > HTTPD_STREAM_PACKET )
1958 i_write = HTTPD_STREAM_PACKET;
1960 i_send = send( p_con->fd, &p_stream->p_buffer[i_pos], i_write, 0 );
1962 #if defined( WIN32 ) || defined( UNDER_CE )
1963 if( ( i_send < 0 && WSAGetLastError() != WSAEWOULDBLOCK )||
1965 if( ( i_send < 0 && errno != EAGAIN && errno != EINTR )||
1969 httpd_connection_t *p_next = p_con->p_next;
1971 httpd_ConnnectionClose( p_httpt, p_con );
1975 else if( i_send > 0 )
1977 p_con->i_last_activity_date = mdate();
1978 p_con->i_stream_pos += i_send;
1981 p_con = p_con->p_next;
1982 continue; /* just for clarity */
1984 else if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
1986 msg_Warn( p_httpt, "cannot occur (Invalid p_con->i_state)" );
1987 p_con = p_con->p_next;
1989 } /* for over connection */
1991 vlc_mutex_unlock( &p_httpt->file_lock );
1993 msg_Info( p_httpt, "httpd stopped" );
1995 _UnregisterFile( p_httpt, p_page_401 );
1996 _UnregisterFile( p_httpt, p_page_404 );
1997 _UnregisterFile( p_httpt, p_page_admin );