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= 1;
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;
539 /* Looks for end of header (i.e. one empty line) */
540 while ( (p = strchr( p, '\r' )) != NULL )
542 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
543 && p[3] && p[3] == '\n' )
552 answer->i_body = strlen((char*)answer->p_body) + 1;
553 answer->p_body = realloc( answer->p_body, answer->i_body );
557 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
559 int i_status, i_headers;
560 char *psz_headers, *psz_new;
561 const char *psz_status;
563 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
566 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
567 if( *psz_headers ) psz_headers++;
568 if( *psz_headers ) psz_headers++;
569 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
574 psz_headers = (char *)answer->p_body;
575 i_headers = answer->i_body;
578 psz_status = httpd_ReasonFromCode( i_status );
579 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
580 + strlen(psz_status) + i_headers - 1;
581 psz_new = (char *)malloc( answer->i_body + 1);
582 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
583 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
584 free( answer->p_body );
585 answer->p_body = (uint8_t *)psz_new;
591 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
592 const char *psz_user,
593 const char *psz_password,
594 const vlc_acl_t *p_acl,
595 httpd_handler_callback_t pf_fill,
596 httpd_handler_sys_t *p_sys )
598 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
600 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
601 psz_password, p_acl )
608 handler->pf_fill = pf_fill;
609 handler->p_sys = p_sys;
611 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
612 (httpd_callback_sys_t*)handler );
613 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
614 (httpd_callback_sys_t*)handler );
615 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
616 (httpd_callback_sys_t*)handler );
621 void httpd_HandlerDelete( httpd_handler_t *handler )
623 httpd_UrlDelete( handler->url );
627 /*****************************************************************************
628 * High Level Functions: httpd_redirect_t
629 *****************************************************************************/
630 struct httpd_redirect_t
636 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
637 httpd_client_t *cl, httpd_message_t *answer,
638 const httpd_message_t *query )
640 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
644 if( answer == NULL || query == NULL )
648 answer->i_proto = HTTPD_PROTO_HTTP;
649 answer->i_version= 1;
650 answer->i_type = HTTPD_MSG_ANSWER;
651 answer->i_status = 301;
653 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
654 answer->p_body = (unsigned char *)p_body;
656 /* XXX check if it's ok or we need to set an absolute url */
657 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
659 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
664 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
665 const char *psz_url_src )
667 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
669 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
674 rdir->psz_dst = strdup( psz_url_dst );
676 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
677 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
678 (httpd_callback_sys_t*)rdir );
679 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
680 (httpd_callback_sys_t*)rdir );
681 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
682 (httpd_callback_sys_t*)rdir );
683 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
684 (httpd_callback_sys_t*)rdir );
688 void httpd_RedirectDelete( httpd_redirect_t *rdir )
690 httpd_UrlDelete( rdir->url );
691 free( rdir->psz_dst );
695 /*****************************************************************************
696 * High Level Funtions: httpd_stream_t
697 *****************************************************************************/
698 struct httpd_stream_t
705 /* Header to send as first packet */
709 /* circular buffer */
710 int i_buffer_size; /* buffer size, can't be reallocated smaller */
711 uint8_t *p_buffer; /* buffer */
712 int64_t i_buffer_pos; /* absolute position from begining */
713 int64_t i_buffer_last_pos; /* a new connection will start with that */
716 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
717 httpd_client_t *cl, httpd_message_t *answer,
718 const httpd_message_t *query )
720 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
722 if( answer == NULL || query == NULL || cl == NULL )
727 if( answer->i_body_offset > 0 )
733 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
734 answer->i_body_offset );
737 if( answer->i_body_offset >= stream->i_buffer_pos )
739 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
740 return VLC_EGENERIC; /* wait, no data available */
742 if( answer->i_body_offset + stream->i_buffer_size <
743 stream->i_buffer_pos )
745 /* this client isn't fast enough */
747 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
748 answer->i_body_offset, stream->i_buffer_last_pos );
750 answer->i_body_offset = stream->i_buffer_last_pos;
753 i_pos = answer->i_body_offset % stream->i_buffer_size;
754 i_write = stream->i_buffer_pos - answer->i_body_offset;
755 if( i_write > HTTPD_CL_BUFSIZE )
757 i_write = HTTPD_CL_BUFSIZE;
759 else if( i_write <= 0 )
761 return VLC_EGENERIC; /* wait, no data available */
764 /* Don't go past the end of the circular buffer */
765 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
767 /* using HTTPD_MSG_ANSWER -> data available */
768 answer->i_proto = HTTPD_PROTO_HTTP;
769 answer->i_version= 0;
770 answer->i_type = HTTPD_MSG_ANSWER;
772 answer->i_body = i_write;
773 answer->p_body = malloc( i_write );
774 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
776 answer->i_body_offset += i_write;
782 answer->i_proto = HTTPD_PROTO_HTTP;
783 answer->i_version= 0;
784 answer->i_type = HTTPD_MSG_ANSWER;
786 answer->i_status = 200;
788 if( query->i_type != HTTPD_MSG_HEAD )
790 httpd_ClientModeStream( cl );
791 vlc_mutex_lock( &stream->lock );
792 /* Send the header */
793 if( stream->i_header > 0 )
795 answer->i_body = stream->i_header;
796 answer->p_body = malloc( stream->i_header );
797 memcpy( answer->p_body, stream->p_header, stream->i_header );
799 answer->i_body_offset = stream->i_buffer_last_pos;
800 vlc_mutex_unlock( &stream->lock );
804 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
805 answer->i_body_offset = 0;
808 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
810 vlc_bool_t b_xplaystream = VLC_FALSE;
813 httpd_MsgAdd( answer, "Content-type", "%s",
814 "application/octet-stream" );
815 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
816 httpd_MsgAdd( answer, "Pragma", "no-cache" );
817 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
818 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
820 /* Check if there is a xPlayStrm=1 */
821 for( i = 0; i < query->i_name; i++ )
823 if( !strcasecmp( query->name[i], "Pragma" ) &&
824 strstr( query->value[i], "xPlayStrm=1" ) )
826 b_xplaystream = VLC_TRUE;
832 answer->i_body_offset = 0;
837 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
839 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
844 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
845 const char *psz_url, const char *psz_mime,
846 const char *psz_user, const char *psz_password,
847 const vlc_acl_t *p_acl )
849 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
851 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
852 psz_password, p_acl )
858 vlc_mutex_init( host, &stream->lock );
859 if( psz_mime && *psz_mime )
861 stream->psz_mime = strdup( psz_mime );
865 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
867 stream->i_header = 0;
868 stream->p_header = NULL;
869 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
870 stream->p_buffer = malloc( stream->i_buffer_size );
871 /* We set to 1 to make life simpler
872 * (this way i_body_offset can never be 0) */
873 stream->i_buffer_pos = 1;
874 stream->i_buffer_last_pos = 1;
876 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
877 (httpd_callback_sys_t*)stream );
878 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
879 (httpd_callback_sys_t*)stream );
880 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
881 (httpd_callback_sys_t*)stream );
886 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
888 vlc_mutex_lock( &stream->lock );
889 if( stream->p_header )
891 free( stream->p_header );
892 stream->p_header = NULL;
894 stream->i_header = i_data;
897 stream->p_header = malloc( i_data );
898 memcpy( stream->p_header, p_data, i_data );
900 vlc_mutex_unlock( &stream->lock );
905 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
910 if( i_data < 0 || p_data == NULL )
914 vlc_mutex_lock( &stream->lock );
916 /* save this pointer (to be used by new connection) */
917 stream->i_buffer_last_pos = stream->i_buffer_pos;
919 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
925 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
927 /* Ok, we can't go past the end of our buffer */
928 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
930 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
935 stream->i_buffer_pos += i_data;
937 vlc_mutex_unlock( &stream->lock );
941 void httpd_StreamDelete( httpd_stream_t *stream )
943 httpd_UrlDelete( stream->url );
944 vlc_mutex_destroy( &stream->lock );
945 if( stream->psz_mime ) free( stream->psz_mime );
946 if( stream->p_header ) free( stream->p_header );
947 if( stream->p_buffer ) free( stream->p_buffer );
951 /*****************************************************************************
953 *****************************************************************************/
954 static void httpd_HostThread( httpd_host_t * );
956 /* create a new host */
957 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
960 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
964 static const char psz_object_type[] = "http server";
966 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
968 const char *psz_cert, const char *psz_key,
969 const char *psz_ca, const char *psz_crl )
978 if( psz_hostname == NULL )
981 psz_host = strdup( psz_hostname );
982 if( psz_host == NULL )
984 msg_Err( p_this, "memory error" );
988 /* to be sure to avoid multiple creation */
989 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
990 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
991 vlc_mutex_lock( lockval.p_address );
993 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
995 msg_Info( p_this, "creating httpd" );
996 httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
1001 vlc_mutex_unlock( lockval.p_address );
1009 vlc_object_yield( httpd );
1010 vlc_object_attach( httpd, p_this->p_libvlc );
1013 /* verify if it already exist */
1014 for( i = httpd->i_host - 1; i >= 0; i-- )
1016 host = httpd->host[i];
1018 /* cannot mix TLS and non-TLS hosts */
1019 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1020 || ( host->i_port != i_port )
1021 || strcmp( host->psz_hostname, psz_hostname ) )
1027 vlc_mutex_unlock( lockval.p_address );
1033 /* determine TLS configuration */
1034 if ( psz_cert != NULL )
1036 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1037 if ( p_tls == NULL )
1039 msg_Err( p_this, "TLS initialization error" );
1043 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1045 msg_Err( p_this, "TLS CA error" );
1049 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1051 msg_Err( p_this, "TLS CRL error" );
1058 /* create the new host */
1059 host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
1060 VLC_OBJECT_HTTPD_HOST,
1065 host->httpd = httpd;
1066 vlc_mutex_init( httpd, &host->lock );
1069 host->fds = net_ListenTCP( p_this, psz_host, i_port );
1070 if( host->fds == NULL )
1072 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1075 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
1077 host->i_port = i_port;
1078 host->psz_hostname = psz_host;
1083 host->client = NULL;
1085 host->p_tls = p_tls;
1087 /* create the thread */
1088 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1089 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1091 msg_Err( p_this, "cannot spawn http host thread" );
1095 /* now add it to httpd */
1096 TAB_APPEND( httpd->i_host, httpd->host, host );
1097 vlc_mutex_unlock( lockval.p_address );
1103 if( httpd->i_host <= 0 )
1105 vlc_object_release( httpd );
1106 vlc_object_detach( httpd );
1107 vlc_object_destroy( httpd );
1109 vlc_mutex_unlock( lockval.p_address );
1113 net_ListenClose( host->fds );
1114 vlc_mutex_destroy( &host->lock );
1115 vlc_object_destroy( host );
1119 tls_ServerDelete( p_tls );
1125 void httpd_HostDelete( httpd_host_t *host )
1127 httpd_t *httpd = host->httpd;
1128 vlc_value_t lockval;
1131 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1132 vlc_mutex_lock( lockval.p_address );
1135 if( host->i_ref > 0 )
1138 vlc_mutex_unlock( lockval.p_address );
1139 msg_Dbg( host, "httpd_HostDelete: host still used" );
1142 TAB_REMOVE( httpd->i_host, httpd->host, host );
1144 vlc_object_kill( host );
1145 vlc_thread_join( host );
1147 msg_Dbg( host, "HTTP host removed" );
1149 for( i = 0; i < host->i_url; i++ )
1151 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1153 for( i = 0; i < host->i_client; i++ )
1155 httpd_client_t *cl = host->client[i];
1156 msg_Warn( host, "client still connected" );
1157 httpd_ClientClean( cl );
1158 TAB_REMOVE( host->i_client, host->client, cl );
1164 if( host->p_tls != NULL)
1165 tls_ServerDelete( host->p_tls );
1167 net_ListenClose( host->fds );
1168 free( host->psz_hostname );
1170 vlc_mutex_destroy( &host->lock );
1171 vlc_object_destroy( host );
1173 vlc_object_release( httpd );
1174 if( httpd->i_host <= 0 )
1176 msg_Dbg( httpd, "no host left, stopping httpd" );
1177 vlc_object_detach( httpd );
1178 vlc_object_destroy( httpd );
1180 vlc_mutex_unlock( lockval.p_address );
1183 /* register a new url */
1184 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1185 const char *psz_user, const char *psz_password,
1186 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1191 assert( psz_url != NULL );
1193 vlc_mutex_lock( &host->lock );
1196 for( i = 0; i < host->i_url; i++ )
1198 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1200 msg_Warn( host->httpd,
1201 "cannot add '%s' (url already defined)", psz_url );
1202 vlc_mutex_unlock( &host->lock );
1208 url = malloc( sizeof( httpd_url_t ) );
1211 vlc_mutex_init( host->httpd, &url->lock );
1212 url->psz_url = strdup( psz_url );
1213 url->psz_user = strdup( psz_user ? psz_user : "" );
1214 url->psz_password = strdup( psz_password ? psz_password : "" );
1215 url->p_acl = ACL_Duplicate( host, p_acl );
1216 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1218 url->catch[i].cb = NULL;
1219 url->catch[i].p_sys = NULL;
1222 TAB_APPEND( host->i_url, host->url, url );
1223 vlc_mutex_unlock( &host->lock );
1228 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1229 const char *psz_user, const char *psz_password,
1230 const vlc_acl_t *p_acl )
1232 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1233 psz_password, p_acl, VLC_FALSE );
1236 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1237 const char *psz_user, const char *psz_password,
1238 const vlc_acl_t *p_acl )
1240 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1241 psz_password, p_acl, VLC_TRUE );
1244 /* register callback on a url */
1245 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1246 httpd_callback_sys_t *p_sys )
1248 vlc_mutex_lock( &url->lock );
1249 url->catch[i_msg].cb = cb;
1250 url->catch[i_msg].p_sys= p_sys;
1251 vlc_mutex_unlock( &url->lock );
1257 void httpd_UrlDelete( httpd_url_t *url )
1259 httpd_host_t *host = url->host;
1262 vlc_mutex_lock( &host->lock );
1263 TAB_REMOVE( host->i_url, host->url, url );
1265 vlc_mutex_destroy( &url->lock );
1266 free( url->psz_url );
1267 free( url->psz_user );
1268 free( url->psz_password );
1269 ACL_Destroy( url->p_acl );
1271 for( i = 0; i < host->i_client; i++ )
1273 httpd_client_t *client = host->client[i];
1275 if( client->url == url )
1277 /* TODO complete it */
1278 msg_Warn( host, "force closing connections" );
1279 httpd_ClientClean( client );
1280 TAB_REMOVE( host->i_client, host->client, client );
1286 vlc_mutex_unlock( &host->lock );
1289 void httpd_MsgInit( httpd_message_t *msg )
1292 msg->i_type = HTTPD_MSG_NONE;
1293 msg->i_proto = HTTPD_PROTO_NONE;
1294 msg->i_version = -1; /* FIXME */
1298 msg->psz_url = NULL;
1299 msg->psz_args = NULL;
1301 msg->i_channel = -1;
1308 msg->i_body_offset = 0;
1313 void httpd_MsgClean( httpd_message_t *msg )
1319 free( msg->psz_url );
1323 free( msg->psz_args );
1325 for( i = 0; i < msg->i_name; i++ )
1327 free( msg->name[i] );
1328 free( msg->value[i] );
1340 free( msg->p_body );
1342 httpd_MsgInit( msg );
1345 const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
1349 for( i = 0; i < msg->i_name; i++ )
1351 if( !strcasecmp( msg->name[i], name ))
1353 return msg->value[i];
1359 void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
1364 va_start( args, psz_value );
1365 if( vasprintf( &value, psz_value, args ) == -1 )
1372 name = strdup( name );
1379 TAB_APPEND( msg->i_name, msg->name, (char*)name );
1380 TAB_APPEND( msg->i_value, msg->value, value );
1383 static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
1385 cl->i_state = HTTPD_CLIENT_RECEIVING;
1386 cl->i_activity_date = now;
1387 cl->i_activity_timeout = I64C(10000000);
1388 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1390 cl->p_buffer = malloc( cl->i_buffer_size );
1391 cl->i_mode = HTTPD_CLIENT_FILE;
1392 cl->b_read_waiting = VLC_FALSE;
1394 httpd_MsgInit( &cl->query );
1395 httpd_MsgInit( &cl->answer );
1398 void httpd_ClientModeStream( httpd_client_t *cl )
1400 cl->i_mode = HTTPD_CLIENT_STREAM;
1403 void httpd_ClientModeBidir( httpd_client_t *cl )
1405 cl->i_mode = HTTPD_CLIENT_BIDIR;
1408 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1410 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1413 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1415 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1418 static void httpd_ClientClean( httpd_client_t *cl )
1422 if( cl->p_tls != NULL )
1423 tls_ServerSessionClose( cl->p_tls );
1424 net_Close( cl->fd );
1428 httpd_MsgClean( &cl->answer );
1429 httpd_MsgClean( &cl->query );
1433 free( cl->p_buffer );
1434 cl->p_buffer = NULL;
1438 static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
1440 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1442 if( !cl ) return NULL;
1449 httpd_ClientInit( cl, now );
1454 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1456 tls_session_t *p_tls;
1460 return tls_Recv( p_tls, p, i_len );
1462 return recv( cl->fd, p, i_len, 0 );
1465 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1467 tls_session_t *p_tls;
1471 return tls_Send( p_tls, p, i_len );
1473 return send( cl->fd, p, i_len, 0 );
1479 const char name[16];
1485 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1486 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1487 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1488 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1489 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1490 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1491 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1492 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1493 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1494 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1495 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1499 static void httpd_ClientRecv( httpd_client_t *cl )
1503 /* ignore leading whites */
1504 if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
1505 ( cl->i_buffer == 0 ) )
1509 i_len = httpd_NetRecv( cl, &c, 1 );
1511 if( ( i_len > 0 ) && ( strchr( "\r\n\t ", c ) == NULL ) )
1513 cl->p_buffer[0] = c;
1518 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1520 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1521 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1525 cl->i_buffer += i_len;
1528 if( cl->i_buffer >= 7 )
1531 if( cl->p_buffer[0] == '$' )
1533 /* Interleaved RTP over RTSP */
1534 cl->query.i_proto = HTTPD_PROTO_RTSP;
1535 cl->query.i_type = HTTPD_MSG_CHANNEL;
1536 cl->query.i_channel = cl->p_buffer[1];
1537 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1538 cl->query.p_body = malloc( cl->query.i_body );
1542 else if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
1544 cl->query.i_proto = HTTPD_PROTO_HTTP;
1545 cl->query.i_type = HTTPD_MSG_ANSWER;
1547 else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
1549 cl->query.i_proto = HTTPD_PROTO_RTSP;
1550 cl->query.i_type = HTTPD_MSG_ANSWER;
1552 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1553 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1554 !memcmp( cl->p_buffer, "POST", 4 ) )
1556 cl->query.i_proto = HTTPD_PROTO_HTTP;
1557 cl->query.i_type = HTTPD_MSG_NONE;
1561 cl->query.i_proto = HTTPD_PROTO_RTSP;
1562 cl->query.i_type = HTTPD_MSG_NONE;
1566 else if( cl->query.i_body > 0 )
1568 /* we are reading the body of a request or a channel */
1569 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1570 cl->query.i_body - cl->i_buffer );
1573 cl->i_buffer += i_len;
1575 if( cl->i_buffer >= cl->query.i_body )
1577 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1582 /* we are reading a header -> char by char */
1585 if( cl->i_buffer == cl->i_buffer_size )
1587 char *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
1588 if( newbuf == NULL )
1594 cl->p_buffer = newbuf;
1595 cl->i_buffer_size += 1024;
1598 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1605 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1606 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1610 /* we have finished the header so parse it and set i_body */
1611 cl->p_buffer[cl->i_buffer] = '\0';
1613 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1616 * assume strlen( "HTTP/1.x" ) = 8
1618 cl->query.i_status =
1619 strtol( (char *)&cl->p_buffer[8],
1629 cl->query.i_type = HTTPD_MSG_NONE;
1631 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1633 for( i = 0; msg_type[i].name[0]; i++ )
1635 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1636 strlen( msg_type[i].name ) ) )
1638 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1639 cl->query.i_type = msg_type[i].i_type;
1640 if( cl->query.i_proto != msg_type[i].i_proto )
1643 cl->query.i_proto = HTTPD_PROTO_NONE;
1644 cl->query.i_type = HTTPD_MSG_NONE;
1651 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1653 cl->query.i_proto = HTTPD_PROTO_HTTP;
1655 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1657 cl->query.i_proto = HTTPD_PROTO_RTSP;
1669 p2 = strchr( p, ' ' );
1674 if( !strncasecmp( p, "rtsp:", 5 ) )
1676 /* for rtsp url, you have rtsp://localhost:port/path */
1678 while( *p == '/' ) p++;
1679 while( *p && *p != '/' ) p++;
1681 cl->query.psz_url = strdup( p );
1682 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1685 cl->query.psz_args = (uint8_t *)strdup( p3 );
1693 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1695 cl->query.i_proto = HTTPD_PROTO_HTTP;
1696 cl->query.i_version = atoi( p2+7 );
1698 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1700 cl->query.i_proto = HTTPD_PROTO_RTSP;
1701 cl->query.i_version = atoi( p2+7 );
1709 p = strchr( p, '\n' );
1713 while( *p == '\n' || *p == '\r' )
1717 while( p && *p != '\0' )
1720 char *eol = p = strchr( p, '\n' );
1723 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1728 if( ( colon = strchr( line, ':' ) ) )
1734 while( *colon == ' ' )
1738 name = strdup( line );
1739 value = strdup( colon );
1741 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1742 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1744 if( !strcasecmp( name, "Content-Length" ) )
1746 cl->query.i_body = atol( value );
1753 while( *p == '\n' || *p == '\r' )
1760 if( cl->query.i_body > 0 )
1762 /* TODO Mhh, handle the case client will only send a request and close the connection
1763 * to mark and of body (probably only RTSP) */
1764 cl->query.p_body = malloc( cl->query.i_body );
1769 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1775 /* check if the client is to be set to dead */
1776 #if defined( WIN32 ) || defined( UNDER_CE )
1777 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1779 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1782 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1784 /* connection closed -> end of data */
1785 if( cl->query.i_body > 0 )
1787 cl->query.i_body = cl->i_buffer;
1789 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1793 cl->i_state = HTTPD_CLIENT_DEAD;
1797 /* XXX: for QT I have to disable timeout. Try to find why */
1798 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1799 cl->i_activity_timeout = 0;
1801 #if 0 /* Debugging only */
1802 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1806 fprintf( stderr, "received new request\n" );
1807 fprintf( stderr, " - proto=%s\n",
1808 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1809 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1810 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1811 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1813 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1814 cl->query.psz_status );
1816 else if( cl->query.i_type != HTTPD_MSG_NONE )
1818 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1820 for( i = 0; i < cl->query.i_name; i++ )
1822 fprintf( stderr, " - option name='%s' value='%s'\n",
1823 cl->query.name[i], cl->query.value[i] );
1829 static void httpd_ClientSend( httpd_client_t *cl )
1834 if( cl->i_buffer < 0 )
1836 /* We need to create the header */
1839 const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
1841 i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
1842 for( i = 0; i < cl->answer.i_name; i++ )
1844 i_size += strlen( cl->answer.name[i] ) + 2 +
1845 strlen( cl->answer.value[i] ) + 2;
1848 if( cl->i_buffer_size < i_size )
1850 cl->i_buffer_size = i_size;
1851 free( cl->p_buffer );
1852 cl->p_buffer = malloc( i_size );
1854 p = (char *)cl->p_buffer;
1856 p += sprintf( p, "%s.%u %d %s\r\n",
1857 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1858 cl->answer.i_version,
1859 cl->answer.i_status, psz_status );
1860 for( i = 0; i < cl->answer.i_name; i++ )
1862 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1863 cl->answer.value[i] );
1865 p += sprintf( p, "\r\n" );
1868 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1870 /*fprintf( stderr, "sending answer\n" );
1871 fprintf( stderr, "%s", cl->p_buffer );*/
1874 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1875 cl->i_buffer_size - cl->i_buffer );
1878 cl->i_buffer += i_len;
1880 if( cl->i_buffer >= cl->i_buffer_size )
1882 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1883 !cl->b_read_waiting )
1885 /* catch more body data */
1886 int i_msg = cl->query.i_type;
1887 int64_t i_offset = cl->answer.i_body_offset;
1889 httpd_MsgClean( &cl->answer );
1890 cl->answer.i_body_offset = i_offset;
1892 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1893 &cl->answer, &cl->query );
1896 if( cl->answer.i_body > 0 )
1898 /* send the body data */
1899 free( cl->p_buffer );
1900 cl->p_buffer = cl->answer.p_body;
1901 cl->i_buffer_size = cl->answer.i_body;
1904 cl->answer.i_body = 0;
1905 cl->answer.p_body = NULL;
1910 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1916 #if defined( WIN32 ) || defined( UNDER_CE )
1917 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1919 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1923 cl->i_state = HTTPD_CLIENT_DEAD;
1928 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1930 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1933 cl->i_state = HTTPD_CLIENT_RECEIVING;
1937 cl->i_state = HTTPD_CLIENT_DEAD;
1942 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1946 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1948 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1951 cl->i_state = HTTPD_CLIENT_RECEIVING;
1955 cl->i_state = HTTPD_CLIENT_DEAD;
1960 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1965 static void httpd_HostThread( httpd_host_t *host )
1967 tls_session_t *p_tls = NULL;
1968 counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1969 counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
1971 while( !host->b_die )
1973 if( host->i_url <= 0 )
1980 /* prepare a new TLS session */
1981 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1982 p_tls = tls_ServerSessionPrepare( host->p_tls );
1984 struct pollfd ufd[host->nfd + host->i_client];
1986 for( nfd = 0; nfd < host->nfd; nfd++ )
1988 ufd[nfd].fd = host->fds[nfd];
1989 ufd[nfd].events = POLLIN;
1990 ufd[nfd].revents = 0;
1993 /* add all socket that should be read/write and close dead connection */
1994 vlc_mutex_lock( &host->lock );
1995 mtime_t now = mdate();
1996 vlc_bool_t b_low_delay = VLC_FALSE;
1998 for(int i_client = 0; i_client < host->i_client; i_client++ )
2000 httpd_client_t *cl = host->client[i_client];
2001 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2002 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2003 ( cl->i_activity_timeout > 0 &&
2004 cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
2006 httpd_ClientClean( cl );
2007 stats_UpdateInteger( host, p_active_counter, -1, NULL );
2008 TAB_REMOVE( host->i_client, host->client, cl );
2014 struct pollfd *pufd = ufd + nfd;
2015 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
2018 pufd->events = pufd->revents = 0;
2020 if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2021 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2023 pufd->events = POLLIN;
2025 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2026 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2028 pufd->events = POLLOUT;
2030 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2032 httpd_message_t *answer = &cl->answer;
2033 httpd_message_t *query = &cl->query;
2034 int i_msg = query->i_type;
2036 httpd_MsgInit( answer );
2038 /* Handle what we received */
2039 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2040 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2042 /* we can only receive request from client when not
2045 cl->i_state = HTTPD_CLIENT_DEAD;
2047 else if( i_msg == HTTPD_MSG_ANSWER )
2049 /* We are in BIDIR mode, trigger the callback and then
2050 * check for new data */
2051 if( cl->url && cl->url->catch[i_msg].cb )
2053 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2056 cl->i_state = HTTPD_CLIENT_WAITING;
2058 else if( i_msg == HTTPD_MSG_CHANNEL )
2060 /* We are in BIDIR mode, trigger the callback and then
2061 * check for new data */
2062 if( cl->url && cl->url->catch[i_msg].cb )
2064 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2067 cl->i_state = HTTPD_CLIENT_WAITING;
2069 else if( i_msg == HTTPD_MSG_OPTIONS )
2072 answer->i_type = HTTPD_MSG_ANSWER;
2073 answer->i_proto = query->i_proto;
2074 answer->i_status = 200;
2076 answer->p_body = NULL;
2078 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
2079 httpd_MsgAdd( answer, "Content-Length", "0" );
2081 switch( query->i_proto )
2083 case HTTPD_PROTO_HTTP:
2084 answer->i_version = 1;
2085 httpd_MsgAdd( answer, "Allow",
2086 "GET,HEAD,POST,OPTIONS" );
2089 case HTTPD_PROTO_RTSP:
2092 answer->i_version = 0;
2094 p = httpd_MsgGet( query, "Cseq" );
2096 httpd_MsgAdd( answer, "Cseq", "%s", p );
2097 p = httpd_MsgGet( query, "Timestamp" );
2099 httpd_MsgAdd( answer, "Timestamp", "%s", p );
2101 p = httpd_MsgGet( query, "Require" );
2104 answer->i_status = 551;
2105 httpd_MsgAdd( query, "Unsupported", "%s", p );
2108 httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
2109 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
2114 cl->i_buffer = -1; /* Force the creation of the answer in
2115 * httpd_ClientSend */
2116 cl->i_state = HTTPD_CLIENT_SENDING;
2118 else if( i_msg == HTTPD_MSG_NONE )
2120 if( query->i_proto == HTTPD_PROTO_NONE )
2123 cl->i_state = HTTPD_CLIENT_DEAD;
2130 answer->i_proto = query->i_proto ;
2131 answer->i_type = HTTPD_MSG_ANSWER;
2132 answer->i_version= 0;
2133 answer->i_status = 501;
2135 answer->i_body = httpd_HtmlError (&p, 501, NULL);
2136 answer->p_body = (uint8_t *)p;
2137 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2139 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2140 cl->i_state = HTTPD_CLIENT_SENDING;
2145 vlc_bool_t b_auth_failed = VLC_FALSE;
2146 vlc_bool_t b_hosts_failed = VLC_FALSE;
2148 /* Search the url and trigger callbacks */
2149 for(int i = 0; i < host->i_url; i++ )
2151 httpd_url_t *url = host->url[i];
2153 if( !strcmp( url->psz_url, query->psz_url ) )
2155 if( url->catch[i_msg].cb )
2157 if( answer && ( url->p_acl != NULL ) )
2159 char ip[NI_MAXNUMERICHOST];
2161 if( ( httpd_ClientIP( cl, ip ) == NULL )
2162 || ACL_Check( url->p_acl, ip ) )
2164 b_hosts_failed = VLC_TRUE;
2169 if( answer && ( *url->psz_user || *url->psz_password ) )
2171 /* create the headers */
2172 const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2173 char *user = NULL, *pass = NULL;
2176 && !strncasecmp( b64, "BASIC", 5 ) )
2179 while( *b64 == ' ' )
2182 user = vlc_b64_decode( b64 );
2185 pass = strchr (user, ':');
2191 if ((user == NULL) || (pass == NULL)
2192 || strcmp (user, url->psz_user)
2193 || strcmp (pass, url->psz_password))
2195 httpd_MsgAdd( answer,
2197 "Basic realm=\"%s\"",
2199 /* We fail for all url */
2200 b_auth_failed = VLC_TRUE;
2208 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2210 if( answer->i_proto == HTTPD_PROTO_NONE )
2212 /* Raw answer from a CGI */
2213 cl->i_buffer = cl->i_buffer_size;
2218 /* only one url can answer */
2220 if( cl->url == NULL )
2233 answer->i_proto = query->i_proto;
2234 answer->i_type = HTTPD_MSG_ANSWER;
2235 answer->i_version= 0;
2237 if( b_hosts_failed )
2239 answer->i_status = 403;
2241 else if( b_auth_failed )
2243 answer->i_status = 401;
2247 /* no url registered */
2248 answer->i_status = 404;
2251 answer->i_body = httpd_HtmlError (&p,
2254 answer->p_body = (uint8_t *)p;
2256 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2257 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2258 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2261 cl->i_state = HTTPD_CLIENT_SENDING;
2264 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2266 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2268 const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2269 const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2270 vlc_bool_t b_connection = VLC_FALSE;
2271 vlc_bool_t b_keepalive = VLC_FALSE;
2272 vlc_bool_t b_query = VLC_FALSE;
2275 if( psz_connection )
2277 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2278 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2283 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2286 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2287 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2288 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2289 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2290 !b_query && !b_connection ) )
2292 httpd_MsgClean( &cl->query );
2293 httpd_MsgInit( &cl->query );
2296 cl->i_buffer_size = 1000;
2297 free( cl->p_buffer );
2298 cl->p_buffer = malloc( cl->i_buffer_size );
2299 cl->i_state = HTTPD_CLIENT_RECEIVING;
2303 cl->i_state = HTTPD_CLIENT_DEAD;
2305 httpd_MsgClean( &cl->answer );
2307 else if( cl->b_read_waiting )
2309 /* we have a message waiting for us to read it */
2310 httpd_MsgClean( &cl->answer );
2311 httpd_MsgClean( &cl->query );
2314 cl->i_buffer_size = 1000;
2315 free( cl->p_buffer );
2316 cl->p_buffer = malloc( cl->i_buffer_size );
2317 cl->i_state = HTTPD_CLIENT_RECEIVING;
2318 cl->b_read_waiting = VLC_FALSE;
2322 int64_t i_offset = cl->answer.i_body_offset;
2323 httpd_MsgClean( &cl->answer );
2325 cl->answer.i_body_offset = i_offset;
2326 free( cl->p_buffer );
2327 cl->p_buffer = NULL;
2329 cl->i_buffer_size = 0;
2331 cl->i_state = HTTPD_CLIENT_WAITING;
2334 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2336 int64_t i_offset = cl->answer.i_body_offset;
2337 int i_msg = cl->query.i_type;
2339 httpd_MsgInit( &cl->answer );
2340 cl->answer.i_body_offset = i_offset;
2342 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2343 &cl->answer, &cl->query );
2344 if( cl->answer.i_type != HTTPD_MSG_NONE )
2346 /* we have new data, so re-enter send mode */
2348 cl->p_buffer = cl->answer.p_body;
2349 cl->i_buffer_size = cl->answer.i_body;
2350 cl->answer.p_body = NULL;
2351 cl->answer.i_body = 0;
2352 cl->i_state = HTTPD_CLIENT_SENDING;
2356 /* we shouldn't wait too long */
2357 b_low_delay = VLC_TRUE;
2361 /* Special for BIDIR mode we also check reading */
2362 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2363 cl->i_state == HTTPD_CLIENT_SENDING )
2365 pufd->events |= POLLIN;
2368 if (pufd->events != 0)
2371 vlc_mutex_unlock( &host->lock );
2373 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2374 switch( poll( ufd, nfd, b_low_delay ? 20 : 100) )
2379 /* This is most likely a bug */
2380 msg_Err( host, "polling error: %m" );
2387 /* Handle client sockets */
2388 vlc_mutex_lock( &host->lock );
2390 for( int i_client = 0; i_client < host->i_client; i_client++ )
2392 httpd_client_t *cl = host->client[i_client];
2393 const struct pollfd *pufd = &ufd[host->nfd + i_client];
2395 assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
2397 if( cl->fd != pufd->fd )
2398 continue; // we were not waiting for this client
2399 if( pufd->revents == 0 )
2400 continue; // no event received
2402 cl->i_activity_date = now;
2404 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2406 httpd_ClientRecv( cl );
2408 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2410 httpd_ClientSend( cl );
2412 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2414 httpd_ClientTlsHsIn( cl );
2416 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2418 httpd_ClientTlsHsOut( cl );
2421 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2422 cl->i_state == HTTPD_CLIENT_SENDING &&
2423 (pufd->revents & POLLIN) )
2425 cl->b_read_waiting = VLC_TRUE;
2428 vlc_mutex_unlock( &host->lock );
2430 /* Handle server sockets (accept new connections) */
2431 for( nfd = 0; nfd < host->nfd; nfd++ )
2436 assert (ufd[nfd].fd == host->fds[nfd]);
2438 if( ufd[nfd].revents == 0 )
2442 int kludge[] = { ufd[nfd].fd, -1 };
2443 int fd = net_Accept( host, kludge, 0 );
2449 switch( tls_ServerSessionHandshake( p_tls, fd ) )
2452 msg_Err( host, "Rejecting TLS connection" );
2458 case 1: /* missing input - most likely */
2459 i_state = HTTPD_CLIENT_TLS_HS_IN;
2462 case 2: /* missing output */
2463 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2467 if( (p_tls == NULL) != (host->p_tls == NULL) )
2468 break; // wasted TLS session, cannot accept() anymore
2471 stats_UpdateInteger( host, p_total_counter, 1, NULL );
2472 stats_UpdateInteger( host, p_active_counter, 1, NULL );
2473 cl = httpd_ClientNew( fd, p_tls, now );
2475 vlc_mutex_lock( &host->lock );
2476 TAB_APPEND( host->i_client, host->client, cl );
2477 vlc_mutex_unlock( &host->lock );
2479 cl->i_state = i_state; // override state for TLS
2481 if (host->p_tls != NULL)
2482 break; // cannot accept further without new TLS session
2488 tls_ServerSessionClose( p_tls );
2489 if( p_total_counter )
2490 stats_CounterClean( p_total_counter );
2491 if( p_active_counter )
2492 stats_CounterClean( p_active_counter );
2495 #else /* ENABLE_HTTPD */
2497 /* We just define an empty wrapper */
2498 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2501 msg_Err( a, "HTTP daemon support is disabled" );
2505 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2507 msg_Err( a, "HTTP daemon support is disabled" );
2511 void httpd_HostDelete( httpd_host_t *a )
2515 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2516 char *psz_user, char *psz_password,
2517 const vlc_acl_t *p_acl )
2522 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2523 char *psz_user, char *psz_password,
2524 const vlc_acl_t *p_acl )
2529 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2530 httpd_callback_sys_t *d )
2535 void httpd_UrlDelete( httpd_url_t *a )
2539 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2544 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2549 void httpd_ClientModeStream( httpd_client_t *a )
2553 void httpd_ClientModeBidir( httpd_client_t *a )
2557 void httpd_FileDelete( httpd_file_t *a )
2561 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2562 char *e, httpd_file_callback_t f,
2563 httpd_file_sys_t *g )
2568 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2569 const char *psz_user,
2570 const char *psz_password,
2571 const vlc_acl_t *p_acl,
2572 httpd_handler_callback_t pf_fill,
2573 httpd_handler_sys_t *p_sys )
2578 void httpd_HandlerDelete( httpd_handler_t *handler )
2582 void httpd_RedirectDelete( httpd_redirect_t *a )
2586 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2592 void httpd_StreamDelete( httpd_stream_t *a )
2596 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2601 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2606 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2612 void httpd_MsgInit ( httpd_message_t *a )
2616 void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
2620 const char *httpd_MsgGet ( httpd_message_t *a, const char *b )
2625 void httpd_MsgClean( httpd_message_t *a )
2629 #endif /* ENABLE_HTTPD */