* http: removed a too verbose message, make vim happier.
[vlc.git] / modules / misc / httpd.c
1 /*****************************************************************************
2  * httpd.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: httpd.c,v 1.20 2003/07/01 21:32:32 fenrir 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 static void httpd_ConnnectionNew( httpd_sys_t *p_httpt, int fd, struct sockaddr_in *p_sock )
1437 {
1438     httpd_connection_t *p_con;
1439
1440     msg_Dbg( p_httpt, "new connection from %s", inet_ntoa( p_sock->sin_addr ) );
1441
1442     /* verify if it's a banned ip */
1443     if(httpd_GetbannedIP( p_httpt,inet_ntoa( p_sock->sin_addr ) ) )
1444     {
1445         msg_Dbg( p_httpt, "Ip %s banned : closing connection", inet_ntoa( p_sock->sin_addr ) );
1446         close(fd);
1447         return;
1448     }
1449
1450     /* create a new connection and link it */
1451     p_con = malloc( sizeof( httpd_connection_t ) );
1452     p_con->i_state  = HTTPD_CONNECTION_RECEIVING_REQUEST;
1453     p_con->fd       = fd;
1454     p_con->i_last_activity_date = mdate();
1455
1456     p_con->sock     = *p_sock;
1457     p_con->psz_file = NULL;
1458     p_con->i_http_error = 0;
1459     p_con->psz_user = NULL;
1460     p_con->psz_password = NULL;
1461     p_con->p_file   = NULL;
1462
1463     p_con->i_request_size = 0;
1464     p_con->p_request = NULL;
1465
1466     p_con->i_buffer = 0;
1467     p_con->i_buffer_size = 8096;
1468     p_con->p_buffer = malloc( p_con->i_buffer_size );
1469
1470     p_con->i_stream_pos = 0; // updated by httpd_thread */
1471     p_con->p_next = NULL;
1472
1473     if( p_httpt->p_first_connection )
1474     {
1475         httpd_connection_t *p_last;
1476
1477         p_last = p_httpt->p_first_connection;
1478         while( p_last->p_next )
1479         {
1480             p_last = p_last->p_next;
1481         }
1482
1483         p_last->p_next = p_con;
1484         p_con->p_prev = p_last;
1485     }
1486     else
1487     {
1488         p_con->p_prev = NULL;
1489
1490         p_httpt->p_first_connection = p_con;
1491     }
1492
1493     p_httpt->i_connection_count++;
1494 }
1495
1496 static void httpd_ConnnectionClose( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1497 {
1498     msg_Dbg( p_httpt, "close connection from %s", inet_ntoa( p_con->sock.sin_addr ) );
1499
1500     p_httpt->i_connection_count--;
1501     /* first cut out from list */
1502     if( p_con->p_prev )
1503     {
1504         p_con->p_prev->p_next = p_con->p_next;
1505     }
1506     else
1507     {
1508         p_httpt->p_first_connection = p_con->p_next;
1509     }
1510
1511     if( p_con->p_next )
1512     {
1513         p_con->p_next->p_prev = p_con->p_prev;
1514     }
1515
1516     if( p_con->p_file ) p_con->p_file->i_ref--;
1517     FREE( p_con->psz_file );
1518
1519     FREE( p_con->p_buffer );
1520     SOCKET_CLOSE( p_con->fd );
1521
1522     FREE( p_con->psz_user );
1523     FREE( p_con->psz_password );
1524
1525     FREE( p_con->p_request );
1526     free( p_con );
1527 }
1528
1529 static void httpd_RequestGetWord( char *word, int i_word_max, char **pp_buffer, char *p_end )
1530 {
1531     char *p = *pp_buffer;
1532     int i;
1533
1534     while( p < p_end && *p && ( *p == ' ' || *p == '\t' ) )
1535     {
1536         p++;
1537     }
1538
1539     i = 0;
1540     for( i = 0; i < i_word_max && p < p_end && *p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r'; i++,p++)
1541     {
1542         word[i] = *p;
1543     }
1544
1545     word[__MIN( i, i_word_max -1 )] = '\0';
1546
1547     *pp_buffer = p;
1548
1549 }
1550 static int httpd_RequestNextLine( char **pp_buffer, char *p_end )
1551 {
1552     char *p;
1553
1554     for( p = *pp_buffer; p < p_end; p++ )
1555     {
1556         if( p + 1 < p_end && *p == '\n' )
1557         {
1558             *pp_buffer = p + 1;
1559             return VLC_SUCCESS;
1560         }
1561         if( p + 2 < p_end && p[0] == '\r' && p[1] == '\n' )
1562         {
1563             *pp_buffer = p + 2;
1564             return VLC_SUCCESS;
1565         }
1566     }
1567     *pp_buffer = p_end;
1568     return VLC_EGENERIC;
1569 }
1570
1571 //char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1572 static void b64_decode( char *dest, char *src )
1573 {
1574     int  i_level;
1575     int  last = 0;
1576     int  b64[256] = {
1577         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
1578         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
1579         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
1580         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
1581         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
1582         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
1583         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
1584         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
1585         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
1586         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
1587         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
1588         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
1589         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
1590         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
1591         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
1592         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
1593         };
1594
1595     for( i_level = 0; *src != '\0'; src++ )
1596     {
1597         int  c;
1598
1599         c = b64[(unsigned int)*src];
1600         if( c == -1 )
1601         {
1602             src++;
1603             continue;
1604         }
1605
1606         switch( i_level )
1607         {
1608             case 0:
1609                 i_level++;
1610                 break;
1611             case 1:
1612                 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1613                 i_level++;
1614                 break;
1615             case 2:
1616                 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1617                 i_level++;
1618                 break;
1619             case 3:
1620                 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1621                 i_level = 0;
1622         }
1623         last = c;
1624     }
1625
1626     *dest = '\0';
1627 }
1628
1629 static void httpd_ConnectionParseRequest( httpd_sys_t *p_httpt, httpd_connection_t *p_con )
1630 {
1631     char *psz_status;
1632     char *p, *p_end;
1633
1634     int  i;
1635     char command[32];
1636     char url[1024];
1637     char version[32];
1638     char user[512] = "";
1639     char password[512] = "";
1640
1641     //msg_Dbg( p_httpt, "new request=\n%s", p_con->p_buffer );
1642
1643
1644     p = p_con->p_buffer;
1645     p_end = p + strlen( p ) + 1;
1646
1647     httpd_RequestGetWord( command, 32, &p, p_end );
1648     httpd_RequestGetWord( url, 1024, &p, p_end );
1649     httpd_RequestGetWord( version, 32, &p, p_end );
1650     //msg_Dbg( p_httpt, "ask =%s= =%s= =%s=", command, url, version );
1651
1652     p_con->p_request      = NULL;
1653     p_con->i_request_size = 0;
1654     if( !strcmp( command, "GET" ) )
1655     {
1656         p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1657     }
1658     else if( !strcmp( command, "POST" ))
1659     {
1660         p_con->i_method = HTTPD_CONNECTION_METHOD_POST;
1661     }
1662     else
1663     {
1664         /* unimplemented */
1665         p_con->psz_file = strdup( "/501.html" );
1666         p_con->i_method = HTTPD_CONNECTION_METHOD_GET;
1667         p_con->i_http_error = 501;
1668         goto create_header;
1669     }
1670
1671     if( strcmp( version, "HTTP/1.0" ) && strcmp( version, "HTTP/1.1" ) )
1672     {
1673         p_con->psz_file = strdup( "/505.html" );
1674         p_con->i_http_error = 505;
1675
1676         goto create_header;
1677     }
1678
1679     /* parse headers */
1680     for( ;; )
1681     {
1682         char header[1024];
1683
1684         if( httpd_RequestNextLine( &p, p_end ) )
1685         {
1686             //msg_Dbg( p_httpt, "failled new line" );
1687             break;;
1688         }
1689         //msg_Dbg( p_httpt, "new line=%s", p );
1690
1691         httpd_RequestGetWord( header, 1024, &p, p_end );
1692         if( !strcmp( header, "\r\n" ) || !strcmp( header, "\n" ) )
1693         {
1694             break;
1695         }
1696
1697         if( !strcmp( header, "Authorization:" ) )
1698         {
1699             char method[32];
1700
1701             httpd_RequestGetWord( method, 32, &p, p_end );
1702             if( !strcasecmp( method, "BASIC" ) )
1703             {
1704                 char basic[1024];
1705                 char decoded[1024];
1706
1707                 httpd_RequestGetWord( basic, 1024, &p, p_end );
1708                 //msg_Dbg( p_httpt, "Authorization: basic:%s", basic );
1709                 b64_decode( decoded, basic );
1710
1711                 //msg_Dbg( p_httpt, "Authorization: decoded:%s", decoded );
1712                 if( strchr( decoded, ':' ) )
1713                 {
1714                     char *p = strchr( decoded, ':' );
1715
1716                     p[0] = '\0'; p++;
1717                     strcpy( user, decoded );
1718                     strcpy( password, p );
1719                 }
1720             }
1721         }
1722     }
1723
1724     if( strchr( url, '?' ) )
1725     {
1726         char *p_request = strchr( url, '?' );
1727         *p_request++ = '\0';
1728         p_con->psz_file = strdup( url );
1729         p_con->p_request = strdup( p_request );
1730         p_con->i_request_size = strlen( p_con->p_request );
1731     }
1732     else
1733     {
1734         p_con->psz_file = strdup( url );
1735     }
1736
1737     /* fix p_request */
1738     if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
1739     {
1740         char *p_request;
1741         if( strstr( p_con->p_buffer, "\r\n\r\n" ) )
1742         {
1743             p_request = strstr( p_con->p_buffer, "\r\n\r\n" ) + 4;
1744         }
1745         else if( strstr( p_con->p_buffer, "\n\n" ) )
1746         {
1747             p_request = strstr( p_con->p_buffer, "\n\n" ) + 2;
1748         }
1749         else
1750         {
1751             p_request = NULL;
1752         }
1753         if( p_request && p_request < p_end )
1754         {
1755             p_con->i_request_size = p_end - p_request;
1756             p_con->p_request = malloc( p_con->i_request_size + 1);
1757
1758             memcpy( p_con->p_request,
1759                     p_request,
1760                     p_con->i_request_size );
1761
1762             p_con->p_request[p_con->i_request_size] = '\0';
1763         }
1764     }
1765     p_con->i_http_error = 200;
1766
1767 create_header:
1768     //msg_Dbg( p_httpt, "ask %s %s %d", command, p_con->psz_file, p_con->i_http_error );
1769     FREE( p_con->p_buffer );
1770     p_con->i_buffer = 0;
1771     p_con->i_buffer_size = 0;
1772
1773     //vlc_mutex_lock( &p_httpt->file_lock );
1774 search_file:
1775     /* search file */
1776     p_con->p_file = NULL;
1777     for( i = 0; i < p_httpt->i_file_count; i++ )
1778     {
1779         if( !strcmp( p_httpt->file[i]->psz_file, p_con->psz_file ) )
1780         {
1781             if( p_httpt->file[i]->b_stream ||
1782                 ( p_con->i_method == HTTPD_CONNECTION_METHOD_GET  && p_httpt->file[i]->pf_get ) ||
1783                 ( p_con->i_method == HTTPD_CONNECTION_METHOD_POST && p_httpt->file[i]->pf_post ) )
1784             {
1785                 p_con->p_file = p_httpt->file[i];
1786                 break;
1787             }
1788         }
1789     }
1790
1791     if( !p_con->p_file )
1792     {
1793         p_con->psz_file = strdup( "/404.html" );
1794         p_con->i_http_error = 404;
1795
1796         /* XXX be sure that "/404.html" exist else ... */
1797         goto search_file;
1798     }
1799
1800     if( p_con->p_file->i_authenticate_method == HTTPD_AUTHENTICATE_BASIC )
1801     {
1802         if( strcmp( user, p_con->p_file->psz_user ) || strcmp( password, p_con->p_file->psz_password ) )
1803         {
1804             p_con->psz_file = strdup( "/401.html" );
1805             strcpy( user, p_con->p_file->psz_user );
1806             p_con->i_http_error = 401;
1807
1808             /* XXX do not put password on 404 else ... */
1809             goto search_file;
1810         }
1811     }
1812
1813     p_con->p_file->i_ref++;
1814 //    vlc_mutex_unlock( &p_httpt->file_lock );
1815
1816     switch( p_con->i_http_error )
1817     {
1818         case 200:
1819             psz_status = "OK";
1820             break;
1821
1822         case 401:
1823             psz_status = "Authorization Required";
1824             break;
1825         default:
1826             psz_status = "Unknown";
1827             break;
1828     }
1829
1830     p_con->i_state = HTTPD_CONNECTION_SENDING_HEADER;
1831
1832     p_con->i_buffer_size = 4096;
1833     p_con->i_buffer = 0;
1834
1835     /* we send stream header with this one */
1836     if( p_con->i_http_error == 200 && p_con->p_file->b_stream )
1837     {
1838         p_con->i_buffer_size += p_con->p_file->i_header_size;
1839     }
1840
1841     p = p_con->p_buffer = malloc( p_con->i_buffer_size );
1842
1843     p += sprintf( p, "HTTP/1.0 %d %s\r\n", p_con->i_http_error, psz_status );
1844     p += sprintf( p, "Content-type: %s\r\n", p_con->p_file->psz_mime );
1845     if( p_con->i_http_error == 401 )
1846     {
1847         p += sprintf( p, "WWW-Authenticate: Basic realm=\"%s\"\r\n", user );
1848     }
1849     p += sprintf( p, "Cache-Control: no-cache\r\n" );
1850     p += sprintf( p, "\r\n" );
1851
1852     p_con->i_buffer_size = strlen( p_con->p_buffer );// + 1;
1853
1854     if( p_con->i_http_error == 200 && p_con->p_file->b_stream && p_con->p_file->i_header_size > 0 )
1855     {
1856         /* add stream header */
1857         memcpy( &p_con->p_buffer[p_con->i_buffer_size],
1858                 p_con->p_file->p_header,
1859                 p_con->p_file->i_header_size );
1860         p_con->i_buffer_size += p_con->p_file->i_header_size;
1861     }
1862
1863     //msg_Dbg( p_httpt, "answer=\n%s", p_con->p_buffer );
1864 }
1865 #define HTTPD_STREAM_PACKET 10000
1866 static void httpd_Thread( httpd_sys_t *p_httpt )
1867 {
1868     httpd_file_t    *p_page_admin;
1869     httpd_file_t    *p_page_401;
1870     httpd_file_t    *p_page_404;
1871
1872     httpd_connection_t *p_con;
1873
1874     msg_Info( p_httpt, "httpd started" );
1875
1876     p_page_401 = _RegisterFile( p_httpt,
1877                                 "/401.html", "text/html",
1878                                 NULL, NULL,
1879                                 httpd_page_401_get,
1880                                 NULL,
1881                                 (httpd_file_callback_args_t*)NULL );
1882     p_page_404 = _RegisterFile( p_httpt,
1883                                 "/404.html", "text/html",
1884                                 NULL, NULL,
1885                                 httpd_page_404_get,
1886                                 NULL,
1887                                 (httpd_file_callback_args_t*)NULL );
1888     p_page_admin = _RegisterFile( p_httpt,
1889                                   "/admin.html", "text/html",
1890                                   "admin", "salut",
1891                                   httpd_page_admin_get,
1892                                   NULL,
1893                                   (httpd_file_callback_args_t*)p_httpt );
1894
1895     while( !p_httpt->b_die )
1896     {
1897         struct timeval  timeout;
1898         fd_set          fds_read;
1899         fd_set          fds_write;
1900         int             i_handle_max = 0;
1901         int             i_ret;
1902         int i;
1903         if( p_httpt->i_host_count <= 0 )
1904         {
1905             msleep( 100 * 1000 );
1906             continue;
1907         }
1908
1909         /* we will create a socket set with host and connection */
1910         FD_ZERO( &fds_read );
1911         FD_ZERO( &fds_write );
1912
1913         vlc_mutex_lock( &p_httpt->host_lock );
1914         vlc_mutex_lock( &p_httpt->connection_lock );
1915         for( i = 0; i < p_httpt->i_host_count; i++ )
1916         {
1917             FD_SET( p_httpt->host[i]->fd, &fds_read );
1918             i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd );
1919         }
1920         for( p_con = p_httpt->p_first_connection; p_con != NULL; )
1921         {
1922             /* no more than 10s of inactivity */
1923             if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ||
1924                 p_con->i_state == HTTPD_CONNECTION_TO_BE_CLOSED)
1925             {
1926                 httpd_connection_t *p_next = p_con->p_next;
1927
1928                 msg_Dbg( p_httpt,  "close unused connection" );
1929                 httpd_ConnnectionClose( p_httpt, p_con );
1930                 p_con = p_next;
1931                 continue;
1932             }
1933
1934             if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos )
1935             {
1936                 p_con = p_con->p_next;
1937                 continue;
1938             }
1939
1940             if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
1941             {
1942                 FD_SET( p_con->fd, &fds_read );
1943             }
1944             else
1945             {
1946                 FD_SET( p_con->fd, &fds_write );
1947             }
1948             i_handle_max = __MAX( i_handle_max, p_con->fd );
1949
1950             p_con = p_con->p_next;
1951         }
1952         vlc_mutex_unlock( &p_httpt->host_lock );
1953         vlc_mutex_unlock( &p_httpt->connection_lock );
1954
1955         /* we will wait 0.5s */
1956         timeout.tv_sec = 0;
1957         timeout.tv_usec = 500*1000;
1958
1959         i_ret = select( i_handle_max + 1,
1960                         &fds_read,
1961                         &fds_write,
1962                         NULL,
1963                         &timeout );
1964         if( i_ret == -1 && errno != EINTR )
1965         {
1966             msg_Warn( p_httpt, "cannot select sockets" );
1967             msleep( 1000 );
1968             continue;
1969         }
1970         if( i_ret <= 0 )
1971         {
1972 //            msg_Dbg( p_httpt, "waiting..." );
1973             continue;
1974         }
1975
1976         vlc_mutex_lock( &p_httpt->host_lock );
1977         /* accept/refuse new connection */
1978         for( i = 0; i < p_httpt->i_host_count; i++ )
1979         {
1980             int     i_sock_size = sizeof( struct sockaddr_in );
1981             struct  sockaddr_in sock;
1982             int     fd;
1983
1984             fd = accept( p_httpt->host[i]->fd, (struct sockaddr *)&sock,
1985                          &i_sock_size );
1986             if( fd > 0 )
1987             {
1988 #if defined( WIN32 ) || defined( UNDER_CE )
1989                 {
1990                     unsigned long i_dummy = 1;
1991                     ioctlsocket( fd, FIONBIO, &i_dummy );
1992                 }
1993 #else
1994                 fcntl( fd, F_SETFL, O_NONBLOCK );
1995 #endif
1996
1997                 if( p_httpt->i_connection_count >= HTTPD_MAX_CONNECTION )
1998                 {
1999                     msg_Warn( p_httpt, "max connection reached" );
2000                     SOCKET_CLOSE( fd );
2001                     continue;
2002                 }
2003                 /* create a new connection and link it */
2004                 httpd_ConnnectionNew( p_httpt, fd, &sock );
2005
2006             }
2007         }
2008         vlc_mutex_unlock( &p_httpt->host_lock );
2009
2010         vlc_mutex_lock( &p_httpt->file_lock );
2011         /* now do work for all connections */
2012         for( p_con = p_httpt->p_first_connection; p_con != NULL; )
2013         {
2014             if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST )
2015             {
2016                 int i_len;
2017                 /* read data */
2018                 i_len = recv( p_con->fd,
2019                               p_con->p_buffer + p_con->i_buffer,
2020                               p_con->i_buffer_size - p_con->i_buffer, 0 );
2021
2022
2023 #if defined( WIN32 ) || defined( UNDER_CE )
2024                 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2025 #else
2026                 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2027 #endif
2028                 {
2029                     httpd_connection_t *p_next = p_con->p_next;
2030
2031                     httpd_ConnnectionClose( p_httpt, p_con );
2032                     p_con = p_next;
2033                 }
2034                 else if( i_len > 0 )
2035                 {
2036                     uint8_t *ptr;
2037                     p_con->i_last_activity_date = mdate();
2038                     p_con->i_buffer += i_len;
2039
2040                     ptr = p_con->p_buffer + p_con->i_buffer;
2041
2042                     if( ( p_con->i_buffer >= 2 && !strncmp( ptr - 2, "\n\n", 2 ) )||
2043                         ( p_con->i_buffer >= 4 && !strncmp( ptr - 4, "\r\n\r\n", 4 ) ) ||
2044                         p_con->i_buffer >= p_con->i_buffer_size )
2045                     {
2046                         p_con->p_buffer[__MIN( p_con->i_buffer, p_con->i_buffer_size - 1 )] = '\0';
2047                         httpd_ConnectionParseRequest( p_httpt, p_con );
2048                     }
2049
2050                     p_con = p_con->p_next;
2051                 }
2052                 else
2053                 {
2054                     p_con = p_con->p_next;
2055                 }
2056                 continue;   /* just for clarity */
2057             }
2058             else if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER || p_con->i_state == HTTPD_CONNECTION_SENDING_FILE )
2059             {
2060                 int i_len;
2061
2062                 /* write data */
2063                 if( p_con->i_buffer_size - p_con->i_buffer > 0 )
2064                 {
2065                     i_len = send( p_con->fd, p_con->p_buffer + p_con->i_buffer, p_con->i_buffer_size - p_con->i_buffer, 0 );
2066                 }
2067                 else
2068                 {
2069                     i_len = 0;
2070                 }
2071 //                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 );
2072
2073 #if defined( WIN32 ) || defined( UNDER_CE )
2074                 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2075 #else
2076                 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2077 #endif
2078                 {
2079                     httpd_connection_t *p_next = p_con->p_next;
2080
2081                     httpd_ConnnectionClose( p_httpt, p_con );
2082                     p_con = p_next;
2083                 }
2084                 else if( i_len > 0 )
2085                 {
2086                     p_con->i_last_activity_date = mdate();
2087                     p_con->i_buffer += i_len;
2088
2089                     if( p_con->i_buffer >= p_con->i_buffer_size )
2090                     {
2091                         if( p_con->i_state == HTTPD_CONNECTION_SENDING_HEADER )
2092                         {
2093                             p_con->i_buffer_size = 0;
2094                             p_con->i_buffer = 0;
2095                             FREE( p_con->p_buffer );
2096
2097                             if( !p_con->p_file->b_stream )
2098                             {
2099                                 p_con->i_state = HTTPD_CONNECTION_SENDING_FILE; // be sure to out from HTTPD_CONNECTION_SENDING_HEADER
2100                                 if( p_con->i_method == HTTPD_CONNECTION_METHOD_GET )
2101                                 {
2102                                     p_con->p_file->pf_get( p_con->p_file->p_sys,
2103                                                            p_con->p_request, p_con->i_request_size,
2104                                                            &p_con->p_buffer, &p_con->i_buffer_size );
2105                                 }
2106                                 else if( p_con->i_method == HTTPD_CONNECTION_METHOD_POST )
2107                                 {
2108                                     p_con->p_file->pf_post( p_con->p_file->p_sys,
2109                                                             p_con->p_request, p_con->i_request_size,
2110                                                             &p_con->p_buffer, &p_con->i_buffer_size );
2111                                 }
2112                                 else
2113                                 {
2114                                     p_con->p_buffer = NULL;
2115                                     p_con->i_buffer_size = 0;
2116                                 }
2117                             }
2118                             else
2119                             {
2120                                 p_con->i_state = HTTPD_CONNECTION_SENDING_STREAM;
2121                                 p_con->i_stream_pos = p_con->p_file->i_buffer_last_pos;
2122                             }
2123                             p_con = p_con->p_next;
2124                         }
2125                         else
2126                         {
2127                             httpd_connection_t *p_next = p_con->p_next;
2128
2129                             httpd_ConnnectionClose( p_httpt, p_con );
2130                             p_con = p_next;
2131                         }
2132                     }
2133                     else
2134                     {
2135                         p_con = p_con->p_next;
2136                     }
2137                 }
2138                 else
2139                 {
2140                     p_con = p_con->p_next;
2141                 }
2142                 continue;   /* just for clarity */
2143             }
2144             else if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM )
2145             {
2146                 httpd_stream_t *p_stream = p_con->p_file;
2147                 int i_send;
2148                 int i_write;
2149
2150                 if( p_con->i_stream_pos < p_stream->i_buffer_pos )
2151                 {
2152                     int i_pos;
2153                     /* check if this p_con aren't to late */
2154                     if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
2155                     {
2156                         fprintf(stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n", p_con->i_stream_pos, p_stream->i_buffer_pos  );
2157                         p_con->i_stream_pos = p_stream->i_buffer_last_pos;
2158                     }
2159
2160                     i_pos = p_con->i_stream_pos % p_stream->i_buffer_size;
2161                     /* size until end of buffer */
2162                     i_write = p_stream->i_buffer_size - i_pos;
2163                     /* is it more than valid data */
2164                     if( i_write >= p_stream->i_buffer_pos - p_con->i_stream_pos )
2165                     {
2166                         i_write = p_stream->i_buffer_pos - p_con->i_stream_pos;
2167                     }
2168                     /* limit to HTTPD_STREAM_PACKET */
2169                     if( i_write > HTTPD_STREAM_PACKET )
2170                     {
2171                         i_write = HTTPD_STREAM_PACKET;
2172                     }
2173                     i_send = send( p_con->fd, &p_stream->p_buffer[i_pos], i_write, 0 );
2174
2175 #if defined( WIN32 ) || defined( UNDER_CE )
2176                     if( ( i_send < 0 && WSAGetLastError() != WSAEWOULDBLOCK )|| ( i_send == 0 ) )
2177 #else
2178                     if( ( i_send < 0 && errno != EAGAIN && errno != EINTR )|| ( i_send == 0 ) )
2179 #endif
2180                     {
2181                         httpd_connection_t *p_next = p_con->p_next;
2182
2183                         httpd_ConnnectionClose( p_httpt, p_con );
2184                         p_con = p_next;
2185                         continue;
2186                     }
2187                     else if( i_send > 0 )
2188                     {
2189                         p_con->i_last_activity_date = mdate();
2190                         p_con->i_stream_pos += i_send;
2191                     }
2192                 }
2193                 p_con = p_con->p_next;
2194                 continue;   /* just for clarity */
2195             }
2196             else if( p_con->i_state != HTTPD_CONNECTION_TO_BE_CLOSED )
2197             {
2198                 msg_Warn( p_httpt, "cannot occur (Invalid p_con->i_state)" );
2199                 p_con = p_con->p_next;
2200             }
2201         }   /* for over connection */
2202
2203         vlc_mutex_unlock( &p_httpt->file_lock );
2204     }
2205     msg_Info( p_httpt, "httpd stopped" );
2206
2207     _UnregisterFile( p_httpt, p_page_401 );
2208     _UnregisterFile( p_httpt, p_page_404 );
2209     _UnregisterFile( p_httpt, p_page_admin );
2210 }