1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: httpd.c,v 1.24 2003/07/10 22:37:02 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 *****************************************************************************/
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
45 #if defined( UNDER_CE )
47 #elif defined( WIN32 )
48 # include <winsock2.h>
49 # include <ws2tcpip.h>
51 # define IN_MULTICAST(a) IN_CLASSD(a)
54 # include <netdb.h> /* hostent ... */
55 # include <sys/socket.h>
56 # include <netinet/in.h>
57 # ifdef HAVE_ARPA_INET_H
58 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
65 # define INADDR_ANY 0x00000000
68 # define INADDR_NONE 0xFFFFFFFF
71 #define LISTEN_BACKLOG 100
72 #define HTTPD_MAX_CONNECTION 512
73 #define HTTPD_CONNECTION_MAX_UNUSED 10000000
76 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
78 #if defined( WIN32 ) || defined( UNDER_CE )
79 #define SOCKET_CLOSE(a) closesocket(a)
81 #define SOCKET_CLOSE(a) close(a)
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Open ( vlc_object_t * );
88 static void Close ( vlc_object_t * );
90 /*****************************************************************************
92 *****************************************************************************/
94 set_description( _("HTTP 1.0 daemon") );
95 set_capability( "httpd", 42 );
96 set_callbacks( Open, Close );
97 var_Create( p_module->p_libvlc, "httpd", VLC_VAR_MUTEX );
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* );
121 static int Control ( httpd_t *, int , void*, void* );
122 /*****************************************************************************
123 * Internal definitions
124 *****************************************************************************/
132 struct sockaddr_in sock;
137 #define HTTPD_AUTHENTICATE_NONE 0
138 #define HTTPD_AUTHENTICATE_BASIC 1
140 //typedef httpd_file_t httpd_stream_t;
150 int i_authenticate_method;
151 char *psz_user; /* NULL if no auth */
152 char *psz_password; /* NULL if no auth */
154 vlc_bool_t b_stream; /* if false: httpd will retreive data by a callback
155 true: it's up to the program to give data to httpd */
156 void *p_sys; /* provided for user */
157 httpd_file_callback pf_get; /* it should allocate and fill *pp_data and *pi_data */
158 httpd_file_callback pf_post; /* it should allocate and fill *pp_data and *pi_data */
162 /* circular buffer for stream only */
163 int i_buffer_size; /* buffer size, can't be reallocated smaller */
164 uint8_t *p_buffer; /* buffer */
165 int64_t i_buffer_pos; /* absolute position from begining */
166 int i_buffer_last_pos; /* a new connection will start with that */
168 /* data to be send at connection time (if any) */
174 #define HTTPD_CONNECTION_RECEIVING_REQUEST 1
175 #define HTTPD_CONNECTION_SENDING_HEADER 2
176 #define HTTPD_CONNECTION_SENDING_FILE 3
177 #define HTTPD_CONNECTION_SENDING_STREAM 4
178 #define HTTPD_CONNECTION_TO_BE_CLOSED 5
180 #define HTTPD_CONNECTION_METHOD_GET 1
181 #define HTTPD_CONNECTION_METHOD_POST 2
182 typedef struct httpd_connection_s
184 struct httpd_connection_s *p_next;
185 struct httpd_connection_s *p_prev;
187 struct sockaddr_in sock;
189 mtime_t i_last_activity_date;
192 int i_method; /* get/post */
194 char *psz_file; // file to be send
195 int i_http_error; // error to be send with the file
196 char *psz_user; // if Authorization in the request header
199 uint8_t *p_request; // whith get: ?<*>, with post: main data
202 httpd_file_t *p_file;
204 /* used while sending header and file */
207 int i_buffer; /* private */
209 /* used for stream */
210 int64_t i_stream_pos; /* absolute pos in stream */
211 } httpd_connection_t;
213 /* Linked List of banned IP */
214 typedef struct httpd_banned_ip_s
216 struct httpd_banned_ip_s *p_next;
217 struct httpd_banned_ip_s *p_prev;
229 vlc_mutex_t host_lock;
230 volatile int i_host_count;
233 vlc_mutex_t file_lock;
237 vlc_mutex_t connection_lock;
238 int i_connection_count;
239 httpd_connection_t *p_first_connection;
241 vlc_mutex_t ban_lock;
242 int i_banned_ip_count;
243 httpd_banned_ip_t *p_first_banned_ip;
246 static void httpd_Thread( httpd_sys_t *p_httpt );
247 static void httpd_ConnnectionNew( httpd_sys_t *, int , struct sockaddr_in * );
248 static void httpd_ConnnectionClose( httpd_sys_t *, httpd_connection_t * );
249 static int httpd_UnbanIP( httpd_sys_t *, httpd_banned_ip_t *);
250 static int httpd_BanIP( httpd_sys_t *, char *);
251 static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *, char * );
253 /*****************************************************************************
255 *****************************************************************************/
257 static int Open( vlc_object_t *p_this )
259 httpd_t *p_httpd = (httpd_t*)p_this;
260 httpd_sys_t *p_httpt;
262 /* Launch httpt thread */
263 if( !( p_httpt = vlc_object_create( p_this, sizeof( httpd_sys_t ) ) ) )
265 msg_Err( p_this, "out of memory" );
266 return( VLC_EGENERIC );
272 /* init httpt_t structure */
273 vlc_mutex_init( p_httpd, &p_httpt->host_lock );
274 p_httpt->i_host_count = 0;
275 p_httpt->host = NULL;
277 vlc_mutex_init( p_httpd, &p_httpt->file_lock );
278 p_httpt->i_file_count = 0;
279 p_httpt->file = NULL;
281 vlc_mutex_init( p_httpd, &p_httpt->connection_lock );
282 p_httpt->i_connection_count = 0;
283 p_httpt->p_first_connection = NULL;
285 vlc_mutex_init( p_httpd, &p_httpt->ban_lock );
286 p_httpt->i_banned_ip_count = 0;
287 p_httpt->p_first_banned_ip = NULL;
289 /* start the thread */
290 if( vlc_thread_create( p_httpt, "httpd thread",
291 httpd_Thread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
293 msg_Err( p_this, "cannot spawn http thread" );
295 vlc_mutex_destroy( &p_httpt->host_lock );
296 vlc_mutex_destroy( &p_httpt->file_lock );
297 vlc_mutex_destroy( &p_httpt->connection_lock );
298 vlc_mutex_destroy( &p_httpt->ban_lock );
300 vlc_object_destroy( p_httpt );
301 return( VLC_EGENERIC );
304 msg_Info( p_httpd, "http thread launched" );
306 p_httpd->p_sys = p_httpt;
307 p_httpd->pf_register_host = RegisterHost;
308 p_httpd->pf_unregister_host = UnregisterHost;
309 p_httpd->pf_register_file = RegisterFile;
310 p_httpd->pf_unregister_file = UnregisterFile;
311 p_httpd->pf_register_stream = RegisterStream;
312 p_httpd->pf_header_stream = HeaderStream;
313 p_httpd->pf_send_stream = SendStream;
314 p_httpd->pf_unregister_stream=UnregisterStream;
315 p_httpd->pf_control = Control;
317 return( VLC_SUCCESS );
320 /*****************************************************************************
321 * Close: close the target
322 *****************************************************************************/
323 static void Close( vlc_object_t * p_this )
325 httpd_t *p_httpd = (httpd_t*)p_this;
326 httpd_sys_t *p_httpt = p_httpd->p_sys;
328 httpd_connection_t *p_con;
329 httpd_banned_ip_t *p_banned_ip;
334 vlc_thread_join( p_httpt );
336 /* first close all host */
337 vlc_mutex_destroy( &p_httpt->host_lock );
338 if( p_httpt->i_host_count )
340 msg_Err( p_httpd, "still have %d hosts registered !", p_httpt->i_host_count );
342 for( i = 0; i < p_httpt->i_host_count; i++ )
344 #define p_host p_httpt->host[i]
345 FREE( p_host->psz_host_addr );
346 SOCKET_CLOSE( p_host->fd );
351 FREE( p_httpt->host );
354 vlc_mutex_destroy( &p_httpt->file_lock );
355 if( p_httpt->i_file_count )
357 msg_Err( p_httpd, "still have %d files registered !", p_httpt->i_file_count );
359 for( i = 0; i < p_httpt->i_file_count; i++ )
361 #define p_file p_httpt->file[i]
362 FREE( p_file->psz_file );
363 FREE( p_file->psz_mime );
364 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
366 FREE( p_file->psz_user );
367 FREE( p_file->psz_password );
369 FREE( p_file->p_buffer );
374 FREE( p_httpt->file );
376 /* andd close all connection */
377 vlc_mutex_destroy( &p_httpt->connection_lock );
378 if( p_httpt->i_connection_count )
380 msg_Warn( p_httpd, "%d connections still in use", p_httpt->i_connection_count );
382 while( ( p_con = p_httpt->p_first_connection ) )
384 httpd_ConnnectionClose( p_httpt, p_con );
387 /* Free all banned IP */
388 vlc_mutex_destroy( &p_httpt->ban_lock );
389 while( ( p_banned_ip = p_httpt->p_first_banned_ip))
391 httpd_UnbanIP(p_httpt,p_banned_ip);
394 msg_Info( p_httpd, "httpd instance closed" );
395 vlc_object_destroy( p_httpt );
399 /****************************************************************************
400 ****************************************************************************
403 ****************************************************************************
404 ****************************************************************************/
405 static int BuildAddr( struct sockaddr_in * p_socket,
406 const char * psz_address, int i_port )
409 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
410 p_socket->sin_family = AF_INET; /* family */
411 p_socket->sin_port = htons( (uint16_t)i_port );
414 p_socket->sin_addr.s_addr = INADDR_ANY;
418 struct hostent * p_hostent;
420 /* Try to convert address directly from in_addr - this will work if
421 * psz_address is dotted decimal. */
422 #ifdef HAVE_ARPA_INET_H
423 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
425 p_socket->sin_addr.s_addr = inet_addr( psz_address );
426 if( p_socket->sin_addr.s_addr == INADDR_NONE )
429 /* We have a fqdn, try to find its address */
430 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
435 /* Copy the first address of the host in the socket address */
436 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
437 p_hostent->h_length );
445 * listen on a host for a httpd instance
448 static httpd_host_t *_RegisterHost( httpd_sys_t *p_httpt, char *psz_host_addr, int i_port )
450 httpd_host_t *p_host;
451 struct sockaddr_in sock;
455 #if !defined( WIN32 ) && !defined( UNDER_CE )
459 if( BuildAddr( &sock, psz_host_addr, i_port ) )
461 msg_Err( p_httpt, "cannot build address for %s:%d", psz_host_addr, i_port );
465 /* is it already declared ? */
466 vlc_mutex_lock( &p_httpt->host_lock );
467 for( i = 0; i < p_httpt->i_host_count; i++ )
469 if( p_httpt->host[i]->sock.sin_port == sock.sin_port &&
470 ( p_httpt->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
471 p_httpt->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
477 if( i < p_httpt->i_host_count )
479 /* yes, increment ref count and succed */
480 p_httpt->host[i]->i_ref++;
481 vlc_mutex_unlock( &p_httpt->host_lock );
482 return( p_httpt->host[i] );
485 /* need to add a new listening socket */
488 fd = socket( AF_INET, SOCK_STREAM, 0 );
491 msg_Err( p_httpt, "cannot open socket" );
496 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
497 (void *) &i_opt, sizeof( i_opt ) ) < 0 )
499 msg_Warn( p_httpt, "cannot configure socket (SO_REUSEADDR)" );
502 if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
504 msg_Err( p_httpt, "cannot bind socket" );
507 /* set to non-blocking */
508 #if defined( WIN32 ) || defined( UNDER_CE )
510 unsigned long i_dummy = 1;
511 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
513 msg_Err( p_httpt, "cannot set socket to non-blocking mode" );
518 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
520 msg_Err( p_httpt, "cannot F_GETFL socket" );
523 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
525 msg_Err( p_httpt, "cannot F_SETFL O_NONBLOCK" );
530 if( listen( fd, LISTEN_BACKLOG ) < 0 )
532 msg_Err( p_httpt, "cannot listen socket" );
538 p_httpt->host = realloc( p_httpt->host, sizeof( httpd_host_t *) * ( p_httpt->i_host_count + 1 ) );
542 p_httpt->host = malloc( sizeof( httpd_host_t *) );
544 p_host = malloc( sizeof( httpd_host_t ) );
546 p_host->psz_host_addr = strdup( psz_host_addr );
547 p_host->i_port = i_port;
551 p_httpt->host[p_httpt->i_host_count++] = p_host;
552 vlc_mutex_unlock( &p_httpt->host_lock );
557 vlc_mutex_unlock( &p_httpt->host_lock );
564 static httpd_host_t *RegisterHost( httpd_t *p_httpd, char *psz_host_addr, int i_port )
566 return( _RegisterHost( p_httpd->p_sys, psz_host_addr, i_port ) );
570 * remove a listening host for an httpd instance
572 static void _UnregisterHost( httpd_sys_t *p_httpt, httpd_host_t *p_host )
576 vlc_mutex_lock( &p_httpt->host_lock );
577 for( i = 0; i < p_httpt->i_host_count; i++ )
579 if( p_httpt->host[i] == p_host )
584 if( i >= p_httpt->i_host_count )
586 vlc_mutex_unlock( &p_httpt->host_lock );
587 msg_Err( p_httpt, "cannot unregister host" );
593 if( p_host->i_ref > 0 )
596 vlc_mutex_unlock( &p_httpt->host_lock );
601 FREE( p_host->psz_host_addr );
602 SOCKET_CLOSE( p_host->fd );
606 if( p_httpt->i_host_count <= 1 )
608 FREE( p_httpt->host );
609 p_httpt->i_host_count = 0;
615 i_move = p_httpt->i_host_count - i - 1;
619 memmove( &p_httpt->host[i],
621 i_move * sizeof( httpd_host_t * ) );
624 p_httpt->i_host_count--;
625 p_httpt->host = realloc( p_httpt->host,
626 p_httpt->i_host_count * sizeof( httpd_host_t * ) );
629 vlc_mutex_unlock( &p_httpt->host_lock );
631 static void UnregisterHost( httpd_t *p_httpd, httpd_host_t *p_host )
633 _UnregisterHost( p_httpd->p_sys, p_host );
637 static void __RegisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
640 if( p_httpt->i_file_count )
642 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * ( p_httpt->i_file_count + 1 ) );
646 p_httpt->file = malloc( sizeof( httpd_file_t *) );
649 p_httpt->file[p_httpt->i_file_count++] = p_file;
652 static httpd_file_t *_RegisterFile( httpd_sys_t *p_httpt,
653 char *psz_file, char *psz_mime,
654 char *psz_user, char *psz_password,
655 httpd_file_callback pf_get,
656 httpd_file_callback pf_post,
657 httpd_file_callback_args_t *p_args )
659 httpd_file_t *p_file;
662 vlc_mutex_lock( &p_httpt->file_lock );
663 for( i = 0; i < p_httpt->i_file_count; i++ )
665 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
670 if( i < p_httpt->i_file_count )
672 vlc_mutex_unlock( &p_httpt->file_lock );
673 msg_Err( p_httpt, "%s already registered", psz_file );
677 p_file = malloc( sizeof( httpd_file_t ) );
679 p_file->psz_file = strdup( psz_file );
680 p_file->psz_mime = strdup( psz_mime );
681 if( psz_user && *psz_user )
683 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
684 p_file->psz_user = strdup( psz_user );
685 p_file->psz_password = strdup( psz_password );
689 p_file->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
690 p_file->psz_user = NULL;
691 p_file->psz_password = NULL;
694 p_file->b_stream = VLC_FALSE;
695 p_file->p_sys = p_args;
696 p_file->pf_get = pf_get;
697 p_file->pf_post = pf_post;
699 p_file->i_buffer_size = 0;
700 p_file->i_buffer_last_pos = 0;
701 p_file->i_buffer_pos = 0;
702 p_file->p_buffer = NULL;
704 p_file->i_header_size = 0;
705 p_file->p_header = NULL;
707 __RegisterFile( p_httpt, p_file );
709 vlc_mutex_unlock( &p_httpt->file_lock );
713 static httpd_file_t *RegisterFile( httpd_t *p_httpd,
714 char *psz_file, char *psz_mime,
715 char *psz_user, char *psz_password,
716 httpd_file_callback pf_get,
717 httpd_file_callback pf_post,
718 httpd_file_callback_args_t *p_args )
720 return( _RegisterFile( p_httpd->p_sys,
721 psz_file, psz_mime, psz_user, psz_password,
722 pf_get, pf_post, p_args ) );
725 static httpd_stream_t *_RegisterStream( httpd_sys_t *p_httpt,
726 char *psz_file, char *psz_mime,
727 char *psz_user, char *psz_password )
729 httpd_stream_t *p_stream;
732 vlc_mutex_lock( &p_httpt->file_lock );
733 for( i = 0; i < p_httpt->i_file_count; i++ )
735 if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
740 if( i < p_httpt->i_file_count )
742 vlc_mutex_unlock( &p_httpt->file_lock );
743 msg_Err( p_httpt, "%s already registered", psz_file );
747 p_stream = malloc( sizeof( httpd_stream_t ) );
749 p_stream->psz_file = strdup( psz_file );
750 p_stream->psz_mime = strdup( psz_mime );
751 if( psz_user && *psz_user )
753 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
754 p_stream->psz_user = strdup( psz_user );
755 p_stream->psz_password = strdup( psz_password );
759 p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
760 p_stream->psz_user = NULL;
761 p_stream->psz_password = NULL;
764 p_stream->b_stream = VLC_TRUE;
765 p_stream->p_sys = NULL;
766 p_stream->pf_get = NULL;
767 p_stream->pf_post = NULL;
769 p_stream->i_buffer_size = 5*1024*1024;
770 p_stream->i_buffer_pos = 0;
771 p_stream->i_buffer_last_pos = 0;
772 p_stream->p_buffer = malloc( p_stream->i_buffer_size );
774 p_stream->i_header_size = 0;
775 p_stream->p_header = NULL;
777 __RegisterFile( p_httpt, p_stream );
779 vlc_mutex_unlock( &p_httpt->file_lock );
783 static httpd_stream_t *RegisterStream( httpd_t *p_httpd,
784 char *psz_file, char *psz_mime,
785 char *psz_user, char *psz_password )
787 return( _RegisterStream( p_httpd->p_sys,
788 psz_file, psz_mime, psz_user, psz_password ) );
791 static void _UnregisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
795 vlc_mutex_lock( &p_httpt->file_lock );
796 for( i = 0; i < p_httpt->i_file_count; i++ )
798 if( !strcmp( p_file->psz_file, p_httpt->file[i]->psz_file ) )
803 if( i >= p_httpt->i_file_count )
805 vlc_mutex_unlock( &p_httpt->file_lock );
806 msg_Err( p_httpt, "cannot unregister file" );
810 if( p_file->i_ref > 0 )
812 httpd_connection_t *p_con;
813 /* force closing all connection for this file */
814 msg_Err( p_httpt, "closing all client connection" );
816 vlc_mutex_lock( &p_httpt->connection_lock );
817 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
819 httpd_connection_t *p_next;
821 p_next = p_con->p_next;
822 if( p_con->p_file == p_file )
824 httpd_ConnnectionClose( p_httpt, p_con );
828 vlc_mutex_unlock( &p_httpt->connection_lock );
831 FREE( p_file->psz_file );
832 FREE( p_file->psz_mime );
833 if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
835 FREE( p_file->psz_user );
836 FREE( p_file->psz_password );
838 FREE( p_file->p_buffer );
839 FREE( p_file->p_header );
844 if( p_httpt->i_file_count == 1 )
846 FREE( p_httpt->file );
847 p_httpt->i_file_count = 0;
853 i_move = p_httpt->i_file_count - i - 1;
856 memmove( &p_httpt->file[i], &p_httpt->file[i + 1], sizeof( httpd_file_t *) * i_move );
858 p_httpt->i_file_count--;
859 p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * p_httpt->i_file_count );
862 vlc_mutex_unlock( &p_httpt->file_lock );
864 static void UnregisterFile( httpd_t *p_httpd, httpd_file_t *p_file )
866 _UnregisterFile( p_httpd->p_sys, p_file );
869 static void UnregisterStream( httpd_t *p_httpd, httpd_stream_t *p_stream )
871 _UnregisterFile( p_httpd->p_sys, p_stream );
876 static int _SendStream( httpd_sys_t *p_httpt, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
881 if( i_data <= 0 || p_data == NULL )
883 return( VLC_SUCCESS );
885 //fprintf( stderr, "## i_data=%d pos=%lld\n", i_data, p_stream->i_buffer_pos );
887 vlc_mutex_lock( &p_httpt->file_lock );
889 /* save this pointer (to be used by new connection) */
890 p_stream->i_buffer_last_pos = p_stream->i_buffer_pos;
892 i_pos = p_stream->i_buffer_pos % p_stream->i_buffer_size;
898 i_copy = __MIN( i_count, p_stream->i_buffer_size - i_pos );
900 memcpy( &p_stream->p_buffer[i_pos],
904 i_pos = ( i_pos + i_copy ) % p_stream->i_buffer_size;
909 p_stream->i_buffer_pos += i_data;
910 vlc_mutex_unlock( &p_httpt->file_lock );
912 return( VLC_SUCCESS );
914 static int SendStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
916 return( _SendStream( p_httpd->p_sys, p_stream, p_data, i_data ) );
919 static int HeaderStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
921 httpd_sys_t *p_httpt = p_httpd->p_sys;
923 vlc_mutex_lock( &p_httpt->file_lock );
925 FREE( p_stream->p_header );
926 if( p_data == NULL || i_data <= 0 )
928 p_stream->i_header_size = 0;
932 p_stream->i_header_size = i_data;
933 p_stream->p_header = malloc( i_data );
934 memcpy( p_stream->p_header,
938 vlc_mutex_unlock( &p_httpt->file_lock );
940 return( VLC_SUCCESS );
943 static void httpd_info_add_ss( httpd_info_t *p_info, char *name, char *value )
945 if( p_info->i_count == 0 )
947 p_info->info = malloc( sizeof( httpd_val_t ) );
952 realloc( p_info->info,
953 sizeof( httpd_val_t ) * ( p_info->i_count + 1 ) );
955 p_info->info[p_info->i_count].psz_name = strdup( name );
956 p_info->info[p_info->i_count].psz_value = strdup( value ? value : "(null)");
960 static void httpd_info_add_si( httpd_info_t *p_info, char *name, int i_value )
964 sprintf( v, "%d", i_value );
965 httpd_info_add_ss( p_info, name, v );
968 static void httpd_info_add_sp( httpd_info_t *p_info, char *name, void *value )
972 sprintf( v, "%p", value );
973 httpd_info_add_ss( p_info, name, v );
977 static int Control( httpd_t *p_httpd,
978 int i_query, void *arg1, void *arg2 )
980 httpd_sys_t *p_httpt = p_httpd->p_sys;
981 httpd_info_t *p_info;
982 httpd_connection_t *p_con;
988 case HTTPD_GET_HOSTS:
991 vlc_mutex_lock( &p_httpt->host_lock );
992 for( i = 0; i < p_httpt->i_host_count; i++ )
994 httpd_info_add_sp( p_info,
995 "id", p_httpt->host[i] );
996 httpd_info_add_ss( p_info,
997 "host", p_httpt->host[i]->psz_host_addr );
998 httpd_info_add_ss( p_info,
1000 inet_ntoa(p_httpt->host[i]->sock.sin_addr));
1001 httpd_info_add_si( p_info,
1002 "port", p_httpt->host[i]->i_port );
1004 vlc_mutex_unlock( &p_httpt->host_lock );
1006 case HTTPD_GET_URLS:
1008 p_info->i_count = 0;
1009 /* we can't take file_lock */
1010 for( i = 0; i < p_httpt->i_file_count; i++ )
1012 httpd_info_add_sp( p_info,
1013 "id", p_httpt->file[i] );
1014 httpd_info_add_si( p_info,
1015 "stream", p_httpt->file[i]->b_stream ? 1 : 0 );
1016 httpd_info_add_ss( p_info,
1017 "url", p_httpt->file[i]->psz_file );
1018 httpd_info_add_ss( p_info,
1019 "mime", p_httpt->file[i]->psz_mime );
1020 httpd_info_add_si( p_info,
1021 "protected", p_httpt->file[i]->psz_user ? 1 : 0 );
1022 httpd_info_add_si( p_info,
1023 "used", p_httpt->file[i]->i_ref );
1026 case HTTPD_GET_CONNECTIONS:
1028 p_info->i_count = 0;
1029 /* we can't take lock */
1030 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1032 if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
1034 httpd_info_add_sp( p_info,
1036 httpd_info_add_ss( p_info,
1037 "ip", inet_ntoa( p_con->sock.sin_addr ) );
1038 httpd_info_add_ss( p_info,
1039 "url", p_con->psz_file );
1040 httpd_info_add_si( p_info,
1041 "status", p_con->i_http_error );
1047 p_info->i_count = 0;
1048 return VLC_EGENERIC;
1050 case HTTPD_SET_CLOSE:
1051 sscanf( arg1, "%p", &id );
1052 fprintf( stderr, "Control: HTTPD_SET_CLOSE: id=%p", id );
1054 for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1056 if( (void*)p_con == id )
1058 /* XXX don't free p_con as it could be the one that it is sending ... */
1059 p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
1063 return VLC_EGENERIC;
1066 sscanf( arg1, "%p", &id );
1067 fprintf( stderr, "Control: %p", id );
1069 return VLC_EGENERIC;
1073 /****************************************************************************/
1074 /****************************************************************************/
1075 /****************************************************************************/
1076 /****************************************************************************/
1077 /****************************************************************************/
1079 static int httpd_page_401_get( httpd_file_callback_args_t *p_args,
1080 uint8_t *p_request, int i_request,
1081 uint8_t **pp_data, int *pi_data )
1085 p = *pp_data = malloc( 1024 );
1087 p += sprintf( p, "<html>\n" );
1088 p += sprintf( p, "<head>\n" );
1089 p += sprintf( p, "<title>Error 401</title>\n" );
1090 p += sprintf( p, "</head>\n" );
1091 p += sprintf( p, "<body>\n" );
1092 p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
1093 p += sprintf( p, "<hr />\n" );
1094 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1095 p += sprintf( p, "</body>\n" );
1096 p += sprintf( p, "</html>\n" );
1098 *pi_data = strlen( *pp_data ) + 1;
1102 static int httpd_page_404_get( httpd_file_callback_args_t *p_args,
1103 uint8_t *p_request, int i_request,
1104 uint8_t **pp_data, int *pi_data )
1108 p = *pp_data = malloc( 1024 );
1110 p += sprintf( p, "<html>\n" );
1111 p += sprintf( p, "<head>\n" );
1112 p += sprintf( p, "<title>Error 404</title>\n" );
1113 p += sprintf( p, "</head>\n" );
1114 p += sprintf( p, "<body>\n" );
1115 p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
1116 p += sprintf( p, "<hr />\n" );
1117 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1118 p += sprintf( p, "</body>\n" );
1119 p += sprintf( p, "</html>\n" );
1121 *pi_data = strlen( *pp_data ) + 1;
1127 static int httpd_BanIP( httpd_sys_t *p_httpt, char * psz_new_banned_ip)
1129 httpd_banned_ip_t *p_new_banned_ip ;
1131 p_new_banned_ip = malloc( sizeof( httpd_banned_ip_t ) );
1132 if( !p_new_banned_ip )
1136 p_new_banned_ip->p_next=NULL;
1137 p_new_banned_ip->psz_ip = malloc( strlen( psz_new_banned_ip ) + 1 );
1138 if( !p_new_banned_ip->psz_ip )
1143 strcpy( p_new_banned_ip->psz_ip, psz_new_banned_ip );
1145 msg_Dbg( p_httpt, "Banning IP %s", psz_new_banned_ip );
1147 if( p_httpt->p_first_banned_ip )
1149 httpd_banned_ip_t *p_last;
1151 p_last = p_httpt->p_first_banned_ip;
1152 while( p_last->p_next )
1154 p_last = p_last->p_next;
1157 p_last->p_next = p_new_banned_ip;
1158 p_new_banned_ip->p_prev = p_last;
1162 p_new_banned_ip->p_prev = NULL;
1164 p_httpt->p_first_banned_ip = p_new_banned_ip;
1167 p_httpt->i_banned_ip_count++;
1171 static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *p_httpt, char *psz_ip )
1173 httpd_banned_ip_t *p_ip;
1175 p_ip = p_httpt->p_first_banned_ip;
1179 if( strcmp( psz_ip, p_ip->psz_ip ) == 0 )
1183 p_ip = p_ip->p_next;
1189 static int httpd_UnbanIP( httpd_sys_t *p_httpt, httpd_banned_ip_t *p_banned_ip )
1196 msg_Dbg( p_httpt, "Unbanning IP %s",p_banned_ip->psz_ip);
1198 /* first cut out from list */
1199 if( p_banned_ip->p_prev )
1201 p_banned_ip->p_prev->p_next = p_banned_ip->p_next;
1205 p_httpt->p_first_banned_ip = p_banned_ip->p_next;
1208 if( p_banned_ip->p_next )
1210 p_banned_ip->p_next->p_prev = p_banned_ip->p_prev;
1213 FREE( p_banned_ip->psz_ip );
1214 FREE( p_banned_ip );
1216 p_httpt->i_banned_ip_count--;
1221 static void httpd_ConnnectionNew( httpd_sys_t *p_httpt, int fd, struct sockaddr_in *p_sock )
1223 httpd_connection_t *p_con;
1225 msg_Dbg( p_httpt, "new connection from %s", inet_ntoa( p_sock->sin_addr ) );
1227 /* verify if it's a banned ip */
1228 if(httpd_GetbannedIP( p_httpt,inet_ntoa( p_sock->sin_addr ) ) )
1230 msg_Dbg( p_httpt, "Ip %s banned : closing connection", inet_ntoa( p_sock->sin_addr ) );
1235 /* create a new connection and link it */
1236 p_con = malloc( sizeof( httpd_connection_t ) );
1237 p_con->i_state = HTTPD_CONNECTION_RECEIVING_REQUEST;
1239 p_con->i_last_activity_date = mdate();
1241 p_con->sock = *p_sock;
1242 p_con->psz_file = NULL;
1243 p_con->i_http_error = 0;
1244 p_con->psz_user = NULL;
1245 p_con->psz_password = NULL;
1246 p_con->p_file = NULL;
1248 p_con->i_request_size = 0;
1249 p_con->p_request = NULL;
1251 p_con->i_buffer = 0;
1252 p_con->i_buffer_size = 8096;
1253 p_con->p_buffer = malloc( p_con->i_buffer_size );
1255 p_con->i_stream_pos = 0; // updated by httpd_thread */
1256 p_con->p_next = NULL;
1258 if( p_httpt->p_first_connection )
1260 httpd_connection_t *p_last;
1262 p_last = p_httpt->p_first_connection;
1263 while( p_last->p_next )
1265 p_last = p_last->p_next;
1268 p_last->p_next = p_con;
1269 p_con->p_prev = p_last;
1273 p_con->p_prev = NULL;
1275 p_httpt->p_first_connection = p_con;
1278 p_httpt->i_connection_count++;
1281 static void httpd_ConnnectionClose( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1283 msg_Dbg( p_httpt, "close connection from %s", inet_ntoa( p_con->sock.sin_addr ) );
1285 p_httpt->i_connection_count--;
1286 /* first cut out from list */
1289 p_con->p_prev->p_next = p_con->p_next;
1293 p_httpt->p_first_connection = p_con->p_next;
1298 p_con->p_next->p_prev = p_con->p_prev;
1301 if( p_con->p_file ) p_con->p_file->i_ref--;
1302 FREE( p_con->psz_file );
1304 FREE( p_con->p_buffer );
1305 SOCKET_CLOSE( p_con->fd );
1307 FREE( p_con->psz_user );
1308 FREE( p_con->psz_password );
1310 FREE( p_con->p_request );
1314 static void httpd_RequestGetWord( char *word, int i_word_max, char **pp_buffer, char *p_end )
1316 char *p = *pp_buffer;
1319 while( p < p_end && *p && ( *p == ' ' || *p == '\t' ) )
1325 for( i = 0; i < i_word_max && p < p_end && *p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r'; i++,p++)
1330 word[__MIN( i, i_word_max -1 )] = '\0';
1335 static int httpd_RequestNextLine( char **pp_buffer, char *p_end )
1339 for( p = *pp_buffer; p < p_end; p++ )
1341 if( p + 1 < p_end && *p == '\n' )
1346 if( p + 2 < p_end && p[0] == '\r' && p[1] == '\n' )
1353 return VLC_EGENERIC;
1356 //char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1357 static void b64_decode( char *dest, char *src )
1362 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1363 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1364 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1365 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1366 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1367 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1368 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1369 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1370 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1371 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1372 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1373 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1374 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1375 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1376 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1377 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1380 for( i_level = 0; *src != '\0'; src++ )
1384 c = b64[(unsigned int)*src];
1397 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1401 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1405 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1414 static void httpd_ConnectionParseRequest( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1423 char user[512] = "";
1424 char password[512] = "";
1426 //msg_Dbg( p_httpt, "new request=\n%s", p_con->p_buffer );
1429 p = p_con->p_buffer;
1430 p_end = p + strlen( p ) + 1;
1432 httpd_RequestGetWord( command, 32, &p, p_end );
1433 httpd_RequestGetWord( url, 1024, &p, p_end );
1434 httpd_RequestGetWord( version, 32, &p, p_end );
1435 //msg_Dbg( p_httpt, "ask =%s= =%s= =%s=", command, url, version );
1437 p_con->p_request = NULL;
1438 p_con->i_request_size = 0;
1439 if( !strcmp( command, "GET" ) )
1441 p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1443 else if( !strcmp( command, "POST" ))
1445 p_con->i_method = HTTPD_CONNECTION_METHOD_POST;
1450 p_con->psz_file = strdup( "/501.html" );
1451 p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1452 p_con->i_http_error = 501;
1456 if( strcmp( version, "HTTP/1.0" ) && strcmp( version, "HTTP/1.1" ) )
1458 p_con->psz_file = strdup( "/505.html" );
1459 p_con->i_http_error = 505;
1469 if( httpd_RequestNextLine( &p, p_end ) )
1471 //msg_Dbg( p_httpt, "failled new line" );
1474 //msg_Dbg( p_httpt, "new line=%s", p );
1476 httpd_RequestGetWord( header, 1024, &p, p_end );
1477 if( !strcmp( header, "\r\n" ) || !strcmp( header, "\n" ) )
1482 if( !strcmp( header, "Authorization:" ) )
1486 httpd_RequestGetWord( method, 32, &p, p_end );
1487 if( !strcasecmp( method, "BASIC" ) )
1492 httpd_RequestGetWord( basic, 1024, &p, p_end );
1493 // msg_Dbg( p_httpt, "Authorization: basic:%s", basic );
1494 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 && p_con->p_file->i_header_size > 0 )
1640 /* add stream header */
1641 memcpy( &p_con->p_buffer[p_con->i_buffer_size],
1642 p_con->p_file->p_header,
1643 p_con->p_file->i_header_size );
1644 p_con->i_buffer_size += p_con->p_file->i_header_size;
1647 //msg_Dbg( p_httpt, "answer=\n%s", p_con->p_buffer );
1649 #define HTTPD_STREAM_PACKET 10000
1650 static void httpd_Thread( httpd_sys_t *p_httpt )
1652 httpd_file_t *p_page_401;
1653 httpd_file_t *p_page_404;
1655 httpd_connection_t *p_con;
1657 msg_Info( p_httpt, "httpd started" );
1659 p_page_401 = _RegisterFile( p_httpt,
1660 "/401.html", "text/html",
1664 (httpd_file_callback_args_t*)NULL );
1665 p_page_404 = _RegisterFile( p_httpt,
1666 "/404.html", "text/html",
1670 (httpd_file_callback_args_t*)NULL );
1672 while( !p_httpt->b_die )
1674 struct timeval timeout;
1677 int i_handle_max = 0;
1680 if( p_httpt->i_host_count <= 0 )
1682 msleep( 100 * 1000 );
1686 /* we will create a socket set with host and connection */
1687 FD_ZERO( &fds_read );
1688 FD_ZERO( &fds_write );
1690 vlc_mutex_lock( &p_httpt->host_lock );
1691 vlc_mutex_lock( &p_httpt->connection_lock );
1692 for( i = 0; i < p_httpt->i_host_count; i++ )
1694 FD_SET( p_httpt->host[i]->fd, &fds_read );
1695 i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd );
1697 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1699 /* no more than 10s of inactivity */
1700 if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ||
1701 p_con->i_state == HTTPD_CONNECTION_TO_BE_CLOSED)
1703 httpd_connection_t *p_next = p_con->p_next;
1705 msg_Dbg( p_httpt, "close unused connection" );
1706 httpd_ConnnectionClose( p_httpt, p_con );
1711 if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos )
1713 p_con = p_con->p_next;
1717 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1719 FD_SET( p_con->fd, &fds_read );
1723 FD_SET( p_con->fd, &fds_write );
1725 i_handle_max = __MAX( i_handle_max, p_con->fd );
1727 p_con = p_con->p_next;
1729 vlc_mutex_unlock( &p_httpt->host_lock );
1730 vlc_mutex_unlock( &p_httpt->connection_lock );
1732 /* we will wait 0.5s */
1734 timeout.tv_usec = 500*1000;
1736 i_ret = select( i_handle_max + 1,
1741 if( i_ret == -1 && errno != EINTR )
1743 msg_Warn( p_httpt, "cannot select sockets" );
1753 vlc_mutex_lock( &p_httpt->host_lock );
1754 /* accept/refuse new connection */
1755 for( i = 0; i < p_httpt->i_host_count; i++ )
1757 int i_sock_size = sizeof( struct sockaddr_in );
1758 struct sockaddr_in sock;
1761 fd = accept( p_httpt->host[i]->fd, (struct sockaddr *)&sock,
1765 #if defined( WIN32 ) || defined( UNDER_CE )
1767 unsigned long i_dummy = 1;
1768 ioctlsocket( fd, FIONBIO, &i_dummy );
1771 fcntl( fd, F_SETFL, O_NONBLOCK );
1774 if( p_httpt->i_connection_count >= HTTPD_MAX_CONNECTION )
1776 msg_Warn( p_httpt, "max connection reached" );
1780 /* create a new connection and link it */
1781 httpd_ConnnectionNew( p_httpt, fd, &sock );
1785 vlc_mutex_unlock( &p_httpt->host_lock );
1787 vlc_mutex_lock( &p_httpt->file_lock );
1788 /* now do work for all connections */
1789 for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1791 if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1795 i_len = recv( p_con->fd,
1796 p_con->p_buffer + p_con->i_buffer,
1797 p_con->i_buffer_size - p_con->i_buffer, 0 );
1800 #if defined( WIN32 ) || defined( UNDER_CE )
1801 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1803 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1806 httpd_connection_t *p_next = p_con->p_next;
1808 httpd_ConnnectionClose( p_httpt, p_con );
1811 else if( i_len > 0 )
1814 p_con->i_last_activity_date = mdate();
1815 p_con->i_buffer += i_len;
1817 ptr = p_con->p_buffer + p_con->i_buffer;
1819 if( ( p_con->i_buffer >= 2 && !strncmp( ptr - 2, "\n\n", 2 ) )||
1820 ( p_con->i_buffer >= 4 && !strncmp( ptr - 4, "\r\n\r\n", 4 ) ) ||
1821 p_con->i_buffer >= p_con->i_buffer_size )
1823 p_con->p_buffer[__MIN( p_con->i_buffer, p_con->i_buffer_size - 1 )] = '\0';
1824 httpd_ConnectionParseRequest( p_httpt, p_con );
1827 p_con = p_con->p_next;
1831 p_con = p_con->p_next;
1833 continue; /* just for clarity */
1835 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER || p_con->i_state == HTTPD_CONNECTION_SENDING_FILE )
1840 if( p_con->i_buffer_size - p_con->i_buffer > 0 )
1842 i_len = send( p_con->fd, p_con->p_buffer + p_con->i_buffer, p_con->i_buffer_size - p_con->i_buffer, 0 );
1848 // 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 );
1850 #if defined( WIN32 ) || defined( UNDER_CE )
1851 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1853 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1856 httpd_connection_t *p_next = p_con->p_next;
1858 httpd_ConnnectionClose( p_httpt, p_con );
1861 else if( i_len > 0 )
1863 p_con->i_last_activity_date = mdate();
1864 p_con->i_buffer += i_len;
1866 if( p_con->i_buffer >= p_con->i_buffer_size )
1868 if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER )
1870 p_con->i_buffer_size = 0;
1871 p_con->i_buffer = 0;
1872 FREE( p_con->p_buffer );
1874 if( !p_con->p_file->b_stream )
1876 p_con->i_state = HTTPD_CONNECTION_SENDING_FILE; // be sure to out from HTTPD_CONNECTION_SENDING_HEADER
1877 if( p_con->i_method == HTTPD_CONNECTION_METHOD_GET )
1879 p_con->p_file->pf_get( p_con->p_file->p_sys,
1880 p_con->p_request, p_con->i_request_size,
1881 &p_con->p_buffer, &p_con->i_buffer_size );
1883 else if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1885 p_con->p_file->pf_post( p_con->p_file->p_sys,
1886 p_con->p_request, p_con->i_request_size,
1887 &p_con->p_buffer, &p_con->i_buffer_size );
1891 p_con->p_buffer = NULL;
1892 p_con->i_buffer_size = 0;
1897 p_con->i_state = HTTPD_CONNECTION_SENDING_STREAM;
1898 p_con->i_stream_pos = p_con->p_file->i_buffer_last_pos;
1900 p_con = p_con->p_next;
1904 httpd_connection_t *p_next = p_con->p_next;
1906 httpd_ConnnectionClose( p_httpt, p_con );
1912 p_con = p_con->p_next;
1917 p_con = p_con->p_next;
1919 continue; /* just for clarity */
1921 else if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM )
1923 httpd_stream_t *p_stream = p_con->p_file;
1927 if( p_con->i_stream_pos < p_stream->i_buffer_pos )
1930 /* check if this p_con aren't to late */
1931 if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
1933 fprintf( stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n",
1934 p_con->i_stream_pos, p_stream->i_buffer_pos );
1935 p_con->i_stream_pos = p_stream->i_buffer_last_pos;
1938 i_pos = p_con->i_stream_pos % p_stream->i_buffer_size;
1939 /* size until end of buffer */
1940 i_write = p_stream->i_buffer_size - i_pos;
1941 /* is it more than valid data */
1942 if( i_write >= p_stream->i_buffer_pos - p_con->i_stream_pos )
1944 i_write = p_stream->i_buffer_pos - p_con->i_stream_pos;
1946 /* limit to HTTPD_STREAM_PACKET */
1947 if( i_write > HTTPD_STREAM_PACKET )
1949 i_write = HTTPD_STREAM_PACKET;
1951 i_send = send( p_con->fd, &p_stream->p_buffer[i_pos], i_write, 0 );
1953 #if defined( WIN32 ) || defined( UNDER_CE )
1954 if( ( i_send < 0 && WSAGetLastError() != WSAEWOULDBLOCK )|| ( i_send == 0 ) )
1956 if( ( i_send < 0 && errno != EAGAIN && errno != EINTR )|| ( i_send == 0 ) )
1959 httpd_connection_t *p_next = p_con->p_next;
1961 httpd_ConnnectionClose( p_httpt, p_con );
1965 else if( i_send > 0 )
1967 p_con->i_last_activity_date = mdate();
1968 p_con->i_stream_pos += i_send;
1971 p_con = p_con->p_next;
1972 continue; /* just for clarity */
1974 else if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
1976 msg_Warn( p_httpt, "cannot occur (Invalid p_con->i_state)" );
1977 p_con = p_con->p_next;
1979 } /* for over connection */
1981 vlc_mutex_unlock( &p_httpt->file_lock );
1984 msg_Info( p_httpt, "httpd stopped" );
1986 _UnregisterFile( p_httpt, p_page_401 );
1987 _UnregisterFile( p_httpt, p_page_404 );