1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2004-2007 Rémi Denis-Courmont
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_httpd.h>
35 #include <vlc_network.h>
38 #include <vlc_strings.h>
40 #include <vlc_charset.h>
41 #include "../libvlc.h"
54 #if defined( UNDER_CE )
56 #elif defined( WIN32 )
57 # include <winsock2.h>
59 # include <sys/socket.h>
63 /* We need HUGE buffer otherwise TCP throughput is very limited */
64 #define HTTPD_CL_BUFSIZE 1000000
66 #define HTTPD_CL_BUFSIZE 10000
69 static void httpd_ClientClean( httpd_client_t
*cl
);
71 /* each host run in his own thread */
79 /* address/port and socket for listening at connections */
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
95 httpd_client_t
**client
;
98 vlc_tls_creds_t
*p_tls
;
116 httpd_callback_sys_t
*p_sys
;
117 } catch[HTTPD_MSG_MAX
];
123 HTTPD_CLIENT_RECEIVING
,
124 HTTPD_CLIENT_RECEIVE_DONE
,
126 HTTPD_CLIENT_SENDING
,
127 HTTPD_CLIENT_SEND_DONE
,
129 HTTPD_CLIENT_WAITING
,
133 HTTPD_CLIENT_TLS_HS_IN
,
134 HTTPD_CLIENT_TLS_HS_OUT
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
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 */
166 httpd_message_t query
; /* client -> httpd */
167 httpd_message_t answer
; /* httpd -> client */
174 /*****************************************************************************
176 *****************************************************************************/
179 const char psz_ext
[8];
180 const char *psz_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" },
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" },
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" },
230 static const char *httpd_MimeFromUrl( const char *psz_url
)
235 psz_ext
= strrchr( psz_url
, '.' );
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";
255 const char psz_reason
[36];
258 static const http_status_info http_reason
[] =
260 /*{ 100, "Continue" },
261 { 101, "Switching Protocols" },*/
263 /*{ 201, "Created" },
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" },
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" },
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"
346 "<title>%s</title>\n"
349 "<h1>%d %s%s%s%s</h1>\n"
351 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
353 "</html>\n", errname
, code
, errname
,
354 (url
? " (" : ""), (url
? url
: ""), (url
? ")" : ""));
366 /*****************************************************************************
367 * High Level Functions: httpd_file_t
368 *****************************************************************************/
376 httpd_file_callback_t pf_fill
;
377 httpd_file_sys_t
*p_sys
;
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
)
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
;
410 /* The file still needs to be executed. */
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
)
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
);
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
)
458 file
->psz_url
= strdup( psz_url
);
459 if( psz_mime
&& *psz_mime
)
461 file
->psz_mime
= strdup( psz_mime
);
465 file
->psz_mime
= strdup( httpd_MimeFromUrl( psz_url
) );
468 file
->pf_fill
= pf_fill
;
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
);
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
);
495 /*****************************************************************************
496 * High Level Functions: httpd_handler_t (for CGIs)
497 *****************************************************************************/
498 struct httpd_handler_t
502 httpd_handler_callback_t pf_fill
;
503 httpd_handler_sys_t
*p_sys
;
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
)
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' )
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 ) )
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
);
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
;
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
)
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
);
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
);
627 /*****************************************************************************
628 * High Level Functions: httpd_redirect_t
629 *****************************************************************************/
630 struct httpd_redirect_t
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
;
644 if( answer
== NULL
|| query
== NULL
)
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
);
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
) ) )
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
);
688 void httpd_RedirectDelete( httpd_redirect_t
*rdir
)
690 httpd_UrlDelete( rdir
->url
);
691 free( rdir
->psz_dst
);
695 /*****************************************************************************
696 * High Level Funtions: httpd_stream_t
697 *****************************************************************************/
698 struct httpd_stream_t
705 /* Header to send as first packet */
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
)
727 if( answer
->i_body_offset
> 0 )
733 fprintf( stderr
, "httpd_StreamCallBack i_body_offset=%lld\n",
734 answer
->i_body_offset
);
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 */
747 fprintf( stderr
, "fixing i_body_offset (old=%lld new=%lld)\n",
748 answer
->i_body_offset
, stream
->i_buffer_last_pos
);
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
;
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
);
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;
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;
833 answer
->i_body_offset
= 0;
838 httpd_MsgAdd( answer
, "Content-type", "%s", stream
->psz_mime
);
840 httpd_MsgAdd( answer
, "Cache-Control", "%s", "no-cache" );
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
)
859 vlc_mutex_init( &stream
->lock
);
860 if( psz_mime
&& *psz_mime
)
862 stream
->psz_mime
= strdup( psz_mime
);
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
);
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
;
896 stream
->p_header
= xmalloc( i_data
);
897 memcpy( stream
->p_header
, p_data
, i_data
);
899 vlc_mutex_unlock( &stream
->lock
);
904 int httpd_StreamSend( httpd_stream_t
*stream
, uint8_t *p_data
, int i_data
)
909 if( i_data
< 0 || p_data
== NULL
)
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
;
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
;
934 stream
->i_buffer_pos
+= i_data
;
936 vlc_mutex_unlock( &stream
->lock
);
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
);
950 /*****************************************************************************
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 *,
958 static struct httpd_t
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" );
976 msg_Err( obj
, "HTTP/TLS certificate not specified!" );
980 char *key
= var_InheritString( obj
, "http-key" );
981 vlc_tls_creds_t
*tls
= vlc_tls_ServerCreate( obj
, cert
, key
);
985 msg_Err( obj
, "HTTP/TLS certificate error (%s and %s)",
986 cert
, (key
!= NULL
) ? key
: cert
);
994 char *ca
= var_InheritString( obj
, "http-ca" );
997 if( vlc_tls_ServerAddCA( tls
, ca
) )
999 msg_Err( obj
, "HTTP/TLS CA error (%s)", ca
);
1006 char *crl
= var_InheritString( obj
, "http-crl" );
1009 if( vlc_tls_ServerAddCRL( tls
, crl
) )
1011 msg_Err( obj
, "TLS CRL error (%s)", crl
);
1018 return httpd_HostCreate( obj
, &httpd
.https
,
1019 "http-host", "https-port", tls
);
1022 vlc_tls_ServerDelete( tls
);
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
)
1040 /* to be sure to avoid multiple creation */
1041 vlc_mutex_lock( &httpd
.mutex
);
1043 /* verify if it already exist */
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
);
1053 vlc_mutex_unlock( &host
->lock
);
1055 vlc_mutex_unlock( &httpd
.mutex
);
1057 vlc_tls_ServerDelete( p_tls
);
1061 /* create the new host */
1062 host
= (httpd_host_t
*)vlc_custom_create( p_this
, sizeof (*host
),
1067 vlc_mutex_init( &host
->lock
);
1068 vlc_cond_init( &host
->wait
);
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
);
1075 if( host
->fds
== NULL
)
1077 msg_Err( p_this
, "cannot create socket(s) for HTTP host" );
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" );
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" );
1103 vlc_mutex_unlock( &httpd
.mutex
);
1108 vlc_mutex_unlock( &httpd
.mutex
);
1112 net_ListenClose( host
->fds
);
1113 vlc_cond_destroy( &host
->wait
);
1114 vlc_mutex_destroy( &host
->lock
);
1115 vlc_object_release( host
);
1119 vlc_tls_ServerDelete( p_tls
);
1125 void httpd_HostDelete( httpd_host_t
*host
)
1128 bool delete = false;
1130 vlc_mutex_lock( &httpd
.mutex
);
1132 vlc_mutex_lock( &host
->lock
);
1134 if( host
->i_ref
== 0 )
1136 vlc_cond_signal( &host
->wait
);
1139 vlc_mutex_unlock( &host
->lock
);
1143 vlc_mutex_unlock( &httpd
.mutex
);
1144 msg_Dbg( host
, "httpd_HostDelete: host still in use" );
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
);
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
)
1177 else if( host
== httpd
.https
)
1179 else if( host
== httpd
.rtsp
)
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
)
1194 assert( psz_url
!= NULL
);
1196 vlc_mutex_lock( &host
->lock
);
1199 for( i
= 0; i
< host
->i_url
; i
++ )
1201 if( !strcmp( psz_url
, host
->url
[i
]->psz_url
) )
1204 "cannot add '%s' (url already defined)", psz_url
);
1205 vlc_mutex_unlock( &host
->lock
);
1211 url
= xmalloc( sizeof( httpd_url_t
) );
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
);
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
);
1261 void httpd_UrlDelete( httpd_url_t
*url
)
1263 httpd_host_t
*host
= url
->host
;
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
);
1290 vlc_mutex_unlock( &host
->lock
);
1293 static void httpd_MsgInit( httpd_message_t
*msg
)
1296 msg
->i_type
= HTTPD_MSG_NONE
;
1297 msg
->i_proto
= HTTPD_PROTO_NONE
;
1298 msg
->i_version
= -1; /* FIXME */
1302 msg
->psz_url
= NULL
;
1303 msg
->psz_args
= NULL
;
1305 msg
->i_channel
= -1;
1312 msg
->i_body_offset
= 0;
1317 static void httpd_MsgClean( httpd_message_t
*msg
)
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
] );
1330 free( msg
->p_body
);
1331 httpd_MsgInit( msg
);
1334 const char *httpd_MsgGet( const httpd_message_t
*msg
, const char *name
)
1338 for( i
= 0; i
< msg
->i_name
; i
++ )
1340 if( !strcasecmp( msg
->name
[i
], name
))
1342 return msg
->value
[i
];
1348 void httpd_MsgAdd( httpd_message_t
*msg
, const char *name
, const char *psz_value
, ... )
1353 va_start( args
, psz_value
);
1354 if( us_vasprintf( &value
, psz_value
, args
) == -1 )
1361 name
= strdup( name
);
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
;
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
)
1411 if( cl
->p_tls
!= NULL
)
1412 vlc_tls_ServerSessionDelete( cl
->p_tls
);
1413 net_Close( cl
->fd
);
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
;
1435 httpd_ClientInit( cl
, now
);
1441 ssize_t
httpd_NetRecv (httpd_client_t
*cl
, uint8_t *p
, size_t i_len
)
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
);
1455 ssize_t
httpd_NetSend (httpd_client_t
*cl
, const uint8_t *p
, size_t i_len
)
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
);
1471 const char name
[16];
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
)
1495 /* ignore leading whites */
1496 if( ( cl
->query
.i_proto
== HTTPD_PROTO_NONE
) &&
1497 ( cl
->i_buffer
== 0 ) )
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
;
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
],
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
);
1529 memcpy( cl
->query
.p_body
, cl
->p_buffer
+ 4, cl
->i_buffer
);
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
;
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
);
1561 cl
->i_buffer
+= i_len
;
1563 if( cl
->i_buffer
>= cl
->query
.i_body
)
1565 cl
->i_state
= HTTPD_CLIENT_RECEIVE_DONE
;
1570 /* we are reading a header -> char by char */
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
)
1582 cl
->p_buffer
= newbuf
;
1583 cl
->i_buffer_size
+= 1024;
1586 i_len
= httpd_NetRecv (cl
, &cl
->p_buffer
[cl
->i_buffer
], 1 );
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
);
1600 assert( cl
->query
.i_type
== HTTPD_MSG_NONE
);
1602 if( p
== NULL
) /* no URI: evil guy */
1604 i_len
= 0; /* drop connection */
1609 p
++; /* skips extra spaces */
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 */
1620 p
++; /* skips extra spaces ever again */
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 */
1627 if( memcmp( p
, "HTTP/1.", 7 ) == 0 )
1629 cl
->query
.i_proto
= HTTPD_PROTO_HTTP
;
1630 cl
->query
.i_version
= atoi( p
+ 7 );
1633 if( memcmp( p
, "RTSP/1.", 7 ) == 0 )
1635 cl
->query
.i_proto
= HTTPD_PROTO_RTSP
;
1636 cl
->query
.i_version
= atoi( p
+ 7 );
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 */
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 */
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 ) ) )
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
)
1672 * assume strlen( "HTTP/1.x" ) = 8
1674 cl
->query
.i_status
=
1675 strtol( (char *)&cl
->p_buffer
[8],
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
)
1699 cl
->query
.i_proto
= HTTPD_PROTO_NONE
;
1700 cl
->query
.i_type
= HTTPD_MSG_NONE
;
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
;
1725 p2
= strchr( p
, ' ' );
1730 if( !strncasecmp( p
, ( cl
->query
.i_proto
1731 == HTTPD_PROTO_HTTP
) ? "http:" : "rtsp:", 5 ) )
1732 { /* Skip hier-part of URL (if present) */
1734 if( !strncmp( p
, "//", 2 ) ) /* skip authority */
1735 { /* see RFC3986 §3.2 */
1737 p
+= strcspn( p
, "/?#" );
1741 if( !strncasecmp( p
, ( cl
->query
.i_proto
1742 == HTTPD_PROTO_HTTP
) ? "https:" : "rtsps:", 6 ) )
1743 { /* Skip hier-part of URL (if present) */
1745 if( !strncmp( p
, "//", 2 ) ) /* skip authority */
1746 { /* see RFC3986 §3.2 */
1748 p
+= strcspn( p
, "/?#" );
1752 cl
->query
.psz_url
= strdup( p
);
1753 if( ( p3
= strchr( cl
->query
.psz_url
, '?' ) ) )
1756 cl
->query
.psz_args
= (uint8_t *)strdup( p3
);
1763 p
= strchr( p
, '\n' );
1767 while( *p
== '\n' || *p
== '\r' )
1771 while( p
&& *p
!= '\0' )
1774 char *eol
= p
= strchr( p
, '\n' );
1777 while( eol
&& eol
>= line
&& ( *eol
== '\n' || *eol
== '\r' ) )
1782 if( ( colon
= strchr( line
, ':' ) ) )
1788 while( *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
);
1807 while( *p
== '\n' || *p
== '\r' )
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
);
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 );
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 );
1842 i_len
= 0; /* drop */
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 ) )
1858 if( ( i_len
< 0 && errno
!= EAGAIN
) || ( i_len
== 0 ) )
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
;
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
)
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
] );
1908 static void httpd_ClientSend( httpd_client_t
*cl
)
1913 if( cl
->i_buffer
< 0 )
1915 /* We need to create the header */
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" );
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
);
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
;
1983 cl
->answer
.i_body
= 0;
1984 cl
->answer
.p_body
= NULL
;
1989 cl
->i_state
= HTTPD_CLIENT_SEND_DONE
;
1995 #if defined( WIN32 ) || defined( UNDER_CE )
1996 if( ( i_len
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
) || ( i_len
== 0 ) )
1998 if( ( i_len
< 0 && errno
!= EAGAIN
) || ( i_len
== 0 ) )
2002 cl
->i_state
= HTTPD_CLIENT_DEAD
;
2007 static void httpd_ClientTlsHsIn( httpd_client_t
*cl
)
2009 switch( vlc_tls_ServerSessionHandshake( cl
->p_tls
) )
2012 cl
->i_state
= HTTPD_CLIENT_RECEIVING
;
2016 cl
->i_state
= HTTPD_CLIENT_DEAD
;
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
) )
2030 cl
->i_state
= HTTPD_CLIENT_RECEIVING
;
2034 cl
->i_state
= HTTPD_CLIENT_DEAD
;
2039 cl
->i_state
= HTTPD_CLIENT_TLS_HS_IN
;
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
) );
2053 struct pollfd ufd
[host
->nfd
+ host
->i_client
+ 1];
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
);
2086 struct pollfd
*pufd
= ufd
+ nfd
;
2087 assert (pufd
< ufd
+ (sizeof (ufd
) / sizeof (ufd
[0])));
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
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
,
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
,
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;
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" );
2161 case HTTPD_PROTO_RTSP
:
2164 answer
->i_version
= 0;
2166 p
= httpd_MsgGet( query
, "Cseq" );
2168 httpd_MsgAdd( answer
, "Cseq", "%s", p
);
2169 p
= httpd_MsgGet( query
, "Timestamp" );
2171 httpd_MsgAdd( answer
, "Timestamp", "%s", p
);
2173 p
= httpd_MsgGet( query
, "Require" );
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" );
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
)
2195 cl
->i_state
= HTTPD_CLIENT_DEAD
;
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
;
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;
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
;
2248 && !strncasecmp( b64
, "BASIC", 5 ) )
2251 while( *b64
== ' ' )
2254 user
= vlc_b64_decode( b64
);
2257 pass
= strchr (user
, ':');
2263 if ((user
== NULL
) || (pass
== NULL
)
2264 || strcmp (user
, url
->psz_user
)
2265 || strcmp (pass
, url
->psz_password
))
2267 httpd_MsgAdd( answer
,
2269 "Basic realm=\"VLC stream\"" );
2270 /* We fail for all url */
2271 b_auth_failed
= true;
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
;
2289 /* only one url can answer */
2291 if( cl
->url
== NULL
)
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;
2318 /* no url registered */
2319 answer
->i_status
= 404;
2322 answer
->i_body
= httpd_HtmlError (&p
,
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;
2346 if( psz_connection
)
2348 b_connection
= ( strcasecmp( psz_connection
, "Close" ) == 0 );
2349 b_keepalive
= ( strcasecmp( psz_connection
, "Keep-Alive" ) == 0 );
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
);
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
;
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
);
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;
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
;
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 */
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)
2439 vlc_mutex_unlock( &host
->lock
);
2442 ufd
[nfd
].events
= POLLIN
;
2443 ufd
[nfd
].revents
= 0;
2446 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2447 switch( poll( ufd
, nfd
, b_low_delay
? 20 : -1) )
2452 /* Kernel on low memory or a bug: pace */
2453 msg_Err( host
, "polling error: %m" );
2460 if( ufd
[nfd
- 1].revents
)
2463 /* Handle client sockets */
2464 vlc_mutex_lock( &host
->lock
);
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
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
++ )
2513 int fd
= ufd
[nfd
].fd
;
2515 assert (fd
== host
->fds
[nfd
]);
2517 if( ufd
[nfd
].revents
== 0 )
2521 fd
= vlc_accept (fd
, NULL
, NULL
, true);
2524 setsockopt (fd
, SOL_SOCKET
, SO_REUSEADDR
,
2525 &(int){ 1 }, sizeof(int));
2529 if( host
->p_tls
!= NULL
)
2531 p_tls
= vlc_tls_ServerSessionCreate( host
->p_tls
, fd
);
2532 switch( vlc_tls_ServerSessionHandshake( p_tls
) )
2535 msg_Err( host
, "Rejecting TLS connection" );
2536 /* p_tls is destroyed implicitly */
2542 case 1: /* missing input - most likely */
2543 i_state
= HTTPD_CLIENT_TLS_HS_IN
;
2546 case 2: /* missing output */
2547 i_state
= HTTPD_CLIENT_TLS_HS_OUT
;
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
);
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
);