Audiobar: fix copyright years and shortname
[vlc.git] / src / network / httpd.c
blob7c0819297f70e50105631d69acecc7f58fc6da3c
1 /*****************************************************************************
2 * httpd.c
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2004-2007 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_httpd.h>
33 #include <assert.h>
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 <vlc_charset.h>
41 #include "../libvlc.h"
43 #include <string.h>
44 #include <errno.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
50 #ifdef HAVE_POLL
51 # include <poll.h>
52 #endif
54 #if defined( UNDER_CE )
55 # include <winsock.h>
56 #elif defined( WIN32 )
57 # include <winsock2.h>
58 #else
59 # include <sys/socket.h>
60 #endif
62 #if defined( WIN32 )
63 /* We need HUGE buffer otherwise TCP throughput is very limited */
64 #define HTTPD_CL_BUFSIZE 1000000
65 #else
66 #define HTTPD_CL_BUFSIZE 10000
67 #endif
69 static void httpd_ClientClean( httpd_client_t *cl );
71 /* each host run in his own thread */
72 struct httpd_host_t
74 VLC_COMMON_MEMBERS
76 /* ref count */
77 unsigned i_ref;
79 /* address/port and socket for listening at connections */
80 int *fds;
81 unsigned nfd;
83 vlc_thread_t thread;
84 vlc_mutex_t lock;
85 vlc_cond_t wait;
87 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
88 * This will slow down the url research but make my live easier
89 * All url will have their cb trigger, but only the first one can answer
90 * */
91 int i_url;
92 httpd_url_t **url;
94 int i_client;
95 httpd_client_t **client;
97 /* TLS data */
98 vlc_tls_creds_t *p_tls;
102 struct httpd_url_t
104 httpd_host_t *host;
106 vlc_mutex_t lock;
108 char *psz_url;
109 char *psz_user;
110 char *psz_password;
111 vlc_acl_t *p_acl;
113 struct
115 httpd_callback_t cb;
116 httpd_callback_sys_t *p_sys;
117 } catch[HTTPD_MSG_MAX];
120 /* status */
121 enum
123 HTTPD_CLIENT_RECEIVING,
124 HTTPD_CLIENT_RECEIVE_DONE,
126 HTTPD_CLIENT_SENDING,
127 HTTPD_CLIENT_SEND_DONE,
129 HTTPD_CLIENT_WAITING,
131 HTTPD_CLIENT_DEAD,
133 HTTPD_CLIENT_TLS_HS_IN,
134 HTTPD_CLIENT_TLS_HS_OUT
137 /* mode */
138 enum
140 HTTPD_CLIENT_FILE, /* default */
141 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
142 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
145 struct httpd_client_t
147 httpd_url_t *url;
149 int i_ref;
151 int fd;
153 int i_mode;
154 int i_state;
155 int b_read_waiting; /* stop as soon as possible sending */
157 mtime_t i_activity_date;
158 mtime_t i_activity_timeout;
160 /* buffer for reading header */
161 int i_buffer_size;
162 int i_buffer;
163 uint8_t *p_buffer;
165 /* */
166 httpd_message_t query; /* client -> httpd */
167 httpd_message_t answer; /* httpd -> client */
169 /* TLS data */
170 vlc_tls_t *p_tls;
174 /*****************************************************************************
175 * Various functions
176 *****************************************************************************/
177 static const struct
179 const char psz_ext[8];
180 const char *psz_mime;
181 } http_mime[] =
183 { ".htm", "text/html" },
184 { ".html", "text/html" },
185 { ".txt", "text/plain" },
186 { ".xml", "text/xml" },
187 { ".dtd", "text/dtd" },
189 { ".css", "text/css" },
191 /* image mime */
192 { ".gif", "image/gif" },
193 { ".jpe", "image/jpeg" },
194 { ".jpg", "image/jpeg" },
195 { ".jpeg", "image/jpeg" },
196 { ".png", "image/png" },
197 /* same as modules/mux/mpjpeg.c here: */
198 { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" },
200 /* media mime */
201 { ".avi", "video/avi" },
202 { ".asf", "video/x-ms-asf" },
203 { ".m1a", "audio/mpeg" },
204 { ".m2a", "audio/mpeg" },
205 { ".m1v", "video/mpeg" },
206 { ".m2v", "video/mpeg" },
207 { ".mp2", "audio/mpeg" },
208 { ".mp3", "audio/mpeg" },
209 { ".mpa", "audio/mpeg" },
210 { ".mpg", "video/mpeg" },
211 { ".mpeg", "video/mpeg" },
212 { ".mpe", "video/mpeg" },
213 { ".mov", "video/quicktime" },
214 { ".moov", "video/quicktime" },
215 { ".oga", "audio/ogg" },
216 { ".ogg", "application/ogg" },
217 { ".ogm", "application/ogg" },
218 { ".ogv", "video/ogg" },
219 { ".ogx", "application/ogg" },
220 { ".spx", "audio/ogg" },
221 { ".wav", "audio/wav" },
222 { ".wma", "audio/x-ms-wma" },
223 { ".wmv", "video/x-ms-wmv" },
226 /* end */
227 { "", "" }
230 static const char *httpd_MimeFromUrl( const char *psz_url )
233 char *psz_ext;
235 psz_ext = strrchr( psz_url, '.' );
236 if( psz_ext )
238 int i;
240 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
242 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
244 return http_mime[i].psz_mime;
248 return "application/octet-stream";
252 typedef struct
254 unsigned i_code;
255 const char psz_reason[36];
256 } http_status_info;
258 static const http_status_info http_reason[] =
260 /*{ 100, "Continue" },
261 { 101, "Switching Protocols" },*/
262 { 200, "OK" },
263 /*{ 201, "Created" },
264 { 202, "Accepted" },
265 { 203, "Non-authoritative information" },
266 { 204, "No content" },
267 { 205, "Reset content" },
268 { 206, "Partial content" },
269 { 250, "Low on storage space" },
270 { 300, "Multiple choices" },*/
271 { 301, "Moved permanently" },
272 /*{ 302, "Moved temporarily" },
273 { 303, "See other" },
274 { 304, "Not modified" },
275 { 305, "Use proxy" },
276 { 307, "Temporary redirect" },
277 { 400, "Bad request" },*/
278 { 401, "Unauthorized" },
279 /*{ 402, "Payment Required" },*/
280 { 403, "Forbidden" },
281 { 404, "Not found" },
282 { 405, "Method not allowed" },
283 /*{ 406, "Not acceptable" },
284 { 407, "Proxy authentication required" },
285 { 408, "Request time-out" },
286 { 409, "Conflict" },
287 { 410, "Gone" },
288 { 411, "Length required" },
289 { 412, "Precondition failed" },
290 { 413, "Request entity too large" },
291 { 414, "Request-URI too large" },
292 { 415, "Unsupported media Type" },
293 { 416, "Requested range not satisfiable" },
294 { 417, "Expectation failed" },
295 { 451, "Parameter not understood" },
296 { 452, "Conference not found" },
297 { 453, "Not enough bandwidth" },*/
298 { 454, "Session not found" },
299 { 455, "Method not valid in this State" },
300 { 456, "Header field not valid for resource" },
301 { 457, "Invalid range" },
302 /*{ 458, "Read-only parameter" },*/
303 { 459, "Aggregate operation not allowed" },
304 { 460, "Non-aggregate operation not allowed" },
305 { 461, "Unsupported transport" },
306 /*{ 462, "Destination unreachable" },*/
307 { 500, "Internal server error" },
308 { 501, "Not implemented" },
309 /*{ 502, "Bad gateway" },*/
310 { 503, "Service unavailable" },
311 /*{ 504, "Gateway time-out" },*/
312 { 505, "Protocol version not supported" },
313 { 551, "Option not supported" },
314 { 999, "" }
317 static const char psz_fallback_reason[5][16] =
318 { "Continue", "OK", "Found", "Client error", "Server error" };
320 static const char *httpd_ReasonFromCode( unsigned i_code )
322 const http_status_info *p;
324 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
326 for (p = http_reason; i_code > p->i_code; p++);
328 if( p->i_code == i_code )
329 return p->psz_reason;
331 return psz_fallback_reason[(i_code / 100) - 1];
335 static size_t httpd_HtmlError (char **body, int code, const char *url)
337 const char *errname = httpd_ReasonFromCode (code);
338 assert (errname != NULL);
340 int res = asprintf (body,
341 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
342 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
343 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
344 "<html lang=\"en\">\n"
345 "<head>\n"
346 "<title>%s</title>\n"
347 "</head>\n"
348 "<body>\n"
349 "<h1>%d %s%s%s%s</h1>\n"
350 "<hr />\n"
351 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
352 "</body>\n"
353 "</html>\n", errname, code, errname,
354 (url ? " (" : ""), (url ? url : ""), (url ? ")" : ""));
356 if (res == -1)
358 *body = NULL;
359 return 0;
362 return (size_t)res;
366 /*****************************************************************************
367 * High Level Functions: httpd_file_t
368 *****************************************************************************/
369 struct httpd_file_t
371 httpd_url_t *url;
373 char *psz_url;
374 char *psz_mime;
376 httpd_file_callback_t pf_fill;
377 httpd_file_sys_t *p_sys;
381 static int
382 httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
383 httpd_message_t *answer, const httpd_message_t *query )
385 httpd_file_t *file = (httpd_file_t*)p_sys;
386 uint8_t **pp_body, *p_body;
387 const char *psz_connection;
388 int *pi_body, i_body;
390 if( answer == NULL || query == NULL )
392 return VLC_SUCCESS;
394 answer->i_proto = HTTPD_PROTO_HTTP;
395 answer->i_version= 1;
396 answer->i_type = HTTPD_MSG_ANSWER;
398 answer->i_status = 200;
400 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
401 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
403 if( query->i_type != HTTPD_MSG_HEAD )
405 pp_body = &answer->p_body;
406 pi_body = &answer->i_body;
408 else
410 /* The file still needs to be executed. */
411 p_body = NULL;
412 i_body = 0;
413 pp_body = &p_body;
414 pi_body = &i_body;
417 if( query->i_type == HTTPD_MSG_POST )
419 /* msg_Warn not supported */
422 uint8_t *psz_args = query->psz_args;
423 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
425 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
427 free( p_body );
430 /* We respect client request */
431 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
432 if( psz_connection != NULL )
434 httpd_MsgAdd( answer, "Connection", "%s", psz_connection );
437 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
439 return VLC_SUCCESS;
442 httpd_file_t *httpd_FileNew( httpd_host_t *host,
443 const char *psz_url, const char *psz_mime,
444 const char *psz_user, const char *psz_password,
445 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
446 httpd_file_sys_t *p_sys )
448 httpd_file_t *file = xmalloc( sizeof( httpd_file_t ) );
450 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
451 psz_password, p_acl )
452 ) == NULL )
454 free( file );
455 return NULL;
458 file->psz_url = strdup( psz_url );
459 if( psz_mime && *psz_mime )
461 file->psz_mime = strdup( psz_mime );
463 else
465 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
468 file->pf_fill = pf_fill;
469 file->p_sys = p_sys;
471 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
472 (httpd_callback_sys_t*)file );
473 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
474 (httpd_callback_sys_t*)file );
475 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
476 (httpd_callback_sys_t*)file );
478 return file;
481 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
483 httpd_file_sys_t *p_sys = file->p_sys;
485 httpd_UrlDelete( file->url );
487 free( file->psz_url );
488 free( file->psz_mime );
490 free( file );
492 return p_sys;
495 /*****************************************************************************
496 * High Level Functions: httpd_handler_t (for CGIs)
497 *****************************************************************************/
498 struct httpd_handler_t
500 httpd_url_t *url;
502 httpd_handler_callback_t pf_fill;
503 httpd_handler_sys_t *p_sys;
507 static int
508 httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
509 httpd_message_t *answer, const httpd_message_t *query )
511 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
512 char psz_remote_addr[NI_MAXNUMERICHOST];
514 if( answer == NULL || query == NULL )
516 return VLC_SUCCESS;
518 answer->i_proto = HTTPD_PROTO_NONE;
519 answer->i_type = HTTPD_MSG_ANSWER;
521 /* We do it ourselves, thanks */
522 answer->i_status = 0;
524 if( httpd_ClientIP( cl, psz_remote_addr, NULL ) == NULL )
525 *psz_remote_addr = '\0';
527 uint8_t *psz_args = query->psz_args;
528 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
529 query->i_type, query->p_body, query->i_body,
530 psz_remote_addr, NULL,
531 &answer->p_body, &answer->i_body );
533 if( query->i_type == HTTPD_MSG_HEAD )
535 char *p = (char *)answer->p_body;
537 /* Looks for end of header (i.e. one empty line) */
538 while ( (p = strchr( p, '\r' )) != NULL )
540 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
541 && p[3] && p[3] == '\n' )
543 break;
547 if( p != NULL )
549 p[4] = '\0';
550 answer->i_body = strlen((char*)answer->p_body) + 1;
551 answer->p_body = xrealloc( answer->p_body, answer->i_body );
555 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
557 int i_status, i_headers;
558 char *psz_headers, *psz_new;
559 const char *psz_status;
561 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
563 /* Apache-style */
564 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
565 if( *psz_headers == '\r' || *psz_headers == '\n' ) psz_headers++;
566 if( *psz_headers == '\n' ) psz_headers++;
567 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
569 else
571 i_status = 200;
572 psz_headers = (char *)answer->p_body;
573 i_headers = answer->i_body;
576 psz_status = httpd_ReasonFromCode( i_status );
577 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
578 + strlen(psz_status) + i_headers - 1;
579 psz_new = (char *)xmalloc( answer->i_body + 1);
580 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
581 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
582 free( answer->p_body );
583 answer->p_body = (uint8_t *)psz_new;
586 return VLC_SUCCESS;
589 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
590 const char *psz_user,
591 const char *psz_password,
592 const vlc_acl_t *p_acl,
593 httpd_handler_callback_t pf_fill,
594 httpd_handler_sys_t *p_sys )
596 httpd_handler_t *handler = xmalloc( sizeof( httpd_handler_t ) );
598 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
599 psz_password, p_acl )
600 ) == NULL )
602 free( handler );
603 return NULL;
606 handler->pf_fill = pf_fill;
607 handler->p_sys = p_sys;
609 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
610 (httpd_callback_sys_t*)handler );
611 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
612 (httpd_callback_sys_t*)handler );
613 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
614 (httpd_callback_sys_t*)handler );
616 return handler;
619 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
621 httpd_handler_sys_t *p_sys = handler->p_sys;
622 httpd_UrlDelete( handler->url );
623 free( handler );
624 return p_sys;
627 /*****************************************************************************
628 * High Level Functions: httpd_redirect_t
629 *****************************************************************************/
630 struct httpd_redirect_t
632 httpd_url_t *url;
633 char *psz_dst;
636 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
637 httpd_client_t *cl, httpd_message_t *answer,
638 const httpd_message_t *query )
640 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
641 char *p_body;
642 (void)cl;
644 if( answer == NULL || query == NULL )
646 return VLC_SUCCESS;
648 answer->i_proto = HTTPD_PROTO_HTTP;
649 answer->i_version= 1;
650 answer->i_type = HTTPD_MSG_ANSWER;
651 answer->i_status = 301;
653 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
654 answer->p_body = (unsigned char *)p_body;
656 /* XXX check if it's ok or we need to set an absolute url */
657 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
659 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
661 return VLC_SUCCESS;
664 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
665 const char *psz_url_src )
667 httpd_redirect_t *rdir = xmalloc( sizeof( httpd_redirect_t ) );
669 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
671 free( rdir );
672 return NULL;
674 rdir->psz_dst = strdup( psz_url_dst );
676 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
677 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
678 (httpd_callback_sys_t*)rdir );
679 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
680 (httpd_callback_sys_t*)rdir );
681 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
682 (httpd_callback_sys_t*)rdir );
683 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
684 (httpd_callback_sys_t*)rdir );
686 return rdir;
688 void httpd_RedirectDelete( httpd_redirect_t *rdir )
690 httpd_UrlDelete( rdir->url );
691 free( rdir->psz_dst );
692 free( rdir );
695 /*****************************************************************************
696 * High Level Funtions: httpd_stream_t
697 *****************************************************************************/
698 struct httpd_stream_t
700 vlc_mutex_t lock;
701 httpd_url_t *url;
703 char *psz_mime;
705 /* Header to send as first packet */
706 uint8_t *p_header;
707 int i_header;
709 /* circular buffer */
710 int i_buffer_size; /* buffer size, can't be reallocated smaller */
711 uint8_t *p_buffer; /* buffer */
712 int64_t i_buffer_pos; /* absolute position from begining */
713 int64_t i_buffer_last_pos; /* a new connection will start with that */
716 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
717 httpd_client_t *cl, httpd_message_t *answer,
718 const httpd_message_t *query )
720 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
722 if( answer == NULL || query == NULL || cl == NULL )
724 return VLC_SUCCESS;
727 if( answer->i_body_offset > 0 )
729 int64_t i_write;
730 int i_pos;
732 #if 0
733 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
734 answer->i_body_offset );
735 #endif
737 if( answer->i_body_offset >= stream->i_buffer_pos )
739 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
740 return VLC_EGENERIC; /* wait, no data available */
742 if( answer->i_body_offset + stream->i_buffer_size <
743 stream->i_buffer_pos )
745 /* this client isn't fast enough */
746 #if 0
747 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
748 answer->i_body_offset, stream->i_buffer_last_pos );
749 #endif
750 answer->i_body_offset = stream->i_buffer_last_pos;
753 i_pos = answer->i_body_offset % stream->i_buffer_size;
754 i_write = stream->i_buffer_pos - answer->i_body_offset;
755 if( i_write > HTTPD_CL_BUFSIZE )
757 i_write = HTTPD_CL_BUFSIZE;
759 else if( i_write <= 0 )
761 return VLC_EGENERIC; /* wait, no data available */
764 /* Don't go past the end of the circular buffer */
765 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
767 /* using HTTPD_MSG_ANSWER -> data available */
768 answer->i_proto = HTTPD_PROTO_HTTP;
769 answer->i_version= 0;
770 answer->i_type = HTTPD_MSG_ANSWER;
772 answer->i_body = i_write;
773 answer->p_body = xmalloc( i_write );
774 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
776 answer->i_body_offset += i_write;
778 return VLC_SUCCESS;
780 else
782 answer->i_proto = HTTPD_PROTO_HTTP;
783 answer->i_version= 0;
784 answer->i_type = HTTPD_MSG_ANSWER;
786 answer->i_status = 200;
788 if( query->i_type != HTTPD_MSG_HEAD )
790 httpd_ClientModeStream( cl );
791 vlc_mutex_lock( &stream->lock );
792 /* Send the header */
793 if( stream->i_header > 0 )
795 answer->i_body = stream->i_header;
796 answer->p_body = xmalloc( stream->i_header );
797 memcpy( answer->p_body, stream->p_header, stream->i_header );
799 answer->i_body_offset = stream->i_buffer_last_pos;
800 vlc_mutex_unlock( &stream->lock );
802 else
804 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
805 answer->i_body_offset = 0;
808 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
810 bool b_xplaystream = false;
811 int i;
813 httpd_MsgAdd( answer, "Content-type", "%s",
814 "application/octet-stream" );
815 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
816 httpd_MsgAdd( answer, "Pragma", "no-cache" );
817 httpd_MsgAdd( answer, "Pragma", "client-id=%lu",
818 vlc_mrand48()&0x7fff );
819 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
821 /* Check if there is a xPlayStrm=1 */
822 for( i = 0; i < query->i_name; i++ )
824 if( !strcasecmp( query->name[i], "Pragma" ) &&
825 strstr( query->value[i], "xPlayStrm=1" ) )
827 b_xplaystream = true;
831 if( !b_xplaystream )
833 answer->i_body_offset = 0;
836 else
838 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
840 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
841 return VLC_SUCCESS;
845 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
846 const char *psz_url, const char *psz_mime,
847 const char *psz_user, const char *psz_password,
848 const vlc_acl_t *p_acl )
850 httpd_stream_t *stream = xmalloc( sizeof( httpd_stream_t ) );
852 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
853 psz_password, p_acl )
854 ) == NULL )
856 free( stream );
857 return NULL;
859 vlc_mutex_init( &stream->lock );
860 if( psz_mime && *psz_mime )
862 stream->psz_mime = strdup( psz_mime );
864 else
866 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
868 stream->i_header = 0;
869 stream->p_header = NULL;
870 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
871 stream->p_buffer = xmalloc( stream->i_buffer_size );
872 /* We set to 1 to make life simpler
873 * (this way i_body_offset can never be 0) */
874 stream->i_buffer_pos = 1;
875 stream->i_buffer_last_pos = 1;
877 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
878 (httpd_callback_sys_t*)stream );
879 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
880 (httpd_callback_sys_t*)stream );
881 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
882 (httpd_callback_sys_t*)stream );
884 return stream;
887 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
889 vlc_mutex_lock( &stream->lock );
890 free( stream->p_header );
891 stream->p_header = NULL;
893 stream->i_header = i_data;
894 if( i_data > 0 )
896 stream->p_header = xmalloc( i_data );
897 memcpy( stream->p_header, p_data, i_data );
899 vlc_mutex_unlock( &stream->lock );
901 return VLC_SUCCESS;
904 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
906 int i_count;
907 int i_pos;
909 if( i_data < 0 || p_data == NULL )
911 return VLC_SUCCESS;
913 vlc_mutex_lock( &stream->lock );
915 /* save this pointer (to be used by new connection) */
916 stream->i_buffer_last_pos = stream->i_buffer_pos;
918 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
919 i_count = i_data;
920 while( i_count > 0)
922 int i_copy;
924 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
926 /* Ok, we can't go past the end of our buffer */
927 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
929 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
930 i_count -= i_copy;
931 p_data += i_copy;
934 stream->i_buffer_pos += i_data;
936 vlc_mutex_unlock( &stream->lock );
937 return VLC_SUCCESS;
940 void httpd_StreamDelete( httpd_stream_t *stream )
942 httpd_UrlDelete( stream->url );
943 vlc_mutex_destroy( &stream->lock );
944 free( stream->psz_mime );
945 free( stream->p_header );
946 free( stream->p_buffer );
947 free( stream );
950 /*****************************************************************************
951 * Low level
952 *****************************************************************************/
953 static void* httpd_HostThread( void * );
954 static httpd_host_t *httpd_HostCreate( vlc_object_t *, httpd_host_t **,
955 const char *, const char *,
956 vlc_tls_creds_t * );
958 static struct httpd_t
960 vlc_mutex_t mutex;
961 httpd_host_t *http, *https, *rtsp;
962 } httpd = { VLC_STATIC_MUTEX, NULL, NULL, NULL };
964 /* create a new host */
965 httpd_host_t *vlc_http_HostNew( vlc_object_t *p_this )
967 return httpd_HostCreate( p_this, &httpd.http,
968 "http-host", "http-port", NULL );
971 httpd_host_t *vlc_https_HostNew( vlc_object_t *obj )
973 char *cert = var_InheritString( obj, "http-cert" );
974 if( cert == NULL )
976 msg_Err( obj, "HTTP/TLS certificate not specified!" );
977 return NULL;
980 char *key = var_InheritString( obj, "http-key" );
981 vlc_tls_creds_t *tls = vlc_tls_ServerCreate( obj, cert, key );
983 if( tls == NULL )
985 msg_Err( obj, "HTTP/TLS certificate error (%s and %s)",
986 cert, (key != NULL) ? key : cert );
987 free( key );
988 free( cert );
989 return NULL;
991 free( key );
992 free( cert );
994 char *ca = var_InheritString( obj, "http-ca" );
995 if( ca != NULL )
997 if( vlc_tls_ServerAddCA( tls, ca ) )
999 msg_Err( obj, "HTTP/TLS CA error (%s)", ca );
1000 free( ca );
1001 goto error;
1003 free( ca );
1006 char *crl = var_InheritString( obj, "http-crl" );
1007 if( crl != NULL )
1009 if( vlc_tls_ServerAddCRL( tls, crl ) )
1011 msg_Err( obj, "TLS CRL error (%s)", crl );
1012 free( crl );
1013 goto error;
1015 free( crl );
1018 return httpd_HostCreate( obj, &httpd.https,
1019 "http-host", "https-port", tls );
1021 error:
1022 vlc_tls_ServerDelete( tls );
1023 return NULL;
1026 httpd_host_t *vlc_rtsp_HostNew( vlc_object_t *p_this )
1028 return httpd_HostCreate( p_this, &httpd.rtsp,
1029 "rtsp-host", "rtsp-port", NULL );
1032 static httpd_host_t *httpd_HostCreate( vlc_object_t *p_this,
1033 httpd_host_t **hostp,
1034 const char *hostvar,
1035 const char *portvar,
1036 vlc_tls_creds_t *p_tls )
1038 httpd_host_t *host;
1040 /* to be sure to avoid multiple creation */
1041 vlc_mutex_lock( &httpd.mutex );
1043 /* verify if it already exist */
1044 host = *hostp;
1045 if( host != NULL )
1047 /* Increase existing matching host reference count.
1048 * The reference count is written under both the global httpd and the
1049 * host lock. It is read with either or both locks held. The global
1050 * lock is always acquired first. */
1051 vlc_mutex_lock( &host->lock );
1052 host->i_ref++;
1053 vlc_mutex_unlock( &host->lock );
1055 vlc_mutex_unlock( &httpd.mutex );
1056 if( p_tls != NULL )
1057 vlc_tls_ServerDelete( p_tls );
1058 return host;
1061 /* create the new host */
1062 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1063 "http host" );
1064 if (host == NULL)
1065 goto error;
1067 vlc_mutex_init( &host->lock );
1068 vlc_cond_init( &host->wait );
1069 host->i_ref = 1;
1071 char *hostname = var_InheritString( p_this, hostvar );
1072 unsigned port = var_InheritInteger( p_this, portvar );
1073 host->fds = net_ListenTCP( p_this, hostname, port );
1074 free( hostname );
1075 if( host->fds == NULL )
1077 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1078 goto error;
1080 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1082 if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
1084 msg_Err( host, "signaling pipe error: %m" );
1085 goto error;
1088 host->i_url = 0;
1089 host->url = NULL;
1090 host->i_client = 0;
1091 host->client = NULL;
1092 host->p_tls = p_tls;
1094 /* create the thread */
1095 if( vlc_clone( &host->thread, httpd_HostThread, host,
1096 VLC_THREAD_PRIORITY_LOW ) )
1098 msg_Err( p_this, "cannot spawn http host thread" );
1099 goto error;
1102 *hostp = host;
1103 vlc_mutex_unlock( &httpd.mutex );
1105 return host;
1107 error:
1108 vlc_mutex_unlock( &httpd.mutex );
1110 if( host != NULL )
1112 net_ListenClose( host->fds );
1113 vlc_cond_destroy( &host->wait );
1114 vlc_mutex_destroy( &host->lock );
1115 vlc_object_release( host );
1118 if( p_tls != NULL )
1119 vlc_tls_ServerDelete( p_tls );
1121 return NULL;
1124 /* delete a host */
1125 void httpd_HostDelete( httpd_host_t *host )
1127 int i;
1128 bool delete = false;
1130 vlc_mutex_lock( &httpd.mutex );
1132 vlc_mutex_lock( &host->lock );
1133 host->i_ref--;
1134 if( host->i_ref == 0 )
1136 vlc_cond_signal( &host->wait );
1137 delete = true;
1139 vlc_mutex_unlock( &host->lock );
1140 if( !delete )
1142 /* still used */
1143 vlc_mutex_unlock( &httpd.mutex );
1144 msg_Dbg( host, "httpd_HostDelete: host still in use" );
1145 return;
1148 vlc_object_kill( host );
1149 vlc_join( host->thread, NULL );
1151 msg_Dbg( host, "HTTP host removed" );
1153 for( i = 0; i < host->i_url; i++ )
1155 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1157 for( i = 0; i < host->i_client; i++ )
1159 httpd_client_t *cl = host->client[i];
1160 msg_Warn( host, "client still connected" );
1161 httpd_ClientClean( cl );
1162 TAB_REMOVE( host->i_client, host->client, cl );
1163 free( cl );
1164 i--;
1165 /* TODO */
1168 if( host->p_tls != NULL)
1169 vlc_tls_ServerDelete( host->p_tls );
1171 net_ListenClose( host->fds );
1172 vlc_cond_destroy( &host->wait );
1173 vlc_mutex_destroy( &host->lock );
1174 vlc_object_release( host );
1175 if( host == httpd.http )
1176 httpd.http = NULL;
1177 else if( host == httpd.https )
1178 httpd.https = NULL;
1179 else if( host == httpd.rtsp )
1180 httpd.rtsp = NULL;
1181 else
1182 assert(0);
1183 vlc_mutex_unlock( &httpd.mutex );
1186 /* register a new url */
1187 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1188 const char *psz_user, const char *psz_password,
1189 const vlc_acl_t *p_acl, bool b_check )
1191 httpd_url_t *url;
1192 int i;
1194 assert( psz_url != NULL );
1196 vlc_mutex_lock( &host->lock );
1197 if( b_check )
1199 for( i = 0; i < host->i_url; i++ )
1201 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1203 msg_Warn( host,
1204 "cannot add '%s' (url already defined)", psz_url );
1205 vlc_mutex_unlock( &host->lock );
1206 return NULL;
1211 url = xmalloc( sizeof( httpd_url_t ) );
1212 url->host = host;
1214 vlc_mutex_init( &url->lock );
1215 url->psz_url = strdup( psz_url );
1216 url->psz_user = strdup( psz_user ? psz_user : "" );
1217 url->psz_password = strdup( psz_password ? psz_password : "" );
1218 url->p_acl = ACL_Duplicate( host, p_acl );
1219 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1221 url->catch[i].cb = NULL;
1222 url->catch[i].p_sys = NULL;
1225 TAB_APPEND( host->i_url, host->url, url );
1226 vlc_cond_signal( &host->wait );
1227 vlc_mutex_unlock( &host->lock );
1229 return url;
1232 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1233 const char *psz_user, const char *psz_password,
1234 const vlc_acl_t *p_acl )
1236 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1237 psz_password, p_acl, false );
1240 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1241 const char *psz_user, const char *psz_password,
1242 const vlc_acl_t *p_acl )
1244 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1245 psz_password, p_acl, true );
1248 /* register callback on a url */
1249 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1250 httpd_callback_sys_t *p_sys )
1252 vlc_mutex_lock( &url->lock );
1253 url->catch[i_msg].cb = cb;
1254 url->catch[i_msg].p_sys= p_sys;
1255 vlc_mutex_unlock( &url->lock );
1257 return VLC_SUCCESS;
1260 /* delete a url */
1261 void httpd_UrlDelete( httpd_url_t *url )
1263 httpd_host_t *host = url->host;
1264 int i;
1266 vlc_mutex_lock( &host->lock );
1267 TAB_REMOVE( host->i_url, host->url, url );
1269 vlc_mutex_destroy( &url->lock );
1270 free( url->psz_url );
1271 free( url->psz_user );
1272 free( url->psz_password );
1273 ACL_Destroy( url->p_acl );
1275 for( i = 0; i < host->i_client; i++ )
1277 httpd_client_t *client = host->client[i];
1279 if( client->url == url )
1281 /* TODO complete it */
1282 msg_Warn( host, "force closing connections" );
1283 httpd_ClientClean( client );
1284 TAB_REMOVE( host->i_client, host->client, client );
1285 free( client );
1286 i--;
1289 free( url );
1290 vlc_mutex_unlock( &host->lock );
1293 static void httpd_MsgInit( httpd_message_t *msg )
1295 msg->cl = NULL;
1296 msg->i_type = HTTPD_MSG_NONE;
1297 msg->i_proto = HTTPD_PROTO_NONE;
1298 msg->i_version = -1; /* FIXME */
1300 msg->i_status = 0;
1302 msg->psz_url = NULL;
1303 msg->psz_args = NULL;
1305 msg->i_channel = -1;
1307 msg->i_name = 0;
1308 msg->name = NULL;
1309 msg->i_value = 0;
1310 msg->value = NULL;
1312 msg->i_body_offset = 0;
1313 msg->i_body = 0;
1314 msg->p_body = NULL;
1317 static void httpd_MsgClean( httpd_message_t *msg )
1319 int i;
1321 free( msg->psz_url );
1322 free( msg->psz_args );
1323 for( i = 0; i < msg->i_name; i++ )
1325 free( msg->name[i] );
1326 free( msg->value[i] );
1328 free( msg->name );
1329 free( msg->value );
1330 free( msg->p_body );
1331 httpd_MsgInit( msg );
1334 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1336 int i;
1338 for( i = 0; i < msg->i_name; i++ )
1340 if( !strcasecmp( msg->name[i], name ))
1342 return msg->value[i];
1345 return NULL;
1348 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1350 va_list args;
1351 char *value = NULL;
1353 va_start( args, psz_value );
1354 if( us_vasprintf( &value, psz_value, args ) == -1 )
1355 value = NULL;
1356 va_end( args );
1358 if( value == NULL )
1359 return;
1361 name = strdup( name );
1362 if( name == NULL )
1364 free( value );
1365 return;
1368 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1369 TAB_APPEND( msg->i_value, msg->value, value );
1372 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1374 cl->i_state = HTTPD_CLIENT_RECEIVING;
1375 cl->i_activity_date = now;
1376 cl->i_activity_timeout = INT64_C(10000000);
1377 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1378 cl->i_buffer = 0;
1379 cl->p_buffer = xmalloc( cl->i_buffer_size );
1380 cl->i_mode = HTTPD_CLIENT_FILE;
1381 cl->b_read_waiting = false;
1383 httpd_MsgInit( &cl->query );
1384 httpd_MsgInit( &cl->answer );
1387 void httpd_ClientModeStream( httpd_client_t *cl )
1389 cl->i_mode = HTTPD_CLIENT_STREAM;
1392 void httpd_ClientModeBidir( httpd_client_t *cl )
1394 cl->i_mode = HTTPD_CLIENT_BIDIR;
1397 char* httpd_ClientIP( const httpd_client_t *cl, char *ip, int *port )
1399 return net_GetPeerAddress( cl->fd, ip, port ) ? NULL : ip;
1402 char* httpd_ServerIP( const httpd_client_t *cl, char *ip, int *port )
1404 return net_GetSockAddress( cl->fd, ip, port ) ? NULL : ip;
1407 static void httpd_ClientClean( httpd_client_t *cl )
1409 if( cl->fd >= 0 )
1411 if( cl->p_tls != NULL )
1412 vlc_tls_ServerSessionDelete( cl->p_tls );
1413 net_Close( cl->fd );
1414 cl->fd = -1;
1417 httpd_MsgClean( &cl->answer );
1418 httpd_MsgClean( &cl->query );
1420 free( cl->p_buffer );
1421 cl->p_buffer = NULL;
1424 static httpd_client_t *httpd_ClientNew( int fd, vlc_tls_t *p_tls, mtime_t now )
1426 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1428 if( !cl ) return NULL;
1430 cl->i_ref = 0;
1431 cl->fd = fd;
1432 cl->url = NULL;
1433 cl->p_tls = p_tls;
1435 httpd_ClientInit( cl, now );
1437 return cl;
1440 static
1441 ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
1443 vlc_tls_t *p_tls;
1444 ssize_t val;
1446 p_tls = cl->p_tls;
1448 val = p_tls ? tls_Recv (p_tls, p, i_len)
1449 : recv (cl->fd, p, i_len, 0);
1450 while (val == -1 && errno == EINTR);
1451 return val;
1454 static
1455 ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
1457 vlc_tls_t *p_tls;
1458 ssize_t val;
1460 p_tls = cl->p_tls;
1462 val = p_tls ? tls_Send( p_tls, p, i_len )
1463 : send (cl->fd, p, i_len, 0);
1464 while (val == -1 && errno == EINTR);
1465 return val;
1469 static const struct
1471 const char name[16];
1472 int i_type;
1473 int i_proto;
1475 msg_type[] =
1477 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1478 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1479 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1480 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1481 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1482 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1483 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1484 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1485 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1486 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1487 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1491 static void httpd_ClientRecv( httpd_client_t *cl )
1493 int i_len;
1495 /* ignore leading whites */
1496 if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
1497 ( cl->i_buffer == 0 ) )
1499 unsigned char c;
1501 i_len = httpd_NetRecv( cl, &c, 1 );
1503 if( ( i_len > 0 ) && ( strchr( "\r\n\t ", c ) == NULL ) )
1505 cl->p_buffer[0] = c;
1506 cl->i_buffer++;
1509 else
1510 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1512 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1513 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1514 7 - cl->i_buffer );
1515 if( i_len > 0 )
1517 cl->i_buffer += i_len;
1520 if( ( cl->i_buffer >= 4 ) && ( cl->p_buffer[0] == '$' ) )
1522 /* Interleaved RTP over RTSP */
1523 cl->query.i_proto = HTTPD_PROTO_RTSP;
1524 cl->query.i_type = HTTPD_MSG_CHANNEL;
1525 cl->query.i_channel = cl->p_buffer[1];
1526 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1527 cl->query.p_body = xmalloc( cl->query.i_body );
1528 cl->i_buffer -= 4;
1529 memcpy( cl->query.p_body, cl->p_buffer + 4, cl->i_buffer );
1531 else
1532 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1533 * this is the maximum we can ask at this point. */
1534 if( cl->i_buffer >= 7 )
1536 if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
1538 cl->query.i_proto = HTTPD_PROTO_HTTP;
1539 cl->query.i_type = HTTPD_MSG_ANSWER;
1541 else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
1543 cl->query.i_proto = HTTPD_PROTO_RTSP;
1544 cl->query.i_type = HTTPD_MSG_ANSWER;
1546 else
1548 /* We need the full request line to determine the protocol. */
1549 cl->query.i_proto = HTTPD_PROTO_HTTP0;
1550 cl->query.i_type = HTTPD_MSG_NONE;
1554 else if( cl->query.i_body > 0 )
1556 /* we are reading the body of a request or a channel */
1557 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1558 cl->query.i_body - cl->i_buffer );
1559 if( i_len > 0 )
1561 cl->i_buffer += i_len;
1563 if( cl->i_buffer >= cl->query.i_body )
1565 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1568 else
1570 /* we are reading a header -> char by char */
1571 for( ;; )
1573 if( cl->i_buffer == cl->i_buffer_size )
1575 uint8_t *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
1576 if( newbuf == NULL )
1578 i_len = 0;
1579 break;
1582 cl->p_buffer = newbuf;
1583 cl->i_buffer_size += 1024;
1586 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1587 if( i_len <= 0 )
1589 break;
1591 cl->i_buffer++;
1593 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP0 )
1594 && ( cl->p_buffer[cl->i_buffer - 1] == '\n' ) )
1596 /* Request line is now complete */
1597 const char *p = memchr( cl->p_buffer, ' ', cl->i_buffer );
1598 size_t len;
1600 assert( cl->query.i_type == HTTPD_MSG_NONE );
1602 if( p == NULL ) /* no URI: evil guy */
1604 i_len = 0; /* drop connection */
1605 break;
1609 p++; /* skips extra spaces */
1610 while( *p == ' ' );
1612 p = memchr( p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p );
1613 if( p == NULL ) /* no explicit protocol: HTTP/0.9 */
1615 i_len = 0; /* not supported currently -> drop */
1616 break;
1620 p++; /* skips extra spaces ever again */
1621 while( *p == ' ' );
1623 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
1624 if( len < 7 ) /* foreign protocol */
1625 i_len = 0; /* I don't understand -> drop */
1626 else
1627 if( memcmp( p, "HTTP/1.", 7 ) == 0 )
1629 cl->query.i_proto = HTTPD_PROTO_HTTP;
1630 cl->query.i_version = atoi( p + 7 );
1632 else
1633 if( memcmp( p, "RTSP/1.", 7 ) == 0 )
1635 cl->query.i_proto = HTTPD_PROTO_RTSP;
1636 cl->query.i_version = atoi( p + 7 );
1638 else
1639 if( memcmp( p, "HTTP/", 5 ) == 0 )
1641 const uint8_t sorry[] =
1642 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1643 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1644 i_len = 0; /* drop */
1646 else
1647 if( memcmp( p, "RTSP/", 5 ) == 0 )
1649 const uint8_t sorry[] =
1650 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1651 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1652 i_len = 0; /* drop */
1654 else /* yet another foreign protocol */
1655 i_len = 0;
1657 if( i_len == 0 )
1658 break;
1661 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1662 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1664 char *p;
1666 /* we have finished the header so parse it and set i_body */
1667 cl->p_buffer[cl->i_buffer] = '\0';
1669 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1671 /* FIXME:
1672 * assume strlen( "HTTP/1.x" ) = 8
1674 cl->query.i_status =
1675 strtol( (char *)&cl->p_buffer[8],
1676 &p, 0 );
1677 while( *p == ' ' )
1678 p++;
1680 else
1682 unsigned i;
1684 p = NULL;
1685 cl->query.i_type = HTTPD_MSG_NONE;
1687 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1689 for( i = 0; msg_type[i].name[0]; i++ )
1691 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1692 strlen( msg_type[i].name ) ) )
1694 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1695 cl->query.i_type = msg_type[i].i_type;
1696 if( cl->query.i_proto != msg_type[i].i_proto )
1698 p = NULL;
1699 cl->query.i_proto = HTTPD_PROTO_NONE;
1700 cl->query.i_type = HTTPD_MSG_NONE;
1702 break;
1705 if( p == NULL )
1707 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1709 cl->query.i_proto = HTTPD_PROTO_HTTP;
1711 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1713 cl->query.i_proto = HTTPD_PROTO_RTSP;
1716 else
1718 char *p2;
1719 char *p3;
1721 while( *p == ' ' )
1723 p++;
1725 p2 = strchr( p, ' ' );
1726 if( p2 )
1728 *p2++ = '\0';
1730 if( !strncasecmp( p, ( cl->query.i_proto
1731 == HTTPD_PROTO_HTTP ) ? "http:" : "rtsp:", 5 ) )
1732 { /* Skip hier-part of URL (if present) */
1733 p += 5;
1734 if( !strncmp( p, "//", 2 ) ) /* skip authority */
1735 { /* see RFC3986 §3.2 */
1736 p += 2;
1737 p += strcspn( p, "/?#" );
1740 else
1741 if( !strncasecmp( p, ( cl->query.i_proto
1742 == HTTPD_PROTO_HTTP ) ? "https:" : "rtsps:", 6 ) )
1743 { /* Skip hier-part of URL (if present) */
1744 p += 6;
1745 if( !strncmp( p, "//", 2 ) ) /* skip authority */
1746 { /* see RFC3986 §3.2 */
1747 p += 2;
1748 p += strcspn( p, "/?#" );
1752 cl->query.psz_url = strdup( p );
1753 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1755 *p3++ = '\0';
1756 cl->query.psz_args = (uint8_t *)strdup( p3 );
1758 p = p2;
1761 if( p )
1763 p = strchr( p, '\n' );
1765 if( p )
1767 while( *p == '\n' || *p == '\r' )
1769 p++;
1771 while( p && *p != '\0' )
1773 char *line = p;
1774 char *eol = p = strchr( p, '\n' );
1775 char *colon;
1777 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1779 *eol-- = '\0';
1782 if( ( colon = strchr( line, ':' ) ) )
1784 char *name;
1785 char *value;
1787 *colon++ = '\0';
1788 while( *colon == ' ' )
1790 colon++;
1792 name = strdup( line );
1793 value = strdup( colon );
1795 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1796 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1798 if( !strcasecmp( name, "Content-Length" ) )
1800 cl->query.i_body = atol( value );
1804 if( p )
1806 p++;
1807 while( *p == '\n' || *p == '\r' )
1809 p++;
1814 if( cl->query.i_body > 0 )
1816 /* TODO Mhh, handle the case where the client only
1817 * sends a request and closes the connection to
1818 * mark the end of the body (probably only RTSP) */
1819 cl->query.p_body = malloc( cl->query.i_body );
1820 cl->i_buffer = 0;
1821 if ( cl->query.p_body == NULL )
1823 switch (cl->query.i_proto)
1825 case HTTPD_PROTO_HTTP:
1827 const uint8_t sorry[] =
1828 "HTTP/1.1 413 Request Entity Too Large\r\n\r\n";
1829 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1830 break;
1832 case HTTPD_PROTO_RTSP:
1834 const uint8_t sorry[] =
1835 "RTSP/1.0 413 Request Entity Too Large\r\n\r\n";
1836 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1837 break;
1839 default:
1840 assert( 0 );
1842 i_len = 0; /* drop */
1844 break;
1846 else
1848 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1854 /* check if the client is to be set to dead */
1855 #if defined( WIN32 ) || defined( UNDER_CE )
1856 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1857 #else
1858 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1859 #endif
1861 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1863 /* connection closed -> end of data */
1864 if( cl->query.i_body > 0 )
1866 cl->query.i_body = cl->i_buffer;
1868 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1870 else
1872 cl->i_state = HTTPD_CLIENT_DEAD;
1876 /* XXX: for QT I have to disable timeout. Try to find why */
1877 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1878 cl->i_activity_timeout = 0;
1880 #if 0 /* Debugging only */
1881 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1883 int i;
1885 fprintf( stderr, "received new request\n" );
1886 fprintf( stderr, " - proto=%s\n",
1887 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1888 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1889 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1890 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1892 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1893 cl->query.psz_status );
1895 else if( cl->query.i_type != HTTPD_MSG_NONE )
1897 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1899 for( i = 0; i < cl->query.i_name; i++ )
1901 fprintf( stderr, " - option name='%s' value='%s'\n",
1902 cl->query.name[i], cl->query.value[i] );
1905 #endif
1908 static void httpd_ClientSend( httpd_client_t *cl )
1910 int i;
1911 int i_len;
1913 if( cl->i_buffer < 0 )
1915 /* We need to create the header */
1916 int i_size = 0;
1917 char *p;
1918 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1920 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1921 for( i = 0; i < cl->answer.i_name; i++ )
1923 i_size += strlen( cl->answer.name[i] ) + 2 +
1924 strlen( cl->answer.value[i] ) + 2;
1927 if( cl->i_buffer_size < i_size )
1929 cl->i_buffer_size = i_size;
1930 free( cl->p_buffer );
1931 cl->p_buffer = xmalloc( i_size );
1933 p = (char *)cl->p_buffer;
1935 p += sprintf( p, "%s.%u %d %s\r\n",
1936 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1937 cl->answer.i_version,
1938 cl->answer.i_status, psz_status );
1939 for( i = 0; i < cl->answer.i_name; i++ )
1941 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1942 cl->answer.value[i] );
1944 p += sprintf( p, "\r\n" );
1946 cl->i_buffer = 0;
1947 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1949 /*fprintf( stderr, "sending answer\n" );
1950 fprintf( stderr, "%s", cl->p_buffer );*/
1953 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1954 cl->i_buffer_size - cl->i_buffer );
1955 if( i_len >= 0 )
1957 cl->i_buffer += i_len;
1959 if( cl->i_buffer >= cl->i_buffer_size )
1961 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1962 !cl->b_read_waiting )
1964 /* catch more body data */
1965 int i_msg = cl->query.i_type;
1966 int64_t i_offset = cl->answer.i_body_offset;
1968 httpd_MsgClean( &cl->answer );
1969 cl->answer.i_body_offset = i_offset;
1971 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1972 &cl->answer, &cl->query );
1975 if( cl->answer.i_body > 0 )
1977 /* send the body data */
1978 free( cl->p_buffer );
1979 cl->p_buffer = cl->answer.p_body;
1980 cl->i_buffer_size = cl->answer.i_body;
1981 cl->i_buffer = 0;
1983 cl->answer.i_body = 0;
1984 cl->answer.p_body = NULL;
1986 else
1988 /* send finished */
1989 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1993 else
1995 #if defined( WIN32 ) || defined( UNDER_CE )
1996 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1997 #else
1998 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1999 #endif
2001 /* error */
2002 cl->i_state = HTTPD_CLIENT_DEAD;
2007 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
2009 switch( vlc_tls_ServerSessionHandshake( cl->p_tls ) )
2011 case 0:
2012 cl->i_state = HTTPD_CLIENT_RECEIVING;
2013 break;
2015 case -1:
2016 cl->i_state = HTTPD_CLIENT_DEAD;
2017 cl->p_tls = NULL;
2018 break;
2020 case 2:
2021 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2025 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2027 switch( vlc_tls_ServerSessionHandshake( cl->p_tls ) )
2029 case 0:
2030 cl->i_state = HTTPD_CLIENT_RECEIVING;
2031 break;
2033 case -1:
2034 cl->i_state = HTTPD_CLIENT_DEAD;
2035 cl->p_tls = NULL;
2036 break;
2038 case 1:
2039 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2040 break;
2044 static void* httpd_HostThread( void *data )
2046 httpd_host_t *host = data;
2047 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
2048 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
2049 int evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
2051 for( ;; )
2053 struct pollfd ufd[host->nfd + host->i_client + 1];
2054 unsigned nfd;
2055 for( nfd = 0; nfd < host->nfd; nfd++ )
2057 ufd[nfd].fd = host->fds[nfd];
2058 ufd[nfd].events = POLLIN;
2059 ufd[nfd].revents = 0;
2062 /* add all socket that should be read/write and close dead connection */
2063 vlc_mutex_lock( &host->lock );
2064 while( host->i_url <= 0 && host->i_ref > 0 )
2065 vlc_cond_wait( &host->wait, &host->lock );
2067 mtime_t now = mdate();
2068 bool b_low_delay = false;
2070 for(int i_client = 0; i_client < host->i_client; i_client++ )
2072 httpd_client_t *cl = host->client[i_client];
2073 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2074 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2075 ( cl->i_activity_timeout > 0 &&
2076 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2078 httpd_ClientClean( cl );
2079 stats_UpdateInteger( host, p_active_counter, -1, NULL );
2080 TAB_REMOVE( host->i_client, host->client, cl );
2081 free( cl );
2082 i_client--;
2083 continue;
2086 struct pollfd *pufd = ufd + nfd;
2087 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2089 pufd->fd = cl->fd;
2090 pufd->events = pufd->revents = 0;
2092 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2093 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2095 pufd->events = POLLIN;
2097 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2098 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2100 pufd->events = POLLOUT;
2102 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2104 httpd_message_t *answer = &cl->answer;
2105 httpd_message_t *query = &cl->query;
2106 int i_msg = query->i_type;
2108 httpd_MsgInit( answer );
2110 /* Handle what we received */
2111 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2112 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2114 /* we can only receive request from client when not
2115 * in BIDIR mode */
2116 cl->url = NULL;
2117 cl->i_state = HTTPD_CLIENT_DEAD;
2119 else if( i_msg == HTTPD_MSG_ANSWER )
2121 /* We are in BIDIR mode, trigger the callback and then
2122 * check for new data */
2123 if( cl->url && cl->url->catch[i_msg].cb )
2125 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2126 cl, NULL, query );
2128 cl->i_state = HTTPD_CLIENT_WAITING;
2130 else if( i_msg == HTTPD_MSG_CHANNEL )
2132 /* We are in BIDIR mode, trigger the callback and then
2133 * check for new data */
2134 if( cl->url && cl->url->catch[i_msg].cb )
2136 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2137 cl, NULL, query );
2139 cl->i_state = HTTPD_CLIENT_WAITING;
2141 else if( i_msg == HTTPD_MSG_OPTIONS )
2144 answer->i_type = HTTPD_MSG_ANSWER;
2145 answer->i_proto = query->i_proto;
2146 answer->i_status = 200;
2147 answer->i_body = 0;
2148 answer->p_body = NULL;
2150 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
2151 httpd_MsgAdd( answer, "Content-Length", "0" );
2153 switch( query->i_proto )
2155 case HTTPD_PROTO_HTTP:
2156 answer->i_version = 1;
2157 httpd_MsgAdd( answer, "Allow",
2158 "GET,HEAD,POST,OPTIONS" );
2159 break;
2161 case HTTPD_PROTO_RTSP:
2163 const char *p;
2164 answer->i_version = 0;
2166 p = httpd_MsgGet( query, "Cseq" );
2167 if( p != NULL )
2168 httpd_MsgAdd( answer, "Cseq", "%s", p );
2169 p = httpd_MsgGet( query, "Timestamp" );
2170 if( p != NULL )
2171 httpd_MsgAdd( answer, "Timestamp", "%s", p );
2173 p = httpd_MsgGet( query, "Require" );
2174 if( p != NULL )
2176 answer->i_status = 551;
2177 httpd_MsgAdd( query, "Unsupported", "%s", p );
2180 httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
2181 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
2182 break;
2186 cl->i_buffer = -1; /* Force the creation of the answer in
2187 * httpd_ClientSend */
2188 cl->i_state = HTTPD_CLIENT_SENDING;
2190 else if( i_msg == HTTPD_MSG_NONE )
2192 if( query->i_proto == HTTPD_PROTO_NONE )
2194 cl->url = NULL;
2195 cl->i_state = HTTPD_CLIENT_DEAD;
2197 else
2199 char *p;
2201 /* unimplemented */
2202 answer->i_proto = query->i_proto ;
2203 answer->i_type = HTTPD_MSG_ANSWER;
2204 answer->i_version= 0;
2205 answer->i_status = 501;
2207 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2208 answer->p_body = (uint8_t *)p;
2209 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2211 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2212 cl->i_state = HTTPD_CLIENT_SENDING;
2215 else
2217 bool b_auth_failed = false;
2218 bool b_hosts_failed = false;
2220 /* Search the url and trigger callbacks */
2221 for(int i = 0; i < host->i_url; i++ )
2223 httpd_url_t *url = host->url[i];
2225 if( !strcmp( url->psz_url, query->psz_url ) )
2227 if( url->catch[i_msg].cb )
2229 if( answer && ( url->p_acl != NULL ) )
2231 char ip[NI_MAXNUMERICHOST];
2233 if( ( httpd_ClientIP( cl, ip, NULL ) == NULL )
2234 || ACL_Check( url->p_acl, ip ) )
2236 b_hosts_failed = true;
2237 break;
2241 if( answer && ( *url->psz_user || *url->psz_password ) )
2243 /* create the headers */
2244 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2245 char *user = NULL, *pass = NULL;
2247 if( b64 != NULL
2248 && !strncasecmp( b64, "BASIC", 5 ) )
2250 b64 += 5;
2251 while( *b64 == ' ' )
2252 b64++;
2254 user = vlc_b64_decode( b64 );
2255 if (user != NULL)
2257 pass = strchr (user, ':');
2258 if (pass != NULL)
2259 *pass++ = '\0';
2263 if ((user == NULL) || (pass == NULL)
2264 || strcmp (user, url->psz_user)
2265 || strcmp (pass, url->psz_password))
2267 httpd_MsgAdd( answer,
2268 "WWW-Authenticate",
2269 "Basic realm=\"VLC stream\"" );
2270 /* We fail for all url */
2271 b_auth_failed = true;
2272 free( user );
2273 break;
2276 free( user );
2279 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2281 if( answer->i_proto == HTTPD_PROTO_NONE )
2283 /* Raw answer from a CGI */
2284 cl->i_buffer = cl->i_buffer_size;
2286 else
2287 cl->i_buffer = -1;
2289 /* only one url can answer */
2290 answer = NULL;
2291 if( cl->url == NULL )
2293 cl->url = url;
2300 if( answer )
2302 char *p;
2304 answer->i_proto = query->i_proto;
2305 answer->i_type = HTTPD_MSG_ANSWER;
2306 answer->i_version= 0;
2308 if( b_hosts_failed )
2310 answer->i_status = 403;
2312 else if( b_auth_failed )
2314 answer->i_status = 401;
2316 else
2318 /* no url registered */
2319 answer->i_status = 404;
2322 answer->i_body = httpd_HtmlError (&p,
2323 answer->i_status,
2324 query->psz_url);
2325 answer->p_body = (uint8_t *)p;
2327 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2328 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2329 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2332 cl->i_state = HTTPD_CLIENT_SENDING;
2335 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2337 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2339 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2340 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2341 bool b_connection = false;
2342 bool b_keepalive = false;
2343 bool b_query = false;
2345 cl->url = NULL;
2346 if( psz_connection )
2348 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2349 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2352 if( psz_query )
2354 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2357 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2358 ( ( cl->query.i_version == 0 && b_keepalive ) ||
2359 ( cl->query.i_version == 1 && !b_connection ) ) ) ||
2360 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2361 !b_query && !b_connection ) )
2363 httpd_MsgClean( &cl->query );
2364 httpd_MsgInit( &cl->query );
2366 cl->i_buffer = 0;
2367 cl->i_buffer_size = 1000;
2368 free( cl->p_buffer );
2369 cl->p_buffer = xmalloc( cl->i_buffer_size );
2370 cl->i_state = HTTPD_CLIENT_RECEIVING;
2372 else
2374 cl->i_state = HTTPD_CLIENT_DEAD;
2376 httpd_MsgClean( &cl->answer );
2378 else if( cl->b_read_waiting )
2380 /* we have a message waiting for us to read it */
2381 httpd_MsgClean( &cl->answer );
2382 httpd_MsgClean( &cl->query );
2384 cl->i_buffer = 0;
2385 cl->i_buffer_size = 1000;
2386 free( cl->p_buffer );
2387 cl->p_buffer = xmalloc( cl->i_buffer_size );
2388 cl->i_state = HTTPD_CLIENT_RECEIVING;
2389 cl->b_read_waiting = false;
2391 else
2393 int64_t i_offset = cl->answer.i_body_offset;
2394 httpd_MsgClean( &cl->answer );
2396 cl->answer.i_body_offset = i_offset;
2397 free( cl->p_buffer );
2398 cl->p_buffer = NULL;
2399 cl->i_buffer = 0;
2400 cl->i_buffer_size = 0;
2402 cl->i_state = HTTPD_CLIENT_WAITING;
2405 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2407 int64_t i_offset = cl->answer.i_body_offset;
2408 int i_msg = cl->query.i_type;
2410 httpd_MsgInit( &cl->answer );
2411 cl->answer.i_body_offset = i_offset;
2413 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2414 &cl->answer, &cl->query );
2415 if( cl->answer.i_type != HTTPD_MSG_NONE )
2417 /* we have new data, so re-enter send mode */
2418 cl->i_buffer = 0;
2419 cl->p_buffer = cl->answer.p_body;
2420 cl->i_buffer_size = cl->answer.i_body;
2421 cl->answer.p_body = NULL;
2422 cl->answer.i_body = 0;
2423 cl->i_state = HTTPD_CLIENT_SENDING;
2427 /* Special for BIDIR mode we also check reading */
2428 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2429 cl->i_state == HTTPD_CLIENT_SENDING )
2431 pufd->events |= POLLIN;
2434 if (pufd->events != 0)
2435 nfd++;
2436 else
2437 b_low_delay = true;
2439 vlc_mutex_unlock( &host->lock );
2441 ufd[nfd].fd = evfd;
2442 ufd[nfd].events = POLLIN;
2443 ufd[nfd].revents = 0;
2444 nfd++;
2446 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2447 switch( poll( ufd, nfd, b_low_delay ? 20 : -1) )
2449 case -1:
2450 if (errno != EINTR)
2452 /* Kernel on low memory or a bug: pace */
2453 msg_Err( host, "polling error: %m" );
2454 msleep( 100000 );
2456 case 0:
2457 continue;
2460 if( ufd[nfd - 1].revents )
2461 break;
2463 /* Handle client sockets */
2464 vlc_mutex_lock( &host->lock );
2465 now = mdate();
2466 nfd = host->nfd;
2467 for( int i_client = 0; i_client < host->i_client; i_client++ )
2469 httpd_client_t *cl = host->client[i_client];
2470 const struct pollfd *pufd = &ufd[nfd];
2472 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2474 if( cl->fd != pufd->fd )
2475 continue; // we were not waiting for this client
2476 ++nfd;
2477 if( pufd->revents == 0 )
2478 continue; // no event received
2480 cl->i_activity_date = now;
2482 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2484 httpd_ClientRecv( cl );
2486 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2488 httpd_ClientSend( cl );
2490 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2492 httpd_ClientTlsHsIn( cl );
2494 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2496 httpd_ClientTlsHsOut( cl );
2499 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2500 cl->i_state == HTTPD_CLIENT_SENDING &&
2501 (pufd->revents & POLLIN) )
2503 cl->b_read_waiting = true;
2506 vlc_mutex_unlock( &host->lock );
2508 /* Handle server sockets (accept new connections) */
2509 for( nfd = 0; nfd < host->nfd; nfd++ )
2511 httpd_client_t *cl;
2512 int i_state = -1;
2513 int fd = ufd[nfd].fd;
2515 assert (fd == host->fds[nfd]);
2517 if( ufd[nfd].revents == 0 )
2518 continue;
2520 /* */
2521 fd = vlc_accept (fd, NULL, NULL, true);
2522 if (fd == -1)
2523 continue;
2524 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
2525 &(int){ 1 }, sizeof(int));
2527 vlc_tls_t *p_tls;
2529 if( host->p_tls != NULL )
2531 p_tls = vlc_tls_ServerSessionCreate( host->p_tls, fd );
2532 switch( vlc_tls_ServerSessionHandshake( p_tls ) )
2534 case -1:
2535 msg_Err( host, "Rejecting TLS connection" );
2536 /* p_tls is destroyed implicitly */
2537 net_Close( fd );
2538 fd = -1;
2539 p_tls = NULL;
2540 continue;
2542 case 1: /* missing input - most likely */
2543 i_state = HTTPD_CLIENT_TLS_HS_IN;
2544 break;
2546 case 2: /* missing output */
2547 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2548 break;
2551 else
2552 p_tls = NULL;
2554 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2555 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2556 cl = httpd_ClientNew( fd, p_tls, now );
2557 vlc_mutex_lock( &host->lock );
2558 TAB_APPEND( host->i_client, host->client, cl );
2559 vlc_mutex_unlock( &host->lock );
2560 if( i_state != -1 )
2561 cl->i_state = i_state; // override state for 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;