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