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>
51 # include <sys/socket.h>
55 /* We need HUGE buffer otherwise TCP throughput is very limited */
56 #define HTTPD_CL_BUFSIZE 1000000
58 #define HTTPD_CL_BUFSIZE 10000
61 static void httpd_ClientClean( httpd_client_t *cl );
63 /* each host run in his own thread */
73 /* address/port and socket for listening at connections */
80 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
81 * This will slow down the url research but make my live easier
82 * All url will have their cb trigger, but only the first one can answer
88 httpd_client_t **client;
108 httpd_callback_sys_t *p_sys;
109 } catch[HTTPD_MSG_MAX];
115 HTTPD_CLIENT_RECEIVING,
116 HTTPD_CLIENT_RECEIVE_DONE,
118 HTTPD_CLIENT_SENDING,
119 HTTPD_CLIENT_SEND_DONE,
121 HTTPD_CLIENT_WAITING,
125 HTTPD_CLIENT_TLS_HS_IN,
126 HTTPD_CLIENT_TLS_HS_OUT
132 HTTPD_CLIENT_FILE, /* default */
133 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
134 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
137 struct httpd_client_t
143 struct sockaddr_storage sock;
149 int b_read_waiting; /* stop as soon as possible sending */
151 mtime_t i_activity_date;
152 mtime_t i_activity_timeout;
154 /* buffer for reading header */
160 httpd_message_t query; /* client -> httpd */
161 httpd_message_t answer; /* httpd -> client */
164 tls_session_t *p_tls;
168 /*****************************************************************************
170 *****************************************************************************/
171 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
172 static void b64_decode( char *dest, char *src )
177 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
178 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
179 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
180 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
181 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
182 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
183 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
184 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
185 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
186 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
187 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
188 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
189 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
190 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
191 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
192 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
195 for( i_level = 0; *src != '\0'; src++ )
199 c = b64[(unsigned int)*src];
211 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
215 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
219 *dest++ = ( ( last &0x03 ) << 6 ) | c;
231 const char *psz_mime;
234 { ".htm", "text/html" },
235 { ".html", "text/html" },
236 { ".txt", "text/plain" },
237 { ".xml", "text/xml" },
238 { ".dtd", "text/dtd" },
240 { ".css", "text/css" },
243 { ".gif", "image/gif" },
244 { ".jpe", "image/jpeg" },
245 { ".jpg", "image/jpeg" },
246 { ".jpeg", "image/jpeg" },
247 { ".png", "image/png" },
248 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
251 { ".avi", "video/avi" },
252 { ".asf", "video/x-ms-asf" },
253 { ".m1a", "audio/mpeg" },
254 { ".m2a", "audio/mpeg" },
255 { ".m1v", "video/mpeg" },
256 { ".m2v", "video/mpeg" },
257 { ".mp2", "audio/mpeg" },
258 { ".mp3", "audio/mpeg" },
259 { ".mpa", "audio/mpeg" },
260 { ".mpg", "video/mpeg" },
261 { ".mpeg", "video/mpeg" },
262 { ".mpe", "video/mpeg" },
263 { ".mov", "video/quicktime" },
264 { ".moov", "video/quicktime" },
265 { ".ogg", "application/ogg" },
266 { ".ogm", "application/ogg" },
267 { ".wav", "audio/wav" },
268 { ".wma", "audio/x-ms-wma" },
269 { ".wmv", "video/x-ms-wmv" },
276 static const char *httpd_MimeFromUrl( const char *psz_url )
281 psz_ext = strrchr( psz_url, '.' );
286 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
288 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
290 return http_mime[i].psz_mime;
294 return "application/octet-stream";
297 /*****************************************************************************
298 * High Level Functions: httpd_file_t
299 *****************************************************************************/
307 httpd_file_callback_t pf_fill;
308 httpd_file_sys_t *p_sys;
312 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
314 httpd_file_t *file = (httpd_file_t*)p_sys;
315 uint8_t *psz_args = query->psz_args;
316 uint8_t **pp_body, *p_body;
317 int *pi_body, i_body;
319 if( answer == NULL || query == NULL )
323 answer->i_proto = HTTPD_PROTO_HTTP;
324 answer->i_version= query->i_version;
325 answer->i_type = HTTPD_MSG_ANSWER;
327 answer->i_status = 200;
328 answer->psz_status = strdup( "OK" );
330 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
331 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
333 if( query->i_type != HTTPD_MSG_HEAD )
335 pp_body = &answer->p_body;
336 pi_body = &answer->i_body;
340 /* The file still needs to be executed. */
347 if( query->i_type == HTTPD_MSG_POST )
349 /* msg_Warn not supported */
352 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
354 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
359 /* We respect client request */
360 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
362 httpd_MsgAdd( answer, "Connection",
363 httpd_MsgGet( &cl->query, "Connection" ) );
366 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
371 httpd_file_t *httpd_FileNew( httpd_host_t *host,
372 const char *psz_url, const char *psz_mime,
373 const char *psz_user, const char *psz_password,
374 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
375 httpd_file_sys_t *p_sys )
377 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
379 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
380 psz_password, p_acl )
387 file->psz_url = strdup( psz_url );
388 if( psz_mime && *psz_mime )
390 file->psz_mime = strdup( psz_mime );
394 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
397 file->pf_fill = pf_fill;
400 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
401 (httpd_callback_sys_t*)file );
402 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
403 (httpd_callback_sys_t*)file );
404 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
405 (httpd_callback_sys_t*)file );
410 void httpd_FileDelete( httpd_file_t *file )
412 httpd_UrlDelete( file->url );
414 free( file->psz_url );
415 free( file->psz_mime );
420 /*****************************************************************************
421 * High Level Functions: httpd_handler_t (for CGIs)
422 *****************************************************************************/
423 struct httpd_handler_t
427 httpd_handler_callback_t pf_fill;
428 httpd_handler_sys_t *p_sys;
432 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
434 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
435 uint8_t *psz_args = query->psz_args;
436 char psz_remote_addr[NI_MAXNUMERICHOST];
438 if( answer == NULL || query == NULL )
442 answer->i_proto = HTTPD_PROTO_NONE;
443 answer->i_type = HTTPD_MSG_ANSWER;
445 /* We do it ourselves, thanks */
446 answer->i_status = 0;
447 answer->psz_status = NULL;
449 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
450 *psz_remote_addr = '\0';
452 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
453 query->i_type, query->p_body, query->i_body,
454 psz_remote_addr, NULL,
455 &answer->p_body, &answer->i_body );
457 if( query->i_type == HTTPD_MSG_HEAD )
459 char *p = (char *)answer->p_body;
460 while ( (p = strchr( p, '\r' )) != NULL )
462 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
463 && p[3] && p[3] == '\n' )
471 answer->i_body = strlen((char*)answer->p_body) + 1;
472 answer->p_body = realloc( answer->p_body, answer->i_body );
476 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
478 int i_status, i_headers;
479 char *psz_headers, *psz_new, *psz_status;
481 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
484 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
485 if( *psz_headers ) psz_headers++;
486 if( *psz_headers ) psz_headers++;
487 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
492 psz_headers = (char *)answer->p_body;
493 i_headers = answer->i_body;
501 psz_status = "Unauthorized";
504 if( (i_status < 0) || (i_status > 999) )
506 psz_status = "Undefined";
509 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
510 + strlen(psz_status) + i_headers - 1;
511 psz_new = (char *)malloc( answer->i_body + 1);
512 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
513 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
514 free( answer->p_body );
515 answer->p_body = (uint8_t *)psz_new;
521 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
522 const char *psz_user,
523 const char *psz_password,
524 const vlc_acl_t *p_acl,
525 httpd_handler_callback_t pf_fill,
526 httpd_handler_sys_t *p_sys )
528 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
530 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
531 psz_password, p_acl )
538 handler->pf_fill = pf_fill;
539 handler->p_sys = p_sys;
541 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
542 (httpd_callback_sys_t*)handler );
543 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
544 (httpd_callback_sys_t*)handler );
545 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
546 (httpd_callback_sys_t*)handler );
551 void httpd_HandlerDelete( httpd_handler_t *handler )
553 httpd_UrlDelete( handler->url );
557 /*****************************************************************************
558 * High Level Functions: httpd_redirect_t
559 *****************************************************************************/
560 struct httpd_redirect_t
566 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
567 httpd_client_t *cl, httpd_message_t *answer,
568 httpd_message_t *query )
570 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
573 if( answer == NULL || query == NULL )
577 answer->i_proto = query->i_proto;
578 answer->i_version= query->i_version;
579 answer->i_type = HTTPD_MSG_ANSWER;
580 answer->i_status = 301;
581 answer->psz_status = strdup( "Moved Permanently" );
583 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
584 p += sprintf( (char *)p,
585 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
586 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
587 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
590 "<title>Redirection</title>\n"
594 "<a href=\"%s\">redirected</a></h1>\n"
596 "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
599 "</html>\n", rdir->psz_dst );
600 answer->i_body = p - answer->p_body;
602 /* XXX check if it's ok or we need to set an absolute url */
603 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
605 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
610 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
611 const char *psz_url_src )
613 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
615 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
620 rdir->psz_dst = strdup( psz_url_dst );
622 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
623 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
624 (httpd_callback_sys_t*)rdir );
625 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
626 (httpd_callback_sys_t*)rdir );
627 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
628 (httpd_callback_sys_t*)rdir );
629 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
630 (httpd_callback_sys_t*)rdir );
634 void httpd_RedirectDelete( httpd_redirect_t *rdir )
636 httpd_UrlDelete( rdir->url );
637 free( rdir->psz_dst );
641 /*****************************************************************************
642 * High Level Funtions: httpd_stream_t
643 *****************************************************************************/
644 struct httpd_stream_t
651 /* Header to send as first packet */
655 /* circular buffer */
656 int i_buffer_size; /* buffer size, can't be reallocated smaller */
657 uint8_t *p_buffer; /* buffer */
658 int64_t i_buffer_pos; /* absolute position from begining */
659 int64_t i_buffer_last_pos; /* a new connection will start with that */
662 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
663 httpd_client_t *cl, httpd_message_t *answer,
664 httpd_message_t *query )
666 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
668 if( answer == NULL || query == NULL || cl == NULL )
673 if( answer->i_body_offset > 0 )
679 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
680 answer->i_body_offset );
683 if( answer->i_body_offset >= stream->i_buffer_pos )
685 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
686 return VLC_EGENERIC; /* wait, no data available */
688 if( answer->i_body_offset + stream->i_buffer_size <
689 stream->i_buffer_pos )
691 /* this client isn't fast enough */
692 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
693 answer->i_body_offset, stream->i_buffer_last_pos );
694 answer->i_body_offset = stream->i_buffer_last_pos;
697 i_pos = answer->i_body_offset % stream->i_buffer_size;
698 i_write = stream->i_buffer_pos - answer->i_body_offset;
699 if( i_write > HTTPD_CL_BUFSIZE )
701 i_write = HTTPD_CL_BUFSIZE;
703 else if( i_write <= 0 )
705 return VLC_EGENERIC; /* wait, no data available */
708 /* Don't go past the end of the circular buffer */
709 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
711 /* using HTTPD_MSG_ANSWER -> data available */
712 answer->i_proto = HTTPD_PROTO_HTTP;
713 answer->i_version= 0;
714 answer->i_type = HTTPD_MSG_ANSWER;
716 answer->i_body = i_write;
717 answer->p_body = malloc( i_write );
718 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
720 answer->i_body_offset += i_write;
726 answer->i_proto = HTTPD_PROTO_HTTP;
727 answer->i_version= 0;
728 answer->i_type = HTTPD_MSG_ANSWER;
730 answer->i_status = 200;
731 answer->psz_status = strdup( "OK" );
733 if( query->i_type != HTTPD_MSG_HEAD )
735 httpd_ClientModeStream( cl );
736 vlc_mutex_lock( &stream->lock );
737 /* Send the header */
738 if( stream->i_header > 0 )
740 answer->i_body = stream->i_header;
741 answer->p_body = malloc( stream->i_header );
742 memcpy( answer->p_body, stream->p_header, stream->i_header );
744 answer->i_body_offset = stream->i_buffer_last_pos;
745 vlc_mutex_unlock( &stream->lock );
749 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
750 answer->i_body_offset = 0;
753 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
755 vlc_bool_t b_xplaystream = VLC_FALSE;
758 httpd_MsgAdd( answer, "Content-type", "%s",
759 "application/octet-stream" );
760 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
761 httpd_MsgAdd( answer, "Pragma", "no-cache" );
762 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
763 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
765 /* Check if there is a xPlayStrm=1 */
766 for( i = 0; i < query->i_name; i++ )
768 if( !strcasecmp( query->name[i], "Pragma" ) &&
769 strstr( query->value[i], "xPlayStrm=1" ) )
771 b_xplaystream = VLC_TRUE;
777 answer->i_body_offset = 0;
782 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
784 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
789 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
790 const char *psz_url, const char *psz_mime,
791 const char *psz_user, const char *psz_password,
792 const vlc_acl_t *p_acl )
794 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
796 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
797 psz_password, p_acl )
803 vlc_mutex_init( host, &stream->lock );
804 if( psz_mime && *psz_mime )
806 stream->psz_mime = strdup( psz_mime );
810 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
812 stream->i_header = 0;
813 stream->p_header = NULL;
814 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
815 stream->p_buffer = malloc( stream->i_buffer_size );
816 /* We set to 1 to make life simpler
817 * (this way i_body_offset can never be 0) */
818 stream->i_buffer_pos = 1;
819 stream->i_buffer_last_pos = 1;
821 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
822 (httpd_callback_sys_t*)stream );
823 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
824 (httpd_callback_sys_t*)stream );
825 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
826 (httpd_callback_sys_t*)stream );
831 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
833 vlc_mutex_lock( &stream->lock );
834 if( stream->p_header )
836 free( stream->p_header );
837 stream->p_header = NULL;
839 stream->i_header = i_data;
842 stream->p_header = malloc( i_data );
843 memcpy( stream->p_header, p_data, i_data );
845 vlc_mutex_unlock( &stream->lock );
850 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
855 if( i_data < 0 || p_data == NULL )
859 vlc_mutex_lock( &stream->lock );
861 /* save this pointer (to be used by new connection) */
862 stream->i_buffer_last_pos = stream->i_buffer_pos;
864 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
870 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
872 /* Ok, we can't go past the end of our buffer */
873 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
875 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
880 stream->i_buffer_pos += i_data;
882 vlc_mutex_unlock( &stream->lock );
886 void httpd_StreamDelete( httpd_stream_t *stream )
888 httpd_UrlDelete( stream->url );
889 vlc_mutex_destroy( &stream->lock );
890 if( stream->psz_mime ) free( stream->psz_mime );
891 if( stream->p_header ) free( stream->p_header );
892 if( stream->p_buffer ) free( stream->p_buffer );
896 /*****************************************************************************
898 *****************************************************************************/
899 static void httpd_HostThread( httpd_host_t * );
901 /* create a new host */
902 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
905 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
909 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
911 const char *psz_cert, const char *psz_key,
912 const char *psz_ca, const char *psz_crl )
921 if( psz_hostname == NULL )
924 psz_host = strdup( psz_hostname );
925 if( psz_host == NULL )
927 msg_Err( p_this, "memory error" );
931 /* to be sure to avoid multiple creation */
932 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
933 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
934 vlc_mutex_lock( lockval.p_address );
936 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
938 msg_Info( p_this, "creating httpd" );
939 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
941 vlc_mutex_unlock( lockval.p_address );
949 vlc_object_yield( httpd );
950 vlc_object_attach( httpd, p_this->p_vlc );
953 /* verify if it already exist */
954 for( i = httpd->i_host - 1; i >= 0; i-- )
956 host = httpd->host[i];
958 /* cannot mix TLS and non-TLS hosts */
959 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
960 || ( host->i_port != i_port )
961 || strcmp( host->psz_hostname, psz_hostname ) )
967 vlc_mutex_unlock( lockval.p_address );
973 /* determine TLS configuration */
974 if ( psz_cert != NULL )
976 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
979 msg_Err( p_this, "TLS initialization error" );
983 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
985 msg_Err( p_this, "TLS CA error" );
989 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
991 msg_Err( p_this, "TLS CRL error" );
998 /* create the new host */
999 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1000 host->httpd = httpd;
1001 vlc_mutex_init( httpd, &host->lock );
1004 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1005 if( host->fd == NULL )
1007 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1011 host->i_port = i_port;
1012 host->psz_hostname = psz_host;
1017 host->client = NULL;
1019 host->p_tls = p_tls;
1021 /* create the thread */
1022 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1023 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1025 msg_Err( p_this, "cannot spawn http host thread" );
1029 /* now add it to httpd */
1030 TAB_APPEND( httpd->i_host, httpd->host, host );
1031 vlc_mutex_unlock( lockval.p_address );
1037 if( httpd->i_host <= 0 )
1039 vlc_object_release( httpd );
1040 vlc_object_detach( httpd );
1041 vlc_object_destroy( httpd );
1043 vlc_mutex_unlock( lockval.p_address );
1047 net_ListenClose( host->fd );
1048 vlc_mutex_destroy( &host->lock );
1049 vlc_object_destroy( host );
1053 tls_ServerDelete( p_tls );
1059 void httpd_HostDelete( httpd_host_t *host )
1061 httpd_t *httpd = host->httpd;
1062 vlc_value_t lockval;
1065 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1066 vlc_mutex_lock( lockval.p_address );
1069 if( host->i_ref > 0 )
1072 vlc_mutex_unlock( lockval.p_address );
1073 msg_Dbg( host, "httpd_HostDelete: host still used" );
1076 TAB_REMOVE( httpd->i_host, httpd->host, host );
1079 vlc_thread_join( host );
1081 msg_Dbg( host, "HTTP host removed" );
1083 for( i = 0; i < host->i_url; i++ )
1085 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1087 for( i = 0; i < host->i_client; i++ )
1089 httpd_client_t *cl = host->client[i];
1090 msg_Warn( host, "client still connected" );
1091 httpd_ClientClean( cl );
1092 TAB_REMOVE( host->i_client, host->client, cl );
1098 if( host->p_tls != NULL)
1099 tls_ServerDelete( host->p_tls );
1101 net_ListenClose( host->fd );
1102 free( host->psz_hostname );
1104 vlc_mutex_destroy( &host->lock );
1105 vlc_object_destroy( host );
1107 vlc_object_release( httpd );
1108 if( httpd->i_host <= 0 )
1110 msg_Dbg( httpd, "no host left, stopping httpd" );
1111 vlc_object_detach( httpd );
1112 vlc_object_destroy( httpd );
1114 vlc_mutex_unlock( lockval.p_address );
1117 /* register a new url */
1118 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1119 const char *psz_user, const char *psz_password,
1120 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1125 vlc_mutex_lock( &host->lock );
1128 for( i = 0; i < host->i_url; i++ )
1130 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1132 msg_Warn( host->httpd,
1133 "cannot add '%s' (url already defined)", psz_url );
1134 vlc_mutex_unlock( &host->lock );
1140 url = malloc( sizeof( httpd_url_t ) );
1143 vlc_mutex_init( host->httpd, &url->lock );
1144 url->psz_url = strdup( psz_url );
1145 url->psz_user = strdup( psz_user ? psz_user : "" );
1146 url->psz_password = strdup( psz_password ? psz_password : "" );
1147 url->p_acl = ACL_Duplicate( host, p_acl );
1148 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1150 url->catch[i].cb = NULL;
1151 url->catch[i].p_sys = NULL;
1154 TAB_APPEND( host->i_url, host->url, url );
1155 vlc_mutex_unlock( &host->lock );
1160 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1161 const char *psz_user, const char *psz_password,
1162 const vlc_acl_t *p_acl )
1164 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1165 psz_password, p_acl, VLC_FALSE );
1168 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1169 const char *psz_user, const char *psz_password,
1170 const vlc_acl_t *p_acl )
1172 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1173 psz_password, p_acl, VLC_TRUE );
1176 /* register callback on a url */
1177 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1178 httpd_callback_sys_t *p_sys )
1180 vlc_mutex_lock( &url->lock );
1181 url->catch[i_msg].cb = cb;
1182 url->catch[i_msg].p_sys= p_sys;
1183 vlc_mutex_unlock( &url->lock );
1189 void httpd_UrlDelete( httpd_url_t *url )
1191 httpd_host_t *host = url->host;
1194 vlc_mutex_lock( &host->lock );
1195 TAB_REMOVE( host->i_url, host->url, url );
1197 vlc_mutex_destroy( &url->lock );
1198 free( url->psz_url );
1199 free( url->psz_user );
1200 free( url->psz_password );
1201 ACL_Destroy( url->p_acl );
1203 for( i = 0; i < host->i_client; i++ )
1205 httpd_client_t *client = host->client[i];
1207 if( client->url == url )
1209 /* TODO complete it */
1210 msg_Warn( host, "force closing connections" );
1211 httpd_ClientClean( client );
1212 TAB_REMOVE( host->i_client, host->client, client );
1218 vlc_mutex_unlock( &host->lock );
1221 void httpd_MsgInit( httpd_message_t *msg )
1224 msg->i_type = HTTPD_MSG_NONE;
1225 msg->i_proto = HTTPD_PROTO_NONE;
1226 msg->i_version = -1;
1229 msg->psz_status = NULL;
1231 msg->psz_url = NULL;
1232 msg->psz_args = NULL;
1234 msg->i_channel = -1;
1241 msg->i_body_offset = 0;
1246 void httpd_MsgClean( httpd_message_t *msg )
1250 if( msg->psz_status )
1252 free( msg->psz_status );
1256 free( msg->psz_url );
1260 free( msg->psz_args );
1262 for( i = 0; i < msg->i_name; i++ )
1264 free( msg->name[i] );
1265 free( msg->value[i] );
1277 free( msg->p_body );
1279 httpd_MsgInit( msg );
1282 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1286 for( i = 0; i < msg->i_name; i++ )
1288 if( !strcasecmp( msg->name[i], name ))
1290 return msg->value[i];
1296 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1301 va_start( args, psz_value );
1302 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1303 vasprintf( &value, psz_value, args );
1306 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1307 value = calloc( i_size, sizeof( char ) );
1308 vsnprintf( value, i_size, psz_value, args );
1309 value[i_size - 1] = 0;
1314 name = strdup( name );
1316 TAB_APPEND( msg->i_name, msg->name, name );
1317 TAB_APPEND( msg->i_value, msg->value, value );
1320 static void httpd_ClientInit( httpd_client_t *cl )
1322 cl->i_state = HTTPD_CLIENT_RECEIVING;
1323 cl->i_activity_date = mdate();
1324 cl->i_activity_timeout = I64C(10000000);
1325 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1327 cl->p_buffer = malloc( cl->i_buffer_size );
1328 cl->i_mode = HTTPD_CLIENT_FILE;
1329 cl->b_read_waiting = VLC_FALSE;
1331 httpd_MsgInit( &cl->query );
1332 httpd_MsgInit( &cl->answer );
1335 void httpd_ClientModeStream( httpd_client_t *cl )
1337 cl->i_mode = HTTPD_CLIENT_STREAM;
1340 void httpd_ClientModeBidir( httpd_client_t *cl )
1342 cl->i_mode = HTTPD_CLIENT_BIDIR;
1345 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1347 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1350 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1352 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1355 static void httpd_ClientClean( httpd_client_t *cl )
1359 if( cl->p_tls != NULL )
1360 tls_ServerSessionClose( cl->p_tls );
1361 net_Close( cl->fd );
1365 httpd_MsgClean( &cl->answer );
1366 httpd_MsgClean( &cl->query );
1370 free( cl->p_buffer );
1371 cl->p_buffer = NULL;
1375 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1377 tls_session_t *p_tls )
1379 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1381 if( !cl ) return NULL;
1385 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1386 cl->i_sock_size = i_sock_size;
1390 httpd_ClientInit( cl );
1395 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1397 tls_session_t *p_tls;
1401 return tls_Recv( p_tls, p, i_len );
1403 return recv( cl->fd, p, i_len, 0 );
1406 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1408 tls_session_t *p_tls;
1412 return tls_Send( p_tls, p, i_len );
1414 return send( cl->fd, p, i_len, 0 );
1417 static void httpd_ClientRecv( httpd_client_t *cl )
1421 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1423 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1424 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1428 cl->i_buffer += i_len;
1431 if( cl->i_buffer >= 4 )
1433 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1435 if( cl->p_buffer[0] == '$' )
1437 /* RTSP (rtp over rtsp) */
1438 cl->query.i_proto = HTTPD_PROTO_RTSP;
1439 cl->query.i_type = HTTPD_MSG_CHANNEL;
1440 cl->query.i_channel = cl->p_buffer[1];
1441 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1442 cl->query.p_body = malloc( cl->query.i_body );
1446 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1448 cl->query.i_proto = HTTPD_PROTO_HTTP;
1449 cl->query.i_type = HTTPD_MSG_ANSWER;
1451 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1453 cl->query.i_proto = HTTPD_PROTO_RTSP;
1454 cl->query.i_type = HTTPD_MSG_ANSWER;
1456 else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
1457 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1458 !memcmp( cl->p_buffer, "POST", 4 ) )
1460 cl->query.i_proto = HTTPD_PROTO_HTTP;
1461 cl->query.i_type = HTTPD_MSG_NONE;
1465 cl->query.i_proto = HTTPD_PROTO_RTSP;
1466 cl->query.i_type = HTTPD_MSG_NONE;
1470 else if( cl->query.i_body > 0 )
1472 /* we are reading the body of a request or a channel */
1473 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1474 cl->query.i_body - cl->i_buffer );
1477 cl->i_buffer += i_len;
1479 if( cl->i_buffer >= cl->query.i_body )
1481 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1486 /* we are reading a header -> char by char */
1489 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1496 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1498 cl->i_buffer_size += 1024;
1499 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1501 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1502 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1506 /* we have finished the header so parse it and set i_body */
1507 cl->p_buffer[cl->i_buffer] = '\0';
1509 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1512 * assume strlen( "HTTP/1.x" ) = 8
1514 cl->query.i_status =
1515 strtol( (char *)&cl->p_buffer[8],
1521 cl->query.psz_status = strdup( p );
1533 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1534 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1535 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1537 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1538 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1539 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1540 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1541 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1542 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1544 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1549 cl->query.i_type = HTTPD_MSG_NONE;
1551 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1553 for( i = 0; msg_type[i].name != NULL; i++ )
1555 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1556 strlen( msg_type[i].name ) ) )
1558 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1559 cl->query.i_type = msg_type[i].i_type;
1560 if( cl->query.i_proto != msg_type[i].i_proto )
1563 cl->query.i_proto = HTTPD_PROTO_NONE;
1564 cl->query.i_type = HTTPD_MSG_NONE;
1571 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1573 cl->query.i_proto = HTTPD_PROTO_HTTP;
1575 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1577 cl->query.i_proto = HTTPD_PROTO_RTSP;
1589 p2 = strchr( p, ' ' );
1594 if( !strncasecmp( p, "rtsp:", 5 ) )
1596 /* for rtsp url, you have rtsp://localhost:port/path */
1598 while( *p == '/' ) p++;
1599 while( *p && *p != '/' ) p++;
1601 cl->query.psz_url = strdup( p );
1602 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1605 cl->query.psz_args = (uint8_t *)strdup( p3 );
1613 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1615 cl->query.i_proto = HTTPD_PROTO_HTTP;
1616 cl->query.i_version = atoi( p2+7 );
1618 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1620 cl->query.i_proto = HTTPD_PROTO_RTSP;
1621 cl->query.i_version = atoi( p2+7 );
1629 p = strchr( p, '\n' );
1633 while( *p == '\n' || *p == '\r' )
1637 while( p && *p != '\0' )
1640 char *eol = p = strchr( p, '\n' );
1643 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1648 if( ( colon = strchr( line, ':' ) ) )
1654 while( *colon == ' ' )
1658 name = strdup( line );
1659 value = strdup( colon );
1661 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1662 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1664 if( !strcasecmp( name, "Content-Length" ) )
1666 cl->query.i_body = atol( value );
1673 while( *p == '\n' || *p == '\r' )
1680 if( cl->query.i_body > 0 )
1682 /* TODO Mhh, handle the case client will only send a request and close the connection
1683 * to mark and of body (probably only RTSP) */
1684 cl->query.p_body = malloc( cl->query.i_body );
1689 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1695 /* check if the client is to be set to dead */
1696 #if defined( WIN32 ) || defined( UNDER_CE )
1697 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1699 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1702 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1704 /* connection closed -> end of data */
1705 if( cl->query.i_body > 0 )
1707 cl->query.i_body = cl->i_buffer;
1709 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1713 cl->i_state = HTTPD_CLIENT_DEAD;
1716 cl->i_activity_date = mdate();
1718 /* XXX: for QT I have to disable timeout. Try to find why */
1719 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1720 cl->i_activity_timeout = 0;
1722 #if 0 /* Debugging only */
1723 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1727 fprintf( stderr, "received new request\n" );
1728 fprintf( stderr, " - proto=%s\n",
1729 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1730 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1731 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1732 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1734 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1735 cl->query.psz_status );
1737 else if( cl->query.i_type != HTTPD_MSG_NONE )
1739 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1741 for( i = 0; i < cl->query.i_name; i++ )
1743 fprintf( stderr, " - option name='%s' value='%s'\n",
1744 cl->query.name[i], cl->query.value[i] );
1750 static void httpd_ClientSend( httpd_client_t *cl )
1755 if( cl->i_buffer < 0 )
1757 /* We need to create the header */
1761 i_size = strlen( "HTTP/1.") + 10 + 10 +
1762 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1763 for( i = 0; i < cl->answer.i_name; i++ )
1765 i_size += strlen( cl->answer.name[i] ) + 2 +
1766 strlen( cl->answer.value[i] ) + 2;
1769 if( cl->i_buffer_size < i_size )
1771 cl->i_buffer_size = i_size;
1772 free( cl->p_buffer );
1773 cl->p_buffer = malloc( i_size );
1775 p = (char *)cl->p_buffer;
1777 p += sprintf( p, "%s/1.%d %d %s\r\n",
1778 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1779 cl->answer.i_version,
1780 cl->answer.i_status, cl->answer.psz_status );
1781 for( i = 0; i < cl->answer.i_name; i++ )
1783 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1784 cl->answer.value[i] );
1786 p += sprintf( p, "\r\n" );
1789 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1791 /*fprintf( stderr, "sending answer\n" );
1792 fprintf( stderr, "%s", cl->p_buffer );*/
1795 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1796 cl->i_buffer_size - cl->i_buffer );
1799 cl->i_activity_date = mdate();
1800 cl->i_buffer += i_len;
1802 if( cl->i_buffer >= cl->i_buffer_size )
1804 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1805 !cl->b_read_waiting )
1807 /* catch more body data */
1808 int i_msg = cl->query.i_type;
1809 int64_t i_offset = cl->answer.i_body_offset;
1811 httpd_MsgClean( &cl->answer );
1812 cl->answer.i_body_offset = i_offset;
1814 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1815 &cl->answer, &cl->query );
1818 if( cl->answer.i_body > 0 )
1820 /* send the body data */
1821 free( cl->p_buffer );
1822 cl->p_buffer = cl->answer.p_body;
1823 cl->i_buffer_size = cl->answer.i_body;
1826 cl->answer.i_body = 0;
1827 cl->answer.p_body = NULL;
1832 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1838 #if defined( WIN32 ) || defined( UNDER_CE )
1839 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1841 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1845 cl->i_state = HTTPD_CLIENT_DEAD;
1850 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1852 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1855 cl->i_state = HTTPD_CLIENT_RECEIVING;
1859 cl->i_state = HTTPD_CLIENT_DEAD;
1864 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1868 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1870 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1873 cl->i_state = HTTPD_CLIENT_RECEIVING;
1877 cl->i_state = HTTPD_CLIENT_DEAD;
1882 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1887 static void httpd_HostThread( httpd_host_t *host )
1889 tls_session_t *p_tls = NULL;
1891 stats_Create( host, "client_connections", STATS_CLIENT_CONNECTIONS,
1892 VLC_VAR_INTEGER, STATS_COUNTER );
1893 stats_Create( host, "active_connections", STATS_ACTIVE_CONNECTIONS,
1894 VLC_VAR_INTEGER, STATS_COUNTER );
1896 while( !host->b_die )
1898 struct timeval timeout;
1901 /* FIXME: (too) many int variables */
1903 int i_handle_max = 0;
1906 int b_low_delay = 0;
1908 if( host->i_url <= 0 )
1915 /* built a set of handle to select */
1916 FD_ZERO( &fds_read );
1917 FD_ZERO( &fds_write );
1921 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1923 FD_SET( fd, &fds_read );
1924 if( fd > i_handle_max )
1928 /* prepare a new TLS session */
1929 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1930 p_tls = tls_ServerSessionPrepare( host->p_tls );
1932 /* add all socket that should be read/write and close dead connection */
1933 vlc_mutex_lock( &host->lock );
1934 for( i_client = 0; i_client < host->i_client; i_client++ )
1936 httpd_client_t *cl = host->client[i_client];
1938 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1939 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1940 ( cl->i_activity_timeout > 0 &&
1941 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
1943 httpd_ClientClean( cl );
1944 stats_UpdateInteger( host, STATS_ACTIVE_CONNECTIONS, -1, NULL );
1945 TAB_REMOVE( host->i_client, host->client, cl );
1950 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1951 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1953 FD_SET( cl->fd, &fds_read );
1954 i_handle_max = __MAX( i_handle_max, cl->fd );
1956 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1957 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1959 FD_SET( cl->fd, &fds_write );
1960 i_handle_max = __MAX( i_handle_max, cl->fd );
1962 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1964 httpd_message_t *answer = &cl->answer;
1965 httpd_message_t *query = &cl->query;
1966 int i_msg = query->i_type;
1968 httpd_MsgInit( answer );
1970 /* Handle what we received */
1971 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
1972 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
1974 /* we can only receive request from client when not
1977 cl->i_state = HTTPD_CLIENT_DEAD;
1979 else if( i_msg == HTTPD_MSG_ANSWER )
1981 /* We are in BIDIR mode, trigger the callback and then
1982 * check for new data */
1983 if( cl->url && cl->url->catch[i_msg].cb )
1985 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1988 cl->i_state = HTTPD_CLIENT_WAITING;
1990 else if( i_msg == HTTPD_MSG_CHANNEL )
1992 /* We are in BIDIR mode, trigger the callback and then
1993 * check for new data */
1994 if( cl->url && cl->url->catch[i_msg].cb )
1996 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1999 cl->i_state = HTTPD_CLIENT_WAITING;
2001 else if( i_msg == HTTPD_MSG_OPTIONS )
2006 answer->i_proto = query->i_proto ;
2007 answer->i_type = HTTPD_MSG_ANSWER;
2008 answer->i_version= 0;
2009 answer->i_status = 200;
2010 answer->psz_status = strdup( "Ok" );
2013 answer->p_body = NULL;
2015 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2016 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2017 httpd_MsgAdd( answer, "Server", "VLC Server" );
2018 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2019 "TEARDOWN, PLAY, PAUSE" );
2020 httpd_MsgAdd( answer, "Content-Length", "%d",
2023 cl->i_buffer = -1; /* Force the creation of the answer in
2024 * httpd_ClientSend */
2025 cl->i_state = HTTPD_CLIENT_SENDING;
2027 else if( i_msg == HTTPD_MSG_NONE )
2029 if( query->i_proto == HTTPD_PROTO_NONE )
2032 cl->i_state = HTTPD_CLIENT_DEAD;
2039 answer->i_proto = query->i_proto ;
2040 answer->i_type = HTTPD_MSG_ANSWER;
2041 answer->i_version= 0;
2042 answer->i_status = 501;
2043 answer->psz_status = strdup( "Unimplemented" );
2045 p = answer->p_body = malloc( 1000 );
2047 p += sprintf( (char *)p,
2048 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2049 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2050 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2053 "<title>Error 501</title>\n"
2056 "<h1>501 Unimplemented</h1>\n"
2058 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2062 answer->i_body = p - answer->p_body;
2063 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2065 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2066 cl->i_state = HTTPD_CLIENT_SENDING;
2071 vlc_bool_t b_auth_failed = VLC_FALSE;
2072 vlc_bool_t b_hosts_failed = VLC_FALSE;
2075 /* Search the url and trigger callbacks */
2076 for( i = 0; i < host->i_url; i++ )
2078 httpd_url_t *url = host->url[i];
2080 if( !strcmp( url->psz_url, query->psz_url ) )
2082 if( url->catch[i_msg].cb )
2084 if( answer && ( url->p_acl != NULL ) )
2086 char ip[NI_MAXNUMERICHOST];
2088 if( httpd_ClientIP( cl, ip ) != NULL )
2090 if( ACL_Check( url->p_acl, ip ) )
2092 b_hosts_failed = VLC_TRUE;
2097 b_hosts_failed = VLC_TRUE;
2100 if( answer && ( *url->psz_user || *url->psz_password ) )
2102 /* create the headers */
2103 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2107 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2108 auth = malloc( strlen(b64) + 1 );
2110 if( !strncasecmp( b64, "BASIC", 5 ) )
2113 while( *b64 == ' ' )
2117 b64_decode( auth, b64 );
2124 if( strcmp( id, auth ) )
2126 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2127 /* We fail for all url */
2128 b_auth_failed = VLC_TRUE;
2138 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2140 if( answer->i_proto == HTTPD_PROTO_NONE )
2142 /* Raw answer from a CGI */
2143 cl->i_buffer = cl->i_buffer_size;
2148 /* only one url can answer */
2150 if( cl->url == NULL )
2163 answer->i_proto = query->i_proto;
2164 answer->i_type = HTTPD_MSG_ANSWER;
2165 answer->i_version= 0;
2166 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2168 if( b_hosts_failed )
2170 answer->i_status = 403;
2171 answer->psz_status = strdup( "Forbidden" );
2173 /* FIXME: lots of code duplication */
2174 p += sprintf( (char *)p,
2175 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2176 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2177 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2180 "<title>Error 403</title>\n"
2183 "<h1>403 Forbidden (%s)</h1>\n"
2185 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2187 "</html>\n", query->psz_url );
2189 else if( b_auth_failed )
2191 answer->i_status = 401;
2192 answer->psz_status = strdup( "Authorization Required" );
2194 p += sprintf( (char *)p,
2195 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2196 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2197 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2200 "<title>Error 401</title>\n"
2203 "<h1>401 Authorization Required (%s)</h1>\n"
2205 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2207 "</html>\n", query->psz_url );
2211 /* no url registered */
2212 answer->i_status = 404;
2213 answer->psz_status = strdup( "Not found" );
2215 p += sprintf( (char *)p,
2216 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2217 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2218 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2221 "<title>Error 404</title>\n"
2224 "<h1>404 Resource not found(%s)</h1>\n"
2226 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2228 "</html>\n", query->psz_url );
2231 answer->i_body = p - answer->p_body;
2232 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2233 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2236 cl->i_state = HTTPD_CLIENT_SENDING;
2239 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2241 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2244 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2245 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2246 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2247 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2248 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2249 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2251 httpd_MsgClean( &cl->query );
2252 httpd_MsgInit( &cl->query );
2255 cl->i_buffer_size = 1000;
2256 free( cl->p_buffer );
2257 cl->p_buffer = malloc( cl->i_buffer_size );
2258 cl->i_state = HTTPD_CLIENT_RECEIVING;
2262 cl->i_state = HTTPD_CLIENT_DEAD;
2264 httpd_MsgClean( &cl->answer );
2266 else if( cl->b_read_waiting )
2268 /* we have a message waiting for us to read it */
2269 httpd_MsgClean( &cl->answer );
2270 httpd_MsgClean( &cl->query );
2273 cl->i_buffer_size = 1000;
2274 free( cl->p_buffer );
2275 cl->p_buffer = malloc( cl->i_buffer_size );
2276 cl->i_state = HTTPD_CLIENT_RECEIVING;
2277 cl->b_read_waiting = VLC_FALSE;
2281 int64_t i_offset = cl->answer.i_body_offset;
2282 httpd_MsgClean( &cl->answer );
2284 cl->answer.i_body_offset = i_offset;
2285 free( cl->p_buffer );
2286 cl->p_buffer = NULL;
2288 cl->i_buffer_size = 0;
2290 cl->i_state = HTTPD_CLIENT_WAITING;
2293 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2295 int64_t i_offset = cl->answer.i_body_offset;
2296 int i_msg = cl->query.i_type;
2298 httpd_MsgInit( &cl->answer );
2299 cl->answer.i_body_offset = i_offset;
2301 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2302 &cl->answer, &cl->query );
2303 if( cl->answer.i_type != HTTPD_MSG_NONE )
2305 /* we have new data, so reenter send mode */
2307 cl->p_buffer = cl->answer.p_body;
2308 cl->i_buffer_size = cl->answer.i_body;
2309 cl->answer.p_body = NULL;
2310 cl->answer.i_body = 0;
2311 cl->i_state = HTTPD_CLIENT_SENDING;
2315 /* we shouldn't wait too long */
2316 b_low_delay = VLC_TRUE;
2320 /* Special for BIDIR mode we also check reading */
2321 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2322 cl->i_state == HTTPD_CLIENT_SENDING )
2324 FD_SET( cl->fd, &fds_read );
2325 i_handle_max = __MAX( i_handle_max, cl->fd );
2328 vlc_mutex_unlock( &host->lock );
2330 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2332 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2334 i_ret = select( i_handle_max + 1,
2335 &fds_read, &fds_write, NULL, &timeout );
2337 if( (i_ret == -1) && (errno != EINTR) )
2339 #if defined(WIN32) || defined(UNDER_CE)
2340 msg_Warn( host, "cannot select sockets (%d)", WSAGetLastError( ) );
2342 msg_Warn( host, "cannot select sockets : %s", strerror( errno ) );
2347 else if( i_ret <= 0 )
2352 /* accept new connections */
2353 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2355 if( FD_ISSET( fd, &fds_read ) )
2357 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2358 struct sockaddr_storage sock;
2360 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2361 msg_Info( host, "Accepting" );
2366 /* set this new socket non-block */
2367 #if defined( WIN32 ) || defined( UNDER_CE )
2369 unsigned long i_dummy = 1;
2370 ioctlsocket( fd, FIONBIO, &i_dummy );
2373 fcntl( fd, F_SETFL, O_NONBLOCK );
2378 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2381 msg_Err( host, "Rejecting TLS connection" );
2387 case 1: /* missing input - most likely */
2388 i_state = HTTPD_CLIENT_TLS_HS_IN;
2391 case 2: /* missing output */
2392 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2400 stats_UpdateInteger( host, STATS_CLIENT_CONNECTIONS,
2402 stats_UpdateInteger( host, STATS_ACTIVE_CONNECTIONS, 1,
2404 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2406 vlc_mutex_lock( &host->lock );
2407 TAB_APPEND( host->i_client, host->client, cl );
2408 vlc_mutex_unlock( &host->lock );
2411 cl->i_state = i_state; // override state for TLS
2417 /* now try all others socket */
2418 vlc_mutex_lock( &host->lock );
2419 for( i_client = 0; i_client < host->i_client; i_client++ )
2421 httpd_client_t *cl = host->client[i_client];
2422 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2424 httpd_ClientRecv( cl );
2426 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2428 httpd_ClientSend( cl );
2430 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2432 httpd_ClientTlsHsIn( cl );
2434 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2436 httpd_ClientTlsHsOut( cl );
2439 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2440 cl->i_state == HTTPD_CLIENT_SENDING &&
2441 FD_ISSET( cl->fd, &fds_read ) )
2443 cl->b_read_waiting = VLC_TRUE;
2446 vlc_mutex_unlock( &host->lock );
2450 tls_ServerSessionClose( p_tls );
2453 #else /* ENABLE_HTTPD */
2455 /* We just define an empty wrapper */
2456 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2459 msg_Err( a, "HTTP daemon support is disabled" );
2463 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2465 msg_Err( a, "HTTP daemon support is disabled" );
2469 void httpd_HostDelete( httpd_host_t *a )
2473 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2474 char *psz_user, char *psz_password,
2475 const vlc_acl_t *p_acl )
2480 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2481 char *psz_user, char *psz_password,
2482 const vlc_acl_t *p_acl )
2487 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2488 httpd_callback_sys_t *d )
2493 void httpd_UrlDelete( httpd_url_t *a )
2497 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2502 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2507 void httpd_ClientModeStream( httpd_client_t *a )
2511 void httpd_ClientModeBidir( httpd_client_t *a )
2515 void httpd_FileDelete( httpd_file_t *a )
2519 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2520 char *e, httpd_file_callback_t f,
2521 httpd_file_sys_t *g )
2526 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2527 const char *psz_user,
2528 const char *psz_password,
2529 const vlc_acl_t *p_acl,
2530 httpd_handler_callback_t pf_fill,
2531 httpd_handler_sys_t *p_sys )
2536 void httpd_HandlerDelete( httpd_handler_t *handler )
2540 void httpd_RedirectDelete( httpd_redirect_t *a )
2544 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2550 void httpd_StreamDelete( httpd_stream_t *a )
2554 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2559 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2564 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2570 void httpd_MsgInit ( httpd_message_t *a )
2574 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... )
2578 char *httpd_MsgGet ( httpd_message_t *a, char *b )
2583 void httpd_MsgClean( httpd_message_t *a )
2587 #endif /* ENABLE_HTTPD */