1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: httpd.c,v 1.8 2003/03/15 00:09:31 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 == sock.sin_addr.s_addr )
445 if( i < p_httpt->i_host_count )
447 /* yes, increment ref count and succed */
448 p_httpt->host[i]->i_ref++;
449 vlc_mutex_unlock( &p_httpt->host_lock );
450 return( p_httpt->host[i] );
453 /* need to add a new listening socket */
456 fd = socket( AF_INET, SOCK_STREAM, 0 );
459 msg_Err( p_httpt, "cannot open socket" );
464 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
465 (void *) &i_opt, sizeof( i_opt ) ) < 0 )
467 msg_Warn( p_httpt, "cannot configure socket (SO_REUSEADDR)" );
470 if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
472 msg_Err( p_httpt, "cannot bind socket" );
475 /* set to non-blocking */
476 #if defined( WIN32 ) || defined( UNDER_CE )
478 unsigned long i_dummy = 1;
479 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
481 msg_Err( p_httpt, "cannot set socket to non-blocking mode" );
486 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
488 msg_Err( p_httpt, "cannot F_GETFL socket" );
491 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
493 msg_Err( p_httpt, "cannot F_SETFL O_NONBLOCK" );
498 if( listen( fd, LISTEN_BACKLOG ) < 0 )
500 msg_Err( p_httpt, "cannot listen socket" );
506 p_httpt->host = realloc( p_httpt->host, sizeof( httpd_host_t *) * ( p_httpt->i_host_count + 1 ) );
510 p_httpt->host = malloc( sizeof( httpd_host_t *) );
512 p_host = malloc( sizeof( httpd_host_t ) );
514 p_host->psz_host_addr = strdup( psz_host_addr );
515 p_host->i_port = i_port;
519 p_httpt->host[p_httpt->i_host_count++] = p_host;
520 vlc_mutex_unlock( &p_httpt->host_lock );
525 vlc_mutex_unlock( &p_httpt->host_lock );
532 static httpd_host_t *RegisterHost( httpd_t *p_httpd, char *psz_host_addr, int i_port )
534 return( _RegisterHost( p_httpd->p_sys, psz_host_addr, i_port ) );
538 * remove a listening host for an httpd instance
540 static void _UnregisterHost( httpd_sys_t *p_httpt, httpd_host_t *p_host )
544 vlc_mutex_lock( &p_httpt->host_lock );
545 for( i = 0; i < p_httpt->i_host_count; i++ )
547 if( p_httpt->host[i] == p_host )
552 if( i >= p_httpt->i_host_count )
554 vlc_mutex_unlock( &p_httpt->host_lock );
555 msg_Err( p_httpt, "cannot unregister host" );
561 if( p_host->i_ref > 0 )
564 vlc_mutex_unlock( &p_httpt->host_lock );
569 FREE( p_host->psz_host_addr );
570 SOCKET_CLOSE( p_host->fd );
574 if( p_httpt->i_host_count <= 1 )
576 FREE( p_httpt->host );
577 p_httpt->i_host_count = 0;
583 i_move = p_httpt->i_host_count - i - 1;
587 memmove( &p_httpt->host[i],
589 i_move * sizeof( httpd_host_t * ) );
592 p_httpt->i_host_count--;
593 p_httpt->host = realloc( p_httpt->host,
594 p_httpt->i_host_count * sizeof( httpd_host_t * ) );
597 vlc_mutex_unlock( &p_httpt->host_lock );
599 static void UnregisterHost( httpd_t *p_httpd, httpd_host_t *p_host )
601 _UnregisterHost( p_httpd->p_sys, p_host );
605 static void __RegisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
608 if( p_httpt->i_file_count )
610 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * ( p_httpt->i_file_count + 1 ) );
614 p_httpt->file = malloc( sizeof( httpd_file_t *) );
617 p_httpt->file[p_httpt->i_file_count++] = p_file;
620 static httpd_file_t *_RegisterFile( httpd_sys_t *p_httpt,
621 char *psz_file, char *psz_mime,
622 char *psz_user, char *psz_password,
623 httpd_file_callback pf_get,
624 httpd_file_callback pf_post,
625 httpd_file_callback_args_t *p_args )
627 httpd_file_t *p_file;
630 vlc_mutex_lock( &p_httpt->file_lock );
631 for( i = 0; i < p_httpt->i_file_count; i++ )
633 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
638 if( i < p_httpt->i_file_count )
640 vlc_mutex_unlock( &p_httpt->file_lock );
641 msg_Err( p_httpt, "%s already registered", psz_file );
645 p_file = malloc( sizeof( httpd_file_t ) );
647 p_file->psz_file = strdup( psz_file );
648 p_file->psz_mime = strdup( psz_mime );
649 if( psz_user && *psz_user )
651 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
652 p_file->psz_user = strdup( psz_user );
653 p_file->psz_password = strdup( psz_password );
657 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
658 p_file->psz_user = NULL;
659 p_file->psz_password = NULL;
662 p_file->b_stream = VLC_FALSE;
663 p_file->p_sys = p_args;
664 p_file->pf_get = pf_get;
665 p_file->pf_post = pf_post;
667 p_file->i_buffer_size = 0;
668 p_file->i_buffer_last_pos = 0;
669 p_file->i_buffer_pos = 0;
670 p_file->p_buffer = NULL;
672 p_file->i_header_size = 0;
673 p_file->p_header = NULL;
675 __RegisterFile( p_httpt, p_file );
677 vlc_mutex_unlock( &p_httpt->file_lock );
681 static httpd_file_t *RegisterFile( httpd_t *p_httpd,
682 char *psz_file, char *psz_mime,
683 char *psz_user, char *psz_password,
684 httpd_file_callback pf_get,
685 httpd_file_callback pf_post,
686 httpd_file_callback_args_t *p_args )
688 return( _RegisterFile( p_httpd->p_sys,
689 psz_file, psz_mime, psz_user, psz_password,
690 pf_get, pf_post, p_args ) );
693 static httpd_stream_t *_RegisterStream( httpd_sys_t *p_httpt,
694 char *psz_file, char *psz_mime,
695 char *psz_user, char *psz_password )
697 httpd_stream_t *p_stream;
700 vlc_mutex_lock( &p_httpt->file_lock );
701 for( i = 0; i < p_httpt->i_file_count; i++ )
703 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
708 if( i < p_httpt->i_file_count )
710 vlc_mutex_unlock( &p_httpt->file_lock );
711 msg_Err( p_httpt, "%s already registered", psz_file );
715 p_stream = malloc( sizeof( httpd_stream_t ) );
717 p_stream->psz_file = strdup( psz_file );
718 p_stream->psz_mime = strdup( psz_mime );
719 if( psz_user && *psz_user )
721 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
722 p_stream->psz_user = strdup( psz_user );
723 p_stream->psz_password = strdup( psz_password );
727 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
728 p_stream->psz_user = NULL;
729 p_stream->psz_password = NULL;
732 p_stream->b_stream = VLC_TRUE;
733 p_stream->p_sys = NULL;
734 p_stream->pf_get = NULL;
735 p_stream->pf_post = NULL;
737 p_stream->i_buffer_size = 1024*1024*10;
738 p_stream->i_buffer_pos = 0;
739 p_stream->i_buffer_last_pos = 0;
740 p_stream->p_buffer = malloc( p_stream->i_buffer_size );
742 p_stream->i_header_size = 0;
743 p_stream->p_header = NULL;
745 __RegisterFile( p_httpt, p_stream );
747 vlc_mutex_unlock( &p_httpt->file_lock );
751 static httpd_stream_t *RegisterStream( httpd_t *p_httpd,
752 char *psz_file, char *psz_mime,
753 char *psz_user, char *psz_password )
755 return( _RegisterStream( p_httpd->p_sys,
756 psz_file, psz_mime, psz_user, psz_password ) );
759 static void _UnregisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
763 vlc_mutex_lock( &p_httpt->file_lock );
764 for( i = 0; i < p_httpt->i_file_count; i++ )
766 if( !strcmp( p_file->psz_file, p_httpt->file[i]->psz_file ) )
771 if( i >= p_httpt->i_file_count )
773 vlc_mutex_unlock( &p_httpt->file_lock );
774 msg_Err( p_httpt, "cannot unregister file" );
778 if( p_file->i_ref > 0 )
780 httpd_connection_t *p_con;
781 /* force closing all connection for this file */
782 msg_Err( p_httpt, "closing all client connection" );
784 vlc_mutex_lock( &p_httpt->connection_lock );
785 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
787 httpd_connection_t *p_next;
789 p_next = p_con->p_next;
790 if( p_con->p_file == p_file )
792 httpd_ConnnectionClose( p_httpt, p_con );
796 vlc_mutex_unlock( &p_httpt->connection_lock );
799 FREE( p_file->psz_file );
800 FREE( p_file->psz_mime );
801 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
803 FREE( p_file->psz_user );
804 FREE( p_file->psz_password );
806 FREE( p_file->p_buffer );
807 FREE( p_file->p_header );
812 if( p_httpt->i_file_count == 1 )
814 FREE( p_httpt->file );
815 p_httpt->i_file_count = 0;
821 i_move = p_httpt->i_file_count - i - 1;
824 memmove( &p_httpt->file[i], &p_httpt->file[i + 1], sizeof( httpd_file_t *) * i_move );
826 p_httpt->i_file_count--;
827 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * p_httpt->i_file_count );
830 vlc_mutex_unlock( &p_httpt->file_lock );
832 static void UnregisterFile( httpd_t *p_httpd, httpd_file_t *p_file )
834 _UnregisterFile( p_httpd->p_sys, p_file );
837 static void UnregisterStream( httpd_t *p_httpd, httpd_stream_t *p_stream )
839 _UnregisterFile( p_httpd->p_sys, p_stream );
844 static int _SendStream( httpd_sys_t *p_httpt, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
849 if( i_data <= 0 || p_data == NULL )
851 return( VLC_SUCCESS );
853 //fprintf( stderr, "## i_data=%d pos=%lld\n", i_data, p_stream->i_buffer_pos );
855 vlc_mutex_lock( &p_httpt->file_lock );
857 /* save this pointer (to be used by new connection) */
858 p_stream->i_buffer_last_pos = p_stream->i_buffer_pos;
860 i_pos = p_stream->i_buffer_pos % p_stream->i_buffer_size;
866 i_copy = __MIN( i_data, p_stream->i_buffer_size - i_pos );
868 memcpy( &p_stream->p_buffer[i_pos],
872 i_pos = ( i_pos + i_copy ) % p_stream->i_buffer_size;
877 p_stream->i_buffer_pos += i_data;
878 vlc_mutex_unlock( &p_httpt->file_lock );
880 return( VLC_SUCCESS );
882 static int SendStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
884 return( _SendStream( p_httpd->p_sys, p_stream, p_data, i_data ) );
887 static int HeaderStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
889 httpd_sys_t *p_httpt = p_httpd->p_sys;
891 vlc_mutex_lock( &p_httpt->file_lock );
893 FREE( p_stream->p_header );
894 if( p_data == NULL || i_data <= 0 )
896 p_stream->i_header_size = 0;
900 p_stream->i_header_size = i_data;
901 p_stream->p_header = malloc( i_data );
902 memcpy( p_stream->p_header,
906 vlc_mutex_unlock( &p_httpt->file_lock );
908 return( VLC_SUCCESS );
911 /****************************************************************************/
912 /****************************************************************************/
913 /****************************************************************************/
914 /****************************************************************************/
915 /****************************************************************************/
917 static int httpd_page_401_get( httpd_file_callback_args_t *p_args,
918 uint8_t *p_request, int i_request,
919 uint8_t **pp_data, int *pi_data )
923 p = *pp_data = malloc( 1024 );
925 p += sprintf( p, "<html>\n" );
926 p += sprintf( p, "<head>\n" );
927 p += sprintf( p, "<title>Error 401</title>\n" );
928 p += sprintf( p, "</head>\n" );
929 p += sprintf( p, "<body>\n" );
930 p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
931 p += sprintf( p, "<hr />\n" );
932 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
933 p += sprintf( p, "</body>\n" );
934 p += sprintf( p, "</html>\n" );
936 *pi_data = strlen( *pp_data ) + 1;
940 static int httpd_page_404_get( httpd_file_callback_args_t *p_args,
941 uint8_t *p_request, int i_request,
942 uint8_t **pp_data, int *pi_data )
946 p = *pp_data = malloc( 1024 );
948 p += sprintf( p, "<html>\n" );
949 p += sprintf( p, "<head>\n" );
950 p += sprintf( p, "<title>Error 404</title>\n" );
951 p += sprintf( p, "</head>\n" );
952 p += sprintf( p, "<body>\n" );
953 p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
954 p += sprintf( p, "<hr />\n" );
955 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
956 p += sprintf( p, "</body>\n" );
957 p += sprintf( p, "</html>\n" );
959 *pi_data = strlen( *pp_data ) + 1;
965 static int _httpd_page_admin_get_status( httpd_file_callback_args_t *p_args,
966 uint8_t **pp_data, int *pi_data )
968 httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
969 httpd_connection_t *p_con;
973 /* FIXME FIXME do not use static size FIXME FIXME*/
974 p = *pp_data = malloc( 8096 );
976 p += sprintf( p, "<html>\n" );
977 p += sprintf( p, "<head>\n" );
978 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
979 p += sprintf( p, "</head>\n" );
980 p += sprintf( p, "<body>\n" );
981 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
982 p += sprintf( p, "<h2><center>Admin page</center></h2>\n" );
985 p += sprintf( p, "<h3>General state</h3>\n" );
986 p += sprintf( p, "<ul>\n" );
987 p += sprintf( p, "<li>Connection count: %d</li>\n", p_httpt->i_connection_count );
988 //p += sprintf( p, "<li>Total bandwith: %d</li>\n", -1 );
989 /*p += sprintf( p, "<li></li>\n" );*/
990 p += sprintf( p, "</ul>\n" );
992 vlc_mutex_lock( &p_httpt->host_lock );
993 p += sprintf( p, "<h3>Host list</h3>\n" );
994 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
995 p += sprintf( p, "<tr>\n<th>Host</th><th>Port</th><th>IP</th>\n</tr>\n" );
997 for( i = 0; i < p_httpt->i_host_count; i++ )
999 p += sprintf( p, "<tr>\n" );
1000 p += sprintf( p, "<td>%s</td>\n", p_httpt->host[i]->psz_host_addr );
1001 p += sprintf( p, "<td>%d</td>\n", p_httpt->host[i]->i_port );
1002 p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_httpt->host[i]->sock.sin_addr ) );
1003 p += sprintf( p, "</tr>\n" );
1005 p += sprintf( p, "</table>\n" );
1006 vlc_mutex_unlock( &p_httpt->host_lock );
1009 /* XXX do not take lock on file_lock */
1010 p += sprintf( p, "<h3>File list</h3>\n" );
1011 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1012 p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1014 for( i = 0; i < p_httpt->i_file_count; i++ )
1016 if( !p_httpt->file[i]->b_stream )
1018 p += sprintf( p, "<tr>\n" );
1019 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1020 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1021 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1022 p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1023 p += sprintf( p, "</tr>\n" );
1026 p += sprintf( p, "</table>\n" );
1029 /* XXX do not take lock on file_lock */
1030 p += sprintf( p, "<h3>Stream list</h3>\n" );
1031 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1032 p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1034 for( i = 0; i < p_httpt->i_file_count; i++ )
1036 if( p_httpt->file[i]->b_stream )
1038 p += sprintf( p, "<tr>\n" );
1039 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1040 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1041 p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1042 p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1043 p += sprintf( p, "</tr>\n" );
1046 p += sprintf( p, "</table>\n" );
1048 /* connection list */
1049 /* XXX do not take lock on connection_lock */
1050 p += sprintf( p, "<h3>Connection list</h3>\n" );
1051 p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1052 p += sprintf( p, "<tr>\n<th>IP</th><th>Requested File</th><th>Status</th><th>Action</th>\n</tr>\n" );
1054 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1056 p += sprintf( p, "<tr>\n" );
1057 p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_con->sock.sin_addr ) );
1058 p += sprintf( p, "<td>%s</td>\n", p_con->psz_file );
1059 p += sprintf( p, "<td>%d</td>\n", p_con->i_http_error );
1060 p += sprintf( p, "<td><form method=\"get\" action=\"\">"
1061 "<select name=\"action\">"
1062 "<option selected>close_connection</option>"
1063 "<option>ban_ip</option>"
1065 "<input type=\"hidden\" name=\"id\" value=\"%p\"/>"
1066 "<input type=\"submit\" value=\"Do it\" />"
1067 "</form></td>\n", p_con);
1068 p += sprintf( p, "</tr>\n" );
1070 p += sprintf( p, "</table>\n" );
1073 /* www.videolan.org */
1074 p += sprintf( p, "<hr />\n" );
1075 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1076 p += sprintf( p, "</body>\n" );
1077 p += sprintf( p, "</html>\n" );
1079 *pi_data = strlen( *pp_data ) + 1;
1081 return( VLC_SUCCESS );
1084 static int _httpd_page_admin_get_success( httpd_file_callback_args_t *p_args,
1085 uint8_t **pp_data, int *pi_data,
1090 p = *pp_data = malloc( 8096 );
1092 p += sprintf( p, "<html>\n" );
1093 p += sprintf( p, "<head>\n" );
1094 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1095 p += sprintf( p, "</head>\n" );
1096 p += sprintf( p, "<body>\n" );
1097 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1099 p += sprintf( p, "<p>Success=`%s'</p>", psz_msg );
1101 p += sprintf( p, "<hr />\n" );
1102 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1103 p += sprintf( p, "</body>\n" );
1104 p += sprintf( p, "</html>\n" );
1106 *pi_data = strlen( *pp_data ) + 1;
1108 return( VLC_SUCCESS );
1111 static int _httpd_page_admin_get_error( httpd_file_callback_args_t *p_args,
1112 uint8_t **pp_data, int *pi_data,
1117 p = *pp_data = malloc( 8096 );
1119 p += sprintf( p, "<html>\n" );
1120 p += sprintf( p, "<head>\n" );
1121 p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1122 p += sprintf( p, "</head>\n" );
1123 p += sprintf( p, "<body>\n" );
1124 p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1126 p += sprintf( p, "<p>Error=`%s'</p>", psz_error );
1128 p += sprintf( p, "<hr />\n" );
1129 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1130 p += sprintf( p, "</body>\n" );
1131 p += sprintf( p, "</html>\n" );
1133 *pi_data = strlen( *pp_data ) + 1;
1135 return( VLC_SUCCESS );
1138 static void _httpd_uri_extract_value( char *psz_uri, char *psz_name, char *psz_value, int i_value_max )
1142 p = strstr( psz_uri, psz_name );
1147 p += strlen( psz_name );
1148 if( *p == '=' ) p++;
1150 if( strchr( p, '&' ) )
1152 i_len = strchr( p, '&' ) - p;
1156 i_len = strlen( p );
1158 i_len = __MIN( i_value_max - 1, i_len );
1161 strncpy( psz_value, p, i_len );
1162 psz_value[i_len] = '\0';
1166 strncpy( psz_value, "", i_value_max );
1171 strncpy( psz_value, "", i_value_max );
1176 static int httpd_page_admin_get( httpd_file_callback_args_t *p_args,
1177 uint8_t *p_request, int i_request,
1178 uint8_t **pp_data, int *pi_data )
1180 httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
1181 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 /* we send stream header with this one */
1617 if( p_con->i_http_error == 200 && p_con->p_file->b_stream )
1619 p_con->i_buffer_size = 4096 + p_con->p_file->i_header_size;
1622 p_con->i_buffer_size = 4096;
1623 p_con->i_buffer = 0;
1624 p = p_con->p_buffer = malloc( p_con->i_buffer_size );
1626 p += sprintf( p, "HTTP/1.0 %d %s\r\n", p_con->i_http_error, psz_status );
1627 p += sprintf( p, "Content-type: %s\r\n", p_con->p_file->psz_mime );
1628 if( p_con->i_http_error == 401 )
1630 p += sprintf( p, "WWW-Authenticate: Basic realm=\"%s\"\r\n", user );
1632 p += sprintf( p, "\r\n" );
1634 p_con->i_buffer_size = strlen( p_con->p_buffer );// + 1;
1636 if( p_con->i_http_error == 200 && p_con->p_file->b_stream && p_con->p_file->i_header_size > 0 )
1638 /* add stream header */
1639 memcpy( &p_con->p_buffer[p_con->i_buffer_size],
1640 p_con->p_file->p_header,
1641 p_con->p_file->i_header_size );
1642 p_con->i_buffer_size += p_con->p_file->i_header_size;
1645 //msg_Dbg( p_httpt, "answer=\n%s", p_con->p_buffer );
1647 #define HTTPD_STREAM_PACKET 10000
1648 static void httpd_Thread( httpd_sys_t *p_httpt )
1650 httpd_file_t *p_page_admin;
1651 httpd_file_t *p_page_401;
1652 httpd_file_t *p_page_404;
1654 httpd_connection_t *p_con;
1656 msg_Info( p_httpt, "httpd started" );
1658 p_page_401 = _RegisterFile( p_httpt,
1659 "/401.html", "text/html",
1663 (httpd_file_callback_args_t*)NULL );
1664 p_page_404 = _RegisterFile( p_httpt,
1665 "/404.html", "text/html",
1669 (httpd_file_callback_args_t*)NULL );
1670 p_page_admin = _RegisterFile( p_httpt,
1671 "/admin.html", "text/html",
1673 httpd_page_admin_get,
1675 (httpd_file_callback_args_t*)p_httpt );
1677 while( !p_httpt->b_die )
1679 struct timeval timeout;
1682 int i_handle_max = 0;
1685 if( p_httpt->i_host_count <= 0 )
1687 msleep( 100 * 1000 );
1691 /* we will create a socket set with host and connection */
1692 FD_ZERO( &fds_read );
1693 FD_ZERO( &fds_write );
1695 vlc_mutex_lock( &p_httpt->host_lock );
1696 vlc_mutex_lock( &p_httpt->connection_lock );
1697 for( i = 0; i < p_httpt->i_host_count; i++ )
1699 FD_SET( p_httpt->host[i]->fd, &fds_read );
1700 i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd );
1702 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1704 /* no more than 10s of inactivity */
1705 if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ||
1706 p_con->i_state == HTTPD_CONNECTION_TO_BE_CLOSED)
1708 httpd_connection_t *p_next = p_con->p_next;
1710 msg_Dbg( p_httpt, "close unused connection" );
1711 httpd_ConnnectionClose( p_httpt, p_con );
1716 if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos )
1718 p_con = p_con->p_next;
1722 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1724 FD_SET( p_con->fd, &fds_read );
1728 FD_SET( p_con->fd, &fds_write );
1730 i_handle_max = __MAX( i_handle_max, p_con->fd );
1732 p_con = p_con->p_next;
1734 vlc_mutex_unlock( &p_httpt->host_lock );
1735 vlc_mutex_unlock( &p_httpt->connection_lock );
1737 /* we will wait 0.5s */
1739 timeout.tv_usec = 500*1000;
1741 i_ret = select( i_handle_max + 1,
1746 if( i_ret == -1 && errno != EINTR )
1748 msg_Warn( p_httpt, "cannot select sockets" );
1754 // msg_Dbg( p_httpt, "waiting..." );
1758 vlc_mutex_lock( &p_httpt->host_lock );
1759 /* accept/refuse new connection */
1760 for( i = 0; i < p_httpt->i_host_count; i++ )
1762 int i_sock_size = sizeof( struct sockaddr_in );
1763 struct sockaddr_in sock;
1766 fd = accept( p_httpt->host[i]->fd, (struct sockaddr *)&sock,
1770 #if defined( WIN32 ) || defined( UNDER_CE )
1772 unsigned long i_dummy = 1;
1773 ioctlsocket( fd, FIONBIO, &i_dummy );
1776 fcntl( fd, F_SETFL, O_NONBLOCK );
1779 if( p_httpt->i_connection_count >= HTTPD_MAX_CONNECTION )
1781 msg_Warn( p_httpt, "max connection reached" );
1785 /* create a new connection and link it */
1786 httpd_ConnnectionNew( p_httpt, fd, &sock );
1790 vlc_mutex_unlock( &p_httpt->host_lock );
1792 vlc_mutex_lock( &p_httpt->file_lock );
1793 /* now do work for all connections */
1794 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1796 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1800 i_len = recv( p_con->fd,
1801 p_con->p_buffer + p_con->i_buffer,
1802 p_con->i_buffer_size - p_con->i_buffer, 0 );
1804 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR )||
1807 httpd_connection_t *p_next = p_con->p_next;
1809 httpd_ConnnectionClose( p_httpt, p_con );
1812 else if( i_len > 0 )
1815 p_con->i_last_activity_date = mdate();
1816 p_con->i_buffer += i_len;
1818 ptr = p_con->p_buffer + p_con->i_buffer;
1820 if( ( p_con->i_buffer >= 2 && !strncmp( ptr - 2, "\n\n", 2 ) )||
1821 ( p_con->i_buffer >= 4 && !strncmp( ptr - 4, "\r\n\r\n", 4 ) ) ||
1822 p_con->i_buffer >= p_con->i_buffer_size )
1824 p_con->p_buffer[__MIN( p_con->i_buffer, p_con->i_buffer_size - 1 )] = '\0';
1825 httpd_ConnectionParseRequest( p_httpt, p_con );
1828 p_con = p_con->p_next;
1832 p_con = p_con->p_next;
1834 continue; /* just for clarity */
1836 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER || p_con->i_state == HTTPD_CONNECTION_SENDING_FILE )
1841 if( p_con->i_buffer_size - p_con->i_buffer > 0 )
1843 i_len = send( p_con->fd, p_con->p_buffer + p_con->i_buffer, p_con->i_buffer_size - p_con->i_buffer, 0 );
1849 // 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 );
1851 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR )||
1854 httpd_connection_t *p_next = p_con->p_next;
1856 httpd_ConnnectionClose( p_httpt, p_con );
1859 else if( i_len > 0 )
1861 p_con->i_last_activity_date = mdate();
1862 p_con->i_buffer += i_len;
1864 if( p_con->i_buffer >= p_con->i_buffer_size )
1866 if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER )
1868 p_con->i_buffer_size = 0;
1869 p_con->i_buffer = 0;
1870 FREE( p_con->p_buffer );
1872 if( !p_con->p_file->b_stream )
1874 p_con->i_state = HTTPD_CONNECTION_SENDING_FILE; // be sure to out from HTTPD_CONNECTION_SENDING_HEADER
1875 if( p_con->i_method == HTTPD_CONNECTION_METHOD_GET )
1877 p_con->p_file->pf_get( p_con->p_file->p_sys,
1878 p_con->p_request, p_con->i_request_size,
1879 &p_con->p_buffer, &p_con->i_buffer_size );
1881 else if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1883 p_con->p_file->pf_post( p_con->p_file->p_sys,
1884 p_con->p_request, p_con->i_request_size,
1885 &p_con->p_buffer, &p_con->i_buffer_size );
1889 p_con->p_buffer = NULL;
1890 p_con->i_buffer_size = 0;
1895 p_con->i_state = HTTPD_CONNECTION_SENDING_STREAM;
1896 p_con->i_stream_pos = p_con->p_file->i_buffer_last_pos;
1898 p_con = p_con->p_next;
1902 httpd_connection_t *p_next = p_con->p_next;
1904 httpd_ConnnectionClose( p_httpt, p_con );
1910 p_con = p_con->p_next;
1915 p_con = p_con->p_next;
1917 continue; /* just for clarity */
1919 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM )
1921 httpd_stream_t *p_stream = p_con->p_file;
1925 if( p_con->i_stream_pos < p_stream->i_buffer_pos )
1928 /* check if this p_con aren't to late */
1929 if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
1931 fprintf(stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n", p_con->i_stream_pos, p_stream->i_buffer_pos );
1932 p_con->i_stream_pos = p_stream->i_buffer_last_pos;
1935 i_pos = p_con->i_stream_pos % p_stream->i_buffer_size;
1936 /* size until end of buffer */
1937 i_write = p_stream->i_buffer_size - i_pos;
1938 /* is it more than valid data */
1939 if( i_write >= p_stream->i_buffer_pos - p_con->i_stream_pos )
1941 i_write = p_stream->i_buffer_pos - p_con->i_stream_pos;
1943 /* limit to HTTPD_STREAM_PACKET */
1944 if( i_write > HTTPD_STREAM_PACKET )
1946 i_write = HTTPD_STREAM_PACKET;
1948 i_send = send( p_con->fd, &p_stream->p_buffer[i_pos], i_write, 0 );
1950 if( ( i_send < 0 && errno != EAGAIN && errno != EINTR )|| ( i_send == 0 ) )
1952 httpd_connection_t *p_next = p_con->p_next;
1954 httpd_ConnnectionClose( p_httpt, p_con );
1958 else if( i_send > 0 )
1960 p_con->i_last_activity_date = mdate();
1961 p_con->i_stream_pos += i_send;
1964 p_con = p_con->p_next;
1965 continue; /* just for clarity */
1967 else if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
1969 msg_Warn( p_httpt, "cannot occur (Invalid p_con->i_state)" );
1970 p_con = p_con->p_next;
1972 } /* for over connection */
1974 vlc_mutex_unlock( &p_httpt->file_lock );
1976 msg_Info( p_httpt, "httpd stopped" );
1978 _UnregisterFile( p_httpt, p_page_401 );
1979 _UnregisterFile( p_httpt, p_page_404 );
1980 _UnregisterFile( p_httpt, p_page_admin );