1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 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 *****************************************************************************/
30 #include "vlc_httpd.h"
46 #if defined( UNDER_CE )
48 #elif defined( WIN32 )
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # include <netdb.h> /* hostent ... */
53 # include <sys/socket.h>
54 /* FIXME: should not be needed */
55 # include <netinet/in.h>
56 # ifdef HAVE_ARPA_INET_H
57 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
62 /* We need HUGE buffer otherwise TCP throughput is very limited */
63 #define HTTPD_CL_BUFSIZE 1000000
65 #define HTTPD_CL_BUFSIZE 10000
68 static void httpd_ClientClean( httpd_client_t *cl );
70 /* each host run in his own thread */
80 /* address/port and socket for listening at connections */
87 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
88 * This will slow down the url research but make my live easier
89 * All url will have their cb trigger, but only the first one can answer
95 httpd_client_t **client;
115 httpd_callback_sys_t *p_sys;
116 } catch[HTTPD_MSG_MAX];
122 HTTPD_CLIENT_RECEIVING,
123 HTTPD_CLIENT_RECEIVE_DONE,
125 HTTPD_CLIENT_SENDING,
126 HTTPD_CLIENT_SEND_DONE,
128 HTTPD_CLIENT_WAITING,
132 HTTPD_CLIENT_TLS_HS_IN,
133 HTTPD_CLIENT_TLS_HS_OUT
139 HTTPD_CLIENT_FILE, /* default */
140 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
141 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
144 struct httpd_client_t
150 struct sockaddr_storage sock;
156 int b_read_waiting; /* stop as soon as possible sending */
158 mtime_t i_activity_date;
159 mtime_t i_activity_timeout;
161 /* buffer for reading header */
167 httpd_message_t query; /* client -> httpd */
168 httpd_message_t answer; /* httpd -> client */
171 tls_session_t *p_tls;
175 /*****************************************************************************
177 *****************************************************************************/
178 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
179 static void b64_decode( char *dest, char *src )
184 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
185 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
186 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
187 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
188 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
189 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
190 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
191 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
192 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
193 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
194 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
195 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
196 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
197 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
198 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
199 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
202 for( i_level = 0; *src != '\0'; src++ )
206 c = b64[(unsigned int)*src];
218 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
222 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
226 *dest++ = ( ( last &0x03 ) << 6 ) | c;
238 const char *psz_mime;
241 { ".htm", "text/html" },
242 { ".html", "text/html" },
243 { ".txt", "text/plain" },
244 { ".xml", "text/xml" },
245 { ".dtd", "text/dtd" },
247 { ".css", "text/css" },
250 { ".gif", "image/gif" },
251 { ".jpe", "image/jpeg" },
252 { ".jpg", "image/jpeg" },
253 { ".jpeg", "image/jpeg" },
254 { ".png", "image/png" },
255 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
258 { ".avi", "video/avi" },
259 { ".asf", "video/x-ms-asf" },
260 { ".m1a", "audio/mpeg" },
261 { ".m2a", "audio/mpeg" },
262 { ".m1v", "video/mpeg" },
263 { ".m2v", "video/mpeg" },
264 { ".mp2", "audio/mpeg" },
265 { ".mp3", "audio/mpeg" },
266 { ".mpa", "audio/mpeg" },
267 { ".mpg", "video/mpeg" },
268 { ".mpeg", "video/mpeg" },
269 { ".mpe", "video/mpeg" },
270 { ".mov", "video/quicktime" },
271 { ".moov", "video/quicktime" },
272 { ".ogg", "application/ogg" },
273 { ".ogm", "application/ogg" },
274 { ".wav", "audio/wav" },
275 { ".wma", "audio/x-ms-wma" },
276 { ".wmv", "video/x-ms-wmv" },
283 static const char *httpd_MimeFromUrl( const char *psz_url )
288 psz_ext = strrchr( psz_url, '.' );
293 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
295 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
297 return http_mime[i].psz_mime;
301 return "application/octet-stream";
304 /*****************************************************************************
305 * High Level Functions: httpd_file_t
306 *****************************************************************************/
314 httpd_file_callback_t pf_fill;
315 httpd_file_sys_t *p_sys;
319 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
321 httpd_file_t *file = (httpd_file_t*)p_sys;
322 uint8_t *psz_args = query->psz_args;
323 uint8_t **pp_body, *p_body;
324 int *pi_body, i_body;
326 if( answer == NULL || query == NULL )
330 answer->i_proto = HTTPD_PROTO_HTTP;
331 answer->i_version= query->i_version;
332 answer->i_type = HTTPD_MSG_ANSWER;
334 answer->i_status = 200;
335 answer->psz_status = strdup( "OK" );
337 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
338 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
340 if( query->i_type != HTTPD_MSG_HEAD )
342 pp_body = &answer->p_body;
343 pi_body = &answer->i_body;
347 /* The file still needs to be executed. */
354 if( query->i_type == HTTPD_MSG_POST )
356 /* msg_Warn not supported */
359 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
361 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
366 /* We respect client request */
367 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
369 httpd_MsgAdd( answer, "Connection",
370 httpd_MsgGet( &cl->query, "Connection" ) );
373 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
378 httpd_file_t *httpd_FileNew( httpd_host_t *host,
379 const char *psz_url, const char *psz_mime,
380 const char *psz_user, const char *psz_password,
381 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
382 httpd_file_sys_t *p_sys )
384 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
386 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
387 psz_password, p_acl )
394 file->psz_url = strdup( psz_url );
395 if( psz_mime && *psz_mime )
397 file->psz_mime = strdup( psz_mime );
401 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
404 file->pf_fill = pf_fill;
407 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
408 (httpd_callback_sys_t*)file );
409 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
410 (httpd_callback_sys_t*)file );
411 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
412 (httpd_callback_sys_t*)file );
417 void httpd_FileDelete( httpd_file_t *file )
419 httpd_UrlDelete( file->url );
421 free( file->psz_url );
422 free( file->psz_mime );
427 /*****************************************************************************
428 * High Level Functions: httpd_handler_t (for CGIs)
429 *****************************************************************************/
430 struct httpd_handler_t
434 httpd_handler_callback_t pf_fill;
435 httpd_handler_sys_t *p_sys;
439 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
441 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
442 uint8_t *psz_args = query->psz_args;
443 char psz_remote_addr[100];
445 if( answer == NULL || query == NULL )
449 answer->i_proto = HTTPD_PROTO_NONE;
450 answer->i_type = HTTPD_MSG_ANSWER;
452 /* We do it ourselves, thanks */
453 answer->i_status = 0;
454 answer->psz_status = NULL;
456 switch( cl->sock.ss_family )
458 #ifdef HAVE_INET_PTON
460 inet_ntop( cl->sock.ss_family,
461 &((struct sockaddr_in *)&cl->sock)->sin_addr,
462 psz_remote_addr, sizeof(psz_remote_addr) );
465 inet_ntop( cl->sock.ss_family,
466 &((struct sockaddr_in6 *)&cl->sock)->sin6_addr,
467 psz_remote_addr, sizeof(psz_remote_addr) );
472 char *psz_tmp = inet_ntoa( ((struct sockaddr_in *)&cl->sock)->sin_addr );
473 strncpy( psz_remote_addr, psz_tmp, sizeof(psz_remote_addr) );
478 psz_remote_addr[0] = '\0';
481 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
482 query->i_type, query->p_body, query->i_body,
483 psz_remote_addr, NULL,
484 &answer->p_body, &answer->i_body );
486 if( query->i_type == HTTPD_MSG_HEAD )
488 char *p = (char *)answer->p_body;
489 while ( (p = strchr( p, '\r' )) != NULL )
491 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
492 && p[3] && p[3] == '\n' )
500 answer->i_body = strlen((char*)answer->p_body) + 1;
501 answer->p_body = realloc( answer->p_body, answer->i_body );
505 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
507 int i_status, i_headers;
508 char *psz_headers, *psz_new, *psz_status;
510 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
513 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
514 if( *psz_headers ) psz_headers++;
515 if( *psz_headers ) psz_headers++;
516 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
521 psz_headers = (char *)answer->p_body;
522 i_headers = answer->i_body;
530 psz_status = "Unauthorized";
533 if( (i_status < 0) || (i_status > 999) )
535 psz_status = "Undefined";
538 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
539 + strlen(psz_status) + i_headers - 1;
540 psz_new = (char *)malloc( answer->i_body + 1);
541 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
542 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
543 free( answer->p_body );
544 answer->p_body = (uint8_t *)psz_new;
550 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
551 const char *psz_user,
552 const char *psz_password,
553 const vlc_acl_t *p_acl,
554 httpd_handler_callback_t pf_fill,
555 httpd_handler_sys_t *p_sys )
557 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
559 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
560 psz_password, p_acl )
567 handler->pf_fill = pf_fill;
568 handler->p_sys = p_sys;
570 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
571 (httpd_callback_sys_t*)handler );
572 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
573 (httpd_callback_sys_t*)handler );
574 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
575 (httpd_callback_sys_t*)handler );
580 void httpd_HandlerDelete( httpd_handler_t *handler )
582 httpd_UrlDelete( handler->url );
586 /*****************************************************************************
587 * High Level Functions: httpd_redirect_t
588 *****************************************************************************/
589 struct httpd_redirect_t
595 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
596 httpd_client_t *cl, httpd_message_t *answer,
597 httpd_message_t *query )
599 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
602 if( answer == NULL || query == NULL )
606 answer->i_proto = query->i_proto;
607 answer->i_version= query->i_version;
608 answer->i_type = HTTPD_MSG_ANSWER;
609 answer->i_status = 301;
610 answer->psz_status = strdup( "Moved Permanently" );
612 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
613 p += sprintf( (char *)p,
614 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
615 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
616 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
619 "<title>Redirection</title>\n"
623 "<a href=\"%s\">redirected</a></h1>\n"
625 "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
628 "</html>\n", rdir->psz_dst );
629 answer->i_body = p - answer->p_body;
631 /* XXX check if it's ok or we need to set an absolute url */
632 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
634 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
639 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
640 const char *psz_url_src )
642 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
644 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
649 rdir->psz_dst = strdup( psz_url_dst );
651 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
652 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
653 (httpd_callback_sys_t*)rdir );
654 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
655 (httpd_callback_sys_t*)rdir );
656 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
657 (httpd_callback_sys_t*)rdir );
658 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
659 (httpd_callback_sys_t*)rdir );
663 void httpd_RedirectDelete( httpd_redirect_t *rdir )
665 httpd_UrlDelete( rdir->url );
666 free( rdir->psz_dst );
670 /*****************************************************************************
671 * High Level Funtions: httpd_stream_t
672 *****************************************************************************/
673 struct httpd_stream_t
680 /* Header to send as first packet */
684 /* circular buffer */
685 int i_buffer_size; /* buffer size, can't be reallocated smaller */
686 uint8_t *p_buffer; /* buffer */
687 int64_t i_buffer_pos; /* absolute position from begining */
688 int64_t i_buffer_last_pos; /* a new connection will start with that */
691 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
692 httpd_client_t *cl, httpd_message_t *answer,
693 httpd_message_t *query )
695 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
697 if( answer == NULL || query == NULL || cl == NULL )
702 if( answer->i_body_offset > 0 )
708 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
709 answer->i_body_offset );
712 if( answer->i_body_offset >= stream->i_buffer_pos )
714 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
715 return VLC_EGENERIC; /* wait, no data available */
717 if( answer->i_body_offset + stream->i_buffer_size <
718 stream->i_buffer_pos )
720 /* this client isn't fast enough */
721 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
722 answer->i_body_offset, stream->i_buffer_last_pos );
723 answer->i_body_offset = stream->i_buffer_last_pos;
726 i_pos = answer->i_body_offset % stream->i_buffer_size;
727 i_write = stream->i_buffer_pos - answer->i_body_offset;
728 if( i_write > HTTPD_CL_BUFSIZE )
730 i_write = HTTPD_CL_BUFSIZE;
732 else if( i_write <= 0 )
734 return VLC_EGENERIC; /* wait, no data available */
737 /* Don't go past the end of the circular buffer */
738 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
740 /* using HTTPD_MSG_ANSWER -> data available */
741 answer->i_proto = HTTPD_PROTO_HTTP;
742 answer->i_version= 0;
743 answer->i_type = HTTPD_MSG_ANSWER;
745 answer->i_body = i_write;
746 answer->p_body = malloc( i_write );
747 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
749 answer->i_body_offset += i_write;
755 answer->i_proto = HTTPD_PROTO_HTTP;
756 answer->i_version= 0;
757 answer->i_type = HTTPD_MSG_ANSWER;
759 answer->i_status = 200;
760 answer->psz_status = strdup( "OK" );
762 if( query->i_type != HTTPD_MSG_HEAD )
764 httpd_ClientModeStream( cl );
765 vlc_mutex_lock( &stream->lock );
766 /* Send the header */
767 if( stream->i_header > 0 )
769 answer->i_body = stream->i_header;
770 answer->p_body = malloc( stream->i_header );
771 memcpy( answer->p_body, stream->p_header, stream->i_header );
773 answer->i_body_offset = stream->i_buffer_last_pos;
774 vlc_mutex_unlock( &stream->lock );
778 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
779 answer->i_body_offset = 0;
782 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
784 vlc_bool_t b_xplaystream = VLC_FALSE;
787 httpd_MsgAdd( answer, "Content-type", "%s",
788 "application/octet-stream" );
789 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
790 httpd_MsgAdd( answer, "Pragma", "no-cache" );
791 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
792 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
794 /* Check if there is a xPlayStrm=1 */
795 for( i = 0; i < query->i_name; i++ )
797 if( !strcasecmp( query->name[i], "Pragma" ) &&
798 strstr( query->value[i], "xPlayStrm=1" ) )
800 b_xplaystream = VLC_TRUE;
806 answer->i_body_offset = 0;
811 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
813 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
818 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
819 const char *psz_url, const char *psz_mime,
820 const char *psz_user, const char *psz_password,
821 const vlc_acl_t *p_acl )
823 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
825 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
826 psz_password, p_acl )
832 vlc_mutex_init( host, &stream->lock );
833 if( psz_mime && *psz_mime )
835 stream->psz_mime = strdup( psz_mime );
839 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
841 stream->i_header = 0;
842 stream->p_header = NULL;
843 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
844 stream->p_buffer = malloc( stream->i_buffer_size );
845 /* We set to 1 to make life simpler
846 * (this way i_body_offset can never be 0) */
847 stream->i_buffer_pos = 1;
848 stream->i_buffer_last_pos = 1;
850 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
851 (httpd_callback_sys_t*)stream );
852 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
853 (httpd_callback_sys_t*)stream );
854 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
855 (httpd_callback_sys_t*)stream );
860 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
862 vlc_mutex_lock( &stream->lock );
863 if( stream->p_header )
865 free( stream->p_header );
866 stream->p_header = NULL;
868 stream->i_header = i_data;
871 stream->p_header = malloc( i_data );
872 memcpy( stream->p_header, p_data, i_data );
874 vlc_mutex_unlock( &stream->lock );
879 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
884 if( i_data < 0 || p_data == NULL )
888 vlc_mutex_lock( &stream->lock );
890 /* save this pointer (to be used by new connection) */
891 stream->i_buffer_last_pos = stream->i_buffer_pos;
893 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
899 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
901 /* Ok, we can't go past the end of our buffer */
902 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
904 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
909 stream->i_buffer_pos += i_data;
911 vlc_mutex_unlock( &stream->lock );
915 void httpd_StreamDelete( httpd_stream_t *stream )
917 httpd_UrlDelete( stream->url );
918 vlc_mutex_destroy( &stream->lock );
919 if( stream->psz_mime ) free( stream->psz_mime );
920 if( stream->p_header ) free( stream->p_header );
921 if( stream->p_buffer ) free( stream->p_buffer );
925 /*****************************************************************************
927 *****************************************************************************/
928 static void httpd_HostThread( httpd_host_t * );
930 /* create a new host */
931 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
934 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
938 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
940 const char *psz_cert, const char *psz_key,
941 const char *psz_ca, const char *psz_crl )
950 if( psz_hostname == NULL )
953 psz_host = strdup( psz_hostname );
954 if( psz_host == NULL )
956 msg_Err( p_this, "memory error" );
960 /* to be sure to avoid multiple creation */
961 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
962 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
963 vlc_mutex_lock( lockval.p_address );
965 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
967 msg_Info( p_this, "creating httpd" );
968 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
970 vlc_mutex_unlock( lockval.p_address );
978 vlc_object_yield( httpd );
979 vlc_object_attach( httpd, p_this->p_vlc );
982 /* verify if it already exist */
983 for( i = httpd->i_host - 1; i >= 0; i-- )
985 host = httpd->host[i];
987 /* cannot mix TLS and non-TLS hosts */
988 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
989 || ( host->i_port != i_port )
990 || strcmp( host->psz_hostname, psz_hostname ) )
996 vlc_mutex_unlock( lockval.p_address );
1002 /* determine TLS configuration */
1003 if ( psz_cert != NULL )
1005 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1006 if ( p_tls == NULL )
1008 msg_Err( p_this, "TLS initialization error" );
1012 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1014 msg_Err( p_this, "TLS CA error" );
1018 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1020 msg_Err( p_this, "TLS CRL error" );
1027 /* create the new host */
1028 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1029 host->httpd = httpd;
1030 vlc_mutex_init( httpd, &host->lock );
1033 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1034 if( host->fd == NULL )
1036 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1040 host->i_port = i_port;
1041 host->psz_hostname = psz_host;
1046 host->client = NULL;
1048 host->p_tls = p_tls;
1050 /* create the thread */
1051 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1052 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1054 msg_Err( p_this, "cannot spawn http host thread" );
1058 /* now add it to httpd */
1059 TAB_APPEND( httpd->i_host, httpd->host, host );
1060 vlc_mutex_unlock( lockval.p_address );
1066 if( httpd->i_host <= 0 )
1068 vlc_object_release( httpd );
1069 vlc_object_detach( httpd );
1070 vlc_object_destroy( httpd );
1072 vlc_mutex_unlock( lockval.p_address );
1076 net_ListenClose( host->fd );
1077 vlc_mutex_destroy( &host->lock );
1078 vlc_object_destroy( host );
1082 tls_ServerDelete( p_tls );
1088 void httpd_HostDelete( httpd_host_t *host )
1090 httpd_t *httpd = host->httpd;
1091 vlc_value_t lockval;
1094 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1095 vlc_mutex_lock( lockval.p_address );
1098 if( host->i_ref > 0 )
1101 vlc_mutex_unlock( lockval.p_address );
1102 msg_Dbg( host, "httpd_HostDelete: host still used" );
1105 TAB_REMOVE( httpd->i_host, httpd->host, host );
1108 vlc_thread_join( host );
1110 msg_Dbg( host, "HTTP host removed" );
1112 for( i = 0; i < host->i_url; i++ )
1114 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1116 for( i = 0; i < host->i_client; i++ )
1118 httpd_client_t *cl = host->client[i];
1119 msg_Warn( host, "client still connected" );
1120 httpd_ClientClean( cl );
1121 TAB_REMOVE( host->i_client, host->client, cl );
1127 if( host->p_tls != NULL)
1128 tls_ServerDelete( host->p_tls );
1130 net_ListenClose( host->fd );
1131 free( host->psz_hostname );
1133 vlc_mutex_destroy( &host->lock );
1134 vlc_object_destroy( host );
1136 vlc_object_release( httpd );
1137 if( httpd->i_host <= 0 )
1139 msg_Dbg( httpd, "no host left, stopping httpd" );
1140 vlc_object_detach( httpd );
1141 vlc_object_destroy( httpd );
1143 vlc_mutex_unlock( lockval.p_address );
1146 /* register a new url */
1147 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1148 const char *psz_user, const char *psz_password,
1149 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1154 vlc_mutex_lock( &host->lock );
1157 for( i = 0; i < host->i_url; i++ )
1159 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1161 msg_Warn( host->httpd,
1162 "cannot add '%s' (url already defined)", psz_url );
1163 vlc_mutex_unlock( &host->lock );
1169 url = malloc( sizeof( httpd_url_t ) );
1172 vlc_mutex_init( host->httpd, &url->lock );
1173 url->psz_url = strdup( psz_url );
1174 url->psz_user = strdup( psz_user ? psz_user : "" );
1175 url->psz_password = strdup( psz_password ? psz_password : "" );
1176 url->p_acl = ACL_Duplicate( host, p_acl );
1177 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1179 url->catch[i].cb = NULL;
1180 url->catch[i].p_sys = NULL;
1183 TAB_APPEND( host->i_url, host->url, url );
1184 vlc_mutex_unlock( &host->lock );
1189 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1190 const char *psz_user, const char *psz_password,
1191 const vlc_acl_t *p_acl )
1193 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1194 psz_password, p_acl, VLC_FALSE );
1197 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1198 const char *psz_user, const char *psz_password,
1199 const vlc_acl_t *p_acl )
1201 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1202 psz_password, p_acl, VLC_TRUE );
1205 /* register callback on a url */
1206 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1207 httpd_callback_sys_t *p_sys )
1209 vlc_mutex_lock( &url->lock );
1210 url->catch[i_msg].cb = cb;
1211 url->catch[i_msg].p_sys= p_sys;
1212 vlc_mutex_unlock( &url->lock );
1218 void httpd_UrlDelete( httpd_url_t *url )
1220 httpd_host_t *host = url->host;
1223 vlc_mutex_lock( &host->lock );
1224 TAB_REMOVE( host->i_url, host->url, url );
1226 vlc_mutex_destroy( &url->lock );
1227 free( url->psz_url );
1228 free( url->psz_user );
1229 free( url->psz_password );
1230 ACL_Destroy( url->p_acl );
1232 for( i = 0; i < host->i_client; i++ )
1234 httpd_client_t *client = host->client[i];
1236 if( client->url == url )
1238 /* TODO complete it */
1239 msg_Warn( host, "force closing connections" );
1240 httpd_ClientClean( client );
1241 TAB_REMOVE( host->i_client, host->client, client );
1247 vlc_mutex_unlock( &host->lock );
1250 void httpd_MsgInit( httpd_message_t *msg )
1253 msg->i_type = HTTPD_MSG_NONE;
1254 msg->i_proto = HTTPD_PROTO_NONE;
1255 msg->i_version = -1;
1258 msg->psz_status = NULL;
1260 msg->psz_url = NULL;
1261 msg->psz_args = NULL;
1263 msg->i_channel = -1;
1270 msg->i_body_offset = 0;
1275 void httpd_MsgClean( httpd_message_t *msg )
1279 if( msg->psz_status )
1281 free( msg->psz_status );
1285 free( msg->psz_url );
1289 free( msg->psz_args );
1291 for( i = 0; i < msg->i_name; i++ )
1293 free( msg->name[i] );
1294 free( msg->value[i] );
1306 free( msg->p_body );
1308 httpd_MsgInit( msg );
1311 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1315 for( i = 0; i < msg->i_name; i++ )
1317 if( !strcasecmp( msg->name[i], name ))
1319 return msg->value[i];
1325 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1330 va_start( args, psz_value );
1331 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1332 vasprintf( &value, psz_value, args );
1335 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1336 value = calloc( i_size, sizeof( char ) );
1337 vsnprintf( value, i_size, psz_value, args );
1338 value[i_size - 1] = 0;
1343 name = strdup( name );
1345 TAB_APPEND( msg->i_name, msg->name, name );
1346 TAB_APPEND( msg->i_value, msg->value, value );
1349 static void httpd_ClientInit( httpd_client_t *cl )
1351 cl->i_state = HTTPD_CLIENT_RECEIVING;
1352 cl->i_activity_date = mdate();
1353 cl->i_activity_timeout = I64C(10000000);
1354 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1356 cl->p_buffer = malloc( cl->i_buffer_size );
1357 cl->i_mode = HTTPD_CLIENT_FILE;
1358 cl->b_read_waiting = VLC_FALSE;
1360 httpd_MsgInit( &cl->query );
1361 httpd_MsgInit( &cl->answer );
1364 void httpd_ClientModeStream( httpd_client_t *cl )
1366 cl->i_mode = HTTPD_CLIENT_STREAM;
1369 void httpd_ClientModeBidir( httpd_client_t *cl )
1371 cl->i_mode = HTTPD_CLIENT_BIDIR;
1374 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
1376 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1379 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
1381 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1384 static void httpd_ClientClean( httpd_client_t *cl )
1388 if( cl->p_tls != NULL )
1389 tls_ServerSessionClose( cl->p_tls );
1390 net_Close( cl->fd );
1394 httpd_MsgClean( &cl->answer );
1395 httpd_MsgClean( &cl->query );
1399 free( cl->p_buffer );
1400 cl->p_buffer = NULL;
1404 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1406 tls_session_t *p_tls )
1408 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1410 if( !cl ) return NULL;
1414 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1415 cl->i_sock_size = i_sock_size;
1419 httpd_ClientInit( cl );
1424 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1426 tls_session_t *p_tls;
1430 return tls_Recv( p_tls, p, i_len );
1432 return recv( cl->fd, p, i_len, 0 );
1435 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1437 tls_session_t *p_tls;
1441 return tls_Send( p_tls, p, i_len );
1443 return send( cl->fd, p, i_len, 0 );
1446 static void httpd_ClientRecv( httpd_client_t *cl )
1450 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1452 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1453 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1457 cl->i_buffer += i_len;
1460 if( cl->i_buffer >= 4 )
1462 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1464 if( cl->p_buffer[0] == '$' )
1466 /* RTSP (rtp over rtsp) */
1467 cl->query.i_proto = HTTPD_PROTO_RTSP;
1468 cl->query.i_type = HTTPD_MSG_CHANNEL;
1469 cl->query.i_channel = cl->p_buffer[1];
1470 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1471 cl->query.p_body = malloc( cl->query.i_body );
1475 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1477 cl->query.i_proto = HTTPD_PROTO_HTTP;
1478 cl->query.i_type = HTTPD_MSG_ANSWER;
1480 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1482 cl->query.i_proto = HTTPD_PROTO_RTSP;
1483 cl->query.i_type = HTTPD_MSG_ANSWER;
1485 else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
1486 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1487 !memcmp( cl->p_buffer, "POST", 4 ) )
1489 cl->query.i_proto = HTTPD_PROTO_HTTP;
1490 cl->query.i_type = HTTPD_MSG_NONE;
1494 cl->query.i_proto = HTTPD_PROTO_RTSP;
1495 cl->query.i_type = HTTPD_MSG_NONE;
1499 else if( cl->query.i_body > 0 )
1501 /* we are reading the body of a request or a channel */
1502 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1503 cl->query.i_body - cl->i_buffer );
1506 cl->i_buffer += i_len;
1508 if( cl->i_buffer >= cl->query.i_body )
1510 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1515 /* we are reading a header -> char by char */
1518 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1525 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1527 cl->i_buffer_size += 1024;
1528 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1530 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1531 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1535 /* we have finished the header so parse it and set i_body */
1536 cl->p_buffer[cl->i_buffer] = '\0';
1538 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1541 * assume strlen( "HTTP/1.x" ) = 8
1543 cl->query.i_status =
1544 strtol( (char *)&cl->p_buffer[8],
1550 cl->query.psz_status = strdup( p );
1562 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1563 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1564 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1566 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1567 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1568 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1569 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1570 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1571 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1573 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1578 cl->query.i_type = HTTPD_MSG_NONE;
1580 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1582 for( i = 0; msg_type[i].name != NULL; i++ )
1584 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1585 strlen( msg_type[i].name ) ) )
1587 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1588 cl->query.i_type = msg_type[i].i_type;
1589 if( cl->query.i_proto != msg_type[i].i_proto )
1592 cl->query.i_proto = HTTPD_PROTO_NONE;
1593 cl->query.i_type = HTTPD_MSG_NONE;
1600 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1602 cl->query.i_proto = HTTPD_PROTO_HTTP;
1604 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1606 cl->query.i_proto = HTTPD_PROTO_RTSP;
1618 p2 = strchr( p, ' ' );
1623 if( !strncasecmp( p, "rtsp:", 5 ) )
1625 /* for rtsp url, you have rtsp://localhost:port/path */
1627 while( *p == '/' ) p++;
1628 while( *p && *p != '/' ) p++;
1630 cl->query.psz_url = strdup( p );
1631 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1634 cl->query.psz_args = (uint8_t *)strdup( p3 );
1642 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1644 cl->query.i_proto = HTTPD_PROTO_HTTP;
1645 cl->query.i_version = atoi( p2+7 );
1647 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1649 cl->query.i_proto = HTTPD_PROTO_RTSP;
1650 cl->query.i_version = atoi( p2+7 );
1658 p = strchr( p, '\n' );
1662 while( *p == '\n' || *p == '\r' )
1666 while( p && *p != '\0' )
1669 char *eol = p = strchr( p, '\n' );
1672 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1677 if( ( colon = strchr( line, ':' ) ) )
1683 while( *colon == ' ' )
1687 name = strdup( line );
1688 value = strdup( colon );
1690 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1691 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1693 if( !strcasecmp( name, "Content-Length" ) )
1695 cl->query.i_body = atol( value );
1702 while( *p == '\n' || *p == '\r' )
1709 if( cl->query.i_body > 0 )
1711 /* TODO Mhh, handle the case client will only send a request and close the connection
1712 * to mark and of body (probably only RTSP) */
1713 cl->query.p_body = malloc( cl->query.i_body );
1718 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1724 /* check if the client is to be set to dead */
1725 #if defined( WIN32 ) || defined( UNDER_CE )
1726 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1728 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1731 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1733 /* connection closed -> end of data */
1734 if( cl->query.i_body > 0 )
1736 cl->query.i_body = cl->i_buffer;
1738 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1742 cl->i_state = HTTPD_CLIENT_DEAD;
1745 cl->i_activity_date = mdate();
1747 /* XXX: for QT I have to disable timeout. Try to find why */
1748 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1749 cl->i_activity_timeout = 0;
1751 #if 0 /* Debugging only */
1752 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1756 fprintf( stderr, "received new request\n" );
1757 fprintf( stderr, " - proto=%s\n",
1758 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1759 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1760 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1761 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1763 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1764 cl->query.psz_status );
1766 else if( cl->query.i_type != HTTPD_MSG_NONE )
1768 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1770 for( i = 0; i < cl->query.i_name; i++ )
1772 fprintf( stderr, " - option name='%s' value='%s'\n",
1773 cl->query.name[i], cl->query.value[i] );
1779 static void httpd_ClientSend( httpd_client_t *cl )
1784 if( cl->i_buffer < 0 )
1786 /* We need to create the header */
1790 i_size = strlen( "HTTP/1.") + 10 + 10 +
1791 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1792 for( i = 0; i < cl->answer.i_name; i++ )
1794 i_size += strlen( cl->answer.name[i] ) + 2 +
1795 strlen( cl->answer.value[i] ) + 2;
1798 if( cl->i_buffer_size < i_size )
1800 cl->i_buffer_size = i_size;
1801 free( cl->p_buffer );
1802 cl->p_buffer = malloc( i_size );
1804 p = (char *)cl->p_buffer;
1806 p += sprintf( p, "%s/1.%d %d %s\r\n",
1807 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1808 cl->answer.i_version,
1809 cl->answer.i_status, cl->answer.psz_status );
1810 for( i = 0; i < cl->answer.i_name; i++ )
1812 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1813 cl->answer.value[i] );
1815 p += sprintf( p, "\r\n" );
1818 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1820 /*fprintf( stderr, "sending answer\n" );
1821 fprintf( stderr, "%s", cl->p_buffer );*/
1824 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1825 cl->i_buffer_size - cl->i_buffer );
1828 cl->i_activity_date = mdate();
1829 cl->i_buffer += i_len;
1831 if( cl->i_buffer >= cl->i_buffer_size )
1833 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1834 !cl->b_read_waiting )
1836 /* catch more body data */
1837 int i_msg = cl->query.i_type;
1838 int64_t i_offset = cl->answer.i_body_offset;
1840 httpd_MsgClean( &cl->answer );
1841 cl->answer.i_body_offset = i_offset;
1843 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1844 &cl->answer, &cl->query );
1847 if( cl->answer.i_body > 0 )
1849 /* send the body data */
1850 free( cl->p_buffer );
1851 cl->p_buffer = cl->answer.p_body;
1852 cl->i_buffer_size = cl->answer.i_body;
1855 cl->answer.i_body = 0;
1856 cl->answer.p_body = NULL;
1861 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1867 #if defined( WIN32 ) || defined( UNDER_CE )
1868 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1870 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1874 cl->i_state = HTTPD_CLIENT_DEAD;
1879 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1881 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1884 cl->i_state = HTTPD_CLIENT_RECEIVING;
1888 cl->i_state = HTTPD_CLIENT_DEAD;
1893 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1897 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1899 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1902 cl->i_state = HTTPD_CLIENT_RECEIVING;
1906 cl->i_state = HTTPD_CLIENT_DEAD;
1911 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1916 static void httpd_HostThread( httpd_host_t *host )
1918 tls_session_t *p_tls = NULL;
1920 stats_Create( host, "client_connections", STATS_CLIENT_CONNECTIONS,
1921 VLC_VAR_INTEGER, STATS_COUNTER );
1922 stats_Create( host, "active_connections", STATS_ACTIVE_CONNECTIONS,
1923 VLC_VAR_INTEGER, STATS_COUNTER );
1925 while( !host->b_die )
1927 struct timeval timeout;
1930 /* FIXME: (too) many int variables */
1932 int i_handle_max = 0;
1935 int b_low_delay = 0;
1937 if( host->i_url <= 0 )
1944 /* built a set of handle to select */
1945 FD_ZERO( &fds_read );
1946 FD_ZERO( &fds_write );
1950 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1952 FD_SET( fd, &fds_read );
1953 if( fd > i_handle_max )
1957 /* prepare a new TLS session */
1958 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1959 p_tls = tls_ServerSessionPrepare( host->p_tls );
1961 /* add all socket that should be read/write and close dead connection */
1962 vlc_mutex_lock( &host->lock );
1963 for( i_client = 0; i_client < host->i_client; i_client++ )
1965 httpd_client_t *cl = host->client[i_client];
1967 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1968 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1969 ( cl->i_activity_timeout > 0 &&
1970 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1972 httpd_ClientClean( cl );
1973 stats_UpdateInteger( host, STATS_ACTIVE_CONNECTIONS, -1, NULL );
1974 TAB_REMOVE( host->i_client, host->client, cl );
1979 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1980 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1982 FD_SET( cl->fd, &fds_read );
1983 i_handle_max = __MAX( i_handle_max, cl->fd );
1985 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1986 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1988 FD_SET( cl->fd, &fds_write );
1989 i_handle_max = __MAX( i_handle_max, cl->fd );
1991 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1993 httpd_message_t *answer = &cl->answer;
1994 httpd_message_t *query = &cl->query;
1995 int i_msg = query->i_type;
1997 httpd_MsgInit( answer );
1999 /* Handle what we received */
2000 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2001 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2003 /* we can only receive request from client when not
2006 cl->i_state = HTTPD_CLIENT_DEAD;
2008 else if( i_msg == HTTPD_MSG_ANSWER )
2010 /* We are in BIDIR mode, trigger the callback and then
2011 * check for new data */
2012 if( cl->url && cl->url->catch[i_msg].cb )
2014 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2017 cl->i_state = HTTPD_CLIENT_WAITING;
2019 else if( i_msg == HTTPD_MSG_CHANNEL )
2021 /* We are in BIDIR mode, trigger the callback and then
2022 * check for new data */
2023 if( cl->url && cl->url->catch[i_msg].cb )
2025 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2028 cl->i_state = HTTPD_CLIENT_WAITING;
2030 else if( i_msg == HTTPD_MSG_OPTIONS )
2035 answer->i_proto = query->i_proto ;
2036 answer->i_type = HTTPD_MSG_ANSWER;
2037 answer->i_version= 0;
2038 answer->i_status = 200;
2039 answer->psz_status = strdup( "Ok" );
2042 answer->p_body = NULL;
2044 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2045 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2046 httpd_MsgAdd( answer, "Server", "VLC Server" );
2047 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2048 "TEARDOWN, PLAY, PAUSE" );
2049 httpd_MsgAdd( answer, "Content-Length", "%d",
2052 cl->i_buffer = -1; /* Force the creation of the answer in
2053 * httpd_ClientSend */
2054 cl->i_state = HTTPD_CLIENT_SENDING;
2056 else if( i_msg == HTTPD_MSG_NONE )
2058 if( query->i_proto == HTTPD_PROTO_NONE )
2061 cl->i_state = HTTPD_CLIENT_DEAD;
2068 answer->i_proto = query->i_proto ;
2069 answer->i_type = HTTPD_MSG_ANSWER;
2070 answer->i_version= 0;
2071 answer->i_status = 501;
2072 answer->psz_status = strdup( "Unimplemented" );
2074 p = answer->p_body = malloc( 1000 );
2076 p += sprintf( (char *)p,
2077 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2078 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2079 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2082 "<title>Error 501</title>\n"
2085 "<h1>501 Unimplemented</h1>\n"
2087 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2091 answer->i_body = p - answer->p_body;
2092 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2094 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2095 cl->i_state = HTTPD_CLIENT_SENDING;
2100 vlc_bool_t b_auth_failed = VLC_FALSE;
2101 vlc_bool_t b_hosts_failed = VLC_FALSE;
2104 /* Search the url and trigger callbacks */
2105 for( i = 0; i < host->i_url; i++ )
2107 httpd_url_t *url = host->url[i];
2109 if( !strcmp( url->psz_url, query->psz_url ) )
2111 if( url->catch[i_msg].cb )
2113 if( answer && ( url->p_acl != NULL ) )
2115 char ip[NI_MAXNUMERICHOST];
2117 if( httpd_ClientIP( cl, ip ) != NULL )
2119 if( ACL_Check( url->p_acl, ip ) )
2121 b_hosts_failed = VLC_TRUE;
2126 b_hosts_failed = VLC_TRUE;
2129 if( answer && ( *url->psz_user || *url->psz_password ) )
2131 /* create the headers */
2132 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2136 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2137 auth = malloc( strlen(b64) + 1 );
2139 if( !strncasecmp( b64, "BASIC", 5 ) )
2142 while( *b64 == ' ' )
2146 b64_decode( auth, b64 );
2153 if( strcmp( id, auth ) )
2155 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2156 /* We fail for all url */
2157 b_auth_failed = VLC_TRUE;
2167 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2169 if( answer->i_proto == HTTPD_PROTO_NONE )
2171 /* Raw answer from a CGI */
2172 cl->i_buffer = cl->i_buffer_size;
2177 /* only one url can answer */
2179 if( cl->url == NULL )
2192 answer->i_proto = query->i_proto;
2193 answer->i_type = HTTPD_MSG_ANSWER;
2194 answer->i_version= 0;
2195 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2197 if( b_hosts_failed )
2199 answer->i_status = 403;
2200 answer->psz_status = strdup( "Forbidden" );
2202 /* FIXME: lots of code duplication */
2203 p += sprintf( (char *)p,
2204 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2205 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2206 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2209 "<title>Error 403</title>\n"
2212 "<h1>403 Forbidden (%s)</h1>\n"
2214 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2216 "</html>\n", query->psz_url );
2218 else if( b_auth_failed )
2220 answer->i_status = 401;
2221 answer->psz_status = strdup( "Authorization Required" );
2223 p += sprintf( (char *)p,
2224 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2225 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2226 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2229 "<title>Error 401</title>\n"
2232 "<h1>401 Authorization Required (%s)</h1>\n"
2234 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2236 "</html>\n", query->psz_url );
2240 /* no url registered */
2241 answer->i_status = 404;
2242 answer->psz_status = strdup( "Not found" );
2244 p += sprintf( (char *)p,
2245 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2246 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2247 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2250 "<title>Error 404</title>\n"
2253 "<h1>404 Resource not found(%s)</h1>\n"
2255 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2257 "</html>\n", query->psz_url );
2260 answer->i_body = p - answer->p_body;
2261 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2262 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2265 cl->i_state = HTTPD_CLIENT_SENDING;
2268 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2270 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2273 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2274 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2275 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2276 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2277 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2278 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2280 httpd_MsgClean( &cl->query );
2281 httpd_MsgInit( &cl->query );
2284 cl->i_buffer_size = 1000;
2285 free( cl->p_buffer );
2286 cl->p_buffer = malloc( cl->i_buffer_size );
2287 cl->i_state = HTTPD_CLIENT_RECEIVING;
2291 cl->i_state = HTTPD_CLIENT_DEAD;
2293 httpd_MsgClean( &cl->answer );
2295 else if( cl->b_read_waiting )
2297 /* we have a message waiting for us to read it */
2298 httpd_MsgClean( &cl->answer );
2299 httpd_MsgClean( &cl->query );
2302 cl->i_buffer_size = 1000;
2303 free( cl->p_buffer );
2304 cl->p_buffer = malloc( cl->i_buffer_size );
2305 cl->i_state = HTTPD_CLIENT_RECEIVING;
2306 cl->b_read_waiting = VLC_FALSE;
2310 int64_t i_offset = cl->answer.i_body_offset;
2311 httpd_MsgClean( &cl->answer );
2313 cl->answer.i_body_offset = i_offset;
2314 free( cl->p_buffer );
2315 cl->p_buffer = NULL;
2317 cl->i_buffer_size = 0;
2319 cl->i_state = HTTPD_CLIENT_WAITING;
2322 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2324 int64_t i_offset = cl->answer.i_body_offset;
2325 int i_msg = cl->query.i_type;
2327 httpd_MsgInit( &cl->answer );
2328 cl->answer.i_body_offset = i_offset;
2330 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2331 &cl->answer, &cl->query );
2332 if( cl->answer.i_type != HTTPD_MSG_NONE )
2334 /* we have new data, so reenter send mode */
2336 cl->p_buffer = cl->answer.p_body;
2337 cl->i_buffer_size = cl->answer.i_body;
2338 cl->answer.p_body = NULL;
2339 cl->answer.i_body = 0;
2340 cl->i_state = HTTPD_CLIENT_SENDING;
2344 /* we shouldn't wait too long */
2345 b_low_delay = VLC_TRUE;
2349 /* Special for BIDIR mode we also check reading */
2350 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2351 cl->i_state == HTTPD_CLIENT_SENDING )
2353 FD_SET( cl->fd, &fds_read );
2354 i_handle_max = __MAX( i_handle_max, cl->fd );
2357 vlc_mutex_unlock( &host->lock );
2359 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2361 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2363 i_ret = select( i_handle_max + 1,
2364 &fds_read, &fds_write, NULL, &timeout );
2366 if( (i_ret == -1) && (errno != EINTR) )
2368 #if defined(WIN32) || defined(UNDER_CE)
2369 msg_Warn( host, "cannot select sockets (%d)", WSAGetLastError( ) );
2371 msg_Warn( host, "cannot select sockets : %s", strerror( errno ) );
2376 else if( i_ret <= 0 )
2381 /* accept new connections */
2382 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2384 if( FD_ISSET( fd, &fds_read ) )
2386 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2387 struct sockaddr_storage sock;
2389 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2390 msg_Info( host, "Accepting" );
2395 /* set this new socket non-block */
2396 #if defined( WIN32 ) || defined( UNDER_CE )
2398 unsigned long i_dummy = 1;
2399 ioctlsocket( fd, FIONBIO, &i_dummy );
2402 fcntl( fd, F_SETFL, O_NONBLOCK );
2407 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2410 msg_Err( host, "Rejecting TLS connection" );
2416 case 1: /* missing input - most likely */
2417 i_state = HTTPD_CLIENT_TLS_HS_IN;
2420 case 2: /* missing output */
2421 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2429 stats_UpdateInteger( host, STATS_CLIENT_CONNECTIONS,
2431 stats_UpdateInteger( host, STATS_ACTIVE_CONNECTIONS, 1,
2433 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2435 vlc_mutex_lock( &host->lock );
2436 TAB_APPEND( host->i_client, host->client, cl );
2437 vlc_mutex_unlock( &host->lock );
2440 cl->i_state = i_state; // override state for TLS
2446 /* now try all others socket */
2447 vlc_mutex_lock( &host->lock );
2448 for( i_client = 0; i_client < host->i_client; i_client++ )
2450 httpd_client_t *cl = host->client[i_client];
2451 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2453 httpd_ClientRecv( cl );
2455 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2457 httpd_ClientSend( cl );
2459 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2461 httpd_ClientTlsHsIn( cl );
2463 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2465 httpd_ClientTlsHsOut( cl );
2468 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2469 cl->i_state == HTTPD_CLIENT_SENDING &&
2470 FD_ISSET( cl->fd, &fds_read ) )
2472 cl->b_read_waiting = VLC_TRUE;
2475 vlc_mutex_unlock( &host->lock );
2479 tls_ServerSessionClose( p_tls );
2482 #else /* ENABLE_HTTPD */
2484 /* We just define an empty wrapper */
2485 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2488 msg_Err( a, "HTTP daemon support is disabled" );
2492 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2494 msg_Err( a, "HTTP daemon support is disabled" );
2498 void httpd_HostDelete( httpd_host_t *a )
2502 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2503 char *psz_user, char *psz_password,
2504 const vlc_acl_t *p_acl )
2509 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2510 char *psz_user, char *psz_password,
2511 const vlc_acl_t *p_acl )
2516 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2517 httpd_callback_sys_t *d )
2522 void httpd_UrlDelete( httpd_url_t *a )
2526 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2531 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2536 void httpd_ClientModeStream( httpd_client_t *a )
2540 void httpd_ClientModeBidir( httpd_client_t *a )
2544 void httpd_FileDelete( httpd_file_t *a )
2548 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2549 char *e, httpd_file_callback_t f,
2550 httpd_file_sys_t *g )
2555 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2556 const char *psz_user,
2557 const char *psz_password,
2558 const vlc_acl_t *p_acl,
2559 httpd_handler_callback_t pf_fill,
2560 httpd_handler_sys_t *p_sys )
2565 void httpd_HandlerDelete( httpd_handler_t *handler )
2569 void httpd_RedirectDelete( httpd_redirect_t *a )
2573 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2579 void httpd_StreamDelete( httpd_stream_t *a )
2583 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2588 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2593 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2599 void httpd_MsgInit ( httpd_message_t *a )
2603 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... )
2607 char *httpd_MsgGet ( httpd_message_t *a, char *b )
2612 void httpd_MsgClean( httpd_message_t *a )
2616 #endif /* ENABLE_HTTPD */