]> git.sesse.net Git - vlc/blob - src/network/httpd.c
c1ddfc98b600687f4ee66472b0cbdd54fc564a03
[vlc] / src / network / httpd.c
1 /*****************************************************************************
2  * httpd.c
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          RĂ©mi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <stdlib.h>
26 #include <vlc/vlc.h>
27
28 #ifdef ENABLE_HTTPD
29
30 #include "vlc_httpd.h"
31 #include "network.h"
32 #include "vlc_tls.h"
33 #include "vlc_acl.h"
34
35 #include <string.h>
36 #include <errno.h>
37
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>
40 #endif
41
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #if defined( UNDER_CE )
47 #   include <winsock.h>
48 #elif defined( WIN32 )
49 #   include <winsock2.h>
50 #   include <ws2tcpip.h>
51 #else
52 #   include <netdb.h>                                         /* hostent ... */
53 #   include <sys/socket.h>
54 /* FIXME: should not be needed */
55 #   include <netinet/in.h>
56 #   ifdef HAVE_ARPA_INET_H
57 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
58 #   endif
59 #endif
60
61 #if defined( WIN32 )
62 /* We need HUGE buffer otherwise TCP throughput is very limited */
63 #define HTTPD_CL_BUFSIZE 1000000
64 #else
65 #define HTTPD_CL_BUFSIZE 10000
66 #endif
67
68 #if 0
69 typedef struct httpd_t          httpd_t;
70
71 typedef struct httpd_host_t     httpd_host_t;
72 typedef struct httpd_url_t      httpd_url_t;
73 typedef struct httpd_client_t   httpd_client_t;
74
75 enum
76 {
77     HTTPD_MSG_NONE,
78
79     /* answer */
80     HTTPD_MSG_ANSWER,
81
82     /* channel communication */
83     HTTPD_MSG_CHANNEL,
84
85     /* http request */
86     HTTPD_MSG_GET,
87     HTTPD_MSG_HEAD,
88     HTTPD_MSG_POST,
89
90     /* rtsp request */
91     HTTPD_MSG_OPTIONS,
92     HTTPD_MSG_DESCRIBE,
93     HTTPD_MSG_SETUP,
94     HTTPD_MSG_PLAY,
95     HTTPD_MSG_PAUSE,
96     HTTPD_MSG_TEARDOWN,
97
98     /* just to track the count of MSG */
99     HTTPD_MSG_MAX
100 };
101
102 enum
103 {
104     HTTPD_PROTO_NONE,
105     HTTPD_PROTO_HTTP,
106     HTTPD_PROTO_RTSP,
107 };
108
109 typedef struct
110 {
111     httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
112
113     int     i_type;
114     int     i_proto;
115     int     i_version;
116
117     /* for an answer */
118     int     i_status;
119     char    *psz_status;
120
121     /* for a query */
122     char    *psz_url;
123     char    *psz_args;  /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
124
125     /* for rtp over rtsp */
126     int     i_channel;
127
128     /* options */
129     int     i_name;
130     char    **name;
131     int     i_value;
132     char    **value;
133
134     /* body */
135     int64_t i_body_offset;
136     int     i_body;
137     uint8_t *p_body;
138
139 } httpd_message_t;
140
141 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
142 /* answer could be null, int this case no answer is requested */
143 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
144
145
146 /* create a new host */
147 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
148 /* delete a host */
149 void          httpd_HostDelete( httpd_host_t * );
150
151 /* register a new url */
152 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
153 /* register callback on a url */
154 int          httpd_UrlCatch( httpd_url_t *, int i_msg,
155                              httpd_callback_t, httpd_callback_sys_t * );
156 /* delete an url */
157 void         httpd_UrlDelete( httpd_url_t * );
158
159
160 void httpd_ClientModeStream( httpd_client_t *cl );
161 void httpd_ClientModeBidir( httpd_client_t *cl );
162 static void httpd_ClientClean( httpd_client_t *cl );
163
164 /* High level */
165 typedef struct httpd_file_t     httpd_file_t;
166 typedef struct httpd_file_sys_t httpd_file_sys_t;
167 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
168 httpd_file_t *httpd_FileNew( httpd_host_t *,
169                              char *psz_url, char *psz_mime,
170                              char *psz_user, char *psz_password,
171                              httpd_file_callback_t pf_fill,
172                              httpd_file_sys_t *p_sys );
173 void         httpd_FileDelete( httpd_file_t * );
174
175 typedef struct httpd_redirect_t httpd_redirect_t;
176 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
177 void              httpd_RedirectDelete( httpd_redirect_t * );
178
179 #if 0
180 typedef struct httpd_stream_t httpd_stream_t;
181 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
182 int             httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
183 int             httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
184 void            httpd_StreamDelete( httpd_stream_t * );
185 #endif
186
187 /* Msg functions facilities */
188 void         httpd_MsgInit( httpd_message_t * );
189 void         httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
190 /* return "" if not found. The string is not allocated */
191 char        *httpd_MsgGet( httpd_message_t *, char *psz_name );
192 void         httpd_MsgClean( httpd_message_t * );
193 #endif
194
195 #if 0
196 struct httpd_t
197 {
198     VLC_COMMON_MEMBERS
199
200     /* vlc_mutex_t  lock; */
201     int          i_host;
202     httpd_host_t **host;
203 };
204 #endif
205
206 static void httpd_ClientClean( httpd_client_t *cl );
207
208 /* each host run in his own thread */
209 struct httpd_host_t
210 {
211     VLC_COMMON_MEMBERS
212
213     httpd_t     *httpd;
214
215     /* ref count */
216     int         i_ref;
217
218     /* address/port and socket for listening at connections */
219     char        *psz_hostname;
220     int         i_port;
221     int         *fd;
222
223     vlc_mutex_t lock;
224
225     /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
226      * This will slow down the url research but make my live easier
227      * All url will have their cb trigger, but only the first one can answer
228      * */
229     int         i_url;
230     httpd_url_t **url;
231
232     int            i_client;
233     httpd_client_t **client;
234     
235     /* TLS data */
236     tls_server_t *p_tls;
237 };
238
239 struct httpd_url_t
240 {
241     httpd_host_t *host;
242
243     vlc_mutex_t lock;
244
245     char      *psz_url;
246     char      *psz_user;
247     char      *psz_password;
248     vlc_acl_t *p_acl;
249
250     struct
251     {
252         httpd_callback_t     cb;
253         httpd_callback_sys_t *p_sys;
254     } catch[HTTPD_MSG_MAX];
255 };
256
257 /* status */
258 enum
259 {
260     HTTPD_CLIENT_RECEIVING,
261     HTTPD_CLIENT_RECEIVE_DONE,
262
263     HTTPD_CLIENT_SENDING,
264     HTTPD_CLIENT_SEND_DONE,
265
266     HTTPD_CLIENT_WAITING,
267
268     HTTPD_CLIENT_DEAD,
269
270     HTTPD_CLIENT_TLS_HS_IN,
271     HTTPD_CLIENT_TLS_HS_OUT
272 };
273 /* mode */
274 enum
275 {
276     HTTPD_CLIENT_FILE,      /* default */
277     HTTPD_CLIENT_STREAM,    /* regulary get data from cb */
278     HTTPD_CLIENT_BIDIR,     /* check for reading and get data from cb */
279 };
280
281 struct httpd_client_t
282 {
283     httpd_url_t *url;
284
285     int     i_ref;
286
287     struct  sockaddr_storage sock;
288     int     i_sock_size;
289     int     fd;
290
291     int     i_mode;
292     int     i_state;
293     int     b_read_waiting; /* stop as soon as possible sending */
294
295     mtime_t i_activity_date;
296     mtime_t i_activity_timeout;
297
298     /* buffer for reading header */
299     int     i_buffer_size;
300     int     i_buffer;
301     uint8_t *p_buffer;
302
303     /* */
304     httpd_message_t query;  /* client -> httpd */
305     httpd_message_t answer; /* httpd -> client */
306     
307     /* TLS data */
308     tls_session_t *p_tls;
309 };
310
311
312 /*****************************************************************************
313  * Various functions
314  *****************************************************************************/
315 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
316 static void b64_decode( char *dest, char *src )
317 {
318     int  i_level;
319     int  last = 0;
320     int  b64[256] = {
321         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
322         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
323         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
324         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
325         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
326         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
327         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
328         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
329         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
330         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
331         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
332         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
333         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
334         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
335         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
336         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
337         };
338
339     for( i_level = 0; *src != '\0'; src++ )
340     {
341         int  c;
342
343         c = b64[(unsigned int)*src];
344         if( c == -1 )
345         {
346             continue;
347         }
348
349         switch( i_level )
350         {
351             case 0:
352                 i_level++;
353                 break;
354             case 1:
355                 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
356                 i_level++;
357                 break;
358             case 2:
359                 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
360                 i_level++;
361                 break;
362             case 3:
363                 *dest++ = ( ( last &0x03 ) << 6 ) | c;
364                 i_level = 0;
365         }
366         last = c;
367     }
368
369     *dest = '\0';
370 }
371
372 static struct
373 {
374     const char *psz_ext;
375     const char *psz_mime;
376 } http_mime[] =
377 {
378     { ".htm",   "text/html" },
379     { ".html",  "text/html" },
380     { ".txt",   "text/plain" },
381     { ".xml",   "text/xml" },
382     { ".dtd",   "text/dtd" },
383
384     { ".css",   "text/css" },
385
386     /* image mime */
387     { ".gif",   "image/gif" },
388     { ".jpe",   "image/jpeg" },
389     { ".jpg",   "image/jpeg" },
390     { ".jpeg",  "image/jpeg" },
391     { ".png",   "image/png" },
392     { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
393
394     /* media mime */
395     { ".avi",   "video/avi" },
396     { ".asf",   "video/x-ms-asf" },
397     { ".m1a",   "audio/mpeg" },
398     { ".m2a",   "audio/mpeg" },
399     { ".m1v",   "video/mpeg" },
400     { ".m2v",   "video/mpeg" },
401     { ".mp2",   "audio/mpeg" },
402     { ".mp3",   "audio/mpeg" },
403     { ".mpa",   "audio/mpeg" },
404     { ".mpg",   "video/mpeg" },
405     { ".mpeg",  "video/mpeg" },
406     { ".mpe",   "video/mpeg" },
407     { ".mov",   "video/quicktime" },
408     { ".moov",  "video/quicktime" },
409     { ".ogg",   "application/ogg" },
410     { ".ogm",   "application/ogg" },
411     { ".wav",   "audio/wav" },
412     { ".wma",   "audio/x-ms-wma" },
413     { ".wmv",   "video/x-ms-wmv" },
414
415
416     /* end */
417     { NULL,     NULL }
418 };
419 static const char *httpd_MimeFromUrl( const char *psz_url )
420 {
421
422     char *psz_ext;
423
424     psz_ext = strrchr( psz_url, '.' );
425     if( psz_ext )
426     {
427         int i;
428
429         for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
430         {
431             if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
432             {
433                 return http_mime[i].psz_mime;
434             }
435         }
436     }
437     return "application/octet-stream";
438 }
439
440 /*****************************************************************************
441  * High Level Functions: httpd_file_t
442  *****************************************************************************/
443 struct httpd_file_t
444 {
445     httpd_url_t *url;
446
447     char *psz_url;
448     char *psz_mime;
449
450     httpd_file_callback_t pf_fill;
451     httpd_file_sys_t      *p_sys;
452
453 };
454
455
456 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
457 {
458     httpd_file_t *file = (httpd_file_t*)p_sys;
459     uint8_t *psz_args = query->psz_args;
460     uint8_t **pp_body, *p_body;
461     int *pi_body, i_body;
462
463     if( answer == NULL || query == NULL )
464     {
465         return VLC_SUCCESS;
466     }
467     answer->i_proto  = HTTPD_PROTO_HTTP;
468     answer->i_version= query->i_version;
469     answer->i_type   = HTTPD_MSG_ANSWER;
470
471     answer->i_status = 200;
472     answer->psz_status = strdup( "OK" );
473
474     httpd_MsgAdd( answer, "Content-type",  "%s", file->psz_mime );
475     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
476
477     if( query->i_type != HTTPD_MSG_HEAD )
478     {
479         pp_body = &answer->p_body;
480         pi_body = &answer->i_body;
481     }
482     else
483     {
484         /* The file still needs to be executed. */
485         p_body = NULL;
486         i_body = 0;
487         pp_body = &p_body;
488         pi_body = &i_body;
489     }
490
491     if( query->i_type == HTTPD_MSG_POST )
492     {
493         /* msg_Warn not supported */
494     }
495
496     file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
497
498     if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
499     {
500         free( p_body );
501     }
502
503     /* We respect client request */
504     if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
505     {
506         httpd_MsgAdd( answer, "Connection",
507                       httpd_MsgGet( &cl->query, "Connection" ) );
508     }
509
510     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
511
512     return VLC_SUCCESS;
513 }
514
515
516 httpd_file_t *httpd_FileNew( httpd_host_t *host,
517                              const char *psz_url, const char *psz_mime,
518                              const char *psz_user, const char *psz_password,
519                              const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
520                              httpd_file_sys_t *p_sys )
521 {
522     httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
523
524     if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
525                                           psz_password, p_acl )
526         ) == NULL )
527     {
528         free( file );
529         return NULL;
530     }
531
532     file->psz_url  = strdup( psz_url );
533     if( psz_mime && *psz_mime )
534     {
535         file->psz_mime = strdup( psz_mime );
536     }
537     else
538     {
539         file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
540     }
541
542     file->pf_fill = pf_fill;
543     file->p_sys   = p_sys;
544
545     httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
546                     (httpd_callback_sys_t*)file );
547     httpd_UrlCatch( file->url, HTTPD_MSG_GET,  httpd_FileCallBack,
548                     (httpd_callback_sys_t*)file );
549     httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
550                     (httpd_callback_sys_t*)file );
551
552     return file;
553 }
554
555 void httpd_FileDelete( httpd_file_t *file )
556 {
557     httpd_UrlDelete( file->url );
558
559     free( file->psz_url );
560     free( file->psz_mime );
561
562     free( file );
563 }
564
565 /*****************************************************************************
566  * High Level Functions: httpd_handler_t (for CGIs)
567  *****************************************************************************/
568 struct httpd_handler_t
569 {
570     httpd_url_t *url;
571
572     httpd_handler_callback_t pf_fill;
573     httpd_handler_sys_t      *p_sys;
574
575 };
576
577 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
578 {
579     httpd_handler_t *handler = (httpd_handler_t*)p_sys;
580     uint8_t *psz_args = query->psz_args;
581     char psz_remote_addr[100];
582
583     if( answer == NULL || query == NULL )
584     {
585         return VLC_SUCCESS;
586     }
587     answer->i_proto  = HTTPD_PROTO_NONE;
588     answer->i_type   = HTTPD_MSG_ANSWER;
589
590     /* We do it ourselves, thanks */
591     answer->i_status = 0;
592     answer->psz_status = NULL;
593
594     switch( cl->sock.ss_family )
595     {
596 #ifdef HAVE_INET_PTON
597     case AF_INET:
598         inet_ntop( cl->sock.ss_family,
599                    &((struct sockaddr_in *)&cl->sock)->sin_addr,
600                    psz_remote_addr, sizeof(psz_remote_addr) );
601         break;
602     case AF_INET6:
603         inet_ntop( cl->sock.ss_family,
604                    &((struct sockaddr_in6 *)&cl->sock)->sin6_addr,
605                    psz_remote_addr, sizeof(psz_remote_addr) );
606         break;
607 #else
608     case AF_INET:
609     {
610         char *psz_tmp = inet_ntoa( ((struct sockaddr_in *)&cl->sock)->sin_addr );
611         strncpy( psz_remote_addr, psz_tmp, sizeof(psz_remote_addr) );
612         break;
613     }
614 #endif
615     default:
616         psz_remote_addr[0] = '\0';
617     }
618
619     handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
620                       query->i_type, query->p_body, query->i_body,
621                       psz_remote_addr, NULL,
622                       &answer->p_body, &answer->i_body );
623
624     if( query->i_type == HTTPD_MSG_HEAD )
625     {
626         char *p = (char *)answer->p_body;
627         while ( (p = strchr( p, '\r' )) != NULL )
628         {
629             if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
630                  && p[3] && p[3] == '\n' )
631             {
632                 break;
633             }
634         }
635         if( p != NULL )
636         {
637             p[4] = '\0';
638             answer->i_body = strlen((char*)answer->p_body) + 1;
639             answer->p_body = realloc( answer->p_body, answer->i_body );
640         }
641     }
642
643     if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
644     {
645         int i_status, i_headers;
646         char *psz_headers, *psz_new, *psz_status;
647
648         if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
649         {
650             /* Apache-style */
651             i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
652             if( *psz_headers ) psz_headers++;
653             if( *psz_headers ) psz_headers++;
654             i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
655         }
656         else
657         {
658             i_status = 200;
659             psz_headers = (char *)answer->p_body;
660             i_headers = answer->i_body;
661         }
662         switch( i_status )
663         {
664         case 200:
665             psz_status = "OK";
666             break;
667         case 401:
668             psz_status = "Unauthorized";
669             break;
670         default:
671             if( (i_status < 0) || (i_status > 999) )
672                 i_status = 500;
673             psz_status = "Undefined";
674             break;
675         }
676         answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
677                         + strlen(psz_status) + i_headers - 1;
678         psz_new = (char *)malloc( answer->i_body + 1);
679         sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
680         memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
681         free( answer->p_body );
682         answer->p_body = (uint8_t *)psz_new;
683     }
684
685     return VLC_SUCCESS;
686 }
687
688 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
689                                    const char *psz_user,
690                                    const char *psz_password,
691                                    const vlc_acl_t *p_acl,
692                                    httpd_handler_callback_t pf_fill,
693                                    httpd_handler_sys_t *p_sys )
694 {
695     httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
696
697     if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
698                                              psz_password, p_acl )
699         ) == NULL )
700     {
701         free( handler );
702         return NULL;
703     }
704
705     handler->pf_fill = pf_fill;
706     handler->p_sys   = p_sys;
707
708     httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
709                     (httpd_callback_sys_t*)handler );
710     httpd_UrlCatch( handler->url, HTTPD_MSG_GET,  httpd_HandlerCallBack,
711                     (httpd_callback_sys_t*)handler );
712     httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
713                     (httpd_callback_sys_t*)handler );
714
715     return handler;
716 }
717
718 void httpd_HandlerDelete( httpd_handler_t *handler )
719 {
720     httpd_UrlDelete( handler->url );
721     free( handler );
722 }
723
724 /*****************************************************************************
725  * High Level Functions: httpd_redirect_t
726  *****************************************************************************/
727 struct httpd_redirect_t
728 {
729     httpd_url_t *url;
730     char        *psz_dst;
731 };
732
733 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
734                                    httpd_client_t *cl, httpd_message_t *answer,
735                                    httpd_message_t *query )
736 {
737     httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
738     uint8_t *p;
739
740     if( answer == NULL || query == NULL )
741     {
742         return VLC_SUCCESS;
743     }
744     answer->i_proto  = query->i_proto;
745     answer->i_version= query->i_version;
746     answer->i_type   = HTTPD_MSG_ANSWER;
747     answer->i_status = 301;
748     answer->psz_status = strdup( "Moved Permanently" );
749
750     p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
751     p += sprintf( (char *)p,
752         "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
753         "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
754         "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
755         "<html>\n"
756         "<head>\n"
757         "<title>Redirection</title>\n"
758         "</head>\n"
759         "<body>\n"
760         "<h1>You should be " 
761         "<a href=\"%s\">redirected</a></h1>\n"
762         "<hr />\n"
763         "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
764         "<hr />\n"
765         "</body>\n"
766         "</html>\n", rdir->psz_dst );
767     answer->i_body = p - answer->p_body;
768
769     /* XXX check if it's ok or we need to set an absolute url */
770     httpd_MsgAdd( answer, "Location",  "%s", rdir->psz_dst );
771
772     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
773
774     return VLC_SUCCESS;
775 }
776
777 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
778                                      const char *psz_url_src )
779 {
780     httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
781
782     if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
783     {
784         free( rdir );
785         return NULL;
786     }
787     rdir->psz_dst = strdup( psz_url_dst );
788
789     /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
790     httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
791                     (httpd_callback_sys_t*)rdir );
792     httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
793                     (httpd_callback_sys_t*)rdir );
794     httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
795                     (httpd_callback_sys_t*)rdir );
796     httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
797                     (httpd_callback_sys_t*)rdir );
798
799     return rdir;
800 }
801 void httpd_RedirectDelete( httpd_redirect_t *rdir )
802 {
803     httpd_UrlDelete( rdir->url );
804     free( rdir->psz_dst );
805     free( rdir );
806 }
807
808 /*****************************************************************************
809  * High Level Funtions: httpd_stream_t
810  *****************************************************************************/
811 struct httpd_stream_t
812 {
813     vlc_mutex_t lock;
814     httpd_url_t *url;
815
816     char    *psz_mime;
817
818     /* Header to send as first packet */
819     uint8_t *p_header;
820     int     i_header;
821
822     /* circular buffer */
823     int         i_buffer_size;      /* buffer size, can't be reallocated smaller */
824     uint8_t     *p_buffer;          /* buffer */
825     int64_t     i_buffer_pos;       /* absolute position from begining */
826     int64_t     i_buffer_last_pos;  /* a new connection will start with that */
827 };
828
829 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
830                                  httpd_client_t *cl, httpd_message_t *answer,
831                                  httpd_message_t *query )
832 {
833     httpd_stream_t *stream = (httpd_stream_t*)p_sys;
834
835     if( answer == NULL || query == NULL || cl == NULL )
836     {
837         return VLC_SUCCESS;
838     }
839     if( answer->i_body_offset > 0 )
840     {
841         int64_t i_write;
842         int     i_pos;
843
844 #if 0
845         fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
846                  answer->i_body_offset );
847 #endif
848
849         if( answer->i_body_offset >= stream->i_buffer_pos )
850         {
851             /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
852             return VLC_EGENERIC;    /* wait, no data available */
853         }
854         if( answer->i_body_offset + stream->i_buffer_size <
855             stream->i_buffer_pos )
856         {
857             /* this client isn't fast enough */
858             fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
859                      answer->i_body_offset, stream->i_buffer_last_pos );
860             answer->i_body_offset = stream->i_buffer_last_pos;
861         }
862
863         i_pos   = answer->i_body_offset % stream->i_buffer_size;
864         i_write = stream->i_buffer_pos - answer->i_body_offset;
865         if( i_write > HTTPD_CL_BUFSIZE )
866         {
867             i_write = HTTPD_CL_BUFSIZE;
868         }
869         else if( i_write <= 0 )
870         {
871             return VLC_EGENERIC;    /* wait, no data available */
872         }
873
874         /* Don't go past the end of the circular buffer */
875         i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
876
877         /* using HTTPD_MSG_ANSWER -> data available */
878         answer->i_proto  = HTTPD_PROTO_HTTP;
879         answer->i_version= 0;
880         answer->i_type   = HTTPD_MSG_ANSWER;
881
882         answer->i_body = i_write;
883         answer->p_body = malloc( i_write );
884         memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
885
886         answer->i_body_offset += i_write;
887
888         return VLC_SUCCESS;
889     }
890     else
891     {
892         answer->i_proto  = HTTPD_PROTO_HTTP;
893         answer->i_version= 0;
894         answer->i_type   = HTTPD_MSG_ANSWER;
895
896         answer->i_status = 200;
897         answer->psz_status = strdup( "OK" );
898
899         if( query->i_type != HTTPD_MSG_HEAD )
900         {
901             httpd_ClientModeStream( cl );
902             vlc_mutex_lock( &stream->lock );
903             /* Send the header */
904             if( stream->i_header > 0 )
905             {
906                 answer->i_body = stream->i_header;
907                 answer->p_body = malloc( stream->i_header );
908                 memcpy( answer->p_body, stream->p_header, stream->i_header );
909             }
910             answer->i_body_offset = stream->i_buffer_last_pos;
911             vlc_mutex_unlock( &stream->lock );
912         }
913         else
914         {
915             httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
916             answer->i_body_offset = 0;
917         }
918
919         if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
920         {
921             vlc_bool_t b_xplaystream = VLC_FALSE;
922             int i;
923
924             httpd_MsgAdd( answer, "Content-type", "%s",
925                           "application/octet-stream" );
926             httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
927             httpd_MsgAdd( answer, "Pragma", "no-cache" );
928             httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
929             httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
930
931             /* Check if there is a xPlayStrm=1 */
932             for( i = 0; i < query->i_name; i++ )
933             {
934                 if( !strcasecmp( query->name[i],  "Pragma" ) &&
935                     strstr( query->value[i], "xPlayStrm=1" ) )
936                 {
937                     b_xplaystream = VLC_TRUE;
938                 }
939             }
940
941             if( !b_xplaystream )
942             {
943                 answer->i_body_offset = 0;
944             }
945         }
946         else
947         {
948             httpd_MsgAdd( answer, "Content-type",  "%s", stream->psz_mime );
949         }
950         httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
951         return VLC_SUCCESS;
952     }
953 }
954
955 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
956                                  const char *psz_url, const char *psz_mime,
957                                  const char *psz_user, const char *psz_password,
958                                  const vlc_acl_t *p_acl )
959 {
960     httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
961
962     if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
963                                             psz_password, p_acl )
964         ) == NULL )
965     {
966         free( stream );
967         return NULL;
968     }
969     vlc_mutex_init( host, &stream->lock );
970     if( psz_mime && *psz_mime )
971     {
972         stream->psz_mime = strdup( psz_mime );
973     }
974     else
975     {
976         stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
977     }
978     stream->i_header = 0;
979     stream->p_header = NULL;
980     stream->i_buffer_size = 5000000;    /* 5 Mo per stream */
981     stream->p_buffer = malloc( stream->i_buffer_size );
982     /* We set to 1 to make life simpler
983      * (this way i_body_offset can never be 0) */
984     stream->i_buffer_pos = 1;
985     stream->i_buffer_last_pos = 1;
986
987     httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
988                     (httpd_callback_sys_t*)stream );
989     httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
990                     (httpd_callback_sys_t*)stream );
991     httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
992                     (httpd_callback_sys_t*)stream );
993
994     return stream;
995 }
996
997 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
998 {
999     vlc_mutex_lock( &stream->lock );
1000     if( stream->p_header )
1001     {
1002         free( stream->p_header );
1003         stream->p_header = NULL;
1004     }
1005     stream->i_header = i_data;
1006     if( i_data > 0 )
1007     {
1008         stream->p_header = malloc( i_data );
1009         memcpy( stream->p_header, p_data, i_data );
1010     }
1011     vlc_mutex_unlock( &stream->lock );
1012
1013     return VLC_SUCCESS;
1014 }
1015
1016 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
1017 {
1018     int i_count;
1019     int i_pos;
1020
1021     if( i_data < 0 || p_data == NULL )
1022     {
1023         return VLC_SUCCESS;
1024     }
1025     vlc_mutex_lock( &stream->lock );
1026
1027     /* save this pointer (to be used by new connection) */
1028     stream->i_buffer_last_pos = stream->i_buffer_pos;
1029
1030     i_pos = stream->i_buffer_pos % stream->i_buffer_size;
1031     i_count = i_data;
1032     while( i_count > 0)
1033     {
1034         int i_copy;
1035
1036         i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
1037
1038         /* Ok, we can't go past the end of our buffer */
1039         memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
1040
1041         i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
1042         i_count -= i_copy;
1043         p_data  += i_copy;
1044     }
1045
1046     stream->i_buffer_pos += i_data;
1047
1048     vlc_mutex_unlock( &stream->lock );
1049     return VLC_SUCCESS;
1050 }
1051
1052 void httpd_StreamDelete( httpd_stream_t *stream )
1053 {
1054     httpd_UrlDelete( stream->url );
1055     vlc_mutex_destroy( &stream->lock );
1056     if( stream->psz_mime ) free( stream->psz_mime );
1057     if( stream->p_header ) free( stream->p_header );
1058     if( stream->p_buffer ) free( stream->p_buffer );
1059     free( stream );
1060 }
1061
1062
1063 /*****************************************************************************
1064  * Low level
1065  *****************************************************************************/
1066 static void httpd_HostThread( httpd_host_t * );
1067
1068 /* create a new host */
1069 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
1070                              int i_port )
1071 {
1072     return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
1073                            );
1074 }
1075
1076 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
1077                                 int i_port,
1078                                 const char *psz_cert, const char *psz_key,
1079                                 const char *psz_ca, const char *psz_crl )
1080 {
1081     httpd_t      *httpd;
1082     httpd_host_t *host;
1083     tls_server_t *p_tls;
1084     char *psz_host;
1085     vlc_value_t  lockval;
1086     int i;
1087
1088     if( psz_hostname == NULL )
1089         psz_hostname = "";
1090
1091     psz_host = strdup( psz_hostname );
1092     if( psz_host == NULL )
1093     {
1094         msg_Err( p_this, "memory error" );
1095         return NULL;
1096     }
1097
1098     /* to be sure to avoid multiple creation */
1099     var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
1100     var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
1101     vlc_mutex_lock( lockval.p_address );
1102
1103     if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1104     {
1105         msg_Info( p_this, "creating httpd" );
1106         if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
1107         {
1108             vlc_mutex_unlock( lockval.p_address );
1109             free( psz_host );
1110             return NULL;
1111         }
1112
1113         httpd->i_host = 0;
1114         httpd->host   = NULL;
1115
1116         vlc_object_yield( httpd );
1117         vlc_object_attach( httpd, p_this->p_vlc );
1118     }
1119
1120     /* verify if it already exist */
1121     for( i = httpd->i_host - 1; i >= 0; i-- )
1122     {
1123         host = httpd->host[i];
1124
1125         /* cannot mix TLS and non-TLS hosts */
1126         if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1127          || ( host->i_port != i_port )
1128          || strcmp( host->psz_hostname, psz_hostname ) )
1129             continue;
1130
1131         /* yep found */
1132         host->i_ref++;
1133
1134         vlc_mutex_unlock( lockval.p_address );
1135         return host;
1136     }
1137
1138     host = NULL;
1139
1140     /* determine TLS configuration */
1141     if ( psz_cert != NULL )
1142     {
1143         p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1144         if ( p_tls == NULL )
1145         {
1146             msg_Err( p_this, "TLS initialization error" );
1147             goto error;
1148         }
1149
1150         if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1151         {
1152             msg_Err( p_this, "TLS CA error" );
1153             goto error;
1154         }
1155
1156         if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1157         {
1158             msg_Err( p_this, "TLS CRL error" );
1159             goto error;
1160         }
1161     }
1162     else
1163         p_tls = NULL;
1164
1165     /* create the new host */
1166     host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1167     host->httpd = httpd;
1168     vlc_mutex_init( httpd, &host->lock );
1169     host->i_ref = 1;
1170
1171     host->fd = net_ListenTCP( p_this, psz_host, i_port );
1172     if( host->fd == NULL )
1173     {
1174         msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1175         goto error;
1176     }
1177        
1178     host->i_port = i_port;
1179     host->psz_hostname = psz_host;
1180
1181     host->i_url     = 0;
1182     host->url       = NULL;
1183     host->i_client  = 0;
1184     host->client    = NULL;
1185
1186     host->p_tls = p_tls;
1187
1188     /* create the thread */
1189     if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1190                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1191     {
1192         msg_Err( p_this, "cannot spawn http host thread" );
1193         goto error;
1194     }
1195
1196     /* now add it to httpd */
1197     TAB_APPEND( httpd->i_host, httpd->host, host );
1198     vlc_mutex_unlock( lockval.p_address );
1199
1200     return host;
1201
1202 error:
1203     free( psz_host );
1204     if( httpd->i_host <= 0 )
1205     {
1206         vlc_object_release( httpd );
1207         vlc_object_detach( httpd );
1208         vlc_object_destroy( httpd );
1209     }
1210     vlc_mutex_unlock( lockval.p_address );
1211
1212     if( host != NULL )
1213     {
1214         net_ListenClose( host->fd );
1215         vlc_mutex_destroy( &host->lock );
1216         vlc_object_destroy( host );
1217     }
1218
1219     if( p_tls != NULL )
1220         tls_ServerDelete( p_tls );
1221
1222     return NULL;
1223 }
1224
1225 /* delete a host */
1226 void httpd_HostDelete( httpd_host_t *host )
1227 {
1228     httpd_t *httpd = host->httpd;
1229     vlc_value_t lockval;
1230     int i;
1231
1232     var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1233     vlc_mutex_lock( lockval.p_address );
1234
1235     host->i_ref--;
1236     if( host->i_ref > 0 )
1237     {
1238         /* still used */
1239         vlc_mutex_unlock( lockval.p_address );
1240         msg_Dbg( host, "httpd_HostDelete: host still used" );
1241         return;
1242     }
1243     TAB_REMOVE( httpd->i_host, httpd->host, host );
1244
1245     host->b_die = 1;
1246     vlc_thread_join( host );
1247
1248     msg_Dbg( host, "HTTP host removed" );
1249
1250     for( i = 0; i < host->i_url; i++ )
1251     {
1252         msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1253     }
1254     for( i = 0; i < host->i_client; i++ )
1255     {
1256         httpd_client_t *cl = host->client[i];
1257         msg_Warn( host, "client still connected" );
1258         httpd_ClientClean( cl );
1259         TAB_REMOVE( host->i_client, host->client, cl );
1260         free( cl );
1261         i--;
1262         /* TODO */
1263     }
1264
1265     if( host->p_tls != NULL)
1266         tls_ServerDelete( host->p_tls );
1267
1268     net_ListenClose( host->fd );
1269     free( host->psz_hostname );
1270
1271     vlc_mutex_destroy( &host->lock );
1272     vlc_object_destroy( host );
1273
1274     vlc_object_release( httpd );
1275     if( httpd->i_host <= 0 )
1276     {
1277         msg_Dbg( httpd, "no host left, stopping httpd" );
1278         vlc_object_detach( httpd );
1279         vlc_object_destroy( httpd );
1280     }
1281     vlc_mutex_unlock( lockval.p_address );
1282 }
1283
1284 /* register a new url */
1285 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1286                                          const char *psz_user, const char *psz_password,
1287                                          const vlc_acl_t *p_acl, vlc_bool_t b_check )
1288 {
1289     httpd_url_t *url;
1290     int         i;
1291
1292     vlc_mutex_lock( &host->lock );
1293     if( b_check )
1294     {
1295         for( i = 0; i < host->i_url; i++ )
1296         {
1297             if( !strcmp( psz_url, host->url[i]->psz_url ) )
1298             {
1299                 msg_Warn( host->httpd,
1300                           "cannot add '%s' (url already defined)", psz_url );
1301                 vlc_mutex_unlock( &host->lock );
1302                 return NULL;
1303             }
1304         }
1305     }
1306
1307     url = malloc( sizeof( httpd_url_t ) );
1308     url->host = host;
1309
1310     vlc_mutex_init( host->httpd, &url->lock );
1311     url->psz_url = strdup( psz_url );
1312     url->psz_user = strdup( psz_user ? psz_user : "" );
1313     url->psz_password = strdup( psz_password ? psz_password : "" );
1314     url->p_acl = ACL_Duplicate( host, p_acl );
1315     for( i = 0; i < HTTPD_MSG_MAX; i++ )
1316     {
1317         url->catch[i].cb = NULL;
1318         url->catch[i].p_sys = NULL;
1319     }
1320
1321     TAB_APPEND( host->i_url, host->url, url );
1322     vlc_mutex_unlock( &host->lock );
1323
1324     return url;
1325 }
1326
1327 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1328                            const char *psz_user, const char *psz_password,
1329                            const vlc_acl_t *p_acl )
1330 {
1331     return httpd_UrlNewPrivate( host, psz_url, psz_user,
1332                                 psz_password, p_acl, VLC_FALSE );
1333 }
1334
1335 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1336                                  const char *psz_user, const char *psz_password,
1337                                  const vlc_acl_t *p_acl )
1338 {
1339     return httpd_UrlNewPrivate( host, psz_url, psz_user,
1340                                 psz_password, p_acl, VLC_TRUE );
1341 }
1342
1343 /* register callback on a url */
1344 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1345                     httpd_callback_sys_t *p_sys )
1346 {
1347     vlc_mutex_lock( &url->lock );
1348     url->catch[i_msg].cb   = cb;
1349     url->catch[i_msg].p_sys= p_sys;
1350     vlc_mutex_unlock( &url->lock );
1351
1352     return VLC_SUCCESS;
1353 }
1354
1355
1356 /* delete an url */
1357 void httpd_UrlDelete( httpd_url_t *url )
1358 {
1359     httpd_host_t *host = url->host;
1360     int          i;
1361
1362     vlc_mutex_lock( &host->lock );
1363     TAB_REMOVE( host->i_url, host->url, url );
1364
1365     vlc_mutex_destroy( &url->lock );
1366     free( url->psz_url );
1367     free( url->psz_user );
1368     free( url->psz_password );
1369     ACL_Destroy( url->p_acl );
1370
1371     for( i = 0; i < host->i_client; i++ )
1372     {
1373         httpd_client_t *client = host->client[i];
1374
1375         if( client->url == url )
1376         {
1377             /* TODO complete it */
1378             msg_Warn( host, "force closing connections" );
1379             httpd_ClientClean( client );
1380             TAB_REMOVE( host->i_client, host->client, client );
1381             free( client );
1382             i--;
1383         }
1384     }
1385     free( url );
1386     vlc_mutex_unlock( &host->lock );
1387 }
1388
1389 void httpd_MsgInit( httpd_message_t *msg )
1390 {
1391     msg->cl         = NULL;
1392     msg->i_type     = HTTPD_MSG_NONE;
1393     msg->i_proto    = HTTPD_PROTO_NONE;
1394     msg->i_version  = -1;
1395
1396     msg->i_status   = 0;
1397     msg->psz_status = NULL;
1398
1399     msg->psz_url = NULL;
1400     msg->psz_args = NULL;
1401
1402     msg->i_channel = -1;
1403
1404     msg->i_name = 0;
1405     msg->name   = NULL;
1406     msg->i_value= 0;
1407     msg->value  = NULL;
1408
1409     msg->i_body_offset = 0;
1410     msg->i_body        = 0;
1411     msg->p_body        = 0;
1412 }
1413
1414 void httpd_MsgClean( httpd_message_t *msg )
1415 {
1416     int i;
1417
1418     if( msg->psz_status )
1419     {
1420         free( msg->psz_status );
1421     }
1422     if( msg->psz_url )
1423     {
1424         free( msg->psz_url );
1425     }
1426     if( msg->psz_args )
1427     {
1428         free( msg->psz_args );
1429     }
1430     for( i = 0; i < msg->i_name; i++ )
1431     {
1432         free( msg->name[i] );
1433         free( msg->value[i] );
1434     }
1435     if( msg->name )
1436     {
1437         free( msg->name );
1438     }
1439     if( msg->value )
1440     {
1441         free( msg->value );
1442     }
1443     if( msg->p_body )
1444     {
1445         free( msg->p_body );
1446     }
1447     httpd_MsgInit( msg );
1448 }
1449
1450 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1451 {
1452     int i;
1453
1454     for( i = 0; i < msg->i_name; i++ )
1455     {
1456         if( !strcasecmp( msg->name[i], name ))
1457         {
1458             return msg->value[i];
1459         }
1460     }
1461     return "";
1462 }
1463 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1464 {
1465     va_list args;
1466     char *value = NULL;
1467
1468     va_start( args, psz_value );
1469 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1470     vasprintf( &value, psz_value, args );
1471 #else
1472     {
1473         int i_size = strlen( psz_value ) + 4096;    /* FIXME stupid system */
1474         value = calloc( i_size, sizeof( char ) );
1475         vsnprintf( value, i_size, psz_value, args );
1476         value[i_size - 1] = 0;
1477     }
1478 #endif
1479     va_end( args );
1480
1481     name = strdup( name );
1482
1483     TAB_APPEND( msg->i_name,  msg->name,  name );
1484     TAB_APPEND( msg->i_value, msg->value, value );
1485 }
1486
1487 static void httpd_ClientInit( httpd_client_t *cl )
1488 {
1489     cl->i_state = HTTPD_CLIENT_RECEIVING;
1490     cl->i_activity_date = mdate();
1491     cl->i_activity_timeout = I64C(10000000);
1492     cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1493     cl->i_buffer = 0;
1494     cl->p_buffer = malloc( cl->i_buffer_size );
1495     cl->i_mode   = HTTPD_CLIENT_FILE;
1496     cl->b_read_waiting = VLC_FALSE;
1497
1498     httpd_MsgInit( &cl->query );
1499     httpd_MsgInit( &cl->answer );
1500 }
1501
1502 void httpd_ClientModeStream( httpd_client_t *cl )
1503 {
1504     cl->i_mode   = HTTPD_CLIENT_STREAM;
1505 }
1506
1507 void httpd_ClientModeBidir( httpd_client_t *cl )
1508 {
1509     cl->i_mode   = HTTPD_CLIENT_BIDIR;
1510 }
1511
1512 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
1513 {
1514     return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1515 }
1516
1517 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
1518 {
1519     return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1520 }
1521
1522 static void httpd_ClientClean( httpd_client_t *cl )
1523 {
1524     if( cl->fd >= 0 )
1525     {
1526         if( cl->p_tls != NULL )
1527             tls_ServerSessionClose( cl->p_tls );
1528         net_Close( cl->fd );
1529         cl->fd = -1;
1530     }
1531
1532     httpd_MsgClean( &cl->answer );
1533     httpd_MsgClean( &cl->query );
1534
1535     if( cl->p_buffer )
1536     {
1537         free( cl->p_buffer );
1538         cl->p_buffer = NULL;
1539     }
1540 }
1541
1542 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1543                                         int i_sock_size,
1544                                         tls_session_t *p_tls )
1545 {
1546     httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1547     cl->i_ref   = 0;
1548     cl->fd      = fd;
1549     memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1550     cl->i_sock_size = i_sock_size;
1551     cl->url     = NULL;
1552     cl->p_tls = p_tls;
1553
1554     httpd_ClientInit( cl );
1555
1556     return cl;
1557 }
1558
1559
1560 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1561 {
1562     tls_session_t *p_tls;
1563     
1564     p_tls = cl->p_tls;
1565     if( p_tls != NULL)
1566         return tls_Recv( p_tls, p, i_len );
1567
1568     return recv( cl->fd, p, i_len, 0 );
1569 }
1570
1571
1572 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1573 {
1574     tls_session_t *p_tls;
1575
1576     p_tls = cl->p_tls;
1577     if( p_tls != NULL)
1578         return tls_Send( p_tls, p, i_len );
1579
1580     return send( cl->fd, p, i_len, 0 );
1581 }
1582
1583
1584 static void httpd_ClientRecv( httpd_client_t *cl )
1585 {
1586     int i_len;
1587
1588     if( cl->query.i_proto == HTTPD_PROTO_NONE )
1589     {
1590         /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1591         i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1592                                4 - cl->i_buffer );
1593         if( i_len > 0 )
1594         {
1595             cl->i_buffer += i_len;
1596         }
1597
1598         if( cl->i_buffer >= 4 )
1599         {
1600             /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1601             /* detect type */
1602             if( cl->p_buffer[0] == '$' )
1603             {
1604                 /* RTSP (rtp over rtsp) */
1605                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1606                 cl->query.i_type  = HTTPD_MSG_CHANNEL;
1607                 cl->query.i_channel = cl->p_buffer[1];
1608                 cl->query.i_body  = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1609                 cl->query.p_body  = malloc( cl->query.i_body );
1610
1611                 cl->i_buffer      = 0;
1612             }
1613             else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1614             {
1615                 cl->query.i_proto = HTTPD_PROTO_HTTP;
1616                 cl->query.i_type  = HTTPD_MSG_ANSWER;
1617             }
1618             else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1619             {
1620                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1621                 cl->query.i_type  = HTTPD_MSG_ANSWER;
1622             }
1623             else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
1624                      !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1625                      !memcmp( cl->p_buffer, "POST", 4 ) )
1626             {
1627                 cl->query.i_proto = HTTPD_PROTO_HTTP;
1628                 cl->query.i_type  = HTTPD_MSG_NONE;
1629             }
1630             else
1631             {
1632                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1633                 cl->query.i_type  = HTTPD_MSG_NONE;
1634             }
1635         }
1636     }
1637     else if( cl->query.i_body > 0 )
1638     {
1639         /* we are reading the body of a request or a channel */
1640         i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1641                                cl->query.i_body - cl->i_buffer );
1642         if( i_len > 0 )
1643         {
1644             cl->i_buffer += i_len;
1645         }
1646         if( cl->i_buffer >= cl->query.i_body )
1647         {
1648             cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1649         }
1650     }
1651     else
1652     {
1653         /* we are reading a header -> char by char */
1654         for( ;; )
1655         {
1656             i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1657             if( i_len <= 0 )
1658             {
1659                 break;
1660             }
1661             cl->i_buffer++;
1662
1663             if( cl->i_buffer + 1 >= cl->i_buffer_size )
1664             {
1665                 cl->i_buffer_size += 1024;
1666                 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1667             }
1668             if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1669                 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1670             {
1671                 char *p;
1672
1673                 /* we have finished the header so parse it and set i_body */
1674                 cl->p_buffer[cl->i_buffer] = '\0';
1675
1676                 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1677                 {
1678                     /* FIXME:
1679                      * assume strlen( "HTTP/1.x" ) = 8
1680                      */
1681                     cl->query.i_status =
1682                         strtol( (char *)&cl->p_buffer[8],
1683                                 &p, 0 );
1684                     while( *p == ' ' )
1685                     {
1686                         p++;
1687                     }
1688                     cl->query.psz_status = strdup( p );
1689                 }
1690                 else
1691                 {
1692                     static const struct
1693                     {
1694                         char *name;
1695                         int  i_type;
1696                         int  i_proto;
1697                     }
1698                     msg_type[] =
1699                     {
1700                         { "GET",        HTTPD_MSG_GET,  HTTPD_PROTO_HTTP },
1701                         { "HEAD",       HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1702                         { "POST",       HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1703
1704                         { "OPTIONS",    HTTPD_MSG_OPTIONS,  HTTPD_PROTO_RTSP },
1705                         { "DESCRIBE",   HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1706                         { "SETUP",      HTTPD_MSG_SETUP,    HTTPD_PROTO_RTSP },
1707                         { "PLAY",       HTTPD_MSG_PLAY,     HTTPD_PROTO_RTSP },
1708                         { "PAUSE",      HTTPD_MSG_PAUSE,    HTTPD_PROTO_RTSP },
1709                         { "TEARDOWN",   HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1710
1711                         { NULL,         HTTPD_MSG_NONE,     HTTPD_PROTO_NONE }
1712                     };
1713                     int  i;
1714
1715                     p = NULL;
1716                     cl->query.i_type = HTTPD_MSG_NONE;
1717
1718                     /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1719
1720                     for( i = 0; msg_type[i].name != NULL; i++ )
1721                     {
1722                         if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1723                                       strlen( msg_type[i].name ) ) )
1724                         {
1725                             p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1726                             cl->query.i_type = msg_type[i].i_type;
1727                             if( cl->query.i_proto != msg_type[i].i_proto )
1728                             {
1729                                 p = NULL;
1730                                 cl->query.i_proto = HTTPD_PROTO_NONE;
1731                                 cl->query.i_type = HTTPD_MSG_NONE;
1732                             }
1733                             break;
1734                         }
1735                     }
1736                     if( p == NULL )
1737                     {
1738                         if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1739                         {
1740                             cl->query.i_proto = HTTPD_PROTO_HTTP;
1741                         }
1742                         else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1743                         {
1744                             cl->query.i_proto = HTTPD_PROTO_RTSP;
1745                         }
1746                     }
1747                     else
1748                     {
1749                         char *p2;
1750                         char *p3;
1751
1752                         while( *p == ' ' )
1753                         {
1754                             p++;
1755                         }
1756                         p2 = strchr( p, ' ' );
1757                         if( p2 )
1758                         {
1759                             *p2++ = '\0';
1760                         }
1761                         if( !strncasecmp( p, "rtsp:", 5 ) )
1762                         {
1763                             /* for rtsp url, you have rtsp://localhost:port/path */
1764                             p += 5;
1765                             while( *p == '/' ) p++;
1766                             while( *p && *p != '/' ) p++;
1767                         }
1768                         cl->query.psz_url = strdup( p );
1769                         if( ( p3 = strchr( cl->query.psz_url, '?' ) )  )
1770                         {
1771                             *p3++ = '\0';
1772                             cl->query.psz_args = (uint8_t *)strdup( p3 );
1773                         }
1774                         if( p2 )
1775                         {
1776                             while( *p2 == ' ' )
1777                             {
1778                                 p2++;
1779                             }
1780                             if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1781                             {
1782                                 cl->query.i_proto = HTTPD_PROTO_HTTP;
1783                                 cl->query.i_version = atoi( p2+7 );
1784                             }
1785                             else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1786                             {
1787                                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1788                                 cl->query.i_version = atoi( p2+7 );
1789                             }
1790                         }
1791                         p = p2;
1792                     }
1793                 }
1794                 if( p )
1795                 {
1796                     p = strchr( p, '\n' );
1797                 }
1798                 if( p )
1799                 {
1800                     while( *p == '\n' || *p == '\r' )
1801                     {
1802                         p++;
1803                     }
1804                     while( p && *p != '\0' )
1805                     {
1806                         char *line = p;
1807                         char *eol = p = strchr( p, '\n' );
1808                         char *colon;
1809
1810                         while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1811                         {
1812                             *eol-- = '\0';
1813                         }
1814
1815                         if( ( colon = strchr( line, ':' ) ) )
1816                         {
1817                             char *name;
1818                             char *value;
1819
1820                             *colon++ = '\0';
1821                             while( *colon == ' ' )
1822                             {
1823                                 colon++;
1824                             }
1825                             name = strdup( line );
1826                             value = strdup( colon );
1827
1828                             TAB_APPEND( cl->query.i_name, cl->query.name, name );
1829                             TAB_APPEND( cl->query.i_value,cl->query.value,value);
1830
1831                             if( !strcasecmp( name, "Content-Length" ) )
1832                             {
1833                                 cl->query.i_body = atol( value );
1834                             }
1835                         }
1836
1837                         if( p )
1838                         {
1839                             p++;
1840                             while( *p == '\n' || *p == '\r' )
1841                             {
1842                                 p++;
1843                             }
1844                         }
1845                     }
1846                 }
1847                 if( cl->query.i_body > 0 )
1848                 {
1849                     /* TODO Mhh, handle the case client will only send a request and close the connection
1850                      * to mark and of body (probably only RTSP) */
1851                     cl->query.p_body = malloc( cl->query.i_body );
1852                     cl->i_buffer = 0;
1853                 }
1854                 else
1855                 {
1856                     cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1857                 }
1858             }
1859         }
1860     }
1861
1862     /* check if the client is to be set to dead */
1863 #if defined( WIN32 ) || defined( UNDER_CE )
1864     if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1865 #else
1866     if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1867 #endif
1868     {
1869         if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1870         {
1871             /* connection closed -> end of data */
1872             if( cl->query.i_body > 0 )
1873             {
1874                 cl->query.i_body = cl->i_buffer;
1875             }
1876             cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1877         }
1878         else
1879         {
1880             cl->i_state = HTTPD_CLIENT_DEAD;
1881         }
1882     }
1883     cl->i_activity_date = mdate();
1884
1885     /* XXX: for QT I have to disable timeout. Try to find why */
1886     if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1887         cl->i_activity_timeout = 0;
1888
1889     /* Debugging only */
1890     /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1891     {
1892         int i;
1893
1894         fprintf( stderr, "received new request\n" );
1895         fprintf( stderr, "  - proto=%s\n",
1896                  cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1897         fprintf( stderr, "  - version=%d\n", cl->query.i_version );
1898         fprintf( stderr, "  - msg=%d\n", cl->query.i_type );
1899         if( cl->query.i_type == HTTPD_MSG_ANSWER )
1900         {
1901             fprintf( stderr, "  - answer=%d '%s'\n", cl->query.i_status,
1902                      cl->query.psz_status );
1903         }
1904         else if( cl->query.i_type != HTTPD_MSG_NONE )
1905         {
1906             fprintf( stderr, "  - url=%s\n", cl->query.psz_url );
1907         }
1908         for( i = 0; i < cl->query.i_name; i++ )
1909         {
1910             fprintf( stderr, "  - option name='%s' value='%s'\n",
1911                      cl->query.name[i], cl->query.value[i] );
1912         }
1913     }*/
1914 }
1915
1916
1917 static void httpd_ClientSend( httpd_client_t *cl )
1918 {
1919     int i;
1920     int i_len;
1921
1922     if( cl->i_buffer < 0 )
1923     {
1924         /* We need to create the header */
1925         int i_size = 0;
1926         char *p;
1927
1928         i_size = strlen( "HTTP/1.") + 10 + 10 +
1929                  strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1930         for( i = 0; i < cl->answer.i_name; i++ )
1931         {
1932             i_size += strlen( cl->answer.name[i] ) + 2 +
1933                       strlen( cl->answer.value[i] ) + 2;
1934         }
1935
1936         if( cl->i_buffer_size < i_size )
1937         {
1938             cl->i_buffer_size = i_size;
1939             free( cl->p_buffer );
1940             cl->p_buffer = malloc( i_size );
1941         }
1942         p = (char *)cl->p_buffer;
1943
1944         p += sprintf( p, "%s/1.%d %d %s\r\n",
1945                       cl->answer.i_proto ==  HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1946                       cl->answer.i_version,
1947                       cl->answer.i_status, cl->answer.psz_status );
1948         for( i = 0; i < cl->answer.i_name; i++ )
1949         {
1950             p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1951                           cl->answer.value[i] );
1952         }
1953         p += sprintf( p, "\r\n" );
1954
1955         cl->i_buffer = 0;
1956         cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1957
1958         /*fprintf( stderr, "sending answer\n" );
1959         fprintf( stderr, "%s",  cl->p_buffer );*/
1960     }
1961
1962     i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1963                            cl->i_buffer_size - cl->i_buffer );
1964     if( i_len >= 0 )
1965     {
1966         cl->i_activity_date = mdate();
1967         cl->i_buffer += i_len;
1968
1969         if( cl->i_buffer >= cl->i_buffer_size )
1970         {
1971             if( cl->answer.i_body == 0  && cl->answer.i_body_offset > 0 &&
1972                 !cl->b_read_waiting )
1973             {
1974                 /* catch more body data */
1975                 int     i_msg = cl->query.i_type;
1976                 int64_t i_offset = cl->answer.i_body_offset;
1977
1978                 httpd_MsgClean( &cl->answer );
1979                 cl->answer.i_body_offset = i_offset;
1980
1981                 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1982                                           &cl->answer, &cl->query );
1983             }
1984
1985             if( cl->answer.i_body > 0 )
1986             {
1987                 /* send the body data */
1988                 free( cl->p_buffer );
1989                 cl->p_buffer = cl->answer.p_body;
1990                 cl->i_buffer_size = cl->answer.i_body;
1991                 cl->i_buffer = 0;
1992
1993                 cl->answer.i_body = 0;
1994                 cl->answer.p_body = NULL;
1995             }
1996             else
1997             {
1998                 /* send finished */
1999                 cl->i_state = HTTPD_CLIENT_SEND_DONE;
2000             }
2001         }
2002     }
2003     else
2004     {
2005 #if defined( WIN32 ) || defined( UNDER_CE )
2006         if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2007 #else
2008         if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2009 #endif
2010         {
2011             /* error */
2012             cl->i_state = HTTPD_CLIENT_DEAD;
2013         }
2014     }
2015 }
2016
2017 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
2018 {
2019     switch( tls_SessionContinueHandshake( cl->p_tls ) )
2020     {
2021         case 0:
2022             cl->i_state = HTTPD_CLIENT_RECEIVING;
2023             break;
2024
2025         case -1:
2026             cl->i_state = HTTPD_CLIENT_DEAD;
2027             cl->p_tls = NULL;
2028             break;
2029
2030         case 2:
2031             cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2032     }
2033 }
2034
2035 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2036 {
2037     switch( tls_SessionContinueHandshake( cl->p_tls ) )
2038     {
2039         case 0:
2040             cl->i_state = HTTPD_CLIENT_RECEIVING;
2041             break;
2042
2043         case -1:
2044             cl->i_state = HTTPD_CLIENT_DEAD;
2045             cl->p_tls = NULL;
2046             break;
2047
2048         case 1:
2049             cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2050             break;
2051     }
2052 }
2053
2054 static void httpd_HostThread( httpd_host_t *host )
2055 {
2056     tls_session_t *p_tls = NULL;
2057
2058     stats_Create( host, "client_connections", VLC_VAR_INTEGER, STATS_COUNTER );
2059     stats_Create( host, "active_connections", VLC_VAR_INTEGER, STATS_COUNTER );
2060
2061     while( !host->b_die )
2062     {
2063         struct timeval  timeout;
2064         fd_set          fds_read;
2065         fd_set          fds_write;
2066         /* FIXME: (too) many int variables */
2067         int             fd, i_fd;
2068         int             i_handle_max = 0;
2069         int             i_ret;
2070         int             i_client;
2071         int             b_low_delay = 0;
2072
2073         if( host->i_url <= 0 )
2074         {
2075             /* 0.2s */
2076             msleep( 200000 );
2077             continue;
2078         }
2079
2080         /* built a set of handle to select */
2081         FD_ZERO( &fds_read );
2082         FD_ZERO( &fds_write );
2083
2084         i_handle_max = -1;
2085
2086         for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2087         {
2088             FD_SET( fd, &fds_read );
2089             if( fd > i_handle_max )
2090                 i_handle_max = fd;
2091         }
2092
2093         /* prepare a new TLS session */
2094         if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2095             p_tls = tls_ServerSessionPrepare( host->p_tls );
2096
2097         /* add all socket that should be read/write and close dead connection */
2098         vlc_mutex_lock( &host->lock );
2099         for( i_client = 0; i_client < host->i_client; i_client++ )
2100         {
2101             httpd_client_t *cl = host->client[i_client];
2102
2103             if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2104                 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2105                   ( cl->i_activity_timeout > 0 &&
2106                     cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2107             {
2108                 httpd_ClientClean( cl );
2109                 stats_UpdateInteger( host, "active_connections", -1, NULL );
2110                 TAB_REMOVE( host->i_client, host->client, cl );
2111                 free( cl );
2112                 i_client--;
2113                 continue;
2114             }
2115             else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2116                   || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2117             {
2118                 FD_SET( cl->fd, &fds_read );
2119                 i_handle_max = __MAX( i_handle_max, cl->fd );
2120             }
2121             else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2122                   || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2123             {
2124                 FD_SET( cl->fd, &fds_write );
2125                 i_handle_max = __MAX( i_handle_max, cl->fd );
2126             }
2127             else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2128             {
2129                 httpd_message_t *answer = &cl->answer;
2130                 httpd_message_t *query  = &cl->query;
2131                 int i_msg = query->i_type;
2132
2133                 httpd_MsgInit( answer );
2134
2135                 /* Handle what we received */
2136                 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2137                     (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2138                 {
2139                     /* we can only receive request from client when not
2140                      * in BIDIR mode */
2141                     cl->url     = NULL;
2142                     cl->i_state = HTTPD_CLIENT_DEAD;
2143                 }
2144                 else if( i_msg == HTTPD_MSG_ANSWER )
2145                 {
2146                     /* We are in BIDIR mode, trigger the callback and then
2147                      * check for new data */
2148                     if( cl->url && cl->url->catch[i_msg].cb )
2149                     {
2150                         cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2151                                                   cl, NULL, query );
2152                     }
2153                     cl->i_state = HTTPD_CLIENT_WAITING;
2154                 }
2155                 else if( i_msg == HTTPD_MSG_CHANNEL )
2156                 {
2157                     /* We are in BIDIR mode, trigger the callback and then
2158                      * check for new data */
2159                     if( cl->url && cl->url->catch[i_msg].cb )
2160                     {
2161                         cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2162                                                   cl, NULL, query );
2163                     }
2164                     cl->i_state = HTTPD_CLIENT_WAITING;
2165                 }
2166                 else if( i_msg == HTTPD_MSG_OPTIONS )
2167                 {
2168                     int i_cseq;
2169
2170                     /* unimplemented */
2171                     answer->i_proto  = query->i_proto ;
2172                     answer->i_type   = HTTPD_MSG_ANSWER;
2173                     answer->i_version= 0;
2174                     answer->i_status = 200;
2175                     answer->psz_status = strdup( "Ok" );
2176
2177                     answer->i_body = 0;
2178                     answer->p_body = NULL;
2179
2180                     i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2181                     httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2182                     httpd_MsgAdd( answer, "Server", "VLC Server" );
2183                     httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2184                                  "TEARDOWN, PLAY, PAUSE" );
2185                     httpd_MsgAdd( answer, "Content-Length", "%d",
2186                                   answer->i_body );
2187
2188                     cl->i_buffer = -1;  /* Force the creation of the answer in
2189                                          * httpd_ClientSend */
2190                     cl->i_state = HTTPD_CLIENT_SENDING;
2191                 }
2192                 else if( i_msg == HTTPD_MSG_NONE )
2193                 {
2194                     if( query->i_proto == HTTPD_PROTO_NONE )
2195                     {
2196                         cl->url = NULL;
2197                         cl->i_state = HTTPD_CLIENT_DEAD;
2198                     }
2199                     else
2200                     {
2201                         uint8_t *p;
2202
2203                         /* unimplemented */
2204                         answer->i_proto  = query->i_proto ;
2205                         answer->i_type   = HTTPD_MSG_ANSWER;
2206                         answer->i_version= 0;
2207                         answer->i_status = 501;
2208                         answer->psz_status = strdup( "Unimplemented" );
2209
2210                         p = answer->p_body = malloc( 1000 );
2211
2212                         p += sprintf( (char *)p,
2213                             "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2214                             "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
2215                             "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2216                             "<html>\n"
2217                             "<head>\n"
2218                             "<title>Error 501</title>\n"
2219                             "</head>\n"
2220                             "<body>\n"
2221                             "<h1>501 Unimplemented</h1>\n"
2222                             "<hr />\n"
2223                             "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2224                             "</body>\n"
2225                             "</html>\n" );
2226
2227                         answer->i_body = p - answer->p_body;
2228                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2229
2230                         cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
2231                         cl->i_state = HTTPD_CLIENT_SENDING;
2232                     }
2233                 }
2234                 else
2235                 {
2236                     vlc_bool_t b_auth_failed = VLC_FALSE;
2237                     vlc_bool_t b_hosts_failed = VLC_FALSE;
2238                     int i;
2239
2240                     /* Search the url and trigger callbacks */
2241                     for( i = 0; i < host->i_url; i++ )
2242                     {
2243                         httpd_url_t *url = host->url[i];
2244
2245                         if( !strcmp( url->psz_url, query->psz_url ) )
2246                         {
2247                             if( url->catch[i_msg].cb )
2248                             {
2249                                 if( answer && ( url->p_acl != NULL ) )
2250                                 {
2251                                     char ip[NI_MAXNUMERICHOST];
2252
2253                                     if( httpd_ClientIP( cl, ip ) != NULL )
2254                                     {
2255                                         if( ACL_Check( url->p_acl, ip ) )
2256                                         {
2257                                             b_hosts_failed = VLC_TRUE;
2258                                             break;
2259                                         }
2260                                     }
2261                                     else
2262                                         b_hosts_failed = VLC_TRUE;
2263                                 }
2264
2265                                 if( answer && ( *url->psz_user || *url->psz_password ) )
2266                                 {
2267                                     /* create the headers */
2268                                     char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2269                                     char *auth;
2270                                     char *id;
2271
2272                                     asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2273                                     auth = malloc( strlen(b64) + 1 );
2274
2275                                     if( !strncasecmp( b64, "BASIC", 5 ) )
2276                                     {
2277                                         b64 += 5;
2278                                         while( *b64 == ' ' )
2279                                         {
2280                                             b64++;
2281                                         }
2282                                         b64_decode( auth, b64 );
2283                                     }
2284                                     else
2285                                     {
2286                                         strcpy( auth, "" );
2287                                     }
2288                                     if( strcmp( id, auth ) )
2289                                     {
2290                                         httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2291                                         /* We fail for all url */
2292                                         b_auth_failed = VLC_TRUE;
2293                                         free( id );
2294                                         free( auth );
2295                                         break;
2296                                     }
2297
2298                                     free( id );
2299                                     free( auth );
2300                                 }
2301
2302                                 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2303                                 {
2304                                     if( answer->i_proto == HTTPD_PROTO_NONE )
2305                                     {
2306                                         /* Raw answer from a CGI */
2307                                         cl->i_buffer = cl->i_buffer_size;
2308                                     }
2309                                     else
2310                                         cl->i_buffer = -1;
2311
2312                                     /* only one url can answer */
2313                                     answer = NULL;
2314                                     if( cl->url == NULL )
2315                                     {
2316                                         cl->url = url;
2317                                     }
2318                                 }
2319                             }
2320                         }
2321                     }
2322                     if( answer )
2323                     {
2324                         uint8_t *p;
2325
2326                         answer->i_proto  = query->i_proto;
2327                         answer->i_type   = HTTPD_MSG_ANSWER;
2328                         answer->i_version= 0;
2329                         p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2330
2331                         if( b_hosts_failed )
2332                         {
2333                             answer->i_status = 403;
2334                             answer->psz_status = strdup( "Forbidden" );
2335
2336                             /* FIXME: lots of code duplication */
2337                             p += sprintf( (char *)p,
2338                                 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2339                                 "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
2340                                 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2341                                 "<html>\n"
2342                                 "<head>\n"
2343                                 "<title>Error 403</title>\n"
2344                                 "</head>\n"
2345                                 "<body>\n"
2346                                 "<h1>403 Forbidden (%s)</h1>\n"
2347                                 "<hr />\n"
2348                                 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2349                                 "</body>\n"
2350                                 "</html>\n", query->psz_url );
2351                         }
2352                         else if( b_auth_failed )
2353                         {
2354                             answer->i_status = 401;
2355                             answer->psz_status = strdup( "Authorization Required" );
2356
2357                             p += sprintf( (char *)p,
2358                                 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2359                                 "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
2360                                 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2361                                 "<html>\n"
2362                                 "<head>\n"
2363                                 "<title>Error 401</title>\n"
2364                                 "</head>\n"
2365                                 "<body>\n"
2366                                 "<h1>401 Authorization Required (%s)</h1>\n"
2367                                 "<hr />\n"
2368                                 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2369                                 "</body>\n"
2370                                 "</html>\n", query->psz_url );
2371                         }
2372                         else
2373                         {
2374                             /* no url registered */
2375                             answer->i_status = 404;
2376                             answer->psz_status = strdup( "Not found" );
2377
2378                             p += sprintf( (char *)p,
2379                                 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2380                                 "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
2381                                 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2382                                 "<html>\n"
2383                                 "<head>\n"
2384                                 "<title>Error 404</title>\n"
2385                                 "</head>\n"
2386                                 "<body>\n"
2387                                 "<h1>404 Resource not found(%s)</h1>\n"
2388                                 "<hr />\n"
2389                                 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2390                                 "</body>\n"
2391                                 "</html>\n", query->psz_url );
2392                         }
2393
2394                         answer->i_body = p - answer->p_body;
2395                         cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
2396                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2397                     }
2398
2399                     cl->i_state = HTTPD_CLIENT_SENDING;
2400                 }
2401             }
2402             else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2403             {
2404                 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2405                 {
2406                     cl->url = NULL;
2407                     if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2408                           ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2409                             ( cl->answer.i_version == 1 &&  strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2410                         ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2411                           strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2412                           strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2413                     {
2414                         httpd_MsgClean( &cl->query );
2415                         httpd_MsgInit( &cl->query );
2416
2417                         cl->i_buffer = 0;
2418                         cl->i_buffer_size = 1000;
2419                         free( cl->p_buffer );
2420                         cl->p_buffer = malloc( cl->i_buffer_size );
2421                         cl->i_state = HTTPD_CLIENT_RECEIVING;
2422                     }
2423                     else
2424                     {
2425                         cl->i_state = HTTPD_CLIENT_DEAD;
2426                     }
2427                     httpd_MsgClean( &cl->answer );
2428                 }
2429                 else if( cl->b_read_waiting )
2430                 {
2431                     /* we have a message waiting for us to read it */
2432                     httpd_MsgClean( &cl->answer );
2433                     httpd_MsgClean( &cl->query );
2434
2435                     cl->i_buffer = 0;
2436                     cl->i_buffer_size = 1000;
2437                     free( cl->p_buffer );
2438                     cl->p_buffer = malloc( cl->i_buffer_size );
2439                     cl->i_state = HTTPD_CLIENT_RECEIVING;
2440                     cl->b_read_waiting = VLC_FALSE;
2441                 }
2442                 else
2443                 {
2444                     int64_t i_offset = cl->answer.i_body_offset;
2445                     httpd_MsgClean( &cl->answer );
2446
2447                     cl->answer.i_body_offset = i_offset;
2448                     free( cl->p_buffer );
2449                     cl->p_buffer = NULL;
2450                     cl->i_buffer = 0;
2451                     cl->i_buffer_size = 0;
2452
2453                     cl->i_state = HTTPD_CLIENT_WAITING;
2454                 }
2455             }
2456             else if( cl->i_state == HTTPD_CLIENT_WAITING )
2457             {
2458                 int64_t i_offset = cl->answer.i_body_offset;
2459                 int     i_msg = cl->query.i_type;
2460
2461                 httpd_MsgInit( &cl->answer );
2462                 cl->answer.i_body_offset = i_offset;
2463
2464                 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2465                                           &cl->answer, &cl->query );
2466                 if( cl->answer.i_type != HTTPD_MSG_NONE )
2467                 {
2468                     /* we have new data, so reenter send mode */
2469                     cl->i_buffer      = 0;
2470                     cl->p_buffer      = cl->answer.p_body;
2471                     cl->i_buffer_size = cl->answer.i_body;
2472                     cl->answer.p_body = NULL;
2473                     cl->answer.i_body = 0;
2474                     cl->i_state = HTTPD_CLIENT_SENDING;
2475                 }
2476                 else
2477                 {
2478                     /* we shouldn't wait too long */
2479                     b_low_delay = VLC_TRUE;
2480                 }
2481             }
2482
2483             /* Special for BIDIR mode we also check reading */
2484             if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2485                 cl->i_state == HTTPD_CLIENT_SENDING )
2486             {
2487                 FD_SET( cl->fd, &fds_read );
2488                 i_handle_max = __MAX( i_handle_max, cl->fd );
2489             }
2490         }
2491         vlc_mutex_unlock( &host->lock );
2492
2493         /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2494         timeout.tv_sec = 0;
2495         timeout.tv_usec = b_low_delay ? 20000 : 100000;
2496
2497         i_ret = select( i_handle_max + 1,
2498                         &fds_read, &fds_write, NULL, &timeout );
2499
2500         if( i_ret == -1 && errno != EINTR )
2501         {
2502 #if defined(WIN32) || defined(UNDER_CE)
2503             msg_Warn( host, "cannot select sockets (%d)", WSAGetLastError( ) );
2504 #else
2505             msg_Warn( host, "cannot select sockets : %s", strerror( errno ) );
2506 #endif
2507             msleep( 1000 );
2508             continue;
2509         }
2510         else if( i_ret <= 0 )
2511         {
2512             continue;
2513         }
2514
2515         /* accept new connections */
2516         for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2517         {
2518             if( FD_ISSET( fd, &fds_read ) )
2519             {
2520                 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2521                 struct  sockaddr_storage sock;
2522     
2523                 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2524                 fprintf ( stderr, "Accepting\n");
2525                 if( fd >= 0 )
2526                 {
2527                     int i_state = 0;
2528     
2529                     /* set this new socket non-block */
2530     #if defined( WIN32 ) || defined( UNDER_CE )
2531                     {
2532                         unsigned long i_dummy = 1;
2533                         ioctlsocket( fd, FIONBIO, &i_dummy );
2534                     }
2535     #else
2536                     fcntl( fd, F_SETFL, O_NONBLOCK );
2537     #endif
2538     
2539                     if( p_tls != NULL)
2540                     {
2541                         switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2542                         {
2543                             case -1:
2544                                 msg_Err( host, "Rejecting TLS connection" );
2545                                 net_Close( fd );
2546                                 fd = -1;
2547                                 p_tls = NULL;
2548                                 break;
2549     
2550                             case 1: /* missing input - most likely */
2551                                 i_state = HTTPD_CLIENT_TLS_HS_IN;
2552                                 break;
2553
2554                             case 2: /* missing output */
2555                                 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2556                                 break;
2557                         }
2558                     }
2559
2560                     if( fd >= 0 )
2561                     {
2562                         httpd_client_t *cl;
2563                         stats_UpdateInteger( host, "client_connections", 1,
2564                                              NULL );
2565                         stats_UpdateInteger( host, "active_connections", 1,
2566                                              NULL );
2567                         cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2568                         p_tls = NULL;
2569                         vlc_mutex_lock( &host->lock );
2570                         TAB_APPEND( host->i_client, host->client, cl );
2571                         vlc_mutex_unlock( &host->lock );
2572
2573                         if( i_state != 0 )
2574                             cl->i_state = i_state; // override state for TLS
2575                     }
2576                 }
2577             }
2578         }
2579
2580         /* now try all others socket */
2581         vlc_mutex_lock( &host->lock );
2582         for( i_client = 0; i_client < host->i_client; i_client++ )
2583         {
2584             httpd_client_t *cl = host->client[i_client];
2585             if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2586             {
2587                 httpd_ClientRecv( cl );
2588             }
2589             else if( cl->i_state == HTTPD_CLIENT_SENDING )
2590             {
2591                 httpd_ClientSend( cl );
2592             }
2593             else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2594             {
2595                 httpd_ClientTlsHsIn( cl );
2596             }
2597             else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2598             {
2599                 httpd_ClientTlsHsOut( cl );
2600             }
2601
2602             if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2603                 cl->i_state == HTTPD_CLIENT_SENDING &&
2604                 FD_ISSET( cl->fd, &fds_read ) )
2605             {
2606                 cl->b_read_waiting = VLC_TRUE;
2607             }
2608         }
2609         vlc_mutex_unlock( &host->lock );
2610     }
2611
2612     if( p_tls != NULL )
2613         tls_ServerSessionClose( p_tls );
2614 }
2615
2616 #else /* ENABLE_HTTPD */
2617
2618 /* We just define an empty wrapper */
2619 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2620                                 tls_server_t *d )
2621 {
2622     msg_Err( a, "HTTP daemon support is disabled" );
2623     return 0;
2624 }
2625 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2626 {
2627     msg_Err( a, "HTTP daemon support is disabled" );
2628     return 0;
2629 }
2630 void httpd_HostDelete( httpd_host_t *a )
2631 {
2632 }
2633 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2634                            char *psz_user, char *psz_password,
2635                            const vlc_acl_t *p_acl )
2636 {
2637     return NULL;
2638 }
2639 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2640                                  char *psz_user, char *psz_password,
2641                                  const vlc_acl_t *p_acl )
2642 {
2643     return NULL;
2644 }
2645 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2646                     httpd_callback_sys_t *d ){ return 0; }
2647 void httpd_UrlDelete( httpd_url_t *a ){}
2648
2649 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip ) { return NULL; }
2650 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip ) { return NULL; }
2651
2652 void httpd_ClientModeStream( httpd_client_t *a ){}
2653 void httpd_ClientModeBidir( httpd_client_t *a ){}
2654
2655 void httpd_FileDelete( httpd_file_t *a ){}
2656 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2657                              char *e, httpd_file_callback_t f,
2658                              httpd_file_sys_t *g ){ return 0; }
2659
2660 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2661                                    const char *psz_user,
2662                                    const char *psz_password,
2663                                    const vlc_acl_t *p_acl,
2664                                    httpd_handler_callback_t pf_fill,
2665                                    httpd_handler_sys_t *p_sys )
2666 {
2667     return NULL;
2668 }
2669 void httpd_HandlerDelete( httpd_handler_t *handler ) {}
2670
2671 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2672 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2673                                      char *b, char *c ){ return 0; }
2674
2675 void httpd_StreamDelete( httpd_stream_t *a ){}
2676 int  httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2677 int  httpd_StreamSend  ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2678 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2679                                  char *d, char *e ){ return 0; }
2680
2681 void httpd_MsgInit ( httpd_message_t *a ){}
2682 void httpd_MsgAdd  ( httpd_message_t *a, char *b, char *c, ... ){}
2683 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2684 void httpd_MsgClean( httpd_message_t *a ){}
2685
2686 #endif /* ENABLE_HTTPD */