1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
34 #include <vlc_httpd.h>
35 #include <vlc_network.h>
38 #include "../libvlc.h"
55 #if defined( UNDER_CE )
57 #elif defined( WIN32 )
58 # include <winsock2.h>
60 # include <sys/socket.h>
64 /* We need HUGE buffer otherwise TCP throughput is very limited */
65 #define HTTPD_CL_BUFSIZE 1000000
67 #define HTTPD_CL_BUFSIZE 10000
70 static void httpd_ClientClean( httpd_client_t *cl );
81 /* each host run in his own thread */
91 /* address/port and socket for listening at connections */
98 counter_t *p_active_counter;
99 counter_t *p_total_counter;
103 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
104 * This will slow down the url research but make my live easier
105 * All url will have their cb trigger, but only the first one can answer
111 httpd_client_t **client;
132 httpd_callback_sys_t *p_sys;
133 } catch[HTTPD_MSG_MAX];
139 HTTPD_CLIENT_RECEIVING,
140 HTTPD_CLIENT_RECEIVE_DONE,
142 HTTPD_CLIENT_SENDING,
143 HTTPD_CLIENT_SEND_DONE,
145 HTTPD_CLIENT_WAITING,
149 HTTPD_CLIENT_TLS_HS_IN,
150 HTTPD_CLIENT_TLS_HS_OUT
156 HTTPD_CLIENT_FILE, /* default */
157 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
158 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
161 struct httpd_client_t
171 int b_read_waiting; /* stop as soon as possible sending */
173 mtime_t i_activity_date;
174 mtime_t i_activity_timeout;
176 /* buffer for reading header */
182 httpd_message_t query; /* client -> httpd */
183 httpd_message_t answer; /* httpd -> client */
186 tls_session_t *p_tls;
190 /*****************************************************************************
192 *****************************************************************************/
193 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
194 static void b64_decode( char *restrict dest, const char *restrict src )
199 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
200 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
201 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
202 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
203 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
204 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
205 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
206 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
207 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
208 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
209 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
210 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
211 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
212 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
213 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
214 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
217 for( i_level = 0; *src != '\0'; src++ )
221 c = b64[(unsigned int)*src];
233 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
237 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
241 *dest++ = ( ( last &0x03 ) << 6 ) | c;
253 const char *psz_mime;
256 { ".htm", "text/html" },
257 { ".html", "text/html" },
258 { ".txt", "text/plain" },
259 { ".xml", "text/xml" },
260 { ".dtd", "text/dtd" },
262 { ".css", "text/css" },
265 { ".gif", "image/gif" },
266 { ".jpe", "image/jpeg" },
267 { ".jpg", "image/jpeg" },
268 { ".jpeg", "image/jpeg" },
269 { ".png", "image/png" },
270 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
273 { ".avi", "video/avi" },
274 { ".asf", "video/x-ms-asf" },
275 { ".m1a", "audio/mpeg" },
276 { ".m2a", "audio/mpeg" },
277 { ".m1v", "video/mpeg" },
278 { ".m2v", "video/mpeg" },
279 { ".mp2", "audio/mpeg" },
280 { ".mp3", "audio/mpeg" },
281 { ".mpa", "audio/mpeg" },
282 { ".mpg", "video/mpeg" },
283 { ".mpeg", "video/mpeg" },
284 { ".mpe", "video/mpeg" },
285 { ".mov", "video/quicktime" },
286 { ".moov", "video/quicktime" },
287 { ".ogg", "application/ogg" },
288 { ".ogm", "application/ogg" },
289 { ".wav", "audio/wav" },
290 { ".wma", "audio/x-ms-wma" },
291 { ".wmv", "video/x-ms-wmv" },
298 static const char *httpd_MimeFromUrl( const char *psz_url )
303 psz_ext = strrchr( psz_url, '.' );
308 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
310 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
312 return http_mime[i].psz_mime;
316 return "application/octet-stream";
323 const char *psz_reason;
326 static const http_status_info http_reason[] =
328 /*{ 100, "Continue" },
329 { 101, "Switching Protocols" },*/
333 { 203, "Non-Authoritative Information" },
334 { 204, "No Content" },
335 { 205, "Reset Content"Â },
336 { 206, "Partial Content" },
337 { 250, "Low on Storage Space" },
338 { 300, "Multiple Choices" }*/,
339 { 301, "Moved Permanently" }/*,
340 { 302, "Moved Temporarily" }, - aka "Found"
341 { 303, "See Other" },
342 { 304, "Not Modified" },
343 { 305, "Use Proxy" },
344 { 307, "Temporary Redirect" },
345 { 400, "Bad Request" }*/,
346 { 401, "Unauthorized" }/*,
347 { 402, "Payment Required" }*/,
348 { 403, "Forbidden" },
349 { 404, "Not Found" }/*,
350 { 405, "Method Not Allowed" },
351 { 406, "Not Acceptable" },
352 { 407, "Proxy Authentication Required" },
353 { 408, "Request Time-out" },
356 { 411, "Length Required" },
357 { 412, "Precondition Failed" },
358 { 413, "Request Entity Too Large" },
359 { 414, "Request-URI Too Large" },
360 { 415, "Unsupported Media Type" },
361 { 416, "Requested range not satisfiable" },
362 { 417, "Expectation Failed" },
363 { 451, "Parameter Not Understood" },
364 { 452, "Conference Not Found" },
365 { 453, "Not Enough Bandwidth" }*/,
366 { 454, "Session Not Found" }/*,
367 { 455, "Method Not Valid in This State" },
368 { 456, "Header Field Not Valid for Resource" },
369 { 457, "Invalid Range" },
370 { 458, "Parameter Is Read-Only" },
371 { 459, "Aggregate operation not allowed" },
372 { 460, "Only aggregate operation allowed" }*/,
373 { 461, "Unsupported transport" }/*,
374 { 462, "Destination unreachable" }*/,
375 { 500, "Internal Server Error" },
376 { 501, "Not Implemented" }/*,
377 { 502, "Bad Gateway" }*/,
378 { 503, "Service Unavailable" }/*,
379 { 504, "Gateway Time-out" },
380 { 505, "Protocol version not supported" }*/,
384 static const char *psz_fallback_reason[] =
385 { "Continue", "OK", "Found", "Client Error", "Server Error" };
387 static const char *httpd_ReasonFromCode( int i_code )
389 const http_status_info *p;
391 for (p = http_reason; p->i_code < i_code; p++);
393 if( p->i_code == i_code )
394 return p->psz_reason;
396 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
397 return psz_fallback_reason[(i_code / 100) - 1];
401 static size_t httpd_HtmlError (char **body, int code, const char *url)
403 const char *errname = httpd_ReasonFromCode (code);
404 assert (errname != NULL);
406 int res = asprintf (body,
407 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
408 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
409 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
410 "<html lang=\"en\">\n"
412 "<title>%s</title>\n"
415 "<h1>%d %s%s%s%s</h1>\n"
417 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
419 "</html>\n", errname, code, errname,
420 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
432 /*****************************************************************************
433 * High Level Functions: httpd_file_t
434 *****************************************************************************/
442 httpd_file_callback_t pf_fill;
443 httpd_file_sys_t *p_sys;
447 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
449 httpd_file_t *file = (httpd_file_t*)p_sys;
450 uint8_t *psz_args = query->psz_args;
451 uint8_t **pp_body, *p_body;
452 const char *psz_connection;
453 int *pi_body, i_body;
455 if( answer == NULL || query == NULL )
459 answer->i_proto = HTTPD_PROTO_HTTP;
460 answer->i_version= query->i_version;
461 answer->i_type = HTTPD_MSG_ANSWER;
463 answer->i_status = 200;
464 answer->psz_status = strdup( "OK" );
466 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
467 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
469 if( query->i_type != HTTPD_MSG_HEAD )
471 pp_body = &answer->p_body;
472 pi_body = &answer->i_body;
476 /* The file still needs to be executed. */
483 if( query->i_type == HTTPD_MSG_POST )
485 /* msg_Warn not supported */
488 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
490 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
495 /* We respect client request */
496 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
497 if( psz_connection != NULL )
499 httpd_MsgAdd( answer, "Connection", psz_connection );
502 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
507 httpd_file_t *httpd_FileNew( httpd_host_t *host,
508 const char *psz_url, const char *psz_mime,
509 const char *psz_user, const char *psz_password,
510 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
511 httpd_file_sys_t *p_sys )
513 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
515 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
516 psz_password, p_acl )
523 file->psz_url = strdup( psz_url );
524 if( psz_mime && *psz_mime )
526 file->psz_mime = strdup( psz_mime );
530 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
533 file->pf_fill = pf_fill;
536 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
537 (httpd_callback_sys_t*)file );
538 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
539 (httpd_callback_sys_t*)file );
540 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
541 (httpd_callback_sys_t*)file );
546 void httpd_FileDelete( httpd_file_t *file )
548 httpd_UrlDelete( file->url );
550 free( file->psz_url );
551 free( file->psz_mime );
556 /*****************************************************************************
557 * High Level Functions: httpd_handler_t (for CGIs)
558 *****************************************************************************/
559 struct httpd_handler_t
563 httpd_handler_callback_t pf_fill;
564 httpd_handler_sys_t *p_sys;
568 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
570 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
571 uint8_t *psz_args = query->psz_args;
572 char psz_remote_addr[NI_MAXNUMERICHOST];
574 if( answer == NULL || query == NULL )
578 answer->i_proto = HTTPD_PROTO_NONE;
579 answer->i_type = HTTPD_MSG_ANSWER;
581 /* We do it ourselves, thanks */
582 answer->i_status = 0;
583 answer->psz_status = NULL;
585 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
586 *psz_remote_addr = '\0';
588 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
589 query->i_type, query->p_body, query->i_body,
590 psz_remote_addr, NULL,
591 &answer->p_body, &answer->i_body );
593 if( query->i_type == HTTPD_MSG_HEAD )
595 char *p = (char *)answer->p_body;
596 while ( (p = strchr( p, '\r' )) != NULL )
598 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
599 && p[3] && p[3] == '\n' )
607 answer->i_body = strlen((char*)answer->p_body) + 1;
608 answer->p_body = realloc( answer->p_body, answer->i_body );
612 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
614 int i_status, i_headers;
615 char *psz_headers, *psz_new;
616 const char *psz_status;
618 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
621 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
622 if( *psz_headers ) psz_headers++;
623 if( *psz_headers ) psz_headers++;
624 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
629 psz_headers = (char *)answer->p_body;
630 i_headers = answer->i_body;
638 psz_status = "Unauthorized";
641 if( (i_status < 0) || (i_status > 999) )
643 psz_status = "Undefined";
646 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
647 + strlen(psz_status) + i_headers - 1;
648 psz_new = (char *)malloc( answer->i_body + 1);
649 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
650 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
651 free( answer->p_body );
652 answer->p_body = (uint8_t *)psz_new;
658 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
659 const char *psz_user,
660 const char *psz_password,
661 const vlc_acl_t *p_acl,
662 httpd_handler_callback_t pf_fill,
663 httpd_handler_sys_t *p_sys )
665 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
667 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
668 psz_password, p_acl )
675 handler->pf_fill = pf_fill;
676 handler->p_sys = p_sys;
678 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
679 (httpd_callback_sys_t*)handler );
680 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
681 (httpd_callback_sys_t*)handler );
682 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
683 (httpd_callback_sys_t*)handler );
688 void httpd_HandlerDelete( httpd_handler_t *handler )
690 httpd_UrlDelete( handler->url );
694 /*****************************************************************************
695 * High Level Functions: httpd_redirect_t
696 *****************************************************************************/
697 struct httpd_redirect_t
703 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
704 httpd_client_t *cl, httpd_message_t *answer,
705 httpd_message_t *query )
707 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
710 if( answer == NULL || query == NULL )
714 answer->i_proto = query->i_proto;
715 answer->i_version= query->i_version;
716 answer->i_type = HTTPD_MSG_ANSWER;
717 answer->i_status = 301;
718 answer->psz_status = strdup( "Moved Permanently" );
720 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
721 answer->p_body = (unsigned char *)p_body;
723 /* XXX check if it's ok or we need to set an absolute url */
724 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
726 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
731 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
732 const char *psz_url_src )
734 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
736 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
741 rdir->psz_dst = strdup( psz_url_dst );
743 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
744 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
745 (httpd_callback_sys_t*)rdir );
746 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
747 (httpd_callback_sys_t*)rdir );
748 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
749 (httpd_callback_sys_t*)rdir );
750 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
751 (httpd_callback_sys_t*)rdir );
755 void httpd_RedirectDelete( httpd_redirect_t *rdir )
757 httpd_UrlDelete( rdir->url );
758 free( rdir->psz_dst );
762 /*****************************************************************************
763 * High Level Funtions: httpd_stream_t
764 *****************************************************************************/
765 struct httpd_stream_t
772 /* Header to send as first packet */
776 /* circular buffer */
777 int i_buffer_size; /* buffer size, can't be reallocated smaller */
778 uint8_t *p_buffer; /* buffer */
779 int64_t i_buffer_pos; /* absolute position from begining */
780 int64_t i_buffer_last_pos; /* a new connection will start with that */
783 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
784 httpd_client_t *cl, httpd_message_t *answer,
785 httpd_message_t *query )
787 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
789 if( answer == NULL || query == NULL || cl == NULL )
794 if( answer->i_body_offset > 0 )
800 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
801 answer->i_body_offset );
804 if( answer->i_body_offset >= stream->i_buffer_pos )
806 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
807 return VLC_EGENERIC; /* wait, no data available */
809 if( answer->i_body_offset + stream->i_buffer_size <
810 stream->i_buffer_pos )
812 /* this client isn't fast enough */
814 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
815 answer->i_body_offset, stream->i_buffer_last_pos );
817 answer->i_body_offset = stream->i_buffer_last_pos;
820 i_pos = answer->i_body_offset % stream->i_buffer_size;
821 i_write = stream->i_buffer_pos - answer->i_body_offset;
822 if( i_write > HTTPD_CL_BUFSIZE )
824 i_write = HTTPD_CL_BUFSIZE;
826 else if( i_write <= 0 )
828 return VLC_EGENERIC; /* wait, no data available */
831 /* Don't go past the end of the circular buffer */
832 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
834 /* using HTTPD_MSG_ANSWER -> data available */
835 answer->i_proto = HTTPD_PROTO_HTTP;
836 answer->i_version= 0;
837 answer->i_type = HTTPD_MSG_ANSWER;
839 answer->i_body = i_write;
840 answer->p_body = malloc( i_write );
841 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
843 answer->i_body_offset += i_write;
849 answer->i_proto = HTTPD_PROTO_HTTP;
850 answer->i_version= 0;
851 answer->i_type = HTTPD_MSG_ANSWER;
853 answer->i_status = 200;
854 answer->psz_status = strdup( "OK" );
856 if( query->i_type != HTTPD_MSG_HEAD )
858 httpd_ClientModeStream( cl );
859 vlc_mutex_lock( &stream->lock );
860 /* Send the header */
861 if( stream->i_header > 0 )
863 answer->i_body = stream->i_header;
864 answer->p_body = malloc( stream->i_header );
865 memcpy( answer->p_body, stream->p_header, stream->i_header );
867 answer->i_body_offset = stream->i_buffer_last_pos;
868 vlc_mutex_unlock( &stream->lock );
872 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
873 answer->i_body_offset = 0;
876 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
878 vlc_bool_t b_xplaystream = VLC_FALSE;
881 httpd_MsgAdd( answer, "Content-type", "%s",
882 "application/octet-stream" );
883 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
884 httpd_MsgAdd( answer, "Pragma", "no-cache" );
885 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
886 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
888 /* Check if there is a xPlayStrm=1 */
889 for( i = 0; i < query->i_name; i++ )
891 if( !strcasecmp( query->name[i], "Pragma" ) &&
892 strstr( query->value[i], "xPlayStrm=1" ) )
894 b_xplaystream = VLC_TRUE;
900 answer->i_body_offset = 0;
905 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
907 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
912 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
913 const char *psz_url, const char *psz_mime,
914 const char *psz_user, const char *psz_password,
915 const vlc_acl_t *p_acl )
917 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
919 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
920 psz_password, p_acl )
926 vlc_mutex_init( host, &stream->lock );
927 if( psz_mime && *psz_mime )
929 stream->psz_mime = strdup( psz_mime );
933 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
935 stream->i_header = 0;
936 stream->p_header = NULL;
937 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
938 stream->p_buffer = malloc( stream->i_buffer_size );
939 /* We set to 1 to make life simpler
940 * (this way i_body_offset can never be 0) */
941 stream->i_buffer_pos = 1;
942 stream->i_buffer_last_pos = 1;
944 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
945 (httpd_callback_sys_t*)stream );
946 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
947 (httpd_callback_sys_t*)stream );
948 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
949 (httpd_callback_sys_t*)stream );
954 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
956 vlc_mutex_lock( &stream->lock );
957 if( stream->p_header )
959 free( stream->p_header );
960 stream->p_header = NULL;
962 stream->i_header = i_data;
965 stream->p_header = malloc( i_data );
966 memcpy( stream->p_header, p_data, i_data );
968 vlc_mutex_unlock( &stream->lock );
973 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
978 if( i_data < 0 || p_data == NULL )
982 vlc_mutex_lock( &stream->lock );
984 /* save this pointer (to be used by new connection) */
985 stream->i_buffer_last_pos = stream->i_buffer_pos;
987 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
993 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
995 /* Ok, we can't go past the end of our buffer */
996 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
998 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
1003 stream->i_buffer_pos += i_data;
1005 vlc_mutex_unlock( &stream->lock );
1009 void httpd_StreamDelete( httpd_stream_t *stream )
1011 httpd_UrlDelete( stream->url );
1012 vlc_mutex_destroy( &stream->lock );
1013 if( stream->psz_mime ) free( stream->psz_mime );
1014 if( stream->p_header ) free( stream->p_header );
1015 if( stream->p_buffer ) free( stream->p_buffer );
1019 /*****************************************************************************
1021 *****************************************************************************/
1022 static void httpd_HostThread( httpd_host_t * );
1024 /* create a new host */
1025 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
1028 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
1032 static const char psz_object_type[] = "http server";
1034 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
1036 const char *psz_cert, const char *psz_key,
1037 const char *psz_ca, const char *psz_crl )
1041 tls_server_t *p_tls;
1043 vlc_value_t lockval;
1046 if( psz_hostname == NULL )
1049 psz_host = strdup( psz_hostname );
1050 if( psz_host == NULL )
1052 msg_Err( p_this, "memory error" );
1056 /* to be sure to avoid multiple creation */
1057 var_Create( p_this->p_libvlc_global, "httpd_mutex", VLC_VAR_MUTEX );
1058 var_Get( p_this->p_libvlc_global, "httpd_mutex", &lockval );
1059 vlc_mutex_lock( lockval.p_address );
1061 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1063 msg_Info( p_this, "creating httpd" );
1064 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1069 vlc_mutex_unlock( lockval.p_address );
1077 vlc_object_yield( httpd );
1078 vlc_object_attach( httpd, p_this->p_libvlc );
1081 /* verify if it already exist */
1082 for( i = httpd->i_host - 1; i >= 0; i-- )
1084 host = httpd->host[i];
1086 /* cannot mix TLS and non-TLS hosts */
1087 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1088 || ( host->i_port != i_port )
1089 || strcmp( host->psz_hostname, psz_hostname ) )
1095 vlc_mutex_unlock( lockval.p_address );
1101 /* determine TLS configuration */
1102 if ( psz_cert != NULL )
1104 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1105 if ( p_tls == NULL )
1107 msg_Err( p_this, "TLS initialization error" );
1111 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1113 msg_Err( p_this, "TLS CA error" );
1117 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1119 msg_Err( p_this, "TLS CRL error" );
1126 /* create the new host */
1127 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1128 VLC_OBJECT_HTTPD_HOST,
1133 host->httpd = httpd;
1134 vlc_mutex_init( httpd, &host->lock );
1137 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1138 if( host->fds == NULL )
1140 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1143 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1145 host->i_port = i_port;
1146 host->psz_hostname = psz_host;
1151 host->client = NULL;
1153 host->p_tls = p_tls;
1155 /* create the thread */
1156 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1157 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1159 msg_Err( p_this, "cannot spawn http host thread" );
1163 /* now add it to httpd */
1164 TAB_APPEND( httpd->i_host, httpd->host, host );
1165 vlc_mutex_unlock( lockval.p_address );
1171 if( httpd->i_host <= 0 )
1173 vlc_object_release( httpd );
1174 vlc_object_detach( httpd );
1175 vlc_object_destroy( httpd );
1177 vlc_mutex_unlock( lockval.p_address );
1181 net_ListenClose( host->fds );
1182 vlc_mutex_destroy( &host->lock );
1183 vlc_object_destroy( host );
1187 tls_ServerDelete( p_tls );
1193 void httpd_HostDelete( httpd_host_t *host )
1195 httpd_t *httpd = host->httpd;
1196 vlc_value_t lockval;
1199 var_Get( httpd->p_libvlc_global, "httpd_mutex", &lockval );
1200 vlc_mutex_lock( lockval.p_address );
1203 if( host->i_ref > 0 )
1206 vlc_mutex_unlock( lockval.p_address );
1207 msg_Dbg( host, "httpd_HostDelete: host still used" );
1210 TAB_REMOVE( httpd->i_host, httpd->host, host );
1213 vlc_thread_join( host );
1215 msg_Dbg( host, "HTTP host removed" );
1217 for( i = 0; i < host->i_url; i++ )
1219 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1221 for( i = 0; i < host->i_client; i++ )
1223 httpd_client_t *cl = host->client[i];
1224 msg_Warn( host, "client still connected" );
1225 httpd_ClientClean( cl );
1226 TAB_REMOVE( host->i_client, host->client, cl );
1232 if( host->p_tls != NULL)
1233 tls_ServerDelete( host->p_tls );
1235 net_ListenClose( host->fds );
1236 free( host->psz_hostname );
1238 vlc_mutex_destroy( &host->lock );
1239 vlc_object_destroy( host );
1241 vlc_object_release( httpd );
1242 if( httpd->i_host <= 0 )
1244 msg_Dbg( httpd, "no host left, stopping httpd" );
1245 vlc_object_detach( httpd );
1246 vlc_object_destroy( httpd );
1248 vlc_mutex_unlock( lockval.p_address );
1251 /* register a new url */
1252 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1253 const char *psz_user, const char *psz_password,
1254 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1259 vlc_mutex_lock( &host->lock );
1262 for( i = 0; i < host->i_url; i++ )
1264 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1266 msg_Warn( host->httpd,
1267 "cannot add '%s' (url already defined)", psz_url );
1268 vlc_mutex_unlock( &host->lock );
1274 url = malloc( sizeof( httpd_url_t ) );
1277 vlc_mutex_init( host->httpd, &url->lock );
1278 url->psz_url = strdup( psz_url );
1279 url->psz_user = strdup( psz_user ? psz_user : "" );
1280 url->psz_password = strdup( psz_password ? psz_password : "" );
1281 url->p_acl = ACL_Duplicate( host, p_acl );
1282 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1284 url->catch[i].cb = NULL;
1285 url->catch[i].p_sys = NULL;
1288 TAB_APPEND( host->i_url, host->url, url );
1289 vlc_mutex_unlock( &host->lock );
1294 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1295 const char *psz_user, const char *psz_password,
1296 const vlc_acl_t *p_acl )
1298 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1299 psz_password, p_acl, VLC_FALSE );
1302 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1303 const char *psz_user, const char *psz_password,
1304 const vlc_acl_t *p_acl )
1306 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1307 psz_password, p_acl, VLC_TRUE );
1310 /* register callback on a url */
1311 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1312 httpd_callback_sys_t *p_sys )
1314 vlc_mutex_lock( &url->lock );
1315 url->catch[i_msg].cb = cb;
1316 url->catch[i_msg].p_sys= p_sys;
1317 vlc_mutex_unlock( &url->lock );
1323 void httpd_UrlDelete( httpd_url_t *url )
1325 httpd_host_t *host = url->host;
1328 vlc_mutex_lock( &host->lock );
1329 TAB_REMOVE( host->i_url, host->url, url );
1331 vlc_mutex_destroy( &url->lock );
1332 free( url->psz_url );
1333 free( url->psz_user );
1334 free( url->psz_password );
1335 ACL_Destroy( url->p_acl );
1337 for( i = 0; i < host->i_client; i++ )
1339 httpd_client_t *client = host->client[i];
1341 if( client->url == url )
1343 /* TODO complete it */
1344 msg_Warn( host, "force closing connections" );
1345 httpd_ClientClean( client );
1346 TAB_REMOVE( host->i_client, host->client, client );
1352 vlc_mutex_unlock( &host->lock );
1355 void httpd_MsgInit( httpd_message_t *msg )
1358 msg->i_type = HTTPD_MSG_NONE;
1359 msg->i_proto = HTTPD_PROTO_NONE;
1360 msg->i_version = -1;
1363 msg->psz_status = NULL;
1365 msg->psz_url = NULL;
1366 msg->psz_args = NULL;
1368 msg->i_channel = -1;
1375 msg->i_body_offset = 0;
1380 void httpd_MsgClean( httpd_message_t *msg )
1384 if( msg->psz_status )
1386 free( msg->psz_status );
1390 free( msg->psz_url );
1394 free( msg->psz_args );
1396 for( i = 0; i < msg->i_name; i++ )
1398 free( msg->name[i] );
1399 free( msg->value[i] );
1411 free( msg->p_body );
1413 httpd_MsgInit( msg );
1416 const char *httpd_MsgGet( httpd_message_t *msg, const char *name )
1420 for( i = 0; i < msg->i_name; i++ )
1422 if( !strcasecmp( msg->name[i], name ))
1424 return msg->value[i];
1430 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1435 va_start( args, psz_value );
1436 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1437 vasprintf( &value, psz_value, args );
1440 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1441 value = calloc( i_size, sizeof( char ) );
1442 vsnprintf( value, i_size, psz_value, args );
1443 value[i_size - 1] = 0;
1448 name = strdup( name );
1450 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1451 TAB_APPEND( msg->i_value, msg->value, value );
1454 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1456 cl->i_state = HTTPD_CLIENT_RECEIVING;
1457 cl->i_activity_date = now;
1458 cl->i_activity_timeout = I64C(10000000);
1459 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1461 cl->p_buffer = malloc( cl->i_buffer_size );
1462 cl->i_mode = HTTPD_CLIENT_FILE;
1463 cl->b_read_waiting = VLC_FALSE;
1465 httpd_MsgInit( &cl->query );
1466 httpd_MsgInit( &cl->answer );
1469 void httpd_ClientModeStream( httpd_client_t *cl )
1471 cl->i_mode = HTTPD_CLIENT_STREAM;
1474 void httpd_ClientModeBidir( httpd_client_t *cl )
1476 cl->i_mode = HTTPD_CLIENT_BIDIR;
1479 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1481 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1484 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1486 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1489 static void httpd_ClientClean( httpd_client_t *cl )
1493 if( cl->p_tls != NULL )
1494 tls_ServerSessionClose( cl->p_tls );
1495 net_Close( cl->fd );
1499 httpd_MsgClean( &cl->answer );
1500 httpd_MsgClean( &cl->query );
1504 free( cl->p_buffer );
1505 cl->p_buffer = NULL;
1509 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1511 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1513 if( !cl ) return NULL;
1520 httpd_ClientInit( cl, now );
1525 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1527 tls_session_t *p_tls;
1531 return tls_Recv( p_tls, p, i_len );
1533 return recv( cl->fd, p, i_len, 0 );
1536 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1538 tls_session_t *p_tls;
1542 return tls_Send( p_tls, p, i_len );
1544 return send( cl->fd, p, i_len, 0 );
1547 static void httpd_ClientRecv( httpd_client_t *cl )
1551 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1553 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1554 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1558 cl->i_buffer += i_len;
1561 if( cl->i_buffer >= 4 )
1563 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1565 if( cl->p_buffer[0] == '$' )
1567 /* RTSP (rtp over rtsp) */
1568 cl->query.i_proto = HTTPD_PROTO_RTSP;
1569 cl->query.i_type = HTTPD_MSG_CHANNEL;
1570 cl->query.i_channel = cl->p_buffer[1];
1571 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1572 cl->query.p_body = malloc( cl->query.i_body );
1576 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1578 cl->query.i_proto = HTTPD_PROTO_HTTP;
1579 cl->query.i_type = HTTPD_MSG_ANSWER;
1581 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1583 cl->query.i_proto = HTTPD_PROTO_RTSP;
1584 cl->query.i_type = HTTPD_MSG_ANSWER;
1586 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1587 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1588 !memcmp( cl->p_buffer, "POST", 4 ) )
1590 cl->query.i_proto = HTTPD_PROTO_HTTP;
1591 cl->query.i_type = HTTPD_MSG_NONE;
1595 cl->query.i_proto = HTTPD_PROTO_RTSP;
1596 cl->query.i_type = HTTPD_MSG_NONE;
1600 else if( cl->query.i_body > 0 )
1602 /* we are reading the body of a request or a channel */
1603 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1604 cl->query.i_body - cl->i_buffer );
1607 cl->i_buffer += i_len;
1609 if( cl->i_buffer >= cl->query.i_body )
1611 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1616 /* we are reading a header -> char by char */
1619 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1626 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1628 cl->i_buffer_size += 1024;
1629 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1631 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1632 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1636 /* we have finished the header so parse it and set i_body */
1637 cl->p_buffer[cl->i_buffer] = '\0';
1639 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1642 * assume strlen( "HTTP/1.x" ) = 8
1644 cl->query.i_status =
1645 strtol( (char *)&cl->p_buffer[8],
1651 cl->query.psz_status = strdup( p );
1663 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1664 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1665 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1666 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1667 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1668 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1669 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1671 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1672 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1673 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1675 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1680 cl->query.i_type = HTTPD_MSG_NONE;
1682 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1684 for( i = 0; msg_type[i].name != NULL; i++ )
1686 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1687 strlen( msg_type[i].name ) ) )
1689 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1690 cl->query.i_type = msg_type[i].i_type;
1691 if( cl->query.i_proto != msg_type[i].i_proto )
1694 cl->query.i_proto = HTTPD_PROTO_NONE;
1695 cl->query.i_type = HTTPD_MSG_NONE;
1702 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1704 cl->query.i_proto = HTTPD_PROTO_HTTP;
1706 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1708 cl->query.i_proto = HTTPD_PROTO_RTSP;
1720 p2 = strchr( p, ' ' );
1725 if( !strncasecmp( p, "rtsp:", 5 ) )
1727 /* for rtsp url, you have rtsp://localhost:port/path */
1729 while( *p == '/' ) p++;
1730 while( *p && *p != '/' ) p++;
1732 cl->query.psz_url = strdup( p );
1733 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1736 cl->query.psz_args = (uint8_t *)strdup( p3 );
1744 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1746 cl->query.i_proto = HTTPD_PROTO_HTTP;
1747 cl->query.i_version = atoi( p2+7 );
1749 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1751 cl->query.i_proto = HTTPD_PROTO_RTSP;
1752 cl->query.i_version = atoi( p2+7 );
1760 p = strchr( p, '\n' );
1764 while( *p == '\n' || *p == '\r' )
1768 while( p && *p != '\0' )
1771 char *eol = p = strchr( p, '\n' );
1774 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1779 if( ( colon = strchr( line, ':' ) ) )
1785 while( *colon == ' ' )
1789 name = strdup( line );
1790 value = strdup( colon );
1792 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1793 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1795 if( !strcasecmp( name, "Content-Length" ) )
1797 cl->query.i_body = atol( value );
1804 while( *p == '\n' || *p == '\r' )
1811 if( cl->query.i_body > 0 )
1813 /* TODO Mhh, handle the case client will only send a request and close the connection
1814 * to mark and of body (probably only RTSP) */
1815 cl->query.p_body = malloc( cl->query.i_body );
1820 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1826 /* check if the client is to be set to dead */
1827 #if defined( WIN32 ) || defined( UNDER_CE )
1828 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1830 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1833 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1835 /* connection closed -> end of data */
1836 if( cl->query.i_body > 0 )
1838 cl->query.i_body = cl->i_buffer;
1840 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1844 cl->i_state = HTTPD_CLIENT_DEAD;
1848 /* XXX: for QT I have to disable timeout. Try to find why */
1849 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1850 cl->i_activity_timeout = 0;
1852 #if 0 /* Debugging only */
1853 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1857 fprintf( stderr, "received new request\n" );
1858 fprintf( stderr, " - proto=%s\n",
1859 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1860 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1861 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1862 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1864 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1865 cl->query.psz_status );
1867 else if( cl->query.i_type != HTTPD_MSG_NONE )
1869 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1871 for( i = 0; i < cl->query.i_name; i++ )
1873 fprintf( stderr, " - option name='%s' value='%s'\n",
1874 cl->query.name[i], cl->query.value[i] );
1880 static void httpd_ClientSend( httpd_client_t *cl )
1885 if( cl->i_buffer < 0 )
1887 /* We need to create the header */
1891 i_size = strlen( "HTTP/1.") + 10 + 10 +
1892 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1893 for( i = 0; i < cl->answer.i_name; i++ )
1895 i_size += strlen( cl->answer.name[i] ) + 2 +
1896 strlen( cl->answer.value[i] ) + 2;
1899 if( cl->i_buffer_size < i_size )
1901 cl->i_buffer_size = i_size;
1902 free( cl->p_buffer );
1903 cl->p_buffer = malloc( i_size );
1905 p = (char *)cl->p_buffer;
1907 p += sprintf( p, "%s/1.%d %d %s\r\n",
1908 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1909 cl->answer.i_version,
1910 cl->answer.i_status, cl->answer.psz_status );
1911 for( i = 0; i < cl->answer.i_name; i++ )
1913 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1914 cl->answer.value[i] );
1916 p += sprintf( p, "\r\n" );
1919 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1921 /*fprintf( stderr, "sending answer\n" );
1922 fprintf( stderr, "%s", cl->p_buffer );*/
1925 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1926 cl->i_buffer_size - cl->i_buffer );
1929 cl->i_buffer += i_len;
1931 if( cl->i_buffer >= cl->i_buffer_size )
1933 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1934 !cl->b_read_waiting )
1936 /* catch more body data */
1937 int i_msg = cl->query.i_type;
1938 int64_t i_offset = cl->answer.i_body_offset;
1940 httpd_MsgClean( &cl->answer );
1941 cl->answer.i_body_offset = i_offset;
1943 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1944 &cl->answer, &cl->query );
1947 if( cl->answer.i_body > 0 )
1949 /* send the body data */
1950 free( cl->p_buffer );
1951 cl->p_buffer = cl->answer.p_body;
1952 cl->i_buffer_size = cl->answer.i_body;
1955 cl->answer.i_body = 0;
1956 cl->answer.p_body = NULL;
1961 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1967 #if defined( WIN32 ) || defined( UNDER_CE )
1968 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1970 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1974 cl->i_state = HTTPD_CLIENT_DEAD;
1979 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1981 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1984 cl->i_state = HTTPD_CLIENT_RECEIVING;
1988 cl->i_state = HTTPD_CLIENT_DEAD;
1993 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1997 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1999 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2002 cl->i_state = HTTPD_CLIENT_RECEIVING;
2006 cl->i_state = HTTPD_CLIENT_DEAD;
2011 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2016 static void httpd_HostThread( httpd_host_t *host )
2018 tls_session_t *p_tls = NULL;
2020 host->p_total_counter = stats_CounterCreate( host,
2021 VLC_VAR_INTEGER, STATS_COUNTER );
2022 host->p_active_counter = stats_CounterCreate( host,
2023 VLC_VAR_INTEGER, STATS_COUNTER );
2025 while( !host->b_die )
2027 if( host->i_url <= 0 )
2034 /* prepare a new TLS session */
2035 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2036 p_tls = tls_ServerSessionPrepare( host->p_tls );
2038 struct pollfd ufd[host->nfd + host->i_client + host->nfd]; /* We have nfd listening sockets, i_client, and lay accept up to nfd new client */
2040 for (nfd = 0; nfd < host->nfd; nfd++)
2042 ufd[nfd].fd = host->fds[nfd];
2043 ufd[nfd].events = POLLIN;
2044 ufd[nfd].revents = 0;
2047 /* add all socket that should be read/write and close dead connection */
2048 vlc_mutex_lock( &host->lock );
2049 mtime_t now = mdate();
2050 vlc_bool_t b_low_delay = VLC_FALSE;
2052 for(int i_client = 0; i_client < host->i_client; i_client++ )
2054 httpd_client_t *cl = host->client[i_client];
2055 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2056 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2057 ( cl->i_activity_timeout > 0 &&
2058 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2060 httpd_ClientClean( cl );
2061 stats_UpdateInteger( host, host->p_active_counter, -1, NULL );
2062 TAB_REMOVE( host->i_client, host->client, cl );
2068 struct pollfd *pufd = ufd + nfd;
2069 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2072 pufd->events = pufd->revents = 0;
2074 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2075 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2077 pufd->events = POLLIN;
2079 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2080 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2082 pufd->events = POLLOUT;
2084 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2086 httpd_message_t *answer = &cl->answer;
2087 httpd_message_t *query = &cl->query;
2088 int i_msg = query->i_type;
2090 httpd_MsgInit( answer );
2092 /* Handle what we received */
2093 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2094 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2096 /* we can only receive request from client when not
2099 cl->i_state = HTTPD_CLIENT_DEAD;
2101 else if( i_msg == HTTPD_MSG_ANSWER )
2103 /* We are in BIDIR mode, trigger the callback and then
2104 * check for new data */
2105 if( cl->url && cl->url->catch[i_msg].cb )
2107 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2110 cl->i_state = HTTPD_CLIENT_WAITING;
2112 else if( i_msg == HTTPD_MSG_CHANNEL )
2114 /* We are in BIDIR mode, trigger the callback and then
2115 * check for new data */
2116 if( cl->url && cl->url->catch[i_msg].cb )
2118 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2121 cl->i_state = HTTPD_CLIENT_WAITING;
2123 else if( i_msg == HTTPD_MSG_OPTIONS )
2125 const char *psz_cseq;
2129 answer->i_proto = query->i_proto ;
2130 answer->i_type = HTTPD_MSG_ANSWER;
2131 answer->i_version= 0;
2132 answer->i_status = 200;
2133 answer->psz_status = strdup( "Ok" );
2136 answer->p_body = NULL;
2138 psz_cseq = httpd_MsgGet( query, "Cseq" );
2140 i_cseq = atoi( psz_cseq );
2143 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2144 httpd_MsgAdd( answer, "Server", "VLC Server" );
2145 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2146 "TEARDOWN, PLAY, PAUSE" );
2147 httpd_MsgAdd( answer, "Content-Length", "%d",
2150 cl->i_buffer = -1; /* Force the creation of the answer in
2151 * httpd_ClientSend */
2152 cl->i_state = HTTPD_CLIENT_SENDING;
2154 else if( i_msg == HTTPD_MSG_NONE )
2156 if( query->i_proto == HTTPD_PROTO_NONE )
2159 cl->i_state = HTTPD_CLIENT_DEAD;
2166 answer->i_proto = query->i_proto ;
2167 answer->i_type = HTTPD_MSG_ANSWER;
2168 answer->i_version= 0;
2169 answer->i_status = 501;
2170 answer->psz_status = strdup( "Unimplemented" );
2172 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2173 answer->p_body = (uint8_t *)p;
2174 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2176 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2177 cl->i_state = HTTPD_CLIENT_SENDING;
2182 vlc_bool_t b_auth_failed = VLC_FALSE;
2183 vlc_bool_t b_hosts_failed = VLC_FALSE;
2185 /* Search the url and trigger callbacks */
2186 for(int i = 0; i < host->i_url; i++ )
2188 httpd_url_t *url = host->url[i];
2190 if( !strcmp( url->psz_url, query->psz_url ) )
2192 if( url->catch[i_msg].cb )
2194 if( answer && ( url->p_acl != NULL ) )
2196 char ip[NI_MAXNUMERICHOST];
2198 if( httpd_ClientIP( cl, ip ) != NULL )
2200 if( ACL_Check( url->p_acl, ip ) )
2202 b_hosts_failed = VLC_TRUE;
2207 b_hosts_failed = VLC_TRUE;
2210 if( answer && ( *url->psz_user || *url->psz_password ) )
2212 /* create the headers */
2213 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2217 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2219 && !strncasecmp( b64, "BASIC", 5 ) )
2222 while( *b64 == ' ' )
2226 auth = malloc( strlen(b64) + 1 );
2227 b64_decode( auth, b64 );
2231 auth = strdup( "" );
2234 if( strcmp( id, auth ) )
2236 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2237 /* We fail for all url */
2238 b_auth_failed = VLC_TRUE;
2248 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2250 if( answer->i_proto == HTTPD_PROTO_NONE )
2252 /* Raw answer from a CGI */
2253 cl->i_buffer = cl->i_buffer_size;
2258 /* only one url can answer */
2260 if( cl->url == NULL )
2273 answer->i_proto = query->i_proto;
2274 answer->i_type = HTTPD_MSG_ANSWER;
2275 answer->i_version= 0;
2277 if( b_hosts_failed )
2279 answer->i_status = 403;
2280 answer->psz_status = strdup( "Forbidden" );
2282 else if( b_auth_failed )
2284 answer->i_status = 401;
2285 answer->psz_status = strdup( "Authorization Required" );
2289 /* no url registered */
2290 answer->i_status = 404;
2291 answer->psz_status = strdup( "Not found" );
2294 answer->i_body = httpd_HtmlError (&p,
2297 answer->p_body = (uint8_t *)p;
2299 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2300 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2301 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2304 cl->i_state = HTTPD_CLIENT_SENDING;
2307 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2309 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2311 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2312 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2313 vlc_bool_t b_connection = VLC_FALSE;
2314 vlc_bool_t b_keepalive = VLC_FALSE;
2315 vlc_bool_t b_query = VLC_FALSE;
2318 if( psz_connection )
2320 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2321 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2326 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2329 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2330 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2331 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2332 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2333 !b_query && !b_connection ) )
2335 httpd_MsgClean( &cl->query );
2336 httpd_MsgInit( &cl->query );
2339 cl->i_buffer_size = 1000;
2340 free( cl->p_buffer );
2341 cl->p_buffer = malloc( cl->i_buffer_size );
2342 cl->i_state = HTTPD_CLIENT_RECEIVING;
2346 cl->i_state = HTTPD_CLIENT_DEAD;
2348 httpd_MsgClean( &cl->answer );
2350 else if( cl->b_read_waiting )
2352 /* we have a message waiting for us to read it */
2353 httpd_MsgClean( &cl->answer );
2354 httpd_MsgClean( &cl->query );
2357 cl->i_buffer_size = 1000;
2358 free( cl->p_buffer );
2359 cl->p_buffer = malloc( cl->i_buffer_size );
2360 cl->i_state = HTTPD_CLIENT_RECEIVING;
2361 cl->b_read_waiting = VLC_FALSE;
2365 int64_t i_offset = cl->answer.i_body_offset;
2366 httpd_MsgClean( &cl->answer );
2368 cl->answer.i_body_offset = i_offset;
2369 free( cl->p_buffer );
2370 cl->p_buffer = NULL;
2372 cl->i_buffer_size = 0;
2374 cl->i_state = HTTPD_CLIENT_WAITING;
2377 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2379 int64_t i_offset = cl->answer.i_body_offset;
2380 int i_msg = cl->query.i_type;
2382 httpd_MsgInit( &cl->answer );
2383 cl->answer.i_body_offset = i_offset;
2385 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2386 &cl->answer, &cl->query );
2387 if( cl->answer.i_type != HTTPD_MSG_NONE )
2389 /* we have new data, so re-enter send mode */
2391 cl->p_buffer = cl->answer.p_body;
2392 cl->i_buffer_size = cl->answer.i_body;
2393 cl->answer.p_body = NULL;
2394 cl->answer.i_body = 0;
2395 cl->i_state = HTTPD_CLIENT_SENDING;
2399 /* we shouldn't wait too long */
2400 b_low_delay = VLC_TRUE;
2404 /* Special for BIDIR mode we also check reading */
2405 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2406 cl->i_state == HTTPD_CLIENT_SENDING )
2408 pufd->events |= POLLIN;
2411 if (pufd->events != 0)
2414 vlc_mutex_unlock( &host->lock );
2416 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2417 switch (poll (ufd, nfd, b_low_delay ? 20 : 100))
2422 /* This is most likely a bug */
2423 msg_Err( host, "polling error: %s", strerror (errno));
2432 /* accept new connections */
2433 for (nfd = 0; nfd < host->nfd; nfd++)
2435 assert (ufd[nfd].fd == host->fds[nfd]);
2437 if (ufd[nfd].revents == 0)
2440 int fd = net_Accept (host, host->fds, 0);
2448 switch (tls_ServerSessionHandshake (p_tls, fd))
2451 msg_Err( host, "Rejecting TLS connection" );
2457 case 1: /* missing input - most likely */
2458 i_state = HTTPD_CLIENT_TLS_HS_IN;
2461 case 2: /* missing output */
2462 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2466 if ((p_tls == NULL) != (host->p_tls == NULL))
2467 break; // wasted TLS session, cannot accept() anymore
2471 stats_UpdateInteger( host, host->p_total_counter, 1, NULL );
2472 stats_UpdateInteger( host, host->p_active_counter, 1, NULL );
2473 cl = httpd_ClientNew( fd, p_tls, now );
2475 vlc_mutex_lock( &host->lock );
2476 TAB_APPEND( host->i_client, host->client, cl );
2477 vlc_mutex_unlock( &host->lock );
2478 cl->i_state = i_state; // override state for TLS
2480 if (host->p_tls != NULL)
2481 break; // cannot accept further without new TLS session
2484 /* now try all others socket */
2485 vlc_mutex_lock( &host->lock );
2487 for( int i_client = 0; i_client < host->i_client; i_client++ )
2489 httpd_client_t *cl = host->client[i_client];
2490 const struct pollfd *pufd = ufd + nfd;
2492 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2494 if (cl->fd != pufd->fd)
2495 continue; // we were not waiting for this client
2498 if (pufd->revents == 0)
2499 continue; // no event received
2501 cl->i_activity_date = now;
2503 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2505 httpd_ClientRecv( cl );
2507 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2509 httpd_ClientSend( cl );
2511 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2513 httpd_ClientTlsHsIn( cl );
2515 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2517 httpd_ClientTlsHsOut( cl );
2520 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2521 cl->i_state == HTTPD_CLIENT_SENDING &&
2522 (pufd->revents & POLLIN) )
2524 cl->b_read_waiting = VLC_TRUE;
2527 vlc_mutex_unlock( &host->lock );
2531 tls_ServerSessionClose( p_tls );
2534 #else /* ENABLE_HTTPD */
2536 /* We just define an empty wrapper */
2537 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2540 msg_Err( a, "HTTP daemon support is disabled" );
2544 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2546 msg_Err( a, "HTTP daemon support is disabled" );
2550 void httpd_HostDelete( httpd_host_t *a )
2554 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2555 char *psz_user, char *psz_password,
2556 const vlc_acl_t *p_acl )
2561 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2562 char *psz_user, char *psz_password,
2563 const vlc_acl_t *p_acl )
2568 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2569 httpd_callback_sys_t *d )
2574 void httpd_UrlDelete( httpd_url_t *a )
2578 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2583 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2588 void httpd_ClientModeStream( httpd_client_t *a )
2592 void httpd_ClientModeBidir( httpd_client_t *a )
2596 void httpd_FileDelete( httpd_file_t *a )
2600 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2601 char *e, httpd_file_callback_t f,
2602 httpd_file_sys_t *g )
2607 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2608 const char *psz_user,
2609 const char *psz_password,
2610 const vlc_acl_t *p_acl,
2611 httpd_handler_callback_t pf_fill,
2612 httpd_handler_sys_t *p_sys )
2617 void httpd_HandlerDelete( httpd_handler_t *handler )
2621 void httpd_RedirectDelete( httpd_redirect_t *a )
2625 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2631 void httpd_StreamDelete( httpd_stream_t *a )
2635 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2640 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2645 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2651 void httpd_MsgInit ( httpd_message_t *a )
2655 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2659 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2664 void httpd_MsgClean( httpd_message_t *a )
2668 #endif /* ENABLE_HTTPD */