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>
50 #if defined( UNDER_CE )
52 #elif defined( WIN32 )
53 # include <winsock2.h>
55 # include <sys/socket.h>
59 /* We need HUGE buffer otherwise TCP throughput is very limited */
60 #define HTTPD_CL_BUFSIZE 1000000
62 #define HTTPD_CL_BUFSIZE 10000
65 static void httpd_ClientClean( httpd_client_t *cl );
81 httpd_callback_sys_t *p_sys;
82 } catch[HTTPD_MSG_MAX];
88 HTTPD_CLIENT_RECEIVING,
89 HTTPD_CLIENT_RECEIVE_DONE,
92 HTTPD_CLIENT_SEND_DONE,
98 HTTPD_CLIENT_TLS_HS_IN,
99 HTTPD_CLIENT_TLS_HS_OUT
105 HTTPD_CLIENT_FILE, /* default */
106 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
107 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
110 struct httpd_client_t
116 struct sockaddr_storage sock;
122 int b_read_waiting; /* stop as soon as possible sending */
124 mtime_t i_activity_date;
125 mtime_t i_activity_timeout;
127 /* buffer for reading header */
133 httpd_message_t query; /* client -> httpd */
134 httpd_message_t answer; /* httpd -> client */
137 tls_session_t *p_tls;
141 /*****************************************************************************
143 *****************************************************************************/
144 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
145 static void b64_decode( char *restrict dest, const char *restrict src )
150 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
151 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
152 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
153 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
154 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
155 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
156 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
157 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
158 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
159 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
160 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
161 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
162 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
163 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
164 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
165 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
168 for( i_level = 0; *src != '\0'; src++ )
172 c = b64[(unsigned int)*src];
184 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
188 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
192 *dest++ = ( ( last &0x03 ) << 6 ) | c;
204 const char *psz_mime;
207 { ".htm", "text/html" },
208 { ".html", "text/html" },
209 { ".txt", "text/plain" },
210 { ".xml", "text/xml" },
211 { ".dtd", "text/dtd" },
213 { ".css", "text/css" },
216 { ".gif", "image/gif" },
217 { ".jpe", "image/jpeg" },
218 { ".jpg", "image/jpeg" },
219 { ".jpeg", "image/jpeg" },
220 { ".png", "image/png" },
221 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
224 { ".avi", "video/avi" },
225 { ".asf", "video/x-ms-asf" },
226 { ".m1a", "audio/mpeg" },
227 { ".m2a", "audio/mpeg" },
228 { ".m1v", "video/mpeg" },
229 { ".m2v", "video/mpeg" },
230 { ".mp2", "audio/mpeg" },
231 { ".mp3", "audio/mpeg" },
232 { ".mpa", "audio/mpeg" },
233 { ".mpg", "video/mpeg" },
234 { ".mpeg", "video/mpeg" },
235 { ".mpe", "video/mpeg" },
236 { ".mov", "video/quicktime" },
237 { ".moov", "video/quicktime" },
238 { ".ogg", "application/ogg" },
239 { ".ogm", "application/ogg" },
240 { ".wav", "audio/wav" },
241 { ".wma", "audio/x-ms-wma" },
242 { ".wmv", "video/x-ms-wmv" },
249 static const char *httpd_MimeFromUrl( const char *psz_url )
254 psz_ext = strrchr( psz_url, '.' );
259 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
261 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
263 return http_mime[i].psz_mime;
267 return "application/octet-stream";
274 const char *psz_reason;
277 static const http_status_info http_reason[] =
279 /*{ 100, "Continue" },
280 { 101, "Switching Protocols" },*/
284 { 203, "Non-Authoritative Information" },
285 { 204, "No Content" },
286 { 205, "Reset Content" },
287 { 206, "Partial Content" },
288 { 250, "Low on Storage Space" },
289 { 300, "Multiple Choices" }*/,
290 { 301, "Moved Permanently" }/*,
291 { 302, "Moved Temporarily" }, - aka "Found"
292 { 303, "See Other" },
293 { 304, "Not Modified" },
294 { 305, "Use Proxy" },
295 { 307, "Temporary Redirect" },
296 { 400, "Bad Request" }*/,
297 { 401, "Unauthorized" }/*,
298 { 402, "Payment Required" }*/,
299 { 403, "Forbidden" },
300 { 404, "Not Found" }/*,
301 { 405, "Method Not Allowed" },
302 { 406, "Not Acceptable" },
303 { 407, "Proxy Authentication Required" },
304 { 408, "Request Time-out" },
307 { 411, "Length Required" },
308 { 412, "Precondition Failed" },
309 { 413, "Request Entity Too Large" },
310 { 414, "Request-URI Too Large" },
311 { 415, "Unsupported Media Type" },
312 { 416, "Requested range not satisfiable" },
313 { 417, "Expectation Failed" },
314 { 451, "Parameter Not Understood" },
315 { 452, "Conference Not Found" },
316 { 453, "Not Enough Bandwidth" }*/,
317 { 454, "Session Not Found" }/*,
318 { 455, "Method Not Valid in This State" },
319 { 456, "Header Field Not Valid for Resource" },
320 { 457, "Invalid Range" },
321 { 458, "Parameter Is Read-Only" },
322 { 459, "Aggregate operation not allowed" },
323 { 460, "Only aggregate operation allowed" }*/,
324 { 461, "Unsupported transport" }/*,
325 { 462, "Destination unreachable" }*/,
326 { 500, "Internal Server Error" },
327 { 501, "Not Implemented" }/*,
328 { 502, "Bad Gateway" }*/,
329 { 503, "Service Unavailable" }/*,
330 { 504, "Gateway Time-out" },
331 { 505, "Protocol version not supported" }*/,
335 static const char *psz_fallback_reason[] =
336 { "Continue", "OK", "Found", "Client Error", "Server Error" };
338 static const char *httpd_ReasonFromCode( int i_code )
340 const http_status_info *p;
342 for (p = http_reason; p->i_code < i_code; p++);
344 if( p->i_code == i_code )
345 return p->psz_reason;
347 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
348 return psz_fallback_reason[(i_code / 100) - 1];
352 static size_t httpd_HtmlError (char **body, int code, const char *url)
354 const char *errname = httpd_ReasonFromCode (code);
355 assert (errname != NULL);
357 int res = asprintf (body,
358 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
359 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
360 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
361 "<html lang=\"en\">\n"
363 "<title>%s</title>\n"
366 "<h1>%d %s%s%s%s</h1>\n"
368 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
370 "</html>\n", errname, code, errname,
371 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
383 /*****************************************************************************
384 * High Level Functions: httpd_file_t
385 *****************************************************************************/
393 httpd_file_callback_t pf_fill;
394 httpd_file_sys_t *p_sys;
398 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
400 httpd_file_t *file = (httpd_file_t*)p_sys;
401 uint8_t *psz_args = query->psz_args;
402 uint8_t **pp_body, *p_body;
403 const char *psz_connection;
404 int *pi_body, i_body;
406 if( answer == NULL || query == NULL )
410 answer->i_proto = HTTPD_PROTO_HTTP;
411 answer->i_version= query->i_version;
412 answer->i_type = HTTPD_MSG_ANSWER;
414 answer->i_status = 200;
415 answer->psz_status = strdup( "OK" );
417 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
418 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
420 if( query->i_type != HTTPD_MSG_HEAD )
422 pp_body = &answer->p_body;
423 pi_body = &answer->i_body;
427 /* The file still needs to be executed. */
434 if( query->i_type == HTTPD_MSG_POST )
436 /* msg_Warn not supported */
439 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
441 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
446 /* We respect client request */
447 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
448 if( psz_connection != NULL )
450 httpd_MsgAdd( answer, "Connection", psz_connection );
453 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
458 httpd_file_t *httpd_FileNew( httpd_host_t *host,
459 const char *psz_url, const char *psz_mime,
460 const char *psz_user, const char *psz_password,
461 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
462 httpd_file_sys_t *p_sys )
464 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
466 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
467 psz_password, p_acl )
474 file->psz_url = strdup( psz_url );
475 if( psz_mime && *psz_mime )
477 file->psz_mime = strdup( psz_mime );
481 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
484 file->pf_fill = pf_fill;
487 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
488 (httpd_callback_sys_t*)file );
489 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
490 (httpd_callback_sys_t*)file );
491 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
492 (httpd_callback_sys_t*)file );
497 void httpd_FileDelete( httpd_file_t *file )
499 httpd_UrlDelete( file->url );
501 free( file->psz_url );
502 free( file->psz_mime );
507 /*****************************************************************************
508 * High Level Functions: httpd_handler_t (for CGIs)
509 *****************************************************************************/
510 struct httpd_handler_t
514 httpd_handler_callback_t pf_fill;
515 httpd_handler_sys_t *p_sys;
519 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
521 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
522 uint8_t *psz_args = query->psz_args;
523 char psz_remote_addr[NI_MAXNUMERICHOST];
525 if( answer == NULL || query == NULL )
529 answer->i_proto = HTTPD_PROTO_NONE;
530 answer->i_type = HTTPD_MSG_ANSWER;
532 /* We do it ourselves, thanks */
533 answer->i_status = 0;
534 answer->psz_status = NULL;
536 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
537 *psz_remote_addr = '\0';
539 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
540 query->i_type, query->p_body, query->i_body,
541 psz_remote_addr, NULL,
542 &answer->p_body, &answer->i_body );
544 if( query->i_type == HTTPD_MSG_HEAD )
546 char *p = (char *)answer->p_body;
547 while ( (p = strchr( p, '\r' )) != NULL )
549 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
550 && p[3] && p[3] == '\n' )
558 answer->i_body = strlen((char*)answer->p_body) + 1;
559 answer->p_body = realloc( answer->p_body, answer->i_body );
563 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
565 int i_status, i_headers;
566 char *psz_headers, *psz_new;
567 const char *psz_status;
569 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
572 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
573 if( *psz_headers ) psz_headers++;
574 if( *psz_headers ) psz_headers++;
575 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
580 psz_headers = (char *)answer->p_body;
581 i_headers = answer->i_body;
589 psz_status = "Unauthorized";
592 if( (i_status < 0) || (i_status > 999) )
594 psz_status = "Undefined";
597 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
598 + strlen(psz_status) + i_headers - 1;
599 psz_new = (char *)malloc( answer->i_body + 1);
600 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
601 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
602 free( answer->p_body );
603 answer->p_body = (uint8_t *)psz_new;
609 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
610 const char *psz_user,
611 const char *psz_password,
612 const vlc_acl_t *p_acl,
613 httpd_handler_callback_t pf_fill,
614 httpd_handler_sys_t *p_sys )
616 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
618 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
619 psz_password, p_acl )
626 handler->pf_fill = pf_fill;
627 handler->p_sys = p_sys;
629 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
630 (httpd_callback_sys_t*)handler );
631 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
632 (httpd_callback_sys_t*)handler );
633 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
634 (httpd_callback_sys_t*)handler );
639 void httpd_HandlerDelete( httpd_handler_t *handler )
641 httpd_UrlDelete( handler->url );
645 /*****************************************************************************
646 * High Level Functions: httpd_redirect_t
647 *****************************************************************************/
648 struct httpd_redirect_t
654 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
655 httpd_client_t *cl, httpd_message_t *answer,
656 httpd_message_t *query )
658 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
661 if( answer == NULL || query == NULL )
665 answer->i_proto = query->i_proto;
666 answer->i_version= query->i_version;
667 answer->i_type = HTTPD_MSG_ANSWER;
668 answer->i_status = 301;
669 answer->psz_status = strdup( "Moved Permanently" );
671 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
672 answer->p_body = (unsigned char *)p_body;
674 /* XXX check if it's ok or we need to set an absolute url */
675 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
677 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
682 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
683 const char *psz_url_src )
685 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
687 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
692 rdir->psz_dst = strdup( psz_url_dst );
694 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
695 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
696 (httpd_callback_sys_t*)rdir );
697 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
698 (httpd_callback_sys_t*)rdir );
699 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
700 (httpd_callback_sys_t*)rdir );
701 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
702 (httpd_callback_sys_t*)rdir );
706 void httpd_RedirectDelete( httpd_redirect_t *rdir )
708 httpd_UrlDelete( rdir->url );
709 free( rdir->psz_dst );
713 /*****************************************************************************
714 * High Level Funtions: httpd_stream_t
715 *****************************************************************************/
716 struct httpd_stream_t
723 /* Header to send as first packet */
727 /* circular buffer */
728 int i_buffer_size; /* buffer size, can't be reallocated smaller */
729 uint8_t *p_buffer; /* buffer */
730 int64_t i_buffer_pos; /* absolute position from begining */
731 int64_t i_buffer_last_pos; /* a new connection will start with that */
734 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
735 httpd_client_t *cl, httpd_message_t *answer,
736 httpd_message_t *query )
738 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
740 if( answer == NULL || query == NULL || cl == NULL )
745 if( answer->i_body_offset > 0 )
751 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
752 answer->i_body_offset );
755 if( answer->i_body_offset >= stream->i_buffer_pos )
757 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
758 return VLC_EGENERIC; /* wait, no data available */
760 if( answer->i_body_offset + stream->i_buffer_size <
761 stream->i_buffer_pos )
763 /* this client isn't fast enough */
765 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
766 answer->i_body_offset, stream->i_buffer_last_pos );
768 answer->i_body_offset = stream->i_buffer_last_pos;
771 i_pos = answer->i_body_offset % stream->i_buffer_size;
772 i_write = stream->i_buffer_pos - answer->i_body_offset;
773 if( i_write > HTTPD_CL_BUFSIZE )
775 i_write = HTTPD_CL_BUFSIZE;
777 else if( i_write <= 0 )
779 return VLC_EGENERIC; /* wait, no data available */
782 /* Don't go past the end of the circular buffer */
783 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
785 /* using HTTPD_MSG_ANSWER -> data available */
786 answer->i_proto = HTTPD_PROTO_HTTP;
787 answer->i_version= 0;
788 answer->i_type = HTTPD_MSG_ANSWER;
790 answer->i_body = i_write;
791 answer->p_body = malloc( i_write );
792 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
794 answer->i_body_offset += i_write;
800 answer->i_proto = HTTPD_PROTO_HTTP;
801 answer->i_version= 0;
802 answer->i_type = HTTPD_MSG_ANSWER;
804 answer->i_status = 200;
805 answer->psz_status = strdup( "OK" );
807 if( query->i_type != HTTPD_MSG_HEAD )
809 httpd_ClientModeStream( cl );
810 vlc_mutex_lock( &stream->lock );
811 /* Send the header */
812 if( stream->i_header > 0 )
814 answer->i_body = stream->i_header;
815 answer->p_body = malloc( stream->i_header );
816 memcpy( answer->p_body, stream->p_header, stream->i_header );
818 answer->i_body_offset = stream->i_buffer_last_pos;
819 vlc_mutex_unlock( &stream->lock );
823 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
824 answer->i_body_offset = 0;
827 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
829 vlc_bool_t b_xplaystream = VLC_FALSE;
832 httpd_MsgAdd( answer, "Content-type", "%s",
833 "application/octet-stream" );
834 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
835 httpd_MsgAdd( answer, "Pragma", "no-cache" );
836 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
837 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
839 /* Check if there is a xPlayStrm=1 */
840 for( i = 0; i < query->i_name; i++ )
842 if( !strcasecmp( query->name[i], "Pragma" ) &&
843 strstr( query->value[i], "xPlayStrm=1" ) )
845 b_xplaystream = VLC_TRUE;
851 answer->i_body_offset = 0;
856 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
858 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
863 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
864 const char *psz_url, const char *psz_mime,
865 const char *psz_user, const char *psz_password,
866 const vlc_acl_t *p_acl )
868 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
870 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
871 psz_password, p_acl )
877 vlc_mutex_init( host, &stream->lock );
878 if( psz_mime && *psz_mime )
880 stream->psz_mime = strdup( psz_mime );
884 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
886 stream->i_header = 0;
887 stream->p_header = NULL;
888 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
889 stream->p_buffer = malloc( stream->i_buffer_size );
890 /* We set to 1 to make life simpler
891 * (this way i_body_offset can never be 0) */
892 stream->i_buffer_pos = 1;
893 stream->i_buffer_last_pos = 1;
895 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
896 (httpd_callback_sys_t*)stream );
897 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
898 (httpd_callback_sys_t*)stream );
899 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
900 (httpd_callback_sys_t*)stream );
905 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
907 vlc_mutex_lock( &stream->lock );
908 if( stream->p_header )
910 free( stream->p_header );
911 stream->p_header = NULL;
913 stream->i_header = i_data;
916 stream->p_header = malloc( i_data );
917 memcpy( stream->p_header, p_data, i_data );
919 vlc_mutex_unlock( &stream->lock );
924 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
929 if( i_data < 0 || p_data == NULL )
933 vlc_mutex_lock( &stream->lock );
935 /* save this pointer (to be used by new connection) */
936 stream->i_buffer_last_pos = stream->i_buffer_pos;
938 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
944 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
946 /* Ok, we can't go past the end of our buffer */
947 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
949 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
954 stream->i_buffer_pos += i_data;
956 vlc_mutex_unlock( &stream->lock );
960 void httpd_StreamDelete( httpd_stream_t *stream )
962 httpd_UrlDelete( stream->url );
963 vlc_mutex_destroy( &stream->lock );
964 if( stream->psz_mime ) free( stream->psz_mime );
965 if( stream->p_header ) free( stream->p_header );
966 if( stream->p_buffer ) free( stream->p_buffer );
970 /*****************************************************************************
972 *****************************************************************************/
973 static void httpd_HostThread( httpd_host_t * );
975 /* create a new host */
976 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
979 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
983 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
985 const char *psz_cert, const char *psz_key,
986 const char *psz_ca, const char *psz_crl )
995 if( psz_hostname == NULL )
998 psz_host = strdup( psz_hostname );
999 if( psz_host == NULL )
1001 msg_Err( p_this, "memory error" );
1005 /* to be sure to avoid multiple creation */
1006 var_Create( p_this->p_libvlc_global, "httpd_mutex", VLC_VAR_MUTEX );
1007 var_Get( p_this->p_libvlc_global, "httpd_mutex", &lockval );
1008 vlc_mutex_lock( lockval.p_address );
1010 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1012 msg_Info( p_this, "creating httpd" );
1013 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
1015 vlc_mutex_unlock( lockval.p_address );
1023 vlc_object_yield( httpd );
1024 vlc_object_attach( httpd, p_this->p_libvlc );
1027 /* verify if it already exist */
1028 for( i = httpd->i_host - 1; i >= 0; i-- )
1030 host = httpd->host[i];
1032 /* cannot mix TLS and non-TLS hosts */
1033 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1034 || ( host->i_port != i_port )
1035 || strcmp( host->psz_hostname, psz_hostname ) )
1041 vlc_mutex_unlock( lockval.p_address );
1047 /* determine TLS configuration */
1048 if ( psz_cert != NULL )
1050 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1051 if ( p_tls == NULL )
1053 msg_Err( p_this, "TLS initialization error" );
1057 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1059 msg_Err( p_this, "TLS CA error" );
1063 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1065 msg_Err( p_this, "TLS CRL error" );
1072 /* create the new host */
1073 host = vlc_object_create( p_this, VLC_OBJECT_HTTPD_HOST );
1074 host->httpd = httpd;
1075 vlc_mutex_init( httpd, &host->lock );
1078 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1079 if( host->fd == NULL )
1081 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1085 host->i_port = i_port;
1086 host->psz_hostname = psz_host;
1091 host->client = NULL;
1093 host->p_tls = p_tls;
1095 /* create the thread */
1096 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1097 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1099 msg_Err( p_this, "cannot spawn http host thread" );
1103 /* now add it to httpd */
1104 TAB_APPEND( httpd->i_host, httpd->host, host );
1105 vlc_mutex_unlock( lockval.p_address );
1111 if( httpd->i_host <= 0 )
1113 vlc_object_release( httpd );
1114 vlc_object_detach( httpd );
1115 vlc_object_destroy( httpd );
1117 vlc_mutex_unlock( lockval.p_address );
1121 net_ListenClose( host->fd );
1122 vlc_mutex_destroy( &host->lock );
1123 vlc_object_destroy( host );
1127 tls_ServerDelete( p_tls );
1133 void httpd_HostDelete( httpd_host_t *host )
1135 httpd_t *httpd = host->httpd;
1136 vlc_value_t lockval;
1139 var_Get( httpd->p_libvlc_global, "httpd_mutex", &lockval );
1140 vlc_mutex_lock( lockval.p_address );
1143 if( host->i_ref > 0 )
1146 vlc_mutex_unlock( lockval.p_address );
1147 msg_Dbg( host, "httpd_HostDelete: host still used" );
1150 TAB_REMOVE( httpd->i_host, httpd->host, host );
1153 vlc_thread_join( host );
1155 msg_Dbg( host, "HTTP host removed" );
1157 for( i = 0; i < host->i_url; i++ )
1159 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1161 for( i = 0; i < host->i_client; i++ )
1163 httpd_client_t *cl = host->client[i];
1164 msg_Warn( host, "client still connected" );
1165 httpd_ClientClean( cl );
1166 TAB_REMOVE( host->i_client, host->client, cl );
1172 if( host->p_tls != NULL)
1173 tls_ServerDelete( host->p_tls );
1175 net_ListenClose( host->fd );
1176 free( host->psz_hostname );
1178 vlc_mutex_destroy( &host->lock );
1179 vlc_object_destroy( host );
1181 vlc_object_release( httpd );
1182 if( httpd->i_host <= 0 )
1184 msg_Dbg( httpd, "no host left, stopping httpd" );
1185 vlc_object_detach( httpd );
1186 vlc_object_destroy( httpd );
1188 vlc_mutex_unlock( lockval.p_address );
1191 /* register a new url */
1192 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1193 const char *psz_user, const char *psz_password,
1194 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1199 vlc_mutex_lock( &host->lock );
1202 for( i = 0; i < host->i_url; i++ )
1204 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1206 msg_Warn( host->httpd,
1207 "cannot add '%s' (url already defined)", psz_url );
1208 vlc_mutex_unlock( &host->lock );
1214 url = malloc( sizeof( httpd_url_t ) );
1217 vlc_mutex_init( host->httpd, &url->lock );
1218 url->psz_url = strdup( psz_url );
1219 url->psz_user = strdup( psz_user ? psz_user : "" );
1220 url->psz_password = strdup( psz_password ? psz_password : "" );
1221 url->p_acl = ACL_Duplicate( host, p_acl );
1222 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1224 url->catch[i].cb = NULL;
1225 url->catch[i].p_sys = NULL;
1228 TAB_APPEND( host->i_url, host->url, url );
1229 vlc_mutex_unlock( &host->lock );
1234 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1235 const char *psz_user, const char *psz_password,
1236 const vlc_acl_t *p_acl )
1238 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1239 psz_password, p_acl, VLC_FALSE );
1242 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1243 const char *psz_user, const char *psz_password,
1244 const vlc_acl_t *p_acl )
1246 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1247 psz_password, p_acl, VLC_TRUE );
1250 /* register callback on a url */
1251 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1252 httpd_callback_sys_t *p_sys )
1254 vlc_mutex_lock( &url->lock );
1255 url->catch[i_msg].cb = cb;
1256 url->catch[i_msg].p_sys= p_sys;
1257 vlc_mutex_unlock( &url->lock );
1263 void httpd_UrlDelete( httpd_url_t *url )
1265 httpd_host_t *host = url->host;
1268 vlc_mutex_lock( &host->lock );
1269 TAB_REMOVE( host->i_url, host->url, url );
1271 vlc_mutex_destroy( &url->lock );
1272 free( url->psz_url );
1273 free( url->psz_user );
1274 free( url->psz_password );
1275 ACL_Destroy( url->p_acl );
1277 for( i = 0; i < host->i_client; i++ )
1279 httpd_client_t *client = host->client[i];
1281 if( client->url == url )
1283 /* TODO complete it */
1284 msg_Warn( host, "force closing connections" );
1285 httpd_ClientClean( client );
1286 TAB_REMOVE( host->i_client, host->client, client );
1292 vlc_mutex_unlock( &host->lock );
1295 void httpd_MsgInit( httpd_message_t *msg )
1298 msg->i_type = HTTPD_MSG_NONE;
1299 msg->i_proto = HTTPD_PROTO_NONE;
1300 msg->i_version = -1;
1303 msg->psz_status = NULL;
1305 msg->psz_url = NULL;
1306 msg->psz_args = NULL;
1308 msg->i_channel = -1;
1315 msg->i_body_offset = 0;
1320 void httpd_MsgClean( httpd_message_t *msg )
1324 if( msg->psz_status )
1326 free( msg->psz_status );
1330 free( msg->psz_url );
1334 free( msg->psz_args );
1336 for( i = 0; i < msg->i_name; i++ )
1338 free( msg->name[i] );
1339 free( msg->value[i] );
1351 free( msg->p_body );
1353 httpd_MsgInit( msg );
1356 const char *httpd_MsgGet( httpd_message_t *msg, const char *name )
1360 for( i = 0; i < msg->i_name; i++ )
1362 if( !strcasecmp( msg->name[i], name ))
1364 return msg->value[i];
1370 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1375 va_start( args, psz_value );
1376 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1377 vasprintf( &value, psz_value, args );
1380 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1381 value = calloc( i_size, sizeof( char ) );
1382 vsnprintf( value, i_size, psz_value, args );
1383 value[i_size - 1] = 0;
1388 name = strdup( name );
1390 TAB_APPEND( msg->i_name, msg->name, name );
1391 TAB_APPEND( msg->i_value, msg->value, value );
1394 static void httpd_ClientInit( httpd_client_t *cl )
1396 cl->i_state = HTTPD_CLIENT_RECEIVING;
1397 cl->i_activity_date = mdate();
1398 cl->i_activity_timeout = I64C(10000000);
1399 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1401 cl->p_buffer = malloc( cl->i_buffer_size );
1402 cl->i_mode = HTTPD_CLIENT_FILE;
1403 cl->b_read_waiting = VLC_FALSE;
1405 httpd_MsgInit( &cl->query );
1406 httpd_MsgInit( &cl->answer );
1409 void httpd_ClientModeStream( httpd_client_t *cl )
1411 cl->i_mode = HTTPD_CLIENT_STREAM;
1414 void httpd_ClientModeBidir( httpd_client_t *cl )
1416 cl->i_mode = HTTPD_CLIENT_BIDIR;
1419 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1421 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1424 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1426 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1429 static void httpd_ClientClean( httpd_client_t *cl )
1433 if( cl->p_tls != NULL )
1434 tls_ServerSessionClose( cl->p_tls );
1435 net_Close( cl->fd );
1439 httpd_MsgClean( &cl->answer );
1440 httpd_MsgClean( &cl->query );
1444 free( cl->p_buffer );
1445 cl->p_buffer = NULL;
1449 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1451 tls_session_t *p_tls )
1453 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1455 if( !cl ) return NULL;
1459 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1460 cl->i_sock_size = i_sock_size;
1464 httpd_ClientInit( cl );
1469 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1471 tls_session_t *p_tls;
1475 return tls_Recv( p_tls, p, i_len );
1477 return recv( cl->fd, p, i_len, 0 );
1480 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1482 tls_session_t *p_tls;
1486 return tls_Send( p_tls, p, i_len );
1488 return send( cl->fd, p, i_len, 0 );
1491 static void httpd_ClientRecv( httpd_client_t *cl )
1495 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1497 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1498 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1502 cl->i_buffer += i_len;
1505 if( cl->i_buffer >= 4 )
1507 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1509 if( cl->p_buffer[0] == '$' )
1511 /* RTSP (rtp over rtsp) */
1512 cl->query.i_proto = HTTPD_PROTO_RTSP;
1513 cl->query.i_type = HTTPD_MSG_CHANNEL;
1514 cl->query.i_channel = cl->p_buffer[1];
1515 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1516 cl->query.p_body = malloc( cl->query.i_body );
1520 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1522 cl->query.i_proto = HTTPD_PROTO_HTTP;
1523 cl->query.i_type = HTTPD_MSG_ANSWER;
1525 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1527 cl->query.i_proto = HTTPD_PROTO_RTSP;
1528 cl->query.i_type = HTTPD_MSG_ANSWER;
1530 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1531 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1532 !memcmp( cl->p_buffer, "POST", 4 ) )
1534 cl->query.i_proto = HTTPD_PROTO_HTTP;
1535 cl->query.i_type = HTTPD_MSG_NONE;
1539 cl->query.i_proto = HTTPD_PROTO_RTSP;
1540 cl->query.i_type = HTTPD_MSG_NONE;
1544 else if( cl->query.i_body > 0 )
1546 /* we are reading the body of a request or a channel */
1547 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1548 cl->query.i_body - cl->i_buffer );
1551 cl->i_buffer += i_len;
1553 if( cl->i_buffer >= cl->query.i_body )
1555 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1560 /* we are reading a header -> char by char */
1563 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1570 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1572 cl->i_buffer_size += 1024;
1573 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1575 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1576 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1580 /* we have finished the header so parse it and set i_body */
1581 cl->p_buffer[cl->i_buffer] = '\0';
1583 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1586 * assume strlen( "HTTP/1.x" ) = 8
1588 cl->query.i_status =
1589 strtol( (char *)&cl->p_buffer[8],
1595 cl->query.psz_status = strdup( p );
1607 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1608 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1609 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1610 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1611 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1612 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1613 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1615 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1616 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1617 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1619 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1624 cl->query.i_type = HTTPD_MSG_NONE;
1626 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1628 for( i = 0; msg_type[i].name != NULL; i++ )
1630 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1631 strlen( msg_type[i].name ) ) )
1633 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1634 cl->query.i_type = msg_type[i].i_type;
1635 if( cl->query.i_proto != msg_type[i].i_proto )
1638 cl->query.i_proto = HTTPD_PROTO_NONE;
1639 cl->query.i_type = HTTPD_MSG_NONE;
1646 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1648 cl->query.i_proto = HTTPD_PROTO_HTTP;
1650 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1652 cl->query.i_proto = HTTPD_PROTO_RTSP;
1664 p2 = strchr( p, ' ' );
1669 if( !strncasecmp( p, "rtsp:", 5 ) )
1671 /* for rtsp url, you have rtsp://localhost:port/path */
1673 while( *p == '/' ) p++;
1674 while( *p && *p != '/' ) p++;
1676 cl->query.psz_url = strdup( p );
1677 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1680 cl->query.psz_args = (uint8_t *)strdup( p3 );
1688 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1690 cl->query.i_proto = HTTPD_PROTO_HTTP;
1691 cl->query.i_version = atoi( p2+7 );
1693 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1695 cl->query.i_proto = HTTPD_PROTO_RTSP;
1696 cl->query.i_version = atoi( p2+7 );
1704 p = strchr( p, '\n' );
1708 while( *p == '\n' || *p == '\r' )
1712 while( p && *p != '\0' )
1715 char *eol = p = strchr( p, '\n' );
1718 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1723 if( ( colon = strchr( line, ':' ) ) )
1729 while( *colon == ' ' )
1733 name = strdup( line );
1734 value = strdup( colon );
1736 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1737 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1739 if( !strcasecmp( name, "Content-Length" ) )
1741 cl->query.i_body = atol( value );
1748 while( *p == '\n' || *p == '\r' )
1755 if( cl->query.i_body > 0 )
1757 /* TODO Mhh, handle the case client will only send a request and close the connection
1758 * to mark and of body (probably only RTSP) */
1759 cl->query.p_body = malloc( cl->query.i_body );
1764 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1770 /* check if the client is to be set to dead */
1771 #if defined( WIN32 ) || defined( UNDER_CE )
1772 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1774 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1777 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1779 /* connection closed -> end of data */
1780 if( cl->query.i_body > 0 )
1782 cl->query.i_body = cl->i_buffer;
1784 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1788 cl->i_state = HTTPD_CLIENT_DEAD;
1791 cl->i_activity_date = mdate();
1793 /* XXX: for QT I have to disable timeout. Try to find why */
1794 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1795 cl->i_activity_timeout = 0;
1797 #if 0 /* Debugging only */
1798 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1802 fprintf( stderr, "received new request\n" );
1803 fprintf( stderr, " - proto=%s\n",
1804 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1805 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1806 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1807 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1809 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1810 cl->query.psz_status );
1812 else if( cl->query.i_type != HTTPD_MSG_NONE )
1814 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1816 for( i = 0; i < cl->query.i_name; i++ )
1818 fprintf( stderr, " - option name='%s' value='%s'\n",
1819 cl->query.name[i], cl->query.value[i] );
1825 static void httpd_ClientSend( httpd_client_t *cl )
1830 if( cl->i_buffer < 0 )
1832 /* We need to create the header */
1836 i_size = strlen( "HTTP/1.") + 10 + 10 +
1837 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1838 for( i = 0; i < cl->answer.i_name; i++ )
1840 i_size += strlen( cl->answer.name[i] ) + 2 +
1841 strlen( cl->answer.value[i] ) + 2;
1844 if( cl->i_buffer_size < i_size )
1846 cl->i_buffer_size = i_size;
1847 free( cl->p_buffer );
1848 cl->p_buffer = malloc( i_size );
1850 p = (char *)cl->p_buffer;
1852 p += sprintf( p, "%s/1.%d %d %s\r\n",
1853 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1854 cl->answer.i_version,
1855 cl->answer.i_status, cl->answer.psz_status );
1856 for( i = 0; i < cl->answer.i_name; i++ )
1858 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1859 cl->answer.value[i] );
1861 p += sprintf( p, "\r\n" );
1864 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1866 /*fprintf( stderr, "sending answer\n" );
1867 fprintf( stderr, "%s", cl->p_buffer );*/
1870 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1871 cl->i_buffer_size - cl->i_buffer );
1874 cl->i_activity_date = mdate();
1875 cl->i_buffer += i_len;
1877 if( cl->i_buffer >= cl->i_buffer_size )
1879 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1880 !cl->b_read_waiting )
1882 /* catch more body data */
1883 int i_msg = cl->query.i_type;
1884 int64_t i_offset = cl->answer.i_body_offset;
1886 httpd_MsgClean( &cl->answer );
1887 cl->answer.i_body_offset = i_offset;
1889 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1890 &cl->answer, &cl->query );
1893 if( cl->answer.i_body > 0 )
1895 /* send the body data */
1896 free( cl->p_buffer );
1897 cl->p_buffer = cl->answer.p_body;
1898 cl->i_buffer_size = cl->answer.i_body;
1901 cl->answer.i_body = 0;
1902 cl->answer.p_body = NULL;
1907 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1913 #if defined( WIN32 ) || defined( UNDER_CE )
1914 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1916 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1920 cl->i_state = HTTPD_CLIENT_DEAD;
1925 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1927 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1930 cl->i_state = HTTPD_CLIENT_RECEIVING;
1934 cl->i_state = HTTPD_CLIENT_DEAD;
1939 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1943 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1945 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1948 cl->i_state = HTTPD_CLIENT_RECEIVING;
1952 cl->i_state = HTTPD_CLIENT_DEAD;
1957 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1962 static void httpd_HostThread( httpd_host_t *host )
1964 tls_session_t *p_tls = NULL;
1966 host->p_total_counter = stats_CounterCreate( host,
1967 VLC_VAR_INTEGER, STATS_COUNTER );
1968 host->p_active_counter = stats_CounterCreate( host,
1969 VLC_VAR_INTEGER, STATS_COUNTER );
1971 while( !host->b_die )
1973 struct timeval timeout;
1976 /* FIXME: (too) many int variables */
1978 int i_handle_max = -1;
1981 int b_low_delay = 0;
1983 if( host->i_url <= 0 )
1990 /* built a set of handle to select */
1991 FD_ZERO( &fds_read );
1992 FD_ZERO( &fds_write );
1994 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1996 FD_SET( fd, &fds_read );
1997 if( fd > i_handle_max )
2001 /* prepare a new TLS session */
2002 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2003 p_tls = tls_ServerSessionPrepare( host->p_tls );
2005 /* add all socket that should be read/write and close dead connection */
2006 vlc_mutex_lock( &host->lock );
2007 for( i_client = 0; i_client < host->i_client; i_client++ )
2009 httpd_client_t *cl = host->client[i_client];
2011 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2012 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2013 ( cl->i_activity_timeout > 0 &&
2014 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2016 httpd_ClientClean( cl );
2017 stats_UpdateInteger( host, host->p_active_counter, -1, NULL );
2018 TAB_REMOVE( host->i_client, host->client, cl );
2023 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2024 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2026 FD_SET( cl->fd, &fds_read );
2027 i_handle_max = __MAX( i_handle_max, cl->fd );
2029 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2030 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2032 FD_SET( cl->fd, &fds_write );
2033 i_handle_max = __MAX( i_handle_max, cl->fd );
2035 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2037 httpd_message_t *answer = &cl->answer;
2038 httpd_message_t *query = &cl->query;
2039 int i_msg = query->i_type;
2041 httpd_MsgInit( answer );
2043 /* Handle what we received */
2044 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2045 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2047 /* we can only receive request from client when not
2050 cl->i_state = HTTPD_CLIENT_DEAD;
2052 else if( i_msg == HTTPD_MSG_ANSWER )
2054 /* We are in BIDIR mode, trigger the callback and then
2055 * check for new data */
2056 if( cl->url && cl->url->catch[i_msg].cb )
2058 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2061 cl->i_state = HTTPD_CLIENT_WAITING;
2063 else if( i_msg == HTTPD_MSG_CHANNEL )
2065 /* We are in BIDIR mode, trigger the callback and then
2066 * check for new data */
2067 if( cl->url && cl->url->catch[i_msg].cb )
2069 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2072 cl->i_state = HTTPD_CLIENT_WAITING;
2074 else if( i_msg == HTTPD_MSG_OPTIONS )
2076 const char *psz_cseq;
2080 answer->i_proto = query->i_proto ;
2081 answer->i_type = HTTPD_MSG_ANSWER;
2082 answer->i_version= 0;
2083 answer->i_status = 200;
2084 answer->psz_status = strdup( "Ok" );
2087 answer->p_body = NULL;
2089 psz_cseq = httpd_MsgGet( query, "Cseq" );
2091 i_cseq = atoi( psz_cseq );
2094 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2095 httpd_MsgAdd( answer, "Server", "VLC Server" );
2096 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2097 "TEARDOWN, PLAY, PAUSE" );
2098 httpd_MsgAdd( answer, "Content-Length", "%d",
2101 cl->i_buffer = -1; /* Force the creation of the answer in
2102 * httpd_ClientSend */
2103 cl->i_state = HTTPD_CLIENT_SENDING;
2105 else if( i_msg == HTTPD_MSG_NONE )
2107 if( query->i_proto == HTTPD_PROTO_NONE )
2110 cl->i_state = HTTPD_CLIENT_DEAD;
2117 answer->i_proto = query->i_proto ;
2118 answer->i_type = HTTPD_MSG_ANSWER;
2119 answer->i_version= 0;
2120 answer->i_status = 501;
2121 answer->psz_status = strdup( "Unimplemented" );
2123 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2124 answer->p_body = (uint8_t *)p;
2125 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2127 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2128 cl->i_state = HTTPD_CLIENT_SENDING;
2133 vlc_bool_t b_auth_failed = VLC_FALSE;
2134 vlc_bool_t b_hosts_failed = VLC_FALSE;
2137 /* Search the url and trigger callbacks */
2138 for( i = 0; i < host->i_url; i++ )
2140 httpd_url_t *url = host->url[i];
2142 if( !strcmp( url->psz_url, query->psz_url ) )
2144 if( url->catch[i_msg].cb )
2146 if( answer && ( url->p_acl != NULL ) )
2148 char ip[NI_MAXNUMERICHOST];
2150 if( httpd_ClientIP( cl, ip ) != NULL )
2152 if( ACL_Check( url->p_acl, ip ) )
2154 b_hosts_failed = VLC_TRUE;
2159 b_hosts_failed = VLC_TRUE;
2162 if( answer && ( *url->psz_user || *url->psz_password ) )
2164 /* create the headers */
2165 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2169 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2171 && !strncasecmp( b64, "BASIC", 5 ) )
2174 while( *b64 == ' ' )
2178 auth = malloc( strlen(b64) + 1 );
2179 b64_decode( auth, b64 );
2183 auth = strdup( "" );
2186 if( strcmp( id, auth ) )
2188 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2189 /* We fail for all url */
2190 b_auth_failed = VLC_TRUE;
2200 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2202 if( answer->i_proto == HTTPD_PROTO_NONE )
2204 /* Raw answer from a CGI */
2205 cl->i_buffer = cl->i_buffer_size;
2210 /* only one url can answer */
2212 if( cl->url == NULL )
2225 answer->i_proto = query->i_proto;
2226 answer->i_type = HTTPD_MSG_ANSWER;
2227 answer->i_version= 0;
2229 if( b_hosts_failed )
2231 answer->i_status = 403;
2232 answer->psz_status = strdup( "Forbidden" );
2234 else if( b_auth_failed )
2236 answer->i_status = 401;
2237 answer->psz_status = strdup( "Authorization Required" );
2241 /* no url registered */
2242 answer->i_status = 404;
2243 answer->psz_status = strdup( "Not found" );
2246 answer->i_body = httpd_HtmlError (&p,
2249 answer->p_body = (uint8_t *)p;
2251 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2252 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2253 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2256 cl->i_state = HTTPD_CLIENT_SENDING;
2259 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2261 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2263 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2264 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2265 vlc_bool_t b_connection = VLC_FALSE;
2266 vlc_bool_t b_keepalive = VLC_FALSE;
2267 vlc_bool_t b_query = VLC_FALSE;
2270 if( psz_connection )
2272 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2273 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2278 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2281 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2282 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2283 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2284 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2285 !b_query && !b_connection ) )
2287 httpd_MsgClean( &cl->query );
2288 httpd_MsgInit( &cl->query );
2291 cl->i_buffer_size = 1000;
2292 free( cl->p_buffer );
2293 cl->p_buffer = malloc( cl->i_buffer_size );
2294 cl->i_state = HTTPD_CLIENT_RECEIVING;
2298 cl->i_state = HTTPD_CLIENT_DEAD;
2300 httpd_MsgClean( &cl->answer );
2302 else if( cl->b_read_waiting )
2304 /* we have a message waiting for us to read it */
2305 httpd_MsgClean( &cl->answer );
2306 httpd_MsgClean( &cl->query );
2309 cl->i_buffer_size = 1000;
2310 free( cl->p_buffer );
2311 cl->p_buffer = malloc( cl->i_buffer_size );
2312 cl->i_state = HTTPD_CLIENT_RECEIVING;
2313 cl->b_read_waiting = VLC_FALSE;
2317 int64_t i_offset = cl->answer.i_body_offset;
2318 httpd_MsgClean( &cl->answer );
2320 cl->answer.i_body_offset = i_offset;
2321 free( cl->p_buffer );
2322 cl->p_buffer = NULL;
2324 cl->i_buffer_size = 0;
2326 cl->i_state = HTTPD_CLIENT_WAITING;
2329 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2331 int64_t i_offset = cl->answer.i_body_offset;
2332 int i_msg = cl->query.i_type;
2334 httpd_MsgInit( &cl->answer );
2335 cl->answer.i_body_offset = i_offset;
2337 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2338 &cl->answer, &cl->query );
2339 if( cl->answer.i_type != HTTPD_MSG_NONE )
2341 /* we have new data, so reenter send mode */
2343 cl->p_buffer = cl->answer.p_body;
2344 cl->i_buffer_size = cl->answer.i_body;
2345 cl->answer.p_body = NULL;
2346 cl->answer.i_body = 0;
2347 cl->i_state = HTTPD_CLIENT_SENDING;
2351 /* we shouldn't wait too long */
2352 b_low_delay = VLC_TRUE;
2356 /* Special for BIDIR mode we also check reading */
2357 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2358 cl->i_state == HTTPD_CLIENT_SENDING )
2360 FD_SET( cl->fd, &fds_read );
2361 i_handle_max = __MAX( i_handle_max, cl->fd );
2364 vlc_mutex_unlock( &host->lock );
2366 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2368 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2370 i_ret = select( i_handle_max + 1,
2371 &fds_read, &fds_write, NULL, &timeout );
2373 if( (i_ret == -1) && (errno != EINTR) )
2375 msg_Warn( host, "select error: %s", net_strerror( net_errno ) );
2379 else if( i_ret <= 0 )
2384 /* accept new connections */
2385 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2387 if( FD_ISSET( fd, &fds_read ) )
2389 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2390 struct sockaddr_storage sock;
2392 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2398 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &i_state, sizeof (i_state));
2401 /* set this new socket non-block */
2402 #if defined( WIN32 ) || defined( UNDER_CE )
2404 unsigned long i_dummy = 1;
2405 ioctlsocket( fd, FIONBIO, &i_dummy );
2408 fcntl( fd, F_SETFD, FD_CLOEXEC );
2410 int i_val = fcntl( fd, F_GETFL );
2412 O_NONBLOCK | ((i_val != -1) ? i_val : 0) );
2415 if( fd >= FD_SETSIZE )
2424 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2427 msg_Err( host, "Rejecting TLS connection" );
2433 case 1: /* missing input - most likely */
2434 i_state = HTTPD_CLIENT_TLS_HS_IN;
2437 case 2: /* missing output */
2438 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2446 char ip[NI_MAXNUMERICHOST];
2447 stats_UpdateInteger( host, host->p_total_counter,
2449 stats_UpdateInteger( host, host->p_active_counter,
2451 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2452 httpd_ClientIP( cl, ip );
2453 msg_Dbg( host, "Connection from %s", ip );
2455 vlc_mutex_lock( &host->lock );
2456 TAB_APPEND( host->i_client, host->client, cl );
2457 vlc_mutex_unlock( &host->lock );
2460 cl->i_state = i_state; // override state for TLS
2466 /* now try all others socket */
2467 vlc_mutex_lock( &host->lock );
2468 for( i_client = 0; i_client < host->i_client; i_client++ )
2470 httpd_client_t *cl = host->client[i_client];
2471 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2473 httpd_ClientRecv( cl );
2475 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2477 httpd_ClientSend( cl );
2479 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2481 httpd_ClientTlsHsIn( cl );
2483 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2485 httpd_ClientTlsHsOut( cl );
2488 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2489 cl->i_state == HTTPD_CLIENT_SENDING &&
2490 FD_ISSET( cl->fd, &fds_read ) )
2492 cl->b_read_waiting = VLC_TRUE;
2495 vlc_mutex_unlock( &host->lock );
2499 tls_ServerSessionClose( p_tls );
2502 #else /* ENABLE_HTTPD */
2504 /* We just define an empty wrapper */
2505 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2508 msg_Err( a, "HTTP daemon support is disabled" );
2512 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2514 msg_Err( a, "HTTP daemon support is disabled" );
2518 void httpd_HostDelete( httpd_host_t *a )
2522 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2523 char *psz_user, char *psz_password,
2524 const vlc_acl_t *p_acl )
2529 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2530 char *psz_user, char *psz_password,
2531 const vlc_acl_t *p_acl )
2536 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2537 httpd_callback_sys_t *d )
2542 void httpd_UrlDelete( httpd_url_t *a )
2546 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2551 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2556 void httpd_ClientModeStream( httpd_client_t *a )
2560 void httpd_ClientModeBidir( httpd_client_t *a )
2564 void httpd_FileDelete( httpd_file_t *a )
2568 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2569 char *e, httpd_file_callback_t f,
2570 httpd_file_sys_t *g )
2575 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2576 const char *psz_user,
2577 const char *psz_password,
2578 const vlc_acl_t *p_acl,
2579 httpd_handler_callback_t pf_fill,
2580 httpd_handler_sys_t *p_sys )
2585 void httpd_HandlerDelete( httpd_handler_t *handler )
2589 void httpd_RedirectDelete( httpd_redirect_t *a )
2593 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2599 void httpd_StreamDelete( httpd_stream_t *a )
2603 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2608 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2613 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2619 void httpd_MsgInit ( httpd_message_t *a )
2623 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2627 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2632 void httpd_MsgClean( httpd_message_t *a )
2636 #endif /* ENABLE_HTTPD */