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 );
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 /* We respect client request */
453 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
455 httpd_MsgAdd( answer, "Connection", httpd_MsgGet( &cl->query, "Connection" ) );
458 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
464 httpd_file_t *httpd_FileNew( httpd_host_t *host,
465 char *psz_url, char *psz_mime,
466 char *psz_user, char *psz_password,
467 httpd_file_callback_t pf_fill,
468 httpd_file_sys_t *p_sys )
470 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
472 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user, psz_password ) ) == NULL )
478 file->psz_url = strdup( psz_url );
479 if( psz_mime && *psz_mime )
481 file->psz_mime = strdup( psz_mime );
485 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
488 file->pf_fill = pf_fill;
491 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack, (httpd_callback_sys_t*)file );
492 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack, (httpd_callback_sys_t*)file );
493 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack, (httpd_callback_sys_t*)file );
498 void httpd_FileDelete( httpd_file_t *file )
500 httpd_UrlDelete( file->url );
502 free( file->psz_url );
503 free( file->psz_mime );
508 /*****************************************************************************
509 * High Level Funtions: httpd_redirect_t
510 *****************************************************************************/
511 struct httpd_redirect_t
517 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
519 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
522 if( answer == NULL || query == NULL )
526 answer->i_proto = query->i_proto;
527 answer->i_version= query->i_version;
528 answer->i_type = HTTPD_MSG_ANSWER;
529 answer->i_status = 301;
530 answer->psz_status = strdup( "Moved Permanently" );
532 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
533 p += sprintf( p, "<html>\n" );
534 p += sprintf( p, "<head>\n" );
535 p += sprintf( p, "<title>Redirection</title>\n" );
536 p += sprintf( p, "</head>\n" );
537 p += sprintf( p, "<body>\n" );
538 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
539 p += sprintf( p, "<hr />\n" );
540 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
541 p += sprintf( p, "</body>\n" );
542 p += sprintf( p, "</html>\n" );
543 answer->i_body = p - answer->p_body;
545 /* XXX check if it's ok or we need to set an absolute url */
546 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
548 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
553 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst, char *psz_url_src )
555 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
557 if( ( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) == NULL )
562 rdir->psz_dst = strdup( psz_url_dst );
563 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
564 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
565 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
566 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
568 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
572 void httpd_RedirectDelete( httpd_redirect_t *rdir )
574 httpd_UrlDelete( rdir->url );
575 free( rdir->psz_dst );
579 /*****************************************************************************
580 * High Level Funtions: httpd_stream_t
581 *****************************************************************************/
582 struct httpd_stream_t
589 /* Header to send as first packet */
593 /* circular buffer */
594 int i_buffer_size; /* buffer size, can't be reallocated smaller */
595 uint8_t *p_buffer; /* buffer */
596 int64_t i_buffer_pos; /* absolute position from begining */
597 int64_t i_buffer_last_pos; /* a new connection will start with that */
600 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
602 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
604 if( answer == NULL || query == NULL || cl == NULL )
608 if( answer->i_body_offset > 0 )
613 /* fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n", answer->i_body_offset ); */
615 if( answer->i_body_offset >= stream->i_buffer_pos )
617 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
618 return VLC_EGENERIC; /* wait, no data available */
620 if( answer->i_body_offset + stream->i_buffer_size < stream->i_buffer_pos )
622 /* this client isn't fast enough */
623 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
624 answer->i_body_offset, stream->i_buffer_last_pos );
625 answer->i_body_offset = stream->i_buffer_last_pos;
628 i_pos = answer->i_body_offset % stream->i_buffer_size;
629 i_write = stream->i_buffer_pos - answer->i_body_offset;
630 if( i_write > 10000 )
634 else if( i_write <= 0 )
636 return VLC_EGENERIC; /* wait, no data available */
639 /* using HTTPD_MSG_ANSWER -> data available */
640 answer->i_proto = HTTPD_PROTO_HTTP;
641 answer->i_version= 0;
642 answer->i_type = HTTPD_MSG_ANSWER;
644 answer->i_body = i_write;
645 answer->p_body = malloc( i_write );
646 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
648 answer->i_body_offset += i_write;
654 answer->i_proto = HTTPD_PROTO_HTTP;
655 answer->i_version= 0;
656 answer->i_type = HTTPD_MSG_ANSWER;
658 answer->i_status = 200;
659 answer->psz_status = strdup( "OK" );
661 if( query->i_type != HTTPD_MSG_HEAD )
663 httpd_ClientModeStream( cl );
664 vlc_mutex_lock( &stream->lock );
665 /* Send the header */
666 if( stream->i_header > 0 )
668 answer->i_body = stream->i_header;
669 answer->p_body = malloc( stream->i_header );
670 memcpy( answer->p_body, stream->p_header, stream->i_header );
672 answer->i_body_offset = stream->i_buffer_last_pos;
673 vlc_mutex_unlock( &stream->lock );
677 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
680 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
682 vlc_bool_t b_xplaystream = VLC_FALSE;
685 httpd_MsgAdd( answer, "Content-type", "%s", "application/octet-stream" );
686 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
687 httpd_MsgAdd( answer, "Pragma", "no-cache" );
688 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
689 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
691 /* Check if there is a xPlayStrm=1 */
692 for( i = 0; i < query->i_name; i++ )
694 if( !strcasecmp( query->name[i], "Pragma" ) &&
695 !strcasecmp( query->value[i], "xPlayStrm=1" ) )
697 b_xplaystream = VLC_TRUE;
703 answer->i_body_offset = 0;
708 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
710 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
715 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
716 char *psz_url, char *psz_mime,
717 char *psz_user, char *psz_password )
719 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
721 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user, psz_password ) ) == NULL )
726 vlc_mutex_init( host, &stream->lock );
727 if( psz_mime && *psz_mime )
729 stream->psz_mime = strdup( psz_mime );
733 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
735 stream->i_header = 0;
736 stream->p_header = NULL;
737 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
738 stream->p_buffer = malloc( stream->i_buffer_size );
739 /* We set to 1, to make life simpler (this way i_body_offset can never be 0) */
740 stream->i_buffer_pos = 1;
741 stream->i_buffer_last_pos = 1;
743 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
744 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
745 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
750 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
752 vlc_mutex_lock( &stream->lock );
753 if( stream->p_header )
755 free( stream->p_header );
756 stream->p_header = NULL;
758 stream->i_header = i_data;
761 stream->p_header = malloc( i_data );
762 memcpy( stream->p_header, p_data, i_data );
764 vlc_mutex_unlock( &stream->lock );
769 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
774 if( i_data < 0 || p_data == NULL )
778 vlc_mutex_lock( &stream->lock );
780 /* save this pointer (to be used by new connection) */
781 stream->i_buffer_last_pos = stream->i_buffer_pos;
783 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
789 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
791 /* Ok, we can't go past the end of our buffer */
792 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
794 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
799 stream->i_buffer_pos += i_data;
801 vlc_mutex_unlock( &stream->lock );
805 void httpd_StreamDelete( httpd_stream_t *stream )
807 httpd_UrlDelete( stream->url );
808 vlc_mutex_destroy( &stream->lock );
809 if( stream->psz_mime ) free( stream->psz_mime );
810 if( stream->p_header ) free( stream->p_header );
811 if( stream->p_buffer ) free( stream->p_buffer );
816 /*****************************************************************************
818 *****************************************************************************/
819 #define LISTEN_BACKLOG 100
821 #if defined( WIN32 ) || defined( UNDER_CE )
822 #define SOCKET_CLOSE(a) closesocket(a)
824 #define SOCKET_CLOSE(a) close(a)
827 static void httpd_HostThread( httpd_host_t * );
828 static int BuildAddr( struct sockaddr_in * p_socket,
829 const char * psz_address, int i_port );
832 /* create a new host */
833 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
838 struct sockaddr_in sock;
842 if( BuildAddr( &sock, psz_host, i_port ) )
844 msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
848 /* to be sure to avoid multiple creation */
849 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
850 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
851 vlc_mutex_lock( lockval.p_address );
853 if( ( httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE ) ) == NULL )
855 msg_Info( p_this, "creating httpd" );
856 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
858 vlc_mutex_unlock( lockval.p_address );
865 vlc_object_yield( httpd );
866 vlc_object_attach( httpd, p_this->p_vlc );
869 /* verify if it already exist */
870 for( i = 0; i < httpd->i_host; i++ )
872 if( httpd->host[i]->sock.sin_port == sock.sin_port &&
873 ( httpd->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
874 httpd->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
877 host = httpd->host[i];
880 vlc_mutex_unlock( lockval.p_address );
882 msg_Dbg( p_this, "host already registered" );
886 /* create the new host */
887 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
889 vlc_mutex_init( httpd, &host->lock );
891 memcpy( &host->sock, &sock, sizeof( struct sockaddr_in ) );
897 /* create the listening socket */
898 if( ( host->fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
904 if( setsockopt( host->fd, SOL_SOCKET, SO_REUSEADDR,
905 (void *) &i, sizeof( i ) ) < 0 )
907 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
910 if( bind( host->fd, (struct sockaddr *)&host->sock, sizeof( struct sockaddr_in ) ) < 0 )
912 msg_Err( p_this, "cannot bind socket" );
915 /* set to non-blocking */
916 #if defined( WIN32 ) || defined( UNDER_CE )
918 unsigned long i_dummy = 1;
919 if( ioctlsocket( host->fd, FIONBIO, &i_dummy ) != 0 )
921 msg_Err( p_this, "cannot set socket to non-blocking mode" );
927 unsigned int i_flags;
928 if( ( i_flags = fcntl( host->fd, F_GETFL, 0 ) ) < 0 )
930 msg_Err( p_this, "cannot F_GETFL socket" );
933 if( fcntl( host->fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
935 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
941 if( listen( host->fd, LISTEN_BACKLOG ) < 0 )
943 msg_Err( p_this, "cannot listen socket" );
947 /* create the thread */
948 if( vlc_thread_create( host, "httpd host thread",
949 httpd_HostThread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
951 msg_Err( p_this, "cannot spawn http host thread" );
955 /* now add it to httpd */
956 TAB_APPEND( httpd->i_host, httpd->host, host );
957 vlc_mutex_unlock( lockval.p_address );
962 vlc_mutex_unlock( lockval.p_address );
966 SOCKET_CLOSE( host->fd );
968 vlc_mutex_destroy( &host->lock );
969 vlc_object_destroy( host );
971 /* TODO destroy no more used httpd TODO */
972 vlc_object_release( httpd );
977 void httpd_HostDelete( httpd_host_t *host )
979 httpd_t *httpd = host->httpd;
983 msg_Dbg( host, "httpd_HostDelete" );
985 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
986 vlc_mutex_lock( lockval.p_address );
988 vlc_object_release( httpd );
991 if( host->i_ref > 0 )
994 vlc_mutex_unlock( lockval.p_address );
995 msg_Dbg( host, "httpd_HostDelete: host still used" );
998 TAB_REMOVE( httpd->i_host, httpd->host, host );
1000 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1003 vlc_thread_join( host );
1005 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1007 for( i = 0; i < host->i_url; i++ )
1009 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1011 for( i = 0; i < host->i_client; i++ )
1013 httpd_client_t *cl = host->client[i];
1014 msg_Warn( host, "client still connected" );
1015 SOCKET_CLOSE( cl->fd );
1019 SOCKET_CLOSE( host->fd );
1020 vlc_mutex_destroy( &host->lock );
1021 vlc_object_destroy( host );
1023 if( httpd->i_host <= 0 )
1025 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1026 vlc_object_detach( httpd );
1027 vlc_object_destroy( httpd );
1029 vlc_mutex_unlock( lockval.p_address );
1032 /* register a new url */
1033 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password, vlc_bool_t b_check )
1038 vlc_mutex_lock( &host->lock );
1041 for( i = 0; i < host->i_url; i++ )
1043 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1045 msg_Warn( host->httpd,
1046 "cannot add '%s' (url already defined)", psz_url );
1047 vlc_mutex_unlock( &host->lock );
1053 url = malloc( sizeof( httpd_url_t ) );
1056 vlc_mutex_init( host->httpd, &url->lock );
1057 url->psz_url = strdup( psz_url );
1058 url->psz_user = strdup( psz_user ? psz_user : "" );
1059 url->psz_password = strdup( psz_password ? psz_password : "" );
1060 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1062 url->catch[i].cb = NULL;
1063 url->catch[i].p_sys = NULL;
1066 TAB_APPEND( host->i_url, host->url, url );
1067 vlc_mutex_unlock( &host->lock );
1072 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password )
1074 return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, VLC_FALSE );
1077 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password )
1079 return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, VLC_TRUE );
1082 /* register callback on a url */
1083 int httpd_UrlCatch( httpd_url_t *url, int i_msg,
1084 httpd_callback_t cb,
1085 httpd_callback_sys_t *p_sys )
1087 vlc_mutex_lock( &url->lock );
1088 url->catch[i_msg].cb = cb;
1089 url->catch[i_msg].p_sys= p_sys;
1090 vlc_mutex_unlock( &url->lock );
1097 void httpd_UrlDelete( httpd_url_t *url )
1099 httpd_host_t *host = url->host;
1102 vlc_mutex_lock( &host->lock );
1103 TAB_REMOVE( host->i_url, host->url, url );
1105 vlc_mutex_destroy( &url->lock );
1106 free( url->psz_url );
1107 free( url->psz_user );
1108 free( url->psz_password );
1110 for( i = 0; i < host->i_client; i++ )
1112 httpd_client_t *client = host->client[i];
1114 if( client->url == url )
1116 /* TODO complete it */
1117 msg_Warn( host, "force closing connections" );
1118 SOCKET_CLOSE( client->fd );
1119 TAB_REMOVE( host->i_client, host->client, client );
1123 vlc_mutex_unlock( &host->lock );
1126 void httpd_MsgInit( httpd_message_t *msg )
1129 msg->i_type = HTTPD_MSG_NONE;
1130 msg->i_proto = HTTPD_PROTO_NONE;
1131 msg->i_version = -1;
1134 msg->psz_status = NULL;
1136 msg->psz_url = NULL;
1137 msg->psz_args = NULL;
1139 msg->i_channel = -1;
1146 msg->i_body_offset = 0;
1151 void httpd_MsgClean( httpd_message_t *msg )
1155 if( msg->psz_status )
1157 free( msg->psz_status );
1161 free( msg->psz_url );
1165 free( msg->psz_args );
1167 for( i = 0; i < msg->i_name; i++ )
1169 free( msg->name[i] );
1170 free( msg->value[i] );
1182 free( msg->p_body );
1184 httpd_MsgInit( msg );
1187 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1191 for( i = 0; i < msg->i_name; i++ )
1193 if( !strcasecmp( msg->name[i], name ))
1195 return msg->value[i];
1200 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1205 va_start( args, psz_value );
1206 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1207 vasprintf( &value, psz_value, args );
1210 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1211 value = calloc( i_size, sizeof( char ) );
1212 vsnprintf( value, i_size, psz_value, args );
1213 value[i_size - 1] = 0;
1218 name = strdup( name );
1220 TAB_APPEND( msg->i_name, msg->name, name );
1221 TAB_APPEND( msg->i_value, msg->value, value );
1224 static void httpd_ClientInit( httpd_client_t *cl )
1226 cl->i_state = HTTPD_CLIENT_RECEIVING;
1227 cl->i_activity_date = mdate();
1228 cl->i_activity_timeout = 10000000LL;
1229 cl->i_buffer_size = 10000;
1231 cl->p_buffer = malloc( cl->i_buffer_size );
1232 cl->i_mode = HTTPD_CLIENT_FILE;
1233 cl->b_read_waiting = VLC_FALSE;
1235 httpd_MsgInit( &cl->query );
1236 httpd_MsgInit( &cl->answer );
1238 void httpd_ClientModeStream( httpd_client_t *cl )
1240 cl->i_mode = HTTPD_CLIENT_STREAM;
1242 void httpd_ClientModeBidir( httpd_client_t *cl )
1244 cl->i_mode = HTTPD_CLIENT_BIDIR;
1247 static void httpd_ClientClean( httpd_client_t *cl )
1251 SOCKET_CLOSE( cl->fd );
1254 httpd_MsgClean( &cl->answer );
1255 httpd_MsgClean( &cl->query );
1259 free( cl->p_buffer );
1263 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_in *sock )
1265 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1266 /* set this new socket non-block */
1267 #if defined( WIN32 ) || defined( UNDER_CE )
1269 unsigned long i_dummy = 1;
1270 ioctlsocket( fd, FIONBIO, &i_dummy );
1273 fcntl( fd, F_SETFL, O_NONBLOCK );
1280 httpd_ClientInit( cl );
1285 static void httpd_ClientRecv( httpd_client_t *cl )
1289 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1291 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1292 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 4 - cl->i_buffer, 0 );
1296 cl->i_buffer += i_len;
1299 if( cl->i_buffer >= 4 )
1301 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1303 if( cl->p_buffer[0] == '$' )
1305 /* RTSP (rtp over rtsp) */
1306 cl->query.i_proto = HTTPD_PROTO_RTSP;
1307 cl->query.i_type = HTTPD_MSG_CHANNEL;
1308 cl->query.i_channel = cl->p_buffer[1];
1309 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1310 cl->query.p_body = malloc( cl->query.i_body );
1314 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1316 cl->query.i_proto = HTTPD_PROTO_HTTP;
1317 cl->query.i_type = HTTPD_MSG_ANSWER;
1319 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1321 cl->query.i_proto = HTTPD_PROTO_RTSP;
1322 cl->query.i_type = HTTPD_MSG_ANSWER;
1324 else if( !strncmp( cl->p_buffer, "GET", 3 ) || !strncmp( cl->p_buffer, "HEAD", 4 ) || !strncmp( cl->p_buffer, "POST", 4 ) )
1326 cl->query.i_proto = HTTPD_PROTO_HTTP;
1327 cl->query.i_type = HTTPD_MSG_NONE;
1331 cl->query.i_proto = HTTPD_PROTO_RTSP;
1332 cl->query.i_type = HTTPD_MSG_NONE;
1336 else if( cl->query.i_body > 0 )
1338 /* we are reading the body of a request or a channel */
1339 i_len = recv( cl->fd, &cl->query.p_body[cl->i_buffer], cl->query.i_body - cl->i_buffer, 0 );
1342 cl->i_buffer += i_len;
1344 if( cl->i_buffer >= cl->query.i_body )
1346 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1351 /* we are reading a header -> char by char */
1354 i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 1, 0 );
1361 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1363 cl->i_buffer_size += 1024;
1364 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1366 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1367 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1371 /* we have finished the header so parse it and set i_body */
1372 cl->p_buffer[cl->i_buffer] = '\0';
1374 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1376 cl->query.i_status = strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1381 cl->query.psz_status = strdup( p );
1393 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1394 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1395 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1397 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1398 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1399 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1400 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1401 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1402 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1404 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1409 cl->query.i_type = HTTPD_MSG_NONE;
1411 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1413 for( i = 0; msg_type[i].name != NULL; i++ )
1415 if( !strncmp( cl->p_buffer, msg_type[i].name, strlen( msg_type[i].name ) ) )
1417 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1418 cl->query.i_type = msg_type[i].i_type;
1419 if( cl->query.i_proto != msg_type[i].i_proto )
1422 cl->query.i_proto = HTTPD_PROTO_NONE;
1423 cl->query.i_type = HTTPD_MSG_NONE;
1430 if( strstr( cl->p_buffer, "HTTP/1." ) )
1432 cl->query.i_proto = HTTPD_PROTO_HTTP;
1434 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1436 cl->query.i_proto = HTTPD_PROTO_RTSP;
1448 p2 = strchr( p, ' ' );
1453 if( !strncasecmp( p, "rtsp:", 5 ) )
1455 /* for rtsp url, you have rtsp://localhost:port/path */
1457 while( *p == '/' ) p++;
1458 while( *p && *p != '/' ) p++;
1460 cl->query.psz_url = strdup( p );
1461 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1464 cl->query.psz_args = strdup( p3 );
1472 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1474 cl->query.i_proto = HTTPD_PROTO_HTTP;
1475 cl->query.i_version = atoi( p2+7 );
1477 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1479 cl->query.i_proto = HTTPD_PROTO_RTSP;
1480 cl->query.i_version = atoi( p2+7 );
1488 p = strchr( p, '\n' );
1492 while( *p == '\n' || *p == '\r' )
1496 while( p && *p != '\0' )
1499 char *eol = p = strchr( p, '\n' );
1502 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1507 if( ( colon = strchr( line, ':' ) ) )
1513 while( *colon == ' ' )
1517 name = strdup( line );
1518 value = strdup( colon );
1520 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1521 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1523 if( !strcasecmp( name, "Content-Length" ) )
1525 cl->query.i_body = atol( value );
1532 while( *p == '\n' || *p == '\r' )
1539 if( cl->query.i_body > 0 )
1541 /* TODO Mhh, handle the case client will only send a request and close the connection
1542 * to mark and of body (probably only RTSP) */
1543 cl->query.p_body = malloc( cl->query.i_body );
1548 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1554 /* check if the client is to be set to dead */
1555 #if defined( WIN32 ) || defined( UNDER_CE )
1556 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1558 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1561 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1563 /* connection closed -> end of data */
1564 if( cl->query.i_body > 0 )
1566 cl->query.i_body = cl->i_buffer;
1568 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1572 cl->i_state = HTTPD_CLIENT_DEAD;
1575 cl->i_activity_date = mdate();
1577 /* Debugging only */
1578 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1582 fprintf( stderr, "received new request\n" );
1583 fprintf( stderr, " - proto=%s\n", cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1584 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1585 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1586 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1588 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status, cl->query.psz_status );
1590 else if( cl->query.i_type != HTTPD_MSG_NONE )
1592 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1594 for( i = 0; i < cl->query.i_name; i++ )
1596 fprintf( stderr, " - option name='%s' value='%s'\n", cl->query.name[i], cl->query.value[i] );
1601 static void httpd_ClientSend( httpd_client_t *cl )
1606 if( cl->i_buffer < 0 )
1608 /* We need to create the header */
1612 i_size = strlen( "HTTP/1.") + 10 + 10 +
1613 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1614 for( i = 0; i < cl->answer.i_name; i++ )
1616 i_size += strlen( cl->answer.name[i] ) + 2 + strlen( cl->answer.value[i] ) + 2;
1619 if( cl->i_buffer_size < i_size )
1621 cl->i_buffer_size = i_size;
1622 free( cl->p_buffer );
1623 cl->p_buffer = malloc( i_size );
1627 p += sprintf( p, "%s/1.%d %d %s\r\n",
1628 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1629 cl->answer.i_version,
1630 cl->answer.i_status, cl->answer.psz_status );
1631 for( i = 0; i < cl->answer.i_name; i++ )
1633 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i], cl->answer.value[i] );
1635 p += sprintf( p, "\r\n" );
1638 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1640 fprintf( stderr, "sending answer\n" );
1641 fprintf( stderr, "%s", cl->p_buffer );
1644 i_len = send( cl->fd, &cl->p_buffer[cl->i_buffer], cl->i_buffer_size - cl->i_buffer, 0 );
1647 cl->i_activity_date = mdate();
1648 cl->i_buffer += i_len;
1650 if( cl->i_buffer >= cl->i_buffer_size )
1652 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 && !cl->b_read_waiting )
1654 /* catch more body data */
1655 int i_msg = cl->query.i_type;
1656 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1657 &cl->answer, &cl->query );
1660 if( cl->answer.i_body > 0 )
1662 /* send the body data */
1663 free( cl->p_buffer );
1664 cl->p_buffer = cl->answer.p_body;
1665 cl->i_buffer_size = cl->answer.i_body;
1668 cl->answer.i_body = 0;
1669 cl->answer.p_body = NULL;
1674 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1680 #if defined( WIN32 ) || defined( UNDER_CE )
1681 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1683 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1687 cl->i_state = HTTPD_CLIENT_DEAD;
1692 static void httpd_HostThread( httpd_host_t *host )
1694 while( !host->b_die )
1696 struct timeval timeout;
1699 int i_handle_max = 0;
1702 int b_low_delay = 0;
1704 if( host->i_url <= 0 )
1711 /* built a set of handle to select */
1712 FD_ZERO( &fds_read );
1713 FD_ZERO( &fds_write );
1715 FD_SET( host->fd, &fds_read );
1716 i_handle_max = host->fd;
1718 /* add all socket that should be read/write and close dead connection */
1719 vlc_mutex_lock( &host->lock );
1720 for( i_client = 0; i_client < host->i_client; i_client++ )
1722 httpd_client_t *cl = host->client[i_client];
1724 if( cl->i_ref < 0 ||
1726 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1727 cl->i_activity_date + cl->i_activity_timeout < mdate() ) ) )
1729 msg_Dbg( host, "connection closed(%s)", inet_ntoa(cl->sock.sin_addr) );
1731 httpd_ClientClean( cl );
1733 TAB_REMOVE( host->i_client, host->client, cl );
1736 else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
1738 FD_SET( cl->fd, &fds_read );
1739 i_handle_max = __MAX( i_handle_max, cl->fd );
1741 else if( cl->i_state == HTTPD_CLIENT_SENDING )
1743 FD_SET( cl->fd, &fds_write );
1744 i_handle_max = __MAX( i_handle_max, cl->fd );
1746 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1748 httpd_message_t *answer = &cl->answer;
1749 httpd_message_t *query = &cl->query;
1750 int i_msg = query->i_type;
1752 httpd_MsgInit( answer );
1754 /* Handle what we received */
1755 if( cl->i_mode != HTTPD_CLIENT_BIDIR && ( i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL ) )
1757 /* we can only receive request from client when not in BIDIR mode */
1759 cl->i_state = HTTPD_CLIENT_DEAD;
1761 else if( i_msg == HTTPD_MSG_ANSWER )
1763 /* We are in BIDIR mode, trigger the callback and then check for new data */
1764 if( cl->url && cl->url->catch[i_msg].cb )
1766 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, NULL, query );
1768 cl->i_state = HTTPD_CLIENT_WAITING;
1770 else if( i_msg == HTTPD_MSG_CHANNEL )
1772 /* We are in BIDIR mode, trigger the callback and then check for new data */
1773 if( cl->url && cl->url->catch[i_msg].cb )
1775 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, NULL, query );
1777 cl->i_state = HTTPD_CLIENT_WAITING;
1779 else if( i_msg == HTTPD_MSG_OPTIONS )
1784 answer->i_proto = query->i_proto ;
1785 answer->i_type = HTTPD_MSG_ANSWER;
1786 answer->i_version= 0;
1787 answer->i_status = 200;
1788 answer->psz_status = strdup( "Ok" );
1791 answer->p_body = NULL;
1793 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
1794 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1795 httpd_MsgAdd( answer, "Server", "VLC Server" );
1796 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE" );
1797 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1799 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1800 cl->i_state = HTTPD_CLIENT_SENDING;
1802 else if( i_msg == HTTPD_MSG_NONE )
1804 if( query->i_proto == HTTPD_PROTO_NONE )
1807 cl->i_state = HTTPD_CLIENT_DEAD;
1814 answer->i_proto = query->i_proto ;
1815 answer->i_type = HTTPD_MSG_ANSWER;
1816 answer->i_version= 0;
1817 answer->i_status = 501;
1818 answer->psz_status = strdup( "Unimplemented" );
1820 p = answer->p_body = malloc( 1000 );
1822 p += sprintf( p, "<html>\n" );
1823 p += sprintf( p, "<head>\n" );
1824 p += sprintf( p, "<title>Error 501</title>\n" );
1825 p += sprintf( p, "</head>\n" );
1826 p += sprintf( p, "<body>\n" );
1827 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
1828 p += sprintf( p, "<hr />\n" );
1829 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1830 p += sprintf( p, "</body>\n" );
1831 p += sprintf( p, "</html>\n" );
1833 answer->i_body = p - answer->p_body;
1834 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1836 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1837 cl->i_state = HTTPD_CLIENT_SENDING;
1842 vlc_bool_t b_auth_failed = VLC_FALSE;
1845 /* Search the url and trigger callbacks */
1846 for( i = 0; i < host->i_url; i++ )
1848 httpd_url_t *url = host->url[i];
1850 if( !strcmp( url->psz_url, query->psz_url ) )
1852 if( url->catch[i_msg].cb )
1854 if( answer && ( *url->psz_user || *url->psz_password ) )
1856 /* create the headers */
1857 char id[strlen(url->psz_user)+strlen(url->psz_password) + 2];
1858 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
1859 char auth[strlen(b64) +1];
1861 sprintf( id, "%s:%s", url->psz_user, url->psz_password );
1862 if( !strncasecmp( b64, "BASIC", 5 ) )
1865 while( *b64 == ' ' )
1869 b64_decode( auth, b64 );
1875 if( strcmp( id, auth ) )
1877 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
1878 /* We fail for all url */
1879 b_auth_failed = VLC_TRUE;
1884 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
1886 /* only one url can answer */
1888 if( cl->url == NULL )
1900 answer->i_proto = query->i_proto;
1901 answer->i_type = HTTPD_MSG_ANSWER;
1902 answer->i_version= 0;
1903 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
1907 answer->i_status = 401;
1908 answer->psz_status = strdup( "Authorization Required" );
1910 p += sprintf( p, "<html>\n" );
1911 p += sprintf( p, "<head>\n" );
1912 p += sprintf( p, "<title>Error 401</title>\n" );
1913 p += sprintf( p, "</head>\n" );
1914 p += sprintf( p, "<body>\n" );
1915 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
1916 p += sprintf( p, "<hr />\n" );
1917 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1918 p += sprintf( p, "</body>\n" );
1919 p += sprintf( p, "</html>\n" );
1923 /* no url registered */
1924 answer->i_status = 404;
1925 answer->psz_status = strdup( "Not found" );
1927 p += sprintf( p, "<html>\n" );
1928 p += sprintf( p, "<head>\n" );
1929 p += sprintf( p, "<title>Error 404</title>\n" );
1930 p += sprintf( p, "</head>\n" );
1931 p += sprintf( p, "<body>\n" );
1932 p += sprintf( p, "<h1><center> 404 Ressource not found(%s)</center></h1>\n", query->psz_url );
1933 p += sprintf( p, "<hr />\n" );
1934 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1935 p += sprintf( p, "</body>\n" );
1936 p += sprintf( p, "</html>\n" );
1939 answer->i_body = p - answer->p_body;
1940 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1942 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1943 cl->i_state = HTTPD_CLIENT_SENDING;
1946 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
1948 if( cl->i_mode == HTTPD_CLIENT_FILE )
1951 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
1952 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
1953 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
1954 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
1955 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
1956 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
1958 httpd_MsgClean( &cl->query );
1959 httpd_MsgInit( &cl->query );
1962 cl->i_buffer_size = 1000;
1963 free( cl->p_buffer );
1964 cl->p_buffer = malloc( cl->i_buffer_size );
1965 cl->i_state = HTTPD_CLIENT_RECEIVING;
1969 cl->i_state = HTTPD_CLIENT_DEAD;
1971 httpd_MsgClean( &cl->answer );
1973 else if( cl->b_read_waiting )
1975 /* we have a message waiting for us to read it */
1976 httpd_MsgClean( &cl->answer );
1977 httpd_MsgClean( &cl->query );
1980 cl->i_buffer_size = 1000;
1981 free( cl->p_buffer );
1982 cl->p_buffer = malloc( cl->i_buffer_size );
1983 cl->i_state = HTTPD_CLIENT_RECEIVING;
1984 cl->b_read_waiting = VLC_FALSE;
1988 int64_t i_offset = cl->answer.i_body_offset;
1989 httpd_MsgClean( &cl->answer );
1991 cl->answer.i_body_offset = i_offset;
1992 cl->i_state = HTTPD_CLIENT_WAITING;
1995 else if( cl->i_state == HTTPD_CLIENT_WAITING )
1997 int64_t i_offset = cl->answer.i_body_offset;
1998 int i_msg = cl->query.i_type;
2000 httpd_MsgInit( &cl->answer );
2001 cl->answer.i_body_offset = i_offset;
2003 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, &cl->answer, &cl->query );
2004 if( cl->answer.i_type != HTTPD_MSG_NONE )
2006 /* we have new data, so reenter send mode */
2008 cl->p_buffer = cl->answer.p_body;
2009 cl->i_buffer_size = cl->answer.i_body;
2010 cl->answer.p_body = NULL;
2011 cl->answer.i_body = 0;
2012 cl->i_state = HTTPD_CLIENT_SENDING;
2016 /* we shouldn't wait too long */
2017 b_low_delay = VLC_TRUE;
2021 /* Special for BIDIR mode we also check reading */
2022 if( cl->i_mode == HTTPD_CLIENT_BIDIR && cl->i_state == HTTPD_CLIENT_SENDING )
2024 FD_SET( cl->fd, &fds_read );
2025 i_handle_max = __MAX( i_handle_max, cl->fd );
2028 vlc_mutex_unlock( &host->lock );
2030 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2032 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2034 i_ret = select( i_handle_max + 1,
2035 &fds_read, &fds_write, NULL, &timeout );
2037 if( i_ret == -1 && errno != EINTR )
2039 msg_Warn( host, "cannot select sockets" );
2043 else if( i_ret <= 0 )
2048 /* accept new connections */
2049 if( FD_ISSET( host->fd, &fds_read ) )
2051 int i_sock_size = sizeof( struct sockaddr_in );
2052 struct sockaddr_in sock;
2055 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2058 httpd_client_t *cl = httpd_ClientNew( fd, &sock );
2060 vlc_mutex_lock( &host->lock );
2061 TAB_APPEND( host->i_client, host->client, cl );
2062 vlc_mutex_unlock( &host->lock );
2064 msg_Dbg( host, "new connection (%s)", inet_ntoa(sock.sin_addr) );
2067 /* now try all others socket */
2068 vlc_mutex_lock( &host->lock );
2069 for( i_client = 0; i_client < host->i_client; i_client++ )
2071 httpd_client_t *cl = host->client[i_client];
2072 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2074 httpd_ClientRecv( cl );
2076 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2078 httpd_ClientSend( cl );
2081 if( cl->i_mode == HTTPD_CLIENT_BIDIR && cl->i_state == HTTPD_CLIENT_SENDING &&
2082 FD_ISSET( cl->fd, &fds_read ) )
2084 cl->b_read_waiting = VLC_TRUE;
2087 vlc_mutex_unlock( &host->lock );
2095 static int BuildAddr( struct sockaddr_in * p_socket,
2096 const char * psz_address, int i_port )
2099 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2100 p_socket->sin_family = AF_INET; /* family */
2101 p_socket->sin_port = htons( (uint16_t)i_port );
2104 p_socket->sin_addr.s_addr = INADDR_ANY;
2108 struct hostent * p_hostent;
2110 /* Try to convert address directly from in_addr - this will work if
2111 * psz_address is dotted decimal. */
2112 #ifdef HAVE_ARPA_INET_H
2113 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2115 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2116 if( p_socket->sin_addr.s_addr == INADDR_NONE )
2119 /* We have a fqdn, try to find its address */
2120 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2125 /* Copy the first address of the host in the socket address */
2126 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
2127 p_hostent->h_length );