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