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"
51 #if defined( UNDER_CE )
53 #elif defined( WIN32 )
54 # include <winsock2.h>
56 # include <sys/socket.h>
60 /* We need HUGE buffer otherwise TCP throughput is very limited */
61 #define HTTPD_CL_BUFSIZE 1000000
63 #define HTTPD_CL_BUFSIZE 10000
66 static void httpd_ClientClean( httpd_client_t *cl );
77 /* each host run in his own thread */
87 /* address/port and socket for listening at connections */
93 counter_t *p_active_counter;
94 counter_t *p_total_counter;
98 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
99 * This will slow down the url research but make my live easier
100 * All url will have their cb trigger, but only the first one can answer
106 httpd_client_t **client;
127 httpd_callback_sys_t *p_sys;
128 } catch[HTTPD_MSG_MAX];
134 HTTPD_CLIENT_RECEIVING,
135 HTTPD_CLIENT_RECEIVE_DONE,
137 HTTPD_CLIENT_SENDING,
138 HTTPD_CLIENT_SEND_DONE,
140 HTTPD_CLIENT_WAITING,
144 HTTPD_CLIENT_TLS_HS_IN,
145 HTTPD_CLIENT_TLS_HS_OUT
151 HTTPD_CLIENT_FILE, /* default */
152 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
153 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
156 struct httpd_client_t
162 struct sockaddr_storage sock;
168 int b_read_waiting; /* stop as soon as possible sending */
170 mtime_t i_activity_date;
171 mtime_t i_activity_timeout;
173 /* buffer for reading header */
179 httpd_message_t query; /* client -> httpd */
180 httpd_message_t answer; /* httpd -> client */
183 tls_session_t *p_tls;
187 /*****************************************************************************
189 *****************************************************************************/
190 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
191 static void b64_decode( char *restrict dest, const char *restrict src )
196 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
197 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
198 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
199 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
200 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
201 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
202 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
203 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
204 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
205 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
206 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
207 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
208 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
209 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
210 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
211 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
214 for( i_level = 0; *src != '\0'; src++ )
218 c = b64[(unsigned int)*src];
230 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
234 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
238 *dest++ = ( ( last &0x03 ) << 6 ) | c;
250 const char *psz_mime;
253 { ".htm", "text/html" },
254 { ".html", "text/html" },
255 { ".txt", "text/plain" },
256 { ".xml", "text/xml" },
257 { ".dtd", "text/dtd" },
259 { ".css", "text/css" },
262 { ".gif", "image/gif" },
263 { ".jpe", "image/jpeg" },
264 { ".jpg", "image/jpeg" },
265 { ".jpeg", "image/jpeg" },
266 { ".png", "image/png" },
267 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
270 { ".avi", "video/avi" },
271 { ".asf", "video/x-ms-asf" },
272 { ".m1a", "audio/mpeg" },
273 { ".m2a", "audio/mpeg" },
274 { ".m1v", "video/mpeg" },
275 { ".m2v", "video/mpeg" },
276 { ".mp2", "audio/mpeg" },
277 { ".mp3", "audio/mpeg" },
278 { ".mpa", "audio/mpeg" },
279 { ".mpg", "video/mpeg" },
280 { ".mpeg", "video/mpeg" },
281 { ".mpe", "video/mpeg" },
282 { ".mov", "video/quicktime" },
283 { ".moov", "video/quicktime" },
284 { ".ogg", "application/ogg" },
285 { ".ogm", "application/ogg" },
286 { ".wav", "audio/wav" },
287 { ".wma", "audio/x-ms-wma" },
288 { ".wmv", "video/x-ms-wmv" },
295 static const char *httpd_MimeFromUrl( const char *psz_url )
300 psz_ext = strrchr( psz_url, '.' );
305 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
307 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
309 return http_mime[i].psz_mime;
313 return "application/octet-stream";
320 const char *psz_reason;
323 static const http_status_info http_reason[] =
325 /*{ 100, "Continue" },
326 { 101, "Switching Protocols" },*/
330 { 203, "Non-Authoritative Information" },
331 { 204, "No Content" },
332 { 205, "Reset Content"Â },
333 { 206, "Partial Content" },
334 { 250, "Low on Storage Space" },
335 { 300, "Multiple Choices" }*/,
336 { 301, "Moved Permanently" }/*,
337 { 302, "Moved Temporarily" }, - aka "Found"
338 { 303, "See Other" },
339 { 304, "Not Modified" },
340 { 305, "Use Proxy" },
341 { 307, "Temporary Redirect" },
342 { 400, "Bad Request" }*/,
343 { 401, "Unauthorized" }/*,
344 { 402, "Payment Required" }*/,
345 { 403, "Forbidden" },
346 { 404, "Not Found" }/*,
347 { 405, "Method Not Allowed" },
348 { 406, "Not Acceptable" },
349 { 407, "Proxy Authentication Required" },
350 { 408, "Request Time-out" },
353 { 411, "Length Required" },
354 { 412, "Precondition Failed" },
355 { 413, "Request Entity Too Large" },
356 { 414, "Request-URI Too Large" },
357 { 415, "Unsupported Media Type" },
358 { 416, "Requested range not satisfiable" },
359 { 417, "Expectation Failed" },
360 { 451, "Parameter Not Understood" },
361 { 452, "Conference Not Found" },
362 { 453, "Not Enough Bandwidth" }*/,
363 { 454, "Session Not Found" }/*,
364 { 455, "Method Not Valid in This State" },
365 { 456, "Header Field Not Valid for Resource" },
366 { 457, "Invalid Range" },
367 { 458, "Parameter Is Read-Only" },
368 { 459, "Aggregate operation not allowed" },
369 { 460, "Only aggregate operation allowed" }*/,
370 { 461, "Unsupported transport" }/*,
371 { 462, "Destination unreachable" }*/,
372 { 500, "Internal Server Error" },
373 { 501, "Not Implemented" }/*,
374 { 502, "Bad Gateway" }*/,
375 { 503, "Service Unavailable" }/*,
376 { 504, "Gateway Time-out" },
377 { 505, "Protocol version not supported" }*/,
381 static const char *psz_fallback_reason[] =
382 { "Continue", "OK", "Found", "Client Error", "Server Error" };
384 static const char *httpd_ReasonFromCode( int i_code )
386 const http_status_info *p;
388 for (p = http_reason; p->i_code < i_code; p++);
390 if( p->i_code == i_code )
391 return p->psz_reason;
393 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
394 return psz_fallback_reason[(i_code / 100) - 1];
398 static size_t httpd_HtmlError (char **body, int code, const char *url)
400 const char *errname = httpd_ReasonFromCode (code);
401 assert (errname != NULL);
403 int res = asprintf (body,
404 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
405 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
406 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
407 "<html lang=\"en\">\n"
409 "<title>%s</title>\n"
412 "<h1>%d %s%s%s%s</h1>\n"
414 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
416 "</html>\n", errname, code, errname,
417 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
429 /*****************************************************************************
430 * High Level Functions: httpd_file_t
431 *****************************************************************************/
439 httpd_file_callback_t pf_fill;
440 httpd_file_sys_t *p_sys;
444 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
446 httpd_file_t *file = (httpd_file_t*)p_sys;
447 uint8_t *psz_args = query->psz_args;
448 uint8_t **pp_body, *p_body;
449 const char *psz_connection;
450 int *pi_body, i_body;
452 if( answer == NULL || query == NULL )
456 answer->i_proto = HTTPD_PROTO_HTTP;
457 answer->i_version= query->i_version;
458 answer->i_type = HTTPD_MSG_ANSWER;
460 answer->i_status = 200;
461 answer->psz_status = strdup( "OK" );
463 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
464 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
466 if( query->i_type != HTTPD_MSG_HEAD )
468 pp_body = &answer->p_body;
469 pi_body = &answer->i_body;
473 /* The file still needs to be executed. */
480 if( query->i_type == HTTPD_MSG_POST )
482 /* msg_Warn not supported */
485 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
487 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
492 /* We respect client request */
493 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
494 if( psz_connection != NULL )
496 httpd_MsgAdd( answer, "Connection", psz_connection );
499 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
504 httpd_file_t *httpd_FileNew( httpd_host_t *host,
505 const char *psz_url, const char *psz_mime,
506 const char *psz_user, const char *psz_password,
507 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
508 httpd_file_sys_t *p_sys )
510 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
512 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
513 psz_password, p_acl )
520 file->psz_url = strdup( psz_url );
521 if( psz_mime && *psz_mime )
523 file->psz_mime = strdup( psz_mime );
527 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
530 file->pf_fill = pf_fill;
533 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
534 (httpd_callback_sys_t*)file );
535 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
536 (httpd_callback_sys_t*)file );
537 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
538 (httpd_callback_sys_t*)file );
543 void httpd_FileDelete( httpd_file_t *file )
545 httpd_UrlDelete( file->url );
547 free( file->psz_url );
548 free( file->psz_mime );
553 /*****************************************************************************
554 * High Level Functions: httpd_handler_t (for CGIs)
555 *****************************************************************************/
556 struct httpd_handler_t
560 httpd_handler_callback_t pf_fill;
561 httpd_handler_sys_t *p_sys;
565 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
567 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
568 uint8_t *psz_args = query->psz_args;
569 char psz_remote_addr[NI_MAXNUMERICHOST];
571 if( answer == NULL || query == NULL )
575 answer->i_proto = HTTPD_PROTO_NONE;
576 answer->i_type = HTTPD_MSG_ANSWER;
578 /* We do it ourselves, thanks */
579 answer->i_status = 0;
580 answer->psz_status = NULL;
582 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
583 *psz_remote_addr = '\0';
585 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
586 query->i_type, query->p_body, query->i_body,
587 psz_remote_addr, NULL,
588 &answer->p_body, &answer->i_body );
590 if( query->i_type == HTTPD_MSG_HEAD )
592 char *p = (char *)answer->p_body;
593 while ( (p = strchr( p, '\r' )) != NULL )
595 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
596 && p[3] && p[3] == '\n' )
604 answer->i_body = strlen((char*)answer->p_body) + 1;
605 answer->p_body = realloc( answer->p_body, answer->i_body );
609 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
611 int i_status, i_headers;
612 char *psz_headers, *psz_new;
613 const char *psz_status;
615 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
618 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
619 if( *psz_headers ) psz_headers++;
620 if( *psz_headers ) psz_headers++;
621 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
626 psz_headers = (char *)answer->p_body;
627 i_headers = answer->i_body;
635 psz_status = "Unauthorized";
638 if( (i_status < 0) || (i_status > 999) )
640 psz_status = "Undefined";
643 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
644 + strlen(psz_status) + i_headers - 1;
645 psz_new = (char *)malloc( answer->i_body + 1);
646 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
647 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
648 free( answer->p_body );
649 answer->p_body = (uint8_t *)psz_new;
655 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
656 const char *psz_user,
657 const char *psz_password,
658 const vlc_acl_t *p_acl,
659 httpd_handler_callback_t pf_fill,
660 httpd_handler_sys_t *p_sys )
662 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
664 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
665 psz_password, p_acl )
672 handler->pf_fill = pf_fill;
673 handler->p_sys = p_sys;
675 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
676 (httpd_callback_sys_t*)handler );
677 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
678 (httpd_callback_sys_t*)handler );
679 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
680 (httpd_callback_sys_t*)handler );
685 void httpd_HandlerDelete( httpd_handler_t *handler )
687 httpd_UrlDelete( handler->url );
691 /*****************************************************************************
692 * High Level Functions: httpd_redirect_t
693 *****************************************************************************/
694 struct httpd_redirect_t
700 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
701 httpd_client_t *cl, httpd_message_t *answer,
702 httpd_message_t *query )
704 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
707 if( answer == NULL || query == NULL )
711 answer->i_proto = query->i_proto;
712 answer->i_version= query->i_version;
713 answer->i_type = HTTPD_MSG_ANSWER;
714 answer->i_status = 301;
715 answer->psz_status = strdup( "Moved Permanently" );
717 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
718 answer->p_body = (unsigned char *)p_body;
720 /* XXX check if it's ok or we need to set an absolute url */
721 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
723 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
728 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
729 const char *psz_url_src )
731 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
733 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
738 rdir->psz_dst = strdup( psz_url_dst );
740 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
741 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
742 (httpd_callback_sys_t*)rdir );
743 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
744 (httpd_callback_sys_t*)rdir );
745 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
746 (httpd_callback_sys_t*)rdir );
747 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
748 (httpd_callback_sys_t*)rdir );
752 void httpd_RedirectDelete( httpd_redirect_t *rdir )
754 httpd_UrlDelete( rdir->url );
755 free( rdir->psz_dst );
759 /*****************************************************************************
760 * High Level Funtions: httpd_stream_t
761 *****************************************************************************/
762 struct httpd_stream_t
769 /* Header to send as first packet */
773 /* circular buffer */
774 int i_buffer_size; /* buffer size, can't be reallocated smaller */
775 uint8_t *p_buffer; /* buffer */
776 int64_t i_buffer_pos; /* absolute position from begining */
777 int64_t i_buffer_last_pos; /* a new connection will start with that */
780 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
781 httpd_client_t *cl, httpd_message_t *answer,
782 httpd_message_t *query )
784 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
786 if( answer == NULL || query == NULL || cl == NULL )
791 if( answer->i_body_offset > 0 )
797 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
798 answer->i_body_offset );
801 if( answer->i_body_offset >= stream->i_buffer_pos )
803 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
804 return VLC_EGENERIC; /* wait, no data available */
806 if( answer->i_body_offset + stream->i_buffer_size <
807 stream->i_buffer_pos )
809 /* this client isn't fast enough */
811 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
812 answer->i_body_offset, stream->i_buffer_last_pos );
814 answer->i_body_offset = stream->i_buffer_last_pos;
817 i_pos = answer->i_body_offset % stream->i_buffer_size;
818 i_write = stream->i_buffer_pos - answer->i_body_offset;
819 if( i_write > HTTPD_CL_BUFSIZE )
821 i_write = HTTPD_CL_BUFSIZE;
823 else if( i_write <= 0 )
825 return VLC_EGENERIC; /* wait, no data available */
828 /* Don't go past the end of the circular buffer */
829 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
831 /* using HTTPD_MSG_ANSWER -> data available */
832 answer->i_proto = HTTPD_PROTO_HTTP;
833 answer->i_version= 0;
834 answer->i_type = HTTPD_MSG_ANSWER;
836 answer->i_body = i_write;
837 answer->p_body = malloc( i_write );
838 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
840 answer->i_body_offset += i_write;
846 answer->i_proto = HTTPD_PROTO_HTTP;
847 answer->i_version= 0;
848 answer->i_type = HTTPD_MSG_ANSWER;
850 answer->i_status = 200;
851 answer->psz_status = strdup( "OK" );
853 if( query->i_type != HTTPD_MSG_HEAD )
855 httpd_ClientModeStream( cl );
856 vlc_mutex_lock( &stream->lock );
857 /* Send the header */
858 if( stream->i_header > 0 )
860 answer->i_body = stream->i_header;
861 answer->p_body = malloc( stream->i_header );
862 memcpy( answer->p_body, stream->p_header, stream->i_header );
864 answer->i_body_offset = stream->i_buffer_last_pos;
865 vlc_mutex_unlock( &stream->lock );
869 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
870 answer->i_body_offset = 0;
873 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
875 vlc_bool_t b_xplaystream = VLC_FALSE;
878 httpd_MsgAdd( answer, "Content-type", "%s",
879 "application/octet-stream" );
880 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
881 httpd_MsgAdd( answer, "Pragma", "no-cache" );
882 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
883 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
885 /* Check if there is a xPlayStrm=1 */
886 for( i = 0; i < query->i_name; i++ )
888 if( !strcasecmp( query->name[i], "Pragma" ) &&
889 strstr( query->value[i], "xPlayStrm=1" ) )
891 b_xplaystream = VLC_TRUE;
897 answer->i_body_offset = 0;
902 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
904 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
909 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
910 const char *psz_url, const char *psz_mime,
911 const char *psz_user, const char *psz_password,
912 const vlc_acl_t *p_acl )
914 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
916 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
917 psz_password, p_acl )
923 vlc_mutex_init( host, &stream->lock );
924 if( psz_mime && *psz_mime )
926 stream->psz_mime = strdup( psz_mime );
930 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
932 stream->i_header = 0;
933 stream->p_header = NULL;
934 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
935 stream->p_buffer = malloc( stream->i_buffer_size );
936 /* We set to 1 to make life simpler
937 * (this way i_body_offset can never be 0) */
938 stream->i_buffer_pos = 1;
939 stream->i_buffer_last_pos = 1;
941 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
942 (httpd_callback_sys_t*)stream );
943 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
944 (httpd_callback_sys_t*)stream );
945 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
946 (httpd_callback_sys_t*)stream );
951 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
953 vlc_mutex_lock( &stream->lock );
954 if( stream->p_header )
956 free( stream->p_header );
957 stream->p_header = NULL;
959 stream->i_header = i_data;
962 stream->p_header = malloc( i_data );
963 memcpy( stream->p_header, p_data, i_data );
965 vlc_mutex_unlock( &stream->lock );
970 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
975 if( i_data < 0 || p_data == NULL )
979 vlc_mutex_lock( &stream->lock );
981 /* save this pointer (to be used by new connection) */
982 stream->i_buffer_last_pos = stream->i_buffer_pos;
984 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
990 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
992 /* Ok, we can't go past the end of our buffer */
993 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
995 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
1000 stream->i_buffer_pos += i_data;
1002 vlc_mutex_unlock( &stream->lock );
1006 void httpd_StreamDelete( httpd_stream_t *stream )
1008 httpd_UrlDelete( stream->url );
1009 vlc_mutex_destroy( &stream->lock );
1010 if( stream->psz_mime ) free( stream->psz_mime );
1011 if( stream->p_header ) free( stream->p_header );
1012 if( stream->p_buffer ) free( stream->p_buffer );
1016 /*****************************************************************************
1018 *****************************************************************************/
1019 static void httpd_HostThread( httpd_host_t * );
1021 /* create a new host */
1022 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
1025 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
1029 static const char psz_object_type[] = "http server";
1031 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
1033 const char *psz_cert, const char *psz_key,
1034 const char *psz_ca, const char *psz_crl )
1038 tls_server_t *p_tls;
1040 vlc_value_t lockval;
1043 if( psz_hostname == NULL )
1046 psz_host = strdup( psz_hostname );
1047 if( psz_host == NULL )
1049 msg_Err( p_this, "memory error" );
1053 /* to be sure to avoid multiple creation */
1054 var_Create( p_this->p_libvlc_global, "httpd_mutex", VLC_VAR_MUTEX );
1055 var_Get( p_this->p_libvlc_global, "httpd_mutex", &lockval );
1056 vlc_mutex_lock( lockval.p_address );
1058 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1060 msg_Info( p_this, "creating httpd" );
1061 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1066 vlc_mutex_unlock( lockval.p_address );
1074 vlc_object_yield( httpd );
1075 vlc_object_attach( httpd, p_this->p_libvlc );
1078 /* verify if it already exist */
1079 for( i = httpd->i_host - 1; i >= 0; i-- )
1081 host = httpd->host[i];
1083 /* cannot mix TLS and non-TLS hosts */
1084 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1085 || ( host->i_port != i_port )
1086 || strcmp( host->psz_hostname, psz_hostname ) )
1092 vlc_mutex_unlock( lockval.p_address );
1098 /* determine TLS configuration */
1099 if ( psz_cert != NULL )
1101 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1102 if ( p_tls == NULL )
1104 msg_Err( p_this, "TLS initialization error" );
1108 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1110 msg_Err( p_this, "TLS CA error" );
1114 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1116 msg_Err( p_this, "TLS CRL error" );
1123 /* create the new host */
1124 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1125 VLC_OBJECT_HTTPD_HOST,
1130 host->httpd = httpd;
1131 vlc_mutex_init( httpd, &host->lock );
1134 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1135 if( host->fd == NULL )
1137 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1141 host->i_port = i_port;
1142 host->psz_hostname = psz_host;
1147 host->client = NULL;
1149 host->p_tls = p_tls;
1151 /* create the thread */
1152 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1153 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1155 msg_Err( p_this, "cannot spawn http host thread" );
1159 /* now add it to httpd */
1160 TAB_APPEND( httpd->i_host, httpd->host, host );
1161 vlc_mutex_unlock( lockval.p_address );
1167 if( httpd->i_host <= 0 )
1169 vlc_object_release( httpd );
1170 vlc_object_detach( httpd );
1171 vlc_object_destroy( httpd );
1173 vlc_mutex_unlock( lockval.p_address );
1177 net_ListenClose( host->fd );
1178 vlc_mutex_destroy( &host->lock );
1179 vlc_object_destroy( host );
1183 tls_ServerDelete( p_tls );
1189 void httpd_HostDelete( httpd_host_t *host )
1191 httpd_t *httpd = host->httpd;
1192 vlc_value_t lockval;
1195 var_Get( httpd->p_libvlc_global, "httpd_mutex", &lockval );
1196 vlc_mutex_lock( lockval.p_address );
1199 if( host->i_ref > 0 )
1202 vlc_mutex_unlock( lockval.p_address );
1203 msg_Dbg( host, "httpd_HostDelete: host still used" );
1206 TAB_REMOVE( httpd->i_host, httpd->host, host );
1209 vlc_thread_join( host );
1211 msg_Dbg( host, "HTTP host removed" );
1213 for( i = 0; i < host->i_url; i++ )
1215 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1217 for( i = 0; i < host->i_client; i++ )
1219 httpd_client_t *cl = host->client[i];
1220 msg_Warn( host, "client still connected" );
1221 httpd_ClientClean( cl );
1222 TAB_REMOVE( host->i_client, host->client, cl );
1228 if( host->p_tls != NULL)
1229 tls_ServerDelete( host->p_tls );
1231 net_ListenClose( host->fd );
1232 free( host->psz_hostname );
1234 vlc_mutex_destroy( &host->lock );
1235 vlc_object_destroy( host );
1237 vlc_object_release( httpd );
1238 if( httpd->i_host <= 0 )
1240 msg_Dbg( httpd, "no host left, stopping httpd" );
1241 vlc_object_detach( httpd );
1242 vlc_object_destroy( httpd );
1244 vlc_mutex_unlock( lockval.p_address );
1247 /* register a new url */
1248 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1249 const char *psz_user, const char *psz_password,
1250 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1255 vlc_mutex_lock( &host->lock );
1258 for( i = 0; i < host->i_url; i++ )
1260 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1262 msg_Warn( host->httpd,
1263 "cannot add '%s' (url already defined)", psz_url );
1264 vlc_mutex_unlock( &host->lock );
1270 url = malloc( sizeof( httpd_url_t ) );
1273 vlc_mutex_init( host->httpd, &url->lock );
1274 url->psz_url = strdup( psz_url );
1275 url->psz_user = strdup( psz_user ? psz_user : "" );
1276 url->psz_password = strdup( psz_password ? psz_password : "" );
1277 url->p_acl = ACL_Duplicate( host, p_acl );
1278 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1280 url->catch[i].cb = NULL;
1281 url->catch[i].p_sys = NULL;
1284 TAB_APPEND( host->i_url, host->url, url );
1285 vlc_mutex_unlock( &host->lock );
1290 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1291 const char *psz_user, const char *psz_password,
1292 const vlc_acl_t *p_acl )
1294 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1295 psz_password, p_acl, VLC_FALSE );
1298 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1299 const char *psz_user, const char *psz_password,
1300 const vlc_acl_t *p_acl )
1302 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1303 psz_password, p_acl, VLC_TRUE );
1306 /* register callback on a url */
1307 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1308 httpd_callback_sys_t *p_sys )
1310 vlc_mutex_lock( &url->lock );
1311 url->catch[i_msg].cb = cb;
1312 url->catch[i_msg].p_sys= p_sys;
1313 vlc_mutex_unlock( &url->lock );
1319 void httpd_UrlDelete( httpd_url_t *url )
1321 httpd_host_t *host = url->host;
1324 vlc_mutex_lock( &host->lock );
1325 TAB_REMOVE( host->i_url, host->url, url );
1327 vlc_mutex_destroy( &url->lock );
1328 free( url->psz_url );
1329 free( url->psz_user );
1330 free( url->psz_password );
1331 ACL_Destroy( url->p_acl );
1333 for( i = 0; i < host->i_client; i++ )
1335 httpd_client_t *client = host->client[i];
1337 if( client->url == url )
1339 /* TODO complete it */
1340 msg_Warn( host, "force closing connections" );
1341 httpd_ClientClean( client );
1342 TAB_REMOVE( host->i_client, host->client, client );
1348 vlc_mutex_unlock( &host->lock );
1351 void httpd_MsgInit( httpd_message_t *msg )
1354 msg->i_type = HTTPD_MSG_NONE;
1355 msg->i_proto = HTTPD_PROTO_NONE;
1356 msg->i_version = -1;
1359 msg->psz_status = NULL;
1361 msg->psz_url = NULL;
1362 msg->psz_args = NULL;
1364 msg->i_channel = -1;
1371 msg->i_body_offset = 0;
1376 void httpd_MsgClean( httpd_message_t *msg )
1380 if( msg->psz_status )
1382 free( msg->psz_status );
1386 free( msg->psz_url );
1390 free( msg->psz_args );
1392 for( i = 0; i < msg->i_name; i++ )
1394 free( msg->name[i] );
1395 free( msg->value[i] );
1407 free( msg->p_body );
1409 httpd_MsgInit( msg );
1412 const char *httpd_MsgGet( httpd_message_t *msg, const char *name )
1416 for( i = 0; i < msg->i_name; i++ )
1418 if( !strcasecmp( msg->name[i], name ))
1420 return msg->value[i];
1426 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1431 va_start( args, psz_value );
1432 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1433 vasprintf( &value, psz_value, args );
1436 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1437 value = calloc( i_size, sizeof( char ) );
1438 vsnprintf( value, i_size, psz_value, args );
1439 value[i_size - 1] = 0;
1444 name = strdup( name );
1446 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1447 TAB_APPEND( msg->i_value, msg->value, value );
1450 static void httpd_ClientInit( httpd_client_t *cl )
1452 cl->i_state = HTTPD_CLIENT_RECEIVING;
1453 cl->i_activity_date = mdate();
1454 cl->i_activity_timeout = I64C(10000000);
1455 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1457 cl->p_buffer = malloc( cl->i_buffer_size );
1458 cl->i_mode = HTTPD_CLIENT_FILE;
1459 cl->b_read_waiting = VLC_FALSE;
1461 httpd_MsgInit( &cl->query );
1462 httpd_MsgInit( &cl->answer );
1465 void httpd_ClientModeStream( httpd_client_t *cl )
1467 cl->i_mode = HTTPD_CLIENT_STREAM;
1470 void httpd_ClientModeBidir( httpd_client_t *cl )
1472 cl->i_mode = HTTPD_CLIENT_BIDIR;
1475 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1477 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1480 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1482 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1485 static void httpd_ClientClean( httpd_client_t *cl )
1489 if( cl->p_tls != NULL )
1490 tls_ServerSessionClose( cl->p_tls );
1491 net_Close( cl->fd );
1495 httpd_MsgClean( &cl->answer );
1496 httpd_MsgClean( &cl->query );
1500 free( cl->p_buffer );
1501 cl->p_buffer = NULL;
1505 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1507 tls_session_t *p_tls )
1509 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1511 if( !cl ) return NULL;
1515 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1516 cl->i_sock_size = i_sock_size;
1520 httpd_ClientInit( cl );
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;
1847 cl->i_activity_date = mdate();
1849 /* XXX: for QT I have to disable timeout. Try to find why */
1850 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1851 cl->i_activity_timeout = 0;
1853 #if 0 /* Debugging only */
1854 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1858 fprintf( stderr, "received new request\n" );
1859 fprintf( stderr, " - proto=%s\n",
1860 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1861 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1862 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1863 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1865 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1866 cl->query.psz_status );
1868 else if( cl->query.i_type != HTTPD_MSG_NONE )
1870 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1872 for( i = 0; i < cl->query.i_name; i++ )
1874 fprintf( stderr, " - option name='%s' value='%s'\n",
1875 cl->query.name[i], cl->query.value[i] );
1881 static void httpd_ClientSend( httpd_client_t *cl )
1886 if( cl->i_buffer < 0 )
1888 /* We need to create the header */
1892 i_size = strlen( "HTTP/1.") + 10 + 10 +
1893 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1894 for( i = 0; i < cl->answer.i_name; i++ )
1896 i_size += strlen( cl->answer.name[i] ) + 2 +
1897 strlen( cl->answer.value[i] ) + 2;
1900 if( cl->i_buffer_size < i_size )
1902 cl->i_buffer_size = i_size;
1903 free( cl->p_buffer );
1904 cl->p_buffer = malloc( i_size );
1906 p = (char *)cl->p_buffer;
1908 p += sprintf( p, "%s/1.%d %d %s\r\n",
1909 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1910 cl->answer.i_version,
1911 cl->answer.i_status, cl->answer.psz_status );
1912 for( i = 0; i < cl->answer.i_name; i++ )
1914 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1915 cl->answer.value[i] );
1917 p += sprintf( p, "\r\n" );
1920 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1922 /*fprintf( stderr, "sending answer\n" );
1923 fprintf( stderr, "%s", cl->p_buffer );*/
1926 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1927 cl->i_buffer_size - cl->i_buffer );
1930 cl->i_activity_date = mdate();
1931 cl->i_buffer += i_len;
1933 if( cl->i_buffer >= cl->i_buffer_size )
1935 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1936 !cl->b_read_waiting )
1938 /* catch more body data */
1939 int i_msg = cl->query.i_type;
1940 int64_t i_offset = cl->answer.i_body_offset;
1942 httpd_MsgClean( &cl->answer );
1943 cl->answer.i_body_offset = i_offset;
1945 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1946 &cl->answer, &cl->query );
1949 if( cl->answer.i_body > 0 )
1951 /* send the body data */
1952 free( cl->p_buffer );
1953 cl->p_buffer = cl->answer.p_body;
1954 cl->i_buffer_size = cl->answer.i_body;
1957 cl->answer.i_body = 0;
1958 cl->answer.p_body = NULL;
1963 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1969 #if defined( WIN32 ) || defined( UNDER_CE )
1970 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1972 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1976 cl->i_state = HTTPD_CLIENT_DEAD;
1981 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1983 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1986 cl->i_state = HTTPD_CLIENT_RECEIVING;
1990 cl->i_state = HTTPD_CLIENT_DEAD;
1995 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1999 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2001 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2004 cl->i_state = HTTPD_CLIENT_RECEIVING;
2008 cl->i_state = HTTPD_CLIENT_DEAD;
2013 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2018 static void httpd_HostThread( httpd_host_t *host )
2020 tls_session_t *p_tls = NULL;
2022 host->p_total_counter = stats_CounterCreate( host,
2023 VLC_VAR_INTEGER, STATS_COUNTER );
2024 host->p_active_counter = stats_CounterCreate( host,
2025 VLC_VAR_INTEGER, STATS_COUNTER );
2027 while( !host->b_die )
2029 struct timeval timeout;
2032 /* FIXME: (too) many int variables */
2034 int i_handle_max = -1;
2037 int b_low_delay = 0;
2039 if( host->i_url <= 0 )
2046 /* built a set of handle to select */
2047 FD_ZERO( &fds_read );
2048 FD_ZERO( &fds_write );
2050 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2052 FD_SET( fd, &fds_read );
2053 if( fd > i_handle_max )
2057 /* prepare a new TLS session */
2058 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2059 p_tls = tls_ServerSessionPrepare( host->p_tls );
2061 /* add all socket that should be read/write and close dead connection */
2062 vlc_mutex_lock( &host->lock );
2063 for( i_client = 0; i_client < host->i_client; i_client++ )
2065 httpd_client_t *cl = host->client[i_client];
2067 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2068 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2069 ( cl->i_activity_timeout > 0 &&
2070 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2072 httpd_ClientClean( cl );
2073 stats_UpdateInteger( host, host->p_active_counter, -1, NULL );
2074 TAB_REMOVE( host->i_client, host->client, cl );
2079 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2080 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2082 FD_SET( cl->fd, &fds_read );
2083 i_handle_max = __MAX( i_handle_max, cl->fd );
2085 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2086 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2088 FD_SET( cl->fd, &fds_write );
2089 i_handle_max = __MAX( i_handle_max, cl->fd );
2091 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2093 httpd_message_t *answer = &cl->answer;
2094 httpd_message_t *query = &cl->query;
2095 int i_msg = query->i_type;
2097 httpd_MsgInit( answer );
2099 /* Handle what we received */
2100 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2101 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2103 /* we can only receive request from client when not
2106 cl->i_state = HTTPD_CLIENT_DEAD;
2108 else if( i_msg == HTTPD_MSG_ANSWER )
2110 /* We are in BIDIR mode, trigger the callback and then
2111 * check for new data */
2112 if( cl->url && cl->url->catch[i_msg].cb )
2114 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2117 cl->i_state = HTTPD_CLIENT_WAITING;
2119 else if( i_msg == HTTPD_MSG_CHANNEL )
2121 /* We are in BIDIR mode, trigger the callback and then
2122 * check for new data */
2123 if( cl->url && cl->url->catch[i_msg].cb )
2125 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2128 cl->i_state = HTTPD_CLIENT_WAITING;
2130 else if( i_msg == HTTPD_MSG_OPTIONS )
2132 const char *psz_cseq;
2136 answer->i_proto = query->i_proto ;
2137 answer->i_type = HTTPD_MSG_ANSWER;
2138 answer->i_version= 0;
2139 answer->i_status = 200;
2140 answer->psz_status = strdup( "Ok" );
2143 answer->p_body = NULL;
2145 psz_cseq = httpd_MsgGet( query, "Cseq" );
2147 i_cseq = atoi( psz_cseq );
2150 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2151 httpd_MsgAdd( answer, "Server", "VLC Server" );
2152 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2153 "TEARDOWN, PLAY, PAUSE" );
2154 httpd_MsgAdd( answer, "Content-Length", "%d",
2157 cl->i_buffer = -1; /* Force the creation of the answer in
2158 * httpd_ClientSend */
2159 cl->i_state = HTTPD_CLIENT_SENDING;
2161 else if( i_msg == HTTPD_MSG_NONE )
2163 if( query->i_proto == HTTPD_PROTO_NONE )
2166 cl->i_state = HTTPD_CLIENT_DEAD;
2173 answer->i_proto = query->i_proto ;
2174 answer->i_type = HTTPD_MSG_ANSWER;
2175 answer->i_version= 0;
2176 answer->i_status = 501;
2177 answer->psz_status = strdup( "Unimplemented" );
2179 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2180 answer->p_body = (uint8_t *)p;
2181 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2183 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2184 cl->i_state = HTTPD_CLIENT_SENDING;
2189 vlc_bool_t b_auth_failed = VLC_FALSE;
2190 vlc_bool_t b_hosts_failed = VLC_FALSE;
2193 /* Search the url and trigger callbacks */
2194 for( i = 0; i < host->i_url; i++ )
2196 httpd_url_t *url = host->url[i];
2198 if( !strcmp( url->psz_url, query->psz_url ) )
2200 if( url->catch[i_msg].cb )
2202 if( answer && ( url->p_acl != NULL ) )
2204 char ip[NI_MAXNUMERICHOST];
2206 if( httpd_ClientIP( cl, ip ) != NULL )
2208 if( ACL_Check( url->p_acl, ip ) )
2210 b_hosts_failed = VLC_TRUE;
2215 b_hosts_failed = VLC_TRUE;
2218 if( answer && ( *url->psz_user || *url->psz_password ) )
2220 /* create the headers */
2221 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2225 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2227 && !strncasecmp( b64, "BASIC", 5 ) )
2230 while( *b64 == ' ' )
2234 auth = malloc( strlen(b64) + 1 );
2235 b64_decode( auth, b64 );
2239 auth = strdup( "" );
2242 if( strcmp( id, auth ) )
2244 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2245 /* We fail for all url */
2246 b_auth_failed = VLC_TRUE;
2256 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2258 if( answer->i_proto == HTTPD_PROTO_NONE )
2260 /* Raw answer from a CGI */
2261 cl->i_buffer = cl->i_buffer_size;
2266 /* only one url can answer */
2268 if( cl->url == NULL )
2281 answer->i_proto = query->i_proto;
2282 answer->i_type = HTTPD_MSG_ANSWER;
2283 answer->i_version= 0;
2285 if( b_hosts_failed )
2287 answer->i_status = 403;
2288 answer->psz_status = strdup( "Forbidden" );
2290 else if( b_auth_failed )
2292 answer->i_status = 401;
2293 answer->psz_status = strdup( "Authorization Required" );
2297 /* no url registered */
2298 answer->i_status = 404;
2299 answer->psz_status = strdup( "Not found" );
2302 answer->i_body = httpd_HtmlError (&p,
2305 answer->p_body = (uint8_t *)p;
2307 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2308 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2309 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2312 cl->i_state = HTTPD_CLIENT_SENDING;
2315 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2317 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2319 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2320 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2321 vlc_bool_t b_connection = VLC_FALSE;
2322 vlc_bool_t b_keepalive = VLC_FALSE;
2323 vlc_bool_t b_query = VLC_FALSE;
2326 if( psz_connection )
2328 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2329 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2334 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2337 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2338 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2339 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2340 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2341 !b_query && !b_connection ) )
2343 httpd_MsgClean( &cl->query );
2344 httpd_MsgInit( &cl->query );
2347 cl->i_buffer_size = 1000;
2348 free( cl->p_buffer );
2349 cl->p_buffer = malloc( cl->i_buffer_size );
2350 cl->i_state = HTTPD_CLIENT_RECEIVING;
2354 cl->i_state = HTTPD_CLIENT_DEAD;
2356 httpd_MsgClean( &cl->answer );
2358 else if( cl->b_read_waiting )
2360 /* we have a message waiting for us to read it */
2361 httpd_MsgClean( &cl->answer );
2362 httpd_MsgClean( &cl->query );
2365 cl->i_buffer_size = 1000;
2366 free( cl->p_buffer );
2367 cl->p_buffer = malloc( cl->i_buffer_size );
2368 cl->i_state = HTTPD_CLIENT_RECEIVING;
2369 cl->b_read_waiting = VLC_FALSE;
2373 int64_t i_offset = cl->answer.i_body_offset;
2374 httpd_MsgClean( &cl->answer );
2376 cl->answer.i_body_offset = i_offset;
2377 free( cl->p_buffer );
2378 cl->p_buffer = NULL;
2380 cl->i_buffer_size = 0;
2382 cl->i_state = HTTPD_CLIENT_WAITING;
2385 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2387 int64_t i_offset = cl->answer.i_body_offset;
2388 int i_msg = cl->query.i_type;
2390 httpd_MsgInit( &cl->answer );
2391 cl->answer.i_body_offset = i_offset;
2393 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2394 &cl->answer, &cl->query );
2395 if( cl->answer.i_type != HTTPD_MSG_NONE )
2397 /* we have new data, so reenter send mode */
2399 cl->p_buffer = cl->answer.p_body;
2400 cl->i_buffer_size = cl->answer.i_body;
2401 cl->answer.p_body = NULL;
2402 cl->answer.i_body = 0;
2403 cl->i_state = HTTPD_CLIENT_SENDING;
2407 /* we shouldn't wait too long */
2408 b_low_delay = VLC_TRUE;
2412 /* Special for BIDIR mode we also check reading */
2413 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2414 cl->i_state == HTTPD_CLIENT_SENDING )
2416 FD_SET( cl->fd, &fds_read );
2417 i_handle_max = __MAX( i_handle_max, cl->fd );
2420 vlc_mutex_unlock( &host->lock );
2422 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2424 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2426 i_ret = select( i_handle_max + 1,
2427 &fds_read, &fds_write, NULL, &timeout );
2429 if( (i_ret == -1) && (errno != EINTR) )
2431 msg_Warn( host, "select error: %s", net_strerror( net_errno ) );
2435 else if( i_ret <= 0 )
2440 /* accept new connections */
2441 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2443 if( FD_ISSET( fd, &fds_read ) )
2445 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2446 struct sockaddr_storage sock;
2448 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2454 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &i_state, sizeof (i_state));
2457 /* set this new socket non-block */
2458 #if defined( WIN32 ) || defined( UNDER_CE )
2460 unsigned long i_dummy = 1;
2461 ioctlsocket( fd, FIONBIO, &i_dummy );
2464 fcntl( fd, F_SETFD, FD_CLOEXEC );
2466 int i_val = fcntl( fd, F_GETFL );
2468 O_NONBLOCK | ((i_val != -1) ? i_val : 0) );
2471 if( fd >= FD_SETSIZE )
2480 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2483 msg_Err( host, "Rejecting TLS connection" );
2489 case 1: /* missing input - most likely */
2490 i_state = HTTPD_CLIENT_TLS_HS_IN;
2493 case 2: /* missing output */
2494 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2502 char ip[NI_MAXNUMERICHOST];
2503 stats_UpdateInteger( host, host->p_total_counter,
2505 stats_UpdateInteger( host, host->p_active_counter,
2507 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2508 httpd_ClientIP( cl, ip );
2509 msg_Dbg( host, "Connection from %s", ip );
2511 vlc_mutex_lock( &host->lock );
2512 TAB_APPEND( host->i_client, host->client, cl );
2513 vlc_mutex_unlock( &host->lock );
2516 cl->i_state = i_state; // override state for TLS
2522 /* now try all others socket */
2523 vlc_mutex_lock( &host->lock );
2524 for( i_client = 0; i_client < host->i_client; i_client++ )
2526 httpd_client_t *cl = host->client[i_client];
2527 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2529 httpd_ClientRecv( cl );
2531 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2533 httpd_ClientSend( cl );
2535 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2537 httpd_ClientTlsHsIn( cl );
2539 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2541 httpd_ClientTlsHsOut( cl );
2544 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2545 cl->i_state == HTTPD_CLIENT_SENDING &&
2546 FD_ISSET( cl->fd, &fds_read ) )
2548 cl->b_read_waiting = VLC_TRUE;
2551 vlc_mutex_unlock( &host->lock );
2555 tls_ServerSessionClose( p_tls );
2558 #else /* ENABLE_HTTPD */
2560 /* We just define an empty wrapper */
2561 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2564 msg_Err( a, "HTTP daemon support is disabled" );
2568 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2570 msg_Err( a, "HTTP daemon support is disabled" );
2574 void httpd_HostDelete( httpd_host_t *a )
2578 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2579 char *psz_user, char *psz_password,
2580 const vlc_acl_t *p_acl )
2585 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2586 char *psz_user, char *psz_password,
2587 const vlc_acl_t *p_acl )
2592 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2593 httpd_callback_sys_t *d )
2598 void httpd_UrlDelete( httpd_url_t *a )
2602 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2607 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2612 void httpd_ClientModeStream( httpd_client_t *a )
2616 void httpd_ClientModeBidir( httpd_client_t *a )
2620 void httpd_FileDelete( httpd_file_t *a )
2624 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2625 char *e, httpd_file_callback_t f,
2626 httpd_file_sys_t *g )
2631 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2632 const char *psz_user,
2633 const char *psz_password,
2634 const vlc_acl_t *p_acl,
2635 httpd_handler_callback_t pf_fill,
2636 httpd_handler_sys_t *p_sys )
2641 void httpd_HandlerDelete( httpd_handler_t *handler )
2645 void httpd_RedirectDelete( httpd_redirect_t *a )
2649 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2655 void httpd_StreamDelete( httpd_stream_t *a )
2659 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2664 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2669 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2675 void httpd_MsgInit ( httpd_message_t *a )
2679 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2683 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2688 void httpd_MsgClean( httpd_message_t *a )
2692 #endif /* ENABLE_HTTPD */