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