1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #include "vlc_httpd.h"
35 #if defined( UNDER_CE )
37 #elif defined( WIN32 )
38 # include <winsock2.h>
39 # include <ws2tcpip.h>
41 # define IN_MULTICAST(a) IN_CLASSD(a)
44 # include <netdb.h> /* hostent ... */
45 # include <sys/socket.h>
46 # include <netinet/in.h>
47 # ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
53 typedef struct httpd_t httpd_t;
55 typedef struct httpd_host_t httpd_host_t;
56 typedef struct httpd_url_t httpd_url_t;
57 typedef struct httpd_client_t httpd_client_t;
66 /* channel communication */
82 /* just to track the count of MSG */
95 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
107 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
109 /* for rtp over rtsp */
119 int64_t i_body_offset;
125 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
126 /* answer could be null, int this case no answer is requested */
127 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
130 /* create a new host */
131 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
133 void httpd_HostDelete( httpd_host_t * );
135 /* register a new url */
136 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
137 /* register callback on a url */
138 int httpd_UrlCatch( httpd_url_t *, int i_msg,
139 httpd_callback_t, httpd_callback_sys_t * );
141 void httpd_UrlDelete( httpd_url_t * );
144 void httpd_ClientModeStream( httpd_client_t *cl );
145 void httpd_ClientModeBidir( httpd_client_t *cl );
146 static void httpd_ClientClean( httpd_client_t *cl );
149 typedef struct httpd_file_t httpd_file_t;
150 typedef struct httpd_file_sys_t httpd_file_sys_t;
151 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
152 httpd_file_t *httpd_FileNew( httpd_host_t *,
153 char *psz_url, char *psz_mime,
154 char *psz_user, char *psz_password,
155 httpd_file_callback_t pf_fill,
156 httpd_file_sys_t *p_sys );
157 void httpd_FileDelete( httpd_file_t * );
159 typedef struct httpd_redirect_t httpd_redirect_t;
160 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
161 void httpd_RedirectDelete( httpd_redirect_t * );
164 typedef struct httpd_stream_t httpd_stream_t;
165 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
166 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
167 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
168 void httpd_StreamDelete( httpd_stream_t * );
171 /* Msg functions facilities */
172 void httpd_MsgInit( httpd_message_t * );
173 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
174 /* return "" if not found. The string is not allocated */
175 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
176 void httpd_MsgClean( httpd_message_t * );
184 /* vlc_mutex_t lock; */
190 static void httpd_ClientClean( httpd_client_t *cl );
192 /* each host run in his own thread */
202 /* address/port and socket for listening at connections */
203 struct sockaddr_in sock;
208 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
209 * This will slow down the url research but make my live easier
210 * All url will have their cb trigger, but only the first one can answer
216 httpd_client_t **client;
232 httpd_callback_sys_t *p_sys;
233 } catch[HTTPD_MSG_MAX];
239 HTTPD_CLIENT_RECEIVING,
240 HTTPD_CLIENT_RECEIVE_DONE,
242 HTTPD_CLIENT_SENDING,
243 HTTPD_CLIENT_SEND_DONE,
245 HTTPD_CLIENT_WAITING,
252 HTTPD_CLIENT_FILE, /* default */
253 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
254 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
257 struct httpd_client_t
263 struct sockaddr_in sock;
268 int b_read_waiting; /* stop as soon as possible sending */
270 mtime_t i_activity_date;
271 mtime_t i_activity_timeout;
273 /* buffer for reading header */
279 httpd_message_t query; /* client -> httpd */
280 httpd_message_t answer; /* httpd -> client */
284 /*****************************************************************************
286 *****************************************************************************/
287 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
288 static void b64_decode( char *dest, char *src )
293 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
294 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
295 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
296 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
297 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
298 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
299 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
300 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
301 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
302 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
303 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
304 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
305 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
306 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
307 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
308 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
311 for( i_level = 0; *src != '\0'; src++ )
315 c = b64[(unsigned int)*src];
327 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
331 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
335 *dest++ = ( ( last &0x03 ) << 6 ) | c;
350 { ".htm", "text/html" },
351 { ".html", "text/html" },
352 { ".txt", "text/plain" },
353 { ".xml", "text/xml" },
354 { ".dtd", "text/dtd" },
356 { ".css", "text/css" },
359 { ".gif", "image/gif" },
360 { ".jpe", "image/jpeg" },
361 { ".jpg", "image/jpeg" },
362 { ".jpeg", "image/jpeg" },
363 { ".png", "image/png" },
366 { ".avi", "video/avi" },
367 { ".asf", "video/x-ms-asf" },
368 { ".m1a", "audio/mpeg" },
369 { ".m2a", "audio/mpeg" },
370 { ".m1v", "video/mpeg" },
371 { ".m2v", "video/mpeg" },
372 { ".mp2", "audio/mpeg" },
373 { ".mp3", "audio/mpeg" },
374 { ".mpa", "audio/mpeg" },
375 { ".mpg", "video/mpeg" },
376 { ".mpeg", "video/mpeg" },
377 { ".mpe", "video/mpeg" },
378 { ".mov", "video/quicktime" },
379 { ".moov", "video/quicktime" },
380 { ".ogg", "application/ogg" },
381 { ".ogm", "application/ogg" },
382 { ".wav", "audio/wav" },
383 { ".wma", "audio/x-ms-wma" },
384 { ".wmv", "video/x-ms-wmv" },
390 static char *httpd_MimeFromUrl( char *psz_url )
395 psz_ext = strrchr( psz_url, '.' );
400 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
402 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
404 return http_mime[i].psz_mime;
408 return "application/octet-stream";
411 /*****************************************************************************
412 * High Level Funtions: httpd_file_t
413 *****************************************************************************/
421 httpd_file_callback_t pf_fill;
422 httpd_file_sys_t *p_sys;
427 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
429 httpd_file_t *file = (httpd_file_t*)p_sys;
431 if( answer == NULL || query == NULL )
435 answer->i_proto = HTTPD_PROTO_HTTP;
436 answer->i_version= query->i_version;
437 answer->i_type = HTTPD_MSG_ANSWER;
439 answer->i_status = 200;
440 answer->psz_status = strdup( "OK" );
442 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
443 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
445 if( query->i_type != HTTPD_MSG_HEAD )
447 char *psz_args = query->psz_args;
448 if( query->i_type == HTTPD_MSG_POST )
451 psz_args = query->p_body;
453 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
456 /* We respect client request */
457 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
459 httpd_MsgAdd( answer, "Connection",
460 httpd_MsgGet( &cl->query, "Connection" ) );
463 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
469 httpd_file_t *httpd_FileNew( httpd_host_t *host,
470 char *psz_url, char *psz_mime,
471 char *psz_user, char *psz_password,
472 httpd_file_callback_t pf_fill,
473 httpd_file_sys_t *p_sys )
475 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
477 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
478 psz_password ) ) == NULL )
484 file->psz_url = strdup( psz_url );
485 if( psz_mime && *psz_mime )
487 file->psz_mime = strdup( psz_mime );
491 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
494 file->pf_fill = pf_fill;
497 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
498 (httpd_callback_sys_t*)file );
499 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
500 (httpd_callback_sys_t*)file );
501 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
502 (httpd_callback_sys_t*)file );
507 void httpd_FileDelete( httpd_file_t *file )
509 httpd_UrlDelete( file->url );
511 free( file->psz_url );
512 free( file->psz_mime );
517 /*****************************************************************************
518 * High Level Funtions: httpd_redirect_t
519 *****************************************************************************/
520 struct httpd_redirect_t
526 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
527 httpd_client_t *cl, httpd_message_t *answer,
528 httpd_message_t *query )
530 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
533 if( answer == NULL || query == NULL )
537 answer->i_proto = query->i_proto;
538 answer->i_version= query->i_version;
539 answer->i_type = HTTPD_MSG_ANSWER;
540 answer->i_status = 301;
541 answer->psz_status = strdup( "Moved Permanently" );
543 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
544 p += sprintf( p, "<html>\n" );
545 p += sprintf( p, "<head>\n" );
546 p += sprintf( p, "<title>Redirection</title>\n" );
547 p += sprintf( p, "</head>\n" );
548 p += sprintf( p, "<body>\n" );
549 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
550 p += sprintf( p, "<hr />\n" );
551 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
552 p += sprintf( p, "</body>\n" );
553 p += sprintf( p, "</html>\n" );
554 answer->i_body = p - answer->p_body;
556 /* XXX check if it's ok or we need to set an absolute url */
557 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
559 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
564 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
567 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
569 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) )
574 rdir->psz_dst = strdup( psz_url_dst );
576 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
577 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
578 (httpd_callback_sys_t*)rdir );
579 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
580 (httpd_callback_sys_t*)rdir );
581 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
582 (httpd_callback_sys_t*)rdir );
583 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
584 (httpd_callback_sys_t*)rdir );
588 void httpd_RedirectDelete( httpd_redirect_t *rdir )
590 httpd_UrlDelete( rdir->url );
591 free( rdir->psz_dst );
595 /*****************************************************************************
596 * High Level Funtions: httpd_stream_t
597 *****************************************************************************/
598 struct httpd_stream_t
605 /* Header to send as first packet */
609 /* circular buffer */
610 int i_buffer_size; /* buffer size, can't be reallocated smaller */
611 uint8_t *p_buffer; /* buffer */
612 int64_t i_buffer_pos; /* absolute position from begining */
613 int64_t i_buffer_last_pos; /* a new connection will start with that */
616 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
617 httpd_client_t *cl, httpd_message_t *answer,
618 httpd_message_t *query )
620 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
622 if( answer == NULL || query == NULL || cl == NULL )
626 if( answer->i_body_offset > 0 )
632 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
633 answer->i_body_offset );
636 if( answer->i_body_offset >= stream->i_buffer_pos )
638 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
639 return VLC_EGENERIC; /* wait, no data available */
641 if( answer->i_body_offset + stream->i_buffer_size <
642 stream->i_buffer_pos )
644 /* this client isn't fast enough */
645 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
646 answer->i_body_offset, stream->i_buffer_last_pos );
647 answer->i_body_offset = stream->i_buffer_last_pos;
650 i_pos = answer->i_body_offset % stream->i_buffer_size;
651 i_write = stream->i_buffer_pos - answer->i_body_offset;
652 if( i_write > 10000 )
656 else if( i_write <= 0 )
658 return VLC_EGENERIC; /* wait, no data available */
661 /* Don't go past the end of the circular buffer */
662 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
664 /* using HTTPD_MSG_ANSWER -> data available */
665 answer->i_proto = HTTPD_PROTO_HTTP;
666 answer->i_version= 0;
667 answer->i_type = HTTPD_MSG_ANSWER;
669 answer->i_body = i_write;
670 answer->p_body = malloc( i_write );
671 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
673 answer->i_body_offset += i_write;
679 answer->i_proto = HTTPD_PROTO_HTTP;
680 answer->i_version= 0;
681 answer->i_type = HTTPD_MSG_ANSWER;
683 answer->i_status = 200;
684 answer->psz_status = strdup( "OK" );
686 if( query->i_type != HTTPD_MSG_HEAD )
688 httpd_ClientModeStream( cl );
689 vlc_mutex_lock( &stream->lock );
690 /* Send the header */
691 if( stream->i_header > 0 )
693 answer->i_body = stream->i_header;
694 answer->p_body = malloc( stream->i_header );
695 memcpy( answer->p_body, stream->p_header, stream->i_header );
697 answer->i_body_offset = stream->i_buffer_last_pos;
698 vlc_mutex_unlock( &stream->lock );
702 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
705 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
707 vlc_bool_t b_xplaystream = VLC_FALSE;
710 httpd_MsgAdd( answer, "Content-type", "%s",
711 "application/octet-stream" );
712 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
713 httpd_MsgAdd( answer, "Pragma", "no-cache" );
714 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
715 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
717 /* Check if there is a xPlayStrm=1 */
718 for( i = 0; i < query->i_name; i++ )
720 if( !strcasecmp( query->name[i], "Pragma" ) &&
721 !strcasecmp( query->value[i], "xPlayStrm=1" ) )
723 b_xplaystream = VLC_TRUE;
729 answer->i_body_offset = 0;
734 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
736 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
741 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
742 char *psz_url, char *psz_mime,
743 char *psz_user, char *psz_password )
745 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
747 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
748 psz_password ) ) == NULL )
753 vlc_mutex_init( host, &stream->lock );
754 if( psz_mime && *psz_mime )
756 stream->psz_mime = strdup( psz_mime );
760 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
762 stream->i_header = 0;
763 stream->p_header = NULL;
764 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
765 stream->p_buffer = malloc( stream->i_buffer_size );
766 /* We set to 1 to make life simpler
767 * (this way i_body_offset can never be 0) */
768 stream->i_buffer_pos = 1;
769 stream->i_buffer_last_pos = 1;
771 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
772 (httpd_callback_sys_t*)stream );
773 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
774 (httpd_callback_sys_t*)stream );
775 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
776 (httpd_callback_sys_t*)stream );
781 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
783 vlc_mutex_lock( &stream->lock );
784 if( stream->p_header )
786 free( stream->p_header );
787 stream->p_header = NULL;
789 stream->i_header = i_data;
792 stream->p_header = malloc( i_data );
793 memcpy( stream->p_header, p_data, i_data );
795 vlc_mutex_unlock( &stream->lock );
800 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
805 if( i_data < 0 || p_data == NULL )
809 vlc_mutex_lock( &stream->lock );
811 /* save this pointer (to be used by new connection) */
812 stream->i_buffer_last_pos = stream->i_buffer_pos;
814 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
820 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
822 /* Ok, we can't go past the end of our buffer */
823 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
825 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
830 stream->i_buffer_pos += i_data;
832 vlc_mutex_unlock( &stream->lock );
836 void httpd_StreamDelete( httpd_stream_t *stream )
838 httpd_UrlDelete( stream->url );
839 vlc_mutex_destroy( &stream->lock );
840 if( stream->psz_mime ) free( stream->psz_mime );
841 if( stream->p_header ) free( stream->p_header );
842 if( stream->p_buffer ) free( stream->p_buffer );
847 /*****************************************************************************
849 *****************************************************************************/
850 #define LISTEN_BACKLOG 100
852 #if defined( WIN32 ) || defined( UNDER_CE )
853 #define SOCKET_CLOSE(a) closesocket(a)
855 #define SOCKET_CLOSE(a) close(a)
858 static void httpd_HostThread( httpd_host_t * );
859 static int BuildAddr( struct sockaddr_in * p_socket,
860 const char * psz_address, int i_port );
863 /* create a new host */
864 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
869 struct sockaddr_in sock;
873 if( BuildAddr( &sock, psz_host, i_port ) )
875 msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
879 /* to be sure to avoid multiple creation */
880 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
881 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
882 vlc_mutex_lock( lockval.p_address );
884 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
886 msg_Info( p_this, "creating httpd" );
887 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
889 vlc_mutex_unlock( lockval.p_address );
896 vlc_object_yield( httpd );
897 vlc_object_attach( httpd, p_this->p_vlc );
900 /* verify if it already exist */
901 for( i = 0; i < httpd->i_host; i++ )
903 if( httpd->host[i]->sock.sin_port == sock.sin_port &&
904 ( httpd->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
905 httpd->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
908 host = httpd->host[i];
911 vlc_mutex_unlock( lockval.p_address );
913 msg_Dbg( p_this, "host already registered" );
917 /* create the new host */
918 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
920 vlc_mutex_init( httpd, &host->lock );
922 memcpy( &host->sock, &sock, sizeof( struct sockaddr_in ) );
928 /* create the listening socket */
929 if( ( host->fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
935 if( setsockopt( host->fd, SOL_SOCKET, SO_REUSEADDR,
936 (void *) &i, sizeof( i ) ) < 0 )
938 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
941 if( bind( host->fd, (struct sockaddr *)&host->sock,
942 sizeof( struct sockaddr_in ) ) < 0 )
944 msg_Err( p_this, "cannot bind socket" );
947 /* set to non-blocking */
948 #if defined( WIN32 ) || defined( UNDER_CE )
950 unsigned long i_dummy = 1;
951 if( ioctlsocket( host->fd, FIONBIO, &i_dummy ) != 0 )
953 msg_Err( p_this, "cannot set socket to non-blocking mode" );
959 unsigned int i_flags;
960 if( ( i_flags = fcntl( host->fd, F_GETFL, 0 ) ) < 0 )
962 msg_Err( p_this, "cannot F_GETFL socket" );
965 if( fcntl( host->fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
967 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
973 if( listen( host->fd, LISTEN_BACKLOG ) < 0 )
975 msg_Err( p_this, "cannot listen socket" );
979 /* create the thread */
980 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
981 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
983 msg_Err( p_this, "cannot spawn http host thread" );
987 /* now add it to httpd */
988 TAB_APPEND( httpd->i_host, httpd->host, host );
989 vlc_mutex_unlock( lockval.p_address );
994 vlc_mutex_unlock( lockval.p_address );
998 SOCKET_CLOSE( host->fd );
1000 vlc_mutex_destroy( &host->lock );
1001 vlc_object_destroy( host );
1003 /* TODO destroy no more used httpd TODO */
1004 vlc_object_release( httpd );
1009 void httpd_HostDelete( httpd_host_t *host )
1011 httpd_t *httpd = host->httpd;
1012 vlc_value_t lockval;
1015 msg_Dbg( host, "httpd_HostDelete" );
1017 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1018 vlc_mutex_lock( lockval.p_address );
1020 vlc_object_release( httpd );
1023 if( host->i_ref > 0 )
1026 vlc_mutex_unlock( lockval.p_address );
1027 msg_Dbg( host, "httpd_HostDelete: host still used" );
1030 TAB_REMOVE( httpd->i_host, httpd->host, host );
1032 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1035 vlc_thread_join( host );
1037 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1039 for( i = 0; i < host->i_url; i++ )
1041 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1043 for( i = 0; i < host->i_client; i++ )
1045 httpd_client_t *cl = host->client[i];
1046 msg_Warn( host, "client still connected" );
1047 httpd_ClientClean( cl );
1048 TAB_REMOVE( host->i_client, host->client, cl );
1054 SOCKET_CLOSE( host->fd );
1055 vlc_mutex_destroy( &host->lock );
1056 vlc_object_destroy( host );
1058 if( httpd->i_host <= 0 )
1060 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1061 vlc_object_detach( httpd );
1062 vlc_object_destroy( httpd );
1064 vlc_mutex_unlock( lockval.p_address );
1067 /* register a new url */
1068 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1069 char *psz_user, char *psz_password,
1070 vlc_bool_t b_check )
1075 vlc_mutex_lock( &host->lock );
1078 for( i = 0; i < host->i_url; i++ )
1080 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1082 msg_Warn( host->httpd,
1083 "cannot add '%s' (url already defined)", psz_url );
1084 vlc_mutex_unlock( &host->lock );
1090 url = malloc( sizeof( httpd_url_t ) );
1093 vlc_mutex_init( host->httpd, &url->lock );
1094 url->psz_url = strdup( psz_url );
1095 url->psz_user = strdup( psz_user ? psz_user : "" );
1096 url->psz_password = strdup( psz_password ? psz_password : "" );
1097 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1099 url->catch[i].cb = NULL;
1100 url->catch[i].p_sys = NULL;
1103 TAB_APPEND( host->i_url, host->url, url );
1104 vlc_mutex_unlock( &host->lock );
1109 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1110 char *psz_user, char *psz_password )
1112 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1113 psz_password, VLC_FALSE );
1116 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1117 char *psz_user, char *psz_password )
1119 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1120 psz_password, VLC_TRUE );
1123 /* register callback on a url */
1124 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1125 httpd_callback_sys_t *p_sys )
1127 vlc_mutex_lock( &url->lock );
1128 url->catch[i_msg].cb = cb;
1129 url->catch[i_msg].p_sys= p_sys;
1130 vlc_mutex_unlock( &url->lock );
1137 void httpd_UrlDelete( httpd_url_t *url )
1139 httpd_host_t *host = url->host;
1142 vlc_mutex_lock( &host->lock );
1143 TAB_REMOVE( host->i_url, host->url, url );
1145 vlc_mutex_destroy( &url->lock );
1146 free( url->psz_url );
1147 free( url->psz_user );
1148 free( url->psz_password );
1150 for( i = 0; i < host->i_client; i++ )
1152 httpd_client_t *client = host->client[i];
1154 if( client->url == url )
1156 /* TODO complete it */
1157 msg_Warn( host, "force closing connections" );
1158 httpd_ClientClean( client );
1159 TAB_REMOVE( host->i_client, host->client, client );
1165 vlc_mutex_unlock( &host->lock );
1168 void httpd_MsgInit( httpd_message_t *msg )
1171 msg->i_type = HTTPD_MSG_NONE;
1172 msg->i_proto = HTTPD_PROTO_NONE;
1173 msg->i_version = -1;
1176 msg->psz_status = NULL;
1178 msg->psz_url = NULL;
1179 msg->psz_args = NULL;
1181 msg->i_channel = -1;
1188 msg->i_body_offset = 0;
1193 void httpd_MsgClean( httpd_message_t *msg )
1197 if( msg->psz_status )
1199 free( msg->psz_status );
1203 free( msg->psz_url );
1207 free( msg->psz_args );
1209 for( i = 0; i < msg->i_name; i++ )
1211 free( msg->name[i] );
1212 free( msg->value[i] );
1224 free( msg->p_body );
1226 httpd_MsgInit( msg );
1229 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1233 for( i = 0; i < msg->i_name; i++ )
1235 if( !strcasecmp( msg->name[i], name ))
1237 return msg->value[i];
1242 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1247 va_start( args, psz_value );
1248 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1249 vasprintf( &value, psz_value, args );
1252 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1253 value = calloc( i_size, sizeof( char ) );
1254 vsnprintf( value, i_size, psz_value, args );
1255 value[i_size - 1] = 0;
1260 name = strdup( name );
1262 TAB_APPEND( msg->i_name, msg->name, name );
1263 TAB_APPEND( msg->i_value, msg->value, value );
1266 static void httpd_ClientInit( httpd_client_t *cl )
1268 cl->i_state = HTTPD_CLIENT_RECEIVING;
1269 cl->i_activity_date = mdate();
1270 cl->i_activity_timeout = 10000000LL;
1271 cl->i_buffer_size = 10000;
1273 cl->p_buffer = malloc( cl->i_buffer_size );
1274 cl->i_mode = HTTPD_CLIENT_FILE;
1275 cl->b_read_waiting = VLC_FALSE;
1277 httpd_MsgInit( &cl->query );
1278 httpd_MsgInit( &cl->answer );
1281 void httpd_ClientModeStream( httpd_client_t *cl )
1283 cl->i_mode = HTTPD_CLIENT_STREAM;
1286 void httpd_ClientModeBidir( httpd_client_t *cl )
1288 cl->i_mode = HTTPD_CLIENT_BIDIR;
1291 static void httpd_ClientClean( httpd_client_t *cl )
1295 SOCKET_CLOSE( cl->fd );
1299 httpd_MsgClean( &cl->answer );
1300 httpd_MsgClean( &cl->query );
1304 free( cl->p_buffer );
1305 cl->p_buffer = NULL;
1309 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_in *sock )
1311 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1312 /* set this new socket non-block */
1313 #if defined( WIN32 ) || defined( UNDER_CE )
1315 unsigned long i_dummy = 1;
1316 ioctlsocket( fd, FIONBIO, &i_dummy );
1319 fcntl( fd, F_SETFL, O_NONBLOCK );
1326 httpd_ClientInit( cl );
1331 static void httpd_ClientRecv( httpd_client_t *cl )
1335 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1337 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1338 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 4 - cl->i_buffer, 0 );
1342 cl->i_buffer += i_len;
1345 if( cl->i_buffer >= 4 )
1347 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1349 if( cl->p_buffer[0] == '$' )
1351 /* RTSP (rtp over rtsp) */
1352 cl->query.i_proto = HTTPD_PROTO_RTSP;
1353 cl->query.i_type = HTTPD_MSG_CHANNEL;
1354 cl->query.i_channel = cl->p_buffer[1];
1355 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1356 cl->query.p_body = malloc( cl->query.i_body );
1360 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1362 cl->query.i_proto = HTTPD_PROTO_HTTP;
1363 cl->query.i_type = HTTPD_MSG_ANSWER;
1365 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1367 cl->query.i_proto = HTTPD_PROTO_RTSP;
1368 cl->query.i_type = HTTPD_MSG_ANSWER;
1370 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1371 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1372 !strncmp( cl->p_buffer, "POST", 4 ) )
1374 cl->query.i_proto = HTTPD_PROTO_HTTP;
1375 cl->query.i_type = HTTPD_MSG_NONE;
1379 cl->query.i_proto = HTTPD_PROTO_RTSP;
1380 cl->query.i_type = HTTPD_MSG_NONE;
1384 else if( cl->query.i_body > 0 )
1386 /* we are reading the body of a request or a channel */
1387 i_len = recv( cl->fd, &cl->query.p_body[cl->i_buffer],
1388 cl->query.i_body - cl->i_buffer, 0 );
1391 cl->i_buffer += i_len;
1393 if( cl->i_buffer >= cl->query.i_body )
1395 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1400 /* we are reading a header -> char by char */
1403 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 1, 0 );
1410 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1412 cl->i_buffer_size += 1024;
1413 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1415 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1416 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1420 /* we have finished the header so parse it and set i_body */
1421 cl->p_buffer[cl->i_buffer] = '\0';
1423 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1425 cl->query.i_status =
1426 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1431 cl->query.psz_status = strdup( p );
1443 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1444 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1445 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1447 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1448 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1449 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1450 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1451 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1452 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1454 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1459 cl->query.i_type = HTTPD_MSG_NONE;
1461 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1463 for( i = 0; msg_type[i].name != NULL; i++ )
1465 if( !strncmp( cl->p_buffer, msg_type[i].name,
1466 strlen( msg_type[i].name ) ) )
1468 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1469 cl->query.i_type = msg_type[i].i_type;
1470 if( cl->query.i_proto != msg_type[i].i_proto )
1473 cl->query.i_proto = HTTPD_PROTO_NONE;
1474 cl->query.i_type = HTTPD_MSG_NONE;
1481 if( strstr( cl->p_buffer, "HTTP/1." ) )
1483 cl->query.i_proto = HTTPD_PROTO_HTTP;
1485 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1487 cl->query.i_proto = HTTPD_PROTO_RTSP;
1499 p2 = strchr( p, ' ' );
1504 if( !strncasecmp( p, "rtsp:", 5 ) )
1506 /* for rtsp url, you have rtsp://localhost:port/path */
1508 while( *p == '/' ) p++;
1509 while( *p && *p != '/' ) p++;
1511 cl->query.psz_url = strdup( p );
1512 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1515 cl->query.psz_args = strdup( p3 );
1523 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1525 cl->query.i_proto = HTTPD_PROTO_HTTP;
1526 cl->query.i_version = atoi( p2+7 );
1528 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1530 cl->query.i_proto = HTTPD_PROTO_RTSP;
1531 cl->query.i_version = atoi( p2+7 );
1539 p = strchr( p, '\n' );
1543 while( *p == '\n' || *p == '\r' )
1547 while( p && *p != '\0' )
1550 char *eol = p = strchr( p, '\n' );
1553 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1558 if( ( colon = strchr( line, ':' ) ) )
1564 while( *colon == ' ' )
1568 name = strdup( line );
1569 value = strdup( colon );
1571 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1572 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1574 if( !strcasecmp( name, "Content-Length" ) )
1576 cl->query.i_body = atol( value );
1583 while( *p == '\n' || *p == '\r' )
1590 if( cl->query.i_body > 0 )
1592 /* TODO Mhh, handle the case client will only send a request and close the connection
1593 * to mark and of body (probably only RTSP) */
1594 cl->query.p_body = malloc( cl->query.i_body );
1599 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1605 /* check if the client is to be set to dead */
1606 #if defined( WIN32 ) || defined( UNDER_CE )
1607 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1609 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1612 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1614 /* connection closed -> end of data */
1615 if( cl->query.i_body > 0 )
1617 cl->query.i_body = cl->i_buffer;
1619 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1623 cl->i_state = HTTPD_CLIENT_DEAD;
1626 cl->i_activity_date = mdate();
1628 /* Debugging only */
1629 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1633 fprintf( stderr, "received new request\n" );
1634 fprintf( stderr, " - proto=%s\n",
1635 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1636 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1637 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1638 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1640 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1641 cl->query.psz_status );
1643 else if( cl->query.i_type != HTTPD_MSG_NONE )
1645 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1647 for( i = 0; i < cl->query.i_name; i++ )
1649 fprintf( stderr, " - option name='%s' value='%s'\n",
1650 cl->query.name[i], cl->query.value[i] );
1655 static void httpd_ClientSend( httpd_client_t *cl )
1660 if( cl->i_buffer < 0 )
1662 /* We need to create the header */
1666 i_size = strlen( "HTTP/1.") + 10 + 10 +
1667 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1668 for( i = 0; i < cl->answer.i_name; i++ )
1670 i_size += strlen( cl->answer.name[i] ) + 2 +
1671 strlen( cl->answer.value[i] ) + 2;
1674 if( cl->i_buffer_size < i_size )
1676 cl->i_buffer_size = i_size;
1677 free( cl->p_buffer );
1678 cl->p_buffer = malloc( i_size );
1682 p += sprintf( p, "%s/1.%d %d %s\r\n",
1683 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1684 cl->answer.i_version,
1685 cl->answer.i_status, cl->answer.psz_status );
1686 for( i = 0; i < cl->answer.i_name; i++ )
1688 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1689 cl->answer.value[i] );
1691 p += sprintf( p, "\r\n" );
1694 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1696 fprintf( stderr, "sending answer\n" );
1697 fprintf( stderr, "%s", cl->p_buffer );
1700 i_len = send( cl->fd, &cl->p_buffer[cl->i_buffer],
1701 cl->i_buffer_size - cl->i_buffer, 0 );
1704 cl->i_activity_date = mdate();
1705 cl->i_buffer += i_len;
1707 if( cl->i_buffer >= cl->i_buffer_size )
1709 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1710 !cl->b_read_waiting )
1712 /* catch more body data */
1713 int i_msg = cl->query.i_type;
1714 int64_t i_offset = cl->answer.i_body_offset;
1716 httpd_MsgClean( &cl->answer );
1717 cl->answer.i_body_offset = i_offset;
1719 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1720 &cl->answer, &cl->query );
1723 if( cl->answer.i_body > 0 )
1725 /* send the body data */
1726 free( cl->p_buffer );
1727 cl->p_buffer = cl->answer.p_body;
1728 cl->i_buffer_size = cl->answer.i_body;
1731 cl->answer.i_body = 0;
1732 cl->answer.p_body = NULL;
1737 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1743 #if defined( WIN32 ) || defined( UNDER_CE )
1744 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1746 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1750 cl->i_state = HTTPD_CLIENT_DEAD;
1755 static void httpd_HostThread( httpd_host_t *host )
1757 while( !host->b_die )
1759 struct timeval timeout;
1762 int i_handle_max = 0;
1765 int b_low_delay = 0;
1767 if( host->i_url <= 0 )
1774 /* built a set of handle to select */
1775 FD_ZERO( &fds_read );
1776 FD_ZERO( &fds_write );
1778 FD_SET( host->fd, &fds_read );
1779 i_handle_max = host->fd;
1781 /* add all socket that should be read/write and close dead connection */
1782 vlc_mutex_lock( &host->lock );
1783 for( i_client = 0; i_client < host->i_client; i_client++ )
1785 httpd_client_t *cl = host->client[i_client];
1787 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1788 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1789 cl->i_activity_date + cl->i_activity_timeout < mdate() ) ) )
1791 msg_Dbg( host, "connection closed(%s)",
1792 inet_ntoa(cl->sock.sin_addr) );
1793 httpd_ClientClean( cl );
1794 TAB_REMOVE( host->i_client, host->client, cl );
1799 else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
1801 FD_SET( cl->fd, &fds_read );
1802 i_handle_max = __MAX( i_handle_max, cl->fd );
1804 else if( cl->i_state == HTTPD_CLIENT_SENDING )
1806 FD_SET( cl->fd, &fds_write );
1807 i_handle_max = __MAX( i_handle_max, cl->fd );
1809 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1811 httpd_message_t *answer = &cl->answer;
1812 httpd_message_t *query = &cl->query;
1813 int i_msg = query->i_type;
1815 httpd_MsgInit( answer );
1817 /* Handle what we received */
1818 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
1819 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
1821 /* we can only receive request from client when not
1824 cl->i_state = HTTPD_CLIENT_DEAD;
1826 else if( i_msg == HTTPD_MSG_ANSWER )
1828 /* We are in BIDIR mode, trigger the callback and then
1829 * check for new data */
1830 if( cl->url && cl->url->catch[i_msg].cb )
1832 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1835 cl->i_state = HTTPD_CLIENT_WAITING;
1837 else if( i_msg == HTTPD_MSG_CHANNEL )
1839 /* We are in BIDIR mode, trigger the callback and then
1840 * check for new data */
1841 if( cl->url && cl->url->catch[i_msg].cb )
1843 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
1846 cl->i_state = HTTPD_CLIENT_WAITING;
1848 else if( i_msg == HTTPD_MSG_OPTIONS )
1853 answer->i_proto = query->i_proto ;
1854 answer->i_type = HTTPD_MSG_ANSWER;
1855 answer->i_version= 0;
1856 answer->i_status = 200;
1857 answer->psz_status = strdup( "Ok" );
1860 answer->p_body = NULL;
1862 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
1863 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1864 httpd_MsgAdd( answer, "Server", "VLC Server" );
1865 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
1866 "TEARDOWN, PLAY, PAUSE" );
1867 httpd_MsgAdd( answer, "Content-Length", "%d",
1870 cl->i_buffer = -1; /* Force the creation of the answer in
1871 * httpd_ClientSend */
1872 cl->i_state = HTTPD_CLIENT_SENDING;
1874 else if( i_msg == HTTPD_MSG_NONE )
1876 if( query->i_proto == HTTPD_PROTO_NONE )
1879 cl->i_state = HTTPD_CLIENT_DEAD;
1886 answer->i_proto = query->i_proto ;
1887 answer->i_type = HTTPD_MSG_ANSWER;
1888 answer->i_version= 0;
1889 answer->i_status = 501;
1890 answer->psz_status = strdup( "Unimplemented" );
1892 p = answer->p_body = malloc( 1000 );
1894 p += sprintf( p, "<html>\n" );
1895 p += sprintf( p, "<head>\n" );
1896 p += sprintf( p, "<title>Error 501</title>\n" );
1897 p += sprintf( p, "</head>\n" );
1898 p += sprintf( p, "<body>\n" );
1899 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
1900 p += sprintf( p, "<hr />\n" );
1901 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1902 p += sprintf( p, "</body>\n" );
1903 p += sprintf( p, "</html>\n" );
1905 answer->i_body = p - answer->p_body;
1906 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1908 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1909 cl->i_state = HTTPD_CLIENT_SENDING;
1914 vlc_bool_t b_auth_failed = VLC_FALSE;
1917 /* Search the url and trigger callbacks */
1918 for( i = 0; i < host->i_url; i++ )
1920 httpd_url_t *url = host->url[i];
1922 if( !strcmp( url->psz_url, query->psz_url ) )
1924 if( url->catch[i_msg].cb )
1926 if( answer && ( *url->psz_user || *url->psz_password ) )
1928 /* create the headers */
1929 char id[strlen(url->psz_user)+strlen(url->psz_password) + 2];
1930 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
1931 char auth[strlen(b64) +1];
1933 sprintf( id, "%s:%s", url->psz_user, url->psz_password );
1934 if( !strncasecmp( b64, "BASIC", 5 ) )
1937 while( *b64 == ' ' )
1941 b64_decode( auth, b64 );
1947 if( strcmp( id, auth ) )
1949 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
1950 /* We fail for all url */
1951 b_auth_failed = VLC_TRUE;
1956 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
1958 /* only one url can answer */
1960 if( cl->url == NULL )
1972 answer->i_proto = query->i_proto;
1973 answer->i_type = HTTPD_MSG_ANSWER;
1974 answer->i_version= 0;
1975 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
1979 answer->i_status = 401;
1980 answer->psz_status = strdup( "Authorization Required" );
1982 p += sprintf( p, "<html>\n" );
1983 p += sprintf( p, "<head>\n" );
1984 p += sprintf( p, "<title>Error 401</title>\n" );
1985 p += sprintf( p, "</head>\n" );
1986 p += sprintf( p, "<body>\n" );
1987 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
1988 p += sprintf( p, "<hr />\n" );
1989 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1990 p += sprintf( p, "</body>\n" );
1991 p += sprintf( p, "</html>\n" );
1995 /* no url registered */
1996 answer->i_status = 404;
1997 answer->psz_status = strdup( "Not found" );
1999 p += sprintf( p, "<html>\n" );
2000 p += sprintf( p, "<head>\n" );
2001 p += sprintf( p, "<title>Error 404</title>\n" );
2002 p += sprintf( p, "</head>\n" );
2003 p += sprintf( p, "<body>\n" );
2004 p += sprintf( p, "<h1><center> 404 Ressource not found(%s)</center></h1>\n", query->psz_url );
2005 p += sprintf( p, "<hr />\n" );
2006 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2007 p += sprintf( p, "</body>\n" );
2008 p += sprintf( p, "</html>\n" );
2011 answer->i_body = p - answer->p_body;
2012 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2014 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2015 cl->i_state = HTTPD_CLIENT_SENDING;
2018 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2020 if( cl->i_mode == HTTPD_CLIENT_FILE )
2023 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2024 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2025 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2026 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2027 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2028 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2030 httpd_MsgClean( &cl->query );
2031 httpd_MsgInit( &cl->query );
2034 cl->i_buffer_size = 1000;
2035 free( cl->p_buffer );
2036 cl->p_buffer = malloc( cl->i_buffer_size );
2037 cl->i_state = HTTPD_CLIENT_RECEIVING;
2041 cl->i_state = HTTPD_CLIENT_DEAD;
2043 httpd_MsgClean( &cl->answer );
2045 else if( cl->b_read_waiting )
2047 /* we have a message waiting for us to read it */
2048 httpd_MsgClean( &cl->answer );
2049 httpd_MsgClean( &cl->query );
2052 cl->i_buffer_size = 1000;
2053 free( cl->p_buffer );
2054 cl->p_buffer = malloc( cl->i_buffer_size );
2055 cl->i_state = HTTPD_CLIENT_RECEIVING;
2056 cl->b_read_waiting = VLC_FALSE;
2060 int64_t i_offset = cl->answer.i_body_offset;
2061 httpd_MsgClean( &cl->answer );
2063 cl->answer.i_body_offset = i_offset;
2064 free( cl->p_buffer );
2065 cl->p_buffer = NULL;
2067 cl->i_buffer_size = 0;
2069 cl->i_state = HTTPD_CLIENT_WAITING;
2072 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2074 int64_t i_offset = cl->answer.i_body_offset;
2075 int i_msg = cl->query.i_type;
2077 httpd_MsgInit( &cl->answer );
2078 cl->answer.i_body_offset = i_offset;
2080 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2081 &cl->answer, &cl->query );
2082 if( cl->answer.i_type != HTTPD_MSG_NONE )
2084 /* we have new data, so reenter send mode */
2086 cl->p_buffer = cl->answer.p_body;
2087 cl->i_buffer_size = cl->answer.i_body;
2088 cl->answer.p_body = NULL;
2089 cl->answer.i_body = 0;
2090 cl->i_state = HTTPD_CLIENT_SENDING;
2094 /* we shouldn't wait too long */
2095 b_low_delay = VLC_TRUE;
2099 /* Special for BIDIR mode we also check reading */
2100 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2101 cl->i_state == HTTPD_CLIENT_SENDING )
2103 FD_SET( cl->fd, &fds_read );
2104 i_handle_max = __MAX( i_handle_max, cl->fd );
2107 vlc_mutex_unlock( &host->lock );
2109 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2111 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2113 i_ret = select( i_handle_max + 1,
2114 &fds_read, &fds_write, NULL, &timeout );
2116 if( i_ret == -1 && errno != EINTR )
2118 msg_Warn( host, "cannot select sockets" );
2122 else if( i_ret <= 0 )
2127 /* accept new connections */
2128 if( FD_ISSET( host->fd, &fds_read ) )
2130 int i_sock_size = sizeof( struct sockaddr_in );
2131 struct sockaddr_in sock;
2134 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2137 httpd_client_t *cl = httpd_ClientNew( fd, &sock );
2139 vlc_mutex_lock( &host->lock );
2140 TAB_APPEND( host->i_client, host->client, cl );
2141 vlc_mutex_unlock( &host->lock );
2143 msg_Dbg( host, "new connection (%s)",
2144 inet_ntoa(sock.sin_addr) );
2147 /* now try all others socket */
2148 vlc_mutex_lock( &host->lock );
2149 for( i_client = 0; i_client < host->i_client; i_client++ )
2151 httpd_client_t *cl = host->client[i_client];
2152 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2154 httpd_ClientRecv( cl );
2156 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2158 httpd_ClientSend( cl );
2161 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2162 cl->i_state == HTTPD_CLIENT_SENDING &&
2163 FD_ISSET( cl->fd, &fds_read ) )
2165 cl->b_read_waiting = VLC_TRUE;
2168 vlc_mutex_unlock( &host->lock );
2172 static int BuildAddr( struct sockaddr_in * p_socket,
2173 const char * psz_address, int i_port )
2176 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2177 p_socket->sin_family = AF_INET; /* family */
2178 p_socket->sin_port = htons( (uint16_t)i_port );
2181 p_socket->sin_addr.s_addr = INADDR_ANY;
2185 struct hostent * p_hostent;
2187 /* Try to convert address directly from in_addr - this will work if
2188 * psz_address is dotted decimal. */
2189 #ifdef HAVE_ARPA_INET_H
2190 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2192 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2193 if( p_socket->sin_addr.s_addr == INADDR_NONE )
2196 /* We have a fqdn, try to find its address */
2197 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2202 /* Copy the first address of the host in the socket address */
2203 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
2204 p_hostent->h_length );