]> git.sesse.net Git - vlc/blob - src/misc/httpd.c
632e56108ec0a66587c0248356c22a2c171a97f1
[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 <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <stdlib.h>
26 #include <vlc/vlc.h>
27
28 #ifdef ENABLE_HTTPD
29
30 #include "vlc_httpd.h"
31 #include "network.h"
32 #include "vlc_tls.h"
33
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 static void httpd_HostThread( httpd_host_t * );
883 static int GetAddrPort( const struct sockaddr_storage *p_ss );
884
885 #ifndef HAVE_GETADDRINFO
886 struct httpd_addrinfo
887 {
888     int ai_family;
889     int ai_socktype;
890     int ai_protocol;
891     /*int ai_flags;*/
892     struct sockaddr *ai_addr;
893     int ai_addrlen;
894     struct httpd_addrinfo *ai_next;
895 };
896 #   define addrinfo httpd_addrinfo
897
898 static int BuildAddr( struct sockaddr_in * p_socket,
899                       const char * psz_address, int i_port );
900 #endif
901
902
903 /* create a new host */
904 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host,
905                              int i_port )
906 {
907     return httpd_TLSHostNew( p_this, psz_host, i_port, NULL );
908 }
909
910 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, char *psz_host,
911                                 int i_port, tls_server_t *p_tls )
912 {
913     httpd_t      *httpd;
914     httpd_host_t *host = NULL;
915     vlc_value_t lockval;
916     int fd = -1;
917     struct addrinfo *res, *ptr;
918
919     /* resolv */
920 #ifdef HAVE_GETADDRINFO
921     {
922         vlc_value_t val;
923         char psz_port[6];
924         struct addrinfo hints;
925         int check;
926
927         memset( &hints, 0, sizeof( hints ) );
928
929         /* Check if ipv4 or ipv6 were forced */
930         var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
931         var_Get( p_this, "ipv4", &val );
932         if( val.b_bool )
933             hints.ai_family = PF_INET;
934
935         var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
936         var_Get( p_this, "ipv6", &val );
937         if( val.b_bool )
938             hints.ai_family = PF_INET6;
939
940         hints.ai_socktype = SOCK_STREAM;
941         hints.ai_flags = AI_PASSIVE;
942
943         if (*psz_host == '\0')
944             psz_host = NULL;
945
946         snprintf( psz_port, sizeof( psz_port ), "%d", i_port );
947         psz_port[sizeof( psz_port ) - 1] = '\0';
948         
949         check = getaddrinfo( psz_host, psz_port, &hints, &res );
950         if( check != 0 )
951         {
952 #ifdef HAVE_GAI_STRERROR
953             msg_Err( p_this, "cannot resolve %s:%d : %s", psz_host, i_port,
954                      gai_strerror( check ) );
955 #else
956             msg_Err( p_this, "cannot resolve %s:%d", psz_host, i_port );
957 #endif
958             return NULL;
959         }
960     }
961
962 #else
963     struct sockaddr_in sock;
964     struct httpd_addrinfo info;
965     
966     info.ai_family = PF_INET;
967     info.ai_socktype = SOCK_STREAM;
968     info.ai_protocol = 0;
969     info.ai_addr = (struct sockaddr *)&sock;
970     info.ai_addrlen = sizeof( sock );
971     info.ai_next = NULL;
972     
973     res = &info;
974
975     if( BuildAddr( &sock, psz_host, i_port ) )
976     {
977         msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
978         return NULL;
979     }
980
981 #   define freeaddrinfo( r ) (void)0;
982 #endif
983
984     /* to be sure to avoid multiple creation */
985     var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
986     var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
987     vlc_mutex_lock( lockval.p_address );
988
989     if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
990     {
991         msg_Info( p_this, "creating httpd" );
992         if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
993         {
994             vlc_mutex_unlock( lockval.p_address );
995             freeaddrinfo( res );
996             return NULL;
997         }
998
999         httpd->i_host = 0;
1000         httpd->host   = NULL;
1001
1002         vlc_object_yield( httpd );
1003         vlc_object_attach( httpd, p_this->p_vlc );
1004     }
1005
1006     for( ptr = res; (ptr != NULL) && (fd == -1); ptr = ptr->ai_next )
1007     {
1008         int i;
1009
1010         if( ((unsigned)ptr->ai_addrlen) > sizeof( struct sockaddr_storage ) )
1011         {
1012             msg_Dbg( p_this, "socket address too big" );
1013             continue;
1014         }
1015
1016         /* verify if it already exist */
1017         for( i = 0; i < httpd->i_host; i++ )
1018         {
1019             if( GetAddrPort (&httpd->host[i]->sock) != i_port )
1020                 continue;
1021
1022             /* Cannot re-use host if it uses TLS/SSL */
1023             if( httpd->host[i]->p_tls != NULL )
1024                 continue;
1025
1026 #ifdef AF_INET6
1027             if( httpd->host[i]->sock.ss_family == AF_INET6 )
1028             {
1029                 const struct sockaddr_in6 *p_hsock, *p_sock;
1030
1031                 p_hsock = (const struct sockaddr_in6 *)&httpd->host[i]->sock;
1032                 p_sock = (const struct sockaddr_in6 *)ptr->ai_addr;
1033
1034                 if( memcmp( &p_hsock->sin6_addr, &in6addr_any,
1035                             sizeof( struct in6_addr ) ) &&
1036                             ( p_sock->sin6_family != AF_INET6 ||
1037                               memcmp( &p_hsock->sin6_addr, &p_sock->sin6_addr,
1038                                       sizeof( struct in6_addr ) ) ) )
1039                     continue; /* does not match */
1040             }
1041             else if( ptr->ai_family == PF_INET6 )
1042                 continue;
1043             else
1044 #endif
1045             if( httpd->host[i]->sock.ss_family == AF_INET )
1046             {
1047                 const struct sockaddr_in *p_hsock, *p_sock;
1048
1049                 p_hsock = (const struct sockaddr_in *)&httpd->host[i]->sock;
1050                 p_sock = (const struct sockaddr_in *)ptr->ai_addr;
1051
1052                 if( p_hsock->sin_addr.s_addr != INADDR_ANY &&
1053                     ( p_sock->sin_family != AF_INET ||
1054                       p_hsock->sin_addr.s_addr != p_sock->sin_addr.s_addr ) )
1055                     continue; /* does not match */
1056             }
1057             else if( ptr->ai_family == PF_INET )
1058                 continue;
1059             else
1060             {
1061                 msg_Dbg( p_this, "host with unknown address family" );
1062                 continue;
1063             }
1064
1065             freeaddrinfo( res );
1066
1067             /* yep found */
1068             host = httpd->host[i];
1069             host->i_ref++;
1070
1071             vlc_mutex_unlock( lockval.p_address );
1072
1073             msg_Dbg( p_this, "host already registered" );
1074             return host;
1075         }
1076
1077         /* create the listening socket */
1078         fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
1079         if( fd == -1 )
1080             continue;
1081
1082         /* reuse socket */
1083         {
1084             int dummy = 1;
1085             if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
1086                             (void *)&dummy, sizeof( dummy ) ) < 0 )
1087             {
1088                 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
1089             }
1090         }
1091
1092         /* bind it */
1093         if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
1094         {
1095             msg_Err( p_this, "cannot bind socket" );
1096             goto socket_error;
1097         }
1098         /* set to non-blocking */
1099 #if defined( WIN32 ) || defined( UNDER_CE )
1100         {
1101             unsigned long i_dummy = 1;
1102             if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
1103             {
1104                 msg_Err( p_this, "cannot set socket to non-blocking mode" );
1105                 goto socket_error;
1106             }
1107         }
1108 #else
1109         {
1110             unsigned int i_flags;
1111             if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
1112             {
1113                 msg_Err( p_this, "cannot F_GETFL socket" );
1114                 goto socket_error;
1115             }
1116             if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
1117             {
1118                 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
1119                 goto socket_error;
1120             }
1121         }
1122 #endif
1123         /* listen */
1124         if( listen( fd, LISTEN_BACKLOG ) < 0 )
1125         {
1126             msg_Err( p_this, "cannot listen socket" );
1127             goto socket_error;
1128         }
1129
1130         break; // success
1131
1132 socket_error:
1133         net_Close( fd );
1134         fd = -1;
1135     }
1136
1137
1138     if( fd == -1 )
1139     {
1140         freeaddrinfo( res );
1141         goto error;
1142     }
1143
1144     /* create the new host */
1145     host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1146     host->httpd = httpd;
1147     vlc_mutex_init( httpd, &host->lock );
1148     host->i_ref = 1;
1149     host->fd = fd;
1150
1151     memcpy( &host->sock, ptr->ai_addr, ptr->ai_addrlen );
1152     host->i_sock_size = ptr->ai_addrlen;
1153     host->i_url     = 0;
1154     host->url       = NULL;
1155     host->i_client  = 0;
1156     host->client    = NULL;
1157
1158     freeaddrinfo( res );
1159     host->p_tls = p_tls;
1160
1161     /* create the thread */
1162     if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1163                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1164     {
1165         msg_Err( p_this, "cannot spawn http host thread" );
1166         goto error;
1167     }
1168
1169     /* now add it to httpd */
1170     TAB_APPEND( httpd->i_host, httpd->host, host );
1171     vlc_mutex_unlock( lockval.p_address );
1172
1173     return host;
1174
1175 error:
1176     vlc_mutex_unlock( lockval.p_address );
1177
1178     if( fd != -1 )
1179         net_Close( fd );
1180
1181     if( host != NULL )
1182     {
1183         vlc_mutex_destroy( &host->lock );
1184         vlc_object_destroy( host );
1185     }
1186
1187     /* TODO destroy no more used httpd TODO */
1188     vlc_object_release( httpd );
1189     return NULL;
1190 }
1191
1192 /* delete a host */
1193 void httpd_HostDelete( httpd_host_t *host )
1194 {
1195     httpd_t *httpd = host->httpd;
1196     vlc_value_t lockval;
1197     int i;
1198
1199     msg_Dbg( host, "httpd_HostDelete" );
1200
1201     var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1202     vlc_mutex_lock( lockval.p_address );
1203
1204     vlc_object_release( httpd );
1205
1206     host->i_ref--;
1207     if( host->i_ref > 0 )
1208     {
1209         /* still used */
1210         vlc_mutex_unlock( lockval.p_address );
1211         msg_Dbg( host, "httpd_HostDelete: host still used" );
1212         return;
1213     }
1214     TAB_REMOVE( httpd->i_host, httpd->host, host );
1215
1216     msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1217
1218     host->b_die = 1;
1219     vlc_thread_join( host );
1220
1221     msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1222
1223     for( i = 0; i < host->i_url; i++ )
1224     {
1225         msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1226     }
1227     for( i = 0; i < host->i_client; i++ )
1228     {
1229         httpd_client_t *cl = host->client[i];
1230         msg_Warn( host, "client still connected" );
1231         httpd_ClientClean( cl );
1232         TAB_REMOVE( host->i_client, host->client, cl );
1233         free( cl );
1234         i--;
1235         /* TODO */
1236     }
1237
1238     if( host->p_tls != NULL)
1239         tls_ServerDelete( host->p_tls );
1240     net_Close( host->fd );
1241     vlc_mutex_destroy( &host->lock );
1242     vlc_object_destroy( host );
1243
1244     if( httpd->i_host <= 0 )
1245     {
1246         msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1247         vlc_object_detach( httpd );
1248         vlc_object_destroy( httpd );
1249     }
1250     vlc_mutex_unlock( lockval.p_address );
1251 }
1252
1253 /* register a new url */
1254 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1255                                          char *psz_user, char *psz_password,
1256                                          vlc_bool_t b_check )
1257 {
1258     httpd_url_t *url;
1259     int         i;
1260
1261     vlc_mutex_lock( &host->lock );
1262     if( b_check )
1263     {
1264         for( i = 0; i < host->i_url; i++ )
1265         {
1266             if( !strcmp( psz_url, host->url[i]->psz_url ) )
1267             {
1268                 msg_Warn( host->httpd,
1269                           "cannot add '%s' (url already defined)", psz_url );
1270                 vlc_mutex_unlock( &host->lock );
1271                 return NULL;
1272             }
1273         }
1274     }
1275
1276     url = malloc( sizeof( httpd_url_t ) );
1277     url->host = host;
1278
1279     vlc_mutex_init( host->httpd, &url->lock );
1280     url->psz_url = strdup( psz_url );
1281     url->psz_user = strdup( psz_user ? psz_user : "" );
1282     url->psz_password = strdup( psz_password ? psz_password : "" );
1283     for( i = 0; i < HTTPD_MSG_MAX; i++ )
1284     {
1285         url->catch[i].cb = NULL;
1286         url->catch[i].p_sys = NULL;
1287     }
1288
1289     TAB_APPEND( host->i_url, host->url, url );
1290     vlc_mutex_unlock( &host->lock );
1291
1292     return url;
1293 }
1294
1295 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1296                            char *psz_user, char *psz_password )
1297 {
1298     return httpd_UrlNewPrivate( host, psz_url, psz_user,
1299                                 psz_password, VLC_FALSE );
1300 }
1301
1302 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1303                                  char *psz_user, char *psz_password )
1304 {
1305     return httpd_UrlNewPrivate( host, psz_url, psz_user,
1306                                 psz_password, VLC_TRUE );
1307 }
1308
1309 /* register callback on a url */
1310 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1311                     httpd_callback_sys_t *p_sys )
1312 {
1313     vlc_mutex_lock( &url->lock );
1314     url->catch[i_msg].cb   = cb;
1315     url->catch[i_msg].p_sys= p_sys;
1316     vlc_mutex_unlock( &url->lock );
1317
1318     return VLC_SUCCESS;
1319 }
1320
1321
1322 /* delete an url */
1323 void httpd_UrlDelete( httpd_url_t *url )
1324 {
1325     httpd_host_t *host = url->host;
1326     int          i;
1327
1328     vlc_mutex_lock( &host->lock );
1329     TAB_REMOVE( host->i_url, host->url, url );
1330
1331     vlc_mutex_destroy( &url->lock );
1332     free( url->psz_url );
1333     free( url->psz_user );
1334     free( url->psz_password );
1335
1336     for( i = 0; i < host->i_client; i++ )
1337     {
1338         httpd_client_t *client = host->client[i];
1339
1340         if( client->url == url )
1341         {
1342             /* TODO complete it */
1343             msg_Warn( host, "force closing connections" );
1344             httpd_ClientClean( client );
1345             TAB_REMOVE( host->i_client, host->client, client );
1346             free( client );
1347             i--;
1348         }
1349     }
1350     free( url );
1351     vlc_mutex_unlock( &host->lock );
1352 }
1353
1354 void httpd_MsgInit( httpd_message_t *msg )
1355 {
1356     msg->cl         = NULL;
1357     msg->i_type     = HTTPD_MSG_NONE;
1358     msg->i_proto    = HTTPD_PROTO_NONE;
1359     msg->i_version  = -1;
1360
1361     msg->i_status   = 0;
1362     msg->psz_status = NULL;
1363
1364     msg->psz_url = NULL;
1365     msg->psz_args = NULL;
1366
1367     msg->i_channel = -1;
1368
1369     msg->i_name = 0;
1370     msg->name   = NULL;
1371     msg->i_value= 0;
1372     msg->value  = NULL;
1373
1374     msg->i_body_offset = 0;
1375     msg->i_body        = 0;
1376     msg->p_body        = 0;
1377 }
1378
1379 void httpd_MsgClean( httpd_message_t *msg )
1380 {
1381     int i;
1382
1383     if( msg->psz_status )
1384     {
1385         free( msg->psz_status );
1386     }
1387     if( msg->psz_url )
1388     {
1389         free( msg->psz_url );
1390     }
1391     if( msg->psz_args )
1392     {
1393         free( msg->psz_args );
1394     }
1395     for( i = 0; i < msg->i_name; i++ )
1396     {
1397         free( msg->name[i] );
1398         free( msg->value[i] );
1399     }
1400     if( msg->name )
1401     {
1402         free( msg->name );
1403     }
1404     if( msg->value )
1405     {
1406         free( msg->value );
1407     }
1408     if( msg->p_body )
1409     {
1410         free( msg->p_body );
1411     }
1412     httpd_MsgInit( msg );
1413 }
1414
1415 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1416 {
1417     int i;
1418
1419     for( i = 0; i < msg->i_name; i++ )
1420     {
1421         if( !strcasecmp( msg->name[i], name ))
1422         {
1423             return msg->value[i];
1424         }
1425     }
1426     return "";
1427 }
1428 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1429 {
1430     va_list args;
1431     char *value = NULL;
1432
1433     va_start( args, psz_value );
1434 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1435     vasprintf( &value, psz_value, args );
1436 #else
1437     {
1438         int i_size = strlen( psz_value ) + 4096;    /* FIXME stupid system */
1439         value = calloc( i_size, sizeof( char ) );
1440         vsnprintf( value, i_size, psz_value, args );
1441         value[i_size - 1] = 0;
1442     }
1443 #endif
1444     va_end( args );
1445
1446     name = strdup( name );
1447
1448     TAB_APPEND( msg->i_name,  msg->name,  name );
1449     TAB_APPEND( msg->i_value, msg->value, value );
1450 }
1451
1452 static void httpd_ClientInit( httpd_client_t *cl )
1453 {
1454     cl->i_state = HTTPD_CLIENT_RECEIVING;
1455     cl->i_activity_date = mdate();
1456     cl->i_activity_timeout = I64C(10000000);
1457     cl->i_buffer_size = 10000;
1458     cl->i_buffer = 0;
1459     cl->p_buffer = malloc( cl->i_buffer_size );
1460     cl->i_mode   = HTTPD_CLIENT_FILE;
1461     cl->b_read_waiting = VLC_FALSE;
1462
1463     httpd_MsgInit( &cl->query );
1464     httpd_MsgInit( &cl->answer );
1465 }
1466
1467 void httpd_ClientModeStream( httpd_client_t *cl )
1468 {
1469     cl->i_mode   = HTTPD_CLIENT_STREAM;
1470 }
1471
1472 void httpd_ClientModeBidir( httpd_client_t *cl )
1473 {
1474     cl->i_mode   = HTTPD_CLIENT_BIDIR;
1475 }
1476
1477 char* httpd_ClientIP( httpd_client_t *cl )
1478 {
1479 #ifdef HAVE_GETNAMEINFO
1480     char sz_ip[INET6_ADDRSTRLEN + 2];
1481     int i;
1482
1483     if( (cl->sock.ss_family == AF_INET6) &&
1484         IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1485                               &cl->sock)->sin6_addr) )
1486     {
1487         /* If client is using IPv4 but server is using IPv6 */
1488         struct sockaddr_in a;
1489         
1490         memset( &a, 0, sizeof( a ) );
1491         a.sin_family = AF_INET;
1492         a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1493         a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1494                             &cl->sock)->sin6_addr)[3];
1495         i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1496                          &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1497     }
1498     else
1499         i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1500                          &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1501                          NI_NUMERICHOST );
1502
1503     if( i != 0 )
1504         /* FIXME: msg_Err */
1505         return NULL;
1506         
1507     if( strchr( &sz_ip[1], ':' ) != NULL )
1508     {
1509         *sz_ip = '[';
1510         i = strlen( sz_ip );
1511         sz_ip[i++] = ']';
1512         sz_ip[i] = '\0';
1513        
1514         return strdup( sz_ip );
1515     }
1516     
1517     return strdup( &sz_ip[1] );
1518
1519 #else
1520     /* FIXME not thread safe */
1521     return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1522 #endif
1523 }
1524
1525 static void httpd_ClientClean( httpd_client_t *cl )
1526 {
1527     if( cl->fd >= 0 )
1528     {
1529         if( cl->p_tls != NULL )
1530             tls_ServerSessionClose( cl->p_tls );
1531         net_Close( cl->fd );
1532         cl->fd = -1;
1533     }
1534
1535     httpd_MsgClean( &cl->answer );
1536     httpd_MsgClean( &cl->query );
1537
1538     if( cl->p_buffer )
1539     {
1540         free( cl->p_buffer );
1541         cl->p_buffer = NULL;
1542     }
1543 }
1544
1545 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1546                                         int i_sock_size,
1547                                         tls_session_t *p_tls )
1548 {
1549     httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1550     cl->i_ref   = 0;
1551     cl->fd      = fd;
1552     memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1553     cl->i_sock_size = i_sock_size;
1554     cl->url     = NULL;
1555     cl->p_tls = p_tls;
1556
1557     httpd_ClientInit( cl );
1558
1559     return cl;
1560 }
1561
1562
1563 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1564 {
1565     tls_session_t *p_tls;
1566     
1567     p_tls = cl->p_tls;
1568     if( p_tls != NULL)
1569         return tls_Recv( p_tls, p, i_len );
1570
1571     return recv( cl->fd, p, i_len, 0 );
1572 }
1573
1574
1575 static int httpd_NetSend( httpd_client_t *cl, const 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_Send( p_tls, p, i_len );
1582
1583     return send( cl->fd, p, i_len, 0 );
1584 }
1585
1586
1587 static void httpd_ClientRecv( httpd_client_t *cl )
1588 {
1589     int i_len;
1590
1591     if( cl->query.i_proto == HTTPD_PROTO_NONE )
1592     {
1593         /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1594         i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1595                                4 - cl->i_buffer );
1596         if( i_len > 0 )
1597         {
1598             cl->i_buffer += i_len;
1599         }
1600
1601         if( cl->i_buffer >= 4 )
1602         {
1603             /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1604             /* detect type */
1605             if( cl->p_buffer[0] == '$' )
1606             {
1607                 /* RTSP (rtp over rtsp) */
1608                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1609                 cl->query.i_type  = HTTPD_MSG_CHANNEL;
1610                 cl->query.i_channel = cl->p_buffer[1];
1611                 cl->query.i_body  = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1612                 cl->query.p_body  = malloc( cl->query.i_body );
1613
1614                 cl->i_buffer      = 0;
1615             }
1616             else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1617             {
1618                 cl->query.i_proto = HTTPD_PROTO_HTTP;
1619                 cl->query.i_type  = HTTPD_MSG_ANSWER;
1620             }
1621             else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1622             {
1623                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1624                 cl->query.i_type  = HTTPD_MSG_ANSWER;
1625             }
1626             else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1627                      !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1628                      !strncmp( cl->p_buffer, "POST", 4 ) )
1629             {
1630                 cl->query.i_proto = HTTPD_PROTO_HTTP;
1631                 cl->query.i_type  = HTTPD_MSG_NONE;
1632             }
1633             else
1634             {
1635                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1636                 cl->query.i_type  = HTTPD_MSG_NONE;
1637             }
1638         }
1639     }
1640     else if( cl->query.i_body > 0 )
1641     {
1642         /* we are reading the body of a request or a channel */
1643         i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1644                                cl->query.i_body - cl->i_buffer );
1645         if( i_len > 0 )
1646         {
1647             cl->i_buffer += i_len;
1648         }
1649         if( cl->i_buffer >= cl->query.i_body )
1650         {
1651             cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1652         }
1653     }
1654     else
1655     {
1656         /* we are reading a header -> char by char */
1657         for( ;; )
1658         {
1659             i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1660             if( i_len <= 0 )
1661             {
1662                 break;
1663             }
1664             cl->i_buffer++;
1665
1666             if( cl->i_buffer + 1 >= cl->i_buffer_size )
1667             {
1668                 cl->i_buffer_size += 1024;
1669                 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1670             }
1671             if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1672                 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1673             {
1674                 char *p;
1675
1676                 /* we have finished the header so parse it and set i_body */
1677                 cl->p_buffer[cl->i_buffer] = '\0';
1678
1679                 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1680                 {
1681                     cl->query.i_status =
1682                         strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1683                     while( *p == ' ' )
1684                     {
1685                         p++;
1686                     }
1687                     cl->query.psz_status = strdup( p );
1688                 }
1689                 else
1690                 {
1691                     static const struct
1692                     {
1693                         char *name;
1694                         int  i_type;
1695                         int  i_proto;
1696                     }
1697                     msg_type[] =
1698                     {
1699                         { "GET",        HTTPD_MSG_GET,  HTTPD_PROTO_HTTP },
1700                         { "HEAD",       HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1701                         { "POST",       HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1702
1703                         { "OPTIONS",    HTTPD_MSG_OPTIONS,  HTTPD_PROTO_RTSP },
1704                         { "DESCRIBE",   HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1705                         { "SETUP",      HTTPD_MSG_SETUP,    HTTPD_PROTO_RTSP },
1706                         { "PLAY",       HTTPD_MSG_PLAY,     HTTPD_PROTO_RTSP },
1707                         { "PAUSE",      HTTPD_MSG_PAUSE,    HTTPD_PROTO_RTSP },
1708                         { "TEARDOWN",   HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1709
1710                         { NULL,         HTTPD_MSG_NONE,     HTTPD_PROTO_NONE }
1711                     };
1712                     int  i;
1713
1714                     p = NULL;
1715                     cl->query.i_type = HTTPD_MSG_NONE;
1716
1717                     /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1718
1719                     for( i = 0; msg_type[i].name != NULL; i++ )
1720                     {
1721                         if( !strncmp( cl->p_buffer, msg_type[i].name,
1722                                       strlen( msg_type[i].name ) ) )
1723                         {
1724                             p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1725                             cl->query.i_type = msg_type[i].i_type;
1726                             if( cl->query.i_proto != msg_type[i].i_proto )
1727                             {
1728                                 p = NULL;
1729                                 cl->query.i_proto = HTTPD_PROTO_NONE;
1730                                 cl->query.i_type = HTTPD_MSG_NONE;
1731                             }
1732                             break;
1733                         }
1734                     }
1735                     if( p == NULL )
1736                     {
1737                         if( strstr( cl->p_buffer, "HTTP/1." ) )
1738                         {
1739                             cl->query.i_proto = HTTPD_PROTO_HTTP;
1740                         }
1741                         else if( strstr( cl->p_buffer, "RTSP/1." ) )
1742                         {
1743                             cl->query.i_proto = HTTPD_PROTO_RTSP;
1744                         }
1745                     }
1746                     else
1747                     {
1748                         char *p2;
1749                         char *p3;
1750
1751                         while( *p == ' ' )
1752                         {
1753                             p++;
1754                         }
1755                         p2 = strchr( p, ' ' );
1756                         if( p2 )
1757                         {
1758                             *p2++ = '\0';
1759                         }
1760                         if( !strncasecmp( p, "rtsp:", 5 ) )
1761                         {
1762                             /* for rtsp url, you have rtsp://localhost:port/path */
1763                             p += 5;
1764                             while( *p == '/' ) p++;
1765                             while( *p && *p != '/' ) p++;
1766                         }
1767                         cl->query.psz_url = strdup( p );
1768                         if( ( p3 = strchr( cl->query.psz_url, '?' ) )  )
1769                         {
1770                             *p3++ = '\0';
1771                             cl->query.psz_args = strdup( p3 );
1772                         }
1773                         if( p2 )
1774                         {
1775                             while( *p2 == ' ' )
1776                             {
1777                                 p2++;
1778                             }
1779                             if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1780                             {
1781                                 cl->query.i_proto = HTTPD_PROTO_HTTP;
1782                                 cl->query.i_version = atoi( p2+7 );
1783                             }
1784                             else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1785                             {
1786                                 cl->query.i_proto = HTTPD_PROTO_RTSP;
1787                                 cl->query.i_version = atoi( p2+7 );
1788                             }
1789                         }
1790                         p = p2;
1791                     }
1792                 }
1793                 if( p )
1794                 {
1795                     p = strchr( p, '\n' );
1796                 }
1797                 if( p )
1798                 {
1799                     while( *p == '\n' || *p == '\r' )
1800                     {
1801                         p++;
1802                     }
1803                     while( p && *p != '\0' )
1804                     {
1805                         char *line = p;
1806                         char *eol = p = strchr( p, '\n' );
1807                         char *colon;
1808
1809                         while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1810                         {
1811                             *eol-- = '\0';
1812                         }
1813
1814                         if( ( colon = strchr( line, ':' ) ) )
1815                         {
1816                             char *name;
1817                             char *value;
1818
1819                             *colon++ = '\0';
1820                             while( *colon == ' ' )
1821                             {
1822                                 colon++;
1823                             }
1824                             name = strdup( line );
1825                             value = strdup( colon );
1826
1827                             TAB_APPEND( cl->query.i_name, cl->query.name, name );
1828                             TAB_APPEND( cl->query.i_value,cl->query.value,value);
1829
1830                             if( !strcasecmp( name, "Content-Length" ) )
1831                             {
1832                                 cl->query.i_body = atol( value );
1833                             }
1834                         }
1835
1836                         if( p )
1837                         {
1838                             p++;
1839                             while( *p == '\n' || *p == '\r' )
1840                             {
1841                                 p++;
1842                             }
1843                         }
1844                     }
1845                 }
1846                 if( cl->query.i_body > 0 )
1847                 {
1848                     /* TODO Mhh, handle the case client will only send a request and close the connection
1849                      * to mark and of body (probably only RTSP) */
1850                     cl->query.p_body = malloc( cl->query.i_body );
1851                     cl->i_buffer = 0;
1852                 }
1853                 else
1854                 {
1855                     cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1856                 }
1857             }
1858         }
1859     }
1860
1861     /* check if the client is to be set to dead */
1862 #if defined( WIN32 ) || defined( UNDER_CE )
1863     if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1864 #else
1865     if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1866 #endif
1867     {
1868         if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1869         {
1870             /* connection closed -> end of data */
1871             if( cl->query.i_body > 0 )
1872             {
1873                 cl->query.i_body = cl->i_buffer;
1874             }
1875             cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1876         }
1877         else
1878         {
1879             cl->i_state = HTTPD_CLIENT_DEAD;
1880         }
1881     }
1882     cl->i_activity_date = mdate();
1883
1884     /* XXX: for QT I have to disable timeout. Try to find why */
1885     if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1886         cl->i_activity_timeout = 0;
1887
1888     /* Debugging only */
1889     /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1890     {
1891         int i;
1892
1893         fprintf( stderr, "received new request\n" );
1894         fprintf( stderr, "  - proto=%s\n",
1895                  cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1896         fprintf( stderr, "  - version=%d\n", cl->query.i_version );
1897         fprintf( stderr, "  - msg=%d\n", cl->query.i_type );
1898         if( cl->query.i_type == HTTPD_MSG_ANSWER )
1899         {
1900             fprintf( stderr, "  - answer=%d '%s'\n", cl->query.i_status,
1901                      cl->query.psz_status );
1902         }
1903         else if( cl->query.i_type != HTTPD_MSG_NONE )
1904         {
1905             fprintf( stderr, "  - url=%s\n", cl->query.psz_url );
1906         }
1907         for( i = 0; i < cl->query.i_name; i++ )
1908         {
1909             fprintf( stderr, "  - option name='%s' value='%s'\n",
1910                      cl->query.name[i], cl->query.value[i] );
1911         }
1912     }*/
1913 }
1914
1915
1916 static void httpd_ClientSend( httpd_client_t *cl )
1917 {
1918     int i;
1919     int i_len;
1920
1921     if( cl->i_buffer < 0 )
1922     {
1923         /* We need to create the header */
1924         int i_size = 0;
1925         char *p;
1926
1927         i_size = strlen( "HTTP/1.") + 10 + 10 +
1928                  strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1929         for( i = 0; i < cl->answer.i_name; i++ )
1930         {
1931             i_size += strlen( cl->answer.name[i] ) + 2 +
1932                       strlen( cl->answer.value[i] ) + 2;
1933         }
1934
1935         if( cl->i_buffer_size < i_size )
1936         {
1937             cl->i_buffer_size = i_size;
1938             free( cl->p_buffer );
1939             cl->p_buffer = malloc( i_size );
1940         }
1941         p = cl->p_buffer;
1942
1943         p += sprintf( p, "%s/1.%d %d %s\r\n",
1944                       cl->answer.i_proto ==  HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1945                       cl->answer.i_version,
1946                       cl->answer.i_status, cl->answer.psz_status );
1947         for( i = 0; i < cl->answer.i_name; i++ )
1948         {
1949             p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1950                           cl->answer.value[i] );
1951         }
1952         p += sprintf( p, "\r\n" );
1953
1954         cl->i_buffer = 0;
1955         cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1956
1957         /*fprintf( stderr, "sending answer\n" );
1958         fprintf( stderr, "%s",  cl->p_buffer );*/
1959     }
1960
1961     i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1962                            cl->i_buffer_size - cl->i_buffer );
1963     if( i_len > 0 )
1964     {
1965         cl->i_activity_date = mdate();
1966         cl->i_buffer += i_len;
1967
1968         if( cl->i_buffer >= cl->i_buffer_size )
1969         {
1970             if( cl->answer.i_body == 0  && cl->answer.i_body_offset > 0 &&
1971                 !cl->b_read_waiting )
1972             {
1973                 /* catch more body data */
1974                 int     i_msg = cl->query.i_type;
1975                 int64_t i_offset = cl->answer.i_body_offset;
1976
1977                 httpd_MsgClean( &cl->answer );
1978                 cl->answer.i_body_offset = i_offset;
1979
1980                 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1981                                           &cl->answer, &cl->query );
1982             }
1983
1984             if( cl->answer.i_body > 0 )
1985             {
1986                 /* send the body data */
1987                 free( cl->p_buffer );
1988                 cl->p_buffer = cl->answer.p_body;
1989                 cl->i_buffer_size = cl->answer.i_body;
1990                 cl->i_buffer = 0;
1991
1992                 cl->answer.i_body = 0;
1993                 cl->answer.p_body = NULL;
1994             }
1995             else
1996             {
1997                 /* send finished */
1998                 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1999             }
2000         }
2001     }
2002     else
2003     {
2004 #if defined( WIN32 ) || defined( UNDER_CE )
2005         if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2006 #else
2007         if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2008 #endif
2009         {
2010             /* error */
2011             cl->i_state = HTTPD_CLIENT_DEAD;
2012         }
2013     }
2014 }
2015
2016 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
2017 {
2018     switch( tls_SessionContinueHandshake( cl->p_tls ) )
2019     {
2020         case 0:
2021             cl->i_state = HTTPD_CLIENT_RECEIVING;
2022             break;
2023
2024         case -1:
2025             cl->i_state = HTTPD_CLIENT_DEAD;
2026             cl->p_tls = NULL;
2027             break;
2028
2029         case 2:
2030             cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2031     }
2032 }
2033
2034 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2035 {
2036     switch( tls_SessionContinueHandshake( cl->p_tls ) )
2037     {
2038         case 0:
2039             cl->i_state = HTTPD_CLIENT_RECEIVING;
2040             break;
2041
2042         case -1:
2043             cl->i_state = HTTPD_CLIENT_DEAD;
2044             cl->p_tls = NULL;
2045             break;
2046
2047         case 1:
2048             cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2049             break;
2050     }
2051 }
2052
2053 static void httpd_HostThread( httpd_host_t *host )
2054 {
2055     tls_session_t *p_tls = NULL;
2056
2057     while( !host->b_die )
2058     {
2059         struct timeval  timeout;
2060         fd_set          fds_read;
2061         fd_set          fds_write;
2062         int             i_handle_max = 0;
2063         int             i_ret;
2064         int             i_client;
2065         int             b_low_delay = 0;
2066
2067         if( host->i_url <= 0 )
2068         {
2069             /* 0.2s */
2070             msleep( 200000 );
2071             continue;
2072         }
2073
2074         /* built a set of handle to select */
2075         FD_ZERO( &fds_read );
2076         FD_ZERO( &fds_write );
2077
2078         FD_SET( host->fd, &fds_read );
2079         i_handle_max = host->fd;
2080
2081         /* prepare a new TLS session */
2082         if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2083             p_tls = tls_ServerSessionPrepare( host->p_tls );
2084
2085         /* add all socket that should be read/write and close dead connection */
2086         vlc_mutex_lock( &host->lock );
2087         for( i_client = 0; i_client < host->i_client; i_client++ )
2088         {
2089             httpd_client_t *cl = host->client[i_client];
2090
2091             if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2092                 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2093                   ( cl->i_activity_timeout > 0 &&
2094                     cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2095             {
2096                 char *ip;
2097
2098                 // FIXME: it sucks to allocate memory on the stack for debug
2099                 ip = httpd_ClientIP( cl );
2100                 msg_Dbg( host, "connection closed(%s)",
2101                          (ip != NULL) ? ip : "unknown" );
2102                 free( ip );
2103
2104                 httpd_ClientClean( cl );
2105                 TAB_REMOVE( host->i_client, host->client, cl );
2106                 free( cl );
2107                 i_client--;
2108                 continue;
2109             }
2110             else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2111                   || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2112             {
2113                 FD_SET( cl->fd, &fds_read );
2114                 i_handle_max = __MAX( i_handle_max, cl->fd );
2115             }
2116             else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2117                   || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2118             {
2119                 FD_SET( cl->fd, &fds_write );
2120                 i_handle_max = __MAX( i_handle_max, cl->fd );
2121             }
2122             else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2123             {
2124                 httpd_message_t *answer = &cl->answer;
2125                 httpd_message_t *query  = &cl->query;
2126                 int i_msg = query->i_type;
2127
2128                 httpd_MsgInit( answer );
2129
2130                 /* Handle what we received */
2131                 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2132                     (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2133                 {
2134                     /* we can only receive request from client when not
2135                      * in BIDIR mode */
2136                     cl->url     = NULL;
2137                     cl->i_state = HTTPD_CLIENT_DEAD;
2138                 }
2139                 else if( i_msg == HTTPD_MSG_ANSWER )
2140                 {
2141                     /* We are in BIDIR mode, trigger the callback and then
2142                      * check for new data */
2143                     if( cl->url && cl->url->catch[i_msg].cb )
2144                     {
2145                         cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2146                                                   cl, NULL, query );
2147                     }
2148                     cl->i_state = HTTPD_CLIENT_WAITING;
2149                 }
2150                 else if( i_msg == HTTPD_MSG_CHANNEL )
2151                 {
2152                     /* We are in BIDIR mode, trigger the callback and then
2153                      * check for new data */
2154                     if( cl->url && cl->url->catch[i_msg].cb )
2155                     {
2156                         cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2157                                                   cl, NULL, query );
2158                     }
2159                     cl->i_state = HTTPD_CLIENT_WAITING;
2160                 }
2161                 else if( i_msg == HTTPD_MSG_OPTIONS )
2162                 {
2163                     int i_cseq;
2164
2165                     /* unimplemented */
2166                     answer->i_proto  = query->i_proto ;
2167                     answer->i_type   = HTTPD_MSG_ANSWER;
2168                     answer->i_version= 0;
2169                     answer->i_status = 200;
2170                     answer->psz_status = strdup( "Ok" );
2171
2172                     answer->i_body = 0;
2173                     answer->p_body = NULL;
2174
2175                     i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2176                     httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2177                     httpd_MsgAdd( answer, "Server", "VLC Server" );
2178                     httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2179                                  "TEARDOWN, PLAY, PAUSE" );
2180                     httpd_MsgAdd( answer, "Content-Length", "%d",
2181                                   answer->i_body );
2182
2183                     cl->i_buffer = -1;  /* Force the creation of the answer in
2184                                          * httpd_ClientSend */
2185                     cl->i_state = HTTPD_CLIENT_SENDING;
2186                 }
2187                 else if( i_msg == HTTPD_MSG_NONE )
2188                 {
2189                     if( query->i_proto == HTTPD_PROTO_NONE )
2190                     {
2191                         cl->url = NULL;
2192                         cl->i_state = HTTPD_CLIENT_DEAD;
2193                     }
2194                     else
2195                     {
2196                         uint8_t *p;
2197
2198                         /* unimplemented */
2199                         answer->i_proto  = query->i_proto ;
2200                         answer->i_type   = HTTPD_MSG_ANSWER;
2201                         answer->i_version= 0;
2202                         answer->i_status = 501;
2203                         answer->psz_status = strdup( "Unimplemented" );
2204
2205                         p = answer->p_body = malloc( 1000 );
2206
2207                         p += sprintf( p, "<html>\n" );
2208                         p += sprintf( p, "<head>\n" );
2209                         p += sprintf( p, "<title>Error 501</title>\n" );
2210                         p += sprintf( p, "</head>\n" );
2211                         p += sprintf( p, "<body>\n" );
2212                         p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2213                         p += sprintf( p, "<hr />\n" );
2214                         p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2215                         p += sprintf( p, "</body>\n" );
2216                         p += sprintf( p, "</html>\n" );
2217
2218                         answer->i_body = p - answer->p_body;
2219                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2220
2221                         cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
2222                         cl->i_state = HTTPD_CLIENT_SENDING;
2223                     }
2224                 }
2225                 else
2226                 {
2227                     vlc_bool_t b_auth_failed = VLC_FALSE;
2228                     int i;
2229
2230                     /* Search the url and trigger callbacks */
2231                     for( i = 0; i < host->i_url; i++ )
2232                     {
2233                         httpd_url_t *url = host->url[i];
2234
2235                         if( !strcmp( url->psz_url, query->psz_url ) )
2236                         {
2237                             if( url->catch[i_msg].cb )
2238                             {
2239                                 if( answer && ( *url->psz_user || *url->psz_password ) )
2240                                 {
2241                                     /* create the headers */
2242                                     char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2243                                     char *auth;
2244                                     char *id;
2245
2246                                     asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2247                                     auth = malloc( strlen(b64) + 1 );
2248
2249                                     if( !strncasecmp( b64, "BASIC", 5 ) )
2250                                     {
2251                                         b64 += 5;
2252                                         while( *b64 == ' ' )
2253                                         {
2254                                             b64++;
2255                                         }
2256                                         b64_decode( auth, b64 );
2257                                     }
2258                                     else
2259                                     {
2260                                         strcpy( auth, "" );
2261                                     }
2262                                     if( strcmp( id, auth ) )
2263                                     {
2264                                         httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2265                                         /* We fail for all url */
2266                                         b_auth_failed = VLC_TRUE;
2267                                         free( id );
2268                                         free( auth );
2269                                         break;
2270                                     }
2271
2272                                     free( id );
2273                                     free( auth );
2274                                 }
2275
2276                                 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2277                                 {
2278                                     /* only one url can answer */
2279                                     answer = NULL;
2280                                     if( cl->url == NULL )
2281                                     {
2282                                         cl->url = url;
2283                                     }
2284                                 }
2285                             }
2286                         }
2287                     }
2288                     if( answer )
2289                     {
2290                         uint8_t *p;
2291
2292                         answer->i_proto  = query->i_proto;
2293                         answer->i_type   = HTTPD_MSG_ANSWER;
2294                         answer->i_version= 0;
2295                         p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2296
2297                         if( b_auth_failed )
2298                         {
2299                             answer->i_status = 401;
2300                             answer->psz_status = strdup( "Authorization Required" );
2301
2302                             p += sprintf( p, "<html>\n" );
2303                             p += sprintf( p, "<head>\n" );
2304                             p += sprintf( p, "<title>Error 401</title>\n" );
2305                             p += sprintf( p, "</head>\n" );
2306                             p += sprintf( p, "<body>\n" );
2307                             p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2308                             p += sprintf( p, "<hr />\n" );
2309                             p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2310                             p += sprintf( p, "</body>\n" );
2311                             p += sprintf( p, "</html>\n" );
2312                         }
2313                         else
2314                         {
2315                             /* no url registered */
2316                             answer->i_status = 404;
2317                             answer->psz_status = strdup( "Not found" );
2318
2319                             p += sprintf( p, "<html>\n" );
2320                             p += sprintf( p, "<head>\n" );
2321                             p += sprintf( p, "<title>Error 404</title>\n" );
2322                             p += sprintf( p, "</head>\n" );
2323                             p += sprintf( p, "<body>\n" );
2324                             p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2325                             p += sprintf( p, "<hr />\n" );
2326                             p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2327                             p += sprintf( p, "</body>\n" );
2328                             p += sprintf( p, "</html>\n" );
2329                         }
2330
2331                         answer->i_body = p - answer->p_body;
2332                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2333                     }
2334                     cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
2335                     cl->i_state = HTTPD_CLIENT_SENDING;
2336                 }
2337             }
2338             else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2339             {
2340                 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2341                 {
2342                     cl->url = NULL;
2343                     if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2344                           ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2345                             ( cl->answer.i_version == 1 &&  strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2346                         ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2347                           strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2348                           strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2349                     {
2350                         httpd_MsgClean( &cl->query );
2351                         httpd_MsgInit( &cl->query );
2352
2353                         cl->i_buffer = 0;
2354                         cl->i_buffer_size = 1000;
2355                         free( cl->p_buffer );
2356                         cl->p_buffer = malloc( cl->i_buffer_size );
2357                         cl->i_state = HTTPD_CLIENT_RECEIVING;
2358                     }
2359                     else
2360                     {
2361                         cl->i_state = HTTPD_CLIENT_DEAD;
2362                     }
2363                     httpd_MsgClean( &cl->answer );
2364                 }
2365                 else if( cl->b_read_waiting )
2366                 {
2367                     /* we have a message waiting for us to read it */
2368                     httpd_MsgClean( &cl->answer );
2369                     httpd_MsgClean( &cl->query );
2370
2371                     cl->i_buffer = 0;
2372                     cl->i_buffer_size = 1000;
2373                     free( cl->p_buffer );
2374                     cl->p_buffer = malloc( cl->i_buffer_size );
2375                     cl->i_state = HTTPD_CLIENT_RECEIVING;
2376                     cl->b_read_waiting = VLC_FALSE;
2377                 }
2378                 else
2379                 {
2380                     int64_t i_offset = cl->answer.i_body_offset;
2381                     httpd_MsgClean( &cl->answer );
2382
2383                     cl->answer.i_body_offset = i_offset;
2384                     free( cl->p_buffer );
2385                     cl->p_buffer = NULL;
2386                     cl->i_buffer = 0;
2387                     cl->i_buffer_size = 0;
2388
2389                     cl->i_state = HTTPD_CLIENT_WAITING;
2390                 }
2391             }
2392             else if( cl->i_state == HTTPD_CLIENT_WAITING )
2393             {
2394                 int64_t i_offset = cl->answer.i_body_offset;
2395                 int     i_msg = cl->query.i_type;
2396
2397                 httpd_MsgInit( &cl->answer );
2398                 cl->answer.i_body_offset = i_offset;
2399
2400                 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2401                                           &cl->answer, &cl->query );
2402                 if( cl->answer.i_type != HTTPD_MSG_NONE )
2403                 {
2404                     /* we have new data, so reenter send mode */
2405                     cl->i_buffer      = 0;
2406                     cl->p_buffer      = cl->answer.p_body;
2407                     cl->i_buffer_size = cl->answer.i_body;
2408                     cl->answer.p_body = NULL;
2409                     cl->answer.i_body = 0;
2410                     cl->i_state = HTTPD_CLIENT_SENDING;
2411                 }
2412                 else
2413                 {
2414                     /* we shouldn't wait too long */
2415                     b_low_delay = VLC_TRUE;
2416                 }
2417             }
2418
2419             /* Special for BIDIR mode we also check reading */
2420             if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2421                 cl->i_state == HTTPD_CLIENT_SENDING )
2422             {
2423                 FD_SET( cl->fd, &fds_read );
2424                 i_handle_max = __MAX( i_handle_max, cl->fd );
2425             }
2426         }
2427         vlc_mutex_unlock( &host->lock );
2428
2429         /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2430         timeout.tv_sec = 0;
2431         timeout.tv_usec = b_low_delay ? 20000 : 100000;
2432
2433         i_ret = select( i_handle_max + 1,
2434                         &fds_read, &fds_write, NULL, &timeout );
2435
2436         if( i_ret == -1 && errno != EINTR )
2437         {
2438             msg_Warn( host, "cannot select sockets" );
2439             msleep( 1000 );
2440             continue;
2441         }
2442         else if( i_ret <= 0 )
2443         {
2444             continue;
2445         }
2446
2447         /* accept new connections */
2448         if( FD_ISSET( host->fd, &fds_read ) )
2449         {
2450             int     i_sock_size = sizeof( struct sockaddr_storage );
2451             struct  sockaddr_storage sock;
2452             int     fd;
2453
2454             fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2455             if( fd >= 0 )
2456             {
2457                 int i_state = 0;
2458
2459                 /* set this new socket non-block */
2460 #if defined( WIN32 ) || defined( UNDER_CE )
2461                 {
2462                     unsigned long i_dummy = 1;
2463                     ioctlsocket( fd, FIONBIO, &i_dummy );
2464                 }
2465 #else
2466                 fcntl( fd, F_SETFL, O_NONBLOCK );
2467 #endif
2468
2469                 if( p_tls != NULL)
2470                 {
2471                     switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2472                     {
2473                         case -1:
2474                             msg_Err( host, "Rejecting TLS connection" );
2475                             net_Close( fd );
2476                             fd = -1;
2477                             p_tls = NULL;
2478                             break;
2479
2480                         case 1: /* missing input - most likely */
2481                             i_state = HTTPD_CLIENT_TLS_HS_IN;
2482                             break;
2483
2484                         case 2: /* missing output */
2485                             i_state = HTTPD_CLIENT_TLS_HS_OUT;
2486                             break;
2487                     }
2488                 }
2489                 
2490                 if( fd >= 0 )
2491                 {
2492                     char *ip;
2493                     httpd_client_t *cl;
2494
2495                     cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2496                     p_tls = NULL;
2497                     vlc_mutex_lock( &host->lock );
2498                     TAB_APPEND( host->i_client, host->client, cl );
2499                     vlc_mutex_unlock( &host->lock );
2500
2501                     if( i_state != 0 )
2502                         cl->i_state = i_state; // override state for TLS
2503
2504                     // FIXME: it sucks to allocate memory for debug
2505                     ip = httpd_ClientIP( cl );
2506                     msg_Dbg( host, "new connection (%s)",
2507                              ip != NULL ? ip : "unknown" );
2508                     if( ip != NULL)
2509                         free( ip );
2510                 }
2511             }
2512         }
2513         /* now try all others socket */
2514         vlc_mutex_lock( &host->lock );
2515         for( i_client = 0; i_client < host->i_client; i_client++ )
2516         {
2517             httpd_client_t *cl = host->client[i_client];
2518             if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2519             {
2520                 httpd_ClientRecv( cl );
2521             }
2522             else if( cl->i_state == HTTPD_CLIENT_SENDING )
2523             {
2524                 httpd_ClientSend( cl );
2525             }
2526             else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2527             {
2528                 httpd_ClientTlsHsIn( cl );
2529             }
2530             else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2531             {
2532                 httpd_ClientTlsHsOut( cl );
2533             }
2534
2535             if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2536                 cl->i_state == HTTPD_CLIENT_SENDING &&
2537                 FD_ISSET( cl->fd, &fds_read ) )
2538             {
2539                 cl->b_read_waiting = VLC_TRUE;
2540             }
2541         }
2542         vlc_mutex_unlock( &host->lock );
2543     }
2544
2545     if( p_tls != NULL )
2546         tls_ServerSessionClose( p_tls );
2547 }
2548
2549 #ifndef HAVE_GETADDRINFO
2550 static int BuildAddr( struct sockaddr_in * p_socket,
2551                       const char * psz_address, int i_port )
2552 {
2553     /* Reset struct */
2554     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2555     p_socket->sin_family = AF_INET;                                /* family */
2556     p_socket->sin_port = htons( (uint16_t)i_port );
2557     if( !*psz_address )
2558     {
2559         p_socket->sin_addr.s_addr = INADDR_ANY;
2560     }
2561     else
2562     {
2563         struct hostent    * p_hostent;
2564
2565         /* Try to convert address directly from in_addr - this will work if
2566          * psz_address is dotted decimal. */
2567 #ifdef HAVE_ARPA_INET_H
2568         if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2569 #else
2570         p_socket->sin_addr.s_addr = inet_addr( psz_address );
2571
2572 /*        if( p_socket->sin_addr.s_addr == INADDR_NONE )*/
2573         if( p_socket->sin_addr.s_addr == INADDR_BROADCAST )
2574 #endif
2575         {
2576             /* We have a fqdn, try to find its address */
2577             if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2578             {
2579                 return( -1 );
2580             }
2581
2582             /* Copy the first address of the host in the socket address */
2583             memcpy( &((struct sockaddr_in *)p_socket)->sin_addr, p_hostent->h_addr_list[0],
2584                      p_hostent->h_length );
2585         }
2586     }
2587     return( 0 );
2588 }
2589 #endif
2590
2591 static int GetAddrPort( const struct sockaddr_storage *p_ss )
2592 {
2593     int i_port = 0;
2594
2595     switch (p_ss->ss_family)
2596     {
2597 #ifdef AF_INET6
2598         case AF_INET6:
2599             i_port = ((const struct sockaddr_in6 *)p_ss)->sin6_port;
2600             break;
2601 #endif
2602
2603         case AF_INET:
2604             i_port = ((const struct sockaddr_in *)p_ss)->sin_port;
2605             break;
2606             
2607         default:
2608             return -1;
2609     }
2610     
2611     return ntohs( i_port );
2612 }
2613
2614 #else /* ENABLE_HTTPD */
2615
2616 /* We just define an empty wrapper */
2617 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2618                                 tls_server_t *d )
2619 {
2620     msg_Err( a, "HTTP daemon support is disabled" );
2621     return 0;
2622 }
2623 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2624 {
2625     msg_Err( a, "HTTP daemon support is disabled" );
2626     return 0;
2627 }
2628 void httpd_HostDelete( httpd_host_t *a ){}
2629 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2630 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2631                                  char *d ){ return 0; }
2632 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2633                     httpd_callback_sys_t *d ){ return 0; }
2634 void httpd_UrlDelete( httpd_url_t *a ){}
2635
2636 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2637 void httpd_ClientModeStream( httpd_client_t *a ){}
2638 void httpd_ClientModeBidir( httpd_client_t *a ){}
2639
2640 void httpd_FileDelete( httpd_file_t *a ){}
2641 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2642                              char *e, httpd_file_callback_t f,
2643                              httpd_file_sys_t *g ){ return 0; }
2644
2645 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2646 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2647                                      char *b, char *c ){ return 0; }
2648
2649 void httpd_StreamDelete( httpd_stream_t *a ){}
2650 int  httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2651 int  httpd_StreamSend  ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2652 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2653                                  char *d, char *e ){ return 0; }
2654
2655 void httpd_MsgInit ( httpd_message_t *a ){}
2656 void httpd_MsgAdd  ( httpd_message_t *a, char *b, char *c, ... ){}
2657 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2658 void httpd_MsgClean( httpd_message_t *a ){}
2659
2660 #endif /* ENABLE_HTTPD */