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 *****************************************************************************/
32 #include <vlc_httpd.h>
33 #include <vlc_network.h>
36 #include <vlc_strings.h>
37 #include "../libvlc.h"
54 #if defined( UNDER_CE )
56 #elif defined( WIN32 )
57 # include <winsock2.h>
59 # include <sys/socket.h>
63 /* We need HUGE buffer otherwise TCP throughput is very limited */
64 #define HTTPD_CL_BUFSIZE 1000000
66 #define HTTPD_CL_BUFSIZE 10000
69 static void httpd_ClientClean( httpd_client_t *cl );
80 /* each host run in his own thread */
90 /* address/port and socket for listening at connections */
98 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
99 * This will slow down the url research but make my live easier
100 * All url will have their cb trigger, but only the first one can answer
106 httpd_client_t **client;
127 httpd_callback_sys_t *p_sys;
128 } catch[HTTPD_MSG_MAX];
134 HTTPD_CLIENT_RECEIVING,
135 HTTPD_CLIENT_RECEIVE_DONE,
137 HTTPD_CLIENT_SENDING,
138 HTTPD_CLIENT_SEND_DONE,
140 HTTPD_CLIENT_WAITING,
144 HTTPD_CLIENT_TLS_HS_IN,
145 HTTPD_CLIENT_TLS_HS_OUT
151 HTTPD_CLIENT_FILE, /* default */
152 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
153 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
156 struct httpd_client_t
166 int b_read_waiting; /* stop as soon as possible sending */
168 mtime_t i_activity_date;
169 mtime_t i_activity_timeout;
171 /* buffer for reading header */
177 httpd_message_t query; /* client -> httpd */
178 httpd_message_t answer; /* httpd -> client */
181 tls_session_t *p_tls;
185 /*****************************************************************************
187 *****************************************************************************/
190 const char psz_ext[8];
191 const char *psz_mime;
194 { ".htm", "text/html" },
195 { ".html", "text/html" },
196 { ".txt", "text/plain" },
197 { ".xml", "text/xml" },
198 { ".dtd", "text/dtd" },
200 { ".css", "text/css" },
203 { ".gif", "image/gif" },
204 { ".jpe", "image/jpeg" },
205 { ".jpg", "image/jpeg" },
206 { ".jpeg", "image/jpeg" },
207 { ".png", "image/png" },
208 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
211 { ".avi", "video/avi" },
212 { ".asf", "video/x-ms-asf" },
213 { ".m1a", "audio/mpeg" },
214 { ".m2a", "audio/mpeg" },
215 { ".m1v", "video/mpeg" },
216 { ".m2v", "video/mpeg" },
217 { ".mp2", "audio/mpeg" },
218 { ".mp3", "audio/mpeg" },
219 { ".mpa", "audio/mpeg" },
220 { ".mpg", "video/mpeg" },
221 { ".mpeg", "video/mpeg" },
222 { ".mpe", "video/mpeg" },
223 { ".mov", "video/quicktime" },
224 { ".moov", "video/quicktime" },
225 { ".ogg", "application/ogg" },
226 { ".ogm", "application/ogg" },
227 { ".wav", "audio/wav" },
228 { ".wma", "audio/x-ms-wma" },
229 { ".wmv", "video/x-ms-wmv" },
236 static const char *httpd_MimeFromUrl( const char *psz_url )
241 psz_ext = strrchr( psz_url, '.' );
246 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
248 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
250 return http_mime[i].psz_mime;
254 return "application/octet-stream";
261 const char *psz_reason;
264 static const http_status_info http_reason[] =
266 /*{ 100, "Continue" },
267 { 101, "Switching Protocols" },*/
271 { 203, "Non-Authoritative Information" },
272 { 204, "No Content" },
273 { 205, "Reset Content" },
274 { 206, "Partial Content" },
275 { 250, "Low on Storage Space" },
276 { 300, "Multiple Choices" }*/,
277 { 301, "Moved Permanently" }/*,
278 { 302, "Moved Temporarily" }, - aka "Found"
279 { 303, "See Other" },
280 { 304, "Not Modified" },
281 { 305, "Use Proxy" },
282 { 307, "Temporary Redirect" },
283 { 400, "Bad Request" }*/,
284 { 401, "Unauthorized" }/*,
285 { 402, "Payment Required" }*/,
286 { 403, "Forbidden" },
287 { 404, "Not Found" }/*,
288 { 405, "Method Not Allowed" },
289 { 406, "Not Acceptable" },
290 { 407, "Proxy Authentication Required" },
291 { 408, "Request Time-out" },
294 { 411, "Length Required" },
295 { 412, "Precondition Failed" },
296 { 413, "Request Entity Too Large" },
297 { 414, "Request-URI Too Large" },
298 { 415, "Unsupported Media Type" },
299 { 416, "Requested range not satisfiable" },
300 { 417, "Expectation Failed" },
301 { 451, "Parameter Not Understood" },
302 { 452, "Conference Not Found" },
303 { 453, "Not Enough Bandwidth" }*/,
304 { 454, "Session Not Found" }/*,
305 { 455, "Method Not Valid in This State" },
306 { 456, "Header Field Not Valid for Resource" },
307 { 457, "Invalid Range" },
308 { 458, "Parameter Is Read-Only" },
309 { 459, "Aggregate operation not allowed" },
310 { 460, "Only aggregate operation allowed" }*/,
311 { 461, "Unsupported transport" }/*,
312 { 462, "Destination unreachable" }*/,
313 { 500, "Internal Server Error" },
314 { 501, "Not Implemented" }/*,
315 { 502, "Bad Gateway" }*/,
316 { 503, "Service Unavailable" }/*,
317 { 504, "Gateway Time-out" },
318 { 505, "Protocol version not supported" }*/,
322 static const char *psz_fallback_reason[] =
323 { "Continue", "OK", "Found", "Client Error", "Server Error" };
325 static const char *httpd_ReasonFromCode( int i_code )
327 const http_status_info *p;
329 for (p = http_reason; p->i_code < i_code; p++);
331 if( p->i_code == i_code )
332 return p->psz_reason;
334 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
335 return psz_fallback_reason[(i_code / 100) - 1];
339 static size_t httpd_HtmlError (char **body, int code, const char *url)
341 const char *errname = httpd_ReasonFromCode (code);
342 assert (errname != NULL);
344 int res = asprintf (body,
345 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
346 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
347 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
348 "<html lang=\"en\">\n"
350 "<title>%s</title>\n"
353 "<h1>%d %s%s%s%s</h1>\n"
355 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
357 "</html>\n", errname, code, errname,
358 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
370 /*****************************************************************************
371 * High Level Functions: httpd_file_t
372 *****************************************************************************/
380 httpd_file_callback_t pf_fill;
381 httpd_file_sys_t *p_sys;
385 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
387 httpd_file_t *file = (httpd_file_t*)p_sys;
388 uint8_t *psz_args = query->psz_args;
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= query->i_version;
399 answer->i_type = HTTPD_MSG_ANSWER;
401 answer->i_status = 200;
402 answer->psz_status = strdup( "OK" );
404 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
405 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
407 if( query->i_type != HTTPD_MSG_HEAD )
409 pp_body = &answer->p_body;
410 pi_body = &answer->i_body;
414 /* The file still needs to be executed. */
421 if( query->i_type == HTTPD_MSG_POST )
423 /* msg_Warn not supported */
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", 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 = malloc( sizeof( httpd_file_t ) );
453 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
454 psz_password, p_acl )
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 void httpd_FileDelete( httpd_file_t *file )
486 httpd_UrlDelete( file->url );
488 free( file->psz_url );
489 free( file->psz_mime );
494 /*****************************************************************************
495 * High Level Functions: httpd_handler_t (for CGIs)
496 *****************************************************************************/
497 struct httpd_handler_t
501 httpd_handler_callback_t pf_fill;
502 httpd_handler_sys_t *p_sys;
506 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
508 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
509 uint8_t *psz_args = query->psz_args;
510 char psz_remote_addr[NI_MAXNUMERICHOST];
512 if( answer == NULL || query == NULL )
516 answer->i_proto = HTTPD_PROTO_NONE;
517 answer->i_type = HTTPD_MSG_ANSWER;
519 /* We do it ourselves, thanks */
520 answer->i_status = 0;
521 answer->psz_status = NULL;
523 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
524 *psz_remote_addr = '\0';
526 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
527 query->i_type, query->p_body, query->i_body,
528 psz_remote_addr, NULL,
529 &answer->p_body, &answer->i_body );
531 if( query->i_type == HTTPD_MSG_HEAD )
533 char *p = (char *)answer->p_body;
534 while ( (p = strchr( p, '\r' )) != NULL )
536 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
537 && p[3] && p[3] == '\n' )
545 answer->i_body = strlen((char*)answer->p_body) + 1;
546 answer->p_body = realloc( answer->p_body, answer->i_body );
550 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
552 int i_status, i_headers;
553 char *psz_headers, *psz_new;
554 const char *psz_status;
556 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
559 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
560 if( *psz_headers ) psz_headers++;
561 if( *psz_headers ) psz_headers++;
562 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
567 psz_headers = (char *)answer->p_body;
568 i_headers = answer->i_body;
576 psz_status = "Unauthorized";
579 if( (i_status < 0) || (i_status > 999) )
581 psz_status = "Undefined";
584 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
585 + strlen(psz_status) + i_headers - 1;
586 psz_new = (char *)malloc( answer->i_body + 1);
587 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
588 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
589 free( answer->p_body );
590 answer->p_body = (uint8_t *)psz_new;
596 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
597 const char *psz_user,
598 const char *psz_password,
599 const vlc_acl_t *p_acl,
600 httpd_handler_callback_t pf_fill,
601 httpd_handler_sys_t *p_sys )
603 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
605 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
606 psz_password, p_acl )
613 handler->pf_fill = pf_fill;
614 handler->p_sys = p_sys;
616 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
617 (httpd_callback_sys_t*)handler );
618 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
619 (httpd_callback_sys_t*)handler );
620 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
621 (httpd_callback_sys_t*)handler );
626 void httpd_HandlerDelete( httpd_handler_t *handler )
628 httpd_UrlDelete( handler->url );
632 /*****************************************************************************
633 * High Level Functions: httpd_redirect_t
634 *****************************************************************************/
635 struct httpd_redirect_t
641 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
642 httpd_client_t *cl, httpd_message_t *answer,
643 httpd_message_t *query )
645 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
649 if( answer == NULL || query == NULL )
653 answer->i_proto = query->i_proto;
654 answer->i_version= query->i_version;
655 answer->i_type = HTTPD_MSG_ANSWER;
656 answer->i_status = 301;
657 answer->psz_status = strdup( "Moved Permanently" );
659 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
660 answer->p_body = (unsigned char *)p_body;
662 /* XXX check if it's ok or we need to set an absolute url */
663 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
665 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
670 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
671 const char *psz_url_src )
673 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
675 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
680 rdir->psz_dst = strdup( psz_url_dst );
682 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
683 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
684 (httpd_callback_sys_t*)rdir );
685 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
686 (httpd_callback_sys_t*)rdir );
687 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
688 (httpd_callback_sys_t*)rdir );
689 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
690 (httpd_callback_sys_t*)rdir );
694 void httpd_RedirectDelete( httpd_redirect_t *rdir )
696 httpd_UrlDelete( rdir->url );
697 free( rdir->psz_dst );
701 /*****************************************************************************
702 * High Level Funtions: httpd_stream_t
703 *****************************************************************************/
704 struct httpd_stream_t
711 /* Header to send as first packet */
715 /* circular buffer */
716 int i_buffer_size; /* buffer size, can't be reallocated smaller */
717 uint8_t *p_buffer; /* buffer */
718 int64_t i_buffer_pos; /* absolute position from begining */
719 int64_t i_buffer_last_pos; /* a new connection will start with that */
722 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
723 httpd_client_t *cl, httpd_message_t *answer,
724 httpd_message_t *query )
726 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
728 if( answer == NULL || query == NULL || cl == NULL )
733 if( answer->i_body_offset > 0 )
739 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
740 answer->i_body_offset );
743 if( answer->i_body_offset >= stream->i_buffer_pos )
745 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
746 return VLC_EGENERIC; /* wait, no data available */
748 if( answer->i_body_offset + stream->i_buffer_size <
749 stream->i_buffer_pos )
751 /* this client isn't fast enough */
753 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
754 answer->i_body_offset, stream->i_buffer_last_pos );
756 answer->i_body_offset = stream->i_buffer_last_pos;
759 i_pos = answer->i_body_offset % stream->i_buffer_size;
760 i_write = stream->i_buffer_pos - answer->i_body_offset;
761 if( i_write > HTTPD_CL_BUFSIZE )
763 i_write = HTTPD_CL_BUFSIZE;
765 else if( i_write <= 0 )
767 return VLC_EGENERIC; /* wait, no data available */
770 /* Don't go past the end of the circular buffer */
771 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
773 /* using HTTPD_MSG_ANSWER -> data available */
774 answer->i_proto = HTTPD_PROTO_HTTP;
775 answer->i_version= 0;
776 answer->i_type = HTTPD_MSG_ANSWER;
778 answer->i_body = i_write;
779 answer->p_body = malloc( i_write );
780 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
782 answer->i_body_offset += i_write;
788 answer->i_proto = HTTPD_PROTO_HTTP;
789 answer->i_version= 0;
790 answer->i_type = HTTPD_MSG_ANSWER;
792 answer->i_status = 200;
793 answer->psz_status = strdup( "OK" );
795 if( query->i_type != HTTPD_MSG_HEAD )
797 httpd_ClientModeStream( cl );
798 vlc_mutex_lock( &stream->lock );
799 /* Send the header */
800 if( stream->i_header > 0 )
802 answer->i_body = stream->i_header;
803 answer->p_body = malloc( stream->i_header );
804 memcpy( answer->p_body, stream->p_header, stream->i_header );
806 answer->i_body_offset = stream->i_buffer_last_pos;
807 vlc_mutex_unlock( &stream->lock );
811 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
812 answer->i_body_offset = 0;
815 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
817 vlc_bool_t b_xplaystream = VLC_FALSE;
820 httpd_MsgAdd( answer, "Content-type", "%s",
821 "application/octet-stream" );
822 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
823 httpd_MsgAdd( answer, "Pragma", "no-cache" );
824 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
825 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
827 /* Check if there is a xPlayStrm=1 */
828 for( i = 0; i < query->i_name; i++ )
830 if( !strcasecmp( query->name[i], "Pragma" ) &&
831 strstr( query->value[i], "xPlayStrm=1" ) )
833 b_xplaystream = VLC_TRUE;
839 answer->i_body_offset = 0;
844 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
846 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
851 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
852 const char *psz_url, const char *psz_mime,
853 const char *psz_user, const char *psz_password,
854 const vlc_acl_t *p_acl )
856 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
858 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
859 psz_password, p_acl )
865 vlc_mutex_init( host, &stream->lock );
866 if( psz_mime && *psz_mime )
868 stream->psz_mime = strdup( psz_mime );
872 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
874 stream->i_header = 0;
875 stream->p_header = NULL;
876 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
877 stream->p_buffer = malloc( stream->i_buffer_size );
878 /* We set to 1 to make life simpler
879 * (this way i_body_offset can never be 0) */
880 stream->i_buffer_pos = 1;
881 stream->i_buffer_last_pos = 1;
883 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
884 (httpd_callback_sys_t*)stream );
885 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
886 (httpd_callback_sys_t*)stream );
887 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
888 (httpd_callback_sys_t*)stream );
893 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
895 vlc_mutex_lock( &stream->lock );
896 if( stream->p_header )
898 free( stream->p_header );
899 stream->p_header = NULL;
901 stream->i_header = i_data;
904 stream->p_header = malloc( i_data );
905 memcpy( stream->p_header, p_data, i_data );
907 vlc_mutex_unlock( &stream->lock );
912 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
917 if( i_data < 0 || p_data == NULL )
921 vlc_mutex_lock( &stream->lock );
923 /* save this pointer (to be used by new connection) */
924 stream->i_buffer_last_pos = stream->i_buffer_pos;
926 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
932 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
934 /* Ok, we can't go past the end of our buffer */
935 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
937 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
942 stream->i_buffer_pos += i_data;
944 vlc_mutex_unlock( &stream->lock );
948 void httpd_StreamDelete( httpd_stream_t *stream )
950 httpd_UrlDelete( stream->url );
951 vlc_mutex_destroy( &stream->lock );
952 if( stream->psz_mime ) free( stream->psz_mime );
953 if( stream->p_header ) free( stream->p_header );
954 if( stream->p_buffer ) free( stream->p_buffer );
958 /*****************************************************************************
960 *****************************************************************************/
961 static void httpd_HostThread( httpd_host_t * );
963 /* create a new host */
964 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
967 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
971 static const char psz_object_type[] = "http server";
973 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
975 const char *psz_cert, const char *psz_key,
976 const char *psz_ca, const char *psz_crl )
985 if( psz_hostname == NULL )
988 psz_host = strdup( psz_hostname );
989 if( psz_host == NULL )
991 msg_Err( p_this, "memory error" );
995 /* to be sure to avoid multiple creation */
996 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
997 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
998 vlc_mutex_lock( lockval.p_address );
1000 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1002 msg_Info( p_this, "creating httpd" );
1003 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1008 vlc_mutex_unlock( lockval.p_address );
1016 vlc_object_yield( httpd );
1017 vlc_object_attach( httpd, p_this->p_libvlc );
1020 /* verify if it already exist */
1021 for( i = httpd->i_host - 1; i >= 0; i-- )
1023 host = httpd->host[i];
1025 /* cannot mix TLS and non-TLS hosts */
1026 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1027 || ( host->i_port != i_port )
1028 || strcmp( host->psz_hostname, psz_hostname ) )
1034 vlc_mutex_unlock( lockval.p_address );
1040 /* determine TLS configuration */
1041 if ( psz_cert != NULL )
1043 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1044 if ( p_tls == NULL )
1046 msg_Err( p_this, "TLS initialization error" );
1050 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1052 msg_Err( p_this, "TLS CA error" );
1056 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1058 msg_Err( p_this, "TLS CRL error" );
1065 /* create the new host */
1066 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1067 VLC_OBJECT_HTTPD_HOST,
1072 host->httpd = httpd;
1073 vlc_mutex_init( httpd, &host->lock );
1076 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1077 if( host->fds == NULL )
1079 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1082 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1084 host->i_port = i_port;
1085 host->psz_hostname = psz_host;
1090 host->client = NULL;
1092 host->p_tls = p_tls;
1094 /* create the thread */
1095 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1096 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1098 msg_Err( p_this, "cannot spawn http host thread" );
1102 /* now add it to httpd */
1103 TAB_APPEND( httpd->i_host, httpd->host, host );
1104 vlc_mutex_unlock( lockval.p_address );
1110 if( httpd->i_host <= 0 )
1112 vlc_object_release( httpd );
1113 vlc_object_detach( httpd );
1114 vlc_object_destroy( httpd );
1116 vlc_mutex_unlock( lockval.p_address );
1120 net_ListenClose( host->fds );
1121 vlc_mutex_destroy( &host->lock );
1122 vlc_object_destroy( host );
1126 tls_ServerDelete( p_tls );
1132 void httpd_HostDelete( httpd_host_t *host )
1134 httpd_t *httpd = host->httpd;
1135 vlc_value_t lockval;
1138 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1139 vlc_mutex_lock( lockval.p_address );
1142 if( host->i_ref > 0 )
1145 vlc_mutex_unlock( lockval.p_address );
1146 msg_Dbg( host, "httpd_HostDelete: host still used" );
1149 TAB_REMOVE( httpd->i_host, httpd->host, host );
1151 vlc_object_kill( host );
1152 vlc_thread_join( host );
1154 msg_Dbg( host, "HTTP host removed" );
1156 for( i = 0; i < host->i_url; i++ )
1158 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1160 for( i = 0; i < host->i_client; i++ )
1162 httpd_client_t *cl = host->client[i];
1163 msg_Warn( host, "client still connected" );
1164 httpd_ClientClean( cl );
1165 TAB_REMOVE( host->i_client, host->client, cl );
1171 if( host->p_tls != NULL)
1172 tls_ServerDelete( host->p_tls );
1174 net_ListenClose( host->fds );
1175 free( host->psz_hostname );
1177 vlc_mutex_destroy( &host->lock );
1178 vlc_object_destroy( host );
1180 vlc_object_release( httpd );
1181 if( httpd->i_host <= 0 )
1183 msg_Dbg( httpd, "no host left, stopping httpd" );
1184 vlc_object_detach( httpd );
1185 vlc_object_destroy( httpd );
1187 vlc_mutex_unlock( lockval.p_address );
1190 /* register a new url */
1191 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1192 const char *psz_user, const char *psz_password,
1193 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1198 vlc_mutex_lock( &host->lock );
1201 for( i = 0; i < host->i_url; i++ )
1203 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1205 msg_Warn( host->httpd,
1206 "cannot add '%s' (url already defined)", psz_url );
1207 vlc_mutex_unlock( &host->lock );
1213 url = malloc( sizeof( httpd_url_t ) );
1216 vlc_mutex_init( host->httpd, &url->lock );
1217 url->psz_url = strdup( psz_url );
1218 url->psz_user = strdup( psz_user ? psz_user : "" );
1219 url->psz_password = strdup( psz_password ? psz_password : "" );
1220 url->p_acl = ACL_Duplicate( host, p_acl );
1221 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1223 url->catch[i].cb = NULL;
1224 url->catch[i].p_sys = NULL;
1227 TAB_APPEND( host->i_url, host->url, url );
1228 vlc_mutex_unlock( &host->lock );
1233 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1234 const char *psz_user, const char *psz_password,
1235 const vlc_acl_t *p_acl )
1237 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1238 psz_password, p_acl, VLC_FALSE );
1241 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1242 const char *psz_user, const char *psz_password,
1243 const vlc_acl_t *p_acl )
1245 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1246 psz_password, p_acl, VLC_TRUE );
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 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;
1302 msg->psz_status = NULL;
1304 msg->psz_url = NULL;
1305 msg->psz_args = NULL;
1307 msg->i_channel = -1;
1314 msg->i_body_offset = 0;
1319 void httpd_MsgClean( httpd_message_t *msg )
1323 if( msg->psz_status )
1325 free( msg->psz_status );
1329 free( msg->psz_url );
1333 free( msg->psz_args );
1335 for( i = 0; i < msg->i_name; i++ )
1337 free( msg->name[i] );
1338 free( msg->value[i] );
1350 free( msg->p_body );
1352 httpd_MsgInit( msg );
1355 const char *httpd_MsgGet( httpd_message_t *msg, const char *name )
1359 for( i = 0; i < msg->i_name; i++ )
1361 if( !strcasecmp( msg->name[i], name ))
1363 return msg->value[i];
1369 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1374 va_start( args, psz_value );
1375 if( vasprintf( &value, psz_value, args ) == -1 )
1382 name = strdup( name );
1389 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1390 TAB_APPEND( msg->i_value, msg->value, value );
1393 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1395 cl->i_state = HTTPD_CLIENT_RECEIVING;
1396 cl->i_activity_date = now;
1397 cl->i_activity_timeout = I64C(10000000);
1398 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1400 cl->p_buffer = malloc( cl->i_buffer_size );
1401 cl->i_mode = HTTPD_CLIENT_FILE;
1402 cl->b_read_waiting = VLC_FALSE;
1404 httpd_MsgInit( &cl->query );
1405 httpd_MsgInit( &cl->answer );
1408 void httpd_ClientModeStream( httpd_client_t *cl )
1410 cl->i_mode = HTTPD_CLIENT_STREAM;
1413 void httpd_ClientModeBidir( httpd_client_t *cl )
1415 cl->i_mode = HTTPD_CLIENT_BIDIR;
1418 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1420 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1423 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1425 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1428 static void httpd_ClientClean( httpd_client_t *cl )
1432 if( cl->p_tls != NULL )
1433 tls_ServerSessionClose( cl->p_tls );
1434 net_Close( cl->fd );
1438 httpd_MsgClean( &cl->answer );
1439 httpd_MsgClean( &cl->query );
1443 free( cl->p_buffer );
1444 cl->p_buffer = NULL;
1448 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1450 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1452 if( !cl ) return NULL;
1459 httpd_ClientInit( cl, now );
1464 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1466 tls_session_t *p_tls;
1470 return tls_Recv( p_tls, p, i_len );
1472 return recv( cl->fd, p, i_len, 0 );
1475 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1477 tls_session_t *p_tls;
1481 return tls_Send( p_tls, p, i_len );
1483 return send( cl->fd, p, i_len, 0 );
1486 static void httpd_ClientRecv( httpd_client_t *cl )
1490 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1492 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1493 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1497 cl->i_buffer += i_len;
1500 if( cl->i_buffer >= 4 )
1502 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1504 if( cl->p_buffer[0] == '$' )
1506 /* RTSP (rtp over rtsp) */
1507 cl->query.i_proto = HTTPD_PROTO_RTSP;
1508 cl->query.i_type = HTTPD_MSG_CHANNEL;
1509 cl->query.i_channel = cl->p_buffer[1];
1510 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1511 cl->query.p_body = malloc( cl->query.i_body );
1515 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1517 cl->query.i_proto = HTTPD_PROTO_HTTP;
1518 cl->query.i_type = HTTPD_MSG_ANSWER;
1520 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1522 cl->query.i_proto = HTTPD_PROTO_RTSP;
1523 cl->query.i_type = HTTPD_MSG_ANSWER;
1525 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1526 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1527 !memcmp( cl->p_buffer, "POST", 4 ) )
1529 cl->query.i_proto = HTTPD_PROTO_HTTP;
1530 cl->query.i_type = HTTPD_MSG_NONE;
1534 cl->query.i_proto = HTTPD_PROTO_RTSP;
1535 cl->query.i_type = HTTPD_MSG_NONE;
1539 else if( cl->query.i_body > 0 )
1541 /* we are reading the body of a request or a channel */
1542 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1543 cl->query.i_body - cl->i_buffer );
1546 cl->i_buffer += i_len;
1548 if( cl->i_buffer >= cl->query.i_body )
1550 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1555 /* we are reading a header -> char by char */
1558 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1565 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1567 cl->i_buffer_size += 1024;
1568 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1570 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1571 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1575 /* we have finished the header so parse it and set i_body */
1576 cl->p_buffer[cl->i_buffer] = '\0';
1578 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1581 * assume strlen( "HTTP/1.x" ) = 8
1583 cl->query.i_status =
1584 strtol( (char *)&cl->p_buffer[8],
1590 cl->query.psz_status = strdup( p );
1602 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1603 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1604 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1605 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1606 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1607 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1608 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1610 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1611 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1612 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1614 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1619 cl->query.i_type = HTTPD_MSG_NONE;
1621 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1623 for( i = 0; msg_type[i].name != NULL; i++ )
1625 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1626 strlen( msg_type[i].name ) ) )
1628 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1629 cl->query.i_type = msg_type[i].i_type;
1630 if( cl->query.i_proto != msg_type[i].i_proto )
1633 cl->query.i_proto = HTTPD_PROTO_NONE;
1634 cl->query.i_type = HTTPD_MSG_NONE;
1641 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1643 cl->query.i_proto = HTTPD_PROTO_HTTP;
1645 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1647 cl->query.i_proto = HTTPD_PROTO_RTSP;
1659 p2 = strchr( p, ' ' );
1664 if( !strncasecmp( p, "rtsp:", 5 ) )
1666 /* for rtsp url, you have rtsp://localhost:port/path */
1668 while( *p == '/' ) p++;
1669 while( *p && *p != '/' ) p++;
1671 cl->query.psz_url = strdup( p );
1672 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1675 cl->query.psz_args = (uint8_t *)strdup( p3 );
1683 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1685 cl->query.i_proto = HTTPD_PROTO_HTTP;
1686 cl->query.i_version = atoi( p2+7 );
1688 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1690 cl->query.i_proto = HTTPD_PROTO_RTSP;
1691 cl->query.i_version = atoi( p2+7 );
1699 p = strchr( p, '\n' );
1703 while( *p == '\n' || *p == '\r' )
1707 while( p && *p != '\0' )
1710 char *eol = p = strchr( p, '\n' );
1713 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1718 if( ( colon = strchr( line, ':' ) ) )
1724 while( *colon == ' ' )
1728 name = strdup( line );
1729 value = strdup( colon );
1731 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1732 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1734 if( !strcasecmp( name, "Content-Length" ) )
1736 cl->query.i_body = atol( value );
1743 while( *p == '\n' || *p == '\r' )
1750 if( cl->query.i_body > 0 )
1752 /* TODO Mhh, handle the case client will only send a request and close the connection
1753 * to mark and of body (probably only RTSP) */
1754 cl->query.p_body = malloc( cl->query.i_body );
1759 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1765 /* check if the client is to be set to dead */
1766 #if defined( WIN32 ) || defined( UNDER_CE )
1767 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1769 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1772 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1774 /* connection closed -> end of data */
1775 if( cl->query.i_body > 0 )
1777 cl->query.i_body = cl->i_buffer;
1779 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1783 cl->i_state = HTTPD_CLIENT_DEAD;
1787 /* XXX: for QT I have to disable timeout. Try to find why */
1788 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1789 cl->i_activity_timeout = 0;
1791 #if 0 /* Debugging only */
1792 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1796 fprintf( stderr, "received new request\n" );
1797 fprintf( stderr, " - proto=%s\n",
1798 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1799 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1800 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1801 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1803 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1804 cl->query.psz_status );
1806 else if( cl->query.i_type != HTTPD_MSG_NONE )
1808 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1810 for( i = 0; i < cl->query.i_name; i++ )
1812 fprintf( stderr, " - option name='%s' value='%s'\n",
1813 cl->query.name[i], cl->query.value[i] );
1819 static void httpd_ClientSend( httpd_client_t *cl )
1824 if( cl->i_buffer < 0 )
1826 /* We need to create the header */
1830 i_size = strlen( "HTTP/1.") + 10 + 10 +
1831 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1832 for( i = 0; i < cl->answer.i_name; i++ )
1834 i_size += strlen( cl->answer.name[i] ) + 2 +
1835 strlen( cl->answer.value[i] ) + 2;
1838 if( cl->i_buffer_size < i_size )
1840 cl->i_buffer_size = i_size;
1841 free( cl->p_buffer );
1842 cl->p_buffer = malloc( i_size );
1844 p = (char *)cl->p_buffer;
1846 p += sprintf( p, "%s/1.%d %d %s\r\n",
1847 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1848 cl->answer.i_version,
1849 cl->answer.i_status, cl->answer.psz_status );
1850 for( i = 0; i < cl->answer.i_name; i++ )
1852 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1853 cl->answer.value[i] );
1855 p += sprintf( p, "\r\n" );
1858 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1860 /*fprintf( stderr, "sending answer\n" );
1861 fprintf( stderr, "%s", cl->p_buffer );*/
1864 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1865 cl->i_buffer_size - cl->i_buffer );
1868 cl->i_buffer += i_len;
1870 if( cl->i_buffer >= cl->i_buffer_size )
1872 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1873 !cl->b_read_waiting )
1875 /* catch more body data */
1876 int i_msg = cl->query.i_type;
1877 int64_t i_offset = cl->answer.i_body_offset;
1879 httpd_MsgClean( &cl->answer );
1880 cl->answer.i_body_offset = i_offset;
1882 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1883 &cl->answer, &cl->query );
1886 if( cl->answer.i_body > 0 )
1888 /* send the body data */
1889 free( cl->p_buffer );
1890 cl->p_buffer = cl->answer.p_body;
1891 cl->i_buffer_size = cl->answer.i_body;
1894 cl->answer.i_body = 0;
1895 cl->answer.p_body = NULL;
1900 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1906 #if defined( WIN32 ) || defined( UNDER_CE )
1907 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1909 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1913 cl->i_state = HTTPD_CLIENT_DEAD;
1918 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1920 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1923 cl->i_state = HTTPD_CLIENT_RECEIVING;
1927 cl->i_state = HTTPD_CLIENT_DEAD;
1932 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1936 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1938 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1941 cl->i_state = HTTPD_CLIENT_RECEIVING;
1945 cl->i_state = HTTPD_CLIENT_DEAD;
1950 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1955 static void httpd_HostThread( httpd_host_t *host )
1957 tls_session_t *p_tls = NULL;
1958 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1959 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1961 while( !host->b_die )
1963 if( host->i_url <= 0 )
1970 /* prepare a new TLS session */
1971 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1972 p_tls = tls_ServerSessionPrepare( host->p_tls );
1974 struct pollfd ufd[host->nfd + host->i_client];
1976 for( nfd = 0; nfd < host->nfd; nfd++ )
1978 ufd[nfd].fd = host->fds[nfd];
1979 ufd[nfd].events = POLLIN;
1980 ufd[nfd].revents = 0;
1983 /* add all socket that should be read/write and close dead connection */
1984 vlc_mutex_lock( &host->lock );
1985 mtime_t now = mdate();
1986 vlc_bool_t b_low_delay = VLC_FALSE;
1988 for(int i_client = 0; i_client < host->i_client; i_client++ )
1990 httpd_client_t *cl = host->client[i_client];
1991 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1992 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1993 ( cl->i_activity_timeout > 0 &&
1994 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
1996 httpd_ClientClean( cl );
1997 stats_UpdateInteger( host, p_active_counter, -1, NULL );
1998 TAB_REMOVE( host->i_client, host->client, cl );
2004 struct pollfd *pufd = ufd + nfd;
2005 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2008 pufd->events = pufd->revents = 0;
2010 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2011 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2013 pufd->events = POLLIN;
2015 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2016 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2018 pufd->events = POLLOUT;
2020 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2022 httpd_message_t *answer = &cl->answer;
2023 httpd_message_t *query = &cl->query;
2024 int i_msg = query->i_type;
2026 httpd_MsgInit( answer );
2028 /* Handle what we received */
2029 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2030 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2032 /* we can only receive request from client when not
2035 cl->i_state = HTTPD_CLIENT_DEAD;
2037 else if( i_msg == HTTPD_MSG_ANSWER )
2039 /* We are in BIDIR mode, trigger the callback and then
2040 * check for new data */
2041 if( cl->url && cl->url->catch[i_msg].cb )
2043 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2046 cl->i_state = HTTPD_CLIENT_WAITING;
2048 else if( i_msg == HTTPD_MSG_CHANNEL )
2050 /* We are in BIDIR mode, trigger the callback and then
2051 * check for new data */
2052 if( cl->url && cl->url->catch[i_msg].cb )
2054 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2057 cl->i_state = HTTPD_CLIENT_WAITING;
2059 else if( i_msg == HTTPD_MSG_OPTIONS )
2061 const char *psz_cseq;
2065 answer->i_proto = query->i_proto ;
2066 answer->i_type = HTTPD_MSG_ANSWER;
2067 answer->i_version= 0;
2068 answer->i_status = 200;
2069 answer->psz_status = strdup( "Ok" );
2072 answer->p_body = NULL;
2074 psz_cseq = httpd_MsgGet( query, "Cseq" );
2076 i_cseq = atoi( psz_cseq );
2079 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2080 httpd_MsgAdd( answer, "Server", "VLC Server" );
2081 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2082 "TEARDOWN, PLAY, PAUSE" );
2083 httpd_MsgAdd( answer, "Content-Length", "%d",
2086 cl->i_buffer = -1; /* Force the creation of the answer in
2087 * httpd_ClientSend */
2088 cl->i_state = HTTPD_CLIENT_SENDING;
2090 else if( i_msg == HTTPD_MSG_NONE )
2092 if( query->i_proto == HTTPD_PROTO_NONE )
2095 cl->i_state = HTTPD_CLIENT_DEAD;
2102 answer->i_proto = query->i_proto ;
2103 answer->i_type = HTTPD_MSG_ANSWER;
2104 answer->i_version= 0;
2105 answer->i_status = 501;
2106 answer->psz_status = strdup( "Unimplemented" );
2108 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2109 answer->p_body = (uint8_t *)p;
2110 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2112 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2113 cl->i_state = HTTPD_CLIENT_SENDING;
2118 vlc_bool_t b_auth_failed = VLC_FALSE;
2119 vlc_bool_t b_hosts_failed = VLC_FALSE;
2121 /* Search the url and trigger callbacks */
2122 for(int i = 0; i < host->i_url; i++ )
2124 httpd_url_t *url = host->url[i];
2126 if( !strcmp( url->psz_url, query->psz_url ) )
2128 if( url->catch[i_msg].cb )
2130 if( answer && ( url->p_acl != NULL ) )
2132 char ip[NI_MAXNUMERICHOST];
2134 if( ( httpd_ClientIP( cl, ip ) == NULL )
2135 || ACL_Check( url->p_acl, ip ) )
2137 b_hosts_failed = VLC_TRUE;
2142 if( answer && ( *url->psz_user || *url->psz_password ) )
2144 /* create the headers */
2145 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2146 char *user = NULL, *pass = NULL;
2149 && !strncasecmp( b64, "BASIC", 5 ) )
2152 while( *b64 == ' ' )
2155 user = vlc_b64_decode( b64 );
2158 pass = strchr (user, ':');
2164 if ((user == NULL) || (pass == NULL)
2165 || strcmp (user, url->psz_user)
2166 || strcmp (pass, url->psz_password))
2168 httpd_MsgAdd( answer,
2170 "Basic realm=\"%s\"",
2172 /* We fail for all url */
2173 b_auth_failed = VLC_TRUE;
2181 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2183 if( answer->i_proto == HTTPD_PROTO_NONE )
2185 /* Raw answer from a CGI */
2186 cl->i_buffer = cl->i_buffer_size;
2191 /* only one url can answer */
2193 if( cl->url == NULL )
2206 answer->i_proto = query->i_proto;
2207 answer->i_type = HTTPD_MSG_ANSWER;
2208 answer->i_version= 0;
2210 if( b_hosts_failed )
2212 answer->i_status = 403;
2213 answer->psz_status = strdup( "Forbidden" );
2215 else if( b_auth_failed )
2217 answer->i_status = 401;
2218 answer->psz_status = strdup( "Authorization Required" );
2222 /* no url registered */
2223 answer->i_status = 404;
2224 answer->psz_status = strdup( "Not found" );
2227 answer->i_body = httpd_HtmlError (&p,
2230 answer->p_body = (uint8_t *)p;
2232 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2233 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2234 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2237 cl->i_state = HTTPD_CLIENT_SENDING;
2240 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2242 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2244 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2245 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2246 vlc_bool_t b_connection = VLC_FALSE;
2247 vlc_bool_t b_keepalive = VLC_FALSE;
2248 vlc_bool_t b_query = VLC_FALSE;
2251 if( psz_connection )
2253 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2254 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2259 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2262 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2263 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2264 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2265 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2266 !b_query && !b_connection ) )
2268 httpd_MsgClean( &cl->query );
2269 httpd_MsgInit( &cl->query );
2272 cl->i_buffer_size = 1000;
2273 free( cl->p_buffer );
2274 cl->p_buffer = malloc( cl->i_buffer_size );
2275 cl->i_state = HTTPD_CLIENT_RECEIVING;
2279 cl->i_state = HTTPD_CLIENT_DEAD;
2281 httpd_MsgClean( &cl->answer );
2283 else if( cl->b_read_waiting )
2285 /* we have a message waiting for us to read it */
2286 httpd_MsgClean( &cl->answer );
2287 httpd_MsgClean( &cl->query );
2290 cl->i_buffer_size = 1000;
2291 free( cl->p_buffer );
2292 cl->p_buffer = malloc( cl->i_buffer_size );
2293 cl->i_state = HTTPD_CLIENT_RECEIVING;
2294 cl->b_read_waiting = VLC_FALSE;
2298 int64_t i_offset = cl->answer.i_body_offset;
2299 httpd_MsgClean( &cl->answer );
2301 cl->answer.i_body_offset = i_offset;
2302 free( cl->p_buffer );
2303 cl->p_buffer = NULL;
2305 cl->i_buffer_size = 0;
2307 cl->i_state = HTTPD_CLIENT_WAITING;
2310 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2312 int64_t i_offset = cl->answer.i_body_offset;
2313 int i_msg = cl->query.i_type;
2315 httpd_MsgInit( &cl->answer );
2316 cl->answer.i_body_offset = i_offset;
2318 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2319 &cl->answer, &cl->query );
2320 if( cl->answer.i_type != HTTPD_MSG_NONE )
2322 /* we have new data, so re-enter send mode */
2324 cl->p_buffer = cl->answer.p_body;
2325 cl->i_buffer_size = cl->answer.i_body;
2326 cl->answer.p_body = NULL;
2327 cl->answer.i_body = 0;
2328 cl->i_state = HTTPD_CLIENT_SENDING;
2332 /* we shouldn't wait too long */
2333 b_low_delay = VLC_TRUE;
2337 /* Special for BIDIR mode we also check reading */
2338 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2339 cl->i_state == HTTPD_CLIENT_SENDING )
2341 pufd->events |= POLLIN;
2344 if (pufd->events != 0)
2347 vlc_mutex_unlock( &host->lock );
2349 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2350 switch( poll( ufd, nfd, b_low_delay ? 20 : 100) )
2355 /* This is most likely a bug */
2356 msg_Err( host, "polling error: %s", strerror (errno));
2363 /* Handle client sockets */
2364 vlc_mutex_lock( &host->lock );
2366 for( int i_client = 0; i_client < host->i_client; i_client++ )
2368 httpd_client_t *cl = host->client[i_client];
2369 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2371 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2373 if( cl->fd != pufd->fd )
2374 continue; // we were not waiting for this client
2375 if( pufd->revents == 0 )
2376 continue; // no event received
2378 cl->i_activity_date = now;
2380 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2382 httpd_ClientRecv( cl );
2384 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2386 httpd_ClientSend( cl );
2388 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2390 httpd_ClientTlsHsIn( cl );
2392 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2394 httpd_ClientTlsHsOut( cl );
2397 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2398 cl->i_state == HTTPD_CLIENT_SENDING &&
2399 (pufd->revents & POLLIN) )
2401 cl->b_read_waiting = VLC_TRUE;
2404 vlc_mutex_unlock( &host->lock );
2406 /* Handle server sockets (accept new connections) */
2407 for( nfd = 0; nfd < host->nfd; nfd++ )
2412 assert (ufd[nfd].fd == host->fds[nfd]);
2414 if( ufd[nfd].revents == 0 )
2418 int kludge[] = { ufd[nfd].fd, -1 };
2419 int fd = net_Accept( host, kludge, 0 );
2425 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2428 msg_Err( host, "Rejecting TLS connection" );
2434 case 1: /* missing input - most likely */
2435 i_state = HTTPD_CLIENT_TLS_HS_IN;
2438 case 2: /* missing output */
2439 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2443 if( (p_tls == NULL) != (host->p_tls == NULL) )
2444 break; // wasted TLS session, cannot accept() anymore
2447 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2448 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2449 cl = httpd_ClientNew( fd, p_tls, now );
2451 vlc_mutex_lock( &host->lock );
2452 TAB_APPEND( host->i_client, host->client, cl );
2453 vlc_mutex_unlock( &host->lock );
2455 cl->i_state = i_state; // override state for TLS
2457 if (host->p_tls != NULL)
2458 break; // cannot accept further without new TLS session
2464 tls_ServerSessionClose( p_tls );
2465 if( p_total_counter )
2466 stats_CounterClean( p_total_counter );
2467 if( p_active_counter )
2468 stats_CounterClean( p_active_counter );
2471 #else /* ENABLE_HTTPD */
2473 /* We just define an empty wrapper */
2474 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2477 msg_Err( a, "HTTP daemon support is disabled" );
2481 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2483 msg_Err( a, "HTTP daemon support is disabled" );
2487 void httpd_HostDelete( httpd_host_t *a )
2491 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2492 char *psz_user, char *psz_password,
2493 const vlc_acl_t *p_acl )
2498 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2499 char *psz_user, char *psz_password,
2500 const vlc_acl_t *p_acl )
2505 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2506 httpd_callback_sys_t *d )
2511 void httpd_UrlDelete( httpd_url_t *a )
2515 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2520 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2525 void httpd_ClientModeStream( httpd_client_t *a )
2529 void httpd_ClientModeBidir( httpd_client_t *a )
2533 void httpd_FileDelete( httpd_file_t *a )
2537 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2538 char *e, httpd_file_callback_t f,
2539 httpd_file_sys_t *g )
2544 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2545 const char *psz_user,
2546 const char *psz_password,
2547 const vlc_acl_t *p_acl,
2548 httpd_handler_callback_t pf_fill,
2549 httpd_handler_sys_t *p_sys )
2554 void httpd_HandlerDelete( httpd_handler_t *handler )
2558 void httpd_RedirectDelete( httpd_redirect_t *a )
2562 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2568 void httpd_StreamDelete( httpd_stream_t *a )
2572 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2577 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2582 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2588 void httpd_MsgInit ( httpd_message_t *a )
2592 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2596 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2601 void httpd_MsgClean( httpd_message_t *a )
2605 #endif /* ENABLE_HTTPD */