1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
5 * Copyright © 2004-2007 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
32 #include <vlc_httpd.h>
33 #include <vlc_network.h>
36 #include <vlc_strings.h>
37 #include "../libvlc.h"
54 #if defined( UNDER_CE )
56 #elif defined( WIN32 )
57 # include <winsock2.h>
59 # include <sys/socket.h>
63 /* We need HUGE buffer otherwise TCP throughput is very limited */
64 #define HTTPD_CL_BUFSIZE 1000000
66 #define HTTPD_CL_BUFSIZE 10000
69 static void httpd_ClientClean( httpd_client_t *cl );
80 /* each host run in his own thread */
90 /* address/port and socket for listening at connections */
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
166 int b_read_waiting; /* stop as soon as possible sending */
168 mtime_t i_activity_date;
169 mtime_t i_activity_timeout;
171 /* buffer for reading header */
177 httpd_message_t query; /* client -> httpd */
178 httpd_message_t answer; /* httpd -> client */
181 tls_session_t *p_tls;
185 /*****************************************************************************
187 *****************************************************************************/
190 const char psz_ext[8];
191 const char *psz_mime;
194 { ".htm", "text/html" },
195 { ".html", "text/html" },
196 { ".txt", "text/plain" },
197 { ".xml", "text/xml" },
198 { ".dtd", "text/dtd" },
200 { ".css", "text/css" },
203 { ".gif", "image/gif" },
204 { ".jpe", "image/jpeg" },
205 { ".jpg", "image/jpeg" },
206 { ".jpeg", "image/jpeg" },
207 { ".png", "image/png" },
208 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
211 { ".avi", "video/avi" },
212 { ".asf", "video/x-ms-asf" },
213 { ".m1a", "audio/mpeg" },
214 { ".m2a", "audio/mpeg" },
215 { ".m1v", "video/mpeg" },
216 { ".m2v", "video/mpeg" },
217 { ".mp2", "audio/mpeg" },
218 { ".mp3", "audio/mpeg" },
219 { ".mpa", "audio/mpeg" },
220 { ".mpg", "video/mpeg" },
221 { ".mpeg", "video/mpeg" },
222 { ".mpe", "video/mpeg" },
223 { ".mov", "video/quicktime" },
224 { ".moov", "video/quicktime" },
225 { ".ogg", "application/ogg" },
226 { ".ogm", "application/ogg" },
227 { ".wav", "audio/wav" },
228 { ".wma", "audio/x-ms-wma" },
229 { ".wmv", "video/x-ms-wmv" },
236 static const char *httpd_MimeFromUrl( const char *psz_url )
241 psz_ext = strrchr( psz_url, '.' );
246 for( i = 0; http_mime[i].psz_ext[0] ; i++ )
248 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
250 return http_mime[i].psz_mime;
254 return "application/octet-stream";
261 const char psz_reason[36];
264 static const http_status_info http_reason[] =
266 /*{ 100, "Continue" },
267 { 101, "Switching Protocols" },*/
269 /*{ 201, "Created" },
271 { 203, "Non-authoritative information" },
272 { 204, "No content" },
273 { 205, "Reset content" },
274 { 206, "Partial content" },
275 { 250, "Low on storage space" },
276 { 300, "Multiple choices" },*/
277 { 301, "Moved permanently" },
278 /*{ 302, "Moved temporarily" },
279 { 303, "See other" },
280 { 304, "Not modified" },
281 { 305, "Use proxy" },
282 { 307, "Temporary redirect" },
283 { 400, "Bad request" },*/
284 { 401, "Unauthorized" },
285 /*{ 402, "Payment Required" },*/
286 { 403, "Forbidden" },
287 { 404, "Not found" },
288 { 405, "Method not allowed" },
289 /*{ 406, "Not acceptable" },
290 { 407, "Proxy authentication required" },
291 { 408, "Request time-out" },
294 { 411, "Length required" },
295 { 412, "Precondition failed" },
296 { 413, "Request entity too large" },
297 { 414, "Request-URI too large" },
298 { 415, "Unsupported media Type" },
299 { 416, "Requested range not satisfiable" },
300 { 417, "Expectation failed" },
301 { 451, "Parameter not understood" },
302 { 452, "Conference not found" },
303 { 453, "Not enough bandwidth" },*/
304 { 454, "Session not found" },
305 /*{ 455, "Method not valid in this State" },
306 { 456, "Header field not valid for resource" },
307 { 457, "Invalid range" },
308 { 458, "Read-only parameter" },*/
309 { 459, "Aggregate operation not allowed" },
310 { 460, "Non-aggregate operation not allowed" },
311 { 461, "Unsupported transport" },
312 /*{ 462, "Destination unreachable" },*/
313 { 500, "Internal server error" },
314 { 501, "Not implemented" },
315 /*{ 502, "Bad gateway" },*/
316 { 503, "Service unavailable" },
317 /*{ 504, "Gateway time-out" },
318 { 505, "Protocol version not supported" },*/
319 { 551, "Option not supported" },
323 static const char psz_fallback_reason[5][16] =
324 { "Continue", "OK", "Found", "Client error", "Server error" };
326 static const char *httpd_ReasonFromCode( unsigned i_code )
328 const http_status_info *p;
330 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
332 for (p = http_reason; i_code > p->i_code; p++);
334 if( p->i_code == i_code )
335 return p->psz_reason;
337 return psz_fallback_reason[(i_code / 100) - 1];
341 static size_t httpd_HtmlError (char **body, int code, const char *url)
343 const char *errname = httpd_ReasonFromCode (code);
344 assert (errname != NULL);
346 int res = asprintf (body,
347 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
348 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
349 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
350 "<html lang=\"en\">\n"
352 "<title>%s</title>\n"
355 "<h1>%d %s%s%s%s</h1>\n"
357 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
359 "</html>\n", errname, code, errname,
360 (url ? " (" : ""), (url ?: ""), (url ? ")" : ""));
372 /*****************************************************************************
373 * High Level Functions: httpd_file_t
374 *****************************************************************************/
382 httpd_file_callback_t pf_fill;
383 httpd_file_sys_t *p_sys;
388 httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
389 httpd_message_t *answer, const httpd_message_t *query )
391 httpd_file_t *file = (httpd_file_t*)p_sys;
392 uint8_t *psz_args = query->psz_args;
393 uint8_t **pp_body, *p_body;
394 const char *psz_connection;
395 int *pi_body, i_body;
397 if( answer == NULL || query == NULL )
401 answer->i_proto = HTTPD_PROTO_HTTP;
402 answer->i_version= query->i_version;
403 answer->i_type = HTTPD_MSG_ANSWER;
405 answer->i_status = 200;
407 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
408 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
410 if( query->i_type != HTTPD_MSG_HEAD )
412 pp_body = &answer->p_body;
413 pi_body = &answer->i_body;
417 /* The file still needs to be executed. */
424 if( query->i_type == HTTPD_MSG_POST )
426 /* msg_Warn not supported */
429 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
431 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
436 /* We respect client request */
437 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
438 if( psz_connection != NULL )
440 httpd_MsgAdd( answer, "Connection", psz_connection );
443 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
448 httpd_file_t *httpd_FileNew( httpd_host_t *host,
449 const char *psz_url, const char *psz_mime,
450 const char *psz_user, const char *psz_password,
451 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
452 httpd_file_sys_t *p_sys )
454 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
456 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
457 psz_password, p_acl )
464 file->psz_url = strdup( psz_url );
465 if( psz_mime && *psz_mime )
467 file->psz_mime = strdup( psz_mime );
471 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
474 file->pf_fill = pf_fill;
477 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
478 (httpd_callback_sys_t*)file );
479 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
480 (httpd_callback_sys_t*)file );
481 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
482 (httpd_callback_sys_t*)file );
487 void httpd_FileDelete( httpd_file_t *file )
489 httpd_UrlDelete( file->url );
491 free( file->psz_url );
492 free( file->psz_mime );
497 /*****************************************************************************
498 * High Level Functions: httpd_handler_t (for CGIs)
499 *****************************************************************************/
500 struct httpd_handler_t
504 httpd_handler_callback_t pf_fill;
505 httpd_handler_sys_t *p_sys;
510 httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
511 httpd_message_t *answer, const httpd_message_t *query )
513 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
514 uint8_t *psz_args = query->psz_args;
515 char psz_remote_addr[NI_MAXNUMERICHOST];
517 if( answer == NULL || query == NULL )
521 answer->i_proto = HTTPD_PROTO_NONE;
522 answer->i_type = HTTPD_MSG_ANSWER;
524 /* We do it ourselves, thanks */
525 answer->i_status = 0;
527 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
528 *psz_remote_addr = '\0';
530 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
531 query->i_type, query->p_body, query->i_body,
532 psz_remote_addr, NULL,
533 &answer->p_body, &answer->i_body );
535 if( query->i_type == HTTPD_MSG_HEAD )
537 char *p = (char *)answer->p_body;
538 while ( (p = strchr( p, '\r' )) != NULL )
540 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
541 && p[3] && p[3] == '\n' )
549 answer->i_body = strlen((char*)answer->p_body) + 1;
550 answer->p_body = realloc( answer->p_body, answer->i_body );
554 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
556 int i_status, i_headers;
557 char *psz_headers, *psz_new;
558 const char *psz_status;
560 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
563 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
564 if( *psz_headers ) psz_headers++;
565 if( *psz_headers ) psz_headers++;
566 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
571 psz_headers = (char *)answer->p_body;
572 i_headers = answer->i_body;
575 psz_status = httpd_ReasonFromCode( i_status );
576 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
577 + strlen(psz_status) + i_headers - 1;
578 psz_new = (char *)malloc( answer->i_body + 1);
579 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
580 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
581 free( answer->p_body );
582 answer->p_body = (uint8_t *)psz_new;
588 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
589 const char *psz_user,
590 const char *psz_password,
591 const vlc_acl_t *p_acl,
592 httpd_handler_callback_t pf_fill,
593 httpd_handler_sys_t *p_sys )
595 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
597 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
598 psz_password, p_acl )
605 handler->pf_fill = pf_fill;
606 handler->p_sys = p_sys;
608 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
609 (httpd_callback_sys_t*)handler );
610 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
611 (httpd_callback_sys_t*)handler );
612 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
613 (httpd_callback_sys_t*)handler );
618 void httpd_HandlerDelete( httpd_handler_t *handler )
620 httpd_UrlDelete( handler->url );
624 /*****************************************************************************
625 * High Level Functions: httpd_redirect_t
626 *****************************************************************************/
627 struct httpd_redirect_t
633 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
634 httpd_client_t *cl, httpd_message_t *answer,
635 const httpd_message_t *query )
637 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
641 if( answer == NULL || query == NULL )
645 answer->i_proto = query->i_proto;
646 answer->i_version= query->i_version;
647 answer->i_type = HTTPD_MSG_ANSWER;
648 answer->i_status = 301;
650 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
651 answer->p_body = (unsigned char *)p_body;
653 /* XXX check if it's ok or we need to set an absolute url */
654 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
656 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
661 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
662 const char *psz_url_src )
664 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
666 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
671 rdir->psz_dst = strdup( psz_url_dst );
673 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
674 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
675 (httpd_callback_sys_t*)rdir );
676 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
677 (httpd_callback_sys_t*)rdir );
678 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
679 (httpd_callback_sys_t*)rdir );
680 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
681 (httpd_callback_sys_t*)rdir );
685 void httpd_RedirectDelete( httpd_redirect_t *rdir )
687 httpd_UrlDelete( rdir->url );
688 free( rdir->psz_dst );
692 /*****************************************************************************
693 * High Level Funtions: httpd_stream_t
694 *****************************************************************************/
695 struct httpd_stream_t
702 /* Header to send as first packet */
706 /* circular buffer */
707 int i_buffer_size; /* buffer size, can't be reallocated smaller */
708 uint8_t *p_buffer; /* buffer */
709 int64_t i_buffer_pos; /* absolute position from begining */
710 int64_t i_buffer_last_pos; /* a new connection will start with that */
713 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
714 httpd_client_t *cl, httpd_message_t *answer,
715 const httpd_message_t *query )
717 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
719 if( answer == NULL || query == NULL || cl == NULL )
724 if( answer->i_body_offset > 0 )
730 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
731 answer->i_body_offset );
734 if( answer->i_body_offset >= stream->i_buffer_pos )
736 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
737 return VLC_EGENERIC; /* wait, no data available */
739 if( answer->i_body_offset + stream->i_buffer_size <
740 stream->i_buffer_pos )
742 /* this client isn't fast enough */
744 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
745 answer->i_body_offset, stream->i_buffer_last_pos );
747 answer->i_body_offset = stream->i_buffer_last_pos;
750 i_pos = answer->i_body_offset % stream->i_buffer_size;
751 i_write = stream->i_buffer_pos - answer->i_body_offset;
752 if( i_write > HTTPD_CL_BUFSIZE )
754 i_write = HTTPD_CL_BUFSIZE;
756 else if( i_write <= 0 )
758 return VLC_EGENERIC; /* wait, no data available */
761 /* Don't go past the end of the circular buffer */
762 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
764 /* using HTTPD_MSG_ANSWER -> data available */
765 answer->i_proto = HTTPD_PROTO_HTTP;
766 answer->i_version= 0;
767 answer->i_type = HTTPD_MSG_ANSWER;
769 answer->i_body = i_write;
770 answer->p_body = malloc( i_write );
771 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
773 answer->i_body_offset += i_write;
779 answer->i_proto = HTTPD_PROTO_HTTP;
780 answer->i_version= 0;
781 answer->i_type = HTTPD_MSG_ANSWER;
783 answer->i_status = 200;
785 if( query->i_type != HTTPD_MSG_HEAD )
787 httpd_ClientModeStream( cl );
788 vlc_mutex_lock( &stream->lock );
789 /* Send the header */
790 if( stream->i_header > 0 )
792 answer->i_body = stream->i_header;
793 answer->p_body = malloc( stream->i_header );
794 memcpy( answer->p_body, stream->p_header, stream->i_header );
796 answer->i_body_offset = stream->i_buffer_last_pos;
797 vlc_mutex_unlock( &stream->lock );
801 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
802 answer->i_body_offset = 0;
805 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
807 vlc_bool_t b_xplaystream = VLC_FALSE;
810 httpd_MsgAdd( answer, "Content-type", "%s",
811 "application/octet-stream" );
812 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
813 httpd_MsgAdd( answer, "Pragma", "no-cache" );
814 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
815 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
817 /* Check if there is a xPlayStrm=1 */
818 for( i = 0; i < query->i_name; i++ )
820 if( !strcasecmp( query->name[i], "Pragma" ) &&
821 strstr( query->value[i], "xPlayStrm=1" ) )
823 b_xplaystream = VLC_TRUE;
829 answer->i_body_offset = 0;
834 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
836 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
841 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
842 const char *psz_url, const char *psz_mime,
843 const char *psz_user, const char *psz_password,
844 const vlc_acl_t *p_acl )
846 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
848 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
849 psz_password, p_acl )
855 vlc_mutex_init( host, &stream->lock );
856 if( psz_mime && *psz_mime )
858 stream->psz_mime = strdup( psz_mime );
862 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
864 stream->i_header = 0;
865 stream->p_header = NULL;
866 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
867 stream->p_buffer = malloc( stream->i_buffer_size );
868 /* We set to 1 to make life simpler
869 * (this way i_body_offset can never be 0) */
870 stream->i_buffer_pos = 1;
871 stream->i_buffer_last_pos = 1;
873 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
874 (httpd_callback_sys_t*)stream );
875 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
876 (httpd_callback_sys_t*)stream );
877 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
878 (httpd_callback_sys_t*)stream );
883 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
885 vlc_mutex_lock( &stream->lock );
886 if( stream->p_header )
888 free( stream->p_header );
889 stream->p_header = NULL;
891 stream->i_header = i_data;
894 stream->p_header = malloc( i_data );
895 memcpy( stream->p_header, p_data, i_data );
897 vlc_mutex_unlock( &stream->lock );
902 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
907 if( i_data < 0 || p_data == NULL )
911 vlc_mutex_lock( &stream->lock );
913 /* save this pointer (to be used by new connection) */
914 stream->i_buffer_last_pos = stream->i_buffer_pos;
916 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
922 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
924 /* Ok, we can't go past the end of our buffer */
925 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
927 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
932 stream->i_buffer_pos += i_data;
934 vlc_mutex_unlock( &stream->lock );
938 void httpd_StreamDelete( httpd_stream_t *stream )
940 httpd_UrlDelete( stream->url );
941 vlc_mutex_destroy( &stream->lock );
942 if( stream->psz_mime ) free( stream->psz_mime );
943 if( stream->p_header ) free( stream->p_header );
944 if( stream->p_buffer ) free( stream->p_buffer );
948 /*****************************************************************************
950 *****************************************************************************/
951 static void httpd_HostThread( httpd_host_t * );
953 /* create a new host */
954 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
957 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
961 static const char psz_object_type[] = "http server";
963 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
965 const char *psz_cert, const char *psz_key,
966 const char *psz_ca, const char *psz_crl )
975 if( psz_hostname == NULL )
978 psz_host = strdup( psz_hostname );
979 if( psz_host == NULL )
981 msg_Err( p_this, "memory error" );
985 /* to be sure to avoid multiple creation */
986 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
987 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
988 vlc_mutex_lock( lockval.p_address );
990 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
992 msg_Info( p_this, "creating httpd" );
993 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
998 vlc_mutex_unlock( lockval.p_address );
1006 vlc_object_yield( httpd );
1007 vlc_object_attach( httpd, p_this->p_libvlc );
1010 /* verify if it already exist */
1011 for( i = httpd->i_host - 1; i >= 0; i-- )
1013 host = httpd->host[i];
1015 /* cannot mix TLS and non-TLS hosts */
1016 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1017 || ( host->i_port != i_port )
1018 || strcmp( host->psz_hostname, psz_hostname ) )
1024 vlc_mutex_unlock( lockval.p_address );
1030 /* determine TLS configuration */
1031 if ( psz_cert != NULL )
1033 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1034 if ( p_tls == NULL )
1036 msg_Err( p_this, "TLS initialization error" );
1040 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1042 msg_Err( p_this, "TLS CA error" );
1046 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1048 msg_Err( p_this, "TLS CRL error" );
1055 /* create the new host */
1056 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1057 VLC_OBJECT_HTTPD_HOST,
1062 host->httpd = httpd;
1063 vlc_mutex_init( httpd, &host->lock );
1066 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1067 if( host->fds == NULL )
1069 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1072 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1074 host->i_port = i_port;
1075 host->psz_hostname = psz_host;
1080 host->client = NULL;
1082 host->p_tls = p_tls;
1084 /* create the thread */
1085 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1086 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1088 msg_Err( p_this, "cannot spawn http host thread" );
1092 /* now add it to httpd */
1093 TAB_APPEND( httpd->i_host, httpd->host, host );
1094 vlc_mutex_unlock( lockval.p_address );
1100 if( httpd->i_host <= 0 )
1102 vlc_object_release( httpd );
1103 vlc_object_detach( httpd );
1104 vlc_object_destroy( httpd );
1106 vlc_mutex_unlock( lockval.p_address );
1110 net_ListenClose( host->fds );
1111 vlc_mutex_destroy( &host->lock );
1112 vlc_object_destroy( host );
1116 tls_ServerDelete( p_tls );
1122 void httpd_HostDelete( httpd_host_t *host )
1124 httpd_t *httpd = host->httpd;
1125 vlc_value_t lockval;
1128 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1129 vlc_mutex_lock( lockval.p_address );
1132 if( host->i_ref > 0 )
1135 vlc_mutex_unlock( lockval.p_address );
1136 msg_Dbg( host, "httpd_HostDelete: host still used" );
1139 TAB_REMOVE( httpd->i_host, httpd->host, host );
1141 vlc_object_kill( host );
1142 vlc_thread_join( host );
1144 msg_Dbg( host, "HTTP host removed" );
1146 for( i = 0; i < host->i_url; i++ )
1148 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1150 for( i = 0; i < host->i_client; i++ )
1152 httpd_client_t *cl = host->client[i];
1153 msg_Warn( host, "client still connected" );
1154 httpd_ClientClean( cl );
1155 TAB_REMOVE( host->i_client, host->client, cl );
1161 if( host->p_tls != NULL)
1162 tls_ServerDelete( host->p_tls );
1164 net_ListenClose( host->fds );
1165 free( host->psz_hostname );
1167 vlc_mutex_destroy( &host->lock );
1168 vlc_object_destroy( host );
1170 vlc_object_release( httpd );
1171 if( httpd->i_host <= 0 )
1173 msg_Dbg( httpd, "no host left, stopping httpd" );
1174 vlc_object_detach( httpd );
1175 vlc_object_destroy( httpd );
1177 vlc_mutex_unlock( lockval.p_address );
1180 /* register a new url */
1181 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1182 const char *psz_user, const char *psz_password,
1183 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1188 vlc_mutex_lock( &host->lock );
1191 for( i = 0; i < host->i_url; i++ )
1193 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1195 msg_Warn( host->httpd,
1196 "cannot add '%s' (url already defined)", psz_url );
1197 vlc_mutex_unlock( &host->lock );
1203 url = malloc( sizeof( httpd_url_t ) );
1206 vlc_mutex_init( host->httpd, &url->lock );
1207 url->psz_url = strdup( psz_url );
1208 url->psz_user = strdup( psz_user ? psz_user : "" );
1209 url->psz_password = strdup( psz_password ? psz_password : "" );
1210 url->p_acl = ACL_Duplicate( host, p_acl );
1211 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1213 url->catch[i].cb = NULL;
1214 url->catch[i].p_sys = NULL;
1217 TAB_APPEND( host->i_url, host->url, url );
1218 vlc_mutex_unlock( &host->lock );
1223 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1224 const char *psz_user, const char *psz_password,
1225 const vlc_acl_t *p_acl )
1227 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1228 psz_password, p_acl, VLC_FALSE );
1231 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1232 const char *psz_user, const char *psz_password,
1233 const vlc_acl_t *p_acl )
1235 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1236 psz_password, p_acl, VLC_TRUE );
1239 /* register callback on a url */
1240 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1241 httpd_callback_sys_t *p_sys )
1243 vlc_mutex_lock( &url->lock );
1244 url->catch[i_msg].cb = cb;
1245 url->catch[i_msg].p_sys= p_sys;
1246 vlc_mutex_unlock( &url->lock );
1252 void httpd_UrlDelete( httpd_url_t *url )
1254 httpd_host_t *host = url->host;
1257 vlc_mutex_lock( &host->lock );
1258 TAB_REMOVE( host->i_url, host->url, url );
1260 vlc_mutex_destroy( &url->lock );
1261 free( url->psz_url );
1262 free( url->psz_user );
1263 free( url->psz_password );
1264 ACL_Destroy( url->p_acl );
1266 for( i = 0; i < host->i_client; i++ )
1268 httpd_client_t *client = host->client[i];
1270 if( client->url == url )
1272 /* TODO complete it */
1273 msg_Warn( host, "force closing connections" );
1274 httpd_ClientClean( client );
1275 TAB_REMOVE( host->i_client, host->client, client );
1281 vlc_mutex_unlock( &host->lock );
1284 void httpd_MsgInit( httpd_message_t *msg )
1287 msg->i_type = HTTPD_MSG_NONE;
1288 msg->i_proto = HTTPD_PROTO_NONE;
1289 msg->i_version = -1;
1293 msg->psz_url = NULL;
1294 msg->psz_args = NULL;
1296 msg->i_channel = -1;
1303 msg->i_body_offset = 0;
1308 void httpd_MsgClean( httpd_message_t *msg )
1314 free( msg->psz_url );
1318 free( msg->psz_args );
1320 for( i = 0; i < msg->i_name; i++ )
1322 free( msg->name[i] );
1323 free( msg->value[i] );
1335 free( msg->p_body );
1337 httpd_MsgInit( msg );
1340 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1344 for( i = 0; i < msg->i_name; i++ )
1346 if( !strcasecmp( msg->name[i], name ))
1348 return msg->value[i];
1354 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1359 va_start( args, psz_value );
1360 if( vasprintf( &value, psz_value, args ) == -1 )
1367 name = strdup( name );
1374 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1375 TAB_APPEND( msg->i_value, msg->value, value );
1378 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1380 cl->i_state = HTTPD_CLIENT_RECEIVING;
1381 cl->i_activity_date = now;
1382 cl->i_activity_timeout = I64C(10000000);
1383 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1385 cl->p_buffer = malloc( cl->i_buffer_size );
1386 cl->i_mode = HTTPD_CLIENT_FILE;
1387 cl->b_read_waiting = VLC_FALSE;
1389 httpd_MsgInit( &cl->query );
1390 httpd_MsgInit( &cl->answer );
1393 void httpd_ClientModeStream( httpd_client_t *cl )
1395 cl->i_mode = HTTPD_CLIENT_STREAM;
1398 void httpd_ClientModeBidir( httpd_client_t *cl )
1400 cl->i_mode = HTTPD_CLIENT_BIDIR;
1403 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1405 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1408 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1410 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1413 static void httpd_ClientClean( httpd_client_t *cl )
1417 if( cl->p_tls != NULL )
1418 tls_ServerSessionClose( cl->p_tls );
1419 net_Close( cl->fd );
1423 httpd_MsgClean( &cl->answer );
1424 httpd_MsgClean( &cl->query );
1428 free( cl->p_buffer );
1429 cl->p_buffer = NULL;
1433 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1435 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1437 if( !cl ) return NULL;
1444 httpd_ClientInit( cl, now );
1449 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1451 tls_session_t *p_tls;
1455 return tls_Recv( p_tls, p, i_len );
1457 return recv( cl->fd, p, i_len, 0 );
1460 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1462 tls_session_t *p_tls;
1466 return tls_Send( p_tls, p, i_len );
1468 return send( cl->fd, p, i_len, 0 );
1471 static void httpd_ClientRecv( httpd_client_t *cl )
1475 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1477 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1478 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1482 cl->i_buffer += i_len;
1485 if( cl->i_buffer >= 4 )
1487 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1489 if( cl->p_buffer[0] == '$' )
1491 /* RTSP (rtp over rtsp) */
1492 cl->query.i_proto = HTTPD_PROTO_RTSP;
1493 cl->query.i_type = HTTPD_MSG_CHANNEL;
1494 cl->query.i_channel = cl->p_buffer[1];
1495 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1496 cl->query.p_body = malloc( cl->query.i_body );
1500 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1502 cl->query.i_proto = HTTPD_PROTO_HTTP;
1503 cl->query.i_type = HTTPD_MSG_ANSWER;
1505 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1507 cl->query.i_proto = HTTPD_PROTO_RTSP;
1508 cl->query.i_type = HTTPD_MSG_ANSWER;
1510 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1511 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1512 !memcmp( cl->p_buffer, "POST", 4 ) )
1514 cl->query.i_proto = HTTPD_PROTO_HTTP;
1515 cl->query.i_type = HTTPD_MSG_NONE;
1519 cl->query.i_proto = HTTPD_PROTO_RTSP;
1520 cl->query.i_type = HTTPD_MSG_NONE;
1524 else if( cl->query.i_body > 0 )
1526 /* we are reading the body of a request or a channel */
1527 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1528 cl->query.i_body - cl->i_buffer );
1531 cl->i_buffer += i_len;
1533 if( cl->i_buffer >= cl->query.i_body )
1535 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1540 /* we are reading a header -> char by char */
1543 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1550 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1552 cl->i_buffer_size += 1024;
1553 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1555 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1556 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1560 /* we have finished the header so parse it and set i_body */
1561 cl->p_buffer[cl->i_buffer] = '\0';
1563 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1566 * assume strlen( "HTTP/1.x" ) = 8
1568 cl->query.i_status =
1569 strtol( (char *)&cl->p_buffer[8],
1578 const char name[16];
1584 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1585 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1586 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1587 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1588 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1589 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1590 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1592 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1593 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1594 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1596 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1601 cl->query.i_type = HTTPD_MSG_NONE;
1603 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1605 for( i = 0; msg_type[i].name[0]; i++ )
1607 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1608 strlen( msg_type[i].name ) ) )
1610 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1611 cl->query.i_type = msg_type[i].i_type;
1612 if( cl->query.i_proto != msg_type[i].i_proto )
1615 cl->query.i_proto = HTTPD_PROTO_NONE;
1616 cl->query.i_type = HTTPD_MSG_NONE;
1623 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1625 cl->query.i_proto = HTTPD_PROTO_HTTP;
1627 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1629 cl->query.i_proto = HTTPD_PROTO_RTSP;
1641 p2 = strchr( p, ' ' );
1646 if( !strncasecmp( p, "rtsp:", 5 ) )
1648 /* for rtsp url, you have rtsp://localhost:port/path */
1650 while( *p == '/' ) p++;
1651 while( *p && *p != '/' ) p++;
1653 cl->query.psz_url = strdup( p );
1654 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1657 cl->query.psz_args = (uint8_t *)strdup( p3 );
1665 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1667 cl->query.i_proto = HTTPD_PROTO_HTTP;
1668 cl->query.i_version = atoi( p2+7 );
1670 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1672 cl->query.i_proto = HTTPD_PROTO_RTSP;
1673 cl->query.i_version = atoi( p2+7 );
1681 p = strchr( p, '\n' );
1685 while( *p == '\n' || *p == '\r' )
1689 while( p && *p != '\0' )
1692 char *eol = p = strchr( p, '\n' );
1695 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1700 if( ( colon = strchr( line, ':' ) ) )
1706 while( *colon == ' ' )
1710 name = strdup( line );
1711 value = strdup( colon );
1713 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1714 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1716 if( !strcasecmp( name, "Content-Length" ) )
1718 cl->query.i_body = atol( value );
1725 while( *p == '\n' || *p == '\r' )
1732 if( cl->query.i_body > 0 )
1734 /* TODO Mhh, handle the case client will only send a request and close the connection
1735 * to mark and of body (probably only RTSP) */
1736 cl->query.p_body = malloc( cl->query.i_body );
1741 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1747 /* check if the client is to be set to dead */
1748 #if defined( WIN32 ) || defined( UNDER_CE )
1749 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1751 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1754 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1756 /* connection closed -> end of data */
1757 if( cl->query.i_body > 0 )
1759 cl->query.i_body = cl->i_buffer;
1761 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1765 cl->i_state = HTTPD_CLIENT_DEAD;
1769 /* XXX: for QT I have to disable timeout. Try to find why */
1770 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1771 cl->i_activity_timeout = 0;
1773 #if 0 /* Debugging only */
1774 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1778 fprintf( stderr, "received new request\n" );
1779 fprintf( stderr, " - proto=%s\n",
1780 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1781 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1782 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1783 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1785 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1786 cl->query.psz_status );
1788 else if( cl->query.i_type != HTTPD_MSG_NONE )
1790 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1792 for( i = 0; i < cl->query.i_name; i++ )
1794 fprintf( stderr, " - option name='%s' value='%s'\n",
1795 cl->query.name[i], cl->query.value[i] );
1801 static void httpd_ClientSend( httpd_client_t *cl )
1806 if( cl->i_buffer < 0 )
1808 /* We need to create the header */
1811 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1813 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1814 for( i = 0; i < cl->answer.i_name; i++ )
1816 i_size += strlen( cl->answer.name[i] ) + 2 +
1817 strlen( cl->answer.value[i] ) + 2;
1820 if( cl->i_buffer_size < i_size )
1822 cl->i_buffer_size = i_size;
1823 free( cl->p_buffer );
1824 cl->p_buffer = malloc( i_size );
1826 p = (char *)cl->p_buffer;
1828 p += sprintf( p, "%s/1.%d %d %s\r\n",
1829 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1830 cl->answer.i_version,
1831 cl->answer.i_status, psz_status );
1832 for( i = 0; i < cl->answer.i_name; i++ )
1834 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1835 cl->answer.value[i] );
1837 p += sprintf( p, "\r\n" );
1840 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1842 /*fprintf( stderr, "sending answer\n" );
1843 fprintf( stderr, "%s", cl->p_buffer );*/
1846 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1847 cl->i_buffer_size - cl->i_buffer );
1850 cl->i_buffer += i_len;
1852 if( cl->i_buffer >= cl->i_buffer_size )
1854 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1855 !cl->b_read_waiting )
1857 /* catch more body data */
1858 int i_msg = cl->query.i_type;
1859 int64_t i_offset = cl->answer.i_body_offset;
1861 httpd_MsgClean( &cl->answer );
1862 cl->answer.i_body_offset = i_offset;
1864 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1865 &cl->answer, &cl->query );
1868 if( cl->answer.i_body > 0 )
1870 /* send the body data */
1871 free( cl->p_buffer );
1872 cl->p_buffer = cl->answer.p_body;
1873 cl->i_buffer_size = cl->answer.i_body;
1876 cl->answer.i_body = 0;
1877 cl->answer.p_body = NULL;
1882 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1888 #if defined( WIN32 ) || defined( UNDER_CE )
1889 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1891 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1895 cl->i_state = HTTPD_CLIENT_DEAD;
1900 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1902 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1905 cl->i_state = HTTPD_CLIENT_RECEIVING;
1909 cl->i_state = HTTPD_CLIENT_DEAD;
1914 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1918 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1920 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1923 cl->i_state = HTTPD_CLIENT_RECEIVING;
1927 cl->i_state = HTTPD_CLIENT_DEAD;
1932 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1937 static void httpd_HostThread( httpd_host_t *host )
1939 tls_session_t *p_tls = NULL;
1940 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1941 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1943 while( !host->b_die )
1945 if( host->i_url <= 0 )
1952 /* prepare a new TLS session */
1953 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1954 p_tls = tls_ServerSessionPrepare( host->p_tls );
1956 struct pollfd ufd[host->nfd + host->i_client];
1958 for( nfd = 0; nfd < host->nfd; nfd++ )
1960 ufd[nfd].fd = host->fds[nfd];
1961 ufd[nfd].events = POLLIN;
1962 ufd[nfd].revents = 0;
1965 /* add all socket that should be read/write and close dead connection */
1966 vlc_mutex_lock( &host->lock );
1967 mtime_t now = mdate();
1968 vlc_bool_t b_low_delay = VLC_FALSE;
1970 for(int i_client = 0; i_client < host->i_client; i_client++ )
1972 httpd_client_t *cl = host->client[i_client];
1973 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1974 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1975 ( cl->i_activity_timeout > 0 &&
1976 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
1978 httpd_ClientClean( cl );
1979 stats_UpdateInteger( host, p_active_counter, -1, NULL );
1980 TAB_REMOVE( host->i_client, host->client, cl );
1986 struct pollfd *pufd = ufd + nfd;
1987 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
1990 pufd->events = pufd->revents = 0;
1992 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1993 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1995 pufd->events = POLLIN;
1997 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1998 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2000 pufd->events = POLLOUT;
2002 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2004 httpd_message_t *answer = &cl->answer;
2005 httpd_message_t *query = &cl->query;
2006 int i_msg = query->i_type;
2008 httpd_MsgInit( answer );
2010 /* Handle what we received */
2011 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2012 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2014 /* we can only receive request from client when not
2017 cl->i_state = HTTPD_CLIENT_DEAD;
2019 else if( i_msg == HTTPD_MSG_ANSWER )
2021 /* We are in BIDIR mode, trigger the callback and then
2022 * check for new data */
2023 if( cl->url && cl->url->catch[i_msg].cb )
2025 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2028 cl->i_state = HTTPD_CLIENT_WAITING;
2030 else if( i_msg == HTTPD_MSG_CHANNEL )
2032 /* We are in BIDIR mode, trigger the callback and then
2033 * check for new data */
2034 if( cl->url && cl->url->catch[i_msg].cb )
2036 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2039 cl->i_state = HTTPD_CLIENT_WAITING;
2041 else if( i_msg == HTTPD_MSG_OPTIONS )
2046 answer->i_proto = query->i_proto ;
2047 answer->i_type = HTTPD_MSG_ANSWER;
2048 answer->i_version= 0;
2050 answer->i_status = 200;
2053 answer->p_body = NULL;
2055 psz = httpd_MsgGet( query, "Require" );
2058 answer->i_status = 551;
2059 httpd_MsgAdd( query, "Unsupported", "%s", psz );
2062 psz = httpd_MsgGet( query, "Cseq" );
2064 httpd_MsgAdd( answer, "Cseq", "%s", psz );
2065 psz = httpd_MsgGet( query, "Timestamp" );
2067 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
2069 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
2070 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2071 "TEARDOWN, PLAY, PAUSE, GET_PARAMETER" );
2072 httpd_MsgAdd( answer, "Content-Length", "0" );
2074 cl->i_buffer = -1; /* Force the creation of the answer in
2075 * httpd_ClientSend */
2076 cl->i_state = HTTPD_CLIENT_SENDING;
2078 else if( i_msg == HTTPD_MSG_NONE )
2080 if( query->i_proto == HTTPD_PROTO_NONE )
2083 cl->i_state = HTTPD_CLIENT_DEAD;
2090 answer->i_proto = query->i_proto ;
2091 answer->i_type = HTTPD_MSG_ANSWER;
2092 answer->i_version= 0;
2093 answer->i_status = 501;
2095 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2096 answer->p_body = (uint8_t *)p;
2097 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2099 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2100 cl->i_state = HTTPD_CLIENT_SENDING;
2105 vlc_bool_t b_auth_failed = VLC_FALSE;
2106 vlc_bool_t b_hosts_failed = VLC_FALSE;
2108 /* Search the url and trigger callbacks */
2109 for(int i = 0; i < host->i_url; i++ )
2111 httpd_url_t *url = host->url[i];
2113 if( !strcmp( url->psz_url, query->psz_url ) )
2115 if( url->catch[i_msg].cb )
2117 if( answer && ( url->p_acl != NULL ) )
2119 char ip[NI_MAXNUMERICHOST];
2121 if( ( httpd_ClientIP( cl, ip ) == NULL )
2122 || ACL_Check( url->p_acl, ip ) )
2124 b_hosts_failed = VLC_TRUE;
2129 if( answer && ( *url->psz_user || *url->psz_password ) )
2131 /* create the headers */
2132 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2133 char *user = NULL, *pass = NULL;
2136 && !strncasecmp( b64, "BASIC", 5 ) )
2139 while( *b64 == ' ' )
2142 user = vlc_b64_decode( b64 );
2145 pass = strchr (user, ':');
2151 if ((user == NULL) || (pass == NULL)
2152 || strcmp (user, url->psz_user)
2153 || strcmp (pass, url->psz_password))
2155 httpd_MsgAdd( answer,
2157 "Basic realm=\"%s\"",
2159 /* We fail for all url */
2160 b_auth_failed = VLC_TRUE;
2168 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2170 if( answer->i_proto == HTTPD_PROTO_NONE )
2172 /* Raw answer from a CGI */
2173 cl->i_buffer = cl->i_buffer_size;
2178 /* only one url can answer */
2180 if( cl->url == NULL )
2193 answer->i_proto = query->i_proto;
2194 answer->i_type = HTTPD_MSG_ANSWER;
2195 answer->i_version= 0;
2197 if( b_hosts_failed )
2199 answer->i_status = 403;
2201 else if( b_auth_failed )
2203 answer->i_status = 401;
2207 /* no url registered */
2208 answer->i_status = 404;
2211 answer->i_body = httpd_HtmlError (&p,
2214 answer->p_body = (uint8_t *)p;
2216 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2217 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2218 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2221 cl->i_state = HTTPD_CLIENT_SENDING;
2224 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2226 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2228 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2229 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2230 vlc_bool_t b_connection = VLC_FALSE;
2231 vlc_bool_t b_keepalive = VLC_FALSE;
2232 vlc_bool_t b_query = VLC_FALSE;
2235 if( psz_connection )
2237 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2238 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2243 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2246 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2247 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2248 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2249 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2250 !b_query && !b_connection ) )
2252 httpd_MsgClean( &cl->query );
2253 httpd_MsgInit( &cl->query );
2256 cl->i_buffer_size = 1000;
2257 free( cl->p_buffer );
2258 cl->p_buffer = malloc( cl->i_buffer_size );
2259 cl->i_state = HTTPD_CLIENT_RECEIVING;
2263 cl->i_state = HTTPD_CLIENT_DEAD;
2265 httpd_MsgClean( &cl->answer );
2267 else if( cl->b_read_waiting )
2269 /* we have a message waiting for us to read it */
2270 httpd_MsgClean( &cl->answer );
2271 httpd_MsgClean( &cl->query );
2274 cl->i_buffer_size = 1000;
2275 free( cl->p_buffer );
2276 cl->p_buffer = malloc( cl->i_buffer_size );
2277 cl->i_state = HTTPD_CLIENT_RECEIVING;
2278 cl->b_read_waiting = VLC_FALSE;
2282 int64_t i_offset = cl->answer.i_body_offset;
2283 httpd_MsgClean( &cl->answer );
2285 cl->answer.i_body_offset = i_offset;
2286 free( cl->p_buffer );
2287 cl->p_buffer = NULL;
2289 cl->i_buffer_size = 0;
2291 cl->i_state = HTTPD_CLIENT_WAITING;
2294 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2296 int64_t i_offset = cl->answer.i_body_offset;
2297 int i_msg = cl->query.i_type;
2299 httpd_MsgInit( &cl->answer );
2300 cl->answer.i_body_offset = i_offset;
2302 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2303 &cl->answer, &cl->query );
2304 if( cl->answer.i_type != HTTPD_MSG_NONE )
2306 /* we have new data, so re-enter send mode */
2308 cl->p_buffer = cl->answer.p_body;
2309 cl->i_buffer_size = cl->answer.i_body;
2310 cl->answer.p_body = NULL;
2311 cl->answer.i_body = 0;
2312 cl->i_state = HTTPD_CLIENT_SENDING;
2316 /* we shouldn't wait too long */
2317 b_low_delay = VLC_TRUE;
2321 /* Special for BIDIR mode we also check reading */
2322 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2323 cl->i_state == HTTPD_CLIENT_SENDING )
2325 pufd->events |= POLLIN;
2328 if (pufd->events != 0)
2331 vlc_mutex_unlock( &host->lock );
2333 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2334 switch( poll( ufd, nfd, b_low_delay ? 20 : 100) )
2339 /* This is most likely a bug */
2340 msg_Err( host, "polling error: %s", strerror (errno));
2347 /* Handle client sockets */
2348 vlc_mutex_lock( &host->lock );
2350 for( int i_client = 0; i_client < host->i_client; i_client++ )
2352 httpd_client_t *cl = host->client[i_client];
2353 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2355 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2357 if( cl->fd != pufd->fd )
2358 continue; // we were not waiting for this client
2359 if( pufd->revents == 0 )
2360 continue; // no event received
2362 cl->i_activity_date = now;
2364 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2366 httpd_ClientRecv( cl );
2368 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2370 httpd_ClientSend( cl );
2372 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2374 httpd_ClientTlsHsIn( cl );
2376 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2378 httpd_ClientTlsHsOut( cl );
2381 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2382 cl->i_state == HTTPD_CLIENT_SENDING &&
2383 (pufd->revents & POLLIN) )
2385 cl->b_read_waiting = VLC_TRUE;
2388 vlc_mutex_unlock( &host->lock );
2390 /* Handle server sockets (accept new connections) */
2391 for( nfd = 0; nfd < host->nfd; nfd++ )
2396 assert (ufd[nfd].fd == host->fds[nfd]);
2398 if( ufd[nfd].revents == 0 )
2402 int kludge[] = { ufd[nfd].fd, -1 };
2403 int fd = net_Accept( host, kludge, 0 );
2409 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2412 msg_Err( host, "Rejecting TLS connection" );
2418 case 1: /* missing input - most likely */
2419 i_state = HTTPD_CLIENT_TLS_HS_IN;
2422 case 2: /* missing output */
2423 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2427 if( (p_tls == NULL) != (host->p_tls == NULL) )
2428 break; // wasted TLS session, cannot accept() anymore
2431 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2432 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2433 cl = httpd_ClientNew( fd, p_tls, now );
2435 vlc_mutex_lock( &host->lock );
2436 TAB_APPEND( host->i_client, host->client, cl );
2437 vlc_mutex_unlock( &host->lock );
2439 cl->i_state = i_state; // override state for TLS
2441 if (host->p_tls != NULL)
2442 break; // cannot accept further without new TLS session
2448 tls_ServerSessionClose( p_tls );
2449 if( p_total_counter )
2450 stats_CounterClean( p_total_counter );
2451 if( p_active_counter )
2452 stats_CounterClean( p_active_counter );
2455 #else /* ENABLE_HTTPD */
2457 /* We just define an empty wrapper */
2458 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2461 msg_Err( a, "HTTP daemon support is disabled" );
2465 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2467 msg_Err( a, "HTTP daemon support is disabled" );
2471 void httpd_HostDelete( httpd_host_t *a )
2475 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2476 char *psz_user, char *psz_password,
2477 const vlc_acl_t *p_acl )
2482 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2483 char *psz_user, char *psz_password,
2484 const vlc_acl_t *p_acl )
2489 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2490 httpd_callback_sys_t *d )
2495 void httpd_UrlDelete( httpd_url_t *a )
2499 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2504 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2509 void httpd_ClientModeStream( httpd_client_t *a )
2513 void httpd_ClientModeBidir( httpd_client_t *a )
2517 void httpd_FileDelete( httpd_file_t *a )
2521 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2522 char *e, httpd_file_callback_t f,
2523 httpd_file_sys_t *g )
2528 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2529 const char *psz_user,
2530 const char *psz_password,
2531 const vlc_acl_t *p_acl,
2532 httpd_handler_callback_t pf_fill,
2533 httpd_handler_sys_t *p_sys )
2538 void httpd_HandlerDelete( httpd_handler_t *handler )
2542 void httpd_RedirectDelete( httpd_redirect_t *a )
2546 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2552 void httpd_StreamDelete( httpd_stream_t *a )
2556 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2561 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2566 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2572 void httpd_MsgInit ( httpd_message_t *a )
2576 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2580 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2585 void httpd_MsgClean( httpd_message_t *a )
2589 #endif /* ENABLE_HTTPD */