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