1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2004-2007 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
31 #include <vlc_httpd.h>
37 #include <vlc_network.h>
40 #include <vlc_strings.h>
41 #include "../libvlc.h"
58 #if defined( UNDER_CE )
60 #elif defined( WIN32 )
61 # include <winsock2.h>
63 # include <sys/socket.h>
67 /* We need HUGE buffer otherwise TCP throughput is very limited */
68 #define HTTPD_CL_BUFSIZE 1000000
70 #define HTTPD_CL_BUFSIZE 10000
73 static void httpd_ClientClean( httpd_client_t *cl );
84 /* each host run in his own thread */
94 /* address/port and socket for listening at connections */
102 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
103 * This will slow down the url research but make my live easier
104 * All url will have their cb trigger, but only the first one can answer
110 httpd_client_t **client;
131 httpd_callback_sys_t *p_sys;
132 } catch[HTTPD_MSG_MAX];
138 HTTPD_CLIENT_RECEIVING,
139 HTTPD_CLIENT_RECEIVE_DONE,
141 HTTPD_CLIENT_SENDING,
142 HTTPD_CLIENT_SEND_DONE,
144 HTTPD_CLIENT_WAITING,
148 HTTPD_CLIENT_TLS_HS_IN,
149 HTTPD_CLIENT_TLS_HS_OUT
155 HTTPD_CLIENT_FILE, /* default */
156 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
157 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
160 struct httpd_client_t
170 int b_read_waiting; /* stop as soon as possible sending */
172 mtime_t i_activity_date;
173 mtime_t i_activity_timeout;
175 /* buffer for reading header */
181 httpd_message_t query; /* client -> httpd */
182 httpd_message_t answer; /* httpd -> client */
185 tls_session_t *p_tls;
189 /*****************************************************************************
191 *****************************************************************************/
194 const char psz_ext[8];
195 const char *psz_mime;
198 { ".htm", "text/html" },
199 { ".html", "text/html" },
200 { ".txt", "text/plain" },
201 { ".xml", "text/xml" },
202 { ".dtd", "text/dtd" },
204 { ".css", "text/css" },
207 { ".gif", "image/gif" },
208 { ".jpe", "image/jpeg" },
209 { ".jpg", "image/jpeg" },
210 { ".jpeg", "image/jpeg" },
211 { ".png", "image/png" },
212 /* same as modules/mux/mpjpeg.c here: */
213 { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" },
216 { ".avi", "video/avi" },
217 { ".asf", "video/x-ms-asf" },
218 { ".m1a", "audio/mpeg" },
219 { ".m2a", "audio/mpeg" },
220 { ".m1v", "video/mpeg" },
221 { ".m2v", "video/mpeg" },
222 { ".mp2", "audio/mpeg" },
223 { ".mp3", "audio/mpeg" },
224 { ".mpa", "audio/mpeg" },
225 { ".mpg", "video/mpeg" },
226 { ".mpeg", "video/mpeg" },
227 { ".mpe", "video/mpeg" },
228 { ".mov", "video/quicktime" },
229 { ".moov", "video/quicktime" },
230 { ".ogg", "application/ogg" },
231 { ".ogm", "application/ogg" },
232 { ".wav", "audio/wav" },
233 { ".wma", "audio/x-ms-wma" },
234 { ".wmv", "video/x-ms-wmv" },
241 static const char *httpd_MimeFromUrl( const char *psz_url )
246 psz_ext = strrchr( psz_url, '.' );
251 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
253 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
255 return http_mime[i].psz_mime;
259 return "application/octet-stream";
266 const char psz_reason[36];
269 static const http_status_info http_reason[] =
271 /*{ 100, "Continue" },
272 { 101, "Switching Protocols" },*/
274 /*{ 201, "Created" },
276 { 203, "Non-authoritative information" },
277 { 204, "No content" },
278 { 205, "Reset content" },
279 { 206, "Partial content" },
280 { 250, "Low on storage space" },
281 { 300, "Multiple choices" },*/
282 { 301, "Moved permanently" },
283 /*{ 302, "Moved temporarily" },
284 { 303, "See other" },
285 { 304, "Not modified" },
286 { 305, "Use proxy" },
287 { 307, "Temporary redirect" },
288 { 400, "Bad request" },*/
289 { 401, "Unauthorized" },
290 /*{ 402, "Payment Required" },*/
291 { 403, "Forbidden" },
292 { 404, "Not found" },
293 { 405, "Method not allowed" },
294 /*{ 406, "Not acceptable" },
295 { 407, "Proxy authentication required" },
296 { 408, "Request time-out" },
299 { 411, "Length required" },
300 { 412, "Precondition failed" },
301 { 413, "Request entity too large" },
302 { 414, "Request-URI too large" },
303 { 415, "Unsupported media Type" },
304 { 416, "Requested range not satisfiable" },
305 { 417, "Expectation failed" },
306 { 451, "Parameter not understood" },
307 { 452, "Conference not found" },
308 { 453, "Not enough bandwidth" },*/
309 { 454, "Session not found" },
310 /*{ 455, "Method not valid in this State" },*/
311 { 456, "Header field not valid for resource" },
312 /*{ 457, "Invalid range" },
313 { 458, "Read-only parameter" },*/
314 { 459, "Aggregate operation not allowed" },
315 { 460, "Non-aggregate operation not allowed" },
316 { 461, "Unsupported transport" },
317 /*{ 462, "Destination unreachable" },*/
318 { 500, "Internal server error" },
319 { 501, "Not implemented" },
320 /*{ 502, "Bad gateway" },*/
321 { 503, "Service unavailable" },
322 /*{ 504, "Gateway time-out" },*/
323 { 505, "Protocol version not supported" },
324 { 551, "Option not supported" },
328 static const char psz_fallback_reason[5][16] =
329 { "Continue", "OK", "Found", "Client error", "Server error" };
331 static const char *httpd_ReasonFromCode( unsigned i_code )
333 const http_status_info *p;
335 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
337 for (p = http_reason; i_code > p->i_code; p++);
339 if( p->i_code == i_code )
340 return p->psz_reason;
342 return psz_fallback_reason[(i_code / 100) - 1];
346 static size_t httpd_HtmlError (char **body, int code, const char *url)
348 const char *errname = httpd_ReasonFromCode (code);
349 assert (errname != NULL);
351 int res = asprintf (body,
352 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
353 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
354 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
355 "<html lang=\"en\">\n"
357 "<title>%s</title>\n"
360 "<h1>%d %s%s%s%s</h1>\n"
362 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
364 "</html>\n", errname, code, errname,
365 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
377 /*****************************************************************************
378 * High Level Functions: httpd_file_t
379 *****************************************************************************/
387 httpd_file_callback_t pf_fill;
388 httpd_file_sys_t *p_sys;
393 httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
394 httpd_message_t *answer, const httpd_message_t *query )
396 httpd_file_t *file = (httpd_file_t*)p_sys;
397 uint8_t *psz_args = query->psz_args;
398 uint8_t **pp_body, *p_body;
399 const char *psz_connection;
400 int *pi_body, i_body;
402 if( answer == NULL || query == NULL )
406 answer->i_proto = HTTPD_PROTO_HTTP;
407 answer->i_version= 1;
408 answer->i_type = HTTPD_MSG_ANSWER;
410 answer->i_status = 200;
412 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
413 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
415 if( query->i_type != HTTPD_MSG_HEAD )
417 pp_body = &answer->p_body;
418 pi_body = &answer->i_body;
422 /* The file still needs to be executed. */
429 if( query->i_type == HTTPD_MSG_POST )
431 /* msg_Warn not supported */
434 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
436 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
441 /* We respect client request */
442 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
443 if( psz_connection != NULL )
445 httpd_MsgAdd( answer, "Connection", "%s", psz_connection );
448 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
453 httpd_file_t *httpd_FileNew( httpd_host_t *host,
454 const char *psz_url, const char *psz_mime,
455 const char *psz_user, const char *psz_password,
456 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
457 httpd_file_sys_t *p_sys )
459 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
461 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
462 psz_password, p_acl )
469 file->psz_url = strdup( psz_url );
470 if( psz_mime && *psz_mime )
472 file->psz_mime = strdup( psz_mime );
476 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
479 file->pf_fill = pf_fill;
482 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
483 (httpd_callback_sys_t*)file );
484 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
485 (httpd_callback_sys_t*)file );
486 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
487 (httpd_callback_sys_t*)file );
492 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
494 httpd_file_sys_t *p_sys = file->p_sys;
496 httpd_UrlDelete( file->url );
498 free( file->psz_url );
499 free( file->psz_mime );
506 /*****************************************************************************
507 * High Level Functions: httpd_handler_t (for CGIs)
508 *****************************************************************************/
509 struct httpd_handler_t
513 httpd_handler_callback_t pf_fill;
514 httpd_handler_sys_t *p_sys;
519 httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
520 httpd_message_t *answer, const httpd_message_t *query )
522 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
523 uint8_t *psz_args = query->psz_args;
524 char psz_remote_addr[NI_MAXNUMERICHOST];
526 if( answer == NULL || query == NULL )
530 answer->i_proto = HTTPD_PROTO_NONE;
531 answer->i_type = HTTPD_MSG_ANSWER;
533 /* We do it ourselves, thanks */
534 answer->i_status = 0;
536 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
537 *psz_remote_addr = '\0';
539 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
540 query->i_type, query->p_body, query->i_body,
541 psz_remote_addr, NULL,
542 &answer->p_body, &answer->i_body );
544 if( query->i_type == HTTPD_MSG_HEAD )
546 char *p = (char *)answer->p_body;
548 /* Looks for end of header (i.e. one empty line) */
549 while ( (p = strchr( p, '\r' )) != NULL )
551 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
552 && p[3] && p[3] == '\n' )
561 answer->i_body = strlen((char*)answer->p_body) + 1;
562 answer->p_body = realloc( answer->p_body, answer->i_body );
566 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
568 int i_status, i_headers;
569 char *psz_headers, *psz_new;
570 const char *psz_status;
572 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
575 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
576 if( *psz_headers ) psz_headers++;
577 if( *psz_headers ) psz_headers++;
578 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
583 psz_headers = (char *)answer->p_body;
584 i_headers = answer->i_body;
587 psz_status = httpd_ReasonFromCode( i_status );
588 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
589 + strlen(psz_status) + i_headers - 1;
590 psz_new = (char *)malloc( answer->i_body + 1);
591 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
592 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
593 free( answer->p_body );
594 answer->p_body = (uint8_t *)psz_new;
600 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
601 const char *psz_user,
602 const char *psz_password,
603 const vlc_acl_t *p_acl,
604 httpd_handler_callback_t pf_fill,
605 httpd_handler_sys_t *p_sys )
607 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
609 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
610 psz_password, p_acl )
617 handler->pf_fill = pf_fill;
618 handler->p_sys = p_sys;
620 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
621 (httpd_callback_sys_t*)handler );
622 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
623 (httpd_callback_sys_t*)handler );
624 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
625 (httpd_callback_sys_t*)handler );
630 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
632 httpd_handler_sys_t *p_sys = handler->p_sys;
633 httpd_UrlDelete( handler->url );
638 /*****************************************************************************
639 * High Level Functions: httpd_redirect_t
640 *****************************************************************************/
641 struct httpd_redirect_t
647 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
648 httpd_client_t *cl, httpd_message_t *answer,
649 const httpd_message_t *query )
651 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
655 if( answer == NULL || query == NULL )
659 answer->i_proto = HTTPD_PROTO_HTTP;
660 answer->i_version= 1;
661 answer->i_type = HTTPD_MSG_ANSWER;
662 answer->i_status = 301;
664 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
665 answer->p_body = (unsigned char *)p_body;
667 /* XXX check if it's ok or we need to set an absolute url */
668 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
670 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
675 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
676 const char *psz_url_src )
678 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
680 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
685 rdir->psz_dst = strdup( psz_url_dst );
687 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
688 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
689 (httpd_callback_sys_t*)rdir );
690 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
691 (httpd_callback_sys_t*)rdir );
692 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
693 (httpd_callback_sys_t*)rdir );
694 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
695 (httpd_callback_sys_t*)rdir );
699 void httpd_RedirectDelete( httpd_redirect_t *rdir )
701 httpd_UrlDelete( rdir->url );
702 free( rdir->psz_dst );
706 /*****************************************************************************
707 * High Level Funtions: httpd_stream_t
708 *****************************************************************************/
709 struct httpd_stream_t
716 /* Header to send as first packet */
720 /* circular buffer */
721 int i_buffer_size; /* buffer size, can't be reallocated smaller */
722 uint8_t *p_buffer; /* buffer */
723 int64_t i_buffer_pos; /* absolute position from begining */
724 int64_t i_buffer_last_pos; /* a new connection will start with that */
727 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
728 httpd_client_t *cl, httpd_message_t *answer,
729 const httpd_message_t *query )
731 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
733 if( answer == NULL || query == NULL || cl == NULL )
738 if( answer->i_body_offset > 0 )
744 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
745 answer->i_body_offset );
748 if( answer->i_body_offset >= stream->i_buffer_pos )
750 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
751 return VLC_EGENERIC; /* wait, no data available */
753 if( answer->i_body_offset + stream->i_buffer_size <
754 stream->i_buffer_pos )
756 /* this client isn't fast enough */
758 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
759 answer->i_body_offset, stream->i_buffer_last_pos );
761 answer->i_body_offset = stream->i_buffer_last_pos;
764 i_pos = answer->i_body_offset % stream->i_buffer_size;
765 i_write = stream->i_buffer_pos - answer->i_body_offset;
766 if( i_write > HTTPD_CL_BUFSIZE )
768 i_write = HTTPD_CL_BUFSIZE;
770 else if( i_write <= 0 )
772 return VLC_EGENERIC; /* wait, no data available */
775 /* Don't go past the end of the circular buffer */
776 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
778 /* using HTTPD_MSG_ANSWER -> data available */
779 answer->i_proto = HTTPD_PROTO_HTTP;
780 answer->i_version= 0;
781 answer->i_type = HTTPD_MSG_ANSWER;
783 answer->i_body = i_write;
784 answer->p_body = malloc( i_write );
785 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
787 answer->i_body_offset += i_write;
793 answer->i_proto = HTTPD_PROTO_HTTP;
794 answer->i_version= 0;
795 answer->i_type = HTTPD_MSG_ANSWER;
797 answer->i_status = 200;
799 if( query->i_type != HTTPD_MSG_HEAD )
801 httpd_ClientModeStream( cl );
802 vlc_mutex_lock( &stream->lock );
803 /* Send the header */
804 if( stream->i_header > 0 )
806 answer->i_body = stream->i_header;
807 answer->p_body = malloc( stream->i_header );
808 memcpy( answer->p_body, stream->p_header, stream->i_header );
810 answer->i_body_offset = stream->i_buffer_last_pos;
811 vlc_mutex_unlock( &stream->lock );
815 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
816 answer->i_body_offset = 0;
819 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
821 bool b_xplaystream = false;
824 httpd_MsgAdd( answer, "Content-type", "%s",
825 "application/octet-stream" );
826 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
827 httpd_MsgAdd( answer, "Pragma", "no-cache" );
828 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
829 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
831 /* Check if there is a xPlayStrm=1 */
832 for( i = 0; i < query->i_name; i++ )
834 if( !strcasecmp( query->name[i], "Pragma" ) &&
835 strstr( query->value[i], "xPlayStrm=1" ) )
837 b_xplaystream = true;
843 answer->i_body_offset = 0;
848 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
850 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
855 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
856 const char *psz_url, const char *psz_mime,
857 const char *psz_user, const char *psz_password,
858 const vlc_acl_t *p_acl )
860 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
862 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
863 psz_password, p_acl )
869 vlc_mutex_init( &stream->lock );
870 if( psz_mime && *psz_mime )
872 stream->psz_mime = strdup( psz_mime );
876 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
878 stream->i_header = 0;
879 stream->p_header = NULL;
880 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
881 stream->p_buffer = malloc( stream->i_buffer_size );
882 /* We set to 1 to make life simpler
883 * (this way i_body_offset can never be 0) */
884 stream->i_buffer_pos = 1;
885 stream->i_buffer_last_pos = 1;
887 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
888 (httpd_callback_sys_t*)stream );
889 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
890 (httpd_callback_sys_t*)stream );
891 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
892 (httpd_callback_sys_t*)stream );
897 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
899 vlc_mutex_lock( &stream->lock );
900 free( stream->p_header );
901 stream->p_header = NULL;
903 stream->i_header = i_data;
906 stream->p_header = malloc( i_data );
907 memcpy( stream->p_header, p_data, i_data );
909 vlc_mutex_unlock( &stream->lock );
914 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
919 if( i_data < 0 || p_data == NULL )
923 vlc_mutex_lock( &stream->lock );
925 /* save this pointer (to be used by new connection) */
926 stream->i_buffer_last_pos = stream->i_buffer_pos;
928 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
934 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
936 /* Ok, we can't go past the end of our buffer */
937 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
939 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
944 stream->i_buffer_pos += i_data;
946 vlc_mutex_unlock( &stream->lock );
950 void httpd_StreamDelete( httpd_stream_t *stream )
952 httpd_UrlDelete( stream->url );
953 vlc_mutex_destroy( &stream->lock );
954 free( stream->psz_mime );
955 free( stream->p_header );
956 free( stream->p_buffer );
960 /*****************************************************************************
962 *****************************************************************************/
963 static void httpd_HostThread( httpd_host_t * );
965 /* create a new host */
966 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
969 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
973 static const char psz_object_type[] = "http server";
975 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
977 const char *psz_cert, const char *psz_key,
978 const char *psz_ca, const char *psz_crl )
984 vlc_value_t lockval, ptrval;
987 if( psz_hostname == NULL )
990 psz_host = strdup( psz_hostname );
991 if( psz_host == NULL )
993 msg_Err( p_this, "memory error" );
997 /* to be sure to avoid multiple creation */
998 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
999 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
1000 vlc_mutex_lock( lockval.p_address );
1001 httpd = libvlc_priv (p_this->p_libvlc)->p_httpd;
1005 msg_Info( p_this, "creating httpd" );
1006 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1011 vlc_mutex_unlock( lockval.p_address );
1019 ptrval.p_address = httpd;
1020 libvlc_priv (p_this->p_libvlc)->p_httpd = httpd;
1021 vlc_object_yield( httpd );
1022 vlc_object_attach( httpd, p_this->p_libvlc );
1025 /* verify if it already exist */
1026 for( i = httpd->i_host - 1; i >= 0; i-- )
1028 host = httpd->host[i];
1030 /* cannot mix TLS and non-TLS hosts */
1031 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1032 || ( host->i_port != i_port )
1033 || strcmp( host->psz_hostname, psz_hostname ) )
1039 vlc_mutex_unlock( lockval.p_address );
1045 /* determine TLS configuration */
1046 if ( psz_cert != NULL )
1048 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1049 if ( p_tls == NULL )
1051 msg_Err( p_this, "TLS initialization error" );
1055 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1057 msg_Err( p_this, "TLS CA error" );
1061 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1063 msg_Err( p_this, "TLS CRL error" );
1070 /* create the new host */
1071 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1072 VLC_OBJECT_HTTPD_HOST,
1077 vlc_object_lock( host );
1078 if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
1080 msg_Err( host, "signaling pipe error: %m" );
1081 vlc_object_unlock( host );
1084 vlc_object_unlock( host );
1086 host->httpd = httpd;
1087 vlc_mutex_init( &host->lock );
1090 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1091 if( host->fds == NULL )
1093 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1096 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1098 host->i_port = i_port;
1099 host->psz_hostname = psz_host;
1104 host->client = NULL;
1106 host->p_tls = p_tls;
1108 /* create the thread */
1109 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1110 VLC_THREAD_PRIORITY_LOW, false ) )
1112 msg_Err( p_this, "cannot spawn http host thread" );
1116 /* now add it to httpd */
1117 TAB_APPEND( httpd->i_host, httpd->host, host );
1118 vlc_mutex_unlock( lockval.p_address );
1124 if( httpd->i_host <= 0 )
1126 vlc_object_release( httpd );
1127 vlc_object_detach( httpd );
1128 vlc_object_release( httpd );
1130 vlc_mutex_unlock( lockval.p_address );
1134 net_ListenClose( host->fds );
1135 vlc_mutex_destroy( &host->lock );
1136 vlc_object_release( host );
1140 tls_ServerDelete( p_tls );
1146 void httpd_HostDelete( httpd_host_t *host )
1148 httpd_t *httpd = host->httpd;
1149 vlc_value_t lockval;
1152 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1153 vlc_mutex_lock( lockval.p_address );
1156 if( host->i_ref > 0 )
1159 vlc_mutex_unlock( lockval.p_address );
1160 msg_Dbg( host, "httpd_HostDelete: host still used" );
1163 TAB_REMOVE( httpd->i_host, httpd->host, host );
1165 vlc_object_kill( host );
1166 vlc_thread_join( host );
1168 msg_Dbg( host, "HTTP host removed" );
1170 for( i = 0; i < host->i_url; i++ )
1172 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1174 for( i = 0; i < host->i_client; i++ )
1176 httpd_client_t *cl = host->client[i];
1177 msg_Warn( host, "client still connected" );
1178 httpd_ClientClean( cl );
1179 TAB_REMOVE( host->i_client, host->client, cl );
1185 if( host->p_tls != NULL)
1186 tls_ServerDelete( host->p_tls );
1188 net_ListenClose( host->fds );
1189 free( host->psz_hostname );
1191 vlc_mutex_destroy( &host->lock );
1192 vlc_object_release( host );
1194 vlc_object_release( httpd );
1195 if( httpd->i_host <= 0 )
1197 msg_Dbg( httpd, "no host left, stopping httpd" );
1199 libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
1200 vlc_object_detach( httpd );
1201 vlc_object_release( httpd );
1204 vlc_mutex_unlock( lockval.p_address );
1207 /* register a new url */
1208 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1209 const char *psz_user, const char *psz_password,
1210 const vlc_acl_t *p_acl, bool b_check )
1215 assert( psz_url != NULL );
1217 vlc_mutex_lock( &host->lock );
1220 for( i = 0; i < host->i_url; i++ )
1222 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1224 msg_Warn( host->httpd,
1225 "cannot add '%s' (url already defined)", psz_url );
1226 vlc_mutex_unlock( &host->lock );
1232 url = malloc( sizeof( httpd_url_t ) );
1235 vlc_mutex_init( &url->lock );
1236 url->psz_url = strdup( psz_url );
1237 url->psz_user = strdup( psz_user ? psz_user : "" );
1238 url->psz_password = strdup( psz_password ? psz_password : "" );
1239 url->p_acl = ACL_Duplicate( host, p_acl );
1240 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1242 url->catch[i].cb = NULL;
1243 url->catch[i].p_sys = NULL;
1246 TAB_APPEND( host->i_url, host->url, url );
1247 vlc_mutex_unlock( &host->lock );
1252 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1253 const char *psz_user, const char *psz_password,
1254 const vlc_acl_t *p_acl )
1256 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1257 psz_password, p_acl, false );
1260 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1261 const char *psz_user, const char *psz_password,
1262 const vlc_acl_t *p_acl )
1264 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1265 psz_password, p_acl, true );
1268 /* register callback on a url */
1269 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1270 httpd_callback_sys_t *p_sys )
1272 vlc_mutex_lock( &url->lock );
1273 url->catch[i_msg].cb = cb;
1274 url->catch[i_msg].p_sys= p_sys;
1275 vlc_mutex_unlock( &url->lock );
1281 void httpd_UrlDelete( httpd_url_t *url )
1283 httpd_host_t *host = url->host;
1286 vlc_mutex_lock( &host->lock );
1287 TAB_REMOVE( host->i_url, host->url, url );
1289 vlc_mutex_destroy( &url->lock );
1290 free( url->psz_url );
1291 free( url->psz_user );
1292 free( url->psz_password );
1293 ACL_Destroy( url->p_acl );
1295 for( i = 0; i < host->i_client; i++ )
1297 httpd_client_t *client = host->client[i];
1299 if( client->url == url )
1301 /* TODO complete it */
1302 msg_Warn( host, "force closing connections" );
1303 httpd_ClientClean( client );
1304 TAB_REMOVE( host->i_client, host->client, client );
1310 vlc_mutex_unlock( &host->lock );
1313 void httpd_MsgInit( httpd_message_t *msg )
1316 msg->i_type = HTTPD_MSG_NONE;
1317 msg->i_proto = HTTPD_PROTO_NONE;
1318 msg->i_version = -1; /* FIXME */
1322 msg->psz_url = NULL;
1323 msg->psz_args = NULL;
1325 msg->i_channel = -1;
1332 msg->i_body_offset = 0;
1337 void httpd_MsgClean( httpd_message_t *msg )
1341 free( msg->psz_url );
1342 free( msg->psz_args );
1343 for( i = 0; i < msg->i_name; i++ )
1345 free( msg->name[i] );
1346 free( msg->value[i] );
1350 free( msg->p_body );
1351 httpd_MsgInit( msg );
1354 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1358 for( i = 0; i < msg->i_name; i++ )
1360 if( !strcasecmp( msg->name[i], name ))
1362 return msg->value[i];
1368 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1373 va_start( args, psz_value );
1374 if( vasprintf( &value, psz_value, args ) == -1 )
1381 name = strdup( name );
1388 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1389 TAB_APPEND( msg->i_value, msg->value, value );
1392 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1394 cl->i_state = HTTPD_CLIENT_RECEIVING;
1395 cl->i_activity_date = now;
1396 cl->i_activity_timeout = INT64_C(10000000);
1397 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1399 cl->p_buffer = malloc( cl->i_buffer_size );
1400 cl->i_mode = HTTPD_CLIENT_FILE;
1401 cl->b_read_waiting = false;
1403 httpd_MsgInit( &cl->query );
1404 httpd_MsgInit( &cl->answer );
1407 void httpd_ClientModeStream( httpd_client_t *cl )
1409 cl->i_mode = HTTPD_CLIENT_STREAM;
1412 void httpd_ClientModeBidir( httpd_client_t *cl )
1414 cl->i_mode = HTTPD_CLIENT_BIDIR;
1417 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1419 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1422 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1424 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1427 static void httpd_ClientClean( httpd_client_t *cl )
1431 if( cl->p_tls != NULL )
1432 tls_ServerSessionClose( cl->p_tls );
1433 net_Close( cl->fd );
1437 httpd_MsgClean( &cl->answer );
1438 httpd_MsgClean( &cl->query );
1440 free( cl->p_buffer );
1441 cl->p_buffer = NULL;
1444 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1446 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1448 if( !cl ) return NULL;
1455 httpd_ClientInit( cl, now );
1460 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1462 tls_session_t *p_tls;
1466 return tls_Recv( p_tls, p, i_len );
1468 return recv( cl->fd, p, i_len, 0 );
1471 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1473 tls_session_t *p_tls;
1477 return tls_Send( p_tls, p, i_len );
1479 return send( cl->fd, p, i_len, 0 );
1485 const char name[16];
1491 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1492 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1493 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1494 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1495 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1496 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1497 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1498 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1499 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1500 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1501 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1505 static void httpd_ClientRecv( httpd_client_t *cl )
1509 /* ignore leading whites */
1510 if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
1511 ( cl->i_buffer == 0 ) )
1515 i_len = httpd_NetRecv( cl, &c, 1 );
1517 if( ( i_len > 0 ) && ( strchr( "\r\n\t ", c ) == NULL ) )
1519 cl->p_buffer[0] = c;
1524 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1526 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1527 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1531 cl->i_buffer += i_len;
1534 if( ( cl->i_buffer >= 4 ) && ( cl->p_buffer[0] == '$' ) )
1536 /* Interleaved RTP over RTSP */
1537 cl->query.i_proto = HTTPD_PROTO_RTSP;
1538 cl->query.i_type = HTTPD_MSG_CHANNEL;
1539 cl->query.i_channel = cl->p_buffer[1];
1540 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1541 cl->query.p_body = malloc( cl->query.i_body );
1543 memcpy( cl->query.p_body, cl->p_buffer + 4, cl->i_buffer );
1546 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1547 * this is the maximum we can ask at this point. */
1548 if( cl->i_buffer >= 7 )
1550 if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
1552 cl->query.i_proto = HTTPD_PROTO_HTTP;
1553 cl->query.i_type = HTTPD_MSG_ANSWER;
1555 else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
1557 cl->query.i_proto = HTTPD_PROTO_RTSP;
1558 cl->query.i_type = HTTPD_MSG_ANSWER;
1562 /* We need the full request line to determine the protocol. */
1563 cl->query.i_proto = HTTPD_PROTO_HTTP0;
1564 cl->query.i_type = HTTPD_MSG_NONE;
1568 else if( cl->query.i_body > 0 )
1570 /* we are reading the body of a request or a channel */
1571 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1572 cl->query.i_body - cl->i_buffer );
1575 cl->i_buffer += i_len;
1577 if( cl->i_buffer >= cl->query.i_body )
1579 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1584 /* we are reading a header -> char by char */
1587 if( cl->i_buffer == cl->i_buffer_size )
1589 uint8_t *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
1590 if( newbuf == NULL )
1596 cl->p_buffer = newbuf;
1597 cl->i_buffer_size += 1024;
1600 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1607 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP0 )
1608 && ( cl->p_buffer[cl->i_buffer - 1] == '\n' ) )
1610 /* Request line is now complete */
1611 const char *p = memchr( cl->p_buffer, ' ', cl->i_buffer );
1614 assert( cl->query.i_type == HTTPD_MSG_NONE );
1616 if( p == NULL ) /* no URI: evil guy */
1618 i_len = 0; /* drop connection */
1623 p++; /* skips extra spaces */
1626 p = memchr( p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p );
1627 if( p == NULL ) /* no explicit protocol: HTTP/0.9 */
1629 i_len = 0; /* not supported currently -> drop */
1634 p++; /* skips extra spaces ever again */
1637 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
1638 if( len < 7 ) /* foreign protocol */
1639 i_len = 0; /* I don't understand -> drop */
1641 if( memcmp( p, "HTTP/1.", 7 ) == 0 )
1643 cl->query.i_proto = HTTPD_PROTO_HTTP;
1644 cl->query.i_version = atoi( p + 7 );
1647 if( memcmp( p, "RTSP/1.", 7 ) == 0 )
1649 cl->query.i_proto = HTTPD_PROTO_RTSP;
1650 cl->query.i_version = atoi( p + 7 );
1653 if( memcmp( p, "HTTP/", 5 ) == 0 )
1655 const uint8_t sorry[] =
1656 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1657 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1658 i_len = 0; /* drop */
1661 if( memcmp( p, "RTSP/", 5 ) == 0 )
1663 const uint8_t sorry[] =
1664 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1665 httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
1666 i_len = 0; /* drop */
1668 else /* yet another foreign protocol */
1675 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1676 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1680 /* we have finished the header so parse it and set i_body */
1681 cl->p_buffer[cl->i_buffer] = '\0';
1683 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1686 * assume strlen( "HTTP/1.x" ) = 8
1688 cl->query.i_status =
1689 strtol( (char *)&cl->p_buffer[8],
1699 cl->query.i_type = HTTPD_MSG_NONE;
1701 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1703 for( i = 0; msg_type[i].name[0]; i++ )
1705 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1706 strlen( msg_type[i].name ) ) )
1708 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1709 cl->query.i_type = msg_type[i].i_type;
1710 if( cl->query.i_proto != msg_type[i].i_proto )
1713 cl->query.i_proto = HTTPD_PROTO_NONE;
1714 cl->query.i_type = HTTPD_MSG_NONE;
1721 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1723 cl->query.i_proto = HTTPD_PROTO_HTTP;
1725 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1727 cl->query.i_proto = HTTPD_PROTO_RTSP;
1739 p2 = strchr( p, ' ' );
1744 if( !strncasecmp( p, "rtsp:", 5 ) )
1746 /* for rtsp url, you have rtsp://localhost:port/path */
1748 while( *p == '/' ) p++;
1749 while( *p && *p != '/' ) p++;
1751 cl->query.psz_url = strdup( p );
1752 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1755 cl->query.psz_args = (uint8_t *)strdup( p3 );
1762 p = strchr( p, '\n' );
1766 while( *p == '\n' || *p == '\r' )
1770 while( p && *p != '\0' )
1773 char *eol = p = strchr( p, '\n' );
1776 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1781 if( ( colon = strchr( line, ':' ) ) )
1787 while( *colon == ' ' )
1791 name = strdup( line );
1792 value = strdup( colon );
1794 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1795 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1797 if( !strcasecmp( name, "Content-Length" ) )
1799 cl->query.i_body = atol( value );
1806 while( *p == '\n' || *p == '\r' )
1813 if( cl->query.i_body > 0 )
1815 /* TODO Mhh, handle the case client will only send a
1816 * request and close the connection
1817 * to mark and of body (probably only RTSP) */
1818 cl->query.p_body = malloc( cl->query.i_body );
1823 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1829 /* check if the client is to be set to dead */
1830 #if defined( WIN32 ) || defined( UNDER_CE )
1831 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1833 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1836 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1838 /* connection closed -> end of data */
1839 if( cl->query.i_body > 0 )
1841 cl->query.i_body = cl->i_buffer;
1843 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1847 cl->i_state = HTTPD_CLIENT_DEAD;
1851 /* XXX: for QT I have to disable timeout. Try to find why */
1852 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1853 cl->i_activity_timeout = 0;
1855 #if 0 /* Debugging only */
1856 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1860 fprintf( stderr, "received new request\n" );
1861 fprintf( stderr, " - proto=%s\n",
1862 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1863 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1864 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1865 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1867 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1868 cl->query.psz_status );
1870 else if( cl->query.i_type != HTTPD_MSG_NONE )
1872 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1874 for( i = 0; i < cl->query.i_name; i++ )
1876 fprintf( stderr, " - option name='%s' value='%s'\n",
1877 cl->query.name[i], cl->query.value[i] );
1883 static void httpd_ClientSend( httpd_client_t *cl )
1888 if( cl->i_buffer < 0 )
1890 /* We need to create the header */
1893 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1895 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1896 for( i = 0; i < cl->answer.i_name; i++ )
1898 i_size += strlen( cl->answer.name[i] ) + 2 +
1899 strlen( cl->answer.value[i] ) + 2;
1902 if( cl->i_buffer_size < i_size )
1904 cl->i_buffer_size = i_size;
1905 free( cl->p_buffer );
1906 cl->p_buffer = malloc( i_size );
1908 p = (char *)cl->p_buffer;
1910 p += sprintf( p, "%s.%u %d %s\r\n",
1911 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1912 cl->answer.i_version,
1913 cl->answer.i_status, psz_status );
1914 for( i = 0; i < cl->answer.i_name; i++ )
1916 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1917 cl->answer.value[i] );
1919 p += sprintf( p, "\r\n" );
1922 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1924 /*fprintf( stderr, "sending answer\n" );
1925 fprintf( stderr, "%s", cl->p_buffer );*/
1928 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1929 cl->i_buffer_size - cl->i_buffer );
1932 cl->i_buffer += i_len;
1934 if( cl->i_buffer >= cl->i_buffer_size )
1936 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1937 !cl->b_read_waiting )
1939 /* catch more body data */
1940 int i_msg = cl->query.i_type;
1941 int64_t i_offset = cl->answer.i_body_offset;
1943 httpd_MsgClean( &cl->answer );
1944 cl->answer.i_body_offset = i_offset;
1946 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1947 &cl->answer, &cl->query );
1950 if( cl->answer.i_body > 0 )
1952 /* send the body data */
1953 free( cl->p_buffer );
1954 cl->p_buffer = cl->answer.p_body;
1955 cl->i_buffer_size = cl->answer.i_body;
1958 cl->answer.i_body = 0;
1959 cl->answer.p_body = NULL;
1964 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1970 #if defined( WIN32 ) || defined( UNDER_CE )
1971 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1973 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1977 cl->i_state = HTTPD_CLIENT_DEAD;
1982 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1984 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1987 cl->i_state = HTTPD_CLIENT_RECEIVING;
1991 cl->i_state = HTTPD_CLIENT_DEAD;
1996 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2000 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2002 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2005 cl->i_state = HTTPD_CLIENT_RECEIVING;
2009 cl->i_state = HTTPD_CLIENT_DEAD;
2014 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2019 static void httpd_HostThread( httpd_host_t *host )
2021 tls_session_t *p_tls = NULL;
2022 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
2023 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
2028 vlc_object_lock( host );
2029 evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
2030 b_die = !vlc_object_alive( host );
2031 vlc_object_unlock( host );
2035 if( host->i_url <= 0 )
2037 /* 0.2s (FIXME: use a condition variable) */
2042 /* prepare a new TLS session */
2043 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2044 p_tls = tls_ServerSessionPrepare( host->p_tls );
2046 struct pollfd ufd[host->nfd + host->i_client + 1];
2048 for( nfd = 0; nfd < host->nfd; nfd++ )
2050 ufd[nfd].fd = host->fds[nfd];
2051 ufd[nfd].events = POLLIN;
2052 ufd[nfd].revents = 0;
2055 /* add all socket that should be read/write and close dead connection */
2056 vlc_mutex_lock( &host->lock );
2057 mtime_t now = mdate();
2058 bool b_low_delay = false;
2060 for(int i_client = 0; i_client < host->i_client; i_client++ )
2062 httpd_client_t *cl = host->client[i_client];
2063 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2064 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2065 ( cl->i_activity_timeout > 0 &&
2066 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2068 httpd_ClientClean( cl );
2069 stats_UpdateInteger( host, p_active_counter, -1, NULL );
2070 TAB_REMOVE( host->i_client, host->client, cl );
2076 struct pollfd *pufd = ufd + nfd;
2077 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2080 pufd->events = pufd->revents = 0;
2082 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2083 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2085 pufd->events = POLLIN;
2087 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2088 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2090 pufd->events = POLLOUT;
2092 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2094 httpd_message_t *answer = &cl->answer;
2095 httpd_message_t *query = &cl->query;
2096 int i_msg = query->i_type;
2098 httpd_MsgInit( answer );
2100 /* Handle what we received */
2101 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2102 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2104 /* we can only receive request from client when not
2107 cl->i_state = HTTPD_CLIENT_DEAD;
2109 else if( i_msg == HTTPD_MSG_ANSWER )
2111 /* We are in BIDIR mode, trigger the callback and then
2112 * check for new data */
2113 if( cl->url && cl->url->catch[i_msg].cb )
2115 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2118 cl->i_state = HTTPD_CLIENT_WAITING;
2120 else if( i_msg == HTTPD_MSG_CHANNEL )
2122 /* We are in BIDIR mode, trigger the callback and then
2123 * check for new data */
2124 if( cl->url && cl->url->catch[i_msg].cb )
2126 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2129 cl->i_state = HTTPD_CLIENT_WAITING;
2131 else if( i_msg == HTTPD_MSG_OPTIONS )
2134 answer->i_type = HTTPD_MSG_ANSWER;
2135 answer->i_proto = query->i_proto;
2136 answer->i_status = 200;
2138 answer->p_body = NULL;
2140 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
2141 httpd_MsgAdd( answer, "Content-Length", "0" );
2143 switch( query->i_proto )
2145 case HTTPD_PROTO_HTTP:
2146 answer->i_version = 1;
2147 httpd_MsgAdd( answer, "Allow",
2148 "GET,HEAD,POST,OPTIONS" );
2151 case HTTPD_PROTO_RTSP:
2154 answer->i_version = 0;
2156 p = httpd_MsgGet( query, "Cseq" );
2158 httpd_MsgAdd( answer, "Cseq", "%s", p );
2159 p = httpd_MsgGet( query, "Timestamp" );
2161 httpd_MsgAdd( answer, "Timestamp", "%s", p );
2163 p = httpd_MsgGet( query, "Require" );
2166 answer->i_status = 551;
2167 httpd_MsgAdd( query, "Unsupported", "%s", p );
2170 httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
2171 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
2176 cl->i_buffer = -1; /* Force the creation of the answer in
2177 * httpd_ClientSend */
2178 cl->i_state = HTTPD_CLIENT_SENDING;
2180 else if( i_msg == HTTPD_MSG_NONE )
2182 if( query->i_proto == HTTPD_PROTO_NONE )
2185 cl->i_state = HTTPD_CLIENT_DEAD;
2192 answer->i_proto = query->i_proto ;
2193 answer->i_type = HTTPD_MSG_ANSWER;
2194 answer->i_version= 0;
2195 answer->i_status = 501;
2197 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2198 answer->p_body = (uint8_t *)p;
2199 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2201 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2202 cl->i_state = HTTPD_CLIENT_SENDING;
2207 bool b_auth_failed = false;
2208 bool b_hosts_failed = false;
2210 /* Search the url and trigger callbacks */
2211 for(int i = 0; i < host->i_url; i++ )
2213 httpd_url_t *url = host->url[i];
2215 if( !strcmp( url->psz_url, query->psz_url ) )
2217 if( url->catch[i_msg].cb )
2219 if( answer && ( url->p_acl != NULL ) )
2221 char ip[NI_MAXNUMERICHOST];
2223 if( ( httpd_ClientIP( cl, ip ) == NULL )
2224 || ACL_Check( url->p_acl, ip ) )
2226 b_hosts_failed = true;
2231 if( answer && ( *url->psz_user || *url->psz_password ) )
2233 /* create the headers */
2234 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2235 char *user = NULL, *pass = NULL;
2238 && !strncasecmp( b64, "BASIC", 5 ) )
2241 while( *b64 == ' ' )
2244 user = vlc_b64_decode( b64 );
2247 pass = strchr (user, ':');
2253 if ((user == NULL) || (pass == NULL)
2254 || strcmp (user, url->psz_user)
2255 || strcmp (pass, url->psz_password))
2257 httpd_MsgAdd( answer,
2259 "Basic realm=\"%s\"",
2261 /* We fail for all url */
2262 b_auth_failed = true;
2270 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2272 if( answer->i_proto == HTTPD_PROTO_NONE )
2274 /* Raw answer from a CGI */
2275 cl->i_buffer = cl->i_buffer_size;
2280 /* only one url can answer */
2282 if( cl->url == NULL )
2295 answer->i_proto = query->i_proto;
2296 answer->i_type = HTTPD_MSG_ANSWER;
2297 answer->i_version= 0;
2299 if( b_hosts_failed )
2301 answer->i_status = 403;
2303 else if( b_auth_failed )
2305 answer->i_status = 401;
2309 /* no url registered */
2310 answer->i_status = 404;
2313 answer->i_body = httpd_HtmlError (&p,
2316 answer->p_body = (uint8_t *)p;
2318 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2319 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2320 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2323 cl->i_state = HTTPD_CLIENT_SENDING;
2326 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2328 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2330 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2331 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2332 bool b_connection = false;
2333 bool b_keepalive = false;
2334 bool b_query = false;
2337 if( psz_connection )
2339 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2340 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2345 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2348 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2349 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2350 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2351 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2352 !b_query && !b_connection ) )
2354 httpd_MsgClean( &cl->query );
2355 httpd_MsgInit( &cl->query );
2358 cl->i_buffer_size = 1000;
2359 free( cl->p_buffer );
2360 cl->p_buffer = malloc( cl->i_buffer_size );
2361 cl->i_state = HTTPD_CLIENT_RECEIVING;
2365 cl->i_state = HTTPD_CLIENT_DEAD;
2367 httpd_MsgClean( &cl->answer );
2369 else if( cl->b_read_waiting )
2371 /* we have a message waiting for us to read it */
2372 httpd_MsgClean( &cl->answer );
2373 httpd_MsgClean( &cl->query );
2376 cl->i_buffer_size = 1000;
2377 free( cl->p_buffer );
2378 cl->p_buffer = malloc( cl->i_buffer_size );
2379 cl->i_state = HTTPD_CLIENT_RECEIVING;
2380 cl->b_read_waiting = false;
2384 int64_t i_offset = cl->answer.i_body_offset;
2385 httpd_MsgClean( &cl->answer );
2387 cl->answer.i_body_offset = i_offset;
2388 free( cl->p_buffer );
2389 cl->p_buffer = NULL;
2391 cl->i_buffer_size = 0;
2393 cl->i_state = HTTPD_CLIENT_WAITING;
2396 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2398 int64_t i_offset = cl->answer.i_body_offset;
2399 int i_msg = cl->query.i_type;
2401 httpd_MsgInit( &cl->answer );
2402 cl->answer.i_body_offset = i_offset;
2404 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2405 &cl->answer, &cl->query );
2406 if( cl->answer.i_type != HTTPD_MSG_NONE )
2408 /* we have new data, so re-enter send mode */
2410 cl->p_buffer = cl->answer.p_body;
2411 cl->i_buffer_size = cl->answer.i_body;
2412 cl->answer.p_body = NULL;
2413 cl->answer.i_body = 0;
2414 cl->i_state = HTTPD_CLIENT_SENDING;
2418 /* Special for BIDIR mode we also check reading */
2419 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2420 cl->i_state == HTTPD_CLIENT_SENDING )
2422 pufd->events |= POLLIN;
2425 if (pufd->events != 0)
2430 vlc_mutex_unlock( &host->lock );
2433 ufd[nfd].events = POLLIN;
2434 ufd[nfd].revents = 0;
2437 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2438 switch( poll( ufd, nfd, b_low_delay ? 20 : -1) )
2443 /* Kernel on low memory or a bug: pace */
2444 msg_Err( host, "polling error: %m" );
2451 vlc_object_lock( host );
2452 if( ufd[nfd - 1].revents )
2454 b_die = !vlc_object_alive( host );
2456 b_die = vlc_object_wait( host );
2458 vlc_object_unlock( host );
2460 /* Handle client sockets */
2461 vlc_mutex_lock( &host->lock );
2463 for( int i_client = 0; i_client < host->i_client; i_client++ )
2465 httpd_client_t *cl = host->client[i_client];
2466 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2468 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2470 if( cl->fd != pufd->fd )
2471 continue; // we were not waiting for this client
2472 if( pufd->revents == 0 )
2473 continue; // no event received
2475 cl->i_activity_date = now;
2477 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2479 httpd_ClientRecv( cl );
2481 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2483 httpd_ClientSend( cl );
2485 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2487 httpd_ClientTlsHsIn( cl );
2489 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2491 httpd_ClientTlsHsOut( cl );
2494 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2495 cl->i_state == HTTPD_CLIENT_SENDING &&
2496 (pufd->revents & POLLIN) )
2498 cl->b_read_waiting = true;
2501 vlc_mutex_unlock( &host->lock );
2503 /* Handle server sockets (accept new connections) */
2504 for( nfd = 0; nfd < host->nfd; nfd++ )
2508 int fd = ufd[nfd].fd;
2510 assert (fd == host->fds[nfd]);
2512 if( ufd[nfd].revents == 0 )
2516 fd = accept (fd, NULL, NULL);
2520 net_SetupSocket (fd);
2523 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2526 msg_Err( host, "Rejecting TLS connection" );
2532 case 1: /* missing input - most likely */
2533 i_state = HTTPD_CLIENT_TLS_HS_IN;
2536 case 2: /* missing output */
2537 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2541 if( (p_tls == NULL) != (host->p_tls == NULL) )
2542 break; // wasted TLS session, cannot accept() anymore
2545 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2546 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2547 cl = httpd_ClientNew( fd, p_tls, now );
2549 vlc_mutex_lock( &host->lock );
2550 TAB_APPEND( host->i_client, host->client, cl );
2551 vlc_mutex_unlock( &host->lock );
2553 cl->i_state = i_state; // override state for TLS
2555 if (host->p_tls != NULL)
2556 break; // cannot accept further without new TLS session
2562 tls_ServerSessionClose( p_tls );
2563 if( p_total_counter )
2564 stats_CounterClean( p_total_counter );
2565 if( p_active_counter )
2566 stats_CounterClean( p_active_counter );
2569 #else /* ENABLE_HTTPD */
2571 /* We just define an empty wrapper */
2572 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, const char *b,
2574 const char *e, const char *f,
2575 const char *g, const char* h)
2577 msg_Err( a, "HTTP daemon support is disabled" );
2581 httpd_host_t *httpd_HostNew( vlc_object_t *a, const char *b,
2584 msg_Err( a, "HTTP daemon support is disabled" );
2588 void httpd_HostDelete( httpd_host_t *a )
2592 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
2593 const char *psz_user, const char *psz_password,
2594 const vlc_acl_t *p_acl )
2599 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
2600 const char *psz_user, const char *psz_password,
2601 const vlc_acl_t *p_acl )
2606 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2607 httpd_callback_sys_t *d )
2612 void httpd_UrlDelete( httpd_url_t *a )
2616 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
2621 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
2626 void httpd_ClientModeStream( httpd_client_t *a )
2630 void httpd_ClientModeBidir( httpd_client_t *a )
2634 httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
2639 httpd_file_t *httpd_FileNew( httpd_host_t *host,
2640 const char *psz_url, const char *psz_mime,
2641 const char *psz_user, const char *psz_password,
2642 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
2643 httpd_file_sys_t *p_sys )
2648 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2649 const char *psz_user,
2650 const char *psz_password,
2651 const vlc_acl_t *p_acl,
2652 httpd_handler_callback_t pf_fill,
2653 httpd_handler_sys_t *p_sys )
2658 httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
2663 void httpd_RedirectDelete( httpd_redirect_t *a )
2667 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
2668 const char *psz_url_src )
2673 void httpd_StreamDelete( httpd_stream_t *a )
2677 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2682 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2687 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
2688 const char *psz_url, const char *psz_mime,
2689 const char *psz_user, const char *psz_password,
2690 const vlc_acl_t *p_acl )
2695 void httpd_MsgInit ( httpd_message_t *a )
2699 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2703 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
2708 void httpd_MsgClean( httpd_message_t *a )
2712 #endif /* ENABLE_HTTPD */