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;
387 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
389 httpd_file_t *file = (httpd_file_t*)p_sys;
390 uint8_t *psz_args = query->psz_args;
391 uint8_t **pp_body, *p_body;
392 const char *psz_connection;
393 int *pi_body, i_body;
395 if( answer == NULL || query == NULL )
399 answer->i_proto = HTTPD_PROTO_HTTP;
400 answer->i_version= query->i_version;
401 answer->i_type = HTTPD_MSG_ANSWER;
403 answer->i_status = 200;
405 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
406 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
408 if( query->i_type != HTTPD_MSG_HEAD )
410 pp_body = &answer->p_body;
411 pi_body = &answer->i_body;
415 /* The file still needs to be executed. */
422 if( query->i_type == HTTPD_MSG_POST )
424 /* msg_Warn not supported */
427 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
429 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
434 /* We respect client request */
435 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
436 if( psz_connection != NULL )
438 httpd_MsgAdd( answer, "Connection", psz_connection );
441 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
446 httpd_file_t *httpd_FileNew( httpd_host_t *host,
447 const char *psz_url, const char *psz_mime,
448 const char *psz_user, const char *psz_password,
449 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
450 httpd_file_sys_t *p_sys )
452 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
454 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
455 psz_password, p_acl )
462 file->psz_url = strdup( psz_url );
463 if( psz_mime && *psz_mime )
465 file->psz_mime = strdup( psz_mime );
469 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
472 file->pf_fill = pf_fill;
475 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
476 (httpd_callback_sys_t*)file );
477 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
478 (httpd_callback_sys_t*)file );
479 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
480 (httpd_callback_sys_t*)file );
485 void httpd_FileDelete( httpd_file_t *file )
487 httpd_UrlDelete( file->url );
489 free( file->psz_url );
490 free( file->psz_mime );
495 /*****************************************************************************
496 * High Level Functions: httpd_handler_t (for CGIs)
497 *****************************************************************************/
498 struct httpd_handler_t
502 httpd_handler_callback_t pf_fill;
503 httpd_handler_sys_t *p_sys;
507 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
509 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
510 uint8_t *psz_args = query->psz_args;
511 char psz_remote_addr[NI_MAXNUMERICHOST];
513 if( answer == NULL || query == NULL )
517 answer->i_proto = HTTPD_PROTO_NONE;
518 answer->i_type = HTTPD_MSG_ANSWER;
520 /* We do it ourselves, thanks */
521 answer->i_status = 0;
523 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
524 *psz_remote_addr = '\0';
526 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
527 query->i_type, query->p_body, query->i_body,
528 psz_remote_addr, NULL,
529 &answer->p_body, &answer->i_body );
531 if( query->i_type == HTTPD_MSG_HEAD )
533 char *p = (char *)answer->p_body;
534 while ( (p = strchr( p, '\r' )) != NULL )
536 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
537 && p[3] && p[3] == '\n' )
545 answer->i_body = strlen((char*)answer->p_body) + 1;
546 answer->p_body = realloc( answer->p_body, answer->i_body );
550 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
552 int i_status, i_headers;
553 char *psz_headers, *psz_new;
554 const char *psz_status;
556 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
559 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
560 if( *psz_headers ) psz_headers++;
561 if( *psz_headers ) psz_headers++;
562 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
567 psz_headers = (char *)answer->p_body;
568 i_headers = answer->i_body;
571 psz_status = httpd_ReasonFromCode( i_status );
572 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
573 + strlen(psz_status) + i_headers - 1;
574 psz_new = (char *)malloc( answer->i_body + 1);
575 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
576 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
577 free( answer->p_body );
578 answer->p_body = (uint8_t *)psz_new;
584 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
585 const char *psz_user,
586 const char *psz_password,
587 const vlc_acl_t *p_acl,
588 httpd_handler_callback_t pf_fill,
589 httpd_handler_sys_t *p_sys )
591 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
593 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
594 psz_password, p_acl )
601 handler->pf_fill = pf_fill;
602 handler->p_sys = p_sys;
604 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
605 (httpd_callback_sys_t*)handler );
606 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
607 (httpd_callback_sys_t*)handler );
608 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
609 (httpd_callback_sys_t*)handler );
614 void httpd_HandlerDelete( httpd_handler_t *handler )
616 httpd_UrlDelete( handler->url );
620 /*****************************************************************************
621 * High Level Functions: httpd_redirect_t
622 *****************************************************************************/
623 struct httpd_redirect_t
629 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
630 httpd_client_t *cl, httpd_message_t *answer,
631 httpd_message_t *query )
633 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
637 if( answer == NULL || query == NULL )
641 answer->i_proto = query->i_proto;
642 answer->i_version= query->i_version;
643 answer->i_type = HTTPD_MSG_ANSWER;
644 answer->i_status = 301;
646 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
647 answer->p_body = (unsigned char *)p_body;
649 /* XXX check if it's ok or we need to set an absolute url */
650 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
652 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
657 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
658 const char *psz_url_src )
660 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
662 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
667 rdir->psz_dst = strdup( psz_url_dst );
669 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
670 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
671 (httpd_callback_sys_t*)rdir );
672 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
673 (httpd_callback_sys_t*)rdir );
674 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
675 (httpd_callback_sys_t*)rdir );
676 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
677 (httpd_callback_sys_t*)rdir );
681 void httpd_RedirectDelete( httpd_redirect_t *rdir )
683 httpd_UrlDelete( rdir->url );
684 free( rdir->psz_dst );
688 /*****************************************************************************
689 * High Level Funtions: httpd_stream_t
690 *****************************************************************************/
691 struct httpd_stream_t
698 /* Header to send as first packet */
702 /* circular buffer */
703 int i_buffer_size; /* buffer size, can't be reallocated smaller */
704 uint8_t *p_buffer; /* buffer */
705 int64_t i_buffer_pos; /* absolute position from begining */
706 int64_t i_buffer_last_pos; /* a new connection will start with that */
709 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
710 httpd_client_t *cl, httpd_message_t *answer,
711 httpd_message_t *query )
713 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
715 if( answer == NULL || query == NULL || cl == NULL )
720 if( answer->i_body_offset > 0 )
726 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
727 answer->i_body_offset );
730 if( answer->i_body_offset >= stream->i_buffer_pos )
732 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
733 return VLC_EGENERIC; /* wait, no data available */
735 if( answer->i_body_offset + stream->i_buffer_size <
736 stream->i_buffer_pos )
738 /* this client isn't fast enough */
740 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
741 answer->i_body_offset, stream->i_buffer_last_pos );
743 answer->i_body_offset = stream->i_buffer_last_pos;
746 i_pos = answer->i_body_offset % stream->i_buffer_size;
747 i_write = stream->i_buffer_pos - answer->i_body_offset;
748 if( i_write > HTTPD_CL_BUFSIZE )
750 i_write = HTTPD_CL_BUFSIZE;
752 else if( i_write <= 0 )
754 return VLC_EGENERIC; /* wait, no data available */
757 /* Don't go past the end of the circular buffer */
758 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
760 /* using HTTPD_MSG_ANSWER -> data available */
761 answer->i_proto = HTTPD_PROTO_HTTP;
762 answer->i_version= 0;
763 answer->i_type = HTTPD_MSG_ANSWER;
765 answer->i_body = i_write;
766 answer->p_body = malloc( i_write );
767 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
769 answer->i_body_offset += i_write;
775 answer->i_proto = HTTPD_PROTO_HTTP;
776 answer->i_version= 0;
777 answer->i_type = HTTPD_MSG_ANSWER;
779 answer->i_status = 200;
781 if( query->i_type != HTTPD_MSG_HEAD )
783 httpd_ClientModeStream( cl );
784 vlc_mutex_lock( &stream->lock );
785 /* Send the header */
786 if( stream->i_header > 0 )
788 answer->i_body = stream->i_header;
789 answer->p_body = malloc( stream->i_header );
790 memcpy( answer->p_body, stream->p_header, stream->i_header );
792 answer->i_body_offset = stream->i_buffer_last_pos;
793 vlc_mutex_unlock( &stream->lock );
797 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
798 answer->i_body_offset = 0;
801 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
803 vlc_bool_t b_xplaystream = VLC_FALSE;
806 httpd_MsgAdd( answer, "Content-type", "%s",
807 "application/octet-stream" );
808 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
809 httpd_MsgAdd( answer, "Pragma", "no-cache" );
810 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
811 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
813 /* Check if there is a xPlayStrm=1 */
814 for( i = 0; i < query->i_name; i++ )
816 if( !strcasecmp( query->name[i], "Pragma" ) &&
817 strstr( query->value[i], "xPlayStrm=1" ) )
819 b_xplaystream = VLC_TRUE;
825 answer->i_body_offset = 0;
830 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
832 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
837 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
838 const char *psz_url, const char *psz_mime,
839 const char *psz_user, const char *psz_password,
840 const vlc_acl_t *p_acl )
842 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
844 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
845 psz_password, p_acl )
851 vlc_mutex_init( host, &stream->lock );
852 if( psz_mime && *psz_mime )
854 stream->psz_mime = strdup( psz_mime );
858 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
860 stream->i_header = 0;
861 stream->p_header = NULL;
862 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
863 stream->p_buffer = malloc( stream->i_buffer_size );
864 /* We set to 1 to make life simpler
865 * (this way i_body_offset can never be 0) */
866 stream->i_buffer_pos = 1;
867 stream->i_buffer_last_pos = 1;
869 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
870 (httpd_callback_sys_t*)stream );
871 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
872 (httpd_callback_sys_t*)stream );
873 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
874 (httpd_callback_sys_t*)stream );
879 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
881 vlc_mutex_lock( &stream->lock );
882 if( stream->p_header )
884 free( stream->p_header );
885 stream->p_header = NULL;
887 stream->i_header = i_data;
890 stream->p_header = malloc( i_data );
891 memcpy( stream->p_header, p_data, i_data );
893 vlc_mutex_unlock( &stream->lock );
898 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
903 if( i_data < 0 || p_data == NULL )
907 vlc_mutex_lock( &stream->lock );
909 /* save this pointer (to be used by new connection) */
910 stream->i_buffer_last_pos = stream->i_buffer_pos;
912 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
918 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
920 /* Ok, we can't go past the end of our buffer */
921 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
923 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
928 stream->i_buffer_pos += i_data;
930 vlc_mutex_unlock( &stream->lock );
934 void httpd_StreamDelete( httpd_stream_t *stream )
936 httpd_UrlDelete( stream->url );
937 vlc_mutex_destroy( &stream->lock );
938 if( stream->psz_mime ) free( stream->psz_mime );
939 if( stream->p_header ) free( stream->p_header );
940 if( stream->p_buffer ) free( stream->p_buffer );
944 /*****************************************************************************
946 *****************************************************************************/
947 static void httpd_HostThread( httpd_host_t * );
949 /* create a new host */
950 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
953 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
957 static const char psz_object_type[] = "http server";
959 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
961 const char *psz_cert, const char *psz_key,
962 const char *psz_ca, const char *psz_crl )
971 if( psz_hostname == NULL )
974 psz_host = strdup( psz_hostname );
975 if( psz_host == NULL )
977 msg_Err( p_this, "memory error" );
981 /* to be sure to avoid multiple creation */
982 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
983 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
984 vlc_mutex_lock( lockval.p_address );
986 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
988 msg_Info( p_this, "creating httpd" );
989 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
994 vlc_mutex_unlock( lockval.p_address );
1002 vlc_object_yield( httpd );
1003 vlc_object_attach( httpd, p_this->p_libvlc );
1006 /* verify if it already exist */
1007 for( i = httpd->i_host - 1; i >= 0; i-- )
1009 host = httpd->host[i];
1011 /* cannot mix TLS and non-TLS hosts */
1012 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1013 || ( host->i_port != i_port )
1014 || strcmp( host->psz_hostname, psz_hostname ) )
1020 vlc_mutex_unlock( lockval.p_address );
1026 /* determine TLS configuration */
1027 if ( psz_cert != NULL )
1029 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1030 if ( p_tls == NULL )
1032 msg_Err( p_this, "TLS initialization error" );
1036 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1038 msg_Err( p_this, "TLS CA error" );
1042 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1044 msg_Err( p_this, "TLS CRL error" );
1051 /* create the new host */
1052 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1053 VLC_OBJECT_HTTPD_HOST,
1058 host->httpd = httpd;
1059 vlc_mutex_init( httpd, &host->lock );
1062 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1063 if( host->fds == NULL )
1065 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1068 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1070 host->i_port = i_port;
1071 host->psz_hostname = psz_host;
1076 host->client = NULL;
1078 host->p_tls = p_tls;
1080 /* create the thread */
1081 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1082 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1084 msg_Err( p_this, "cannot spawn http host thread" );
1088 /* now add it to httpd */
1089 TAB_APPEND( httpd->i_host, httpd->host, host );
1090 vlc_mutex_unlock( lockval.p_address );
1096 if( httpd->i_host <= 0 )
1098 vlc_object_release( httpd );
1099 vlc_object_detach( httpd );
1100 vlc_object_destroy( httpd );
1102 vlc_mutex_unlock( lockval.p_address );
1106 net_ListenClose( host->fds );
1107 vlc_mutex_destroy( &host->lock );
1108 vlc_object_destroy( host );
1112 tls_ServerDelete( p_tls );
1118 void httpd_HostDelete( httpd_host_t *host )
1120 httpd_t *httpd = host->httpd;
1121 vlc_value_t lockval;
1124 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1125 vlc_mutex_lock( lockval.p_address );
1128 if( host->i_ref > 0 )
1131 vlc_mutex_unlock( lockval.p_address );
1132 msg_Dbg( host, "httpd_HostDelete: host still used" );
1135 TAB_REMOVE( httpd->i_host, httpd->host, host );
1137 vlc_object_kill( host );
1138 vlc_thread_join( host );
1140 msg_Dbg( host, "HTTP host removed" );
1142 for( i = 0; i < host->i_url; i++ )
1144 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1146 for( i = 0; i < host->i_client; i++ )
1148 httpd_client_t *cl = host->client[i];
1149 msg_Warn( host, "client still connected" );
1150 httpd_ClientClean( cl );
1151 TAB_REMOVE( host->i_client, host->client, cl );
1157 if( host->p_tls != NULL)
1158 tls_ServerDelete( host->p_tls );
1160 net_ListenClose( host->fds );
1161 free( host->psz_hostname );
1163 vlc_mutex_destroy( &host->lock );
1164 vlc_object_destroy( host );
1166 vlc_object_release( httpd );
1167 if( httpd->i_host <= 0 )
1169 msg_Dbg( httpd, "no host left, stopping httpd" );
1170 vlc_object_detach( httpd );
1171 vlc_object_destroy( httpd );
1173 vlc_mutex_unlock( lockval.p_address );
1176 /* register a new url */
1177 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1178 const char *psz_user, const char *psz_password,
1179 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1184 vlc_mutex_lock( &host->lock );
1187 for( i = 0; i < host->i_url; i++ )
1189 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1191 msg_Warn( host->httpd,
1192 "cannot add '%s' (url already defined)", psz_url );
1193 vlc_mutex_unlock( &host->lock );
1199 url = malloc( sizeof( httpd_url_t ) );
1202 vlc_mutex_init( host->httpd, &url->lock );
1203 url->psz_url = strdup( psz_url );
1204 url->psz_user = strdup( psz_user ? psz_user : "" );
1205 url->psz_password = strdup( psz_password ? psz_password : "" );
1206 url->p_acl = ACL_Duplicate( host, p_acl );
1207 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1209 url->catch[i].cb = NULL;
1210 url->catch[i].p_sys = NULL;
1213 TAB_APPEND( host->i_url, host->url, url );
1214 vlc_mutex_unlock( &host->lock );
1219 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1220 const char *psz_user, const char *psz_password,
1221 const vlc_acl_t *p_acl )
1223 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1224 psz_password, p_acl, VLC_FALSE );
1227 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1228 const char *psz_user, const char *psz_password,
1229 const vlc_acl_t *p_acl )
1231 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1232 psz_password, p_acl, VLC_TRUE );
1235 /* register callback on a url */
1236 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1237 httpd_callback_sys_t *p_sys )
1239 vlc_mutex_lock( &url->lock );
1240 url->catch[i_msg].cb = cb;
1241 url->catch[i_msg].p_sys= p_sys;
1242 vlc_mutex_unlock( &url->lock );
1248 void httpd_UrlDelete( httpd_url_t *url )
1250 httpd_host_t *host = url->host;
1253 vlc_mutex_lock( &host->lock );
1254 TAB_REMOVE( host->i_url, host->url, url );
1256 vlc_mutex_destroy( &url->lock );
1257 free( url->psz_url );
1258 free( url->psz_user );
1259 free( url->psz_password );
1260 ACL_Destroy( url->p_acl );
1262 for( i = 0; i < host->i_client; i++ )
1264 httpd_client_t *client = host->client[i];
1266 if( client->url == url )
1268 /* TODO complete it */
1269 msg_Warn( host, "force closing connections" );
1270 httpd_ClientClean( client );
1271 TAB_REMOVE( host->i_client, host->client, client );
1277 vlc_mutex_unlock( &host->lock );
1280 void httpd_MsgInit( httpd_message_t *msg )
1283 msg->i_type = HTTPD_MSG_NONE;
1284 msg->i_proto = HTTPD_PROTO_NONE;
1285 msg->i_version = -1;
1289 msg->psz_url = NULL;
1290 msg->psz_args = NULL;
1292 msg->i_channel = -1;
1299 msg->i_body_offset = 0;
1304 void httpd_MsgClean( httpd_message_t *msg )
1310 free( msg->psz_url );
1314 free( msg->psz_args );
1316 for( i = 0; i < msg->i_name; i++ )
1318 free( msg->name[i] );
1319 free( msg->value[i] );
1331 free( msg->p_body );
1333 httpd_MsgInit( msg );
1336 const char *httpd_MsgGet( httpd_message_t *msg, const char *name )
1340 for( i = 0; i < msg->i_name; i++ )
1342 if( !strcasecmp( msg->name[i], name ))
1344 return msg->value[i];
1350 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1355 va_start( args, psz_value );
1356 if( vasprintf( &value, psz_value, args ) == -1 )
1363 name = strdup( name );
1370 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1371 TAB_APPEND( msg->i_value, msg->value, value );
1374 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1376 cl->i_state = HTTPD_CLIENT_RECEIVING;
1377 cl->i_activity_date = now;
1378 cl->i_activity_timeout = I64C(10000000);
1379 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1381 cl->p_buffer = malloc( cl->i_buffer_size );
1382 cl->i_mode = HTTPD_CLIENT_FILE;
1383 cl->b_read_waiting = VLC_FALSE;
1385 httpd_MsgInit( &cl->query );
1386 httpd_MsgInit( &cl->answer );
1389 void httpd_ClientModeStream( httpd_client_t *cl )
1391 cl->i_mode = HTTPD_CLIENT_STREAM;
1394 void httpd_ClientModeBidir( httpd_client_t *cl )
1396 cl->i_mode = HTTPD_CLIENT_BIDIR;
1399 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1401 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1404 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1406 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1409 static void httpd_ClientClean( httpd_client_t *cl )
1413 if( cl->p_tls != NULL )
1414 tls_ServerSessionClose( cl->p_tls );
1415 net_Close( cl->fd );
1419 httpd_MsgClean( &cl->answer );
1420 httpd_MsgClean( &cl->query );
1424 free( cl->p_buffer );
1425 cl->p_buffer = NULL;
1429 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1431 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1433 if( !cl ) return NULL;
1440 httpd_ClientInit( cl, now );
1445 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1447 tls_session_t *p_tls;
1451 return tls_Recv( p_tls, p, i_len );
1453 return recv( cl->fd, p, i_len, 0 );
1456 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1458 tls_session_t *p_tls;
1462 return tls_Send( p_tls, p, i_len );
1464 return send( cl->fd, p, i_len, 0 );
1467 static void httpd_ClientRecv( httpd_client_t *cl )
1471 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1473 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1474 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1478 cl->i_buffer += i_len;
1481 if( cl->i_buffer >= 4 )
1483 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1485 if( cl->p_buffer[0] == '$' )
1487 /* RTSP (rtp over rtsp) */
1488 cl->query.i_proto = HTTPD_PROTO_RTSP;
1489 cl->query.i_type = HTTPD_MSG_CHANNEL;
1490 cl->query.i_channel = cl->p_buffer[1];
1491 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1492 cl->query.p_body = malloc( cl->query.i_body );
1496 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1498 cl->query.i_proto = HTTPD_PROTO_HTTP;
1499 cl->query.i_type = HTTPD_MSG_ANSWER;
1501 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1503 cl->query.i_proto = HTTPD_PROTO_RTSP;
1504 cl->query.i_type = HTTPD_MSG_ANSWER;
1506 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1507 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1508 !memcmp( cl->p_buffer, "POST", 4 ) )
1510 cl->query.i_proto = HTTPD_PROTO_HTTP;
1511 cl->query.i_type = HTTPD_MSG_NONE;
1515 cl->query.i_proto = HTTPD_PROTO_RTSP;
1516 cl->query.i_type = HTTPD_MSG_NONE;
1520 else if( cl->query.i_body > 0 )
1522 /* we are reading the body of a request or a channel */
1523 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1524 cl->query.i_body - cl->i_buffer );
1527 cl->i_buffer += i_len;
1529 if( cl->i_buffer >= cl->query.i_body )
1531 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1536 /* we are reading a header -> char by char */
1539 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1546 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1548 cl->i_buffer_size += 1024;
1549 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1551 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1552 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1556 /* we have finished the header so parse it and set i_body */
1557 cl->p_buffer[cl->i_buffer] = '\0';
1559 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1562 * assume strlen( "HTTP/1.x" ) = 8
1564 cl->query.i_status =
1565 strtol( (char *)&cl->p_buffer[8],
1574 const char name[16];
1580 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1581 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1582 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1583 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1584 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1585 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1586 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1588 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1589 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1590 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1592 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1597 cl->query.i_type = HTTPD_MSG_NONE;
1599 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1601 for( i = 0; msg_type[i].name[0]; i++ )
1603 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1604 strlen( msg_type[i].name ) ) )
1606 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1607 cl->query.i_type = msg_type[i].i_type;
1608 if( cl->query.i_proto != msg_type[i].i_proto )
1611 cl->query.i_proto = HTTPD_PROTO_NONE;
1612 cl->query.i_type = HTTPD_MSG_NONE;
1619 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1621 cl->query.i_proto = HTTPD_PROTO_HTTP;
1623 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1625 cl->query.i_proto = HTTPD_PROTO_RTSP;
1637 p2 = strchr( p, ' ' );
1642 if( !strncasecmp( p, "rtsp:", 5 ) )
1644 /* for rtsp url, you have rtsp://localhost:port/path */
1646 while( *p == '/' ) p++;
1647 while( *p && *p != '/' ) p++;
1649 cl->query.psz_url = strdup( p );
1650 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1653 cl->query.psz_args = (uint8_t *)strdup( p3 );
1661 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1663 cl->query.i_proto = HTTPD_PROTO_HTTP;
1664 cl->query.i_version = atoi( p2+7 );
1666 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1668 cl->query.i_proto = HTTPD_PROTO_RTSP;
1669 cl->query.i_version = atoi( p2+7 );
1677 p = strchr( p, '\n' );
1681 while( *p == '\n' || *p == '\r' )
1685 while( p && *p != '\0' )
1688 char *eol = p = strchr( p, '\n' );
1691 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1696 if( ( colon = strchr( line, ':' ) ) )
1702 while( *colon == ' ' )
1706 name = strdup( line );
1707 value = strdup( colon );
1709 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1710 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1712 if( !strcasecmp( name, "Content-Length" ) )
1714 cl->query.i_body = atol( value );
1721 while( *p == '\n' || *p == '\r' )
1728 if( cl->query.i_body > 0 )
1730 /* TODO Mhh, handle the case client will only send a request and close the connection
1731 * to mark and of body (probably only RTSP) */
1732 cl->query.p_body = malloc( cl->query.i_body );
1737 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1743 /* check if the client is to be set to dead */
1744 #if defined( WIN32 ) || defined( UNDER_CE )
1745 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1747 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1750 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1752 /* connection closed -> end of data */
1753 if( cl->query.i_body > 0 )
1755 cl->query.i_body = cl->i_buffer;
1757 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1761 cl->i_state = HTTPD_CLIENT_DEAD;
1765 /* XXX: for QT I have to disable timeout. Try to find why */
1766 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1767 cl->i_activity_timeout = 0;
1769 #if 0 /* Debugging only */
1770 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1774 fprintf( stderr, "received new request\n" );
1775 fprintf( stderr, " - proto=%s\n",
1776 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1777 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1778 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1779 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1781 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1782 cl->query.psz_status );
1784 else if( cl->query.i_type != HTTPD_MSG_NONE )
1786 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1788 for( i = 0; i < cl->query.i_name; i++ )
1790 fprintf( stderr, " - option name='%s' value='%s'\n",
1791 cl->query.name[i], cl->query.value[i] );
1797 static void httpd_ClientSend( httpd_client_t *cl )
1802 if( cl->i_buffer < 0 )
1804 /* We need to create the header */
1807 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1809 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1810 for( i = 0; i < cl->answer.i_name; i++ )
1812 i_size += strlen( cl->answer.name[i] ) + 2 +
1813 strlen( cl->answer.value[i] ) + 2;
1816 if( cl->i_buffer_size < i_size )
1818 cl->i_buffer_size = i_size;
1819 free( cl->p_buffer );
1820 cl->p_buffer = malloc( i_size );
1822 p = (char *)cl->p_buffer;
1824 p += sprintf( p, "%s/1.%d %d %s\r\n",
1825 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1826 cl->answer.i_version,
1827 cl->answer.i_status, psz_status );
1828 for( i = 0; i < cl->answer.i_name; i++ )
1830 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1831 cl->answer.value[i] );
1833 p += sprintf( p, "\r\n" );
1836 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1838 /*fprintf( stderr, "sending answer\n" );
1839 fprintf( stderr, "%s", cl->p_buffer );*/
1842 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1843 cl->i_buffer_size - cl->i_buffer );
1846 cl->i_buffer += i_len;
1848 if( cl->i_buffer >= cl->i_buffer_size )
1850 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1851 !cl->b_read_waiting )
1853 /* catch more body data */
1854 int i_msg = cl->query.i_type;
1855 int64_t i_offset = cl->answer.i_body_offset;
1857 httpd_MsgClean( &cl->answer );
1858 cl->answer.i_body_offset = i_offset;
1860 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1861 &cl->answer, &cl->query );
1864 if( cl->answer.i_body > 0 )
1866 /* send the body data */
1867 free( cl->p_buffer );
1868 cl->p_buffer = cl->answer.p_body;
1869 cl->i_buffer_size = cl->answer.i_body;
1872 cl->answer.i_body = 0;
1873 cl->answer.p_body = NULL;
1878 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1884 #if defined( WIN32 ) || defined( UNDER_CE )
1885 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1887 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1891 cl->i_state = HTTPD_CLIENT_DEAD;
1896 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1898 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1901 cl->i_state = HTTPD_CLIENT_RECEIVING;
1905 cl->i_state = HTTPD_CLIENT_DEAD;
1910 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1914 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1916 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1919 cl->i_state = HTTPD_CLIENT_RECEIVING;
1923 cl->i_state = HTTPD_CLIENT_DEAD;
1928 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1933 static void httpd_HostThread( httpd_host_t *host )
1935 tls_session_t *p_tls = NULL;
1936 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1937 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1939 while( !host->b_die )
1941 if( host->i_url <= 0 )
1948 /* prepare a new TLS session */
1949 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1950 p_tls = tls_ServerSessionPrepare( host->p_tls );
1952 struct pollfd ufd[host->nfd + host->i_client];
1954 for( nfd = 0; nfd < host->nfd; nfd++ )
1956 ufd[nfd].fd = host->fds[nfd];
1957 ufd[nfd].events = POLLIN;
1958 ufd[nfd].revents = 0;
1961 /* add all socket that should be read/write and close dead connection */
1962 vlc_mutex_lock( &host->lock );
1963 mtime_t now = mdate();
1964 vlc_bool_t b_low_delay = VLC_FALSE;
1966 for(int i_client = 0; i_client < host->i_client; i_client++ )
1968 httpd_client_t *cl = host->client[i_client];
1969 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1970 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1971 ( cl->i_activity_timeout > 0 &&
1972 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
1974 httpd_ClientClean( cl );
1975 stats_UpdateInteger( host, p_active_counter, -1, NULL );
1976 TAB_REMOVE( host->i_client, host->client, cl );
1982 struct pollfd *pufd = ufd + nfd;
1983 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
1986 pufd->events = pufd->revents = 0;
1988 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
1989 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
1991 pufd->events = POLLIN;
1993 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
1994 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
1996 pufd->events = POLLOUT;
1998 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2000 httpd_message_t *answer = &cl->answer;
2001 httpd_message_t *query = &cl->query;
2002 int i_msg = query->i_type;
2004 httpd_MsgInit( answer );
2006 /* Handle what we received */
2007 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2008 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2010 /* we can only receive request from client when not
2013 cl->i_state = HTTPD_CLIENT_DEAD;
2015 else if( i_msg == HTTPD_MSG_ANSWER )
2017 /* We are in BIDIR mode, trigger the callback and then
2018 * check for new data */
2019 if( cl->url && cl->url->catch[i_msg].cb )
2021 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2024 cl->i_state = HTTPD_CLIENT_WAITING;
2026 else if( i_msg == HTTPD_MSG_CHANNEL )
2028 /* We are in BIDIR mode, trigger the callback and then
2029 * check for new data */
2030 if( cl->url && cl->url->catch[i_msg].cb )
2032 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2035 cl->i_state = HTTPD_CLIENT_WAITING;
2037 else if( i_msg == HTTPD_MSG_OPTIONS )
2042 answer->i_proto = query->i_proto ;
2043 answer->i_type = HTTPD_MSG_ANSWER;
2044 answer->i_version= 0;
2046 answer->i_status = 200;
2049 answer->p_body = NULL;
2051 psz = httpd_MsgGet( query, "Require" );
2054 answer->i_status = 551;
2055 httpd_MsgAdd( query, "Unsupported", "%s", psz );
2058 psz = httpd_MsgGet( query, "Cseq" );
2060 httpd_MsgAdd( answer, "Cseq", "%s", psz );
2061 psz = httpd_MsgGet( query, "Timestamp" );
2063 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
2065 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
2066 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2067 "TEARDOWN, PLAY, PAUSE" );
2068 httpd_MsgAdd( answer, "Content-Length", "0" );
2070 cl->i_buffer = -1; /* Force the creation of the answer in
2071 * httpd_ClientSend */
2072 cl->i_state = HTTPD_CLIENT_SENDING;
2074 else if( i_msg == HTTPD_MSG_NONE )
2076 if( query->i_proto == HTTPD_PROTO_NONE )
2079 cl->i_state = HTTPD_CLIENT_DEAD;
2086 answer->i_proto = query->i_proto ;
2087 answer->i_type = HTTPD_MSG_ANSWER;
2088 answer->i_version= 0;
2089 answer->i_status = 501;
2091 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2092 answer->p_body = (uint8_t *)p;
2093 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2095 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2096 cl->i_state = HTTPD_CLIENT_SENDING;
2101 vlc_bool_t b_auth_failed = VLC_FALSE;
2102 vlc_bool_t b_hosts_failed = VLC_FALSE;
2104 /* Search the url and trigger callbacks */
2105 for(int i = 0; i < host->i_url; i++ )
2107 httpd_url_t *url = host->url[i];
2109 if( !strcmp( url->psz_url, query->psz_url ) )
2111 if( url->catch[i_msg].cb )
2113 if( answer && ( url->p_acl != NULL ) )
2115 char ip[NI_MAXNUMERICHOST];
2117 if( ( httpd_ClientIP( cl, ip ) == NULL )
2118 || ACL_Check( url->p_acl, ip ) )
2120 b_hosts_failed = VLC_TRUE;
2125 if( answer && ( *url->psz_user || *url->psz_password ) )
2127 /* create the headers */
2128 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2129 char *user = NULL, *pass = NULL;
2132 && !strncasecmp( b64, "BASIC", 5 ) )
2135 while( *b64 == ' ' )
2138 user = vlc_b64_decode( b64 );
2141 pass = strchr (user, ':');
2147 if ((user == NULL) || (pass == NULL)
2148 || strcmp (user, url->psz_user)
2149 || strcmp (pass, url->psz_password))
2151 httpd_MsgAdd( answer,
2153 "Basic realm=\"%s\"",
2155 /* We fail for all url */
2156 b_auth_failed = VLC_TRUE;
2164 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2166 if( answer->i_proto == HTTPD_PROTO_NONE )
2168 /* Raw answer from a CGI */
2169 cl->i_buffer = cl->i_buffer_size;
2174 /* only one url can answer */
2176 if( cl->url == NULL )
2189 answer->i_proto = query->i_proto;
2190 answer->i_type = HTTPD_MSG_ANSWER;
2191 answer->i_version= 0;
2193 if( b_hosts_failed )
2195 answer->i_status = 403;
2197 else if( b_auth_failed )
2199 answer->i_status = 401;
2203 /* no url registered */
2204 answer->i_status = 404;
2207 answer->i_body = httpd_HtmlError (&p,
2210 answer->p_body = (uint8_t *)p;
2212 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2213 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2214 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2217 cl->i_state = HTTPD_CLIENT_SENDING;
2220 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2222 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2224 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2225 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2226 vlc_bool_t b_connection = VLC_FALSE;
2227 vlc_bool_t b_keepalive = VLC_FALSE;
2228 vlc_bool_t b_query = VLC_FALSE;
2231 if( psz_connection )
2233 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2234 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2239 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2242 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2243 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2244 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2245 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2246 !b_query && !b_connection ) )
2248 httpd_MsgClean( &cl->query );
2249 httpd_MsgInit( &cl->query );
2252 cl->i_buffer_size = 1000;
2253 free( cl->p_buffer );
2254 cl->p_buffer = malloc( cl->i_buffer_size );
2255 cl->i_state = HTTPD_CLIENT_RECEIVING;
2259 cl->i_state = HTTPD_CLIENT_DEAD;
2261 httpd_MsgClean( &cl->answer );
2263 else if( cl->b_read_waiting )
2265 /* we have a message waiting for us to read it */
2266 httpd_MsgClean( &cl->answer );
2267 httpd_MsgClean( &cl->query );
2270 cl->i_buffer_size = 1000;
2271 free( cl->p_buffer );
2272 cl->p_buffer = malloc( cl->i_buffer_size );
2273 cl->i_state = HTTPD_CLIENT_RECEIVING;
2274 cl->b_read_waiting = VLC_FALSE;
2278 int64_t i_offset = cl->answer.i_body_offset;
2279 httpd_MsgClean( &cl->answer );
2281 cl->answer.i_body_offset = i_offset;
2282 free( cl->p_buffer );
2283 cl->p_buffer = NULL;
2285 cl->i_buffer_size = 0;
2287 cl->i_state = HTTPD_CLIENT_WAITING;
2290 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2292 int64_t i_offset = cl->answer.i_body_offset;
2293 int i_msg = cl->query.i_type;
2295 httpd_MsgInit( &cl->answer );
2296 cl->answer.i_body_offset = i_offset;
2298 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2299 &cl->answer, &cl->query );
2300 if( cl->answer.i_type != HTTPD_MSG_NONE )
2302 /* we have new data, so re-enter send mode */
2304 cl->p_buffer = cl->answer.p_body;
2305 cl->i_buffer_size = cl->answer.i_body;
2306 cl->answer.p_body = NULL;
2307 cl->answer.i_body = 0;
2308 cl->i_state = HTTPD_CLIENT_SENDING;
2312 /* we shouldn't wait too long */
2313 b_low_delay = VLC_TRUE;
2317 /* Special for BIDIR mode we also check reading */
2318 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2319 cl->i_state == HTTPD_CLIENT_SENDING )
2321 pufd->events |= POLLIN;
2324 if (pufd->events != 0)
2327 vlc_mutex_unlock( &host->lock );
2329 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2330 switch( poll( ufd, nfd, b_low_delay ? 20 : 100) )
2335 /* This is most likely a bug */
2336 msg_Err( host, "polling error: %s", strerror (errno));
2343 /* Handle client sockets */
2344 vlc_mutex_lock( &host->lock );
2346 for( int i_client = 0; i_client < host->i_client; i_client++ )
2348 httpd_client_t *cl = host->client[i_client];
2349 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2351 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2353 if( cl->fd != pufd->fd )
2354 continue; // we were not waiting for this client
2355 if( pufd->revents == 0 )
2356 continue; // no event received
2358 cl->i_activity_date = now;
2360 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2362 httpd_ClientRecv( cl );
2364 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2366 httpd_ClientSend( cl );
2368 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2370 httpd_ClientTlsHsIn( cl );
2372 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2374 httpd_ClientTlsHsOut( cl );
2377 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2378 cl->i_state == HTTPD_CLIENT_SENDING &&
2379 (pufd->revents & POLLIN) )
2381 cl->b_read_waiting = VLC_TRUE;
2384 vlc_mutex_unlock( &host->lock );
2386 /* Handle server sockets (accept new connections) */
2387 for( nfd = 0; nfd < host->nfd; nfd++ )
2392 assert (ufd[nfd].fd == host->fds[nfd]);
2394 if( ufd[nfd].revents == 0 )
2398 int kludge[] = { ufd[nfd].fd, -1 };
2399 int fd = net_Accept( host, kludge, 0 );
2405 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2408 msg_Err( host, "Rejecting TLS connection" );
2414 case 1: /* missing input - most likely */
2415 i_state = HTTPD_CLIENT_TLS_HS_IN;
2418 case 2: /* missing output */
2419 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2423 if( (p_tls == NULL) != (host->p_tls == NULL) )
2424 break; // wasted TLS session, cannot accept() anymore
2427 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2428 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2429 cl = httpd_ClientNew( fd, p_tls, now );
2431 vlc_mutex_lock( &host->lock );
2432 TAB_APPEND( host->i_client, host->client, cl );
2433 vlc_mutex_unlock( &host->lock );
2435 cl->i_state = i_state; // override state for TLS
2437 if (host->p_tls != NULL)
2438 break; // cannot accept further without new TLS session
2444 tls_ServerSessionClose( p_tls );
2445 if( p_total_counter )
2446 stats_CounterClean( p_total_counter );
2447 if( p_active_counter )
2448 stats_CounterClean( p_active_counter );
2451 #else /* ENABLE_HTTPD */
2453 /* We just define an empty wrapper */
2454 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2457 msg_Err( a, "HTTP daemon support is disabled" );
2461 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2463 msg_Err( a, "HTTP daemon support is disabled" );
2467 void httpd_HostDelete( httpd_host_t *a )
2471 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2472 char *psz_user, char *psz_password,
2473 const vlc_acl_t *p_acl )
2478 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2479 char *psz_user, char *psz_password,
2480 const vlc_acl_t *p_acl )
2485 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2486 httpd_callback_sys_t *d )
2491 void httpd_UrlDelete( httpd_url_t *a )
2495 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2500 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2505 void httpd_ClientModeStream( httpd_client_t *a )
2509 void httpd_ClientModeBidir( httpd_client_t *a )
2513 void httpd_FileDelete( httpd_file_t *a )
2517 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2518 char *e, httpd_file_callback_t f,
2519 httpd_file_sys_t *g )
2524 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2525 const char *psz_user,
2526 const char *psz_password,
2527 const vlc_acl_t *p_acl,
2528 httpd_handler_callback_t pf_fill,
2529 httpd_handler_sys_t *p_sys )
2534 void httpd_HandlerDelete( httpd_handler_t *handler )
2538 void httpd_RedirectDelete( httpd_redirect_t *a )
2542 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2548 void httpd_StreamDelete( httpd_stream_t *a )
2552 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2557 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2562 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2568 void httpd_MsgInit ( httpd_message_t *a )
2572 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2576 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2581 void httpd_MsgClean( httpd_message_t *a )
2585 #endif /* ENABLE_HTTPD */