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