1fc89b551136c0bed5d85052853f0ce20e1a576b
[vlc.git] / modules / misc / httpd.c
1 /*****************************************************************************
2  * httpd.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: httpd.c,v 1.19 2003/07/01 09:00:50 adn Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include <string.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <vlc/vlc.h>
37 #include "httpd.h"
38
39 #ifdef HAVE_SYS_TIME_H
40 #    include <sys/time.h>
41 #endif
42
43 #ifdef HAVE_UNISTD_H
44 #   include <unistd.h>
45 #endif
46
47 #if defined( UNDER_CE )
48 #   include <winsock.h>
49 #elif defined( WIN32 )
50 #   include <winsock2.h>
51 #   include <ws2tcpip.h>
52 #   ifndef IN_MULTICAST
53 #       define IN_MULTICAST(a) IN_CLASSD(a)
54 #   endif
55 #else
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() */
61 #   endif
62 #endif
63
64 #include "network.h"
65
66 #ifndef INADDR_ANY
67 #   define INADDR_ANY  0x00000000
68 #endif
69 #ifndef INADDR_NONE
70 #   define INADDR_NONE 0xFFFFFFFF
71 #endif
72
73 #define LISTEN_BACKLOG          100
74 #define HTTPD_MAX_CONNECTION    512
75 #define HTTPD_CONNECTION_MAX_UNUSED 10000000
76
77 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
78
79 #if defined( WIN32 ) || defined( UNDER_CE )
80 #define SOCKET_CLOSE(a)    closesocket(a)
81 #else
82 #define SOCKET_CLOSE(a)    close(a)
83 #endif
84
85 /*****************************************************************************
86  * Exported prototypes
87  *****************************************************************************/
88 static int              Open   ( vlc_object_t * );
89 static void             Close  ( vlc_object_t * );
90
91 /*****************************************************************************
92  * Module descriptor
93  *****************************************************************************/
94 vlc_module_begin();
95     set_description( _("HTTP 1.0 daemon") );
96     set_capability( "httpd", 42 );
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100 /*****************************************************************************
101  * Prototypes
102  *****************************************************************************/
103 static httpd_host_t     *RegisterHost   ( httpd_t *, char *, int );
104 static void             UnregisterHost  ( httpd_t *, httpd_host_t * );
105
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 * );
113
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
122 /*****************************************************************************
123  * Internal definitions
124  *****************************************************************************/
125 struct httpd_host_t
126 {
127     int    i_ref;
128
129     char   *psz_host_addr;
130     int    i_port;
131
132     struct sockaddr_in sock;
133     int    fd;
134
135 };
136
137 #define HTTPD_AUTHENTICATE_NONE     0
138 #define HTTPD_AUTHENTICATE_BASIC    1
139
140 //typedef httpd_file_t httpd_stream_t;
141
142 struct httpd_file_t
143 {
144     int         i_ref;
145
146     char        *psz_file;
147     char        *psz_mime;
148
149
150     int         i_authenticate_method;
151         char        *psz_user;          /* NULL if no auth */
152         char        *psz_password;      /* NULL if no auth */
153
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 */
159
160     /* private */
161
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 */
167
168     /* data to be send at connection time (if any) */
169     int         i_header_size;
170     uint8_t     *p_header;
171 };
172
173
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
179
180 #define HTTPD_CONNECTION_METHOD_GET             1
181 #define HTTPD_CONNECTION_METHOD_POST            2
182 typedef struct httpd_connection_s
183 {
184     struct httpd_connection_s *p_next;
185     struct httpd_connection_s *p_prev;
186
187     struct  sockaddr_in sock;
188     int     fd;
189     mtime_t i_last_activity_date;
190
191     int    i_state;
192     int    i_method;       /* get/post */
193
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
197     char    *psz_password;
198
199     uint8_t *p_request;     // whith get: ?<*>, with post: main data
200     int      i_request_size;
201
202     httpd_file_t    *p_file;
203
204     /* used while sending header and file */
205     int     i_buffer_size;
206     uint8_t *p_buffer;
207     int     i_buffer;            /* private */
208
209     /* used for stream */
210     int64_t i_stream_pos;   /* absolute pos in stream */
211 } httpd_connection_t;
212
213 /* Linked List of banned IP */
214 typedef struct httpd_banned_ip_s
215 {
216     struct httpd_banned_ip_s *p_next;
217     struct httpd_banned_ip_s *p_prev;
218     
219     char *psz_ip;
220
221 } httpd_banned_ip_t;
222 /*
223  * The httpd thread
224  */
225 struct httpd_sys_t
226 {
227     VLC_COMMON_MEMBERS
228
229     vlc_mutex_t             host_lock;
230     volatile int            i_host_count;
231     httpd_host_t            **host;
232
233     vlc_mutex_t             file_lock;
234     int                     i_file_count;
235     httpd_file_t            **file;
236
237     vlc_mutex_t             connection_lock;
238     int                     i_connection_count;
239     httpd_connection_t      *p_first_connection;
240
241     vlc_mutex_t             ban_lock;
242     int                     i_banned_ip_count;
243     httpd_banned_ip_t       *p_first_banned_ip;           
244 };
245
246 static void httpd_Thread( httpd_sys_t *p_httpt );
247 static void httpd_ConnnectionNew( httpd_sys_t *, int , struct sockaddr_in * );
248 static void httpd_ConnnectionClose( httpd_sys_t *, httpd_connection_t * );
249 static int httpd_UnbanIP( httpd_sys_t *, httpd_banned_ip_t *);
250 static int httpd_BanIP( httpd_sys_t *, char *);
251 static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *, char * );
252 /*****************************************************************************
253  * Open:
254  *****************************************************************************/
255
256 static int Open( vlc_object_t *p_this )
257 {
258     httpd_t     *p_httpd = (httpd_t*)p_this;
259     httpd_sys_t *p_httpt;
260
261     /* Launch httpt thread */
262     if( !( p_httpt = vlc_object_create( p_this, sizeof( httpd_sys_t ) ) ) )
263     {
264         msg_Err( p_this, "out of memory" );
265         return( VLC_EGENERIC );
266     }
267
268     p_httpt->b_die  = 0;
269     p_httpt->b_error= 0;
270
271     /* init httpt_t structure */
272     vlc_mutex_init( p_httpd, &p_httpt->host_lock );
273     p_httpt->i_host_count = 0;
274     p_httpt->host = NULL;
275
276     vlc_mutex_init( p_httpd, &p_httpt->file_lock );
277     p_httpt->i_file_count = 0;
278     p_httpt->file = NULL;
279
280     vlc_mutex_init( p_httpd, &p_httpt->connection_lock );
281     p_httpt->i_connection_count = 0;
282     p_httpt->p_first_connection = NULL;
283
284     vlc_mutex_init( p_httpd, &p_httpt->ban_lock );
285     p_httpt->i_banned_ip_count = 0;
286     p_httpt->p_first_banned_ip = NULL;
287                 
288     /* start the thread */
289     if( vlc_thread_create( p_httpt, "httpd thread",
290                            httpd_Thread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
291     {
292         msg_Err( p_this, "cannot spawn http thread" );
293
294         vlc_mutex_destroy( &p_httpt->host_lock );
295         vlc_mutex_destroy( &p_httpt->file_lock );
296         vlc_mutex_destroy( &p_httpt->connection_lock );
297         vlc_mutex_destroy( &p_httpt->ban_lock );                
298
299         vlc_object_destroy( p_httpt );
300         return( VLC_EGENERIC );
301     }
302
303     msg_Info( p_httpd, "http thread launched" );
304
305     p_httpd->p_sys = p_httpt;
306     p_httpd->pf_register_host   = RegisterHost;
307     p_httpd->pf_unregister_host = UnregisterHost;
308     p_httpd->pf_register_file   = RegisterFile;
309     p_httpd->pf_unregister_file = UnregisterFile;
310     p_httpd->pf_register_stream = RegisterStream;
311     p_httpd->pf_header_stream   = HeaderStream;
312     p_httpd->pf_send_stream     = SendStream;
313     p_httpd->pf_unregister_stream=UnregisterStream;
314
315     return( VLC_SUCCESS );
316 }
317
318 /*****************************************************************************
319  * Close: close the target
320  *****************************************************************************/
321 static void Close( vlc_object_t * p_this )
322 {
323     httpd_t     *p_httpd = (httpd_t*)p_this;
324     httpd_sys_t *p_httpt = p_httpd->p_sys;
325
326     httpd_connection_t *p_con;
327     httpd_banned_ip_t *p_banned_ip;
328     
329     int i;
330
331     p_httpt->b_die = 1;
332     vlc_thread_join( p_httpt );
333
334     /* first close all host */
335     vlc_mutex_destroy( &p_httpt->host_lock );
336     if( p_httpt->i_host_count )
337     {
338         msg_Err( p_httpd, "still have %d hosts registered !", p_httpt->i_host_count );
339     }
340     for( i = 0; i < p_httpt->i_host_count; i++ )
341     {
342 #define p_host p_httpt->host[i]
343         FREE( p_host->psz_host_addr );
344         SOCKET_CLOSE( p_host->fd );
345
346         FREE( p_host );
347 #undef p_host
348     }
349     FREE( p_httpt->host );
350
351     /* now all file */
352     vlc_mutex_destroy( &p_httpt->file_lock );
353     if( p_httpt->i_file_count )
354     {
355         msg_Err( p_httpd, "still have %d files registered !", p_httpt->i_file_count );
356     }
357     for( i = 0; i < p_httpt->i_file_count; i++ )
358     {
359 #define p_file p_httpt->file[i]
360         FREE( p_file->psz_file );
361         FREE( p_file->psz_mime );
362         if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
363         {
364             FREE( p_file->psz_user );
365             FREE( p_file->psz_password );
366         }
367         FREE( p_file->p_buffer );
368
369         FREE( p_file );
370 #undef p_file
371     }
372     FREE( p_httpt->file );
373
374     /* andd close all connection */
375     vlc_mutex_destroy( &p_httpt->connection_lock );
376     if( p_httpt->i_connection_count )
377     {
378         msg_Warn( p_httpd, "%d connections still in use", p_httpt->i_connection_count );
379     }
380     while( ( p_con = p_httpt->p_first_connection ) )
381     {
382         httpd_ConnnectionClose( p_httpt, p_con );
383     }
384     
385     /* Free all banned IP */   
386     vlc_mutex_destroy( &p_httpt->ban_lock );
387     while( ( p_banned_ip = p_httpt->p_first_banned_ip))
388     {
389         httpd_UnbanIP(p_httpt,p_banned_ip);
390     }
391             
392     msg_Info( p_httpd, "httpd instance closed" );
393     vlc_object_destroy( p_httpt );
394 }
395
396
397 /****************************************************************************
398  ****************************************************************************
399  ***
400  ***
401  ****************************************************************************
402  ****************************************************************************/
403 static int BuildAddr( struct sockaddr_in * p_socket,
404                       const char * psz_address, int i_port )
405 {
406     /* Reset struct */
407     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
408     p_socket->sin_family = AF_INET;                                /* family */
409     p_socket->sin_port = htons( (uint16_t)i_port );
410     if( !*psz_address )
411     {
412         p_socket->sin_addr.s_addr = INADDR_ANY;
413     }
414     else
415     {
416         struct hostent    * p_hostent;
417
418         /* Try to convert address directly from in_addr - this will work if
419          * psz_address is dotted decimal. */
420 #ifdef HAVE_ARPA_INET_H
421         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
422 #else
423         p_socket->sin_addr.s_addr = inet_addr( psz_address );
424         if( p_socket->sin_addr.s_addr == INADDR_NONE )
425 #endif
426         {
427             /* We have a fqdn, try to find its address */
428             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
429             {
430                 return( -1 );
431             }
432
433             /* Copy the first address of the host in the socket address */
434             memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
435                      p_hostent->h_length );
436         }
437     }
438     return( 0 );
439 }
440
441
442 /*
443  * listen on a host for a httpd instance
444  */
445
446 static httpd_host_t *_RegisterHost( httpd_sys_t *p_httpt, char *psz_host_addr, int i_port )
447 {
448     httpd_host_t    *p_host;
449     struct sockaddr_in  sock;
450     int i;
451     int fd = -1;
452     int i_opt;
453 #if !defined( WIN32 ) && !defined( UNDER_CE )
454     int i_flags;
455 #endif
456
457     if( BuildAddr( &sock, psz_host_addr, i_port ) )
458     {
459         msg_Err( p_httpt, "cannot build address for %s:%d", psz_host_addr, i_port );
460         return NULL;
461     }
462
463     /* is it already declared ? */
464     vlc_mutex_lock( &p_httpt->host_lock );
465     for( i = 0; i < p_httpt->i_host_count; i++ )
466     {
467         if( p_httpt->host[i]->sock.sin_port == sock.sin_port &&
468             ( p_httpt->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
469             p_httpt->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
470         {
471             break;
472         }
473     }
474
475     if( i < p_httpt->i_host_count )
476     {
477         /* yes, increment ref count and succed */
478         p_httpt->host[i]->i_ref++;
479         vlc_mutex_unlock( &p_httpt->host_lock );
480         return( p_httpt->host[i] );
481     }
482
483     /* need to add a new listening socket */
484
485     /* open socket */
486     fd = socket( AF_INET, SOCK_STREAM, 0 );
487     if( fd < 0 )
488     {
489         msg_Err( p_httpt, "cannot open socket" );
490         goto socket_failed;
491     }
492     /* reuse socket */
493     i_opt = 1;
494     if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
495                     (void *) &i_opt, sizeof( i_opt ) ) < 0 )
496     {
497         msg_Warn( p_httpt, "cannot configure socket (SO_REUSEADDR)" );
498     }
499     /* bind it */
500     if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
501     {
502         msg_Err( p_httpt, "cannot bind socket" );
503         goto socket_failed;
504     }
505     /* set to non-blocking */
506 #if defined( WIN32 ) || defined( UNDER_CE )
507     {
508         unsigned long i_dummy = 1;
509         if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
510         {
511             msg_Err( p_httpt, "cannot set socket to non-blocking mode" );
512             goto socket_failed;
513         }
514     }
515 #else
516     if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
517     {
518         msg_Err( p_httpt, "cannot F_GETFL socket" );
519         goto socket_failed;
520     }
521     if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
522     {
523         msg_Err( p_httpt, "cannot F_SETFL O_NONBLOCK" );
524         goto socket_failed;
525     }
526 #endif
527     /* listen */
528     if( listen( fd, LISTEN_BACKLOG ) < 0 )
529     {
530         msg_Err( p_httpt, "cannot listen socket" );
531         goto socket_failed;
532     }
533
534     if( p_httpt->host )
535     {
536         p_httpt->host = realloc( p_httpt->host, sizeof( httpd_host_t *)  * ( p_httpt->i_host_count + 1 ) );
537     }
538     else
539     {
540         p_httpt->host = malloc( sizeof( httpd_host_t *) );
541     }
542     p_host                = malloc( sizeof( httpd_host_t ) );
543     p_host->i_ref         = 1;
544     p_host->psz_host_addr = strdup( psz_host_addr );
545     p_host->i_port        = i_port;
546     p_host->sock          = sock;
547     p_host->fd            = fd;
548
549     p_httpt->host[p_httpt->i_host_count++] = p_host;
550     vlc_mutex_unlock( &p_httpt->host_lock );
551
552     return p_host;
553
554 socket_failed:
555     vlc_mutex_unlock( &p_httpt->host_lock );
556     if( fd >= 0 )
557     {
558         SOCKET_CLOSE( fd );
559     }
560     return NULL;
561 }
562 static httpd_host_t     *RegisterHost( httpd_t *p_httpd, char *psz_host_addr, int i_port )
563 {
564     return( _RegisterHost( p_httpd->p_sys, psz_host_addr, i_port ) );
565 }
566
567 /*
568  * remove a listening host for an httpd instance
569  */
570 static void            _UnregisterHost( httpd_sys_t *p_httpt, httpd_host_t *p_host )
571 {
572     int i;
573
574     vlc_mutex_lock( &p_httpt->host_lock );
575     for( i = 0; i < p_httpt->i_host_count; i++ )
576     {
577         if( p_httpt->host[i] == p_host )
578         {
579             break;
580         }
581     }
582     if( i >= p_httpt->i_host_count )
583     {
584         vlc_mutex_unlock( &p_httpt->host_lock );
585         msg_Err( p_httpt, "cannot unregister host" );
586         return;
587     }
588
589     p_host->i_ref--;
590
591     if( p_host->i_ref > 0 )
592     {
593         /* still in use */
594         vlc_mutex_unlock( &p_httpt->host_lock );
595         return;
596     }
597
598     /* no more used */
599     FREE( p_host->psz_host_addr );
600     SOCKET_CLOSE( p_host->fd );
601
602     FREE( p_host );
603
604     if( p_httpt->i_host_count <= 1 )
605     {
606         FREE( p_httpt->host );
607         p_httpt->i_host_count = 0;
608     }
609     else
610     {
611         int i_move;
612
613         i_move = p_httpt->i_host_count - i - 1;
614
615         if( i_move > 0 )
616         {
617             memmove( &p_httpt->host[i],
618                      &p_httpt->host[i+1],
619                      i_move * sizeof( httpd_host_t * ) );
620         }
621
622         p_httpt->i_host_count--;
623         p_httpt->host = realloc( p_httpt->host,
624                                  p_httpt->i_host_count * sizeof( httpd_host_t * ) );
625     }
626
627     vlc_mutex_unlock( &p_httpt->host_lock );
628 }
629 static void             UnregisterHost( httpd_t *p_httpd, httpd_host_t *p_host )
630 {
631     _UnregisterHost( p_httpd->p_sys, p_host );
632 }
633
634
635 static void __RegisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
636 {
637     /* add a new file */
638     if( p_httpt->i_file_count )
639     {
640         p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *)  * ( p_httpt->i_file_count + 1 ) );
641     }
642     else
643     {
644         p_httpt->file = malloc( sizeof( httpd_file_t *) );
645     }
646
647     p_httpt->file[p_httpt->i_file_count++] = p_file;
648 }
649
650 static httpd_file_t    *_RegisterFile( httpd_sys_t *p_httpt,
651                                        char *psz_file, char *psz_mime,
652                                        char *psz_user, char *psz_password,
653                                        httpd_file_callback pf_get,
654                                        httpd_file_callback pf_post,
655                                        httpd_file_callback_args_t *p_args )
656 {
657     httpd_file_t    *p_file;
658     int i;
659
660     vlc_mutex_lock( &p_httpt->file_lock );
661     for( i = 0; i < p_httpt->i_file_count; i++ )
662     {
663         if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
664         {
665             break;
666         }
667     }
668     if( i < p_httpt->i_file_count )
669     {
670         vlc_mutex_unlock( &p_httpt->file_lock );
671         msg_Err( p_httpt, "%s already registered", psz_file );
672         return NULL;
673     }
674
675     p_file              = malloc( sizeof( httpd_file_t ) );
676     p_file->i_ref       = 0;
677     p_file->psz_file    = strdup( psz_file );
678     p_file->psz_mime    = strdup( psz_mime );
679     if( psz_user && *psz_user )
680     {
681         p_file->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
682         p_file->psz_user              = strdup( psz_user );
683         p_file->psz_password          = strdup( psz_password );
684     }
685     else
686     {
687         p_file->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
688         p_file->psz_user              = NULL;
689         p_file->psz_password          = NULL;
690     }
691
692     p_file->b_stream          = VLC_FALSE;
693     p_file->p_sys             = p_args;
694     p_file->pf_get            = pf_get;
695     p_file->pf_post           = pf_post;
696
697     p_file->i_buffer_size     = 0;
698     p_file->i_buffer_last_pos = 0;
699     p_file->i_buffer_pos      = 0;
700     p_file->p_buffer          = NULL;
701
702     p_file->i_header_size     = 0;
703     p_file->p_header          = NULL;
704
705     __RegisterFile( p_httpt, p_file );
706
707     vlc_mutex_unlock( &p_httpt->file_lock );
708
709     return p_file;
710 }
711 static httpd_file_t     *RegisterFile( httpd_t *p_httpd,
712                                        char *psz_file, char *psz_mime,
713                                        char *psz_user, char *psz_password,
714                                        httpd_file_callback pf_get,
715                                        httpd_file_callback pf_post,
716                                        httpd_file_callback_args_t *p_args )
717 {
718     return( _RegisterFile( p_httpd->p_sys,
719                            psz_file, psz_mime, psz_user, psz_password,
720                            pf_get, pf_post, p_args ) );
721 }
722
723 static httpd_stream_t  *_RegisterStream( httpd_sys_t *p_httpt,
724                                          char *psz_file, char *psz_mime,
725                                          char *psz_user, char *psz_password )
726 {
727     httpd_stream_t    *p_stream;
728     int i;
729
730     vlc_mutex_lock( &p_httpt->file_lock );
731     for( i = 0; i < p_httpt->i_file_count; i++ )
732     {
733         if( !strcmp( psz_file, p_httpt->file[i]->psz_file ) )
734         {
735             break;
736         }
737     }
738     if( i < p_httpt->i_file_count )
739     {
740         vlc_mutex_unlock( &p_httpt->file_lock );
741         msg_Err( p_httpt, "%s already registered", psz_file );
742         return NULL;
743     }
744
745     p_stream              = malloc( sizeof( httpd_stream_t ) );
746     p_stream->i_ref       = 0;
747     p_stream->psz_file    = strdup( psz_file );
748     p_stream->psz_mime    = strdup( psz_mime );
749     if( psz_user && *psz_user )
750     {
751         p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_BASIC;
752         p_stream->psz_user              = strdup( psz_user );
753         p_stream->psz_password          = strdup( psz_password );
754     }
755     else
756     {
757         p_stream->i_authenticate_method = HTTPD_AUTHENTICATE_NONE;
758         p_stream->psz_user              = NULL;
759         p_stream->psz_password          = NULL;
760     }
761
762     p_stream->b_stream        = VLC_TRUE;
763     p_stream->p_sys           = NULL;
764     p_stream->pf_get          = NULL;
765     p_stream->pf_post         = NULL;
766
767     p_stream->i_buffer_size   = 5*1024*1024;
768     p_stream->i_buffer_pos      = 0;
769     p_stream->i_buffer_last_pos = 0;
770     p_stream->p_buffer        = malloc( p_stream->i_buffer_size );
771
772     p_stream->i_header_size   = 0;
773     p_stream->p_header        = NULL;
774
775     __RegisterFile( p_httpt, p_stream );
776
777     vlc_mutex_unlock( &p_httpt->file_lock );
778
779     return p_stream;
780 }
781 static httpd_stream_t   *RegisterStream( httpd_t *p_httpd,
782                                          char *psz_file, char *psz_mime,
783                                          char *psz_user, char *psz_password )
784 {
785     return( _RegisterStream( p_httpd->p_sys,
786                              psz_file, psz_mime, psz_user, psz_password ) );
787 }
788
789 static void            _UnregisterFile( httpd_sys_t *p_httpt, httpd_file_t *p_file )
790 {
791     int i;
792
793     vlc_mutex_lock( &p_httpt->file_lock );
794     for( i = 0; i < p_httpt->i_file_count; i++ )
795     {
796         if( !strcmp( p_file->psz_file, p_httpt->file[i]->psz_file ) )
797         {
798             break;
799         }
800     }
801     if( i >= p_httpt->i_file_count )
802     {
803         vlc_mutex_unlock( &p_httpt->file_lock );
804         msg_Err( p_httpt, "cannot unregister file" );
805         return;
806     }
807
808     if( p_file->i_ref > 0 )
809     {
810         httpd_connection_t *p_con;
811         /* force closing all connection for this file */
812         msg_Err( p_httpt, "closing all client connection" );
813
814         vlc_mutex_lock( &p_httpt->connection_lock );
815         for( p_con = p_httpt->p_first_connection; p_con != NULL; )
816         {
817             httpd_connection_t *p_next;
818
819             p_next = p_con->p_next;
820             if( p_con->p_file == p_file )
821             {
822                 httpd_ConnnectionClose( p_httpt, p_con );
823             }
824             p_con = p_next;
825         }
826         vlc_mutex_unlock( &p_httpt->connection_lock );
827     }
828
829     FREE( p_file->psz_file );
830     FREE( p_file->psz_mime );
831     if( p_file->i_authenticate_method != HTTPD_AUTHENTICATE_NONE )
832     {
833         FREE( p_file->psz_user );
834         FREE( p_file->psz_password );
835     }
836     FREE( p_file->p_buffer );
837     FREE( p_file->p_header );
838
839     FREE( p_file );
840
841
842     if( p_httpt->i_file_count == 1 )
843     {
844         FREE( p_httpt->file );
845         p_httpt->i_file_count = 0;
846     }
847     else
848     {
849         int i_move;
850
851         i_move = p_httpt->i_file_count - i - 1;
852         if( i_move > 0  )
853         {
854             memmove( &p_httpt->file[i], &p_httpt->file[i + 1], sizeof( httpd_file_t *) * i_move );
855         }
856         p_httpt->i_file_count--;
857         p_httpt->file = realloc( p_httpt->file, sizeof( httpd_file_t *) * p_httpt->i_file_count );
858     }
859
860     vlc_mutex_unlock( &p_httpt->file_lock );
861 }
862 static void            UnregisterFile( httpd_t *p_httpd, httpd_file_t *p_file )
863 {
864     _UnregisterFile( p_httpd->p_sys, p_file );
865 }
866
867 static void             UnregisterStream( httpd_t *p_httpd, httpd_stream_t *p_stream )
868 {
869     _UnregisterFile( p_httpd->p_sys, p_stream );
870 }
871
872
873
874 static int             _SendStream( httpd_sys_t *p_httpt, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
875 {
876     int i_count;
877     int i_pos;
878
879     if( i_data <= 0 || p_data == NULL )
880     {
881         return( VLC_SUCCESS );
882     }
883     //fprintf( stderr, "## i_data=%d pos=%lld\n", i_data, p_stream->i_buffer_pos );
884
885     vlc_mutex_lock( &p_httpt->file_lock );
886
887     /* save this pointer (to be used by new connection) */
888     p_stream->i_buffer_last_pos = p_stream->i_buffer_pos;
889
890     i_pos = p_stream->i_buffer_pos % p_stream->i_buffer_size;
891     i_count = i_data;
892     while( i_count > 0)
893     {
894         int i_copy;
895
896         i_copy = __MIN( i_count, p_stream->i_buffer_size - i_pos );
897
898         memcpy( &p_stream->p_buffer[i_pos],
899                 p_data,
900                 i_copy );
901
902         i_pos = ( i_pos + i_copy ) % p_stream->i_buffer_size;
903         i_count -= i_copy;
904         p_data += i_copy;
905     }
906
907     p_stream->i_buffer_pos += i_data;
908     vlc_mutex_unlock( &p_httpt->file_lock );
909
910     return( VLC_SUCCESS );
911 }
912 static int             SendStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
913 {
914     return( _SendStream( p_httpd->p_sys, p_stream, p_data, i_data ) );
915 }
916
917 static int             HeaderStream( httpd_t *p_httpd, httpd_stream_t *p_stream, uint8_t *p_data, int i_data )
918 {
919     httpd_sys_t *p_httpt = p_httpd->p_sys;
920
921     vlc_mutex_lock( &p_httpt->file_lock );
922
923     FREE( p_stream->p_header );
924     if( p_data == NULL || i_data <= 0 )
925     {
926         p_stream->i_header_size = 0;
927     }
928     else
929     {
930         p_stream->i_header_size = i_data;
931         p_stream->p_header = malloc( i_data );
932         memcpy( p_stream->p_header,
933                 p_data,
934                 i_data );
935     }
936     vlc_mutex_unlock( &p_httpt->file_lock );
937
938     return( VLC_SUCCESS );
939 }
940
941 /****************************************************************************/
942 /****************************************************************************/
943 /****************************************************************************/
944 /****************************************************************************/
945 /****************************************************************************/
946
947 static int  httpd_page_401_get( httpd_file_callback_args_t *p_args,
948                                 uint8_t *p_request, int i_request,
949                                 uint8_t **pp_data, int *pi_data )
950 {
951     char *p;
952
953     p = *pp_data = malloc( 1024 );
954
955     p += sprintf( p, "<html>\n" );
956     p += sprintf( p, "<head>\n" );
957     p += sprintf( p, "<title>Error 401</title>\n" );
958     p += sprintf( p, "</head>\n" );
959     p += sprintf( p, "<body>\n" );
960     p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
961     p += sprintf( p, "<hr />\n" );
962     p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
963     p += sprintf( p, "</body>\n" );
964     p += sprintf( p, "</html>\n" );
965
966     *pi_data = strlen( *pp_data ) + 1;
967
968     return VLC_SUCCESS;
969 }
970 static int  httpd_page_404_get( httpd_file_callback_args_t *p_args,
971                                 uint8_t *p_request, int i_request,
972                                 uint8_t **pp_data, int *pi_data )
973 {
974     char *p;
975
976     p = *pp_data = malloc( 1024 );
977
978     p += sprintf( p, "<html>\n" );
979     p += sprintf( p, "<head>\n" );
980     p += sprintf( p, "<title>Error 404</title>\n" );
981     p += sprintf( p, "</head>\n" );
982     p += sprintf( p, "<body>\n" );
983     p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
984     p += sprintf( p, "<hr />\n" );
985     p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
986     p += sprintf( p, "</body>\n" );
987     p += sprintf( p, "</html>\n" );
988
989     *pi_data = strlen( *pp_data ) + 1;
990
991     return VLC_SUCCESS;
992 }
993
994
995 static int _httpd_page_admin_get_status( httpd_file_callback_args_t *p_args,
996                                          uint8_t **pp_data, int *pi_data )
997 {
998     httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
999     httpd_connection_t *p_con;
1000     httpd_banned_ip_t *p_ip;
1001         
1002     int i;
1003     char *p;
1004
1005     /* FIXME FIXME do not use static size FIXME FIXME*/
1006     p = *pp_data = malloc( 8096 );
1007
1008     p += sprintf( p, "<html>\n" );
1009     p += sprintf( p, "<head>\n" );
1010     p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1011     p += sprintf( p, "</head>\n" );
1012     p += sprintf( p, "<body>\n" );
1013     p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1014     p += sprintf( p, "<h2><center>Admin page</center></h2>\n" );
1015
1016     /* general */
1017     p += sprintf( p, "<h3>General state</h3>\n" );
1018     p += sprintf( p, "<ul>\n" );
1019     p += sprintf( p, "<li>Connection count: %d</li>\n", p_httpt->i_connection_count );
1020     //p += sprintf( p, "<li>Total bandwith: %d</li>\n", -1 );
1021     /*p += sprintf( p, "<li></li>\n" );*/
1022     p += sprintf( p, "<li>Ban count: %d</li>\n", p_httpt->i_banned_ip_count );
1023     p += sprintf( p, "</ul>\n" );
1024     
1025     /* ban list */
1026     /* XXX do not lock on ban_lock */
1027     p += sprintf( p, "<h3>Ban list</h3>\n" );
1028     p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1029     p += sprintf( p, "<tr>\n<th>IP</th>\n<th>Action</th></tr>\n" );
1030     for( p_ip = p_httpt->p_first_banned_ip;p_ip != NULL; p_ip = p_ip->p_next )
1031     {
1032         p += sprintf( p, "<tr>\n" );
1033         p += sprintf( p, "<td>%s</td>\n", p_ip->psz_ip );
1034         p += sprintf( p, "<td><form method=\"get\" action=\"\">"
1035                          "<select name=\"action\">"
1036                          "<option selected>unban_ip</option>"
1037                          "</select>"
1038                          "<input type=\"hidden\" name=\"id\" value=\"%s\"/>"                         
1039                          "<input type=\"submit\" value=\"Do it\" />"
1040                          "</form></td>\n", p_ip->psz_ip);        
1041         p += sprintf( p, "</tr>\n" );
1042     }
1043     p += sprintf( p, "</table>\n" );
1044
1045     
1046     
1047     /* host list */
1048     vlc_mutex_lock( &p_httpt->host_lock );
1049     p += sprintf( p, "<h3>Host list</h3>\n" );
1050     p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1051     p += sprintf( p, "<tr>\n<th>Host</th><th>Port</th><th>IP</th>\n</tr>\n" );
1052
1053     for( i = 0; i < p_httpt->i_host_count; i++ )
1054     {
1055         p += sprintf( p, "<tr>\n" );
1056         p += sprintf( p, "<td>%s</td>\n", p_httpt->host[i]->psz_host_addr );
1057         p += sprintf( p, "<td>%d</td>\n", p_httpt->host[i]->i_port );
1058         p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_httpt->host[i]->sock.sin_addr ) );
1059         p += sprintf( p, "</tr>\n" );
1060     }
1061     p += sprintf( p, "</table>\n" );
1062     vlc_mutex_unlock( &p_httpt->host_lock );
1063
1064     /* file list */
1065     /* XXX do not take lock on file_lock */
1066     p += sprintf( p, "<h3>File list</h3>\n" );
1067     p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1068     p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1069
1070     for( i = 0; i < p_httpt->i_file_count; i++ )
1071     {
1072         if( !p_httpt->file[i]->b_stream )
1073         {
1074             p += sprintf( p, "<tr>\n" );
1075             p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1076             p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1077             p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1078             p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1079             p += sprintf( p, "</tr>\n" );
1080         }
1081     }
1082     p += sprintf( p, "</table>\n" );
1083
1084     /* stream list */
1085     /* XXX do not take lock on file_lock */
1086     p += sprintf( p, "<h3>Stream list</h3>\n" );
1087     p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1088     p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
1089
1090     for( i = 0; i < p_httpt->i_file_count; i++ )
1091     {
1092         if( p_httpt->file[i]->b_stream )
1093         {
1094             p += sprintf( p, "<tr>\n" );
1095             p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
1096             p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
1097             p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
1098             p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
1099             p += sprintf( p, "</tr>\n" );
1100         }
1101     }
1102     p += sprintf( p, "</table>\n" );
1103
1104     /* connection list */
1105     /* XXX do not take lock on connection_lock */
1106     p += sprintf( p, "<h3>Connection list</h3>\n" );
1107     p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
1108     p += sprintf( p, "<tr>\n<th>IP</th><th>Requested File</th><th>Status</th><th>Action</th>\n</tr>\n" );
1109
1110     for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1111     {
1112         p += sprintf( p, "<tr>\n" );
1113         p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_con->sock.sin_addr ) );
1114         p += sprintf( p, "<td>%s</td>\n", p_con->psz_file );
1115         p += sprintf( p, "<td>%d</td>\n", p_con->i_http_error );
1116         p += sprintf( p, "<td><form method=\"get\" action=\"\">"
1117                          "<select name=\"action\">"
1118                          "<option selected>close_connection</option>"
1119                          "<option>ban_ip</option>"
1120                          "<option>close_connection_and_ban_ip</option>"
1121                          "</select>"
1122                          "<input type=\"hidden\" name=\"id\" value=\"%p\"/>"
1123                          "<input type=\"submit\" value=\"Do it\" />"
1124                               "</form></td>\n", p_con);
1125         p += sprintf( p, "</tr>\n" );
1126     }
1127     p += sprintf( p, "</table>\n" );
1128
1129
1130     /* www.videolan.org */
1131     p += sprintf( p, "<hr />\n" );
1132     p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1133     p += sprintf( p, "</body>\n" );
1134     p += sprintf( p, "</html>\n" );
1135
1136     *pi_data = strlen( *pp_data ) + 1;
1137
1138     return( VLC_SUCCESS );
1139 }
1140
1141 static int _httpd_page_admin_get_success( httpd_file_callback_args_t *p_args,
1142                                           uint8_t **pp_data, int *pi_data,
1143                                           char *psz_msg )
1144 {
1145     char *p;
1146
1147     p = *pp_data = malloc( 8096 );
1148
1149     p += sprintf( p, "<html>\n" );
1150     p += sprintf( p, "<head>\n" );
1151     p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1152     p += sprintf( p, "</head>\n" );
1153     p += sprintf( p, "<body>\n" );
1154     p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1155
1156     p += sprintf( p, "<p>Success=`%s'</p>", psz_msg );
1157     p += sprintf( p, "<a href=\"admin.html\">Retour a la page d'administration</a>\n" );
1158     
1159     p += sprintf( p, "<hr />\n" );
1160     p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1161     p += sprintf( p, "</body>\n" );
1162     p += sprintf( p, "</html>\n" );
1163
1164     *pi_data = strlen( *pp_data ) + 1;
1165
1166     return( VLC_SUCCESS );
1167 }
1168
1169 static int _httpd_page_admin_get_error( httpd_file_callback_args_t *p_args,
1170                                         uint8_t **pp_data, int *pi_data,
1171                                         char *psz_error )
1172 {
1173     char *p;
1174
1175     p = *pp_data = malloc( 8096 );
1176
1177     p += sprintf( p, "<html>\n" );
1178     p += sprintf( p, "<head>\n" );
1179     p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
1180     p += sprintf( p, "</head>\n" );
1181     p += sprintf( p, "<body>\n" );
1182     p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
1183
1184     p += sprintf( p, "<p>Error=`%s'</p>", psz_error );
1185     p += sprintf( p, "<a href=\"admin.html\">Retour a la page d'administration</a>\n" );
1186
1187     p += sprintf( p, "<hr />\n" );
1188     p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1189     p += sprintf( p, "</body>\n" );
1190     p += sprintf( p, "</html>\n" );
1191
1192     *pi_data = strlen( *pp_data ) + 1;
1193
1194     return( VLC_SUCCESS );
1195 }
1196
1197 static void _httpd_uri_extract_value( char *psz_uri, char *psz_name, char *psz_value, int i_value_max )
1198 {
1199     char *p;
1200
1201     p = strstr( psz_uri, psz_name );
1202     if( p )
1203     {
1204         int i_len;
1205
1206         p += strlen( psz_name );
1207         if( *p == '=' ) p++;
1208
1209         if( strchr( p, '&' ) )
1210         {
1211             i_len = strchr( p, '&' ) - p;
1212         }
1213         else
1214         {
1215             i_len = strlen( p );
1216         }
1217         i_len = __MIN( i_value_max - 1, i_len );
1218         if( i_len > 0 )
1219         {
1220             strncpy( psz_value, p, i_len );
1221             psz_value[i_len] = '\0';
1222         }
1223         else
1224         {
1225             strncpy( psz_value, "", i_value_max );
1226         }
1227     }
1228     else
1229     {
1230         strncpy( psz_value, "", i_value_max );
1231     }
1232 }
1233
1234
1235 static int  httpd_page_admin_get( httpd_file_callback_args_t *p_args,
1236                                   uint8_t *p_request, int i_request,
1237                                   uint8_t **pp_data, int *pi_data )
1238 {
1239     httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
1240     httpd_connection_t *p_con;
1241
1242     if( i_request > 0)
1243     {
1244         char action[512];
1245
1246         _httpd_uri_extract_value( p_request, "action", action, 512 );
1247
1248         if( !strcmp( action, "close_connection" ) )
1249         {
1250             char id[128];
1251             void *i_id;
1252
1253             _httpd_uri_extract_value( p_request, "id", id, 512 );
1254             i_id = (void*)strtol( id, NULL, 0 );
1255             msg_Dbg( p_httpt, "requested closing connection id=%s %p", id, i_id );
1256             for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1257             {
1258                 if( (void*)p_con == i_id )
1259                 {
1260                     /* XXX don't free p_con as it could be the one that it is sending ... */
1261                     p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
1262                     return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "connection closed" ) );
1263                 }
1264             }
1265             return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
1266         }
1267         else if( !strcmp( action, "ban_ip" ) )
1268         {
1269             char id[128];
1270             void *i_id;
1271             
1272             _httpd_uri_extract_value( p_request, "id", id, 512 );
1273             i_id = (void*)strtol( id, NULL, 0 );
1274             
1275             msg_Dbg( p_httpt, "requested banning ip id=%s %p", id, i_id );
1276             
1277             for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1278             {
1279                 if( (void*)p_con == i_id )
1280                 {
1281                     if( httpd_BanIP( p_httpt,inet_ntoa( p_con->sock.sin_addr ) ) == 0)
1282                         return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "IP banned" ) );
1283                     else
1284                         break;
1285                 }
1286             }
1287             
1288             return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1289         }
1290         else if( !strcmp( action, "unban_ip" ) )
1291         {
1292             char id[128];
1293             
1294             _httpd_uri_extract_value( p_request, "id", id, 512 );
1295             msg_Dbg( p_httpt, "requested unbanning ip %s", id);
1296             
1297             if( httpd_UnbanIP( p_httpt, httpd_GetbannedIP ( p_httpt, id ) ) == 0)
1298                 return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "IP Unbanned" ) );
1299             else
1300                 return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1301         }
1302         else if( !strcmp( action, "close_connection_and_ban_ip" ) )           
1303         {
1304             char id[128];
1305             void *i_id;
1306             
1307             _httpd_uri_extract_value( p_request, "id", id, 512 );
1308             i_id = (void*)strtol( id, NULL, 0 );
1309             msg_Dbg( p_httpt, "requested closing connection and banning ip id=%s %p", id, i_id );
1310             for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
1311             {
1312                 if( (void*)p_con == i_id )
1313                 {
1314                     /* XXX don't free p_con as it could be the one that it is sending ... */
1315                     p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
1316                     
1317                     if( httpd_BanIP( p_httpt,inet_ntoa( p_con->sock.sin_addr ) ) == 0)
1318                         return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "Connection closed and IP banned" ) );
1319                     else
1320                         break;
1321                 }
1322
1323             }
1324             return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
1325             
1326             
1327             return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1328         }       
1329         else
1330         {
1331             return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
1332         }
1333     }
1334     else
1335     {
1336         return( _httpd_page_admin_get_status( p_args, pp_data, pi_data ) );
1337     }
1338
1339     return VLC_SUCCESS;
1340 }
1341
1342 static int httpd_BanIP( httpd_sys_t *p_httpt, char * psz_new_banned_ip)
1343 {
1344     httpd_banned_ip_t *p_new_banned_ip ;
1345     
1346     p_new_banned_ip = malloc( sizeof( httpd_banned_ip_t ) );    
1347     if( !p_new_banned_ip )
1348     {
1349         return -1;
1350     }
1351     p_new_banned_ip->p_next=NULL;
1352     p_new_banned_ip->psz_ip = malloc( strlen( psz_new_banned_ip ) + 1 );
1353     if( !p_new_banned_ip->psz_ip )
1354     {
1355         return -2;
1356     }
1357     
1358     strcpy( p_new_banned_ip->psz_ip, psz_new_banned_ip );
1359     
1360     msg_Dbg( p_httpt, "Banning IP %s", psz_new_banned_ip );
1361     
1362     if( p_httpt->p_first_banned_ip )
1363     {
1364         httpd_banned_ip_t *p_last;
1365         
1366         p_last = p_httpt->p_first_banned_ip;
1367         while( p_last->p_next )
1368         {
1369             p_last = p_last->p_next;
1370         }
1371         
1372         p_last->p_next = p_new_banned_ip;
1373         p_new_banned_ip->p_prev = p_last;
1374     }
1375     else
1376     {
1377         p_new_banned_ip->p_prev = NULL;
1378         
1379         p_httpt->p_first_banned_ip = p_new_banned_ip;
1380     }
1381     
1382     p_httpt->i_banned_ip_count++;
1383     return 0;
1384 }
1385
1386 static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *p_httpt, char *psz_ip )
1387 {
1388     httpd_banned_ip_t *p_ip;
1389     
1390     p_ip = p_httpt->p_first_banned_ip;
1391     
1392     while( p_ip)
1393     {
1394         if( strcmp( psz_ip, p_ip->psz_ip ) == 0 )
1395         {
1396             return p_ip;
1397         }
1398         p_ip = p_ip->p_next;
1399     }
1400     
1401     return NULL;
1402 }
1403
1404 static int httpd_UnbanIP( httpd_sys_t *p_httpt, httpd_banned_ip_t *p_banned_ip )
1405 {
1406     if(!p_banned_ip)
1407     {
1408         return -1;
1409     }
1410     
1411     msg_Dbg( p_httpt, "Unbanning IP %s",p_banned_ip->psz_ip);
1412     
1413     /* first cut out from list */
1414     if( p_banned_ip->p_prev )
1415     {
1416         p_banned_ip->p_prev->p_next = p_banned_ip->p_next;
1417     }
1418     else
1419     {
1420         p_httpt->p_first_banned_ip = p_banned_ip->p_next;
1421     }
1422     
1423     if( p_banned_ip->p_next )
1424     {
1425         p_banned_ip->p_next->p_prev = p_banned_ip->p_prev;
1426     }
1427     
1428     FREE( p_banned_ip->psz_ip );
1429     FREE( p_banned_ip );
1430     
1431     p_httpt->i_banned_ip_count--;
1432     
1433     return 0;
1434     
1435 }
1436
1437 static void httpd_ConnnectionNew( httpd_sys_t *p_httpt, int fd, struct sockaddr_in *p_sock )
1438 {
1439     httpd_connection_t *p_con;
1440
1441     msg_Dbg( p_httpt, "new connection from %s", inet_ntoa( p_sock->sin_addr ) );
1442     
1443     /* verify if it's a banned ip */
1444     if(httpd_GetbannedIP( p_httpt,inet_ntoa( p_sock->sin_addr ) ) )
1445     {
1446         msg_Dbg( p_httpt, "Ip %s banned : closing connection", inet_ntoa( p_sock->sin_addr ) );
1447         close(fd);
1448         return;
1449     }
1450         
1451     /* create a new connection and link it */
1452     p_con = malloc( sizeof( httpd_connection_t ) );
1453     p_con->i_state  = HTTPD_CONNECTION_RECEIVING_REQUEST;
1454     p_con->fd       = fd;
1455     p_con->i_last_activity_date = mdate();
1456
1457     p_con->sock     = *p_sock;
1458     p_con->psz_file = NULL;
1459     p_con->i_http_error = 0;
1460     p_con->psz_user = NULL;
1461     p_con->psz_password = NULL;
1462     p_con->p_file   = NULL;
1463
1464     p_con->i_request_size = 0;
1465     p_con->p_request = NULL;
1466
1467     p_con->i_buffer = 0;
1468     p_con->i_buffer_size = 8096;
1469     p_con->p_buffer = malloc( p_con->i_buffer_size );
1470
1471     p_con->i_stream_pos = 0; // updated by httpd_thread */
1472     p_con->p_next = NULL;
1473
1474     if( p_httpt->p_first_connection )
1475     {
1476         httpd_connection_t *p_last;
1477
1478         p_last = p_httpt->p_first_connection;
1479         while( p_last->p_next )
1480         {
1481             p_last = p_last->p_next;
1482         }
1483
1484         p_last->p_next = p_con;
1485         p_con->p_prev = p_last;
1486     }
1487     else
1488     {
1489         p_con->p_prev = NULL;
1490
1491         p_httpt->p_first_connection = p_con;
1492     }
1493
1494     p_httpt->i_connection_count++;
1495 }
1496
1497 static void httpd_ConnnectionClose( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1498 {
1499     msg_Dbg( p_httpt, "close connection from %s", inet_ntoa( p_con->sock.sin_addr ) );
1500
1501     p_httpt->i_connection_count--;
1502     /* first cut out from list */
1503     if( p_con->p_prev )
1504     {
1505         p_con->p_prev->p_next = p_con->p_next;
1506     }
1507     else
1508     {
1509         p_httpt->p_first_connection = p_con->p_next;
1510     }
1511
1512     if( p_con->p_next )
1513     {
1514         p_con->p_next->p_prev = p_con->p_prev;
1515     }
1516
1517     if( p_con->p_file ) p_con->p_file->i_ref--;
1518     FREE( p_con->psz_file );
1519
1520     FREE( p_con->p_buffer );
1521     SOCKET_CLOSE( p_con->fd );
1522
1523     FREE( p_con->psz_user );
1524     FREE( p_con->psz_password );
1525
1526     FREE( p_con->p_request );
1527     free( p_con );
1528 }
1529
1530 static void httpd_RequestGetWord( char *word, int i_word_max, char **pp_buffer, char *p_end )
1531 {
1532     char *p = *pp_buffer;
1533     int i;
1534
1535     while( p < p_end && *p && ( *p == ' ' || *p == '\t' ) )
1536     {
1537         p++;
1538     }
1539
1540     i = 0;
1541     for( i = 0; i < i_word_max && p < p_end && *p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r'; i++,p++)
1542     {
1543         word[i] = *p;
1544     }
1545
1546     word[__MIN( i, i_word_max -1 )] = '\0';
1547
1548     *pp_buffer = p;
1549
1550 }
1551 static int httpd_RequestNextLine( char **pp_buffer, char *p_end )
1552 {
1553     char *p;
1554
1555     for( p = *pp_buffer; p < p_end; p++ )
1556     {
1557         if( p + 1 < p_end && *p == '\n' )
1558         {
1559             *pp_buffer = p + 1;
1560             return VLC_SUCCESS;
1561         }
1562         if( p + 2 < p_end && p[0] == '\r' && p[1] == '\n' )
1563         {
1564             *pp_buffer = p + 2;
1565             return VLC_SUCCESS;
1566         }
1567     }
1568     *pp_buffer = p_end;
1569     return VLC_EGENERIC;
1570 }
1571
1572 //char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1573 static void b64_decode( char *dest, char *src )
1574 {
1575     int  i_level;
1576     int  last = 0;
1577     int  b64[256] = {
1578         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
1579         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
1580         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
1581         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
1582         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
1583         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
1584         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
1585         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
1586         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
1587         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
1588         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
1589         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
1590         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
1591         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
1592         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
1593         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
1594         };
1595
1596     for( i_level = 0; *src != '\0'; src++ )
1597     {
1598         int  c;
1599
1600         c = b64[(unsigned int)*src];
1601         if( c == -1 )
1602         {
1603             src++;
1604             continue;
1605         }
1606
1607         switch( i_level )
1608         {
1609             case 0:
1610                 i_level++;
1611                 break;
1612             case 1:
1613                 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1614                 i_level++;
1615                 break;
1616             case 2:
1617                 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1618                 i_level++;
1619                 break;
1620             case 3:
1621                 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1622                 i_level = 0;
1623         }
1624         last = c;
1625     }
1626
1627     *dest = '\0';
1628 }
1629
1630 static void httpd_ConnectionParseRequest( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1631 {
1632     char *psz_status;
1633     char *p, *p_end;
1634
1635     int  i;
1636     char command[32];
1637     char url[1024];
1638     char version[32];
1639     char user[512] = "";
1640     char password[512] = "";
1641
1642     //msg_Dbg( p_httpt, "new request=\n%s", p_con->p_buffer );
1643
1644
1645     p = p_con->p_buffer;
1646     p_end = p + strlen( p ) + 1;
1647
1648     httpd_RequestGetWord( command, 32, &p, p_end );
1649     httpd_RequestGetWord( url, 1024, &p, p_end );
1650     httpd_RequestGetWord( version, 32, &p, p_end );
1651     //msg_Dbg( p_httpt, "ask =%s= =%s= =%s=", command, url, version );
1652
1653     p_con->p_request      = NULL;
1654     p_con->i_request_size = 0;
1655     if( !strcmp( command, "GET" ) )
1656     {
1657         p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1658     }
1659     else if( !strcmp( command, "POST" ))
1660     {
1661         p_con->i_method = HTTPD_CONNECTION_METHOD_POST;
1662     }
1663     else
1664     {
1665         /* unimplemented */
1666         p_con->psz_file = strdup( "/501.html" );
1667         p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1668         p_con->i_http_error = 501;
1669         goto create_header;
1670     }
1671
1672     if( strcmp( version, "HTTP/1.0" ) && strcmp( version, "HTTP/1.1" ) )
1673     {
1674         p_con->psz_file = strdup( "/505.html" );
1675         p_con->i_http_error = 505;
1676
1677         goto create_header;
1678     }
1679
1680     /* parse headers */
1681     for( ;; )
1682     {
1683         char header[1024];
1684
1685         if( httpd_RequestNextLine( &p, p_end ) )
1686         {
1687             //msg_Dbg( p_httpt, "failled new line" );
1688             break;;
1689         }
1690         //msg_Dbg( p_httpt, "new line=%s", p );
1691
1692         httpd_RequestGetWord( header, 1024, &p, p_end );
1693         if( !strcmp( header, "\r\n" ) || !strcmp( header, "\n" ) )
1694         {
1695             break;
1696         }
1697
1698         if( !strcmp( header, "Authorization:" ) )
1699         {
1700             char method[32];
1701
1702             httpd_RequestGetWord( method, 32, &p, p_end );
1703             if( !strcasecmp( method, "BASIC" ) )
1704             {
1705                 char basic[1024];
1706                 char decoded[1024];
1707
1708                 httpd_RequestGetWord( basic, 1024, &p, p_end );
1709                 //msg_Dbg( p_httpt, "Authorization: basic:%s", basic );
1710                 b64_decode( decoded, basic );
1711
1712                 //msg_Dbg( p_httpt, "Authorization: decoded:%s", decoded );
1713                 if( strchr( decoded, ':' ) )
1714                 {
1715                     char *p = strchr( decoded, ':' );
1716
1717                     p[0] = '\0'; p++;
1718                     strcpy( user, decoded );
1719                     strcpy( password, p );
1720                 }
1721             }
1722         }
1723     }
1724
1725     if( strchr( url, '?' ) )
1726     {
1727         char *p_request = strchr( url, '?' );
1728         *p_request++ = '\0';
1729         p_con->psz_file = strdup( url );
1730         p_con->p_request = strdup( p_request );
1731         p_con->i_request_size = strlen( p_con->p_request );
1732     }
1733     else
1734     {
1735         p_con->psz_file = strdup( url );
1736     }
1737
1738     /* fix p_request */
1739     if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1740     {
1741         char *p_request;
1742         if( strstr( p_con->p_buffer, "\r\n\r\n" ) )
1743         {
1744             p_request = strstr( p_con->p_buffer, "\r\n\r\n" ) + 4;
1745         }
1746         else if( strstr( p_con->p_buffer, "\n\n" ) )
1747         {
1748             p_request = strstr( p_con->p_buffer, "\n\n" ) + 2;
1749         }
1750         else
1751         {
1752             p_request = NULL;
1753         }
1754         if( p_request && p_request < p_end )
1755         {
1756             p_con->i_request_size = p_end - p_request;
1757             p_con->p_request = malloc( p_con->i_request_size + 1);
1758
1759             memcpy( p_con->p_request,
1760                     p_request,
1761                     p_con->i_request_size );
1762
1763             p_con->p_request[p_con->i_request_size] = '\0';
1764         }
1765     }
1766     p_con->i_http_error = 200;
1767
1768 create_header:
1769     //msg_Dbg( p_httpt, "ask %s %s %d", command, p_con->psz_file, p_con->i_http_error );
1770     FREE( p_con->p_buffer );
1771     p_con->i_buffer = 0;
1772     p_con->i_buffer_size = 0;
1773
1774     //vlc_mutex_lock( &p_httpt->file_lock );
1775 search_file:
1776     /* search file */
1777     p_con->p_file = NULL;
1778     for( i = 0; i < p_httpt->i_file_count; i++ )
1779     {
1780         if( !strcmp( p_httpt->file[i]->psz_file, p_con->psz_file ) )
1781         {
1782             if( p_httpt->file[i]->b_stream ||
1783                 ( p_con->i_method == HTTPD_CONNECTION_METHOD_GET  && p_httpt->file[i]->pf_get ) ||
1784                 ( p_con->i_method == HTTPD_CONNECTION_METHOD_POST && p_httpt->file[i]->pf_post ) )
1785             {
1786                 p_con->p_file = p_httpt->file[i];
1787                 break;
1788             }
1789         }
1790     }
1791
1792     if( !p_con->p_file )
1793     {
1794         p_con->psz_file = strdup( "/404.html" );
1795         p_con->i_http_error = 404;
1796
1797         /* XXX be sure that "/404.html" exist else ... */
1798         goto search_file;
1799     }
1800
1801     if( p_con->p_file->i_authenticate_method == HTTPD_AUTHENTICATE_BASIC )
1802     {
1803         if( strcmp( user, p_con->p_file->psz_user ) || strcmp( password, p_con->p_file->psz_password ) )
1804         {
1805             p_con->psz_file = strdup( "/401.html" );
1806             strcpy( user, p_con->p_file->psz_user );
1807             p_con->i_http_error = 401;
1808
1809             /* XXX do not put password on 404 else ... */
1810             goto search_file;
1811         }
1812     }
1813
1814     p_con->p_file->i_ref++;
1815 //    vlc_mutex_unlock( &p_httpt->file_lock );
1816
1817     switch( p_con->i_http_error )
1818     {
1819         case 200:
1820             psz_status = "OK";
1821             break;
1822
1823         case 401:
1824             psz_status = "Authorization Required";
1825             break;
1826         default:
1827             psz_status = "Unknown";
1828             break;
1829     }
1830
1831     p_con->i_state = HTTPD_CONNECTION_SENDING_HEADER;
1832
1833     p_con->i_buffer_size = 4096;  
1834     p_con->i_buffer = 0;
1835     
1836     /* we send stream header with this one */
1837     if( p_con->i_http_error == 200 && p_con->p_file->b_stream )
1838     {
1839         p_con->i_buffer_size += p_con->p_file->i_header_size;
1840     }
1841
1842     p = p_con->p_buffer = malloc( p_con->i_buffer_size );
1843
1844     p += sprintf( p, "HTTP/1.0 %d %s\r\n", p_con->i_http_error, psz_status );
1845     p += sprintf( p, "Content-type: %s\r\n", p_con->p_file->psz_mime );
1846     if( p_con->i_http_error == 401 )
1847     {
1848         p += sprintf( p, "WWW-Authenticate: Basic realm=\"%s\"\r\n", user );
1849     }
1850     p += sprintf( p, "Cache-Control: no-cache\r\n" );
1851     p += sprintf( p, "\r\n" );
1852
1853     p_con->i_buffer_size = strlen( p_con->p_buffer );// + 1;
1854
1855     if( p_con->i_http_error == 200 && p_con->p_file->b_stream && p_con->p_file->i_header_size > 0 )
1856     {
1857         /* add stream header */
1858         memcpy( &p_con->p_buffer[p_con->i_buffer_size],
1859                 p_con->p_file->p_header,
1860                 p_con->p_file->i_header_size );
1861         p_con->i_buffer_size += p_con->p_file->i_header_size;
1862     }
1863
1864     //msg_Dbg( p_httpt, "answer=\n%s", p_con->p_buffer );
1865 }
1866 #define HTTPD_STREAM_PACKET 10000
1867 static void httpd_Thread( httpd_sys_t *p_httpt )
1868 {
1869     httpd_file_t    *p_page_admin;
1870     httpd_file_t    *p_page_401;
1871     httpd_file_t    *p_page_404;
1872
1873     httpd_connection_t *p_con;
1874
1875     msg_Info( p_httpt, "httpd started" );
1876
1877     p_page_401 = _RegisterFile( p_httpt,
1878                                 "/401.html", "text/html",
1879                                 NULL, NULL,
1880                                 httpd_page_401_get,
1881                                 NULL,
1882                                 (httpd_file_callback_args_t*)NULL );
1883     p_page_404 = _RegisterFile( p_httpt,
1884                                 "/404.html", "text/html",
1885                                 NULL, NULL,
1886                                 httpd_page_404_get,
1887                                 NULL,
1888                                 (httpd_file_callback_args_t*)NULL );
1889     p_page_admin = _RegisterFile( p_httpt,
1890                                   "/admin.html", "text/html",
1891                                   "admin", "salut",
1892                                   httpd_page_admin_get,
1893                                   NULL,
1894                                   (httpd_file_callback_args_t*)p_httpt );
1895
1896     while( !p_httpt->b_die )
1897     {
1898         struct timeval  timeout;
1899         fd_set          fds_read;
1900         fd_set          fds_write;
1901         int             i_handle_max = 0;
1902         int             i_ret;
1903         int i;
1904         if( p_httpt->i_host_count <= 0 )
1905         {
1906             msleep( 100 * 1000 );
1907             continue;
1908         }
1909
1910         /* we will create a socket set with host and connection */
1911         FD_ZERO( &fds_read );
1912         FD_ZERO( &fds_write );
1913
1914         vlc_mutex_lock( &p_httpt->host_lock );
1915         vlc_mutex_lock( &p_httpt->connection_lock );
1916         for( i = 0; i < p_httpt->i_host_count; i++ )
1917         {
1918             FD_SET( p_httpt->host[i]->fd, &fds_read );
1919             i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd );
1920         }
1921         for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1922         {
1923             /* no more than 10s of inactivity */
1924             if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ||
1925                 p_con->i_state == HTTPD_CONNECTION_TO_BE_CLOSED)
1926             {
1927                 httpd_connection_t *p_next = p_con->p_next;
1928
1929                 msg_Dbg( p_httpt,  "close unused connection" );
1930                 httpd_ConnnectionClose( p_httpt, p_con );
1931                 p_con = p_next;
1932                 continue;
1933             }
1934
1935             if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos )
1936             {
1937                 p_con = p_con->p_next;
1938                 continue;
1939             }
1940
1941             if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1942             {
1943                 FD_SET( p_con->fd, &fds_read );
1944             }
1945             else
1946             {
1947                 FD_SET( p_con->fd, &fds_write );
1948             }
1949             i_handle_max = __MAX( i_handle_max, p_con->fd );
1950
1951             p_con = p_con->p_next;
1952         }
1953         vlc_mutex_unlock( &p_httpt->host_lock );
1954         vlc_mutex_unlock( &p_httpt->connection_lock );
1955
1956         /* we will wait 0.5s */
1957         timeout.tv_sec = 0;
1958         timeout.tv_usec = 500*1000;
1959
1960         i_ret = select( i_handle_max + 1, 
1961                         &fds_read,
1962                         &fds_write,
1963                         NULL,
1964                         &timeout );
1965         if( i_ret == -1 && errno != EINTR )
1966         {
1967             msg_Warn( p_httpt, "cannot select sockets" );
1968             msleep( 1000 );
1969             continue;
1970         }
1971         if( i_ret <= 0 )
1972         {
1973 //            msg_Dbg( p_httpt, "waiting..." );
1974             continue;
1975         }
1976
1977         vlc_mutex_lock( &p_httpt->host_lock );
1978         /* accept/refuse new connection */
1979         for( i = 0; i < p_httpt->i_host_count; i++ )
1980         {
1981             int     i_sock_size = sizeof( struct sockaddr_in );
1982             struct  sockaddr_in sock;
1983             int     fd;
1984
1985             fd = accept( p_httpt->host[i]->fd, (struct sockaddr *)&sock,
1986                          &i_sock_size );
1987             if( fd > 0 )
1988             {
1989 #if defined( WIN32 ) || defined( UNDER_CE )
1990                 {
1991                     unsigned long i_dummy = 1;
1992                     ioctlsocket( fd, FIONBIO, &i_dummy );
1993                 }
1994 #else
1995                 fcntl( fd, F_SETFL, O_NONBLOCK );
1996 #endif
1997
1998                 if( p_httpt->i_connection_count >= HTTPD_MAX_CONNECTION )
1999                 {
2000                     msg_Warn( p_httpt, "max connection reached" );
2001                     SOCKET_CLOSE( fd );
2002                     continue;
2003                 }
2004                 /* create a new connection and link it */
2005                 httpd_ConnnectionNew( p_httpt, fd, &sock );
2006
2007             }
2008         }
2009         vlc_mutex_unlock( &p_httpt->host_lock );
2010
2011         vlc_mutex_lock( &p_httpt->file_lock );
2012         /* now do work for all connections */
2013         for( p_con = p_httpt->p_first_connection; p_con != NULL; )
2014         {
2015             if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
2016             {
2017                 int i_len;
2018                 /* read data */
2019                 i_len = recv( p_con->fd,
2020                               p_con->p_buffer + p_con->i_buffer,
2021                               p_con->i_buffer_size - p_con->i_buffer, 0 );
2022
2023
2024 #if defined( WIN32 ) || defined( UNDER_CE )
2025                 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) ||
2026 #else
2027                 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) ||
2028 #endif
2029                     ( i_len == 0 ) )
2030                 {
2031                     httpd_connection_t *p_next = p_con->p_next;
2032
2033                     httpd_ConnnectionClose( p_httpt, p_con );
2034                     p_con = p_next;
2035                 }
2036                 else if( i_len > 0 )
2037                 {
2038                     uint8_t *ptr;
2039                     p_con->i_last_activity_date = mdate();
2040                     p_con->i_buffer += i_len;
2041
2042                     ptr = p_con->p_buffer + p_con->i_buffer;
2043
2044                     if( ( p_con->i_buffer >= 2 && !strncmp( ptr - 2, "\n\n", 2 ) )||
2045                         ( p_con->i_buffer >= 4 && !strncmp( ptr - 4, "\r\n\r\n", 4 ) ) ||
2046                         p_con->i_buffer >= p_con->i_buffer_size )
2047                     {
2048                         p_con->p_buffer[__MIN( p_con->i_buffer, p_con->i_buffer_size - 1 )] = '\0';
2049                         httpd_ConnectionParseRequest( p_httpt, p_con );
2050                     }
2051
2052                     p_con = p_con->p_next;
2053                 }
2054                 else
2055                 {
2056                     p_con = p_con->p_next;
2057                 }
2058                 continue;   /* just for clarity */
2059             }
2060             else if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER || p_con->i_state == HTTPD_CONNECTION_SENDING_FILE )
2061             {
2062                 int i_len;
2063
2064                 /* write data */
2065                 if( p_con->i_buffer_size - p_con->i_buffer > 0 )
2066                 {
2067                     i_len = send( p_con->fd, p_con->p_buffer + p_con->i_buffer, p_con->i_buffer_size - p_con->i_buffer, 0 );
2068                 }
2069                 else
2070                 {
2071                     i_len = 0;
2072                 }
2073 //                msg_Warn( p_httpt, "on %d send %d bytes %s", p_con->i_buffer_size, i_len, p_con->p_buffer + p_con->i_buffer );
2074
2075 #if defined( WIN32 ) || defined( UNDER_CE )
2076                 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) ||
2077 #else
2078                 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) ||
2079 #endif
2080                     ( i_len == 0 ) )
2081                 {
2082                     httpd_connection_t *p_next = p_con->p_next;
2083
2084                     httpd_ConnnectionClose( p_httpt, p_con );
2085                     p_con = p_next;
2086                 }
2087                 else if( i_len > 0 )
2088                 {
2089                     p_con->i_last_activity_date = mdate();
2090                     p_con->i_buffer += i_len;
2091
2092                     if( p_con->i_buffer >= p_con->i_buffer_size )
2093                     {
2094                         if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER )
2095                         {
2096                             p_con->i_buffer_size = 0;
2097                             p_con->i_buffer = 0;
2098                             FREE( p_con->p_buffer );
2099
2100                             if( !p_con->p_file->b_stream )
2101                             {
2102                                 p_con->i_state = HTTPD_CONNECTION_SENDING_FILE; // be sure to out from HTTPD_CONNECTION_SENDING_HEADER
2103                                 if( p_con->i_method == HTTPD_CONNECTION_METHOD_GET )
2104                                 {
2105                                     p_con->p_file->pf_get( p_con->p_file->p_sys,
2106                                                            p_con->p_request, p_con->i_request_size,
2107                                                            &p_con->p_buffer, &p_con->i_buffer_size );
2108                                 }
2109                                 else if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
2110                                 {
2111                                     p_con->p_file->pf_post( p_con->p_file->p_sys,
2112                                                             p_con->p_request, p_con->i_request_size,
2113                                                             &p_con->p_buffer, &p_con->i_buffer_size );
2114                                 }
2115                                 else
2116                                 {
2117                                     p_con->p_buffer = NULL;
2118                                     p_con->i_buffer_size = 0;
2119                                 }
2120                             }
2121                             else
2122                             {
2123                                 p_con->i_state = HTTPD_CONNECTION_SENDING_STREAM;
2124                                 p_con->i_stream_pos = p_con->p_file->i_buffer_last_pos;
2125                             }
2126                             p_con = p_con->p_next;
2127                         }
2128                         else
2129                         {
2130                             httpd_connection_t *p_next = p_con->p_next;
2131
2132                             httpd_ConnnectionClose( p_httpt, p_con );
2133                             p_con = p_next;
2134                         }
2135                     }
2136                     else
2137                     {
2138                         p_con = p_con->p_next;
2139                     }
2140                 }
2141                 else
2142                 {
2143                     p_con = p_con->p_next;
2144                 }
2145                 continue;   /* just for clarity */
2146             }
2147             else if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM )
2148             {
2149                 httpd_stream_t *p_stream = p_con->p_file;
2150                 int i_send;
2151                 int i_write;
2152
2153                 if( p_con->i_stream_pos < p_stream->i_buffer_pos )
2154                 {
2155                     int i_pos;
2156                     /* check if this p_con aren't to late */
2157                     if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
2158                     {
2159                         fprintf(stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n", p_con->i_stream_pos, p_stream->i_buffer_pos  );
2160                         p_con->i_stream_pos = p_stream->i_buffer_last_pos;
2161                     }
2162
2163                     i_pos = p_con->i_stream_pos % p_stream->i_buffer_size;
2164                     /* size until end of buffer */
2165                     i_write = p_stream->i_buffer_size - i_pos;
2166                     /* is it more than valid data */
2167                     if( i_write >= p_stream->i_buffer_pos - p_con->i_stream_pos )
2168                     {
2169                         i_write = p_stream->i_buffer_pos - p_con->i_stream_pos;
2170                     }
2171                     /* limit to HTTPD_STREAM_PACKET */
2172                     if( i_write > HTTPD_STREAM_PACKET )
2173                     {
2174                         i_write = HTTPD_STREAM_PACKET;
2175                     }
2176                     i_send = send( p_con->fd, &p_stream->p_buffer[i_pos], i_write, 0 );
2177
2178 #if defined( WIN32 ) || defined( UNDER_CE )
2179                     if( ( i_send < 0 && WSAGetLastError() != WSAEWOULDBLOCK )||
2180 #else
2181                     if( ( i_send < 0 && errno != EAGAIN && errno != EINTR )||
2182 #endif
2183                         ( i_send == 0 ) )
2184                     {
2185                         httpd_connection_t *p_next = p_con->p_next;
2186
2187                         httpd_ConnnectionClose( p_httpt, p_con );
2188                         p_con = p_next;
2189                         continue;
2190                     }
2191                     else if( i_send > 0 )
2192                     {
2193                         msg_Dbg( p_httpt, "Sending %d bytes",i_send );
2194                         p_con->i_last_activity_date = mdate();
2195                         p_con->i_stream_pos += i_send;
2196                     }
2197                 }
2198                 p_con = p_con->p_next;
2199                 continue;   /* just for clarity */
2200             }
2201             else if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
2202             {
2203                 msg_Warn( p_httpt, "cannot occur (Invalid p_con->i_state)" );
2204                 p_con = p_con->p_next;
2205             }
2206         }   /* for over connection */
2207
2208         vlc_mutex_unlock( &p_httpt->file_lock );
2209     }
2210     msg_Info( p_httpt, "httpd stopped" );
2211
2212     _UnregisterFile( p_httpt, p_page_401 );
2213     _UnregisterFile( p_httpt, p_page_404 );
2214     _UnregisterFile( p_httpt, p_page_admin );
2215 }