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 *****************************************************************************/
35 #include <vlc_httpd.h>
36 #include <vlc_network.h>
39 #include <vlc_strings.h>
40 #include "../libvlc.h"
57 #if defined( UNDER_CE )
59 #elif defined( WIN32 )
60 # include <winsock2.h>
62 # include <sys/socket.h>
66 /* We need HUGE buffer otherwise TCP throughput is very limited */
67 #define HTTPD_CL_BUFSIZE 1000000
69 #define HTTPD_CL_BUFSIZE 10000
72 static void httpd_ClientClean( httpd_client_t *cl );
83 /* each host run in his own thread */
93 /* address/port and socket for listening at connections */
101 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
102 * This will slow down the url research but make my live easier
103 * All url will have their cb trigger, but only the first one can answer
109 httpd_client_t **client;
130 httpd_callback_sys_t *p_sys;
131 } catch[HTTPD_MSG_MAX];
137 HTTPD_CLIENT_RECEIVING,
138 HTTPD_CLIENT_RECEIVE_DONE,
140 HTTPD_CLIENT_SENDING,
141 HTTPD_CLIENT_SEND_DONE,
143 HTTPD_CLIENT_WAITING,
147 HTTPD_CLIENT_TLS_HS_IN,
148 HTTPD_CLIENT_TLS_HS_OUT
154 HTTPD_CLIENT_FILE, /* default */
155 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
156 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
159 struct httpd_client_t
169 int b_read_waiting; /* stop as soon as possible sending */
171 mtime_t i_activity_date;
172 mtime_t i_activity_timeout;
174 /* buffer for reading header */
180 httpd_message_t query; /* client -> httpd */
181 httpd_message_t answer; /* httpd -> client */
184 tls_session_t *p_tls;
188 /*****************************************************************************
190 *****************************************************************************/
194 const char *psz_mime;
197 { ".htm", "text/html" },
198 { ".html", "text/html" },
199 { ".txt", "text/plain" },
200 { ".xml", "text/xml" },
201 { ".dtd", "text/dtd" },
203 { ".css", "text/css" },
206 { ".gif", "image/gif" },
207 { ".jpe", "image/jpeg" },
208 { ".jpg", "image/jpeg" },
209 { ".jpeg", "image/jpeg" },
210 { ".png", "image/png" },
211 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
214 { ".avi", "video/avi" },
215 { ".asf", "video/x-ms-asf" },
216 { ".m1a", "audio/mpeg" },
217 { ".m2a", "audio/mpeg" },
218 { ".m1v", "video/mpeg" },
219 { ".m2v", "video/mpeg" },
220 { ".mp2", "audio/mpeg" },
221 { ".mp3", "audio/mpeg" },
222 { ".mpa", "audio/mpeg" },
223 { ".mpg", "video/mpeg" },
224 { ".mpeg", "video/mpeg" },
225 { ".mpe", "video/mpeg" },
226 { ".mov", "video/quicktime" },
227 { ".moov", "video/quicktime" },
228 { ".ogg", "application/ogg" },
229 { ".ogm", "application/ogg" },
230 { ".wav", "audio/wav" },
231 { ".wma", "audio/x-ms-wma" },
232 { ".wmv", "video/x-ms-wmv" },
239 static const char *httpd_MimeFromUrl( const char *psz_url )
244 psz_ext = strrchr( psz_url, '.' );
249 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
251 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
253 return http_mime[i].psz_mime;
257 return "application/octet-stream";
264 const char *psz_reason;
267 static const http_status_info http_reason[] =
269 /*{ 100, "Continue" },
270 { 101, "Switching Protocols" },*/
274 { 203, "Non-Authoritative Information" },
275 { 204, "No Content" },
276 { 205, "Reset Content" },
277 { 206, "Partial Content" },
278 { 250, "Low on Storage Space" },
279 { 300, "Multiple Choices" }*/,
280 { 301, "Moved Permanently" }/*,
281 { 302, "Moved Temporarily" }, - aka "Found"
282 { 303, "See Other" },
283 { 304, "Not Modified" },
284 { 305, "Use Proxy" },
285 { 307, "Temporary Redirect" },
286 { 400, "Bad Request" }*/,
287 { 401, "Unauthorized" }/*,
288 { 402, "Payment Required" }*/,
289 { 403, "Forbidden" },
290 { 404, "Not Found" }/*,
291 { 405, "Method Not Allowed" },
292 { 406, "Not Acceptable" },
293 { 407, "Proxy Authentication Required" },
294 { 408, "Request Time-out" },
297 { 411, "Length Required" },
298 { 412, "Precondition Failed" },
299 { 413, "Request Entity Too Large" },
300 { 414, "Request-URI Too Large" },
301 { 415, "Unsupported Media Type" },
302 { 416, "Requested range not satisfiable" },
303 { 417, "Expectation Failed" },
304 { 451, "Parameter Not Understood" },
305 { 452, "Conference Not Found" },
306 { 453, "Not Enough Bandwidth" }*/,
307 { 454, "Session Not Found" }/*,
308 { 455, "Method Not Valid in This State" },
309 { 456, "Header Field Not Valid for Resource" },
310 { 457, "Invalid Range" },
311 { 458, "Parameter Is Read-Only" },
312 { 459, "Aggregate operation not allowed" },
313 { 460, "Only aggregate operation allowed" }*/,
314 { 461, "Unsupported transport" }/*,
315 { 462, "Destination unreachable" }*/,
316 { 500, "Internal Server Error" },
317 { 501, "Not Implemented" }/*,
318 { 502, "Bad Gateway" }*/,
319 { 503, "Service Unavailable" }/*,
320 { 504, "Gateway Time-out" },
321 { 505, "Protocol version not supported" }*/,
325 static const char *psz_fallback_reason[] =
326 { "Continue", "OK", "Found", "Client Error", "Server Error" };
328 static const char *httpd_ReasonFromCode( int i_code )
330 const http_status_info *p;
332 for (p = http_reason; p->i_code < i_code; p++);
334 if( p->i_code == i_code )
335 return p->psz_reason;
337 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
338 return psz_fallback_reason[(i_code / 100) - 1];
342 static size_t httpd_HtmlError (char **body, int code, const char *url)
344 const char *errname = httpd_ReasonFromCode (code);
345 assert (errname != NULL);
347 int res = asprintf (body,
348 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
349 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
350 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
351 "<html lang=\"en\">\n"
353 "<title>%s</title>\n"
356 "<h1>%d %s%s%s%s</h1>\n"
358 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
360 "</html>\n", errname, code, errname,
361 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
373 /*****************************************************************************
374 * High Level Functions: httpd_file_t
375 *****************************************************************************/
383 httpd_file_callback_t pf_fill;
384 httpd_file_sys_t *p_sys;
388 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
390 httpd_file_t *file = (httpd_file_t*)p_sys;
391 uint8_t *psz_args = query->psz_args;
392 uint8_t **pp_body, *p_body;
393 const char *psz_connection;
394 int *pi_body, i_body;
396 if( answer == NULL || query == NULL )
400 answer->i_proto = HTTPD_PROTO_HTTP;
401 answer->i_version= query->i_version;
402 answer->i_type = HTTPD_MSG_ANSWER;
404 answer->i_status = 200;
405 answer->psz_status = strdup( "OK" );
407 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
408 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
410 if( query->i_type != HTTPD_MSG_HEAD )
412 pp_body = &answer->p_body;
413 pi_body = &answer->i_body;
417 /* The file still needs to be executed. */
424 if( query->i_type == HTTPD_MSG_POST )
426 /* msg_Warn not supported */
429 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
431 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
436 /* We respect client request */
437 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
438 if( psz_connection != NULL )
440 httpd_MsgAdd( answer, "Connection", psz_connection );
443 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
448 httpd_file_t *httpd_FileNew( httpd_host_t *host,
449 const char *psz_url, const char *psz_mime,
450 const char *psz_user, const char *psz_password,
451 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
452 httpd_file_sys_t *p_sys )
454 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
456 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
457 psz_password, p_acl )
464 file->psz_url = strdup( psz_url );
465 if( psz_mime && *psz_mime )
467 file->psz_mime = strdup( psz_mime );
471 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
474 file->pf_fill = pf_fill;
477 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
478 (httpd_callback_sys_t*)file );
479 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
480 (httpd_callback_sys_t*)file );
481 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
482 (httpd_callback_sys_t*)file );
487 void httpd_FileDelete( httpd_file_t *file )
489 httpd_UrlDelete( file->url );
491 free( file->psz_url );
492 free( file->psz_mime );
497 /*****************************************************************************
498 * High Level Functions: httpd_handler_t (for CGIs)
499 *****************************************************************************/
500 struct httpd_handler_t
504 httpd_handler_callback_t pf_fill;
505 httpd_handler_sys_t *p_sys;
509 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
511 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
512 uint8_t *psz_args = query->psz_args;
513 char psz_remote_addr[NI_MAXNUMERICHOST];
515 if( answer == NULL || query == NULL )
519 answer->i_proto = HTTPD_PROTO_NONE;
520 answer->i_type = HTTPD_MSG_ANSWER;
522 /* We do it ourselves, thanks */
523 answer->i_status = 0;
524 answer->psz_status = NULL;
526 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
527 *psz_remote_addr = '\0';
529 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
530 query->i_type, query->p_body, query->i_body,
531 psz_remote_addr, NULL,
532 &answer->p_body, &answer->i_body );
534 if( query->i_type == HTTPD_MSG_HEAD )
536 char *p = (char *)answer->p_body;
537 while ( (p = strchr( p, '\r' )) != NULL )
539 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
540 && p[3] && p[3] == '\n' )
548 answer->i_body = strlen((char*)answer->p_body) + 1;
549 answer->p_body = realloc( answer->p_body, answer->i_body );
553 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
555 int i_status, i_headers;
556 char *psz_headers, *psz_new;
557 const char *psz_status;
559 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
562 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
563 if( *psz_headers ) psz_headers++;
564 if( *psz_headers ) psz_headers++;
565 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
570 psz_headers = (char *)answer->p_body;
571 i_headers = answer->i_body;
579 psz_status = "Unauthorized";
582 if( (i_status < 0) || (i_status > 999) )
584 psz_status = "Undefined";
587 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
588 + strlen(psz_status) + i_headers - 1;
589 psz_new = (char *)malloc( answer->i_body + 1);
590 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
591 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
592 free( answer->p_body );
593 answer->p_body = (uint8_t *)psz_new;
599 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
600 const char *psz_user,
601 const char *psz_password,
602 const vlc_acl_t *p_acl,
603 httpd_handler_callback_t pf_fill,
604 httpd_handler_sys_t *p_sys )
606 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
608 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
609 psz_password, p_acl )
616 handler->pf_fill = pf_fill;
617 handler->p_sys = p_sys;
619 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
620 (httpd_callback_sys_t*)handler );
621 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
622 (httpd_callback_sys_t*)handler );
623 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
624 (httpd_callback_sys_t*)handler );
629 void httpd_HandlerDelete( httpd_handler_t *handler )
631 httpd_UrlDelete( handler->url );
635 /*****************************************************************************
636 * High Level Functions: httpd_redirect_t
637 *****************************************************************************/
638 struct httpd_redirect_t
644 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
645 httpd_client_t *cl, httpd_message_t *answer,
646 httpd_message_t *query )
648 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
652 if( answer == NULL || query == NULL )
656 answer->i_proto = query->i_proto;
657 answer->i_version= query->i_version;
658 answer->i_type = HTTPD_MSG_ANSWER;
659 answer->i_status = 301;
660 answer->psz_status = strdup( "Moved Permanently" );
662 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
663 answer->p_body = (unsigned char *)p_body;
665 /* XXX check if it's ok or we need to set an absolute url */
666 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
668 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
673 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
674 const char *psz_url_src )
676 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
678 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
683 rdir->psz_dst = strdup( psz_url_dst );
685 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
686 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
687 (httpd_callback_sys_t*)rdir );
688 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
689 (httpd_callback_sys_t*)rdir );
690 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
691 (httpd_callback_sys_t*)rdir );
692 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
693 (httpd_callback_sys_t*)rdir );
697 void httpd_RedirectDelete( httpd_redirect_t *rdir )
699 httpd_UrlDelete( rdir->url );
700 free( rdir->psz_dst );
704 /*****************************************************************************
705 * High Level Funtions: httpd_stream_t
706 *****************************************************************************/
707 struct httpd_stream_t
714 /* Header to send as first packet */
718 /* circular buffer */
719 int i_buffer_size; /* buffer size, can't be reallocated smaller */
720 uint8_t *p_buffer; /* buffer */
721 int64_t i_buffer_pos; /* absolute position from begining */
722 int64_t i_buffer_last_pos; /* a new connection will start with that */
725 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
726 httpd_client_t *cl, httpd_message_t *answer,
727 httpd_message_t *query )
729 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
731 if( answer == NULL || query == NULL || cl == NULL )
736 if( answer->i_body_offset > 0 )
742 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
743 answer->i_body_offset );
746 if( answer->i_body_offset >= stream->i_buffer_pos )
748 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
749 return VLC_EGENERIC; /* wait, no data available */
751 if( answer->i_body_offset + stream->i_buffer_size <
752 stream->i_buffer_pos )
754 /* this client isn't fast enough */
756 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
757 answer->i_body_offset, stream->i_buffer_last_pos );
759 answer->i_body_offset = stream->i_buffer_last_pos;
762 i_pos = answer->i_body_offset % stream->i_buffer_size;
763 i_write = stream->i_buffer_pos - answer->i_body_offset;
764 if( i_write > HTTPD_CL_BUFSIZE )
766 i_write = HTTPD_CL_BUFSIZE;
768 else if( i_write <= 0 )
770 return VLC_EGENERIC; /* wait, no data available */
773 /* Don't go past the end of the circular buffer */
774 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
776 /* using HTTPD_MSG_ANSWER -> data available */
777 answer->i_proto = HTTPD_PROTO_HTTP;
778 answer->i_version= 0;
779 answer->i_type = HTTPD_MSG_ANSWER;
781 answer->i_body = i_write;
782 answer->p_body = malloc( i_write );
783 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
785 answer->i_body_offset += i_write;
791 answer->i_proto = HTTPD_PROTO_HTTP;
792 answer->i_version= 0;
793 answer->i_type = HTTPD_MSG_ANSWER;
795 answer->i_status = 200;
796 answer->psz_status = strdup( "OK" );
798 if( query->i_type != HTTPD_MSG_HEAD )
800 httpd_ClientModeStream( cl );
801 vlc_mutex_lock( &stream->lock );
802 /* Send the header */
803 if( stream->i_header > 0 )
805 answer->i_body = stream->i_header;
806 answer->p_body = malloc( stream->i_header );
807 memcpy( answer->p_body, stream->p_header, stream->i_header );
809 answer->i_body_offset = stream->i_buffer_last_pos;
810 vlc_mutex_unlock( &stream->lock );
814 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
815 answer->i_body_offset = 0;
818 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
820 vlc_bool_t b_xplaystream = VLC_FALSE;
823 httpd_MsgAdd( answer, "Content-type", "%s",
824 "application/octet-stream" );
825 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
826 httpd_MsgAdd( answer, "Pragma", "no-cache" );
827 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
828 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
830 /* Check if there is a xPlayStrm=1 */
831 for( i = 0; i < query->i_name; i++ )
833 if( !strcasecmp( query->name[i], "Pragma" ) &&
834 strstr( query->value[i], "xPlayStrm=1" ) )
836 b_xplaystream = VLC_TRUE;
842 answer->i_body_offset = 0;
847 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
849 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
854 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
855 const char *psz_url, const char *psz_mime,
856 const char *psz_user, const char *psz_password,
857 const vlc_acl_t *p_acl )
859 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
861 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
862 psz_password, p_acl )
868 vlc_mutex_init( host, &stream->lock );
869 if( psz_mime && *psz_mime )
871 stream->psz_mime = strdup( psz_mime );
875 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
877 stream->i_header = 0;
878 stream->p_header = NULL;
879 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
880 stream->p_buffer = malloc( stream->i_buffer_size );
881 /* We set to 1 to make life simpler
882 * (this way i_body_offset can never be 0) */
883 stream->i_buffer_pos = 1;
884 stream->i_buffer_last_pos = 1;
886 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
887 (httpd_callback_sys_t*)stream );
888 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
889 (httpd_callback_sys_t*)stream );
890 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
891 (httpd_callback_sys_t*)stream );
896 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
898 vlc_mutex_lock( &stream->lock );
899 if( stream->p_header )
901 free( stream->p_header );
902 stream->p_header = NULL;
904 stream->i_header = i_data;
907 stream->p_header = malloc( i_data );
908 memcpy( stream->p_header, p_data, i_data );
910 vlc_mutex_unlock( &stream->lock );
915 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
920 if( i_data < 0 || p_data == NULL )
924 vlc_mutex_lock( &stream->lock );
926 /* save this pointer (to be used by new connection) */
927 stream->i_buffer_last_pos = stream->i_buffer_pos;
929 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
935 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
937 /* Ok, we can't go past the end of our buffer */
938 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
940 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
945 stream->i_buffer_pos += i_data;
947 vlc_mutex_unlock( &stream->lock );
951 void httpd_StreamDelete( httpd_stream_t *stream )
953 httpd_UrlDelete( stream->url );
954 vlc_mutex_destroy( &stream->lock );
955 if( stream->psz_mime ) free( stream->psz_mime );
956 if( stream->p_header ) free( stream->p_header );
957 if( stream->p_buffer ) free( stream->p_buffer );
961 /*****************************************************************************
963 *****************************************************************************/
964 static void httpd_HostThread( httpd_host_t * );
966 /* create a new host */
967 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
970 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
974 static const char psz_object_type[] = "http server";
976 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
978 const char *psz_cert, const char *psz_key,
979 const char *psz_ca, const char *psz_crl )
988 if( psz_hostname == NULL )
991 psz_host = strdup( psz_hostname );
992 if( psz_host == NULL )
994 msg_Err( p_this, "memory error" );
998 /* to be sure to avoid multiple creation */
999 var_Create( p_this->p_libvlc_global, "httpd_mutex", VLC_VAR_MUTEX );
1000 var_Get( p_this->p_libvlc_global, "httpd_mutex", &lockval );
1001 vlc_mutex_lock( lockval.p_address );
1003 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1005 msg_Info( p_this, "creating httpd" );
1006 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1011 vlc_mutex_unlock( lockval.p_address );
1019 vlc_object_yield( httpd );
1020 vlc_object_attach( httpd, p_this->p_libvlc );
1023 /* verify if it already exist */
1024 for( i = httpd->i_host - 1; i >= 0; i-- )
1026 host = httpd->host[i];
1028 /* cannot mix TLS and non-TLS hosts */
1029 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1030 || ( host->i_port != i_port )
1031 || strcmp( host->psz_hostname, psz_hostname ) )
1037 vlc_mutex_unlock( lockval.p_address );
1043 /* determine TLS configuration */
1044 if ( psz_cert != NULL )
1046 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1047 if ( p_tls == NULL )
1049 msg_Err( p_this, "TLS initialization error" );
1053 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1055 msg_Err( p_this, "TLS CA error" );
1059 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1061 msg_Err( p_this, "TLS CRL error" );
1068 /* create the new host */
1069 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1070 VLC_OBJECT_HTTPD_HOST,
1075 host->httpd = httpd;
1076 vlc_mutex_init( httpd, &host->lock );
1079 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1080 if( host->fds == NULL )
1082 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1085 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1087 host->i_port = i_port;
1088 host->psz_hostname = psz_host;
1093 host->client = NULL;
1095 host->p_tls = p_tls;
1097 /* create the thread */
1098 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1099 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1101 msg_Err( p_this, "cannot spawn http host thread" );
1105 /* now add it to httpd */
1106 TAB_APPEND( httpd->i_host, httpd->host, host );
1107 vlc_mutex_unlock( lockval.p_address );
1113 if( httpd->i_host <= 0 )
1115 vlc_object_release( httpd );
1116 vlc_object_detach( httpd );
1117 vlc_object_destroy( httpd );
1119 vlc_mutex_unlock( lockval.p_address );
1123 net_ListenClose( host->fds );
1124 vlc_mutex_destroy( &host->lock );
1125 vlc_object_destroy( host );
1129 tls_ServerDelete( p_tls );
1135 void httpd_HostDelete( httpd_host_t *host )
1137 httpd_t *httpd = host->httpd;
1138 vlc_value_t lockval;
1141 var_Get( httpd->p_libvlc_global, "httpd_mutex", &lockval );
1142 vlc_mutex_lock( lockval.p_address );
1145 if( host->i_ref > 0 )
1148 vlc_mutex_unlock( lockval.p_address );
1149 msg_Dbg( host, "httpd_HostDelete: host still used" );
1152 TAB_REMOVE( httpd->i_host, httpd->host, host );
1154 vlc_object_kill( host );
1155 vlc_thread_join( host );
1157 msg_Dbg( host, "HTTP host removed" );
1159 for( i = 0; i < host->i_url; i++ )
1161 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1163 for( i = 0; i < host->i_client; i++ )
1165 httpd_client_t *cl = host->client[i];
1166 msg_Warn( host, "client still connected" );
1167 httpd_ClientClean( cl );
1168 TAB_REMOVE( host->i_client, host->client, cl );
1174 if( host->p_tls != NULL)
1175 tls_ServerDelete( host->p_tls );
1177 net_ListenClose( host->fds );
1178 free( host->psz_hostname );
1180 vlc_mutex_destroy( &host->lock );
1181 vlc_object_destroy( host );
1183 vlc_object_release( httpd );
1184 if( httpd->i_host <= 0 )
1186 msg_Dbg( httpd, "no host left, stopping httpd" );
1187 vlc_object_detach( httpd );
1188 vlc_object_destroy( httpd );
1190 vlc_mutex_unlock( lockval.p_address );
1193 /* register a new url */
1194 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1195 const char *psz_user, const char *psz_password,
1196 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1201 vlc_mutex_lock( &host->lock );
1204 for( i = 0; i < host->i_url; i++ )
1206 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1208 msg_Warn( host->httpd,
1209 "cannot add '%s' (url already defined)", psz_url );
1210 vlc_mutex_unlock( &host->lock );
1216 url = malloc( sizeof( httpd_url_t ) );
1219 vlc_mutex_init( host->httpd, &url->lock );
1220 url->psz_url = strdup( psz_url );
1221 url->psz_user = strdup( psz_user ? psz_user : "" );
1222 url->psz_password = strdup( psz_password ? psz_password : "" );
1223 url->p_acl = ACL_Duplicate( host, p_acl );
1224 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1226 url->catch[i].cb = NULL;
1227 url->catch[i].p_sys = NULL;
1230 TAB_APPEND( host->i_url, host->url, url );
1231 vlc_mutex_unlock( &host->lock );
1236 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1237 const char *psz_user, const char *psz_password,
1238 const vlc_acl_t *p_acl )
1240 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1241 psz_password, p_acl, VLC_FALSE );
1244 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1245 const char *psz_user, const char *psz_password,
1246 const vlc_acl_t *p_acl )
1248 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1249 psz_password, p_acl, VLC_TRUE );
1252 /* register callback on a url */
1253 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1254 httpd_callback_sys_t *p_sys )
1256 vlc_mutex_lock( &url->lock );
1257 url->catch[i_msg].cb = cb;
1258 url->catch[i_msg].p_sys= p_sys;
1259 vlc_mutex_unlock( &url->lock );
1265 void httpd_UrlDelete( httpd_url_t *url )
1267 httpd_host_t *host = url->host;
1270 vlc_mutex_lock( &host->lock );
1271 TAB_REMOVE( host->i_url, host->url, url );
1273 vlc_mutex_destroy( &url->lock );
1274 free( url->psz_url );
1275 free( url->psz_user );
1276 free( url->psz_password );
1277 ACL_Destroy( url->p_acl );
1279 for( i = 0; i < host->i_client; i++ )
1281 httpd_client_t *client = host->client[i];
1283 if( client->url == url )
1285 /* TODO complete it */
1286 msg_Warn( host, "force closing connections" );
1287 httpd_ClientClean( client );
1288 TAB_REMOVE( host->i_client, host->client, client );
1294 vlc_mutex_unlock( &host->lock );
1297 void httpd_MsgInit( httpd_message_t *msg )
1300 msg->i_type = HTTPD_MSG_NONE;
1301 msg->i_proto = HTTPD_PROTO_NONE;
1302 msg->i_version = -1;
1305 msg->psz_status = NULL;
1307 msg->psz_url = NULL;
1308 msg->psz_args = NULL;
1310 msg->i_channel = -1;
1317 msg->i_body_offset = 0;
1322 void httpd_MsgClean( httpd_message_t *msg )
1326 if( msg->psz_status )
1328 free( msg->psz_status );
1332 free( msg->psz_url );
1336 free( msg->psz_args );
1338 for( i = 0; i < msg->i_name; i++ )
1340 free( msg->name[i] );
1341 free( msg->value[i] );
1353 free( msg->p_body );
1355 httpd_MsgInit( msg );
1358 const char *httpd_MsgGet( httpd_message_t *msg, const char *name )
1362 for( i = 0; i < msg->i_name; i++ )
1364 if( !strcasecmp( msg->name[i], name ))
1366 return msg->value[i];
1372 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1377 va_start( args, psz_value );
1378 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1379 vasprintf( &value, psz_value, args );
1382 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1383 value = calloc( i_size, sizeof( char ) );
1384 vsnprintf( value, i_size, psz_value, args );
1385 value[i_size - 1] = 0;
1390 name = strdup( name );
1392 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1393 TAB_APPEND( msg->i_value, msg->value, value );
1396 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1398 cl->i_state = HTTPD_CLIENT_RECEIVING;
1399 cl->i_activity_date = now;
1400 cl->i_activity_timeout = I64C(10000000);
1401 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1403 cl->p_buffer = malloc( cl->i_buffer_size );
1404 cl->i_mode = HTTPD_CLIENT_FILE;
1405 cl->b_read_waiting = VLC_FALSE;
1407 httpd_MsgInit( &cl->query );
1408 httpd_MsgInit( &cl->answer );
1411 void httpd_ClientModeStream( httpd_client_t *cl )
1413 cl->i_mode = HTTPD_CLIENT_STREAM;
1416 void httpd_ClientModeBidir( httpd_client_t *cl )
1418 cl->i_mode = HTTPD_CLIENT_BIDIR;
1421 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1423 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1426 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1428 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1431 static void httpd_ClientClean( httpd_client_t *cl )
1435 if( cl->p_tls != NULL )
1436 tls_ServerSessionClose( cl->p_tls );
1437 net_Close( cl->fd );
1441 httpd_MsgClean( &cl->answer );
1442 httpd_MsgClean( &cl->query );
1446 free( cl->p_buffer );
1447 cl->p_buffer = NULL;
1451 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1453 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1455 if( !cl ) return NULL;
1462 httpd_ClientInit( cl, now );
1467 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1469 tls_session_t *p_tls;
1473 return tls_Recv( p_tls, p, i_len );
1475 return recv( cl->fd, p, i_len, 0 );
1478 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1480 tls_session_t *p_tls;
1484 return tls_Send( p_tls, p, i_len );
1486 return send( cl->fd, p, i_len, 0 );
1489 static void httpd_ClientRecv( httpd_client_t *cl )
1493 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1495 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1496 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1500 cl->i_buffer += i_len;
1503 if( cl->i_buffer >= 4 )
1505 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1507 if( cl->p_buffer[0] == '$' )
1509 /* RTSP (rtp over rtsp) */
1510 cl->query.i_proto = HTTPD_PROTO_RTSP;
1511 cl->query.i_type = HTTPD_MSG_CHANNEL;
1512 cl->query.i_channel = cl->p_buffer[1];
1513 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1514 cl->query.p_body = malloc( cl->query.i_body );
1518 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1520 cl->query.i_proto = HTTPD_PROTO_HTTP;
1521 cl->query.i_type = HTTPD_MSG_ANSWER;
1523 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1525 cl->query.i_proto = HTTPD_PROTO_RTSP;
1526 cl->query.i_type = HTTPD_MSG_ANSWER;
1528 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1529 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1530 !memcmp( cl->p_buffer, "POST", 4 ) )
1532 cl->query.i_proto = HTTPD_PROTO_HTTP;
1533 cl->query.i_type = HTTPD_MSG_NONE;
1537 cl->query.i_proto = HTTPD_PROTO_RTSP;
1538 cl->query.i_type = HTTPD_MSG_NONE;
1542 else if( cl->query.i_body > 0 )
1544 /* we are reading the body of a request or a channel */
1545 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1546 cl->query.i_body - cl->i_buffer );
1549 cl->i_buffer += i_len;
1551 if( cl->i_buffer >= cl->query.i_body )
1553 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1558 /* we are reading a header -> char by char */
1561 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1568 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1570 cl->i_buffer_size += 1024;
1571 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1573 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1574 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1578 /* we have finished the header so parse it and set i_body */
1579 cl->p_buffer[cl->i_buffer] = '\0';
1581 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1584 * assume strlen( "HTTP/1.x" ) = 8
1586 cl->query.i_status =
1587 strtol( (char *)&cl->p_buffer[8],
1593 cl->query.psz_status = strdup( p );
1605 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1606 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1607 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1608 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1609 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1610 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1611 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1613 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1614 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1615 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1617 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1622 cl->query.i_type = HTTPD_MSG_NONE;
1624 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1626 for( i = 0; msg_type[i].name != NULL; i++ )
1628 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1629 strlen( msg_type[i].name ) ) )
1631 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1632 cl->query.i_type = msg_type[i].i_type;
1633 if( cl->query.i_proto != msg_type[i].i_proto )
1636 cl->query.i_proto = HTTPD_PROTO_NONE;
1637 cl->query.i_type = HTTPD_MSG_NONE;
1644 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1646 cl->query.i_proto = HTTPD_PROTO_HTTP;
1648 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1650 cl->query.i_proto = HTTPD_PROTO_RTSP;
1662 p2 = strchr( p, ' ' );
1667 if( !strncasecmp( p, "rtsp:", 5 ) )
1669 /* for rtsp url, you have rtsp://localhost:port/path */
1671 while( *p == '/' ) p++;
1672 while( *p && *p != '/' ) p++;
1674 cl->query.psz_url = strdup( p );
1675 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1678 cl->query.psz_args = (uint8_t *)strdup( p3 );
1686 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1688 cl->query.i_proto = HTTPD_PROTO_HTTP;
1689 cl->query.i_version = atoi( p2+7 );
1691 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1693 cl->query.i_proto = HTTPD_PROTO_RTSP;
1694 cl->query.i_version = atoi( p2+7 );
1702 p = strchr( p, '\n' );
1706 while( *p == '\n' || *p == '\r' )
1710 while( p && *p != '\0' )
1713 char *eol = p = strchr( p, '\n' );
1716 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1721 if( ( colon = strchr( line, ':' ) ) )
1727 while( *colon == ' ' )
1731 name = strdup( line );
1732 value = strdup( colon );
1734 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1735 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1737 if( !strcasecmp( name, "Content-Length" ) )
1739 cl->query.i_body = atol( value );
1746 while( *p == '\n' || *p == '\r' )
1753 if( cl->query.i_body > 0 )
1755 /* TODO Mhh, handle the case client will only send a request and close the connection
1756 * to mark and of body (probably only RTSP) */
1757 cl->query.p_body = malloc( cl->query.i_body );
1762 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1768 /* check if the client is to be set to dead */
1769 #if defined( WIN32 ) || defined( UNDER_CE )
1770 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1772 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1775 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1777 /* connection closed -> end of data */
1778 if( cl->query.i_body > 0 )
1780 cl->query.i_body = cl->i_buffer;
1782 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1786 cl->i_state = HTTPD_CLIENT_DEAD;
1790 /* XXX: for QT I have to disable timeout. Try to find why */
1791 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1792 cl->i_activity_timeout = 0;
1794 #if 0 /* Debugging only */
1795 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1799 fprintf( stderr, "received new request\n" );
1800 fprintf( stderr, " - proto=%s\n",
1801 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1802 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1803 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1804 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1806 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1807 cl->query.psz_status );
1809 else if( cl->query.i_type != HTTPD_MSG_NONE )
1811 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1813 for( i = 0; i < cl->query.i_name; i++ )
1815 fprintf( stderr, " - option name='%s' value='%s'\n",
1816 cl->query.name[i], cl->query.value[i] );
1822 static void httpd_ClientSend( httpd_client_t *cl )
1827 if( cl->i_buffer < 0 )
1829 /* We need to create the header */
1833 i_size = strlen( "HTTP/1.") + 10 + 10 +
1834 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1835 for( i = 0; i < cl->answer.i_name; i++ )
1837 i_size += strlen( cl->answer.name[i] ) + 2 +
1838 strlen( cl->answer.value[i] ) + 2;
1841 if( cl->i_buffer_size < i_size )
1843 cl->i_buffer_size = i_size;
1844 free( cl->p_buffer );
1845 cl->p_buffer = malloc( i_size );
1847 p = (char *)cl->p_buffer;
1849 p += sprintf( p, "%s/1.%d %d %s\r\n",
1850 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1851 cl->answer.i_version,
1852 cl->answer.i_status, cl->answer.psz_status );
1853 for( i = 0; i < cl->answer.i_name; i++ )
1855 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1856 cl->answer.value[i] );
1858 p += sprintf( p, "\r\n" );
1861 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1863 /*fprintf( stderr, "sending answer\n" );
1864 fprintf( stderr, "%s", cl->p_buffer );*/
1867 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1868 cl->i_buffer_size - cl->i_buffer );
1871 cl->i_buffer += i_len;
1873 if( cl->i_buffer >= cl->i_buffer_size )
1875 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1876 !cl->b_read_waiting )
1878 /* catch more body data */
1879 int i_msg = cl->query.i_type;
1880 int64_t i_offset = cl->answer.i_body_offset;
1882 httpd_MsgClean( &cl->answer );
1883 cl->answer.i_body_offset = i_offset;
1885 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1886 &cl->answer, &cl->query );
1889 if( cl->answer.i_body > 0 )
1891 /* send the body data */
1892 free( cl->p_buffer );
1893 cl->p_buffer = cl->answer.p_body;
1894 cl->i_buffer_size = cl->answer.i_body;
1897 cl->answer.i_body = 0;
1898 cl->answer.p_body = NULL;
1903 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1909 #if defined( WIN32 ) || defined( UNDER_CE )
1910 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1912 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1916 cl->i_state = HTTPD_CLIENT_DEAD;
1921 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1923 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1926 cl->i_state = HTTPD_CLIENT_RECEIVING;
1930 cl->i_state = HTTPD_CLIENT_DEAD;
1935 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1939 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1941 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1944 cl->i_state = HTTPD_CLIENT_RECEIVING;
1948 cl->i_state = HTTPD_CLIENT_DEAD;
1953 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1958 static void httpd_HostThread( httpd_host_t *host )
1960 tls_session_t *p_tls = NULL;
1961 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1962 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1964 while( !host->b_die )
1966 if( host->i_url <= 0 )
1973 /* prepare a new TLS session */
1974 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1975 p_tls = tls_ServerSessionPrepare( host->p_tls );
1977 struct pollfd ufd[host->nfd + host->i_client];
1979 for( nfd = 0; nfd < host->nfd; nfd++ )
1981 ufd[nfd].fd = host->fds[nfd];
1982 ufd[nfd].events = POLLIN;
1983 ufd[nfd].revents = 0;
1986 /* add all socket that should be read/write and close dead connection */
1987 vlc_mutex_lock( &host->lock );
1988 mtime_t now = mdate();
1989 vlc_bool_t b_low_delay = VLC_FALSE;
1991 for(int i_client = 0; i_client < host->i_client; i_client++ )
1993 httpd_client_t *cl = host->client[i_client];
1994 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1995 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1996 ( cl->i_activity_timeout > 0 &&
1997 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
1999 httpd_ClientClean( cl );
2000 stats_UpdateInteger( host, p_active_counter, -1, NULL );
2001 TAB_REMOVE( host->i_client, host->client, cl );
2007 struct pollfd *pufd = ufd + nfd;
2008 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2011 pufd->events = pufd->revents = 0;
2013 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2014 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2016 pufd->events = POLLIN;
2018 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2019 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2021 pufd->events = POLLOUT;
2023 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2025 httpd_message_t *answer = &cl->answer;
2026 httpd_message_t *query = &cl->query;
2027 int i_msg = query->i_type;
2029 httpd_MsgInit( answer );
2031 /* Handle what we received */
2032 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2033 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2035 /* we can only receive request from client when not
2038 cl->i_state = HTTPD_CLIENT_DEAD;
2040 else if( i_msg == HTTPD_MSG_ANSWER )
2042 /* We are in BIDIR mode, trigger the callback and then
2043 * check for new data */
2044 if( cl->url && cl->url->catch[i_msg].cb )
2046 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2049 cl->i_state = HTTPD_CLIENT_WAITING;
2051 else if( i_msg == HTTPD_MSG_CHANNEL )
2053 /* We are in BIDIR mode, trigger the callback and then
2054 * check for new data */
2055 if( cl->url && cl->url->catch[i_msg].cb )
2057 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2060 cl->i_state = HTTPD_CLIENT_WAITING;
2062 else if( i_msg == HTTPD_MSG_OPTIONS )
2064 const char *psz_cseq;
2068 answer->i_proto = query->i_proto ;
2069 answer->i_type = HTTPD_MSG_ANSWER;
2070 answer->i_version= 0;
2071 answer->i_status = 200;
2072 answer->psz_status = strdup( "Ok" );
2075 answer->p_body = NULL;
2077 psz_cseq = httpd_MsgGet( query, "Cseq" );
2079 i_cseq = atoi( psz_cseq );
2082 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2083 httpd_MsgAdd( answer, "Server", "VLC Server" );
2084 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2085 "TEARDOWN, PLAY, PAUSE" );
2086 httpd_MsgAdd( answer, "Content-Length", "%d",
2089 cl->i_buffer = -1; /* Force the creation of the answer in
2090 * httpd_ClientSend */
2091 cl->i_state = HTTPD_CLIENT_SENDING;
2093 else if( i_msg == HTTPD_MSG_NONE )
2095 if( query->i_proto == HTTPD_PROTO_NONE )
2098 cl->i_state = HTTPD_CLIENT_DEAD;
2105 answer->i_proto = query->i_proto ;
2106 answer->i_type = HTTPD_MSG_ANSWER;
2107 answer->i_version= 0;
2108 answer->i_status = 501;
2109 answer->psz_status = strdup( "Unimplemented" );
2111 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2112 answer->p_body = (uint8_t *)p;
2113 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2115 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2116 cl->i_state = HTTPD_CLIENT_SENDING;
2121 vlc_bool_t b_auth_failed = VLC_FALSE;
2122 vlc_bool_t b_hosts_failed = VLC_FALSE;
2124 /* Search the url and trigger callbacks */
2125 for(int i = 0; i < host->i_url; i++ )
2127 httpd_url_t *url = host->url[i];
2129 if( !strcmp( url->psz_url, query->psz_url ) )
2131 if( url->catch[i_msg].cb )
2133 if( answer && ( url->p_acl != NULL ) )
2135 char ip[NI_MAXNUMERICHOST];
2137 if( httpd_ClientIP( cl, ip ) != NULL )
2139 if( ACL_Check( url->p_acl, ip ) )
2141 b_hosts_failed = VLC_TRUE;
2146 b_hosts_failed = VLC_TRUE;
2149 if( answer && ( *url->psz_user || *url->psz_password ) )
2151 /* create the headers */
2152 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2156 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2158 && !strncasecmp( b64, "BASIC", 5 ) )
2161 while( *b64 == ' ' )
2165 auth = vlc_b64_decode( b64 );
2168 if( (auth == NULL) || strcmp( id, auth ) )
2170 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2171 /* We fail for all url */
2172 b_auth_failed = VLC_TRUE;
2182 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2184 if( answer->i_proto == HTTPD_PROTO_NONE )
2186 /* Raw answer from a CGI */
2187 cl->i_buffer = cl->i_buffer_size;
2192 /* only one url can answer */
2194 if( cl->url == NULL )
2207 answer->i_proto = query->i_proto;
2208 answer->i_type = HTTPD_MSG_ANSWER;
2209 answer->i_version= 0;
2211 if( b_hosts_failed )
2213 answer->i_status = 403;
2214 answer->psz_status = strdup( "Forbidden" );
2216 else if( b_auth_failed )
2218 answer->i_status = 401;
2219 answer->psz_status = strdup( "Authorization Required" );
2223 /* no url registered */
2224 answer->i_status = 404;
2225 answer->psz_status = strdup( "Not found" );
2228 answer->i_body = httpd_HtmlError (&p,
2231 answer->p_body = (uint8_t *)p;
2233 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2234 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2235 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2238 cl->i_state = HTTPD_CLIENT_SENDING;
2241 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2243 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2245 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2246 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2247 vlc_bool_t b_connection = VLC_FALSE;
2248 vlc_bool_t b_keepalive = VLC_FALSE;
2249 vlc_bool_t b_query = VLC_FALSE;
2252 if( psz_connection )
2254 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2255 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2260 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2263 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2264 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2265 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2266 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2267 !b_query && !b_connection ) )
2269 httpd_MsgClean( &cl->query );
2270 httpd_MsgInit( &cl->query );
2273 cl->i_buffer_size = 1000;
2274 free( cl->p_buffer );
2275 cl->p_buffer = malloc( cl->i_buffer_size );
2276 cl->i_state = HTTPD_CLIENT_RECEIVING;
2280 cl->i_state = HTTPD_CLIENT_DEAD;
2282 httpd_MsgClean( &cl->answer );
2284 else if( cl->b_read_waiting )
2286 /* we have a message waiting for us to read it */
2287 httpd_MsgClean( &cl->answer );
2288 httpd_MsgClean( &cl->query );
2291 cl->i_buffer_size = 1000;
2292 free( cl->p_buffer );
2293 cl->p_buffer = malloc( cl->i_buffer_size );
2294 cl->i_state = HTTPD_CLIENT_RECEIVING;
2295 cl->b_read_waiting = VLC_FALSE;
2299 int64_t i_offset = cl->answer.i_body_offset;
2300 httpd_MsgClean( &cl->answer );
2302 cl->answer.i_body_offset = i_offset;
2303 free( cl->p_buffer );
2304 cl->p_buffer = NULL;
2306 cl->i_buffer_size = 0;
2308 cl->i_state = HTTPD_CLIENT_WAITING;
2311 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2313 int64_t i_offset = cl->answer.i_body_offset;
2314 int i_msg = cl->query.i_type;
2316 httpd_MsgInit( &cl->answer );
2317 cl->answer.i_body_offset = i_offset;
2319 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2320 &cl->answer, &cl->query );
2321 if( cl->answer.i_type != HTTPD_MSG_NONE )
2323 /* we have new data, so re-enter send mode */
2325 cl->p_buffer = cl->answer.p_body;
2326 cl->i_buffer_size = cl->answer.i_body;
2327 cl->answer.p_body = NULL;
2328 cl->answer.i_body = 0;
2329 cl->i_state = HTTPD_CLIENT_SENDING;
2333 /* we shouldn't wait too long */
2334 b_low_delay = VLC_TRUE;
2338 /* Special for BIDIR mode we also check reading */
2339 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2340 cl->i_state == HTTPD_CLIENT_SENDING )
2342 pufd->events |= POLLIN;
2345 if (pufd->events != 0)
2348 vlc_mutex_unlock( &host->lock );
2350 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2351 switch( poll( ufd, nfd, b_low_delay ? 20 : 100) )
2356 /* This is most likely a bug */
2357 msg_Err( host, "polling error: %s", strerror (errno));
2364 /* Handle client sockets */
2365 vlc_mutex_lock( &host->lock );
2367 for( int i_client = 0; i_client < host->i_client; i_client++ )
2369 httpd_client_t *cl = host->client[i_client];
2370 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2372 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2374 if( cl->fd != pufd->fd )
2375 continue; // we were not waiting for this client
2376 if( pufd->revents == 0 )
2377 continue; // no event received
2379 cl->i_activity_date = now;
2381 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2383 httpd_ClientRecv( cl );
2385 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2387 httpd_ClientSend( cl );
2389 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2391 httpd_ClientTlsHsIn( cl );
2393 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2395 httpd_ClientTlsHsOut( cl );
2398 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2399 cl->i_state == HTTPD_CLIENT_SENDING &&
2400 (pufd->revents & POLLIN) )
2402 cl->b_read_waiting = VLC_TRUE;
2405 vlc_mutex_unlock( &host->lock );
2407 /* Handle server sockets (accept new connections) */
2408 for( nfd = 0; nfd < host->nfd; nfd++ )
2413 assert (ufd[nfd].fd == host->fds[nfd]);
2415 if( ufd[nfd].revents == 0 )
2419 int kludge[] = { ufd[nfd].fd, -1 };
2420 int fd = net_Accept( host, kludge, 0 );
2426 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2429 msg_Err( host, "Rejecting TLS connection" );
2435 case 1: /* missing input - most likely */
2436 i_state = HTTPD_CLIENT_TLS_HS_IN;
2439 case 2: /* missing output */
2440 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2444 if( (p_tls == NULL) != (host->p_tls == NULL) )
2445 break; // wasted TLS session, cannot accept() anymore
2448 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2449 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2450 cl = httpd_ClientNew( fd, p_tls, now );
2452 vlc_mutex_lock( &host->lock );
2453 TAB_APPEND( host->i_client, host->client, cl );
2454 vlc_mutex_unlock( &host->lock );
2456 cl->i_state = i_state; // override state for TLS
2458 if (host->p_tls != NULL)
2459 break; // cannot accept further without new TLS session
2465 tls_ServerSessionClose( p_tls );
2466 if( p_total_counter )
2467 stats_CounterClean( p_total_counter );
2468 if( p_active_counter )
2469 stats_CounterClean( p_active_counter );
2472 #else /* ENABLE_HTTPD */
2474 /* We just define an empty wrapper */
2475 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2478 msg_Err( a, "HTTP daemon support is disabled" );
2482 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2484 msg_Err( a, "HTTP daemon support is disabled" );
2488 void httpd_HostDelete( httpd_host_t *a )
2492 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2493 char *psz_user, char *psz_password,
2494 const vlc_acl_t *p_acl )
2499 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2500 char *psz_user, char *psz_password,
2501 const vlc_acl_t *p_acl )
2506 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2507 httpd_callback_sys_t *d )
2512 void httpd_UrlDelete( httpd_url_t *a )
2516 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2521 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2526 void httpd_ClientModeStream( httpd_client_t *a )
2530 void httpd_ClientModeBidir( httpd_client_t *a )
2534 void httpd_FileDelete( httpd_file_t *a )
2538 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2539 char *e, httpd_file_callback_t f,
2540 httpd_file_sys_t *g )
2545 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2546 const char *psz_user,
2547 const char *psz_password,
2548 const vlc_acl_t *p_acl,
2549 httpd_handler_callback_t pf_fill,
2550 httpd_handler_sys_t *p_sys )
2555 void httpd_HandlerDelete( httpd_handler_t *handler )
2559 void httpd_RedirectDelete( httpd_redirect_t *a )
2563 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2569 void httpd_StreamDelete( httpd_stream_t *a )
2573 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2578 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2583 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2589 void httpd_MsgInit ( httpd_message_t *a )
2593 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2597 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2602 void httpd_MsgClean( httpd_message_t *a )
2606 #endif /* ENABLE_HTTPD */