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