1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: httpd.c,v 1.1 2004/03/03 13:23:47 fenrir Exp $
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 );
148 typedef struct httpd_file_t httpd_file_t;
149 typedef struct httpd_file_sys_t httpd_file_sys_t;
150 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
151 httpd_file_t *httpd_FileNew( httpd_host_t *,
152 char *psz_url, char *psz_mime,
153 char *psz_user, char *psz_password,
154 httpd_file_callback_t pf_fill,
155 httpd_file_sys_t *p_sys );
156 void httpd_FileDelete( httpd_file_t * );
158 typedef struct httpd_redirect_t httpd_redirect_t;
159 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
160 void httpd_RedirectDelete( httpd_redirect_t * );
163 typedef struct httpd_stream_t httpd_stream_t;
164 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
165 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
166 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
167 void httpd_StreamDelete( httpd_stream_t * );
170 /* Msg functions facilities */
171 void httpd_MsgInit( httpd_message_t * );
172 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
173 /* return "" if not found. The string is not allocated */
174 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
175 void httpd_MsgClean( httpd_message_t * );
183 /* vlc_mutex_t lock; */
189 /* each host run in his own thread */
199 /* address/port and socket for listening at connections */
200 struct sockaddr_in sock;
205 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
206 * This will slow down the url research but make my live easier
207 * All url will have their cb trigger, but only the first one can answer
213 httpd_client_t **client;
229 httpd_callback_sys_t *p_sys;
230 } catch[HTTPD_MSG_MAX];
236 HTTPD_CLIENT_RECEIVING,
237 HTTPD_CLIENT_RECEIVE_DONE,
239 HTTPD_CLIENT_SENDING,
240 HTTPD_CLIENT_SEND_DONE,
242 HTTPD_CLIENT_WAITING,
249 HTTPD_CLIENT_FILE, /* default */
250 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
251 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
254 struct httpd_client_t
260 struct sockaddr_in sock;
265 int b_read_waiting; /* stop as soon as possible sending */
267 mtime_t i_activity_date;
268 mtime_t i_activity_timeout;
270 /* buffer for reading header */
276 httpd_message_t query; /* client -> httpd */
277 httpd_message_t answer; /* httpd -> client */
281 /*****************************************************************************
283 *****************************************************************************/
284 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
285 static void b64_decode( char *dest, char *src )
290 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
291 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
292 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
293 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
294 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
295 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
296 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
297 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
298 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
299 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
300 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
301 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
302 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
303 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
304 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
305 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
308 for( i_level = 0; *src != '\0'; src++ )
312 c = b64[(unsigned int)*src];
324 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
328 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
332 *dest++ = ( ( last &0x03 ) << 6 ) | c;
347 { ".htm", "text/html" },
348 { ".html", "text/html" },
349 { ".txt", "text/plain" },
350 { ".xml", "text/xml" },
351 { ".dtd", "text/dtd" },
353 { ".css", "text/css" },
356 { ".gif", "image/gif" },
357 { ".jpe", "image/jpeg" },
358 { ".jpg", "image/jpeg" },
359 { ".jpeg", "image/jpeg" },
360 { ".png", "image/png" },
363 { ".avi", "video/avi" },
364 { ".asf", "video/x-ms-asf" },
365 { ".m1a", "audio/mpeg" },
366 { ".m2a", "audio/mpeg" },
367 { ".m1v", "video/mpeg" },
368 { ".m2v", "video/mpeg" },
369 { ".mp2", "audio/mpeg" },
370 { ".mp3", "audio/mpeg" },
371 { ".mpa", "audio/mpeg" },
372 { ".mpg", "video/mpeg" },
373 { ".mpeg", "video/mpeg" },
374 { ".mpe", "video/mpeg" },
375 { ".mov", "video/quicktime" },
376 { ".moov", "video/quicktime" },
377 { ".ogg", "application/ogg" },
378 { ".ogm", "application/ogg" },
379 { ".wav", "audio/wav" },
380 { ".wma", "audio/x-ms-wma" },
381 { ".wmv", "video/x-ms-wmv" },
387 static char *httpd_MimeFromUrl( char *psz_url )
392 psz_ext = strrchr( psz_url, '.' );
397 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
399 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
401 return http_mime[i].psz_mime;
405 return "application/octet-stream";
408 /*****************************************************************************
409 * High Level Funtions: httpd_file_t
410 *****************************************************************************/
418 httpd_file_callback_t pf_fill;
419 httpd_file_sys_t *p_sys;
424 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
426 httpd_file_t *file = (httpd_file_t*)p_sys;
428 if( answer == NULL || query == NULL )
432 answer->i_proto = HTTPD_PROTO_HTTP;
433 answer->i_version= query->i_version;
434 answer->i_type = HTTPD_MSG_ANSWER;
436 answer->i_status = 200;
437 answer->psz_status = strdup( "OK" );
439 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
440 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
442 if( query->i_type != HTTPD_MSG_HEAD )
444 char *psz_args = query->psz_args;
445 if( query->i_type == HTTPD_MSG_POST )
448 psz_args = query->p_body;
450 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body, &answer->i_body );
452 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
458 httpd_file_t *httpd_FileNew( httpd_host_t *host,
459 char *psz_url, char *psz_mime,
460 char *psz_user, char *psz_password,
461 httpd_file_callback_t pf_fill,
462 httpd_file_sys_t *p_sys )
464 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
466 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user, psz_password ) ) == NULL )
472 file->psz_url = strdup( psz_url );
473 if( psz_mime && *psz_mime )
475 file->psz_mime = strdup( psz_mime );
479 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
482 file->pf_fill = pf_fill;
485 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack, (httpd_callback_sys_t*)file );
486 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack, (httpd_callback_sys_t*)file );
487 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack, (httpd_callback_sys_t*)file );
492 void httpd_FileDelete( httpd_file_t *file )
494 httpd_UrlDelete( file->url );
496 free( file->psz_url );
497 free( file->psz_mime );
502 /*****************************************************************************
503 * High Level Funtions: httpd_redirect_t
504 *****************************************************************************/
505 struct httpd_redirect_t
511 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
513 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
516 if( answer == NULL || query == NULL )
520 answer->i_proto = query->i_proto;
521 answer->i_version= query->i_version;
522 answer->i_type = HTTPD_MSG_ANSWER;
523 answer->i_status = 301;
524 answer->psz_status = strdup( "Moved Permanently" );
526 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
527 p += sprintf( p, "<html>\n" );
528 p += sprintf( p, "<head>\n" );
529 p += sprintf( p, "<title>Redirection</title>\n" );
530 p += sprintf( p, "</head>\n" );
531 p += sprintf( p, "<body>\n" );
532 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
533 p += sprintf( p, "<hr />\n" );
534 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
535 p += sprintf( p, "</body>\n" );
536 p += sprintf( p, "</html>\n" );
537 answer->i_body = p - answer->p_body;
539 /* XXX check if it's ok or we need to set an absolute url */
540 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
542 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
547 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst, char *psz_url_src )
549 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
551 if( ( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) == NULL )
556 rdir->psz_dst = strdup( psz_url_dst );
557 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
558 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
559 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
560 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
562 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
566 void httpd_RedirectDelete( httpd_redirect_t *rdir )
568 httpd_UrlDelete( rdir->url );
569 free( rdir->psz_dst );
573 /*****************************************************************************
574 * High Level Funtions: httpd_stream_t
575 *****************************************************************************/
576 struct httpd_stream_t
583 /* Header to send as first packet */
587 /* circular buffer */
588 int i_buffer_size; /* buffer size, can't be reallocated smaller */
589 uint8_t *p_buffer; /* buffer */
590 int64_t i_buffer_pos; /* absolute position from begining */
591 int64_t i_buffer_last_pos; /* a new connection will start with that */
594 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
596 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
598 if( answer == NULL || query == NULL || cl == NULL )
602 if( answer->i_body_offset > 0 )
607 /* fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n", answer->i_body_offset ); */
609 if( answer->i_body_offset >= stream->i_buffer_pos )
611 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
612 return VLC_EGENERIC; /* wait, no data available */
614 if( answer->i_body_offset + stream->i_buffer_size < stream->i_buffer_pos )
616 /* this client isn't fast enough */
617 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
618 answer->i_body_offset, stream->i_buffer_last_pos );
619 answer->i_body_offset = stream->i_buffer_last_pos;
622 i_pos = answer->i_body_offset % stream->i_buffer_size;
623 i_write = stream->i_buffer_pos - answer->i_body_offset;
624 if( i_write > 10000 )
628 else if( i_write <= 0 )
630 return VLC_EGENERIC; /* wait, no data available */
633 /* using HTTPD_MSG_ANSWER -> data available */
634 answer->i_proto = HTTPD_PROTO_HTTP;
635 answer->i_version= 0;
636 answer->i_type = HTTPD_MSG_ANSWER;
638 answer->i_body = i_write;
639 answer->p_body = malloc( i_write );
640 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
642 answer->i_body_offset += i_write;
648 answer->i_proto = HTTPD_PROTO_HTTP;
649 answer->i_version= 0;
650 answer->i_type = HTTPD_MSG_ANSWER;
652 answer->i_status = 200;
653 answer->psz_status = strdup( "OK" );
655 if( query->i_type != HTTPD_MSG_HEAD )
657 httpd_ClientModeStream( cl );
658 vlc_mutex_lock( &stream->lock );
659 /* Send the header */
660 if( stream->i_header > 0 )
662 answer->i_body = stream->i_header;
663 answer->p_body = malloc( stream->i_header );
664 memcpy( answer->p_body, stream->p_header, stream->i_header );
666 answer->i_body_offset = stream->i_buffer_last_pos;
667 vlc_mutex_unlock( &stream->lock );
671 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
674 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
676 vlc_bool_t b_xplaystream = VLC_FALSE;
679 httpd_MsgAdd( answer, "Content-type", "%s", "application/octet-stream" );
680 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
681 httpd_MsgAdd( answer, "Pragma", "no-cache" );
682 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
683 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
685 /* Check if there is a xPlayStrm=1 */
686 for( i = 0; i < query->i_name; i++ )
688 if( !strcasecmp( query->name[i], "Pragma" ) &&
689 !strcasecmp( query->value[i], "xPlayStrm=1" ) )
691 b_xplaystream = VLC_TRUE;
697 answer->i_body_offset = 0;
702 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
704 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
709 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
710 char *psz_url, char *psz_mime,
711 char *psz_user, char *psz_password )
713 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
715 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user, psz_password ) ) == NULL )
720 vlc_mutex_init( host, &stream->lock );
721 if( psz_mime && *psz_mime )
723 stream->psz_mime = strdup( psz_mime );
727 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
729 stream->i_header = 0;
730 stream->p_header = NULL;
731 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
732 stream->p_buffer = malloc( stream->i_buffer_size );
733 /* We set to 1, to make life simpler (this way i_body_offset can never be 0) */
734 stream->i_buffer_pos = 1;
735 stream->i_buffer_last_pos = 1;
737 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
738 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
739 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
744 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
746 vlc_mutex_lock( &stream->lock );
747 if( stream->p_header )
749 free( stream->p_header );
750 stream->p_header = NULL;
752 stream->i_header = i_data;
755 stream->p_header = malloc( i_data );
756 memcpy( stream->p_header, p_data, i_data );
758 vlc_mutex_unlock( &stream->lock );
763 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
768 if( i_data < 0 || p_data == NULL )
772 vlc_mutex_lock( &stream->lock );
774 /* save this pointer (to be used by new connection) */
775 stream->i_buffer_last_pos = stream->i_buffer_pos;
777 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
783 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
785 /* Ok, we can't go past the end of our buffer */
786 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
788 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
793 stream->i_buffer_pos += i_data;
795 vlc_mutex_unlock( &stream->lock );
799 void httpd_StreamDelete( httpd_stream_t *stream )
801 httpd_UrlDelete( stream->url );
802 vlc_mutex_destroy( &stream->lock );
803 if( stream->psz_mime ) free( stream->psz_mime );
804 if( stream->p_header ) free( stream->p_header );
805 if( stream->p_buffer ) free( stream->p_buffer );
810 /*****************************************************************************
812 *****************************************************************************/
813 #define LISTEN_BACKLOG 100
815 #if defined( WIN32 ) || defined( UNDER_CE )
816 #define SOCKET_CLOSE(a) closesocket(a)
818 #define SOCKET_CLOSE(a) close(a)
821 static void httpd_HostThread( httpd_host_t * );
822 static int BuildAddr( struct sockaddr_in * p_socket,
823 const char * psz_address, int i_port );
826 /* create a new host */
827 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
832 struct sockaddr_in sock;
836 if( BuildAddr( &sock, psz_host, i_port ) )
838 msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
842 /* to be sure to avoid multiple creation */
843 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
844 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
845 vlc_mutex_lock( lockval.p_address );
847 if( ( httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE ) ) == NULL )
849 msg_Info( p_this, "creating httpd" );
850 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
852 vlc_mutex_unlock( lockval.p_address );
859 vlc_object_yield( httpd );
860 vlc_object_attach( httpd, p_this->p_vlc );
863 /* verify if it already exist */
864 for( i = 0; i < httpd->i_host; i++ )
866 if( httpd->host[i]->sock.sin_port == sock.sin_port &&
867 ( httpd->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
868 httpd->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
871 host = httpd->host[i];
874 vlc_mutex_unlock( lockval.p_address );
876 msg_Dbg( p_this, "host already registered" );
880 /* create the new host */
881 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
883 vlc_mutex_init( httpd, &host->lock );
885 memcpy( &host->sock, &sock, sizeof( struct sockaddr_in ) );
891 /* create the listening socket */
892 if( ( host->fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
898 if( setsockopt( host->fd, SOL_SOCKET, SO_REUSEADDR,
899 (void *) &i, sizeof( i ) ) < 0 )
901 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
904 if( bind( host->fd, (struct sockaddr *)&host->sock, sizeof( struct sockaddr_in ) ) < 0 )
906 msg_Err( p_this, "cannot bind socket" );
909 /* set to non-blocking */
910 #if defined( WIN32 ) || defined( UNDER_CE )
912 unsigned long i_dummy = 1;
913 if( ioctlsocket( host->fd, FIONBIO, &i_dummy ) != 0 )
915 msg_Err( p_this, "cannot set socket to non-blocking mode" );
921 unsigned int i_flags;
922 if( ( i_flags = fcntl( host->fd, F_GETFL, 0 ) ) < 0 )
924 msg_Err( p_this, "cannot F_GETFL socket" );
927 if( fcntl( host->fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
929 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
935 if( listen( host->fd, LISTEN_BACKLOG ) < 0 )
937 msg_Err( p_this, "cannot listen socket" );
941 /* create the thread */
942 if( vlc_thread_create( host, "httpd host thread",
943 httpd_HostThread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
945 msg_Err( p_this, "cannot spawn http host thread" );
949 /* now add it to httpd */
950 TAB_APPEND( httpd->i_host, httpd->host, host );
951 vlc_mutex_unlock( lockval.p_address );
956 vlc_mutex_unlock( lockval.p_address );
960 SOCKET_CLOSE( host->fd );
962 vlc_mutex_destroy( &host->lock );
963 vlc_object_destroy( host );
965 /* TODO destroy no more used httpd TODO */
966 vlc_object_release( httpd );
971 void httpd_HostDelete( httpd_host_t *host )
973 httpd_t *httpd = host->httpd;
977 msg_Dbg( host, "httpd_HostDelete" );
979 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
980 vlc_mutex_lock( lockval.p_address );
982 vlc_object_release( httpd );
985 if( host->i_ref > 0 )
988 vlc_mutex_unlock( lockval.p_address );
989 msg_Dbg( host, "httpd_HostDelete: host still used" );
992 TAB_REMOVE( httpd->i_host, httpd->host, host );
994 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
997 vlc_thread_join( host );
999 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1001 for( i = 0; i < host->i_url; i++ )
1003 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1005 for( i = 0; i < host->i_client; i++ )
1007 httpd_client_t *cl = host->client[i];
1008 msg_Warn( host, "client still connected" );
1009 SOCKET_CLOSE( cl->fd );
1013 SOCKET_CLOSE( host->fd );
1014 vlc_mutex_destroy( &host->lock );
1015 vlc_object_destroy( host );
1017 if( httpd->i_host <= 0 )
1019 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1020 vlc_object_detach( httpd );
1021 vlc_object_destroy( httpd );
1023 vlc_mutex_unlock( lockval.p_address );
1026 /* register a new url */
1027 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password, vlc_bool_t b_check )
1032 vlc_mutex_lock( &host->lock );
1035 for( i = 0; i < host->i_url; i++ )
1037 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1039 msg_Warn( host->httpd,
1040 "cannot add '%s' (url already defined)", psz_url );
1041 vlc_mutex_unlock( &host->lock );
1047 url = malloc( sizeof( httpd_url_t ) );
1050 vlc_mutex_init( host->httpd, &url->lock );
1051 url->psz_url = strdup( psz_url );
1052 url->psz_user = strdup( psz_user ? psz_user : "" );
1053 url->psz_password = strdup( psz_password ? psz_password : "" );
1054 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1056 url->catch[i].cb = NULL;
1057 url->catch[i].p_sys = NULL;
1060 TAB_APPEND( host->i_url, host->url, url );
1061 vlc_mutex_unlock( &host->lock );
1066 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password )
1068 return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, VLC_FALSE );
1071 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password )
1073 return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, VLC_TRUE );
1076 /* register callback on a url */
1077 int httpd_UrlCatch( httpd_url_t *url, int i_msg,
1078 httpd_callback_t cb,
1079 httpd_callback_sys_t *p_sys )
1081 vlc_mutex_lock( &url->lock );
1082 url->catch[i_msg].cb = cb;
1083 url->catch[i_msg].p_sys= p_sys;
1084 vlc_mutex_unlock( &url->lock );
1091 void httpd_UrlDelete( httpd_url_t *url )
1093 httpd_host_t *host = url->host;
1096 vlc_mutex_lock( &host->lock );
1097 TAB_REMOVE( host->i_url, host->url, url );
1099 vlc_mutex_destroy( &url->lock );
1100 free( url->psz_url );
1101 free( url->psz_user );
1102 free( url->psz_password );
1104 for( i = 0; i < host->i_client; i++ )
1106 httpd_client_t *client = host->client[i];
1108 if( client->url == url )
1110 /* TODO complete it */
1111 msg_Warn( host, "force closing connections" );
1112 SOCKET_CLOSE( client->fd );
1113 TAB_REMOVE( host->i_client, host->client, client );
1117 vlc_mutex_unlock( &host->lock );
1120 void httpd_MsgInit( httpd_message_t *msg )
1123 msg->i_type = HTTPD_MSG_NONE;
1124 msg->i_proto = HTTPD_PROTO_NONE;
1125 msg->i_version = -1;
1128 msg->psz_status = NULL;
1130 msg->psz_url = NULL;
1131 msg->psz_args = NULL;
1133 msg->i_channel = -1;
1140 msg->i_body_offset = 0;
1145 void httpd_MsgClean( httpd_message_t *msg )
1149 if( msg->psz_status )
1151 free( msg->psz_status );
1155 free( msg->psz_url );
1159 free( msg->psz_args );
1161 for( i = 0; i < msg->i_name; i++ )
1163 free( msg->name[i] );
1164 free( msg->value[i] );
1176 free( msg->p_body );
1178 httpd_MsgInit( msg );
1181 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1185 for( i = 0; i < msg->i_name; i++ )
1187 if( !strcasecmp( msg->name[i], name ))
1189 return msg->value[i];
1194 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1199 va_start( args, psz_value );
1200 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1201 vasprintf( &value, psz_value, args );
1204 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1205 value = calloc( i_size, sizeof( char ) );
1206 vsnprintf( value, i_size, psz_value, args );
1207 value[i_size - 1] = 0;
1212 name = strdup( name );
1214 TAB_APPEND( msg->i_name, msg->name, name );
1215 TAB_APPEND( msg->i_value, msg->value, value );
1218 static void httpd_ClientInit( httpd_client_t *cl )
1220 cl->i_state = HTTPD_CLIENT_RECEIVING;
1221 cl->i_activity_date = mdate();
1222 cl->i_activity_timeout = 1500000000LL;
1223 cl->i_buffer_size = 10000;
1225 cl->p_buffer = malloc( cl->i_buffer_size );
1226 cl->i_mode = HTTPD_CLIENT_FILE;
1227 cl->b_read_waiting = VLC_FALSE;
1229 httpd_MsgInit( &cl->query );
1230 httpd_MsgInit( &cl->answer );
1232 void httpd_ClientModeStream( httpd_client_t *cl )
1234 cl->i_mode = HTTPD_CLIENT_STREAM;
1236 void httpd_ClientModeBidir( httpd_client_t *cl )
1238 cl->i_mode = HTTPD_CLIENT_BIDIR;
1241 static void httpd_ClientClean( httpd_client_t *cl )
1245 SOCKET_CLOSE( cl->fd );
1248 httpd_MsgClean( &cl->answer );
1249 httpd_MsgClean( &cl->query );
1253 free( cl->p_buffer );
1257 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_in *sock )
1259 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1260 /* set this new socket non-block */
1261 #if defined( WIN32 ) || defined( UNDER_CE )
1263 unsigned long i_dummy = 1;
1264 ioctlsocket( fd, FIONBIO, &i_dummy );
1267 fcntl( fd, F_SETFL, O_NONBLOCK );
1274 httpd_ClientInit( cl );
1279 static void httpd_ClientRecv( httpd_client_t *cl )
1283 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1285 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1286 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 4 - cl->i_buffer, 0 );
1290 cl->i_buffer += i_len;
1293 if( cl->i_buffer >= 4 )
1295 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1297 if( cl->p_buffer[0] == '$' )
1299 /* RTSP (rtp over rtsp) */
1300 cl->query.i_proto = HTTPD_PROTO_RTSP;
1301 cl->query.i_type = HTTPD_MSG_CHANNEL;
1302 cl->query.i_channel = cl->p_buffer[1];
1303 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1304 cl->query.p_body = malloc( cl->query.i_body );
1308 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1310 cl->query.i_proto = HTTPD_PROTO_HTTP;
1311 cl->query.i_type = HTTPD_MSG_ANSWER;
1313 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1315 cl->query.i_proto = HTTPD_PROTO_RTSP;
1316 cl->query.i_type = HTTPD_MSG_ANSWER;
1318 else if( !strncmp( cl->p_buffer, "GET", 3 ) || !strncmp( cl->p_buffer, "HEAD", 4 ) || !strncmp( cl->p_buffer, "POST", 4 ) )
1320 cl->query.i_proto = HTTPD_PROTO_HTTP;
1321 cl->query.i_type = HTTPD_MSG_NONE;
1325 cl->query.i_proto = HTTPD_PROTO_RTSP;
1326 cl->query.i_type = HTTPD_MSG_NONE;
1330 else if( cl->query.i_body > 0 )
1332 /* we are reading the body of a request or a channel */
1333 i_len = recv( cl->fd, &cl->query.p_body[cl->i_buffer], cl->query.i_body - cl->i_buffer, 0 );
1336 cl->i_buffer += i_len;
1338 if( cl->i_buffer >= cl->query.i_body )
1340 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1345 /* we are reading a header -> char by char */
1348 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 1, 0 );
1355 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1357 cl->i_buffer_size += 1024;
1358 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1360 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1361 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1365 /* we have finished the header so parse it and set i_body */
1366 cl->p_buffer[cl->i_buffer] = '\0';
1368 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1370 cl->query.i_status = strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1375 cl->query.psz_status = strdup( p );
1387 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1388 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1389 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1391 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1392 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1393 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1394 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1395 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1396 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1398 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1403 cl->query.i_type = HTTPD_MSG_NONE;
1405 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1407 for( i = 0; msg_type[i].name != NULL; i++ )
1409 if( !strncmp( cl->p_buffer, msg_type[i].name, strlen( msg_type[i].name ) ) )
1411 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1412 cl->query.i_type = msg_type[i].i_type;
1413 if( cl->query.i_proto != msg_type[i].i_proto )
1416 cl->query.i_proto = HTTPD_PROTO_NONE;
1417 cl->query.i_type = HTTPD_MSG_NONE;
1424 if( strstr( cl->p_buffer, "HTTP/1." ) )
1426 cl->query.i_proto = HTTPD_PROTO_HTTP;
1428 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1430 cl->query.i_proto = HTTPD_PROTO_RTSP;
1442 p2 = strchr( p, ' ' );
1447 if( !strncasecmp( p, "rtsp:", 5 ) )
1449 /* for rtsp url, you have rtsp://localhost:port/path */
1451 while( *p == '/' ) p++;
1452 while( *p && *p != '/' ) p++;
1454 cl->query.psz_url = strdup( p );
1455 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1458 cl->query.psz_args = strdup( p3 );
1466 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1468 cl->query.i_proto = HTTPD_PROTO_HTTP;
1469 cl->query.i_version = atoi( p2+7 );
1471 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1473 cl->query.i_proto = HTTPD_PROTO_RTSP;
1474 cl->query.i_version = atoi( p2+7 );
1482 p = strchr( p, '\n' );
1486 while( *p == '\n' || *p == '\r' )
1490 while( p && *p != '\0' )
1493 char *eol = p = strchr( p, '\n' );
1496 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1501 if( ( colon = strchr( line, ':' ) ) )
1507 while( *colon == ' ' )
1511 name = strdup( line );
1512 value = strdup( colon );
1514 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1515 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1517 if( !strcasecmp( name, "Content-Length" ) )
1519 cl->query.i_body = atol( value );
1526 while( *p == '\n' || *p == '\r' )
1533 if( cl->query.i_body > 0 )
1535 /* TODO Mhh, handle the case client will only send a request and close the connection
1536 * to mark and of body (probably only RTSP) */
1537 cl->query.p_body = malloc( cl->query.i_body );
1542 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1548 /* check if the client is to be set to dead */
1549 #if defined( WIN32 ) || defined( UNDER_CE )
1550 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1552 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1555 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1557 /* connection closed -> end of data */
1558 if( cl->query.i_body > 0 )
1560 cl->query.i_body = cl->i_buffer;
1562 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1566 cl->i_state = HTTPD_CLIENT_DEAD;
1569 cl->i_activity_date = mdate();
1571 /* Debugging only */
1572 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1576 fprintf( stderr, "received new request\n" );
1577 fprintf( stderr, " - proto=%s\n", cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1578 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1579 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1580 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1582 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status, cl->query.psz_status );
1584 else if( cl->query.i_type != HTTPD_MSG_NONE )
1586 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1588 for( i = 0; i < cl->query.i_name; i++ )
1590 fprintf( stderr, " - option name='%s' value='%s'\n", cl->query.name[i], cl->query.value[i] );
1595 static void httpd_ClientSend( httpd_client_t *cl )
1600 if( cl->i_buffer < 0 )
1602 /* We need to create the header */
1606 i_size = strlen( "HTTP/1.") + 10 + 10 +
1607 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1608 for( i = 0; i < cl->answer.i_name; i++ )
1610 i_size += strlen( cl->answer.name[i] ) + 2 + strlen( cl->answer.value[i] ) + 2;
1613 if( cl->i_buffer_size < i_size )
1615 cl->i_buffer_size = i_size;
1616 free( cl->p_buffer );
1617 cl->p_buffer = malloc( i_size );
1621 p += sprintf( p, "%s/1.%d %d %s\r\n",
1622 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1623 cl->answer.i_version,
1624 cl->answer.i_status, cl->answer.psz_status );
1625 for( i = 0; i < cl->answer.i_name; i++ )
1627 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i], cl->answer.value[i] );
1629 p += sprintf( p, "\r\n" );
1632 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1634 fprintf( stderr, "sending answer\n" );
1635 fprintf( stderr, "%s", cl->p_buffer );
1638 i_len = send( cl->fd, &cl->p_buffer[cl->i_buffer], cl->i_buffer_size - cl->i_buffer, 0 );
1641 cl->i_activity_date = mdate();
1642 cl->i_buffer += i_len;
1644 if( cl->i_buffer >= cl->i_buffer_size )
1646 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 && !cl->b_read_waiting )
1648 /* catch more body data */
1649 int i_msg = cl->query.i_type;
1650 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1651 &cl->answer, &cl->query );
1654 if( cl->answer.i_body > 0 )
1656 /* send the body data */
1657 free( cl->p_buffer );
1658 cl->p_buffer = cl->answer.p_body;
1659 cl->i_buffer_size = cl->answer.i_body;
1662 cl->answer.i_body = 0;
1663 cl->answer.p_body = NULL;
1668 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1674 #if defined( WIN32 ) || defined( UNDER_CE )
1675 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1677 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1681 cl->i_state = HTTPD_CLIENT_DEAD;
1686 static void httpd_HostThread( httpd_host_t *host )
1688 while( !host->b_die )
1690 struct timeval timeout;
1693 int i_handle_max = 0;
1696 int b_low_delay = 0;
1698 if( host->i_url <= 0 )
1705 /* built a set of handle to select */
1706 FD_ZERO( &fds_read );
1707 FD_ZERO( &fds_write );
1709 FD_SET( host->fd, &fds_read );
1710 i_handle_max = host->fd;
1712 /* add all socket that should be read/write and close dead connection */
1713 vlc_mutex_lock( &host->lock );
1714 for( i_client = 0; i_client < host->i_client; i_client++ )
1716 httpd_client_t *cl = host->client[i_client];
1718 if( cl->i_ref < 0 ||
1720 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1721 cl->i_activity_date + cl->i_activity_timeout < mdate() ) ) )
1723 msg_Dbg( host, "connection closed(%s)", inet_ntoa(cl->sock.sin_addr) );
1725 httpd_ClientClean( cl );
1727 TAB_REMOVE( host->i_client, host->client, cl );
1730 else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
1732 FD_SET( cl->fd, &fds_read );
1733 i_handle_max = __MAX( i_handle_max, cl->fd );
1735 else if( cl->i_state == HTTPD_CLIENT_SENDING )
1737 FD_SET( cl->fd, &fds_write );
1738 i_handle_max = __MAX( i_handle_max, cl->fd );
1740 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1742 httpd_message_t *answer = &cl->answer;
1743 httpd_message_t *query = &cl->query;
1744 int i_msg = query->i_type;
1746 httpd_MsgInit( answer );
1748 /* Handle what we received */
1749 if( cl->i_mode != HTTPD_CLIENT_BIDIR && ( i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL ) )
1751 /* we can only receive request from client when not in BIDIR mode */
1753 cl->i_state = HTTPD_CLIENT_DEAD;
1755 else if( i_msg == HTTPD_MSG_ANSWER )
1757 /* We are in BIDIR mode, trigger the callback and then check for new data */
1758 if( cl->url && cl->url->catch[i_msg].cb )
1760 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, NULL, query );
1762 cl->i_state = HTTPD_CLIENT_WAITING;
1764 else if( i_msg == HTTPD_MSG_CHANNEL )
1766 /* We are in BIDIR mode, trigger the callback and then check for new data */
1767 if( cl->url && cl->url->catch[i_msg].cb )
1769 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, NULL, query );
1771 cl->i_state = HTTPD_CLIENT_WAITING;
1773 else if( i_msg == HTTPD_MSG_OPTIONS )
1778 answer->i_proto = query->i_proto ;
1779 answer->i_type = HTTPD_MSG_ANSWER;
1780 answer->i_version= 0;
1781 answer->i_status = 200;
1782 answer->psz_status = strdup( "Ok" );
1785 answer->p_body = NULL;
1787 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
1788 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1789 httpd_MsgAdd( answer, "Server", "VLC Server" );
1790 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE" );
1791 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1793 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1794 cl->i_state = HTTPD_CLIENT_SENDING;
1796 else if( i_msg == HTTPD_MSG_NONE )
1798 if( query->i_proto == HTTPD_PROTO_NONE )
1801 cl->i_state = HTTPD_CLIENT_DEAD;
1808 answer->i_proto = query->i_proto ;
1809 answer->i_type = HTTPD_MSG_ANSWER;
1810 answer->i_version= 0;
1811 answer->i_status = 501;
1812 answer->psz_status = strdup( "Unimplemented" );
1814 p = answer->p_body = malloc( 1000 );
1816 p += sprintf( p, "<html>\n" );
1817 p += sprintf( p, "<head>\n" );
1818 p += sprintf( p, "<title>Error 501</title>\n" );
1819 p += sprintf( p, "</head>\n" );
1820 p += sprintf( p, "<body>\n" );
1821 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
1822 p += sprintf( p, "<hr />\n" );
1823 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1824 p += sprintf( p, "</body>\n" );
1825 p += sprintf( p, "</html>\n" );
1827 answer->i_body = p - answer->p_body;
1828 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1830 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1831 cl->i_state = HTTPD_CLIENT_SENDING;
1836 vlc_bool_t b_auth_failed = VLC_FALSE;
1839 /* Search the url and trigger callbacks */
1840 for( i = 0; i < host->i_url; i++ )
1842 httpd_url_t *url = host->url[i];
1844 if( !strcmp( url->psz_url, query->psz_url ) )
1846 if( url->catch[i_msg].cb )
1848 if( answer && ( *url->psz_user || *url->psz_password ) )
1850 /* create the headers */
1851 char id[strlen(url->psz_user)+strlen(url->psz_password) + 2];
1852 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
1853 char auth[strlen(b64) +1];
1855 sprintf( id, "%s:%s", url->psz_user, url->psz_password );
1856 if( !strncasecmp( b64, "BASIC", 5 ) )
1859 while( *b64 == ' ' )
1863 b64_decode( auth, b64 );
1869 if( strcmp( id, auth ) )
1871 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
1872 /* We fail for all url */
1873 b_auth_failed = VLC_TRUE;
1878 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
1880 /* only one url can answer */
1882 if( cl->url == NULL )
1894 answer->i_proto = query->i_proto;
1895 answer->i_type = HTTPD_MSG_ANSWER;
1896 answer->i_version= 0;
1897 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
1901 answer->i_status = 401;
1902 answer->psz_status = strdup( "Authorization Required" );
1904 p += sprintf( p, "<html>\n" );
1905 p += sprintf( p, "<head>\n" );
1906 p += sprintf( p, "<title>Error 401</title>\n" );
1907 p += sprintf( p, "</head>\n" );
1908 p += sprintf( p, "<body>\n" );
1909 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
1910 p += sprintf( p, "<hr />\n" );
1911 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1912 p += sprintf( p, "</body>\n" );
1913 p += sprintf( p, "</html>\n" );
1917 /* no url registered */
1918 answer->i_status = 404;
1919 answer->psz_status = strdup( "Not found" );
1921 p += sprintf( p, "<html>\n" );
1922 p += sprintf( p, "<head>\n" );
1923 p += sprintf( p, "<title>Error 404</title>\n" );
1924 p += sprintf( p, "</head>\n" );
1925 p += sprintf( p, "<body>\n" );
1926 p += sprintf( p, "<h1><center> 404 Ressource not found(%s)</center></h1>\n", query->psz_url );
1927 p += sprintf( p, "<hr />\n" );
1928 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1929 p += sprintf( p, "</body>\n" );
1930 p += sprintf( p, "</html>\n" );
1933 answer->i_body = p - answer->p_body;
1934 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1936 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1937 cl->i_state = HTTPD_CLIENT_SENDING;
1940 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
1942 if( cl->i_mode == HTTPD_CLIENT_FILE )
1945 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
1946 ( !strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Keep-Alive" )||
1947 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) ) ) ) ||
1948 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
1949 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
1950 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
1952 httpd_MsgClean( &cl->query );
1953 httpd_MsgInit( &cl->query );
1956 cl->i_buffer_size = 1000;
1957 free( cl->p_buffer );
1958 cl->p_buffer = malloc( cl->i_buffer_size );
1959 cl->i_state = HTTPD_CLIENT_RECEIVING;
1963 cl->i_state = HTTPD_CLIENT_DEAD;
1965 httpd_MsgClean( &cl->answer );
1967 else if( cl->b_read_waiting )
1969 /* we have a message waiting for us to read it */
1970 httpd_MsgClean( &cl->answer );
1971 httpd_MsgClean( &cl->query );
1974 cl->i_buffer_size = 1000;
1975 free( cl->p_buffer );
1976 cl->p_buffer = malloc( cl->i_buffer_size );
1977 cl->i_state = HTTPD_CLIENT_RECEIVING;
1978 cl->b_read_waiting = VLC_FALSE;
1982 int64_t i_offset = cl->answer.i_body_offset;
1983 httpd_MsgClean( &cl->answer );
1985 cl->answer.i_body_offset = i_offset;
1986 cl->i_state = HTTPD_CLIENT_WAITING;
1989 else if( cl->i_state == HTTPD_CLIENT_WAITING )
1991 int64_t i_offset = cl->answer.i_body_offset;
1992 int i_msg = cl->query.i_type;
1994 httpd_MsgInit( &cl->answer );
1995 cl->answer.i_body_offset = i_offset;
1997 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, &cl->answer, &cl->query );
1998 if( cl->answer.i_type != HTTPD_MSG_NONE )
2000 /* we have new data, so reenter send mode */
2002 cl->p_buffer = cl->answer.p_body;
2003 cl->i_buffer_size = cl->answer.i_body;
2004 cl->answer.p_body = NULL;
2005 cl->answer.i_body = 0;
2006 cl->i_state = HTTPD_CLIENT_SENDING;
2010 /* we shouldn't wait too long */
2011 b_low_delay = VLC_TRUE;
2015 /* Special for BIDIR mode we also check reading */
2016 if( cl->i_mode == HTTPD_CLIENT_BIDIR && cl->i_state == HTTPD_CLIENT_SENDING )
2018 FD_SET( cl->fd, &fds_read );
2019 i_handle_max = __MAX( i_handle_max, cl->fd );
2022 vlc_mutex_unlock( &host->lock );
2024 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2026 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2028 i_ret = select( i_handle_max + 1,
2029 &fds_read, &fds_write, NULL, &timeout );
2031 if( i_ret == -1 && errno != EINTR )
2033 msg_Warn( host, "cannot select sockets" );
2037 else if( i_ret <= 0 )
2042 /* accept new connections */
2043 if( FD_ISSET( host->fd, &fds_read ) )
2045 int i_sock_size = sizeof( struct sockaddr_in );
2046 struct sockaddr_in sock;
2049 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2052 httpd_client_t *cl = httpd_ClientNew( fd, &sock );
2054 vlc_mutex_lock( &host->lock );
2055 TAB_APPEND( host->i_client, host->client, cl );
2056 vlc_mutex_unlock( &host->lock );
2058 msg_Dbg( host, "new connection (%s)", inet_ntoa(sock.sin_addr) );
2061 /* now try all others socket */
2062 vlc_mutex_lock( &host->lock );
2063 for( i_client = 0; i_client < host->i_client; i_client++ )
2065 httpd_client_t *cl = host->client[i_client];
2066 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2068 httpd_ClientRecv( cl );
2070 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2072 httpd_ClientSend( cl );
2075 if( cl->i_mode == HTTPD_CLIENT_BIDIR && cl->i_state == HTTPD_CLIENT_SENDING &&
2076 FD_ISSET( cl->fd, &fds_read ) )
2078 cl->b_read_waiting = VLC_TRUE;
2081 vlc_mutex_unlock( &host->lock );
2089 static int BuildAddr( struct sockaddr_in * p_socket,
2090 const char * psz_address, int i_port )
2093 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2094 p_socket->sin_family = AF_INET; /* family */
2095 p_socket->sin_port = htons( (uint16_t)i_port );
2098 p_socket->sin_addr.s_addr = INADDR_ANY;
2102 struct hostent * p_hostent;
2104 /* Try to convert address directly from in_addr - this will work if
2105 * psz_address is dotted decimal. */
2106 #ifdef HAVE_ARPA_INET_H
2107 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2109 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2110 if( p_socket->sin_addr.s_addr == INADDR_NONE )
2113 /* We have a fqdn, try to find its address */
2114 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2119 /* Copy the first address of the host in the socket address */
2120 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
2121 p_hostent->h_length );