1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * 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>
42 #include "../libvlc.h"
55 #if defined( UNDER_CE )
57 #elif defined( WIN32 )
58 # include <winsock2.h>
60 # include <sys/socket.h>
64 /* We need HUGE buffer otherwise TCP throughput is very limited */
65 #define HTTPD_CL_BUFSIZE 1000000
67 #define HTTPD_CL_BUFSIZE 10000
70 static void httpd_ClientClean( httpd_client_t *cl );
71 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *, const char *,
72 const char *, const char *,
75 /* each host run in his own thread */
83 /* address/port and socket for listening at connections */
92 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
93 * This will slow down the url research but make my live easier
94 * All url will have their cb trigger, but only the first one can answer
100 httpd_client_t **client;
103 vlc_tls_creds_t *p_tls;
121 httpd_callback_sys_t *p_sys;
122 } catch[HTTPD_MSG_MAX];
128 HTTPD_CLIENT_RECEIVING,
129 HTTPD_CLIENT_RECEIVE_DONE,
131 HTTPD_CLIENT_SENDING,
132 HTTPD_CLIENT_SEND_DONE,
134 HTTPD_CLIENT_WAITING,
138 HTTPD_CLIENT_TLS_HS_IN,
139 HTTPD_CLIENT_TLS_HS_OUT
145 HTTPD_CLIENT_FILE, /* default */
146 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
149 struct httpd_client_t
160 mtime_t i_activity_date;
161 mtime_t i_activity_timeout;
163 /* buffer for reading header */
169 httpd_message_t query; /* client -> httpd */
170 httpd_message_t answer; /* httpd -> client */
177 /*****************************************************************************
179 *****************************************************************************/
182 const char psz_ext[8];
183 const char *psz_mime;
186 { ".htm", "text/html" },
187 { ".html", "text/html" },
188 { ".txt", "text/plain" },
189 { ".xml", "text/xml" },
190 { ".dtd", "text/dtd" },
192 { ".css", "text/css" },
195 { ".gif", "image/gif" },
196 { ".jpe", "image/jpeg" },
197 { ".jpg", "image/jpeg" },
198 { ".jpeg", "image/jpeg" },
199 { ".png", "image/png" },
200 /* same as modules/mux/mpjpeg.c here: */
201 { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" },
204 { ".avi", "video/avi" },
205 { ".asf", "video/x-ms-asf" },
206 { ".m1a", "audio/mpeg" },
207 { ".m2a", "audio/mpeg" },
208 { ".m1v", "video/mpeg" },
209 { ".m2v", "video/mpeg" },
210 { ".mp2", "audio/mpeg" },
211 { ".mp3", "audio/mpeg" },
212 { ".mpa", "audio/mpeg" },
213 { ".mpg", "video/mpeg" },
214 { ".mpeg", "video/mpeg" },
215 { ".mpe", "video/mpeg" },
216 { ".mov", "video/quicktime" },
217 { ".moov", "video/quicktime" },
218 { ".oga", "audio/ogg" },
219 { ".ogg", "application/ogg" },
220 { ".ogm", "application/ogg" },
221 { ".ogv", "video/ogg" },
222 { ".ogx", "application/ogg" },
223 { ".spx", "audio/ogg" },
224 { ".wav", "audio/wav" },
225 { ".wma", "audio/x-ms-wma" },
226 { ".wmv", "video/x-ms-wmv" },
233 static const char *httpd_MimeFromUrl( const char *psz_url )
238 psz_ext = strrchr( psz_url, '.' );
243 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
245 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
247 return http_mime[i].psz_mime;
251 return "application/octet-stream";
258 const char psz_reason[36];
261 static const http_status_info http_reason[] =
263 /*{ 100, "Continue" },
264 { 101, "Switching Protocols" },*/
266 /*{ 201, "Created" },
268 { 203, "Non-authoritative information" },
269 { 204, "No content" },
270 { 205, "Reset content" },
271 { 206, "Partial content" },
272 { 250, "Low on storage space" },
273 { 300, "Multiple choices" },*/
274 { 301, "Moved permanently" },
275 /*{ 302, "Moved temporarily" },
276 { 303, "See other" },
277 { 304, "Not modified" },
278 { 305, "Use proxy" },
279 { 307, "Temporary redirect" },
280 { 400, "Bad request" },*/
281 { 401, "Unauthorized" },
282 /*{ 402, "Payment Required" },*/
283 { 403, "Forbidden" },
284 { 404, "Not found" },
285 { 405, "Method not allowed" },
286 /*{ 406, "Not acceptable" },
287 { 407, "Proxy authentication required" },
288 { 408, "Request time-out" },
291 { 411, "Length required" },
292 { 412, "Precondition failed" },
293 { 413, "Request entity too large" },
294 { 414, "Request-URI too large" },
295 { 415, "Unsupported media Type" },
296 { 416, "Requested range not satisfiable" },
297 { 417, "Expectation failed" },
298 { 451, "Parameter not understood" },
299 { 452, "Conference not found" },
300 { 453, "Not enough bandwidth" },*/
301 { 454, "Session not found" },
302 { 455, "Method not valid in this State" },
303 { 456, "Header field not valid for resource" },
304 { 457, "Invalid range" },
305 /*{ 458, "Read-only parameter" },*/
306 { 459, "Aggregate operation not allowed" },
307 { 460, "Non-aggregate operation not allowed" },
308 { 461, "Unsupported transport" },
309 /*{ 462, "Destination unreachable" },*/
310 { 500, "Internal server error" },
311 { 501, "Not implemented" },
312 /*{ 502, "Bad gateway" },*/
313 { 503, "Service unavailable" },
314 /*{ 504, "Gateway time-out" },*/
315 { 505, "Protocol version not supported" },
316 { 551, "Option not supported" },
320 static const char psz_fallback_reason[5][16] =
321 { "Continue", "OK", "Found", "Client error", "Server error" };
323 static const char *httpd_ReasonFromCode( unsigned i_code )
325 const http_status_info *p;
327 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
329 for (p = http_reason; i_code > p->i_code; p++);
331 if( p->i_code == i_code )
332 return p->psz_reason;
334 return psz_fallback_reason[(i_code / 100) - 1];
338 static size_t httpd_HtmlError (char **body, int code, const char *url)
340 const char *errname = httpd_ReasonFromCode (code);
341 assert (errname != NULL);
343 int res = asprintf (body,
344 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
345 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
346 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
347 "<html lang=\"en\">\n"
349 "<title>%s</title>\n"
352 "<h1>%d %s%s%s%s</h1>\n"
354 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
356 "</html>\n", errname, code, errname,
357 (url ? " (" : ""), (url ? url : ""), (url ? ")" : ""));
369 /*****************************************************************************
370 * High Level Functions: httpd_file_t
371 *****************************************************************************/
379 httpd_file_callback_t pf_fill;
380 httpd_file_sys_t *p_sys;
385 httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
386 httpd_message_t *answer, const httpd_message_t *query )
388 httpd_file_t *file = (httpd_file_t*)p_sys;
389 uint8_t **pp_body, *p_body;
390 const char *psz_connection;
391 int *pi_body, i_body;
393 if( answer == NULL || query == NULL )
397 answer->i_proto = HTTPD_PROTO_HTTP;
398 answer->i_version= 1;
399 answer->i_type = HTTPD_MSG_ANSWER;
401 answer->i_status = 200;
403 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
404 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
406 if( query->i_type != HTTPD_MSG_HEAD )
408 pp_body = &answer->p_body;
409 pi_body = &answer->i_body;
413 /* The file still needs to be executed. */
420 if( query->i_type == HTTPD_MSG_POST )
422 /* msg_Warn not supported */
425 uint8_t *psz_args = query->psz_args;
426 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
428 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
433 /* We respect client request */
434 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
435 if( psz_connection != NULL )
437 httpd_MsgAdd( answer, "Connection", "%s", psz_connection );
440 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
445 httpd_file_t *httpd_FileNew( httpd_host_t *host,
446 const char *psz_url, const char *psz_mime,
447 const char *psz_user, const char *psz_password,
448 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
449 httpd_file_sys_t *p_sys )
451 httpd_file_t *file = xmalloc( sizeof( httpd_file_t ) );
453 file->url = httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password,
455 if( file->url == NULL )
461 file->psz_url = strdup( psz_url );
462 if( psz_mime && *psz_mime )
464 file->psz_mime = strdup( psz_mime );
468 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
471 file->pf_fill = pf_fill;
474 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
475 (httpd_callback_sys_t*)file );
476 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
477 (httpd_callback_sys_t*)file );
478 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
479 (httpd_callback_sys_t*)file );
484 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
486 httpd_file_sys_t *p_sys = file->p_sys;
488 httpd_UrlDelete( file->url );
490 free( file->psz_url );
491 free( file->psz_mime );
498 /*****************************************************************************
499 * High Level Functions: httpd_handler_t (for CGIs)
500 *****************************************************************************/
501 struct httpd_handler_t
505 httpd_handler_callback_t pf_fill;
506 httpd_handler_sys_t *p_sys;
511 httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
512 httpd_message_t *answer, const httpd_message_t *query )
514 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
515 char psz_remote_addr[NI_MAXNUMERICHOST];
517 if( answer == NULL || query == NULL )
521 answer->i_proto = HTTPD_PROTO_NONE;
522 answer->i_type = HTTPD_MSG_ANSWER;
524 /* We do it ourselves, thanks */
525 answer->i_status = 0;
527 if( httpd_ClientIP( cl, psz_remote_addr, NULL ) == NULL )
528 *psz_remote_addr = '\0';
530 uint8_t *psz_args = query->psz_args;
531 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
532 query->i_type, query->p_body, query->i_body,
533 psz_remote_addr, NULL,
534 &answer->p_body, &answer->i_body );
536 if( query->i_type == HTTPD_MSG_HEAD )
538 char *p = (char *)answer->p_body;
540 /* Looks for end of header (i.e. one empty line) */
541 while ( (p = strchr( p, '\r' )) != NULL )
543 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
544 && p[3] && p[3] == '\n' )
553 answer->i_body = strlen((char*)answer->p_body) + 1;
554 answer->p_body = xrealloc( answer->p_body, answer->i_body );
558 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
560 int i_status, i_headers;
561 char *psz_headers, *psz_new;
562 const char *psz_status;
564 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
567 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
568 if( *psz_headers == '\r' || *psz_headers == '\n' ) psz_headers++;
569 if( *psz_headers == '\n' ) psz_headers++;
570 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
575 psz_headers = (char *)answer->p_body;
576 i_headers = answer->i_body;
579 psz_status = httpd_ReasonFromCode( i_status );
580 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
581 + strlen(psz_status) + i_headers - 1;
582 psz_new = (char *)xmalloc( answer->i_body + 1);
583 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
584 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
585 free( answer->p_body );
586 answer->p_body = (uint8_t *)psz_new;
592 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
593 const char *psz_user,
594 const char *psz_password,
595 const vlc_acl_t *p_acl,
596 httpd_handler_callback_t pf_fill,
597 httpd_handler_sys_t *p_sys )
599 httpd_handler_t *handler = xmalloc( sizeof( httpd_handler_t ) );
601 handler->url = httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password,
603 if( handler->url == NULL )
609 handler->pf_fill = pf_fill;
610 handler->p_sys = p_sys;
612 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
613 (httpd_callback_sys_t*)handler );
614 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
615 (httpd_callback_sys_t*)handler );
616 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
617 (httpd_callback_sys_t*)handler );
622 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
624 httpd_handler_sys_t *p_sys = handler->p_sys;
625 httpd_UrlDelete( handler->url );
630 /*****************************************************************************
631 * High Level Functions: httpd_redirect_t
632 *****************************************************************************/
633 struct httpd_redirect_t
639 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
640 httpd_client_t *cl, httpd_message_t *answer,
641 const httpd_message_t *query )
643 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
647 if( answer == NULL || query == NULL )
651 answer->i_proto = HTTPD_PROTO_HTTP;
652 answer->i_version= 1;
653 answer->i_type = HTTPD_MSG_ANSWER;
654 answer->i_status = 301;
656 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
657 answer->p_body = (unsigned char *)p_body;
659 /* XXX check if it's ok or we need to set an absolute url */
660 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
662 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
667 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
668 const char *psz_url_src )
670 httpd_redirect_t *rdir = xmalloc( sizeof( httpd_redirect_t ) );
672 rdir->url = httpd_UrlNew( host, psz_url_src, NULL, NULL );
673 if( rdir->url == NULL )
678 rdir->psz_dst = strdup( psz_url_dst );
680 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
681 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
682 (httpd_callback_sys_t*)rdir );
683 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
684 (httpd_callback_sys_t*)rdir );
685 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
686 (httpd_callback_sys_t*)rdir );
687 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
688 (httpd_callback_sys_t*)rdir );
692 void httpd_RedirectDelete( httpd_redirect_t *rdir )
694 httpd_UrlDelete( rdir->url );
695 free( rdir->psz_dst );
699 /*****************************************************************************
700 * High Level Funtions: httpd_stream_t
701 *****************************************************************************/
702 struct httpd_stream_t
709 /* Header to send as first packet */
713 /* circular buffer */
714 int i_buffer_size; /* buffer size, can't be reallocated smaller */
715 uint8_t *p_buffer; /* buffer */
716 int64_t i_buffer_pos; /* absolute position from begining */
717 int64_t i_buffer_last_pos; /* a new connection will start with that */
720 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
721 httpd_client_t *cl, httpd_message_t *answer,
722 const httpd_message_t *query )
724 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
726 if( answer == NULL || query == NULL || cl == NULL )
731 if( answer->i_body_offset > 0 )
737 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
738 answer->i_body_offset );
741 if( answer->i_body_offset >= stream->i_buffer_pos )
743 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
744 return VLC_EGENERIC; /* wait, no data available */
746 if( answer->i_body_offset + stream->i_buffer_size <
747 stream->i_buffer_pos )
749 /* this client isn't fast enough */
751 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
752 answer->i_body_offset, stream->i_buffer_last_pos );
754 answer->i_body_offset = stream->i_buffer_last_pos;
757 i_pos = answer->i_body_offset % stream->i_buffer_size;
758 i_write = stream->i_buffer_pos - answer->i_body_offset;
759 if( i_write > HTTPD_CL_BUFSIZE )
761 i_write = HTTPD_CL_BUFSIZE;
763 else if( i_write <= 0 )
765 return VLC_EGENERIC; /* wait, no data available */
768 /* Don't go past the end of the circular buffer */
769 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
771 /* using HTTPD_MSG_ANSWER -> data available */
772 answer->i_proto = HTTPD_PROTO_HTTP;
773 answer->i_version= 0;
774 answer->i_type = HTTPD_MSG_ANSWER;
776 answer->i_body = i_write;
777 answer->p_body = xmalloc( i_write );
778 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
780 answer->i_body_offset += i_write;
786 answer->i_proto = HTTPD_PROTO_HTTP;
787 answer->i_version= 0;
788 answer->i_type = HTTPD_MSG_ANSWER;
790 answer->i_status = 200;
792 if( query->i_type != HTTPD_MSG_HEAD )
794 cl->b_stream_mode = true;
795 vlc_mutex_lock( &stream->lock );
796 /* Send the header */
797 if( stream->i_header > 0 )
799 answer->i_body = stream->i_header;
800 answer->p_body = xmalloc( stream->i_header );
801 memcpy( answer->p_body, stream->p_header, stream->i_header );
803 answer->i_body_offset = stream->i_buffer_last_pos;
804 vlc_mutex_unlock( &stream->lock );
808 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
809 answer->i_body_offset = 0;
812 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
814 bool b_xplaystream = false;
817 httpd_MsgAdd( answer, "Content-type", "%s",
818 "application/octet-stream" );
819 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
820 httpd_MsgAdd( answer, "Pragma", "no-cache" );
821 httpd_MsgAdd( answer, "Pragma", "client-id=%lu",
822 vlc_mrand48()&0x7fff );
823 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
825 /* Check if there is a xPlayStrm=1 */
826 for( i = 0; i < query->i_name; i++ )
828 if( !strcasecmp( query->name[i], "Pragma" ) &&
829 strstr( query->value[i], "xPlayStrm=1" ) )
831 b_xplaystream = true;
837 answer->i_body_offset = 0;
842 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
844 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
849 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
850 const char *psz_url, const char *psz_mime,
851 const char *psz_user, const char *psz_password )
853 httpd_stream_t *stream = xmalloc( sizeof( httpd_stream_t ) );
855 stream->url = httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password,
857 if( stream->url == NULL )
862 vlc_mutex_init( &stream->lock );
863 if( psz_mime && *psz_mime )
865 stream->psz_mime = strdup( psz_mime );
869 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
871 stream->i_header = 0;
872 stream->p_header = NULL;
873 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
874 stream->p_buffer = xmalloc( stream->i_buffer_size );
875 /* We set to 1 to make life simpler
876 * (this way i_body_offset can never be 0) */
877 stream->i_buffer_pos = 1;
878 stream->i_buffer_last_pos = 1;
880 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
881 (httpd_callback_sys_t*)stream );
882 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
883 (httpd_callback_sys_t*)stream );
884 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
885 (httpd_callback_sys_t*)stream );
890 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
892 vlc_mutex_lock( &stream->lock );
893 free( stream->p_header );
894 stream->p_header = NULL;
896 stream->i_header = i_data;
899 stream->p_header = xmalloc( i_data );
900 memcpy( stream->p_header, p_data, i_data );
902 vlc_mutex_unlock( &stream->lock );
907 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
912 if( i_data < 0 || p_data == NULL )
916 vlc_mutex_lock( &stream->lock );
918 /* save this pointer (to be used by new connection) */
919 stream->i_buffer_last_pos = stream->i_buffer_pos;
921 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
927 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
929 /* Ok, we can't go past the end of our buffer */
930 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
932 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
937 stream->i_buffer_pos += i_data;
939 vlc_mutex_unlock( &stream->lock );
943 void httpd_StreamDelete( httpd_stream_t *stream )
945 httpd_UrlDelete( stream->url );
946 vlc_mutex_destroy( &stream->lock );
947 free( stream->psz_mime );
948 free( stream->p_header );
949 free( stream->p_buffer );
953 /*****************************************************************************
955 *****************************************************************************/
956 static void* httpd_HostThread( void * );
957 static httpd_host_t *httpd_HostCreate( vlc_object_t *, const char *,
958 const char *, vlc_tls_creds_t * );
960 /* create a new host */
961 httpd_host_t *vlc_http_HostNew( vlc_object_t *p_this )
963 return httpd_HostCreate( p_this, "http-host", "http-port", NULL );
966 httpd_host_t *vlc_https_HostNew( vlc_object_t *obj )
968 char *cert = var_InheritString( obj, "http-cert" );
971 msg_Err( obj, "HTTP/TLS certificate not specified!" );
975 char *key = var_InheritString( obj, "http-key" );
976 vlc_tls_creds_t *tls = vlc_tls_ServerCreate( obj, cert, key );
980 msg_Err( obj, "HTTP/TLS certificate error (%s and %s)",
981 cert, (key != NULL) ? key : cert );
989 char *ca = var_InheritString( obj, "http-ca" );
992 if( vlc_tls_ServerAddCA( tls, ca ) )
994 msg_Err( obj, "HTTP/TLS CA error (%s)", ca );
1001 char *crl = var_InheritString( obj, "http-crl" );
1004 if( vlc_tls_ServerAddCRL( tls, crl ) )
1006 msg_Err( obj, "TLS CRL error (%s)", crl );
1013 return httpd_HostCreate( obj, "http-host", "https-port", tls );
1016 vlc_tls_ServerDelete( tls );
1020 httpd_host_t *vlc_rtsp_HostNew( vlc_object_t *p_this )
1022 return httpd_HostCreate( p_this, "rtsp-host", "rtsp-port", NULL );
1025 static struct httpd_t
1029 httpd_host_t **host;
1031 } httpd = { VLC_STATIC_MUTEX, NULL, 0 };
1033 static httpd_host_t *httpd_HostCreate( vlc_object_t *p_this,
1034 const char *hostvar,
1035 const char *portvar,
1036 vlc_tls_creds_t *p_tls )
1039 char *hostname = var_InheritString( p_this, hostvar );
1040 unsigned port = var_InheritInteger( p_this, portvar );
1043 vlc_UrlParse( &url, hostname, 0 );
1045 if( url.i_port != 0 )
1047 msg_Err( p_this, "Ignoring port %d (using %d)", url.i_port, port );
1048 msg_Info( p_this, "Specify port %d separately with the "
1049 "%s option instead.", url.i_port, portvar );
1052 /* to be sure to avoid multiple creation */
1053 vlc_mutex_lock( &httpd.mutex );
1055 /* verify if it already exist */
1056 for( int i = 0; i < httpd.i_host; i++ )
1058 host = httpd.host[i];
1060 /* cannot mix TLS and non-TLS hosts */
1061 if( host->port != port
1062 || (host->p_tls != NULL) != (p_tls != NULL) )
1065 /* Increase existing matching host reference count.
1066 * The reference count is written under both the global httpd and the
1067 * host lock. It is read with either or both locks held. The global
1068 * lock is always acquired first. */
1069 vlc_mutex_lock( &host->lock );
1071 vlc_mutex_unlock( &host->lock );
1073 vlc_mutex_unlock( &httpd.mutex );
1074 vlc_UrlClean( &url );
1076 vlc_tls_ServerDelete( p_tls );
1080 /* create the new host */
1081 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1086 vlc_mutex_init( &host->lock );
1087 vlc_cond_init( &host->wait );
1090 host->fds = net_ListenTCP( p_this, url.psz_host, port );
1091 if( host->fds == NULL )
1093 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1096 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1098 if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
1100 msg_Err( host, "signaling pipe error: %m" );
1108 host->client = NULL;
1109 host->p_tls = p_tls;
1111 /* create the thread */
1112 if( vlc_clone( &host->thread, httpd_HostThread, host,
1113 VLC_THREAD_PRIORITY_LOW ) )
1115 msg_Err( p_this, "cannot spawn http host thread" );
1119 /* now add it to httpd */
1120 TAB_APPEND( httpd.i_host, httpd.host, host );
1121 vlc_mutex_unlock( &httpd.mutex );
1123 vlc_UrlClean( &url );
1128 vlc_mutex_unlock( &httpd.mutex );
1132 net_ListenClose( host->fds );
1133 vlc_cond_destroy( &host->wait );
1134 vlc_mutex_destroy( &host->lock );
1135 vlc_object_release( host );
1138 vlc_UrlClean( &url );
1141 vlc_tls_ServerDelete( p_tls );
1147 void httpd_HostDelete( httpd_host_t *host )
1150 bool delete = false;
1152 vlc_mutex_lock( &httpd.mutex );
1154 vlc_mutex_lock( &host->lock );
1156 if( host->i_ref == 0 )
1158 vlc_cond_signal( &host->wait );
1161 vlc_mutex_unlock( &host->lock );
1165 vlc_mutex_unlock( &httpd.mutex );
1166 msg_Dbg( host, "httpd_HostDelete: host still in use" );
1169 TAB_REMOVE( httpd.i_host, httpd.host, host );
1171 vlc_object_kill( host );
1172 vlc_join( host->thread, NULL );
1174 msg_Dbg( host, "HTTP host removed" );
1176 for( i = 0; i < host->i_url; i++ )
1178 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1180 for( i = 0; i < host->i_client; i++ )
1182 httpd_client_t *cl = host->client[i];
1183 msg_Warn( host, "client still connected" );
1184 httpd_ClientClean( cl );
1185 TAB_REMOVE( host->i_client, host->client, cl );
1191 if( host->p_tls != NULL)
1192 vlc_tls_ServerDelete( host->p_tls );
1194 net_ListenClose( host->fds );
1195 vlc_cond_destroy( &host->wait );
1196 vlc_mutex_destroy( &host->lock );
1197 vlc_object_release( host );
1198 vlc_mutex_unlock( &httpd.mutex );
1201 /* register a new url */
1202 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1203 const char *psz_user, const char *psz_password,
1204 const vlc_acl_t *p_acl )
1208 assert( psz_url != NULL );
1210 vlc_mutex_lock( &host->lock );
1211 for( int i = 0; i < host->i_url; i++ )
1213 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1216 "cannot add '%s' (url already defined)", psz_url );
1217 vlc_mutex_unlock( &host->lock );
1222 url = xmalloc( sizeof( httpd_url_t ) );
1225 vlc_mutex_init( &url->lock );
1226 url->psz_url = strdup( psz_url );
1227 url->psz_user = strdup( psz_user ? psz_user : "" );
1228 url->psz_password = strdup( psz_password ? psz_password : "" );
1229 url->p_acl = ACL_Duplicate( host, p_acl );
1230 for( int i = 0; i < HTTPD_MSG_MAX; i++ )
1232 url->catch[i].cb = NULL;
1233 url->catch[i].p_sys = NULL;
1236 TAB_APPEND( host->i_url, host->url, url );
1237 vlc_cond_signal( &host->wait );
1238 vlc_mutex_unlock( &host->lock );
1243 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1244 const char *psz_user, const char *psz_password )
1246 return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, NULL );
1249 /* register callback on a url */
1250 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1251 httpd_callback_sys_t *p_sys )
1253 vlc_mutex_lock( &url->lock );
1254 url->catch[i_msg].cb = cb;
1255 url->catch[i_msg].p_sys= p_sys;
1256 vlc_mutex_unlock( &url->lock );
1262 void httpd_UrlDelete( httpd_url_t *url )
1264 httpd_host_t *host = url->host;
1267 vlc_mutex_lock( &host->lock );
1268 TAB_REMOVE( host->i_url, host->url, url );
1270 vlc_mutex_destroy( &url->lock );
1271 free( url->psz_url );
1272 free( url->psz_user );
1273 free( url->psz_password );
1274 ACL_Destroy( url->p_acl );
1276 for( i = 0; i < host->i_client; i++ )
1278 httpd_client_t *client = host->client[i];
1280 if( client->url == url )
1282 /* TODO complete it */
1283 msg_Warn( host, "force closing connections" );
1284 httpd_ClientClean( client );
1285 TAB_REMOVE( host->i_client, host->client, client );
1291 vlc_mutex_unlock( &host->lock );
1294 static void httpd_MsgInit( httpd_message_t *msg )
1297 msg->i_type = HTTPD_MSG_NONE;
1298 msg->i_proto = HTTPD_PROTO_NONE;
1299 msg->i_version = -1; /* FIXME */
1303 msg->psz_url = NULL;
1304 msg->psz_args = NULL;
1311 msg->i_body_offset = 0;
1316 static void httpd_MsgClean( httpd_message_t *msg )
1320 free( msg->psz_url );
1321 free( msg->psz_args );
1322 for( i = 0; i < msg->i_name; i++ )
1324 free( msg->name[i] );
1325 free( msg->value[i] );
1329 free( msg->p_body );
1330 httpd_MsgInit( msg );
1333 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1337 for( i = 0; i < msg->i_name; i++ )
1339 if( !strcasecmp( msg->name[i], name ))
1341 return msg->value[i];
1347 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1352 va_start( args, psz_value );
1353 if( us_vasprintf( &value, psz_value, args ) == -1 )
1360 name = strdup( name );
1367 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1368 TAB_APPEND( msg->i_value, msg->value, value );
1371 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1373 cl->i_state = HTTPD_CLIENT_RECEIVING;
1374 cl->i_activity_date = now;
1375 cl->i_activity_timeout = INT64_C(10000000);
1376 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1378 cl->p_buffer = xmalloc( cl->i_buffer_size );
1379 cl->b_stream_mode = false;
1381 httpd_MsgInit( &cl->query );
1382 httpd_MsgInit( &cl->answer );
1385 char* httpd_ClientIP( const httpd_client_t *cl, char *ip, int *port )
1387 return net_GetPeerAddress( cl->fd, ip, port ) ? NULL : ip;
1390 char* httpd_ServerIP( const httpd_client_t *cl, char *ip, int *port )
1392 return net_GetSockAddress( cl->fd, ip, port ) ? NULL : ip;
1395 static void httpd_ClientClean( httpd_client_t *cl )
1399 if( cl->p_tls != NULL )
1400 vlc_tls_ServerSessionDelete( cl->p_tls );
1401 net_Close( cl->fd );
1405 httpd_MsgClean( &cl->answer );
1406 httpd_MsgClean( &cl->query );
1408 free( cl->p_buffer );
1409 cl->p_buffer = NULL;
1412 static httpd_client_t *httpd_ClientNew( int fd, vlc_tls_t *p_tls, mtime_t now )
1414 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1416 if( !cl ) return NULL;
1423 httpd_ClientInit( cl, now );
1429 ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
1436 val = p_tls ? tls_Recv (p_tls, p, i_len)
1437 : recv (cl->fd, p, i_len, 0);
1438 while (val == -1 && errno == EINTR);
1443 ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
1450 val = p_tls ? tls_Send( p_tls, p, i_len )
1451 : send (cl->fd, p, i_len, 0);
1452 while (val == -1 && errno == EINTR);
1459 const char name[16];
1465 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1466 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1467 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1468 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1469 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1470 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1471 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1472 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1473 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1474 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1475 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1479 static void httpd_ClientRecv( httpd_client_t *cl )
1483 /* ignore leading whites */
1484 if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
1485 ( cl->i_buffer == 0 ) )
1489 i_len = httpd_NetRecv( cl, &c, 1 );
1491 if( ( i_len > 0 ) && ( strchr( "\r\n\t ", c ) == NULL ) )
1493 cl->p_buffer[0] = c;
1498 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1500 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1501 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1505 cl->i_buffer += i_len;
1508 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1509 * this is the maximum we can ask at this point. */
1510 if( cl->i_buffer >= 7 )
1512 if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
1514 cl->query.i_proto = HTTPD_PROTO_HTTP;
1515 cl->query.i_type = HTTPD_MSG_ANSWER;
1517 else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
1519 cl->query.i_proto = HTTPD_PROTO_RTSP;
1520 cl->query.i_type = HTTPD_MSG_ANSWER;
1524 /* We need the full request line to determine the protocol. */
1525 cl->query.i_proto = HTTPD_PROTO_HTTP0;
1526 cl->query.i_type = HTTPD_MSG_NONE;
1530 else if( cl->query.i_body > 0 )
1532 /* we are reading the body of a request or a channel */
1533 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1534 cl->query.i_body - cl->i_buffer );
1537 cl->i_buffer += i_len;
1539 if( cl->i_buffer >= cl->query.i_body )
1541 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1546 /* we are reading a header -> char by char */
1549 if( cl->i_buffer == cl->i_buffer_size )
1551 uint8_t *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
1552 if( newbuf == NULL )
1558 cl->p_buffer = newbuf;
1559 cl->i_buffer_size += 1024;
1562 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1569 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP0 )
1570 && ( cl->p_buffer[cl->i_buffer - 1] == '\n' ) )
1572 /* Request line is now complete */
1573 const char *p = memchr( cl->p_buffer, ' ', cl->i_buffer );
1576 assert( cl->query.i_type == HTTPD_MSG_NONE );
1578 if( p == NULL ) /* no URI: evil guy */
1580 i_len = 0; /* drop connection */
1585 p++; /* skips extra spaces */
1588 p = memchr( p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p );
1589 if( p == NULL ) /* no explicit protocol: HTTP/0.9 */
1591 i_len = 0; /* not supported currently -> drop */
1596 p++; /* skips extra spaces ever again */
1599 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
1600 if( len < 7 ) /* foreign protocol */
1601 i_len = 0; /* I don't understand -> drop */
1603 if( memcmp( p, "HTTP/1.", 7 ) == 0 )
1605 cl->query.i_proto = HTTPD_PROTO_HTTP;
1606 cl->query.i_version = atoi( p + 7 );
1609 if( memcmp( p, "RTSP/1.", 7 ) == 0 )
1611 cl->query.i_proto = HTTPD_PROTO_RTSP;
1612 cl->query.i_version = atoi( p + 7 );
1615 if( memcmp( p, "HTTP/", 5 ) == 0 )
1617 const uint8_t sorry[] =
1618 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1619 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1620 i_len = 0; /* drop */
1623 if( memcmp( p, "RTSP/", 5 ) == 0 )
1625 const uint8_t sorry[] =
1626 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1627 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1628 i_len = 0; /* drop */
1630 else /* yet another foreign protocol */
1637 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1638 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1642 /* we have finished the header so parse it and set i_body */
1643 cl->p_buffer[cl->i_buffer] = '\0';
1645 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1648 * assume strlen( "HTTP/1.x" ) = 8
1650 cl->query.i_status =
1651 strtol( (char *)&cl->p_buffer[8],
1661 cl->query.i_type = HTTPD_MSG_NONE;
1663 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1665 for( i = 0; msg_type[i].name[0]; i++ )
1667 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1668 strlen( msg_type[i].name ) ) )
1670 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1671 cl->query.i_type = msg_type[i].i_type;
1672 if( cl->query.i_proto != msg_type[i].i_proto )
1675 cl->query.i_proto = HTTPD_PROTO_NONE;
1676 cl->query.i_type = HTTPD_MSG_NONE;
1683 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1685 cl->query.i_proto = HTTPD_PROTO_HTTP;
1687 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1689 cl->query.i_proto = HTTPD_PROTO_RTSP;
1701 p2 = strchr( p, ' ' );
1706 if( !strncasecmp( p, ( cl->query.i_proto
1707 == HTTPD_PROTO_HTTP ) ? "http:" : "rtsp:", 5 ) )
1708 { /* Skip hier-part of URL (if present) */
1710 if( !strncmp( p, "//", 2 ) ) /* skip authority */
1711 { /* see RFC3986 §3.2 */
1713 p += strcspn( p, "/?#" );
1717 if( !strncasecmp( p, ( cl->query.i_proto
1718 == HTTPD_PROTO_HTTP ) ? "https:" : "rtsps:", 6 ) )
1719 { /* Skip hier-part of URL (if present) */
1721 if( !strncmp( p, "//", 2 ) ) /* skip authority */
1722 { /* see RFC3986 §3.2 */
1724 p += strcspn( p, "/?#" );
1728 cl->query.psz_url = strdup( p );
1729 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1732 cl->query.psz_args = (uint8_t *)strdup( p3 );
1739 p = strchr( p, '\n' );
1743 while( *p == '\n' || *p == '\r' )
1747 while( p && *p != '\0' )
1750 char *eol = p = strchr( p, '\n' );
1753 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1758 if( ( colon = strchr( line, ':' ) ) )
1764 while( *colon == ' ' )
1768 name = strdup( line );
1769 value = strdup( colon );
1771 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1772 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1774 if( !strcasecmp( name, "Content-Length" ) )
1776 cl->query.i_body = atol( value );
1783 while( *p == '\n' || *p == '\r' )
1790 if( cl->query.i_body > 0 )
1792 /* TODO Mhh, handle the case where the client only
1793 * sends a request and closes the connection to
1794 * mark the end of the body (probably only RTSP) */
1795 cl->query.p_body = malloc( cl->query.i_body );
1797 if ( cl->query.p_body == NULL )
1799 switch (cl->query.i_proto)
1801 case HTTPD_PROTO_HTTP:
1803 const uint8_t sorry[] =
1804 "HTTP/1.1 413 Request Entity Too Large\r\n\r\n";
1805 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1808 case HTTPD_PROTO_RTSP:
1810 const uint8_t sorry[] =
1811 "RTSP/1.0 413 Request Entity Too Large\r\n\r\n";
1812 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1818 i_len = 0; /* drop */
1824 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1830 /* check if the client is to be set to dead */
1831 #if defined( WIN32 ) || defined( UNDER_CE )
1832 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1834 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1837 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1839 /* connection closed -> end of data */
1840 if( cl->query.i_body > 0 )
1842 cl->query.i_body = cl->i_buffer;
1844 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1848 cl->i_state = HTTPD_CLIENT_DEAD;
1852 /* XXX: for QT I have to disable timeout. Try to find why */
1853 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1854 cl->i_activity_timeout = 0;
1856 #if 0 /* Debugging only */
1857 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1861 fprintf( stderr, "received new request\n" );
1862 fprintf( stderr, " - proto=%s\n",
1863 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1864 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1865 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1866 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1868 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1869 cl->query.psz_status );
1871 else if( cl->query.i_type != HTTPD_MSG_NONE )
1873 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1875 for( i = 0; i < cl->query.i_name; i++ )
1877 fprintf( stderr, " - option name='%s' value='%s'\n",
1878 cl->query.name[i], cl->query.value[i] );
1884 static void httpd_ClientSend( httpd_client_t *cl )
1889 if( cl->i_buffer < 0 )
1891 /* We need to create the header */
1894 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1896 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1897 for( i = 0; i < cl->answer.i_name; i++ )
1899 i_size += strlen( cl->answer.name[i] ) + 2 +
1900 strlen( cl->answer.value[i] ) + 2;
1903 if( cl->i_buffer_size < i_size )
1905 cl->i_buffer_size = i_size;
1906 free( cl->p_buffer );
1907 cl->p_buffer = xmalloc( i_size );
1909 p = (char *)cl->p_buffer;
1911 p += sprintf( p, "%s.%u %d %s\r\n",
1912 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1913 cl->answer.i_version,
1914 cl->answer.i_status, psz_status );
1915 for( i = 0; i < cl->answer.i_name; i++ )
1917 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1918 cl->answer.value[i] );
1920 p += sprintf( p, "\r\n" );
1923 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1925 /*fprintf( stderr, "sending answer\n" );
1926 fprintf( stderr, "%s", cl->p_buffer );*/
1929 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1930 cl->i_buffer_size - cl->i_buffer );
1933 cl->i_buffer += i_len;
1935 if( cl->i_buffer >= cl->i_buffer_size )
1937 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 )
1939 /* catch more body data */
1940 int i_msg = cl->query.i_type;
1941 int64_t i_offset = cl->answer.i_body_offset;
1943 httpd_MsgClean( &cl->answer );
1944 cl->answer.i_body_offset = i_offset;
1946 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1947 &cl->answer, &cl->query );
1950 if( cl->answer.i_body > 0 )
1952 /* send the body data */
1953 free( cl->p_buffer );
1954 cl->p_buffer = cl->answer.p_body;
1955 cl->i_buffer_size = cl->answer.i_body;
1958 cl->answer.i_body = 0;
1959 cl->answer.p_body = NULL;
1964 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1970 #if defined( WIN32 ) || defined( UNDER_CE )
1971 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1973 if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
1977 cl->i_state = HTTPD_CLIENT_DEAD;
1982 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1984 switch( vlc_tls_ServerSessionHandshake( cl->p_tls ) )
1987 cl->i_state = HTTPD_CLIENT_RECEIVING;
1991 cl->i_state = HTTPD_CLIENT_DEAD;
1996 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2000 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2002 switch( vlc_tls_ServerSessionHandshake( cl->p_tls ) )
2005 cl->i_state = HTTPD_CLIENT_RECEIVING;
2009 cl->i_state = HTTPD_CLIENT_DEAD;
2014 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2019 static void* httpd_HostThread( void *data )
2021 httpd_host_t *host = data;
2022 int evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
2026 struct pollfd ufd[host->nfd + host->i_client + 1];
2028 for( nfd = 0; nfd < host->nfd; nfd++ )
2030 ufd[nfd].fd = host->fds[nfd];
2031 ufd[nfd].events = POLLIN;
2032 ufd[nfd].revents = 0;
2035 /* add all socket that should be read/write and close dead connection */
2036 vlc_mutex_lock( &host->lock );
2037 while( host->i_url <= 0 && host->i_ref > 0 )
2038 vlc_cond_wait( &host->wait, &host->lock );
2040 mtime_t now = mdate();
2041 bool b_low_delay = false;
2043 for(int i_client = 0; i_client < host->i_client; i_client++ )
2045 httpd_client_t *cl = host->client[i_client];
2046 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2047 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2048 ( cl->i_activity_timeout > 0 &&
2049 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2051 httpd_ClientClean( cl );
2052 TAB_REMOVE( host->i_client, host->client, cl );
2058 struct pollfd *pufd = ufd + nfd;
2059 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2062 pufd->events = pufd->revents = 0;
2064 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2065 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2067 pufd->events = POLLIN;
2069 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2070 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2072 pufd->events = POLLOUT;
2074 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2076 httpd_message_t *answer = &cl->answer;
2077 httpd_message_t *query = &cl->query;
2078 int i_msg = query->i_type;
2080 httpd_MsgInit( answer );
2082 /* Handle what we received */
2083 if( i_msg == HTTPD_MSG_ANSWER )
2086 cl->i_state = HTTPD_CLIENT_DEAD;
2088 else if( i_msg == HTTPD_MSG_OPTIONS )
2091 answer->i_type = HTTPD_MSG_ANSWER;
2092 answer->i_proto = query->i_proto;
2093 answer->i_status = 200;
2095 answer->p_body = NULL;
2097 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
2098 httpd_MsgAdd( answer, "Content-Length", "0" );
2100 switch( query->i_proto )
2102 case HTTPD_PROTO_HTTP:
2103 answer->i_version = 1;
2104 httpd_MsgAdd( answer, "Allow",
2105 "GET,HEAD,POST,OPTIONS" );
2108 case HTTPD_PROTO_RTSP:
2111 answer->i_version = 0;
2113 p = httpd_MsgGet( query, "Cseq" );
2115 httpd_MsgAdd( answer, "Cseq", "%s", p );
2116 p = httpd_MsgGet( query, "Timestamp" );
2118 httpd_MsgAdd( answer, "Timestamp", "%s", p );
2120 p = httpd_MsgGet( query, "Require" );
2123 answer->i_status = 551;
2124 httpd_MsgAdd( query, "Unsupported", "%s", p );
2127 httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
2128 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
2133 cl->i_buffer = -1; /* Force the creation of the answer in
2134 * httpd_ClientSend */
2135 cl->i_state = HTTPD_CLIENT_SENDING;
2137 else if( i_msg == HTTPD_MSG_NONE )
2139 if( query->i_proto == HTTPD_PROTO_NONE )
2142 cl->i_state = HTTPD_CLIENT_DEAD;
2149 answer->i_proto = query->i_proto ;
2150 answer->i_type = HTTPD_MSG_ANSWER;
2151 answer->i_version= 0;
2152 answer->i_status = 501;
2154 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2155 answer->p_body = (uint8_t *)p;
2156 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2158 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2159 cl->i_state = HTTPD_CLIENT_SENDING;
2164 bool b_auth_failed = false;
2165 bool b_hosts_failed = false;
2167 /* Search the url and trigger callbacks */
2168 for(int i = 0; i < host->i_url; i++ )
2170 httpd_url_t *url = host->url[i];
2172 if( !strcmp( url->psz_url, query->psz_url ) )
2174 if( url->catch[i_msg].cb )
2176 if( answer && ( url->p_acl != NULL ) )
2178 char ip[NI_MAXNUMERICHOST];
2180 if( ( httpd_ClientIP( cl, ip, NULL ) == NULL )
2181 || ACL_Check( url->p_acl, ip ) )
2183 b_hosts_failed = true;
2188 if( answer && ( *url->psz_user || *url->psz_password ) )
2190 /* create the headers */
2191 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2192 char *user = NULL, *pass = NULL;
2195 && !strncasecmp( b64, "BASIC", 5 ) )
2198 while( *b64 == ' ' )
2201 user = vlc_b64_decode( b64 );
2204 pass = strchr (user, ':');
2210 if ((user == NULL) || (pass == NULL)
2211 || strcmp (user, url->psz_user)
2212 || strcmp (pass, url->psz_password))
2214 httpd_MsgAdd( answer,
2216 "Basic realm=\"VLC stream\"" );
2217 /* We fail for all url */
2218 b_auth_failed = true;
2226 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2228 if( answer->i_proto == HTTPD_PROTO_NONE )
2230 /* Raw answer from a CGI */
2231 cl->i_buffer = cl->i_buffer_size;
2236 /* only one url can answer */
2238 if( cl->url == NULL )
2251 answer->i_proto = query->i_proto;
2252 answer->i_type = HTTPD_MSG_ANSWER;
2253 answer->i_version= 0;
2255 if( b_hosts_failed )
2257 answer->i_status = 403;
2259 else if( b_auth_failed )
2261 answer->i_status = 401;
2265 /* no url registered */
2266 answer->i_status = 404;
2269 answer->i_body = httpd_HtmlError (&p,
2272 answer->p_body = (uint8_t *)p;
2274 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2275 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2276 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2279 cl->i_state = HTTPD_CLIENT_SENDING;
2282 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2284 if( !cl->b_stream_mode || cl->answer.i_body_offset == 0 )
2286 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2287 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2288 bool b_connection = false;
2289 bool b_keepalive = false;
2290 bool b_query = false;
2293 if( psz_connection )
2295 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2296 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2301 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2304 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2305 ( ( cl->query.i_version == 0 && b_keepalive ) ||
2306 ( cl->query.i_version == 1 && !b_connection ) ) ) ||
2307 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2308 !b_query && !b_connection ) )
2310 httpd_MsgClean( &cl->query );
2311 httpd_MsgInit( &cl->query );
2314 cl->i_buffer_size = 1000;
2315 free( cl->p_buffer );
2316 cl->p_buffer = xmalloc( cl->i_buffer_size );
2317 cl->i_state = HTTPD_CLIENT_RECEIVING;
2321 cl->i_state = HTTPD_CLIENT_DEAD;
2323 httpd_MsgClean( &cl->answer );
2327 int64_t i_offset = cl->answer.i_body_offset;
2328 httpd_MsgClean( &cl->answer );
2330 cl->answer.i_body_offset = i_offset;
2331 free( cl->p_buffer );
2332 cl->p_buffer = NULL;
2334 cl->i_buffer_size = 0;
2336 cl->i_state = HTTPD_CLIENT_WAITING;
2339 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2341 int64_t i_offset = cl->answer.i_body_offset;
2342 int i_msg = cl->query.i_type;
2344 httpd_MsgInit( &cl->answer );
2345 cl->answer.i_body_offset = i_offset;
2347 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2348 &cl->answer, &cl->query );
2349 if( cl->answer.i_type != HTTPD_MSG_NONE )
2351 /* we have new data, so re-enter send mode */
2353 cl->p_buffer = cl->answer.p_body;
2354 cl->i_buffer_size = cl->answer.i_body;
2355 cl->answer.p_body = NULL;
2356 cl->answer.i_body = 0;
2357 cl->i_state = HTTPD_CLIENT_SENDING;
2361 if (pufd->events != 0)
2366 vlc_mutex_unlock( &host->lock );
2369 ufd[nfd].events = POLLIN;
2370 ufd[nfd].revents = 0;
2373 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2374 switch( poll( ufd, nfd, b_low_delay ? 20 : -1) )
2379 /* Kernel on low memory or a bug: pace */
2380 msg_Err( host, "polling error: %m" );
2387 if( ufd[nfd - 1].revents )
2390 /* Handle client sockets */
2391 vlc_mutex_lock( &host->lock );
2394 for( int i_client = 0; i_client < host->i_client; i_client++ )
2396 httpd_client_t *cl = host->client[i_client];
2397 const struct pollfd *pufd = &ufd[nfd];
2399 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2401 if( cl->fd != pufd->fd )
2402 continue; // we were not waiting for this client
2404 if( pufd->revents == 0 )
2405 continue; // no event received
2407 cl->i_activity_date = now;
2409 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2411 httpd_ClientRecv( cl );
2413 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2415 httpd_ClientSend( cl );
2417 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2419 httpd_ClientTlsHsIn( cl );
2421 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2423 httpd_ClientTlsHsOut( cl );
2426 vlc_mutex_unlock( &host->lock );
2428 /* Handle server sockets (accept new connections) */
2429 for( nfd = 0; nfd < host->nfd; nfd++ )
2433 int fd = ufd[nfd].fd;
2435 assert (fd == host->fds[nfd]);
2437 if( ufd[nfd].revents == 0 )
2441 fd = vlc_accept (fd, NULL, NULL, true);
2444 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
2445 &(int){ 1 }, sizeof(int));
2449 if( host->p_tls != NULL )
2451 p_tls = vlc_tls_ServerSessionCreate( host->p_tls, fd );
2452 switch( vlc_tls_ServerSessionHandshake( p_tls ) )
2455 msg_Err( host, "Rejecting TLS connection" );
2456 /* p_tls is destroyed implicitly */
2462 case 1: /* missing input - most likely */
2463 i_state = HTTPD_CLIENT_TLS_HS_IN;
2466 case 2: /* missing output */
2467 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2474 cl = httpd_ClientNew( fd, p_tls, now );
2475 vlc_mutex_lock( &host->lock );
2476 TAB_APPEND( host->i_client, host->client, cl );
2477 vlc_mutex_unlock( &host->lock );
2479 cl->i_state = i_state; // override state for TLS