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