Commit fe927c0f authored by Christophe Massiot's avatar Christophe Massiot

* src/extras/libc.c: Implemented a wrapper around fork() and execve()

   to spawn an external process and get its output. Only implemented for
   UNIX-style systems at present.
 * src/misc/httpd.c: New "handler" node type which bypasses the internal
   HTTPD behaviour.
 * modules/control/http: New --http-handlers option to dedicate
   particular extensions to external programs (PHP or Perl for instance).
   This is in accordance with the CGI/1.1 specification.
parent 375abfbd
...@@ -325,7 +325,7 @@ CPPFLAGS_save="${CPPFLAGS_save} -DSYS_`echo ${SYS} | sed -e 's/-.*//' | tr 'abcd ...@@ -325,7 +325,7 @@ CPPFLAGS_save="${CPPFLAGS_save} -DSYS_`echo ${SYS} | sed -e 's/-.*//' | tr 'abcd
dnl Check for system libs needed dnl Check for system libs needed
need_libc=false need_libc=false
AC_CHECK_FUNCS(gettimeofday select strerror strtod strtol strtof strtoll strtoull strsep isatty vasprintf asprintf swab sigrelse getpwuid memalign posix_memalign if_nametoindex atoll getenv putenv setenv gmtime_r ctime_r localtime_r lrintf daemon scandir) AC_CHECK_FUNCS(gettimeofday select strerror strtod strtol strtof strtoll strtoull strsep isatty vasprintf asprintf swab sigrelse getpwuid memalign posix_memalign if_nametoindex atoll getenv putenv setenv gmtime_r ctime_r localtime_r lrintf daemon scandir fork)
dnl Check for usual libc functions dnl Check for usual libc functions
AC_CHECK_FUNCS(strdup strndup atof) AC_CHECK_FUNCS(strdup strndup atof)
...@@ -1882,7 +1882,7 @@ have_ipv6=no ...@@ -1882,7 +1882,7 @@ have_ipv6=no
AC_CHECK_FUNCS(inet_pton,[have_ipv6=yes],[ AC_CHECK_FUNCS(inet_pton,[have_ipv6=yes],[
AC_CHECK_LIB(resolv,inet_pton, AC_CHECK_LIB(resolv,inet_pton,
[have_ipv6=yes [have_ipv6=yes
VLC_ADD_LDFLAGS([ipv6],[-lresolv])]) VLC_ADD_LDFLAGS([ipv6 vlc],[-lresolv])])
]) ])
AS_IF([test "${have_ipv6}" == "yes"], [ AS_IF([test "${have_ipv6}" == "yes"], [
......
...@@ -378,7 +378,10 @@ typedef struct httpd_message_t httpd_message_t; ...@@ -378,7 +378,10 @@ typedef struct httpd_message_t httpd_message_t;
typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query ); typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
typedef struct httpd_file_t httpd_file_t; typedef struct httpd_file_t httpd_file_t;
typedef struct httpd_file_sys_t httpd_file_sys_t; typedef struct httpd_file_sys_t httpd_file_sys_t;
typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data ); typedef int (*httpd_file_callback_t)( httpd_file_sys_t *, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
typedef struct httpd_handler_t httpd_handler_t;
typedef struct httpd_handler_sys_t httpd_handler_sys_t;
typedef int (*httpd_handler_callback_t)( httpd_handler_sys_t *, httpd_handler_t *, uint8_t *psz_url, uint8_t *psz_request, int i_type, uint8_t *p_in, int i_in, char *psz_remote_addr, char *psz_remote_host, uint8_t **pp_data, int *pi_data );
typedef struct httpd_redirect_t httpd_redirect_t; typedef struct httpd_redirect_t httpd_redirect_t;
typedef struct httpd_stream_t httpd_stream_t; typedef struct httpd_stream_t httpd_stream_t;
...@@ -1051,6 +1054,10 @@ VLC_EXPORT( vlc_iconv_t, vlc_iconv_open, ( const char *, const char * ) ); ...@@ -1051,6 +1054,10 @@ VLC_EXPORT( vlc_iconv_t, vlc_iconv_open, ( const char *, const char * ) );
VLC_EXPORT( size_t, vlc_iconv, ( vlc_iconv_t, char **, size_t *, char **, size_t * ) ); VLC_EXPORT( size_t, vlc_iconv, ( vlc_iconv_t, char **, size_t *, char **, size_t * ) );
VLC_EXPORT( int, vlc_iconv_close, ( vlc_iconv_t ) ); VLC_EXPORT( int, vlc_iconv_close, ( vlc_iconv_t ) );
/* execve wrapper (defined in src/extras/libc.c) */
VLC_EXPORT( int, __vlc_execve, ( vlc_object_t *p_object, int i_argc, char **pp_argv, char **pp_env, char *psz_cwd, char *p_in, int i_in, char **pp_data, int *pi_data ) );
#define vlc_execve(a,b,c,d,e,f,g,h,i) __vlc_execve(VLC_OBJECT(a),b,c,d,e,f,g,h,i)
/***************************************************************************** /*****************************************************************************
* CPU capabilities * CPU capabilities
*****************************************************************************/ *****************************************************************************/
......
...@@ -140,6 +140,10 @@ VLC_EXPORT( httpd_file_t *, httpd_FileNew, ( httpd_host_t *, const char *psz_url ...@@ -140,6 +140,10 @@ VLC_EXPORT( httpd_file_t *, httpd_FileNew, ( httpd_host_t *, const char *psz_url
VLC_EXPORT( void, httpd_FileDelete, ( httpd_file_t * ) ); VLC_EXPORT( void, httpd_FileDelete, ( httpd_file_t * ) );
VLC_EXPORT( httpd_handler_t *, httpd_HandlerNew, ( httpd_host_t *, const char *psz_url, const char *psz_user, const char *psz_password, const vlc_acl_t *p_acl, httpd_handler_callback_t pf_fill, httpd_handler_sys_t * ) );
VLC_EXPORT( void, httpd_HandlerDelete, ( httpd_handler_t * ) );
VLC_EXPORT( httpd_redirect_t *, httpd_RedirectNew, ( httpd_host_t *, const char *psz_url_dst, const char *psz_url_src ) ); VLC_EXPORT( httpd_redirect_t *, httpd_RedirectNew, ( httpd_host_t *, const char *psz_url_dst, const char *psz_url_src ) );
VLC_EXPORT( void, httpd_RedirectDelete, ( httpd_redirect_t * ) ); VLC_EXPORT( void, httpd_RedirectDelete, ( httpd_redirect_t * ) );
......
...@@ -166,6 +166,7 @@ char * vlc_strcasestr (const char *s1, const char *s2); ...@@ -166,6 +166,7 @@ char * vlc_strcasestr (const char *s1, const char *s2);
httpd_file_t * httpd_FileNew (httpd_host_t *, const char *psz_url, const char *psz_mime, const char *psz_user, const char *psz_password, const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill, httpd_file_sys_t *); httpd_file_t * httpd_FileNew (httpd_host_t *, const char *psz_url, const char *psz_mime, const char *psz_user, const char *psz_password, const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill, httpd_file_sys_t *);
void vlc_freeaddrinfo (struct addrinfo *); void vlc_freeaddrinfo (struct addrinfo *);
void vlm_Delete (vlm_t *); void vlm_Delete (vlm_t *);
void httpd_HandlerDelete (httpd_handler_t *);
void vout_DisplayPicture (vout_thread_t *, picture_t *); void vout_DisplayPicture (vout_thread_t *, picture_t *);
void httpd_MsgClean (httpd_message_t *); void httpd_MsgClean (httpd_message_t *);
int vout_ControlWindow (vout_thread_t *, void *, int, va_list); int vout_ControlWindow (vout_thread_t *, void *, int, va_list);
...@@ -203,6 +204,7 @@ float __config_GetFloat (vlc_object_t *, const char *); ...@@ -203,6 +204,7 @@ float __config_GetFloat (vlc_object_t *, const char *);
playlist_item_t * playlist_ItemGetById (playlist_t *, int); playlist_item_t * playlist_ItemGetById (playlist_t *, int);
const char * vlc_gai_strerror (int); const char * vlc_gai_strerror (int);
void net_ListenClose (int *fd); void net_ListenClose (int *fd);
int __vlc_execve (vlc_object_t *p_object, int i_argc, char **pp_argv, char **pp_env, char *psz_cwd, char *p_in, int i_in, char **pp_data, int *pi_data);
int playlist_NodeAppend (playlist_t *,int,playlist_item_t*,playlist_item_t *); int playlist_NodeAppend (playlist_t *,int,playlist_item_t*,playlist_item_t *);
const iso639_lang_t * GetLang_2B (const char *); const iso639_lang_t * GetLang_2B (const char *);
void sout_AccessOutDelete (sout_access_out_t *); void sout_AccessOutDelete (sout_access_out_t *);
...@@ -357,6 +359,7 @@ module_config_t * config_FindConfig (vlc_object_t *, const char *); ...@@ -357,6 +359,7 @@ module_config_t * config_FindConfig (vlc_object_t *, const char *);
playlist_item_t * playlist_ItemGetByInput (playlist_t *,input_item_t *); playlist_item_t * playlist_ItemGetByInput (playlist_t *,input_item_t *);
void config_SetCallbacks (module_config_t *, module_config_t *); void config_SetCallbacks (module_config_t *, module_config_t *);
int __vlc_cond_destroy (char *, int, vlc_cond_t *); int __vlc_cond_destroy (char *, int, vlc_cond_t *);
httpd_handler_t * httpd_HandlerNew (httpd_host_t *, const char *psz_url, const char *psz_user, const char *psz_password, const vlc_acl_t *p_acl, httpd_handler_callback_t pf_fill, httpd_handler_sys_t *);
vlc_list_t * __vlc_list_find (vlc_object_t *, int, int); vlc_list_t * __vlc_list_find (vlc_object_t *, int, int);
char * ToLocale (const char *); char * ToLocale (const char *);
int vlm_Load (vlm_t *, char *); int vlm_Load (vlm_t *, char *);
...@@ -833,6 +836,9 @@ struct module_symbols_t ...@@ -833,6 +836,9 @@ struct module_symbols_t
struct dirent * (*vlc_readdir_wrapper_inner) (void *); struct dirent * (*vlc_readdir_wrapper_inner) (void *);
int (*vlc_closedir_wrapper_inner) (void *); int (*vlc_closedir_wrapper_inner) (void *);
void * (*vlc_opendir_wrapper_inner) (const char *); void * (*vlc_opendir_wrapper_inner) (const char *);
void (*httpd_HandlerDelete_inner) (httpd_handler_t *);
int (*__vlc_execve_inner) (vlc_object_t *p_object, int i_argc, char **pp_argv, char **pp_env, char *psz_cwd, char *p_in, int i_in, char **pp_data, int *pi_data);
httpd_handler_t * (*httpd_HandlerNew_inner) (httpd_host_t *, const char *psz_url, const char *psz_user, const char *psz_password, const vlc_acl_t *p_acl, httpd_handler_callback_t pf_fill, httpd_handler_sys_t *);
}; };
# if defined (__PLUGIN__) # if defined (__PLUGIN__)
# define aout_FiltersCreatePipeline (p_symbols)->aout_FiltersCreatePipeline_inner # define aout_FiltersCreatePipeline (p_symbols)->aout_FiltersCreatePipeline_inner
...@@ -1232,6 +1238,9 @@ struct module_symbols_t ...@@ -1232,6 +1238,9 @@ struct module_symbols_t
# define vlc_readdir_wrapper (p_symbols)->vlc_readdir_wrapper_inner # define vlc_readdir_wrapper (p_symbols)->vlc_readdir_wrapper_inner
# define vlc_closedir_wrapper (p_symbols)->vlc_closedir_wrapper_inner # define vlc_closedir_wrapper (p_symbols)->vlc_closedir_wrapper_inner
# define vlc_opendir_wrapper (p_symbols)->vlc_opendir_wrapper_inner # define vlc_opendir_wrapper (p_symbols)->vlc_opendir_wrapper_inner
# define httpd_HandlerDelete (p_symbols)->httpd_HandlerDelete_inner
# define __vlc_execve (p_symbols)->__vlc_execve_inner
# define httpd_HandlerNew (p_symbols)->httpd_HandlerNew_inner
# elif defined (HAVE_DYNAMIC_PLUGINS) && !defined (__BUILTIN__) # elif defined (HAVE_DYNAMIC_PLUGINS) && !defined (__BUILTIN__)
/****************************************************************** /******************************************************************
* STORE_SYMBOLS: store VLC APIs into p_symbols for plugin access. * STORE_SYMBOLS: store VLC APIs into p_symbols for plugin access.
...@@ -1634,6 +1643,9 @@ struct module_symbols_t ...@@ -1634,6 +1643,9 @@ struct module_symbols_t
((p_symbols)->vlc_readdir_wrapper_inner) = vlc_readdir_wrapper; \ ((p_symbols)->vlc_readdir_wrapper_inner) = vlc_readdir_wrapper; \
((p_symbols)->vlc_closedir_wrapper_inner) = vlc_closedir_wrapper; \ ((p_symbols)->vlc_closedir_wrapper_inner) = vlc_closedir_wrapper; \
((p_symbols)->vlc_opendir_wrapper_inner) = vlc_opendir_wrapper; \ ((p_symbols)->vlc_opendir_wrapper_inner) = vlc_opendir_wrapper; \
((p_symbols)->httpd_HandlerDelete_inner) = httpd_HandlerDelete; \
((p_symbols)->__vlc_execve_inner) = __vlc_execve; \
((p_symbols)->httpd_HandlerNew_inner) = httpd_HandlerNew; \
(p_symbols)->net_ConvertIPv4_deprecated = NULL; \ (p_symbols)->net_ConvertIPv4_deprecated = NULL; \
(p_symbols)->vlc_fix_readdir_charset_deprecated = NULL; \ (p_symbols)->vlc_fix_readdir_charset_deprecated = NULL; \
(p_symbols)->osd_Slider_deprecated = NULL; \ (p_symbols)->osd_Slider_deprecated = NULL; \
......
...@@ -39,6 +39,10 @@ static void Close( vlc_object_t * ); ...@@ -39,6 +39,10 @@ static void Close( vlc_object_t * );
#define CHARSET_TEXT N_( "Charset" ) #define CHARSET_TEXT N_( "Charset" )
#define CHARSET_LONGTEXT N_( \ #define CHARSET_LONGTEXT N_( \
"Charset declared in Content-Type header (default UTF-8)." ) "Charset declared in Content-Type header (default UTF-8)." )
#define HANDLERS_TEXT N_( "Handlers" )
#define HANDLERS_LONGTEXT N_( \
"List of extensions and executable paths (for instance: " \
"php=/usr/bin/php,pl=/usr/bin/perl)." )
#define CERT_TEXT N_( "Certificate file" ) #define CERT_TEXT N_( "Certificate file" )
#define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \ #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
"(enables SSL)" ) "(enables SSL)" )
...@@ -58,6 +62,9 @@ vlc_module_begin(); ...@@ -58,6 +62,9 @@ vlc_module_begin();
add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE ); add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE ); add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
add_string ( "http-charset", "UTF-8", NULL, CHARSET_TEXT, CHARSET_LONGTEXT, VLC_TRUE ); add_string ( "http-charset", "UTF-8", NULL, CHARSET_TEXT, CHARSET_LONGTEXT, VLC_TRUE );
#if defined( HAVE_FORK )
add_string ( "http-handlers", NULL, NULL, HANDLERS_TEXT, HANDLERS_LONGTEXT, VLC_TRUE );
#endif
set_section( N_("HTTP SSL" ), 0 ); set_section( N_("HTTP SSL" ), 0 );
add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE ); add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE ); add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
...@@ -108,26 +115,24 @@ static int Open( vlc_object_t *p_this ) ...@@ -108,26 +115,24 @@ static int Open( vlc_object_t *p_this )
{ {
intf_thread_t *p_intf = (intf_thread_t*)p_this; intf_thread_t *p_intf = (intf_thread_t*)p_this;
intf_sys_t *p_sys; intf_sys_t *p_sys;
char *psz_host; char *psz_address;
char *psz_address = "";
const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL, const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
*psz_crl = NULL; *psz_crl = NULL;
int i_port = 0; int i_port = 0;
char *psz_src; char *psz_src;
psz_host = config_GetPsz( p_intf, "http-host" ); psz_address = config_GetPsz( p_intf, "http-host" );
if( psz_host ) if( psz_address != NULL )
{ {
char *psz_parser; char *psz_parser = strchr( psz_address, ':' );
psz_address = psz_host;
psz_parser = strchr( psz_host, ':' );
if( psz_parser ) if( psz_parser )
{ {
*psz_parser++ = '\0'; *psz_parser++ = '\0';
i_port = atoi( psz_parser ); i_port = atoi( psz_parser );
} }
} }
else
psz_address = strdup("");
p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) ); p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
if( !p_intf->p_sys ) if( !p_intf->p_sys )
...@@ -137,6 +142,8 @@ static int Open( vlc_object_t *p_this ) ...@@ -137,6 +142,8 @@ static int Open( vlc_object_t *p_this )
p_sys->p_playlist = NULL; p_sys->p_playlist = NULL;
p_sys->p_input = NULL; p_sys->p_input = NULL;
p_sys->p_vlm = NULL; p_sys->p_vlm = NULL;
p_sys->psz_address = psz_address;
p_sys->i_port = i_port;
/* determine Content-Type value for HTML pages */ /* determine Content-Type value for HTML pages */
psz_src = config_GetPsz( p_intf, "http-charset" ); psz_src = config_GetPsz( p_intf, "http-charset" );
...@@ -149,6 +156,7 @@ static int Open( vlc_object_t *p_this ) ...@@ -149,6 +156,7 @@ static int Open( vlc_object_t *p_this )
p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) ); p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
if( p_sys->psz_html_type == NULL ) if( p_sys->psz_html_type == NULL )
{ {
free( p_sys->psz_address );
free( p_sys ); free( p_sys );
free( psz_src ); free( psz_src );
return VLC_ENOMEM ; return VLC_ENOMEM ;
...@@ -178,6 +186,50 @@ static int Open( vlc_object_t *p_this ) ...@@ -178,6 +186,50 @@ static int Open( vlc_object_t *p_this )
free( psz_src ); free( psz_src );
/* determine file handler associations */
p_sys->i_handlers = 0;
p_sys->pp_handlers = NULL;
#if defined( HAVE_FORK )
psz_src = config_GetPsz( p_intf, "http-handlers" );
if( psz_src != NULL && *psz_src )
{
char *p = psz_src;
while( p != NULL )
{
http_association_t *p_handler;
char *psz_ext = p;
char *psz_program, *psz_options;
p = strchr( p, '=' );
if( p == NULL ) break;
*p++ = '\0';
psz_program = p;
p = strchr( p, ',' );
if( p != NULL )
*p++ = '\0';
p_handler = malloc( sizeof( http_association_t ) );
p_handler->psz_ext = strdup( psz_ext );
psz_options = E_(FirstWord)( psz_program, psz_program );
p_handler->i_argc = 0;
p_handler->ppsz_argv = NULL;
TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
strdup( psz_program ) );
while( psz_options != NULL && *psz_options )
{
char *psz_next = E_(FirstWord)( psz_options, psz_options );
TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
strdup( psz_options ) );
psz_options = psz_next;
}
/* NULL will be appended later on */
TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, p_handler );
}
}
if( psz_src != NULL )
free( psz_src );
#endif
/* determine SSL configuration */ /* determine SSL configuration */
psz_cert = config_GetPsz( p_intf, "http-intf-cert" ); psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
if ( psz_cert != NULL ) if ( psz_cert != NULL )
...@@ -206,15 +258,11 @@ static int Open( vlc_object_t *p_this ) ...@@ -206,15 +258,11 @@ static int Open( vlc_object_t *p_this )
{ {
msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port ); msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
free( p_sys->psz_html_type ); free( p_sys->psz_html_type );
free( p_sys->psz_address );
free( p_sys ); free( p_sys );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
if( psz_host )
{
free( psz_host );
}
p_sys->i_files = 0; p_sys->i_files = 0;
p_sys->pp_files = NULL; p_sys->pp_files = NULL;
...@@ -225,9 +273,9 @@ static int Open( vlc_object_t *p_this ) ...@@ -225,9 +273,9 @@ static int Open( vlc_object_t *p_this )
psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 ); psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
if( !psz_src ) return VLC_ENOMEM; if( !psz_src ) return VLC_ENOMEM;
#if defined(WIN32) #if defined(WIN32)
sprintf( psz_src, "%s/http", psz_vlcpath); sprintf( psz_src, "%s/http", psz_vlcpath );
#else #else
sprintf( psz_src, "%s/share/http", psz_vlcpath); sprintf( psz_src, "%s/share/http", psz_vlcpath );
#endif #endif
} }
#else #else
...@@ -279,6 +327,7 @@ failed: ...@@ -279,6 +327,7 @@ failed:
free( p_sys->pp_files ); free( p_sys->pp_files );
} }
httpd_HostDelete( p_sys->p_httpd_host ); httpd_HostDelete( p_sys->p_httpd_host );
free( p_sys->psz_address );
free( p_sys->psz_html_type ); free( p_sys->psz_html_type );
if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 ) if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
vlc_iconv_close( p_sys->iconv_from_utf8 ); vlc_iconv_close( p_sys->iconv_from_utf8 );
...@@ -304,6 +353,9 @@ static void Close ( vlc_object_t *p_this ) ...@@ -304,6 +353,9 @@ static void Close ( vlc_object_t *p_this )
} }
for( i = 0; i < p_sys->i_files; i++ ) for( i = 0; i < p_sys->i_files; i++ )
{ {
if( p_sys->pp_files[i]->b_handler )
httpd_HandlerDelete( ((httpd_handler_sys_t *)p_sys->pp_files[i])->p_handler );
else
httpd_FileDelete( p_sys->pp_files[i]->p_file ); httpd_FileDelete( p_sys->pp_files[i]->p_file );
if( p_sys->pp_files[i]->p_redir ) if( p_sys->pp_files[i]->p_redir )
httpd_RedirectDelete( p_sys->pp_files[i]->p_redir ); httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
...@@ -318,7 +370,21 @@ static void Close ( vlc_object_t *p_this ) ...@@ -318,7 +370,21 @@ static void Close ( vlc_object_t *p_this )
{ {
free( p_sys->pp_files ); free( p_sys->pp_files );
} }
for( i = 0; i < p_sys->i_handlers; i++ )
{
http_association_t *p_handler = p_sys->pp_handlers[i];
int j;
free( p_handler->psz_ext );
for( j = 0; j < p_handler->i_argc; j++ )
free( p_handler->ppsz_argv[j] );
if( p_handler->i_argc )
free( p_handler->ppsz_argv );
free( p_handler );
}
if( p_sys->i_handlers )
free( p_sys->pp_handlers );
httpd_HostDelete( p_sys->p_httpd_host ); httpd_HostDelete( p_sys->p_httpd_host );
free( p_sys->psz_address );
free( p_sys->psz_html_type ); free( p_sys->psz_html_type );
if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 ) if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
...@@ -382,27 +448,14 @@ static void Run( intf_thread_t *p_intf ) ...@@ -382,27 +448,14 @@ static void Run( intf_thread_t *p_intf )
* HttpCallback: * HttpCallback:
**************************************************************************** ****************************************************************************
* a file with b_html is parsed and all "macro" replaced * a file with b_html is parsed and all "macro" replaced
* <vlc id="macro name" [param1="" [param2=""]] />
* valid id are
*
****************************************************************************/ ****************************************************************************/
int E_(HttpCallback)( httpd_file_sys_t *p_args, static void Callback404( httpd_file_sys_t *p_args, char **pp_data,
httpd_file_t *p_file, int *pi_data )
uint8_t *_p_request,
uint8_t **_pp_data, int *pi_data )
{ {
char *p_request = (char *)_p_request; char *p = *pp_data = malloc( 10240 );
char **pp_data = (char **)_pp_data;
int i_request = p_request ? strlen( p_request ) : 0;
char *p;
FILE *f;
if( ( f = fopen( p_args->file, "r" ) ) == NULL )
{
p = *pp_data = malloc( 10240 );
if( !p ) if( !p )
{ {
return VLC_EGENERIC; return;
} }
p += sprintf( p, "<html>\n" ); p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" ); p += sprintf( p, "<head>\n" );
...@@ -416,18 +469,13 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args, ...@@ -416,18 +469,13 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args,
p += sprintf( p, "</html>\n" ); p += sprintf( p, "</html>\n" );
*pi_data = strlen( *pp_data ); *pi_data = strlen( *pp_data );
}
return VLC_SUCCESS; static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
} int i_buffer, char *p_request,
char **pp_data, int *pi_data )
if( !p_args->b_html ) {
{ int i_request = p_request != NULL ? strlen( p_request ) : 0;
E_(FileLoad)( f, pp_data, pi_data );
}
else
{
int i_buffer;
char *p_buffer;
char *dst; char *dst;
vlc_value_t val; vlc_value_t val;
char position[4]; /* percentage */ char position[4]; /* percentage */
...@@ -470,8 +518,8 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args, ...@@ -470,8 +518,8 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args,
} }
#undef p_sys #undef p_sys
aout_VolumeGet( p_args->p_intf , &i_volume ); aout_VolumeGet( p_args->p_intf, &i_volume );
sprintf( volume , "%d" , (int)i_volume ); sprintf( volume, "%d", (int)i_volume );
p_args->vars = E_(mvar_New)( "variables", "" ); p_args->vars = E_(mvar_New)( "variables", "" );
E_(mvar_AppendNewVar)( p_args->vars, "url_param", E_(mvar_AppendNewVar)( p_args->vars, "url_param",
...@@ -494,9 +542,6 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args, ...@@ -494,9 +542,6 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args,
E_(SSInit)( &p_args->stack ); E_(SSInit)( &p_args->stack );
/* first we load in a temporary buffer */
E_(FileLoad)( f, &p_buffer, &i_buffer );
/* allocate output */ /* allocate output */
*pi_data = i_buffer + 1000; *pi_data = i_buffer + 1000;
dst = *pp_data = malloc( *pi_data ); dst = *pp_data = malloc( *pi_data );
...@@ -510,6 +555,37 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args, ...@@ -510,6 +555,37 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args,
E_(SSClean)( &p_args->stack ); E_(SSClean)( &p_args->stack );
E_(mvar_Delete)( p_args->vars ); E_(mvar_Delete)( p_args->vars );
}
int E_(HttpCallback)( httpd_file_sys_t *p_args,
httpd_file_t *p_file,
uint8_t *_p_request,
uint8_t **_pp_data, int *pi_data )
{
char *p_request = (char *)_p_request;
char **pp_data = (char **)_pp_data;
FILE *f;
if( ( f = fopen( p_args->file, "r" ) ) == NULL )
{
Callback404( p_args, pp_data, pi_data );
return VLC_SUCCESS;
}
if( !p_args->b_html )
{
E_(FileLoad)( f, pp_data, pi_data );
}
else
{
int i_buffer;
char *p_buffer;
/* first we load in a temporary buffer */
E_(FileLoad)( f, &p_buffer, &i_buffer );
ParseExecute( p_args, p_buffer, i_buffer, p_request, pp_data, pi_data );
free( p_buffer ); free( p_buffer );
} }
...@@ -518,3 +594,212 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args, ...@@ -518,3 +594,212 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args,
return VLC_SUCCESS; return VLC_SUCCESS;
} }
/****************************************************************************
* HandlerCallback:
****************************************************************************
* call the external handler and parse vlc macros if Content-Type is HTML
****************************************************************************/
int E_(HandlerCallback)( httpd_handler_sys_t *p_args,
httpd_handler_t *p_handler, uint8_t *_p_url,
uint8_t *_p_request, int i_type,
uint8_t *_p_in, int i_in,
char *psz_remote_addr, char *psz_remote_host,
uint8_t **_pp_data, int *pi_data )
{
char *p_url = (char *)_p_url;
char *p_request = (char *)_p_request;
char **pp_data = (char **)_pp_data;
char *p_in = (char *)p_in;
int i_request = p_request != NULL ? strlen( p_request ) : 0;
char *p;
int i_env = 0;
char **pp_env = NULL;
char *psz_tmp;
char sep;
int i_buffer;
char *p_buffer;
char *psz_cwd;
#ifdef WIN32
sep = '\\';
#else
sep = '/';
#endif
/* Create environment for the CGI */
TAB_APPEND( i_env, pp_env, strdup("GATEWAY_INTERFACE=CGI/1.1") );
TAB_APPEND( i_env, pp_env, strdup("SERVER_PROTOCOL=HTTP/1.1") );
TAB_APPEND( i_env, pp_env, strdup("SERVER_SOFTWARE=" COPYRIGHT_MESSAGE) );
switch( i_type )
{
case HTTPD_MSG_GET:
TAB_APPEND( i_env, pp_env, strdup("REQUEST_METHOD=GET") );
break;
case HTTPD_MSG_POST:
TAB_APPEND( i_env, pp_env, strdup("REQUEST_METHOD=POST") );
break;
case HTTPD_MSG_HEAD:
TAB_APPEND( i_env, pp_env, strdup("REQUEST_METHOD=HEAD") );
break;
default:
break;
}
if( i_request )
{
psz_tmp = malloc( sizeof("QUERY_STRING=") + i_request );
sprintf( psz_tmp, "QUERY_STRING=%s", p_request );
TAB_APPEND( i_env, pp_env, psz_tmp );
psz_tmp = malloc( sizeof("REQUEST_URI=?") + strlen(p_url)
+ i_request );
sprintf( psz_tmp, "REQUEST_URI=%s?%s", p_url, p_request );
TAB_APPEND( i_env, pp_env, psz_tmp );
}
else
{
psz_tmp = malloc( sizeof("REQUEST_URI=") + strlen(p_url) );
sprintf( psz_tmp, "REQUEST_URI=%s", p_url );
TAB_APPEND( i_env, pp_env, psz_tmp );
}
psz_tmp = malloc( sizeof("SCRIPT_NAME=") + strlen(p_url) );
sprintf( psz_tmp, "SCRIPT_NAME=%s", p_url );
TAB_APPEND( i_env, pp_env, psz_tmp );
psz_tmp = malloc( sizeof("SCRIPT_FILENAME=") + strlen(p_args->file.file) );
sprintf( psz_tmp, "SCRIPT_FILENAME=%s", p_args->file.file );
TAB_APPEND( i_env, pp_env, psz_tmp );
#define p_sys p_args->file.p_intf->p_sys
psz_tmp = malloc( sizeof("SERVER_NAME=") + strlen(p_sys->psz_address) );
sprintf( psz_tmp, "SERVER_NAME=%s", p_sys->psz_address );
TAB_APPEND( i_env, pp_env, psz_tmp );
psz_tmp = malloc( sizeof("SERVER_PORT=") + 5 );
sprintf( psz_tmp, "SERVER_PORT=%u", p_sys->i_port );
TAB_APPEND( i_env, pp_env, psz_tmp );
#undef p_sys
if( psz_remote_addr != NULL && *psz_remote_addr )
{
psz_tmp = malloc( sizeof("REMOTE_ADDR=") + strlen(psz_remote_addr) );
sprintf( psz_tmp, "REMOTE_ADDR=%s", psz_remote_addr );
TAB_APPEND( i_env, pp_env, psz_tmp );
}
if( psz_remote_host != NULL && *psz_remote_host )
{
psz_tmp = malloc( sizeof("REMOTE_HOST=") + strlen(psz_remote_host) );
sprintf( psz_tmp, "REMOTE_HOST=%s", psz_remote_host );
TAB_APPEND( i_env, pp_env, psz_tmp );
}
if( i_in )
{
p = p_in;
for ( ; ; )
{
if( !strncmp( p, "Content-Type: ", strlen("Content-Type: ") ) )
{
char *end = strchr( p, '\r' );
if( end == NULL )
break;
*end = '\0';
psz_tmp = malloc( sizeof("CONTENT_TYPE=") + strlen(p) );
sprintf( psz_tmp, "CONTENT_TYPE=%s", p );
TAB_APPEND( i_env, pp_env, psz_tmp );
*end = '\r';
}
if( !strncmp( p, "Content-Length: ", strlen("Content-Length: ") ) )
{
char *end = strchr( p, '\r' );
if( end == NULL )
break;
*end = '\0';
psz_tmp = malloc( sizeof("CONTENT_LENGTH=") + strlen(p) );
sprintf( psz_tmp, "CONTENT_LENGTH=%s", p );
TAB_APPEND( i_env, pp_env, psz_tmp );
*end = '\r';
}
p = strchr( p, '\n' );
if( p == NULL || p[1] == '\r' )
{
p = NULL;
break;
}
p++;
}
}
TAB_APPEND( i_env, pp_env, NULL );
TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
p_args->file.file );
TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
NULL );
psz_tmp = strdup( p_args->file.file );
p = strrchr( psz_tmp, sep );
if( p != NULL )
{
*p = '\0';
psz_cwd = psz_tmp;
}
else
{
free( psz_tmp );
psz_cwd = NULL;
}
if( vlc_execve( p_args->file.p_intf, p_args->p_association->i_argc,
p_args->p_association->ppsz_argv, pp_env, psz_cwd,
(char *)p_in, i_in, &p_buffer, &i_buffer ) == -1 )
{
TAB_REMOVE( p_args->p_association->i_argc,
p_args->p_association->ppsz_argv, NULL );
TAB_REMOVE( p_args->p_association->i_argc,
p_args->p_association->ppsz_argv, p_args->file.file );
if( psz_cwd != NULL )
free( psz_cwd );
Callback404( (httpd_file_sys_t *)p_args, pp_data, pi_data );
return VLC_SUCCESS;
}
TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
NULL );
TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
p_args->file.file );
if( psz_cwd != NULL )
free( psz_cwd );
p = p_buffer;
while( strncmp( p, "Content-Type: text/html",
strlen("Content-Type: text/html") ) )
{
p = strchr( p, '\n' );
if( p == NULL || p[1] == '\r' )
{
p = NULL;
break;
}
p++;
}
if( p == NULL )
{
*pp_data = p_buffer;
*pi_data = i_buffer;
}
else
{
ParseExecute( (httpd_file_sys_t *)p_args, p_buffer, i_buffer,
p_request, pp_data, pi_data );
free( p_buffer );
}
return VLC_SUCCESS;
}
...@@ -334,13 +334,36 @@ struct httpd_file_sys_t ...@@ -334,13 +334,36 @@ struct httpd_file_sys_t
char *file; char *file;
char *name; char *name;
vlc_bool_t b_html; vlc_bool_t b_html, b_handler;
/* inited for each access */ /* inited for each access */
rpn_stack_t stack; rpn_stack_t stack;
mvar_t *vars; mvar_t *vars;
}; };
/** \struct
* Structure associating an extension to an external program
*/
typedef struct http_association_t
{
char *psz_ext;
int i_argc;
char **ppsz_argv;
} http_association_t;
/** \struct
* This structure represent a single CGI file to be parsed by the macros
* handling engine */
struct httpd_handler_sys_t
{
httpd_file_sys_t file;
/* HACK ALERT: this is added below so that casting httpd_handler_sys_t
* to httpd_file_sys_t works */
httpd_handler_t *p_handler;
http_association_t *p_association;
};
/** \struct /** \struct
* Internal service structure for the HTTP interface * Internal service structure for the HTTP interface
*/ */
...@@ -351,11 +374,17 @@ struct intf_sys_t ...@@ -351,11 +374,17 @@ struct intf_sys_t
int i_files; int i_files;
httpd_file_sys_t **pp_files; httpd_file_sys_t **pp_files;
int i_handlers;
http_association_t **pp_handlers;
playlist_t *p_playlist; playlist_t *p_playlist;
input_thread_t *p_input; input_thread_t *p_input;
vlm_t *p_vlm; vlm_t *p_vlm;
char *psz_html_type; char *psz_html_type;
vlc_iconv_t iconv_from_utf8, iconv_to_utf8; vlc_iconv_t iconv_from_utf8, iconv_to_utf8;
char *psz_address;
unsigned short i_port;
}; };
/** This function is the main HTTPD Callback used by the HTTP Interface */ /** This function is the main HTTPD Callback used by the HTTP Interface */
...@@ -363,6 +392,13 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args, ...@@ -363,6 +392,13 @@ int E_(HttpCallback)( httpd_file_sys_t *p_args,
httpd_file_t *, httpd_file_t *,
uint8_t *p_request, uint8_t *p_request,
uint8_t **pp_data, int *pi_data ); uint8_t **pp_data, int *pi_data );
/** This function is the HTTPD Callback used for CGIs */
int E_(HandlerCallback)( httpd_handler_sys_t *p_args,
httpd_handler_t *p_handler, uint8_t *_p_url,
uint8_t *_p_request, int i_type,
uint8_t *_p_in, int i_in,
char *psz_remote_addr, char *psz_remote_host,
uint8_t **_pp_data, int *pi_data );
/**@}*/ /**@}*/
#endif #endif
......
...@@ -205,21 +205,46 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root, ...@@ -205,21 +205,46 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
sprintf( dir, "%s%c%s", psz_dir, sep, p_dir_content->d_name ); sprintf( dir, "%s%c%s", psz_dir, sep, p_dir_content->d_name );
if( E_(ParseDirectory)( p_intf, psz_root, dir ) ) if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
{ {
httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) ); httpd_file_sys_t *f = NULL;
httpd_handler_sys_t *h = NULL;
vlc_bool_t b_index; vlc_bool_t b_index;
char *psz_tmp; char *psz_tmp, *psz_file, *psz_name, *psz_ext;
f->p_intf = p_intf;
f->p_file = NULL;
f->p_redir = NULL;
f->p_redir2 = NULL;
psz_tmp = vlc_fix_readdir_charset( p_intf, dir ); psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
f->file = E_(FromUTF8)( p_intf, psz_tmp ); psz_file = E_(FromUTF8)( p_intf, psz_tmp );
free( psz_tmp ); free( psz_tmp );
psz_tmp = vlc_fix_readdir_charset( p_intf, psz_tmp = vlc_fix_readdir_charset( p_intf,
&dir[strlen( psz_root )] ); &dir[strlen( psz_root )] );
f->name = E_(FileToUrl)( psz_tmp, &b_index ); psz_name = E_(FileToUrl)( psz_tmp, &b_index );
free( psz_tmp ); free( psz_tmp );
psz_ext = strrchr( psz_file, '.' );
if( psz_ext != NULL )
{
int i;
psz_ext++;
for( i = 0; i < p_sys->i_handlers; i++ )
if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
break;
if( i < p_sys->i_handlers )
{
f = malloc( sizeof( httpd_handler_sys_t ) );
h = (httpd_handler_sys_t *)f;
f->b_handler = VLC_TRUE;
h->p_association = p_sys->pp_handlers[i];
}
}
if( f == NULL )
{
f = malloc( sizeof( httpd_file_sys_t ) );
f->b_handler = VLC_FALSE;
}
f->p_intf = p_intf;
f->p_file = NULL;
f->p_redir = NULL;
f->p_redir2 = NULL;
f->file = psz_file;
f->name = psz_name;
f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE; f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
if( !f->name ) if( !f->name )
...@@ -232,16 +257,32 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root, ...@@ -232,16 +257,32 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
msg_Dbg( p_intf, "file=%s (url=%s)", msg_Dbg( p_intf, "file=%s (url=%s)",
f->file, f->name ); f->file, f->name );
if( !f->b_handler )
{
f->p_file = httpd_FileNew( p_sys->p_httpd_host, f->p_file = httpd_FileNew( p_sys->p_httpd_host,
f->name, f->name,
f->b_html ? p_sys->psz_html_type : NULL, f->b_html ? p_sys->psz_html_type :
NULL,
user, password, p_acl, user, password, p_acl,
E_(HttpCallback), f ); E_(HttpCallback), f );
if( f->p_file != NULL )
if( f->p_file )
{ {
TAB_APPEND( p_sys->i_files, p_sys->pp_files, f ); TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
} }
}
else
{
h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
f->name,
user, password, p_acl,
E_(HandlerCallback), h );
if( h->p_handler != NULL )
{
TAB_APPEND( p_sys->i_files, p_sys->pp_files,
(httpd_file_sys_t *)h );
}
}
/* for url that ends by / add /* for url that ends by / add
* - a redirect from rep to rep/ * - a redirect from rep to rep/
* - in case of index.* rep/index.html to rep/ */ * - in case of index.* rep/index.html to rep/ */
......
...@@ -43,6 +43,13 @@ ...@@ -43,6 +43,13 @@
# include <dirent.h> # include <dirent.h>
#endif #endif
#ifdef HAVE_FORK
# include <sys/time.h>
# include <unistd.h>
# include <errno.h>
# include <sys/wait.h>
#endif
/***************************************************************************** /*****************************************************************************
* getenv: just in case, but it should never be called * getenv: just in case, but it should never be called
*****************************************************************************/ *****************************************************************************/
...@@ -810,3 +817,137 @@ char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args ) ...@@ -810,3 +817,137 @@ char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
free( psz_orig ); free( psz_orig );
return argv; return argv;
} }
/*************************************************************************
* vlc_execve: Execute an external program with a given environment,
* wait until it finishes and return its standard output
*************************************************************************/
int __vlc_execve( vlc_object_t *p_object, int i_argc, char **ppsz_argv,
char **ppsz_env, char *psz_cwd, char *p_in, int i_in,
char **pp_data, int *pi_data )
{
#ifdef HAVE_FORK
int pi_stdin[2];
int pi_stdout[2];
pid_t i_child_pid;
pipe( pi_stdin );
pipe( pi_stdout );
if ( (i_child_pid = fork()) == -1 )
{
msg_Err( p_object, "unable to fork (%s)", strerror(errno) );
return -1;
}
if ( i_child_pid == 0 )
{
close(0);
dup(pi_stdin[1]);
close(pi_stdin[0]);
close(1);
dup(pi_stdout[1]);
close(pi_stdout[0]);
close(2);
if ( psz_cwd != NULL )
chdir( psz_cwd );
execve( ppsz_argv[0], ppsz_argv, ppsz_env );
exit(1);
}
close(pi_stdin[1]);
close(pi_stdout[1]);
if ( !i_in )
close( pi_stdin[0] );
*pi_data = 0;
*pp_data = malloc( 1025 ); /* +1 for \0 */
while ( !p_object->b_die )
{
int i_ret, i_status;
fd_set readfds, writefds;
struct timeval tv;
FD_ZERO( &readfds );
FD_ZERO( &writefds );
FD_SET( pi_stdout[0], &readfds );
if ( i_in )
FD_SET( pi_stdin[0], &writefds );
tv.tv_sec = 0;
tv.tv_usec = 10000;
i_ret = select( pi_stdin[0] > pi_stdout[0] ? pi_stdin[0] + 1 :
pi_stdout[0] + 1, &readfds, &writefds, NULL, &tv );
if ( i_ret > 0 )
{
if ( FD_ISSET( pi_stdout[0], &readfds ) )
{
ssize_t i_read = read( pi_stdout[0], &(*pp_data)[*pi_data],
1024 );
if ( i_read > 0 )
{
*pi_data += i_read;
*pp_data = realloc( *pp_data, *pi_data + 1025 );
}
}
if ( FD_ISSET( pi_stdin[0], &writefds ) )
{
ssize_t i_write = write( pi_stdin[0], p_in, __MIN(i_in, 1024) );
if ( i_write > 0 )
{
p_in += i_write;
i_in -= i_write;
}
if ( !i_in )
close( pi_stdin[0] );
}
}
if ( waitpid( i_child_pid, &i_status, WNOHANG ) == i_child_pid )
{
if ( WIFEXITED( i_status ) )
{
if ( WEXITSTATUS( i_status ) )
{
msg_Warn( p_object,
"child %s returned with error code %d",
ppsz_argv[0], WEXITSTATUS( i_status ) );
}
}
else
{
if ( WIFSIGNALED( i_status ) )
{
msg_Warn( p_object,
"child %s quit on signal %d", ppsz_argv[0],
WTERMSIG( i_status ) );
}
}
if ( i_in )
close( pi_stdin[0] );
close( pi_stdout[0] );
break;
}
if ( i_ret < 0 && errno != EINTR )
{
msg_Warn( p_object, "select failed (%s)", strerror(errno) );
}
}
#else
msg_Err( p_intf, "vlc_execve called but no implementation is available" );
return -1;
#endif
(*pp_data)[*pi_data] = '\0';
return 0;
}
...@@ -431,7 +431,7 @@ static const char *httpd_MimeFromUrl( const char *psz_url ) ...@@ -431,7 +431,7 @@ static const char *httpd_MimeFromUrl( const char *psz_url )
} }
/***************************************************************************** /*****************************************************************************
* High Level Funtions: httpd_file_t * High Level Functions: httpd_file_t
*****************************************************************************/ *****************************************************************************/
struct httpd_file_t struct httpd_file_t
{ {
...@@ -449,6 +449,9 @@ struct httpd_file_t ...@@ -449,6 +449,9 @@ struct httpd_file_t
static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query ) static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
{ {
httpd_file_t *file = (httpd_file_t*)p_sys; httpd_file_t *file = (httpd_file_t*)p_sys;
uint8_t *psz_args = query->psz_args;
uint8_t **pp_body, *p_body;
int *pi_body, i_body;
if( answer == NULL || query == NULL ) if( answer == NULL || query == NULL )
{ {
...@@ -466,15 +469,30 @@ static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, ...@@ -466,15 +469,30 @@ static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
if( query->i_type != HTTPD_MSG_HEAD ) if( query->i_type != HTTPD_MSG_HEAD )
{ {
uint8_t *psz_args = query->psz_args; pp_body = &answer->p_body;
pi_body = &answer->i_body;
}
else
{
/* The file still needs to be executed. */
p_body = NULL;
i_body = 0;
pp_body = &p_body;
pi_body = &i_body;
}
if( query->i_type == HTTPD_MSG_POST ) if( query->i_type == HTTPD_MSG_POST )
{ {
/* Check that */ /* msg_Warn not supported */
psz_args = query->p_body;
} }
file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
&answer->i_body ); file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
{
free( p_body );
} }
/* We respect client request */ /* We respect client request */
if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) ) if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
{ {
...@@ -537,6 +555,164 @@ void httpd_FileDelete( httpd_file_t *file ) ...@@ -537,6 +555,164 @@ void httpd_FileDelete( httpd_file_t *file )
free( file ); free( file );
} }
/*****************************************************************************
* High Level Functions: httpd_handler_t (for CGIs)
*****************************************************************************/
struct httpd_handler_t
{
httpd_url_t *url;
httpd_handler_callback_t pf_fill;
httpd_handler_sys_t *p_sys;
};
static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
{
httpd_handler_t *handler = (httpd_handler_t*)p_sys;
uint8_t *psz_args = query->psz_args;
char psz_remote_addr[100];
if( answer == NULL || query == NULL )
{
return VLC_SUCCESS;
}
answer->i_proto = HTTPD_PROTO_NONE;
answer->i_type = HTTPD_MSG_ANSWER;
/* We do it ourselves, thanks */
answer->i_status = 0;
answer->psz_status = NULL;
switch( cl->sock.ss_family )
{
#ifdef HAVE_INET_PTON
case AF_INET:
inet_ntop( cl->sock.ss_family,
&((struct sockaddr_in *)&cl->sock)->sin_addr,
psz_remote_addr, sizeof(psz_remote_addr) );
break;
case AF_INET6:
inet_ntop( cl->sock.ss_family,
&((struct sockaddr_in6 *)&cl->sock)->sin6_addr,
psz_remote_addr, sizeof(psz_remote_addr) );
break;
#else
case AF_INET:
{
char *psz_tmp = inet_ntoa( ((struct sockaddr_in *)&cl->sock)->sin_addr );
strncpy( psz_remote_addr, psz_tmp, sizeof(psz_remote_addr) );
break;
}
#endif
default:
psz_remote_addr[0] = '\0';
}
handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
query->i_type, query->p_body, query->i_body,
psz_remote_addr, NULL,
&answer->p_body, &answer->i_body );
if( query->i_type == HTTPD_MSG_HEAD )
{
char *p = answer->p_body;
while ( (p = strchr( p, '\r' )) != NULL )
{
if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
&& p[3] && p[3] == '\n' )
{
break;
}
}
if( p != NULL )
{
p[4] = '\0';
answer->i_body = strlen(answer->p_body) + 1;
answer->p_body = realloc( answer->p_body, answer->i_body );
}
}
if( strncmp( answer->p_body, "HTTP/1.", 7 ) )
{
int i_status, i_headers;
char *psz_headers, *psz_new, *psz_status;
char psz_code[12];
if( !strncmp( answer->p_body, "Status: ", 8 ) )
{
/* Apache-style */
i_status = strtol( &answer->p_body[8], &psz_headers, 0 );
if( *psz_headers ) psz_headers++;
if( *psz_headers ) psz_headers++;
i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
}
else
{
i_status = 200;
psz_headers = answer->p_body;
i_headers = answer->i_body;
}
switch( i_status )
{
case 200:
psz_status = "OK";
break;
case 401:
psz_status = "Unauthorized";
break;
default:
psz_status = "Undefined";
break;
}
snprintf( psz_code, sizeof(psz_code), "%d", i_status );
answer->i_body = sizeof("HTTP/1.0 \r\n") + strlen(psz_code)
+ strlen(psz_status) + i_headers - 1;
psz_new = malloc( answer->i_body + 1);
sprintf( psz_new, "HTTP/1.0 %s %s\r\n", psz_code, psz_status );
memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
free( answer->p_body );
answer->p_body = psz_new;
}
return VLC_SUCCESS;
}
httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
const char *psz_user,
const char *psz_password,
const vlc_acl_t *p_acl,
httpd_handler_callback_t pf_fill,
httpd_handler_sys_t *p_sys )
{
httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
psz_password, p_acl )
) == NULL )
{
free( handler );
return NULL;
}
handler->pf_fill = pf_fill;
handler->p_sys = p_sys;
httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
(httpd_callback_sys_t*)handler );
httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
(httpd_callback_sys_t*)handler );
httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
(httpd_callback_sys_t*)handler );
return handler;
}
void httpd_HandlerDelete( httpd_handler_t *handler )
{
httpd_UrlDelete( handler->url );
free( handler );
}
/***************************************************************************** /*****************************************************************************
* High Level Functions: httpd_redirect_t * High Level Functions: httpd_redirect_t
*****************************************************************************/ *****************************************************************************/
...@@ -1407,7 +1583,7 @@ static void httpd_ClientRecv( httpd_client_t *cl ) ...@@ -1407,7 +1583,7 @@ static void httpd_ClientRecv( httpd_client_t *cl )
if( cl->query.i_proto == HTTPD_PROTO_NONE ) if( cl->query.i_proto == HTTPD_PROTO_NONE )
{ {
/* enought to see if it's rtp over rtsp or RTSP/HTTP */ /* enough to see if it's rtp over rtsp or RTSP/HTTP */
i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer], i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
4 - cl->i_buffer ); 4 - cl->i_buffer );
if( i_len > 0 ) if( i_len > 0 )
...@@ -1781,7 +1957,7 @@ static void httpd_ClientSend( httpd_client_t *cl ) ...@@ -1781,7 +1957,7 @@ static void httpd_ClientSend( httpd_client_t *cl )
i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer], i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
cl->i_buffer_size - cl->i_buffer ); cl->i_buffer_size - cl->i_buffer );
if( i_len > 0 ) if( i_len >= 0 )
{ {
cl->i_activity_date = mdate(); cl->i_activity_date = mdate();
cl->i_buffer += i_len; cl->i_buffer += i_len;
...@@ -2117,6 +2293,14 @@ static void httpd_HostThread( httpd_host_t *host ) ...@@ -2117,6 +2293,14 @@ static void httpd_HostThread( httpd_host_t *host )
if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) ) if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
{ {
if( answer->i_proto == HTTPD_PROTO_NONE )
{
/* Raw answer from a CGI */
cl->i_buffer = cl->i_buffer_size;
}
else
cl->i_buffer = -1;
/* only one url can answer */ /* only one url can answer */
answer = NULL; answer = NULL;
if( cl->url == NULL ) if( cl->url == NULL )
...@@ -2200,9 +2384,10 @@ static void httpd_HostThread( httpd_host_t *host ) ...@@ -2200,9 +2384,10 @@ static void httpd_HostThread( httpd_host_t *host )
} }
answer->i_body = p - answer->p_body; answer->i_body = p - answer->p_body;
cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body ); httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
} }
cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
cl->i_state = HTTPD_CLIENT_SENDING; cl->i_state = HTTPD_CLIENT_SENDING;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment