1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
32 #include "vlc_httpd.h"
48 #if defined( UNDER_CE )
50 #elif defined( WIN32 )
51 # include <winsock2.h>
53 # include <sys/socket.h>
57 /* We need HUGE buffer otherwise TCP throughput is very limited */
58 #define HTTPD_CL_BUFSIZE 1000000
60 #define HTTPD_CL_BUFSIZE 10000
63 static void httpd_ClientClean( httpd_client_t *cl );
65 /* each host run in his own thread */
75 /* address/port and socket for listening at connections */
82 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
83 * This will slow down the url research but make my live easier
84 * All url will have their cb trigger, but only the first one can answer
90 httpd_client_t **client;
110 httpd_callback_sys_t *p_sys;
111 } catch[HTTPD_MSG_MAX];
117 HTTPD_CLIENT_RECEIVING,
118 HTTPD_CLIENT_RECEIVE_DONE,
120 HTTPD_CLIENT_SENDING,
121 HTTPD_CLIENT_SEND_DONE,
123 HTTPD_CLIENT_WAITING,
127 HTTPD_CLIENT_TLS_HS_IN,
128 HTTPD_CLIENT_TLS_HS_OUT
134 HTTPD_CLIENT_FILE, /* default */
135 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
136 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
139 struct httpd_client_t
145 struct sockaddr_storage sock;
151 int b_read_waiting; /* stop as soon as possible sending */
153 mtime_t i_activity_date;
154 mtime_t i_activity_timeout;
156 /* buffer for reading header */
162 httpd_message_t query; /* client -> httpd */
163 httpd_message_t answer; /* httpd -> client */
166 tls_session_t *p_tls;
170 /*****************************************************************************
172 *****************************************************************************/
173 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
174 static void b64_decode( char *dest, char *src )
179 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
180 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
181 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
182 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
183 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
184 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
185 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
186 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
187 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
188 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
189 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
190 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
191 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
192 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
193 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
194 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
197 for( i_level = 0; *src != '\0'; src++ )
201 c = b64[(unsigned int)*src];
213 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
217 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
221 *dest++ = ( ( last &0x03 ) << 6 ) | c;
233 const char *psz_mime;
236 { ".htm", "text/html" },
237 { ".html", "text/html" },
238 { ".txt", "text/plain" },
239 { ".xml", "text/xml" },
240 { ".dtd", "text/dtd" },
242 { ".css", "text/css" },
245 { ".gif", "image/gif" },
246 { ".jpe", "image/jpeg" },
247 { ".jpg", "image/jpeg" },
248 { ".jpeg", "image/jpeg" },
249 { ".png", "image/png" },
250 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
253 { ".avi", "video/avi" },
254 { ".asf", "video/x-ms-asf" },
255 { ".m1a", "audio/mpeg" },
256 { ".m2a", "audio/mpeg" },
257 { ".m1v", "video/mpeg" },
258 { ".m2v", "video/mpeg" },
259 { ".mp2", "audio/mpeg" },
260 { ".mp3", "audio/mpeg" },
261 { ".mpa", "audio/mpeg" },
262 { ".mpg", "video/mpeg" },
263 { ".mpeg", "video/mpeg" },
264 { ".mpe", "video/mpeg" },
265 { ".mov", "video/quicktime" },
266 { ".moov", "video/quicktime" },
267 { ".ogg", "application/ogg" },
268 { ".ogm", "application/ogg" },
269 { ".wav", "audio/wav" },
270 { ".wma", "audio/x-ms-wma" },
271 { ".wmv", "video/x-ms-wmv" },
278 static const char *httpd_MimeFromUrl( const char *psz_url )
283 psz_ext = strrchr( psz_url, '.' );
288 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
290 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
292 return http_mime[i].psz_mime;
296 return "application/octet-stream";
303 const char *psz_reason;
306 static const http_status_info http_reason[] =
308 /*{ 100, "Continue" },
309 { 101, "Switching Protocols" },*/
313 { 203, "Non-Authoritative Information" },
314 { 204, "No Content" },
315 { 205, "Reset Content"Â },
316 { 206, "Partial Content" },
317 { 250, "Low on Storage Space" },
318 { 300, "Multiple Choices" }*/,
319 { 301, "Moved Permanently" }/*,
320 { 302, "Moved Temporarily" }, - aka "Found"
321 { 303, "See Other" },
322 { 304, "Not Modified" },
323 { 305, "Use Proxy" },
324 { 307, "Temporary Redirect" },
325 { 400, "Bad Request" }*/,
326 { 401, "Unauthorized" }/*,
327 { 402, "Payment Required" }*/,
328 { 403, "Forbidden" },
329 { 404, "Not Found" }/*,
330 { 405, "Method Not Allowed" },
331 { 406, "Not Acceptable" },
332 { 407, "Proxy Authentication Required" },
333 { 408, "Request Time-out" },
336 { 411, "Length Required" },
337 { 412, "Precondition Failed" },
338 { 413, "Request Entity Too Large" },
339 { 414, "Request-URI Too Large" },
340 { 415, "Unsupported Media Type" },
341 { 416, "Requested range not satisfiable" },
342 { 417, "Expectation Failed" },
343 { 451, "Parameter Not Understood" },
344 { 452, "Conference Not Found" },
345 { 453, "Not Enough Bandwidth" }*/,
346 { 454, "Session Not Found" }/*,
347 { 455, "Method Not Valid in This State" },
348 { 456, "Header Field Not Valid for Resource" },
349 { 457, "Invalid Range" },
350 { 458, "Parameter Is Read-Only" },
351 { 459, "Aggregate operation not allowed" },
352 { 460, "Only aggregate operation allowed" }*/,
353 { 461, "Unsupported transport" }/*,
354 { 462, "Destination unreachable" }*/,
355 { 500, "Internal Server Error" },
356 { 501, "Not Implemented" }/*,
357 { 502, "Bad Gateway" }*/,
358 { 503, "Service Unavailable" }/*,
359 { 504, "Gateway Time-out" },
360 { 505, "Protocol version not supported" }*/,
364 static const char *psz_fallback_reason[] =
365 { "Continue", "OK", "Found", "Client Error", "Server Error" };
367 static const char *httpd_ReasonFromCode( int i_code )
369 const http_status_info *p;
371 for (p = http_reason; p->i_code < i_code; p++);
373 if( p->i_code == i_code )
374 return p->psz_reason;
376 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
377 return psz_fallback_reason[(i_code / 100) - 1];
381 /*****************************************************************************
382 * High Level Functions: httpd_file_t
383 *****************************************************************************/
391 httpd_file_callback_t pf_fill;
392 httpd_file_sys_t *p_sys;
396 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
398 httpd_file_t *file = (httpd_file_t*)p_sys;
399 uint8_t *psz_args = query->psz_args;
400 uint8_t **pp_body, *p_body;
401 int *pi_body, i_body;
403 if( answer == NULL || query == NULL )
407 answer->i_proto = HTTPD_PROTO_HTTP;
408 answer->i_version= query->i_version;
409 answer->i_type = HTTPD_MSG_ANSWER;
411 answer->i_status = 200;
412 answer->psz_status = strdup( "OK" );
414 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
415 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
417 if( query->i_type != HTTPD_MSG_HEAD )
419 pp_body = &answer->p_body;
420 pi_body = &answer->i_body;
424 /* The file still needs to be executed. */
431 if( query->i_type == HTTPD_MSG_POST )
433 /* msg_Warn not supported */
436 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
438 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
443 /* We respect client request */
444 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
446 httpd_MsgAdd( answer, "Connection",
447 httpd_MsgGet( &cl->query, "Connection" ) );
450 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
455 httpd_file_t *httpd_FileNew( httpd_host_t *host,
456 const char *psz_url, const char *psz_mime,
457 const char *psz_user, const char *psz_password,
458 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
459 httpd_file_sys_t *p_sys )
461 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
463 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
464 psz_password, p_acl )
471 file->psz_url = strdup( psz_url );
472 if( psz_mime && *psz_mime )
474 file->psz_mime = strdup( psz_mime );
478 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
481 file->pf_fill = pf_fill;
484 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
485 (httpd_callback_sys_t*)file );
486 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
487 (httpd_callback_sys_t*)file );
488 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
489 (httpd_callback_sys_t*)file );
494 void httpd_FileDelete( httpd_file_t *file )
496 httpd_UrlDelete( file->url );
498 free( file->psz_url );
499 free( file->psz_mime );
504 /*****************************************************************************
505 * High Level Functions: httpd_handler_t (for CGIs)
506 *****************************************************************************/
507 struct httpd_handler_t
511 httpd_handler_callback_t pf_fill;
512 httpd_handler_sys_t *p_sys;
516 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
518 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
519 uint8_t *psz_args = query->psz_args;
520 char psz_remote_addr[NI_MAXNUMERICHOST];
522 if( answer == NULL || query == NULL )
526 answer->i_proto = HTTPD_PROTO_NONE;
527 answer->i_type = HTTPD_MSG_ANSWER;
529 /* We do it ourselves, thanks */
530 answer->i_status = 0;
531 answer->psz_status = NULL;
533 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
534 *psz_remote_addr = '\0';
536 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
537 query->i_type, query->p_body, query->i_body,
538 psz_remote_addr, NULL,
539 &answer->p_body, &answer->i_body );
541 if( query->i_type == HTTPD_MSG_HEAD )
543 char *p = (char *)answer->p_body;
544 while ( (p = strchr( p, '\r' )) != NULL )
546 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
547 && p[3] && p[3] == '\n' )
555 answer->i_body = strlen((char*)answer->p_body) + 1;
556 answer->p_body = realloc( answer->p_body, answer->i_body );
560 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
562 int i_status, i_headers;
563 char *psz_headers, *psz_new, *psz_status;
565 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
568 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
569 if( *psz_headers ) psz_headers++;
570 if( *psz_headers ) psz_headers++;
571 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
576 psz_headers = (char *)answer->p_body;
577 i_headers = answer->i_body;
585 psz_status = "Unauthorized";
588 if( (i_status < 0) || (i_status > 999) )
590 psz_status = "Undefined";
593 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
594 + strlen(psz_status) + i_headers - 1;
595 psz_new = (char *)malloc( answer->i_body + 1);
596 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
597 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
598 free( answer->p_body );
599 answer->p_body = (uint8_t *)psz_new;
605 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
606 const char *psz_user,
607 const char *psz_password,
608 const vlc_acl_t *p_acl,
609 httpd_handler_callback_t pf_fill,
610 httpd_handler_sys_t *p_sys )
612 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
614 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
615 psz_password, p_acl )
622 handler->pf_fill = pf_fill;
623 handler->p_sys = p_sys;
625 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
626 (httpd_callback_sys_t*)handler );
627 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
628 (httpd_callback_sys_t*)handler );
629 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
630 (httpd_callback_sys_t*)handler );
635 void httpd_HandlerDelete( httpd_handler_t *handler )
637 httpd_UrlDelete( handler->url );
641 /*****************************************************************************
642 * High Level Functions: httpd_redirect_t
643 *****************************************************************************/
644 struct httpd_redirect_t
650 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
651 httpd_client_t *cl, httpd_message_t *answer,
652 httpd_message_t *query )
654 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
656 if( answer == NULL || query == NULL )
660 answer->i_proto = query->i_proto;
661 answer->i_version= query->i_version;
662 answer->i_type = HTTPD_MSG_ANSWER;
663 answer->i_status = 301;
664 answer->psz_status = strdup( "Moved Permanently" );
666 answer->i_body = asprintf( (char **)&answer->p_body,
667 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
668 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
669 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
672 "<title>Redirection</title>\n"
676 "<a href=\"%s\">redirected</a></h1>\n"
678 "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
681 "</html>\n", rdir->psz_dst );
683 /* XXX check if it's ok or we need to set an absolute url */
684 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
686 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
691 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
692 const char *psz_url_src )
694 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
696 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
701 rdir->psz_dst = strdup( psz_url_dst );
703 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
704 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
705 (httpd_callback_sys_t*)rdir );
706 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
707 (httpd_callback_sys_t*)rdir );
708 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
709 (httpd_callback_sys_t*)rdir );
710 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
711 (httpd_callback_sys_t*)rdir );
715 void httpd_RedirectDelete( httpd_redirect_t *rdir )
717 httpd_UrlDelete( rdir->url );
718 free( rdir->psz_dst );
722 /*****************************************************************************
723 * High Level Funtions: httpd_stream_t
724 *****************************************************************************/
725 struct httpd_stream_t
732 /* Header to send as first packet */
736 /* circular buffer */
737 int i_buffer_size; /* buffer size, can't be reallocated smaller */
738 uint8_t *p_buffer; /* buffer */
739 int64_t i_buffer_pos; /* absolute position from begining */
740 int64_t i_buffer_last_pos; /* a new connection will start with that */
743 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
744 httpd_client_t *cl, httpd_message_t *answer,
745 httpd_message_t *query )
747 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
749 if( answer == NULL || query == NULL || cl == NULL )
754 if( answer->i_body_offset > 0 )
760 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
761 answer->i_body_offset );
764 if( answer->i_body_offset >= stream->i_buffer_pos )
766 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
767 return VLC_EGENERIC; /* wait, no data available */
769 if( answer->i_body_offset + stream->i_buffer_size <
770 stream->i_buffer_pos )
772 /* this client isn't fast enough */
774 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
775 answer->i_body_offset, stream->i_buffer_last_pos );
777 answer->i_body_offset = stream->i_buffer_last_pos;
780 i_pos = answer->i_body_offset % stream->i_buffer_size;
781 i_write = stream->i_buffer_pos - answer->i_body_offset;
782 if( i_write > HTTPD_CL_BUFSIZE )
784 i_write = HTTPD_CL_BUFSIZE;
786 else if( i_write <= 0 )
788 return VLC_EGENERIC; /* wait, no data available */
791 /* Don't go past the end of the circular buffer */
792 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
794 /* using HTTPD_MSG_ANSWER -> data available */
795 answer->i_proto = HTTPD_PROTO_HTTP;
796 answer->i_version= 0;
797 answer->i_type = HTTPD_MSG_ANSWER;
799 answer->i_body = i_write;
800 answer->p_body = malloc( i_write );
801 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
803 answer->i_body_offset += i_write;
809 answer->i_proto = HTTPD_PROTO_HTTP;
810 answer->i_version= 0;
811 answer->i_type = HTTPD_MSG_ANSWER;
813 answer->i_status = 200;
814 answer->psz_status = strdup( "OK" );
816 if( query->i_type != HTTPD_MSG_HEAD )
818 httpd_ClientModeStream( cl );
819 vlc_mutex_lock( &stream->lock );
820 /* Send the header */
821 if( stream->i_header > 0 )
823 answer->i_body = stream->i_header;
824 answer->p_body = malloc( stream->i_header );
825 memcpy( answer->p_body, stream->p_header, stream->i_header );
827 answer->i_body_offset = stream->i_buffer_last_pos;
828 vlc_mutex_unlock( &stream->lock );
832 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
833 answer->i_body_offset = 0;
836 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
838 vlc_bool_t b_xplaystream = VLC_FALSE;
841 httpd_MsgAdd( answer, "Content-type", "%s",
842 "application/octet-stream" );
843 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
844 httpd_MsgAdd( answer, "Pragma", "no-cache" );
845 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
846 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
848 /* Check if there is a xPlayStrm=1 */
849 for( i = 0; i < query->i_name; i++ )
851 if( !strcasecmp( query->name[i], "Pragma" ) &&
852 strstr( query->value[i], "xPlayStrm=1" ) )
854 b_xplaystream = VLC_TRUE;
860 answer->i_body_offset = 0;
865 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
867 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
872 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
873 const char *psz_url, const char *psz_mime,
874 const char *psz_user, const char *psz_password,
875 const vlc_acl_t *p_acl )
877 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
879 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
880 psz_password, p_acl )
886 vlc_mutex_init( host, &stream->lock );
887 if( psz_mime && *psz_mime )
889 stream->psz_mime = strdup( psz_mime );
893 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
895 stream->i_header = 0;
896 stream->p_header = NULL;
897 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
898 stream->p_buffer = malloc( stream->i_buffer_size );
899 /* We set to 1 to make life simpler
900 * (this way i_body_offset can never be 0) */
901 stream->i_buffer_pos = 1;
902 stream->i_buffer_last_pos = 1;
904 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
905 (httpd_callback_sys_t*)stream );
906 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
907 (httpd_callback_sys_t*)stream );
908 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
909 (httpd_callback_sys_t*)stream );
914 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
916 vlc_mutex_lock( &stream->lock );
917 if( stream->p_header )
919 free( stream->p_header );
920 stream->p_header = NULL;
922 stream->i_header = i_data;
925 stream->p_header = malloc( i_data );
926 memcpy( stream->p_header, p_data, i_data );
928 vlc_mutex_unlock( &stream->lock );
933 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
938 if( i_data < 0 || p_data == NULL )
942 vlc_mutex_lock( &stream->lock );
944 /* save this pointer (to be used by new connection) */
945 stream->i_buffer_last_pos = stream->i_buffer_pos;
947 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
953 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
955 /* Ok, we can't go past the end of our buffer */
956 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
958 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
963 stream->i_buffer_pos += i_data;
965 vlc_mutex_unlock( &stream->lock );
969 void httpd_StreamDelete( httpd_stream_t *stream )
971 httpd_UrlDelete( stream->url );
972 vlc_mutex_destroy( &stream->lock );
973 if( stream->psz_mime ) free( stream->psz_mime );
974 if( stream->p_header ) free( stream->p_header );
975 if( stream->p_buffer ) free( stream->p_buffer );
979 /*****************************************************************************
981 *****************************************************************************/
982 static void httpd_HostThread( httpd_host_t * );
984 /* create a new host */
985 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
988 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
992 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
994 const char *psz_cert, const char *psz_key,
995 const char *psz_ca, const char *psz_crl )
1001 vlc_value_t lockval;
1004 if( psz_hostname == NULL )
1007 psz_host = strdup( psz_hostname );
1008 if( psz_host == NULL )
1010 msg_Err( p_this, "memory error" );
1014 /* to be sure to avoid multiple creation */
1015 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
1016 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
1017 vlc_mutex_lock( lockval.p_address );
1019 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1021 msg_Info( p_this, "creating httpd" );
1022 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
1024 vlc_mutex_unlock( lockval.p_address );
1032 vlc_object_yield( httpd );
1033 vlc_object_attach( httpd, p_this->p_vlc );
1036 /* verify if it already exist */
1037 for( i = httpd->i_host - 1; i >= 0; i-- )
1039 host = httpd->host[i];
1041 /* cannot mix TLS and non-TLS hosts */
1042 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1043 || ( host->i_port != i_port )
1044 || strcmp( host->psz_hostname, psz_hostname ) )
1050 vlc_mutex_unlock( lockval.p_address );
1056 /* determine TLS configuration */
1057 if ( psz_cert != NULL )
1059 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1060 if ( p_tls == NULL )
1062 msg_Err( p_this, "TLS initialization error" );
1066 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1068 msg_Err( p_this, "TLS CA error" );
1072 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1074 msg_Err( p_this, "TLS CRL error" );
1081 /* create the new host */
1082 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1083 host->httpd = httpd;
1084 vlc_mutex_init( httpd, &host->lock );
1087 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1088 if( host->fd == NULL )
1090 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1094 host->i_port = i_port;
1095 host->psz_hostname = psz_host;
1100 host->client = NULL;
1102 host->p_tls = p_tls;
1104 /* create the thread */
1105 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1106 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1108 msg_Err( p_this, "cannot spawn http host thread" );
1112 /* now add it to httpd */
1113 TAB_APPEND( httpd->i_host, httpd->host, host );
1114 vlc_mutex_unlock( lockval.p_address );
1120 if( httpd->i_host <= 0 )
1122 vlc_object_release( httpd );
1123 vlc_object_detach( httpd );
1124 vlc_object_destroy( httpd );
1126 vlc_mutex_unlock( lockval.p_address );
1130 net_ListenClose( host->fd );
1131 vlc_mutex_destroy( &host->lock );
1132 vlc_object_destroy( host );
1136 tls_ServerDelete( p_tls );
1142 void httpd_HostDelete( httpd_host_t *host )
1144 httpd_t *httpd = host->httpd;
1145 vlc_value_t lockval;
1148 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1149 vlc_mutex_lock( lockval.p_address );
1152 if( host->i_ref > 0 )
1155 vlc_mutex_unlock( lockval.p_address );
1156 msg_Dbg( host, "httpd_HostDelete: host still used" );
1159 TAB_REMOVE( httpd->i_host, httpd->host, host );
1162 vlc_thread_join( host );
1164 msg_Dbg( host, "HTTP host removed" );
1166 for( i = 0; i < host->i_url; i++ )
1168 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1170 for( i = 0; i < host->i_client; i++ )
1172 httpd_client_t *cl = host->client[i];
1173 msg_Warn( host, "client still connected" );
1174 httpd_ClientClean( cl );
1175 TAB_REMOVE( host->i_client, host->client, cl );
1181 if( host->p_tls != NULL)
1182 tls_ServerDelete( host->p_tls );
1184 net_ListenClose( host->fd );
1185 free( host->psz_hostname );
1187 vlc_mutex_destroy( &host->lock );
1188 vlc_object_destroy( host );
1190 vlc_object_release( httpd );
1191 if( httpd->i_host <= 0 )
1193 msg_Dbg( httpd, "no host left, stopping httpd" );
1194 vlc_object_detach( httpd );
1195 vlc_object_destroy( httpd );
1197 vlc_mutex_unlock( lockval.p_address );
1200 /* register a new url */
1201 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1202 const char *psz_user, const char *psz_password,
1203 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1208 vlc_mutex_lock( &host->lock );
1211 for( i = 0; i < host->i_url; i++ )
1213 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1215 msg_Warn( host->httpd,
1216 "cannot add '%s' (url already defined)", psz_url );
1217 vlc_mutex_unlock( &host->lock );
1223 url = malloc( sizeof( httpd_url_t ) );
1226 vlc_mutex_init( host->httpd, &url->lock );
1227 url->psz_url = strdup( psz_url );
1228 url->psz_user = strdup( psz_user ? psz_user : "" );
1229 url->psz_password = strdup( psz_password ? psz_password : "" );
1230 url->p_acl = ACL_Duplicate( host, p_acl );
1231 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1233 url->catch[i].cb = NULL;
1234 url->catch[i].p_sys = NULL;
1237 TAB_APPEND( host->i_url, host->url, url );
1238 vlc_mutex_unlock( &host->lock );
1243 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1244 const char *psz_user, const char *psz_password,
1245 const vlc_acl_t *p_acl )
1247 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1248 psz_password, p_acl, VLC_FALSE );
1251 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1252 const char *psz_user, const char *psz_password,
1253 const vlc_acl_t *p_acl )
1255 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1256 psz_password, p_acl, VLC_TRUE );
1259 /* register callback on a url */
1260 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1261 httpd_callback_sys_t *p_sys )
1263 vlc_mutex_lock( &url->lock );
1264 url->catch[i_msg].cb = cb;
1265 url->catch[i_msg].p_sys= p_sys;
1266 vlc_mutex_unlock( &url->lock );
1272 void httpd_UrlDelete( httpd_url_t *url )
1274 httpd_host_t *host = url->host;
1277 vlc_mutex_lock( &host->lock );
1278 TAB_REMOVE( host->i_url, host->url, url );
1280 vlc_mutex_destroy( &url->lock );
1281 free( url->psz_url );
1282 free( url->psz_user );
1283 free( url->psz_password );
1284 ACL_Destroy( url->p_acl );
1286 for( i = 0; i < host->i_client; i++ )
1288 httpd_client_t *client = host->client[i];
1290 if( client->url == url )
1292 /* TODO complete it */
1293 msg_Warn( host, "force closing connections" );
1294 httpd_ClientClean( client );
1295 TAB_REMOVE( host->i_client, host->client, client );
1301 vlc_mutex_unlock( &host->lock );
1304 void httpd_MsgInit( httpd_message_t *msg )
1307 msg->i_type = HTTPD_MSG_NONE;
1308 msg->i_proto = HTTPD_PROTO_NONE;
1309 msg->i_version = -1;
1312 msg->psz_status = NULL;
1314 msg->psz_url = NULL;
1315 msg->psz_args = NULL;
1317 msg->i_channel = -1;
1324 msg->i_body_offset = 0;
1329 void httpd_MsgClean( httpd_message_t *msg )
1333 if( msg->psz_status )
1335 free( msg->psz_status );
1339 free( msg->psz_url );
1343 free( msg->psz_args );
1345 for( i = 0; i < msg->i_name; i++ )
1347 free( msg->name[i] );
1348 free( msg->value[i] );
1360 free( msg->p_body );
1362 httpd_MsgInit( msg );
1365 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1369 for( i = 0; i < msg->i_name; i++ )
1371 if( !strcasecmp( msg->name[i], name ))
1373 return msg->value[i];
1379 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1384 va_start( args, psz_value );
1385 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1386 vasprintf( &value, psz_value, args );
1389 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1390 value = calloc( i_size, sizeof( char ) );
1391 vsnprintf( value, i_size, psz_value, args );
1392 value[i_size - 1] = 0;
1397 name = strdup( name );
1399 TAB_APPEND( msg->i_name, msg->name, name );
1400 TAB_APPEND( msg->i_value, msg->value, value );
1403 static void httpd_ClientInit( httpd_client_t *cl )
1405 cl->i_state = HTTPD_CLIENT_RECEIVING;
1406 cl->i_activity_date = mdate();
1407 cl->i_activity_timeout = I64C(10000000);
1408 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1410 cl->p_buffer = malloc( cl->i_buffer_size );
1411 cl->i_mode = HTTPD_CLIENT_FILE;
1412 cl->b_read_waiting = VLC_FALSE;
1414 httpd_MsgInit( &cl->query );
1415 httpd_MsgInit( &cl->answer );
1418 void httpd_ClientModeStream( httpd_client_t *cl )
1420 cl->i_mode = HTTPD_CLIENT_STREAM;
1423 void httpd_ClientModeBidir( httpd_client_t *cl )
1425 cl->i_mode = HTTPD_CLIENT_BIDIR;
1428 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1430 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1433 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1435 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1438 static void httpd_ClientClean( httpd_client_t *cl )
1442 if( cl->p_tls != NULL )
1443 tls_ServerSessionClose( cl->p_tls );
1444 net_Close( cl->fd );
1448 httpd_MsgClean( &cl->answer );
1449 httpd_MsgClean( &cl->query );
1453 free( cl->p_buffer );
1454 cl->p_buffer = NULL;
1458 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1460 tls_session_t *p_tls )
1462 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1464 if( !cl ) return NULL;
1468 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1469 cl->i_sock_size = i_sock_size;
1473 httpd_ClientInit( cl );
1478 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1480 tls_session_t *p_tls;
1484 return tls_Recv( p_tls, p, i_len );
1486 return recv( cl->fd, p, i_len, 0 );
1489 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1491 tls_session_t *p_tls;
1495 return tls_Send( p_tls, p, i_len );
1497 return send( cl->fd, p, i_len, 0 );
1500 static void httpd_ClientRecv( httpd_client_t *cl )
1504 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1506 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1507 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1511 cl->i_buffer += i_len;
1514 if( cl->i_buffer >= 4 )
1516 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1518 if( cl->p_buffer[0] == '$' )
1520 /* RTSP (rtp over rtsp) */
1521 cl->query.i_proto = HTTPD_PROTO_RTSP;
1522 cl->query.i_type = HTTPD_MSG_CHANNEL;
1523 cl->query.i_channel = cl->p_buffer[1];
1524 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1525 cl->query.p_body = malloc( cl->query.i_body );
1529 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1531 cl->query.i_proto = HTTPD_PROTO_HTTP;
1532 cl->query.i_type = HTTPD_MSG_ANSWER;
1534 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1536 cl->query.i_proto = HTTPD_PROTO_RTSP;
1537 cl->query.i_type = HTTPD_MSG_ANSWER;
1539 else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
1540 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1541 !memcmp( cl->p_buffer, "POST", 4 ) )
1543 cl->query.i_proto = HTTPD_PROTO_HTTP;
1544 cl->query.i_type = HTTPD_MSG_NONE;
1548 cl->query.i_proto = HTTPD_PROTO_RTSP;
1549 cl->query.i_type = HTTPD_MSG_NONE;
1553 else if( cl->query.i_body > 0 )
1555 /* we are reading the body of a request or a channel */
1556 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1557 cl->query.i_body - cl->i_buffer );
1560 cl->i_buffer += i_len;
1562 if( cl->i_buffer >= cl->query.i_body )
1564 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1569 /* we are reading a header -> char by char */
1572 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1579 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1581 cl->i_buffer_size += 1024;
1582 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1584 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1585 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1589 /* we have finished the header so parse it and set i_body */
1590 cl->p_buffer[cl->i_buffer] = '\0';
1592 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1595 * assume strlen( "HTTP/1.x" ) = 8
1597 cl->query.i_status =
1598 strtol( (char *)&cl->p_buffer[8],
1604 cl->query.psz_status = strdup( p );
1616 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1617 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1618 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1620 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1621 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1622 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1623 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1624 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1625 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1627 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1632 cl->query.i_type = HTTPD_MSG_NONE;
1634 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1636 for( i = 0; msg_type[i].name != NULL; i++ )
1638 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1639 strlen( msg_type[i].name ) ) )
1641 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1642 cl->query.i_type = msg_type[i].i_type;
1643 if( cl->query.i_proto != msg_type[i].i_proto )
1646 cl->query.i_proto = HTTPD_PROTO_NONE;
1647 cl->query.i_type = HTTPD_MSG_NONE;
1654 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1656 cl->query.i_proto = HTTPD_PROTO_HTTP;
1658 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1660 cl->query.i_proto = HTTPD_PROTO_RTSP;
1672 p2 = strchr( p, ' ' );
1677 if( !strncasecmp( p, "rtsp:", 5 ) )
1679 /* for rtsp url, you have rtsp://localhost:port/path */
1681 while( *p == '/' ) p++;
1682 while( *p && *p != '/' ) p++;
1684 cl->query.psz_url = strdup( p );
1685 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1688 cl->query.psz_args = (uint8_t *)strdup( p3 );
1696 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1698 cl->query.i_proto = HTTPD_PROTO_HTTP;
1699 cl->query.i_version = atoi( p2+7 );
1701 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1703 cl->query.i_proto = HTTPD_PROTO_RTSP;
1704 cl->query.i_version = atoi( p2+7 );
1712 p = strchr( p, '\n' );
1716 while( *p == '\n' || *p == '\r' )
1720 while( p && *p != '\0' )
1723 char *eol = p = strchr( p, '\n' );
1726 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1731 if( ( colon = strchr( line, ':' ) ) )
1737 while( *colon == ' ' )
1741 name = strdup( line );
1742 value = strdup( colon );
1744 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1745 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1747 if( !strcasecmp( name, "Content-Length" ) )
1749 cl->query.i_body = atol( value );
1756 while( *p == '\n' || *p == '\r' )
1763 if( cl->query.i_body > 0 )
1765 /* TODO Mhh, handle the case client will only send a request and close the connection
1766 * to mark and of body (probably only RTSP) */
1767 cl->query.p_body = malloc( cl->query.i_body );
1772 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1778 /* check if the client is to be set to dead */
1779 #if defined( WIN32 ) || defined( UNDER_CE )
1780 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1782 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1785 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1787 /* connection closed -> end of data */
1788 if( cl->query.i_body > 0 )
1790 cl->query.i_body = cl->i_buffer;
1792 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1796 cl->i_state = HTTPD_CLIENT_DEAD;
1799 cl->i_activity_date = mdate();
1801 /* XXX: for QT I have to disable timeout. Try to find why */
1802 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1803 cl->i_activity_timeout = 0;
1805 #if 0 /* Debugging only */
1806 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1810 fprintf( stderr, "received new request\n" );
1811 fprintf( stderr, " - proto=%s\n",
1812 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1813 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1814 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1815 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1817 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1818 cl->query.psz_status );
1820 else if( cl->query.i_type != HTTPD_MSG_NONE )
1822 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1824 for( i = 0; i < cl->query.i_name; i++ )
1826 fprintf( stderr, " - option name='%s' value='%s'\n",
1827 cl->query.name[i], cl->query.value[i] );
1833 static void httpd_ClientSend( httpd_client_t *cl )
1838 if( cl->i_buffer < 0 )
1840 /* We need to create the header */
1844 i_size = strlen( "HTTP/1.") + 10 + 10 +
1845 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1846 for( i = 0; i < cl->answer.i_name; i++ )
1848 i_size += strlen( cl->answer.name[i] ) + 2 +
1849 strlen( cl->answer.value[i] ) + 2;
1852 if( cl->i_buffer_size < i_size )
1854 cl->i_buffer_size = i_size;
1855 free( cl->p_buffer );
1856 cl->p_buffer = malloc( i_size );
1858 p = (char *)cl->p_buffer;
1860 p += sprintf( p, "%s/1.%d %d %s\r\n",
1861 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1862 cl->answer.i_version,
1863 cl->answer.i_status, cl->answer.psz_status );
1864 for( i = 0; i < cl->answer.i_name; i++ )
1866 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1867 cl->answer.value[i] );
1869 p += sprintf( p, "\r\n" );
1872 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1874 /*fprintf( stderr, "sending answer\n" );
1875 fprintf( stderr, "%s", cl->p_buffer );*/
1878 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1879 cl->i_buffer_size - cl->i_buffer );
1882 cl->i_activity_date = mdate();
1883 cl->i_buffer += i_len;
1885 if( cl->i_buffer >= cl->i_buffer_size )
1887 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1888 !cl->b_read_waiting )
1890 /* catch more body data */
1891 int i_msg = cl->query.i_type;
1892 int64_t i_offset = cl->answer.i_body_offset;
1894 httpd_MsgClean( &cl->answer );
1895 cl->answer.i_body_offset = i_offset;
1897 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1898 &cl->answer, &cl->query );
1901 if( cl->answer.i_body > 0 )
1903 /* send the body data */
1904 free( cl->p_buffer );
1905 cl->p_buffer = cl->answer.p_body;
1906 cl->i_buffer_size = cl->answer.i_body;
1909 cl->answer.i_body = 0;
1910 cl->answer.p_body = NULL;
1915 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1921 #if defined( WIN32 ) || defined( UNDER_CE )
1922 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1924 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1928 cl->i_state = HTTPD_CLIENT_DEAD;
1933 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1935 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1938 cl->i_state = HTTPD_CLIENT_RECEIVING;
1942 cl->i_state = HTTPD_CLIENT_DEAD;
1947 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1951 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1953 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1956 cl->i_state = HTTPD_CLIENT_RECEIVING;
1960 cl->i_state = HTTPD_CLIENT_DEAD;
1965 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1970 static void httpd_HostThread( httpd_host_t *host )
1972 tls_session_t *p_tls = NULL;
1974 stats_Create( host, "client_connections", STATS_CLIENT_CONNECTIONS,
1975 VLC_VAR_INTEGER, STATS_COUNTER );
1976 stats_Create( host, "active_connections", STATS_ACTIVE_CONNECTIONS,
1977 VLC_VAR_INTEGER, STATS_COUNTER );
1979 while( !host->b_die )
1981 struct timeval timeout;
1984 /* FIXME: (too) many int variables */
1986 int i_handle_max = 0;
1989 int b_low_delay = 0;
1991 if( host->i_url <= 0 )
1998 /* built a set of handle to select */
1999 FD_ZERO( &fds_read );
2000 FD_ZERO( &fds_write );
2004 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2006 FD_SET( fd, &fds_read );
2007 if( fd > i_handle_max )
2011 /* prepare a new TLS session */
2012 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2013 p_tls = tls_ServerSessionPrepare( host->p_tls );
2015 /* add all socket that should be read/write and close dead connection */
2016 vlc_mutex_lock( &host->lock );
2017 for( i_client = 0; i_client < host->i_client; i_client++ )
2019 httpd_client_t *cl = host->client[i_client];
2021 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2022 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2023 ( cl->i_activity_timeout > 0 &&
2024 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2026 httpd_ClientClean( cl );
2027 stats_UpdateInteger( host, STATS_ACTIVE_CONNECTIONS, -1, NULL );
2028 TAB_REMOVE( host->i_client, host->client, cl );
2033 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2034 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2036 FD_SET( cl->fd, &fds_read );
2037 i_handle_max = __MAX( i_handle_max, cl->fd );
2039 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2040 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2042 FD_SET( cl->fd, &fds_write );
2043 i_handle_max = __MAX( i_handle_max, cl->fd );
2045 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2047 httpd_message_t *answer = &cl->answer;
2048 httpd_message_t *query = &cl->query;
2049 int i_msg = query->i_type;
2051 httpd_MsgInit( answer );
2053 /* Handle what we received */
2054 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2055 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2057 /* we can only receive request from client when not
2060 cl->i_state = HTTPD_CLIENT_DEAD;
2062 else if( i_msg == HTTPD_MSG_ANSWER )
2064 /* We are in BIDIR mode, trigger the callback and then
2065 * check for new data */
2066 if( cl->url && cl->url->catch[i_msg].cb )
2068 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2071 cl->i_state = HTTPD_CLIENT_WAITING;
2073 else if( i_msg == HTTPD_MSG_CHANNEL )
2075 /* We are in BIDIR mode, trigger the callback and then
2076 * check for new data */
2077 if( cl->url && cl->url->catch[i_msg].cb )
2079 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2082 cl->i_state = HTTPD_CLIENT_WAITING;
2084 else if( i_msg == HTTPD_MSG_OPTIONS )
2089 answer->i_proto = query->i_proto ;
2090 answer->i_type = HTTPD_MSG_ANSWER;
2091 answer->i_version= 0;
2092 answer->i_status = 200;
2093 answer->psz_status = strdup( "Ok" );
2096 answer->p_body = NULL;
2098 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2099 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2100 httpd_MsgAdd( answer, "Server", "VLC Server" );
2101 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2102 "TEARDOWN, PLAY, PAUSE" );
2103 httpd_MsgAdd( answer, "Content-Length", "%d",
2106 cl->i_buffer = -1; /* Force the creation of the answer in
2107 * httpd_ClientSend */
2108 cl->i_state = HTTPD_CLIENT_SENDING;
2110 else if( i_msg == HTTPD_MSG_NONE )
2112 if( query->i_proto == HTTPD_PROTO_NONE )
2115 cl->i_state = HTTPD_CLIENT_DEAD;
2122 answer->i_proto = query->i_proto ;
2123 answer->i_type = HTTPD_MSG_ANSWER;
2124 answer->i_version= 0;
2125 answer->i_status = 501;
2126 answer->psz_status = strdup( "Unimplemented" );
2128 p = answer->p_body = malloc( 1000 );
2130 p += sprintf( (char *)p,
2131 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2132 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2133 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2136 "<title>Error 501</title>\n"
2139 "<h1>501 Unimplemented</h1>\n"
2141 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2145 answer->i_body = p - answer->p_body;
2146 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2148 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2149 cl->i_state = HTTPD_CLIENT_SENDING;
2154 vlc_bool_t b_auth_failed = VLC_FALSE;
2155 vlc_bool_t b_hosts_failed = VLC_FALSE;
2158 /* Search the url and trigger callbacks */
2159 for( i = 0; i < host->i_url; i++ )
2161 httpd_url_t *url = host->url[i];
2163 if( !strcmp( url->psz_url, query->psz_url ) )
2165 if( url->catch[i_msg].cb )
2167 if( answer && ( url->p_acl != NULL ) )
2169 char ip[NI_MAXNUMERICHOST];
2171 if( httpd_ClientIP( cl, ip ) != NULL )
2173 if( ACL_Check( url->p_acl, ip ) )
2175 b_hosts_failed = VLC_TRUE;
2180 b_hosts_failed = VLC_TRUE;
2183 if( answer && ( *url->psz_user || *url->psz_password ) )
2185 /* create the headers */
2186 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2190 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2191 auth = malloc( strlen(b64) + 1 );
2193 if( !strncasecmp( b64, "BASIC", 5 ) )
2196 while( *b64 == ' ' )
2200 b64_decode( auth, b64 );
2207 if( strcmp( id, auth ) )
2209 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2210 /* We fail for all url */
2211 b_auth_failed = VLC_TRUE;
2221 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2223 if( answer->i_proto == HTTPD_PROTO_NONE )
2225 /* Raw answer from a CGI */
2226 cl->i_buffer = cl->i_buffer_size;
2231 /* only one url can answer */
2233 if( cl->url == NULL )
2246 answer->i_proto = query->i_proto;
2247 answer->i_type = HTTPD_MSG_ANSWER;
2248 answer->i_version= 0;
2249 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2251 if( b_hosts_failed )
2253 answer->i_status = 403;
2254 answer->psz_status = strdup( "Forbidden" );
2256 /* FIXME: lots of code duplication */
2257 p += sprintf( (char *)p,
2258 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2259 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2260 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2263 "<title>Error 403</title>\n"
2266 "<h1>403 Forbidden (%s)</h1>\n"
2268 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2270 "</html>\n", query->psz_url );
2272 else if( b_auth_failed )
2274 answer->i_status = 401;
2275 answer->psz_status = strdup( "Authorization Required" );
2277 p += sprintf( (char *)p,
2278 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2279 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2280 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2283 "<title>Error 401</title>\n"
2286 "<h1>401 Authorization Required (%s)</h1>\n"
2288 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2290 "</html>\n", query->psz_url );
2294 /* no url registered */
2295 answer->i_status = 404;
2296 answer->psz_status = strdup( "Not found" );
2298 p += sprintf( (char *)p,
2299 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2300 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2301 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2304 "<title>Error 404</title>\n"
2307 "<h1>404 Resource not found(%s)</h1>\n"
2309 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2311 "</html>\n", query->psz_url );
2314 answer->i_body = p - answer->p_body;
2315 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2316 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2319 cl->i_state = HTTPD_CLIENT_SENDING;
2322 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2324 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2327 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2328 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2329 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2330 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2331 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2332 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2334 httpd_MsgClean( &cl->query );
2335 httpd_MsgInit( &cl->query );
2338 cl->i_buffer_size = 1000;
2339 free( cl->p_buffer );
2340 cl->p_buffer = malloc( cl->i_buffer_size );
2341 cl->i_state = HTTPD_CLIENT_RECEIVING;
2345 cl->i_state = HTTPD_CLIENT_DEAD;
2347 httpd_MsgClean( &cl->answer );
2349 else if( cl->b_read_waiting )
2351 /* we have a message waiting for us to read it */
2352 httpd_MsgClean( &cl->answer );
2353 httpd_MsgClean( &cl->query );
2356 cl->i_buffer_size = 1000;
2357 free( cl->p_buffer );
2358 cl->p_buffer = malloc( cl->i_buffer_size );
2359 cl->i_state = HTTPD_CLIENT_RECEIVING;
2360 cl->b_read_waiting = VLC_FALSE;
2364 int64_t i_offset = cl->answer.i_body_offset;
2365 httpd_MsgClean( &cl->answer );
2367 cl->answer.i_body_offset = i_offset;
2368 free( cl->p_buffer );
2369 cl->p_buffer = NULL;
2371 cl->i_buffer_size = 0;
2373 cl->i_state = HTTPD_CLIENT_WAITING;
2376 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2378 int64_t i_offset = cl->answer.i_body_offset;
2379 int i_msg = cl->query.i_type;
2381 httpd_MsgInit( &cl->answer );
2382 cl->answer.i_body_offset = i_offset;
2384 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2385 &cl->answer, &cl->query );
2386 if( cl->answer.i_type != HTTPD_MSG_NONE )
2388 /* we have new data, so reenter send mode */
2390 cl->p_buffer = cl->answer.p_body;
2391 cl->i_buffer_size = cl->answer.i_body;
2392 cl->answer.p_body = NULL;
2393 cl->answer.i_body = 0;
2394 cl->i_state = HTTPD_CLIENT_SENDING;
2398 /* we shouldn't wait too long */
2399 b_low_delay = VLC_TRUE;
2403 /* Special for BIDIR mode we also check reading */
2404 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2405 cl->i_state == HTTPD_CLIENT_SENDING )
2407 FD_SET( cl->fd, &fds_read );
2408 i_handle_max = __MAX( i_handle_max, cl->fd );
2411 vlc_mutex_unlock( &host->lock );
2413 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2415 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2417 i_ret = select( i_handle_max + 1,
2418 &fds_read, &fds_write, NULL, &timeout );
2420 if( (i_ret == -1) && (errno != EINTR) )
2422 #if defined(WIN32) || defined(UNDER_CE)
2423 msg_Warn( host, "cannot select sockets (%d)", WSAGetLastError( ) );
2425 msg_Warn( host, "cannot select sockets : %s", strerror( errno ) );
2430 else if( i_ret <= 0 )
2435 /* accept new connections */
2436 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2438 if( FD_ISSET( fd, &fds_read ) )
2440 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2441 struct sockaddr_storage sock;
2443 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2444 msg_Info( host, "Accepting" );
2449 /* set this new socket non-block */
2450 #if defined( WIN32 ) || defined( UNDER_CE )
2452 unsigned long i_dummy = 1;
2453 ioctlsocket( fd, FIONBIO, &i_dummy );
2456 fcntl( fd, F_SETFL, O_NONBLOCK );
2461 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2464 msg_Err( host, "Rejecting TLS connection" );
2470 case 1: /* missing input - most likely */
2471 i_state = HTTPD_CLIENT_TLS_HS_IN;
2474 case 2: /* missing output */
2475 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2483 stats_UpdateInteger( host, STATS_CLIENT_CONNECTIONS,
2485 stats_UpdateInteger( host, STATS_ACTIVE_CONNECTIONS, 1,
2487 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2489 vlc_mutex_lock( &host->lock );
2490 TAB_APPEND( host->i_client, host->client, cl );
2491 vlc_mutex_unlock( &host->lock );
2494 cl->i_state = i_state; // override state for TLS
2500 /* now try all others socket */
2501 vlc_mutex_lock( &host->lock );
2502 for( i_client = 0; i_client < host->i_client; i_client++ )
2504 httpd_client_t *cl = host->client[i_client];
2505 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2507 httpd_ClientRecv( cl );
2509 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2511 httpd_ClientSend( cl );
2513 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2515 httpd_ClientTlsHsIn( cl );
2517 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2519 httpd_ClientTlsHsOut( cl );
2522 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2523 cl->i_state == HTTPD_CLIENT_SENDING &&
2524 FD_ISSET( cl->fd, &fds_read ) )
2526 cl->b_read_waiting = VLC_TRUE;
2529 vlc_mutex_unlock( &host->lock );
2533 tls_ServerSessionClose( p_tls );
2536 #else /* ENABLE_HTTPD */
2538 /* We just define an empty wrapper */
2539 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2542 msg_Err( a, "HTTP daemon support is disabled" );
2546 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2548 msg_Err( a, "HTTP daemon support is disabled" );
2552 void httpd_HostDelete( httpd_host_t *a )
2556 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2557 char *psz_user, char *psz_password,
2558 const vlc_acl_t *p_acl )
2563 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2564 char *psz_user, char *psz_password,
2565 const vlc_acl_t *p_acl )
2570 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2571 httpd_callback_sys_t *d )
2576 void httpd_UrlDelete( httpd_url_t *a )
2580 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2585 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2590 void httpd_ClientModeStream( httpd_client_t *a )
2594 void httpd_ClientModeBidir( httpd_client_t *a )
2598 void httpd_FileDelete( httpd_file_t *a )
2602 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2603 char *e, httpd_file_callback_t f,
2604 httpd_file_sys_t *g )
2609 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2610 const char *psz_user,
2611 const char *psz_password,
2612 const vlc_acl_t *p_acl,
2613 httpd_handler_callback_t pf_fill,
2614 httpd_handler_sys_t *p_sys )
2619 void httpd_HandlerDelete( httpd_handler_t *handler )
2623 void httpd_RedirectDelete( httpd_redirect_t *a )
2627 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2633 void httpd_StreamDelete( httpd_stream_t *a )
2637 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2642 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2647 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2653 void httpd_MsgInit ( httpd_message_t *a )
2657 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... )
2661 char *httpd_MsgGet ( httpd_message_t *a, char *b )
2666 void httpd_MsgClean( httpd_message_t *a )
2670 #endif /* ENABLE_HTTPD */