Commit a362f5c3 authored by Laurent Aimar's avatar Laurent Aimar

* http: rework of the http interface.

 There is no more hardcoded html page. The interface recursively parse a
directory, exporting all  files. All .htm/.html are  parsed, and special
macro are  used. I will commit  somes samples pages in  share/http and I
hope documentations.

 TODO: - (re)implement access control in httpd (but in a useable way).
       - clean login/password management. (for now .access protects
       a whole directory).
       - doc ;)
parent 16afd989
......@@ -2,7 +2,7 @@
* httpd.h
*****************************************************************************
* Copyright (C) 2001-2003 VideoLAN
* $Id: httpd.h,v 1.5 2003/06/25 15:50:52 fenrir Exp $
* $Id: httpd.h,v 1.6 2003/07/10 22:24:09 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
......@@ -21,8 +21,6 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
typedef struct httpd_t httpd_t;
typedef struct httpd_host_t httpd_host_t;
typedef struct httpd_file_t httpd_file_t;
......@@ -34,6 +32,30 @@ typedef int (*httpd_file_callback)( httpd_file_callback_args_t *p_args, uint8_t
typedef struct httpd_sys_t httpd_sys_t;
enum httpdControl_e
{
HTTPD_GET_HOSTS,
HTTPD_GET_URLS,
HTTPD_GET_CONNECTIONS,
HTTPD_GET_ACL, /* not implemented */
HTTPD_SET_CLOSE,
HTTPD_SET_ACL /* not implemented */
};
typedef struct
{
char *psz_name;
char *psz_value;
} httpd_val_t;
typedef struct
{
int i_count;
httpd_val_t *info;
} httpd_info_t;
struct httpd_t
{
VLC_COMMON_MEMBERS
......@@ -62,31 +84,36 @@ struct httpd_t
httpd_stream_t *,
uint8_t *, int );
void (*pf_unregister_stream) ( httpd_t *, httpd_stream_t * );
int (*pf_control) ( httpd_t *,
int i_query,
void *arg1, void *arg2 );
};
/*
/*****************************************************************************
* httpd_Find:
*
* Return the running httpd instance
* (if none and b_create then a new one is created)
*/
* Return the running httpd instance (if none and b_create then a new one is created)
* httpd_release:
*****************************************************************************/
static inline httpd_t* httpd_Find( vlc_object_t *p_this, vlc_bool_t b_create )
{
httpd_t *p_httpd = NULL;
vlc_value_t lockval;
var_Get( p_this->p_libvlc, "httpd", &lockval );
vlc_mutex_lock( lockval.p_address );
p_httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE );
if( !p_httpd )
if( !p_httpd && b_create)
{
msg_Info(p_this, "creating new http daemon" );
if( !b_create )
{
return( NULL );
}
p_httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD );
if( !p_httpd )
{
msg_Err( p_this, "out of memory" );
vlc_mutex_unlock( lockval.p_address );
return( NULL );
}
......@@ -96,12 +123,14 @@ static inline httpd_t* httpd_Find( vlc_object_t *p_this, vlc_bool_t b_create )
{
msg_Err( p_this, "no suitable httpd module" );
vlc_object_destroy( p_httpd );
vlc_mutex_unlock( lockval.p_address );
return( NULL );
}
vlc_object_yield( p_httpd );
vlc_object_attach( p_httpd, p_this->p_vlc );
}
vlc_mutex_unlock( lockval.p_address );
return( p_httpd );
}
......@@ -118,4 +147,3 @@ static inline void httpd_Release( httpd_t *p_httpd )
vlc_object_destroy( p_httpd );
}
}
/*****************************************************************************
* http.c : http remote control plugin for vlc
* http.c : http mini-server ;)
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: http.c,v 1.10 2003/07/10 18:29:41 zorglub Exp $
* $Id: http.c,v 1.11 2003/07/10 22:24:09 fenrir Exp $
*
* Authors: Gildas Bazin <gbazin@netcourrier.com>
* Laurent Aimar <fenrir@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
......@@ -24,27 +25,33 @@
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdlib.h> /* malloc(), free() */
#include <string.h>
#include <errno.h> /* ENOMEM */
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include <vlc/vlc.h>
#include <vlc/intf.h>
#include "httpd.h"
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
# include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
# include <io.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#if (!defined( WIN32 ) || defined(__MINGW32__))
/* Mingw has its own version of dirent */
# include <dirent.h>
#endif
#include <sys/types.h>
#include "httpd.h"
/*****************************************************************************
* Local prototypes
......@@ -53,240 +60,1586 @@ static int Activate ( vlc_object_t * );
static void Close ( vlc_object_t * );
static void Run ( intf_thread_t *p_intf );
static int httpd_page_interface_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data );
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root, char *psz_dir );
static int http_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data );
static void uri_extract_value( char *psz_uri, char *psz_name,
char *psz_value, int i_value_max );
static void uri_decode_url_encoded( char *psz );
/*****************************************************************************
* intf_sys_t: description and status of interface
*
*****************************************************************************/
typedef struct mvar_s
{
char *name;
char *value;
int i_field;
struct mvar_s **field;
} mvar_t;
#define STACK_MAX 100
typedef struct
{
char *stack[STACK_MAX];
int i_stack;
} stack_t;
struct httpd_file_callback_args_t
{
intf_thread_t *p_intf;
httpd_file_t *p_file;
char *file;
char *name;
char *mime;
/* inited for each access */
stack_t stack;
mvar_t *vars;
};
struct intf_sys_t
{
httpd_t *p_httpd;
httpd_host_t *p_httpd_host;
input_thread_t * p_input;
};
int i_files;
httpd_file_callback_args_t **pp_files;
playlist_t *p_playlist;
input_thread_t *p_input;
};
/*****************************************************************************
* Module descriptor
*****************************************************************************/
#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
"You can set the address and port on which the http interface will bind" )
#define SRC_TEXT N_( "Source directory" )
#define SRC_LONGTEXT N_( "Source directory" )
vlc_module_begin();
set_description( _("HTTP remote control interface") );
add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
#if defined(SYS_DARWIN) || defined(SYS_BEOS) \
|| (defined(WIN32) && !defined(UNDER_CE))
add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
#else
add_string ( "http-src", "share/http", NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
#endif
set_capability( "interface", 0 );
set_callbacks( Activate, Close );
vlc_module_end();
/*****************************************************************************
* Activate: initialize and create stuff
*****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
intf_thread_t *p_intf = (intf_thread_t*)p_this;
intf_sys_t *p_sys;
char *psz_host;
char *psz_address = "";
int i_port = 0;
char *psz_src;
psz_host = config_GetPsz( p_intf, "http-host" );
if( psz_host )
{
char *psz_parser;
psz_address = psz_host;
psz_parser = strchr( psz_host, ':' );
if( psz_parser )
{
*psz_parser++ = '\0';
i_port = atoi( psz_parser );
}
}
if( i_port <= 0 )
{
i_port= 8080;
}
msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
p_sys->p_playlist = NULL;
p_sys->p_input = NULL;
if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
{
msg_Err( p_intf, "cannot create/find httpd" );
free( p_sys );
return VLC_EGENERIC;
}
if( ( p_sys->p_httpd_host =
p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
psz_address, i_port ) ) == NULL )
{
msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
httpd_Release( p_sys->p_httpd );
free( p_sys );
return VLC_EGENERIC;
}
if( psz_host )
{
free( psz_host );
}
p_sys->i_files = 0;
p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
#if defined(SYS_DARWIN) || defined(SYS_BEOS)
if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
{
char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
sprintf( psz_src, "%s/share/http", psz_vlcpath);
}
#elif defined(WIN32) && !defined(UNDER_CE)
if ( (psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
{
char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
psz_src = malloc( strlen(psz_vlcpath) + strlen("\\share\\http") + 1 );
sprintf( psz_src, "%s\\share\\", psz_vlcpath);
}
#else
psz_src = config_GetPsz( p_intf, "http-src" );
#endif
if( !psz_src || *psz_src == '\0' )
{
msg_Err( p_intf, "invalid src dir" );
goto failed;
}
/* remove trainling \ or / */
if( psz_src[strlen( psz_src ) - 1] == '\\' ||
psz_src[strlen( psz_src ) - 1] == '/' )
{
psz_src[strlen( psz_src ) - 1] = '\0';
}
ParseDirectory( p_intf, psz_src, psz_src );
if( p_sys->i_files <= 0 )
{
msg_Err( p_intf, "cannot find any files" );
goto failed;
}
p_intf->pf_run = Run;
return VLC_SUCCESS;
failed:
free( p_sys->pp_files );
p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
p_sys->p_httpd_host );
httpd_Release( p_sys->p_httpd );
free( p_sys );
return VLC_EGENERIC;
}
/*****************************************************************************
* CloseIntf: destroy interface
*****************************************************************************/
void Close ( vlc_object_t *p_this )
{
intf_thread_t *p_intf = (intf_thread_t *)p_this;
intf_sys_t *p_sys = p_intf->p_sys;
int i;
for( i = 0; i < p_sys->i_files; i++ )
{
p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
p_sys->pp_files[i]->p_file );
/* do not free mime */
free( p_sys->pp_files[i]->file );
free( p_sys->pp_files[i]->name );
free( p_sys->pp_files[i] );
}
free( p_sys->pp_files );
p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
p_sys->p_httpd_host );
httpd_Release( p_sys->p_httpd );
free( p_sys );
}
/*****************************************************************************
* Run: http interface thread
*****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
intf_sys_t *p_sys = p_intf->p_sys;
while( !p_intf->b_die )
{
/* get the playlist */
if( p_sys->p_playlist == NULL )
{
p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
}
/* Manage the input part */
if( p_sys->p_input == NULL )
{
if( p_sys->p_playlist )
{
p_sys->p_input =
vlc_object_find( p_sys->p_playlist,
VLC_OBJECT_INPUT,
FIND_CHILD );
}
}
else if( p_sys->p_input->b_dead )
{
vlc_object_release( p_sys->p_input );
p_sys->p_input = NULL;
}
/* Wait a bit */
msleep( INTF_IDLE_SLEEP );
}
if( p_sys->p_input )
{
vlc_object_release( p_sys->p_input );
p_sys->p_input = NULL;
}
if( p_sys->p_playlist )
{
vlc_object_release( p_sys->p_playlist );
p_sys->p_playlist = NULL;
}
}
/*****************************************************************************
* Local functions
*****************************************************************************/
#define MAX_DIR_SIZE 10240
/****************************************************************************
* FileToUrl: create a good name for an url from filename
****************************************************************************/
static char *FileToUrl( char *name )
{
char *url, *p;
url = p = malloc( strlen( name ) + 1 );
#ifdef WIN32
while( *name == '\\' )
{
name++;
}
*p++ = '/';
while( *name )
{
if( *name == '\\' )
{
*p++ = '/';
}
else
{
*p++ = *name;
}
name++;
}
*p++ = '\0';
#else
while( name[0] == '/' )
{
name++;
}
*p++ = '/';
strcpy( p, name );
#endif
/* index.* -> / */
if( ( p = strrchr( url, '/' ) ) != NULL )
{
if( !strncmp( p, "/index.", 7 ) )
{
p[1] = '\0';
}
}
return url;
}
/****************************************************************************
* FileToMime: XXX duplicated with modules/access_out/http.c
****************************************************************************/
static struct
{
char *psz_ext;
char *psz_mime;
} http_mime[] =
{
{ ".htm", "text/html" },
{ ".html", "text/html" },
/* media mime */
{ ".avi", "video/avi" },
{ ".asf", "video/x-ms-asf" },
{ ".m1a", "audio/mpeg" },
{ ".m2a", "audio/mpeg" },
{ ".m1v", "video/mpeg" },
{ ".m2v", "video/mpeg" },
{ ".mp2", "audio/mpeg" },
{ ".mp3", "audio/mpeg" },
{ ".mpa", "audio/mpeg" },
{ ".mpg", "video/mpeg" },
{ ".mpeg", "video/mpeg" },
{ ".mpe", "video/mpeg" },
{ ".mov", "video/quicktime" },
{ ".moov", "video/quicktime" },
{ ".ogg", "application/ogg" },
{ ".ogm", "application/ogg" },
{ ".wav", "audio/wav" },
/* end */
{ NULL, NULL }
};
static char *FileToMime( char *psz_name )
{
char *psz_ext;
psz_ext = strrchr( psz_name, '.' );
if( psz_ext )
{
int i;
for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
{
if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
{
return( http_mime[i].psz_mime );
}
}
}
return( "application/octet-stream" );
}
/****************************************************************************
* ParseDirectory: parse recursively a directory, adding each file
****************************************************************************/
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root, char *psz_dir )
{
intf_sys_t *p_sys = p_intf->p_sys;
char dir[MAX_DIR_SIZE];
#ifdef HAVE_SYS_STAT_H
struct stat stat_info;
#endif
DIR *p_dir;
struct dirent *p_dir_content;
FILE *file;
char *user = NULL;
char *password = NULL;
#ifdef HAVE_SYS_STAT_H
if( ( stat( psz_dir, &stat_info ) == -1 ) || !S_ISDIR( stat_info.st_mode ) )
{
return VLC_EGENERIC;
}
#endif
if( ( p_dir = opendir( psz_dir ) ) == NULL )
{
msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
return VLC_EGENERIC;
}
msg_Dbg( p_intf, "dir=%s", psz_dir );
sprintf( dir, "%s/.access", psz_dir );
if( ( file = fopen( dir, "r" ) ) != NULL )
{
char line[1024];
int i_size;
msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
i_size = fread( line, 1, 1023, file );
if( i_size > 0 )
{
char *p;
while( i_size > 0 && ( line[i_size-1] == '\n' || line[i_size-1] == '\r' ) )
{
i_size--;
}
line[i_size] = '\0';
p = strchr( line, ':' );
if( p )
{
*p++ = '\0';
user = strdup( line );
password = strdup( p );
}
}
msg_Dbg( p_intf, "using user=%s password=%s (read=%d)", user, password, i_size );
fclose( file );
}
for( ;; )
{
/* parse psz_src dir */
if( ( p_dir_content = readdir( p_dir ) ) == NULL )
{
break;
}
if( p_dir_content->d_name[0] == '.' )
{
continue;
}
sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
if( ParseDirectory( p_intf, psz_root, dir ) )
{
#define f p_sys->pp_files[p_sys->i_files]
f = malloc( sizeof( httpd_file_callback_args_t ) );
f->p_intf = p_intf;
f->file = strdup( dir );
f->name = FileToUrl( &dir[strlen( psz_root )] );
f->mime = FileToMime( &dir[strlen( psz_root )] );
msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name, f->mime );
f->p_file =
p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
f->name, f->mime,
user, password,
http_get, http_get,
f );
if( f->p_file )
{
p_sys->i_files++;
p_sys->pp_files = realloc( p_sys->pp_files, (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
}
#define fold p_sys->pp_files[p_sys->i_files-1]
/* FIXME for rep/ add rep (it would be better to do a redirection) */
if( strlen(fold->name) > 1 && fold->name[strlen(fold->name) - 1] == '/' )
{
f = malloc( sizeof( httpd_file_callback_args_t ) );
f->p_intf = p_intf;
f->file = strdup( fold->file );
f->name = strdup( fold->name );
f->mime = fold->mime;
f->name[strlen(f->name) - 1] = '\0';
msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name, f->mime );
f->p_file =
p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
f->name, f->mime,
user, password,
http_get, http_get,
f );
if( f->p_file )
{
p_sys->i_files++;
p_sys->pp_files = realloc( p_sys->pp_files, (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
}
}
#undef fold
#undef f
}
}
if( user )
{
free( user );
}
if( password )
{
free( password );
}
return VLC_SUCCESS;
}
/****************************************************************************
* var and set handling
****************************************************************************/
static mvar_t *mvar_New( char *name, char *value )
{
mvar_t *v = malloc( sizeof( mvar_t ) );
v->name = strdup( name );
v->value = strdup( value ? value : "" );
v->i_field = 0;
v->field = malloc( sizeof( mvar_t * ) );
v->field[0] = NULL;
return v;
}
static void mvar_Delete( mvar_t *v )
{
int i;
free( v->name );
free( v->value );
for( i = 0; i < v->i_field; i++ )
{
mvar_Delete( v->field[i] );
}
free( v->field );
free( v );
}
static void mvar_AppendVar( mvar_t *v, mvar_t *f )
{
v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
v->field[v->i_field] = f;
v->i_field++;
}
static mvar_t *mvar_Duplicate( mvar_t *v )
{
int i;
mvar_t *n;
n = mvar_New( v->name, v->value );
for( i = 0; i < v->i_field; i++ )
{
mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
}
return n;
}
static void mvar_PushVar( mvar_t *v, mvar_t *f )
{
v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
if( v->i_field > 0 )
{
memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
}
v->field[0] = f;
v->i_field++;
}
static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
{
int i;
for( i = 0; i < v->i_field; i++ )
{
if( v->field[i] == f )
{
break;
}
}
if( i >= v->i_field )
{
return;
}
if( i + 1 < v->i_field )
{
memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
}
v->i_field--;
/* FIXME should do a realloc */
}
static mvar_t *mvar_GetVar( mvar_t *s, char *name )
{
int i;
char base[512], *field, *p;
int i_index;
/* format: name[index].field */
field = strchr( name, '.' );
if( field )
{
int i = field - name;
strncpy( base, name, i );
base[i] = '\0';
field++;
}
else
{
strcpy( base, name );
}
if( ( p = strchr( base, '[' ) ) )
{
*p++ = '\0';
sscanf( p, "%d]", &i_index );
if( i_index < 0 )
{
return NULL;
}
}
else
{
i_index = 0;
}
for( i = 0; i < s->i_field; i++ )
{
if( !strcmp( s->field[i]->name, base ) )
{
if( i_index > 0 )
{
i_index--;
}
else
{
if( field )
{
return mvar_GetVar( s->field[i], field );
}
else
{
return s->field[i];
}
}
}
}
return NULL;
}
static char *mvar_GetValue( mvar_t *v, char *field )
{
if( *field == '\0' )
{
return v->value;
}
else
{
mvar_t *f = mvar_GetVar( v, field );
if( f )
{
return f->value;
}
else
{
return "";
}
}
}
static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
{
mvar_t *f = mvar_New( name, value );
mvar_PushVar( vars, f );
}
static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
{
mvar_t *f = mvar_New( name, value );
mvar_AppendVar( vars, f );
}
/* arg= start[:stop[:step]],.. */
static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
{
char *dup = strdup( arg );
char *str = dup;
mvar_t *s = mvar_New( name, "set" );
fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
while( str )
{
char *p;
int i_start,i_stop,i_step;
int i_match;
p = strchr( str, ',' );
if( p )
{
*p++ = '\0';
}
i_step = 0;
i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
if( i_match == 1 )
{
i_stop = i_start;
i_step = 1;
}
else if( i_match == 2 )
{
i_step = i_start < i_stop ? 1 : -1;
}
if( i_match >= 1 )
{
int i;
if( ( i_start < i_stop && i_step > 0 ) ||
( i_start > i_stop && i_step < 0 ) )
{
for( i = i_start; ; i += i_step )
{
char value[512];
if( ( i_step > 0 && i > i_stop ) ||
( i_step < 0 && i < i_stop ) )
{
break;
}
fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
sprintf( value, "%d", i );
mvar_PushNewVar( s, name, value );
}
}
}
str = p;
}
free( dup );
return s;
}
static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
{
mvar_t *s = mvar_New( name, "set" );
int i;
fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
vlc_mutex_lock( &p_pl->object_lock );
for( i = 0; i < p_pl->i_size; i++ )
{
mvar_t *itm = mvar_New( name, "set" );
char value[512];
sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
mvar_AppendNewVar( itm, "current", value );
sprintf( value, "%d", i );
mvar_AppendNewVar( itm, "index", value );
mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
mvar_AppendVar( s, itm );
}
vlc_mutex_unlock( &p_pl->object_lock );
return s;
}
static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
{
mvar_t *s = mvar_New( name, "set" );
input_info_category_t * p_category;
input_info_t * p_info;
fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
if( p_input == NULL )
{
return s;
}
vlc_mutex_lock( &p_input->stream.stream_lock );
p_category = p_input->stream.p_info;
while ( p_category )
{
mvar_t *cat = mvar_New( name, "set" );
mvar_t *iset = mvar_New( "info", "set" );
mvar_AppendNewVar( cat, "name", p_category->psz_name );
mvar_AppendVar( cat, iset );
p_info = p_category->p_info;
while ( p_info )
{
mvar_t *info = mvar_New( "info", "" );
msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
mvar_AppendNewVar( info, "name", p_info->psz_name );
mvar_AppendNewVar( info, "value", p_info->psz_value );
mvar_AppendVar( iset, info );
p_info = p_info->p_next;
}
mvar_AppendVar( s, cat );
p_category = p_category->p_next;
}
vlc_mutex_unlock( &p_input->stream.stream_lock );
return s;
}
static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
{
mvar_t *s = mvar_New( name, "set" );
httpd_info_t info;
int i;
fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
{
for( i= 0; i < info.i_count; )
{
mvar_t *inf;
inf = mvar_New( name, "set" );
do
{
/* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
info.info[i].psz_name, info.info[i].psz_value ); */
mvar_AppendNewVar( inf,
info.info[i].psz_name,
info.info[i].psz_value );
i++;
} while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
mvar_AppendVar( s, inf );
}
}
/* free mem */
for( i = 0; i < info.i_count; i++ )
{
free( info.info[i].psz_name );
free( info.info[i].psz_value );
}
return s;
}
static void SSInit( stack_t * );
static void SSClean( stack_t * );
static void EvaluateRPN( mvar_t *, stack_t *, char * );
static void SSPush ( stack_t *, char * );
static char *SSPop ( stack_t * );
static void SSPushN ( stack_t *, int );
static int SSPopN ( stack_t *, mvar_t * );
/****************************************************************************
* Macro handling
****************************************************************************/
typedef struct
{
char *id;
char *param1;
char *param2;
} macro_t;
static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
{
int i_read;
/* just load the file */
*pi_data = 0;
*pp_data = malloc( 1025 ); /* +1 for \0 */
while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
{
*pi_data += 1024;
*pp_data = realloc( *pp_data, *pi_data + 1025 );
}
if( i_read > 0 )
{
*pi_data += i_read;
}
(*pp_data)[*pi_data] = '\0';
return VLC_SUCCESS;
}
static int MacroParse( macro_t *m, uint8_t *psz_src )
{
uint8_t *dup = strdup( psz_src );
uint8_t *src = dup;
uint8_t *p;
int i_skip;
#define EXTRACT( name, l ) \
src += l; \
p = strchr( src, '"' ); \
if( p ) \
{ \
*p++ = '\0'; \
} \
m->name = strdup( src ); \
if( !p ) \
{ \
break; \
} \
src = p;
/* init m */
m->id = NULL;
m->param1 = NULL;
m->param2 = NULL;
/* parse */
src += 4;
while( *src )
{
while( *src == ' ')
{
src++;
}
if( !strncmp( src, "id=\"", 4 ) )
{
EXTRACT( id, 4 );
}
else if( !strncmp( src, "param1=\"", 8 ) )
{
EXTRACT( param1, 8 );
}
else if( !strncmp( src, "param2=\"", 8 ) )
{
EXTRACT( param2, 8 );
}
else
{
break;
}
}
if( strstr( src, "/>" ) )
{
src = strstr( src, "/>" ) + 2;
}
else
{
src += strlen( src );
}
if( m->id == NULL )
{
m->id = strdup( "" );
}
if( m->param1 == NULL )
{
m->param1 = strdup( "" );
}
if( m->param2 == NULL )
{
m->param2 = strdup( "" );
}
i_skip = src - dup;
free( dup );
return i_skip;
#undef EXTRACT
}
/*****************************************************************************
* Module descriptor
*****************************************************************************/
#define PORT_TEXT N_( "HTTP interface bind port" )
#define PORT_LONGTEXT N_( \
"You can set the port on which the http interface will accept connections" )
#define ADDR_TEXT N_( "HTTP interface bind address" )
#define ADDR_LONGTEXT N_( \
"You can set the address on which the http interface will bind" )
#define USER_TEXT N_( "Username" )
#define USER_LONGTEXT N_( \
"You can set the username that will be allowed to connect \
If you do not set it, everyone is allowed without password" )
#define PASS_TEXT N_( "Password" )
#define PASS_LONGTEXT N_( \
"You can set the password that corresponds to the username \
If you do not set it, the password is left blank" )
static void MacroClean( macro_t *m )
{
free( m->id );
free( m->param1 );
free( m->param2 );
}
enum macroType
{
MVLC_UNKNOWN = 0,
MVLC_CONTROL,
MVLC_PLAY,
MVLC_STOP,
MVLC_PAUSE,
MVLC_NEXT,
MVLC_PREVIOUS,
MVLC_ADD,
MVLC_CLOSE,
MVLC_SHUTDOWN,
MVLC_FOREACH,
MVLC_IF,
MVLC_RPN,
MVLC_ELSE,
MVLC_END,
MVLC_GET,
MVLC_SET,
MVLC_INT,
MVLC_FLOAT,
MVLC_STRING,
MVLC_VALUE
};
vlc_module_begin();
add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
add_string( "http-addr", NULL, NULL, ADDR_TEXT, ADDR_LONGTEXT, VLC_TRUE );
add_integer( "http-port", 8080, NULL, PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_TRUE );
add_string( "http-pass", NULL, NULL, PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
set_description( _("HTTP remote control interface") );
set_capability( "interface", 10 );
set_callbacks( Activate, Close );
vlc_module_end();
static struct
{
char *psz_name;
int i_type;
}
StrToMacroTypeTab [] =
{
{ "control", MVLC_CONTROL },
/* player control */
{ "play", MVLC_PLAY },
{ "stop", MVLC_STOP },
{ "pause", MVLC_PAUSE },
{ "next", MVLC_NEXT },
{ "prevous", MVLC_PREVIOUS },
{ "add", MVLC_ADD },
/* admin control */
{ "close", MVLC_CLOSE },
{ "shutdown", MVLC_SHUTDOWN },
{ "rpn", MVLC_RPN },
{ "foreach", MVLC_FOREACH },
{ "value", MVLC_VALUE },
{ "if", MVLC_IF },
{ "else", MVLC_ELSE },
{ "end", MVLC_END },
{ "get", MVLC_GET },
{ "set", MVLC_SET },
{ "int", MVLC_INT },
{ "float", MVLC_FLOAT },
{ "string", MVLC_STRING },
/* end */
{ NULL, MVLC_UNKNOWN }
};
/*****************************************************************************
* Activate: initialize and create stuff
*****************************************************************************/
static int Activate( vlc_object_t *p_this )
static int StrToMacroType( char *name )
{
intf_thread_t *p_intf = (intf_thread_t*)p_this;
int i;
/* Allocate instance and initialize some members */
p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
if( p_intf->p_sys == NULL )
if( !name || *name == '\0')
{
return VLC_EGENERIC;
};
p_intf->p_sys->p_httpd = NULL;
p_intf->p_sys->p_httpd_host = NULL;
p_intf->pf_run = Run;
CONSOLE_INTRO_MSG;
return VLC_SUCCESS;
return MVLC_UNKNOWN;
}
for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
{
if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
{
return StrToMacroTypeTab[i].i_type;
}
}
return MVLC_UNKNOWN;
}
/*****************************************************************************
* CloseIntf: destroy interface
*****************************************************************************/
void Close ( vlc_object_t *p_this )
static void MacroDo( httpd_file_callback_args_t *p_args,
macro_t *m,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data,
uint8_t **pp_dst )
{
intf_thread_t *p_intf = (intf_thread_t *)p_this;
intf_thread_t *p_intf = p_args->p_intf;
intf_sys_t *p_sys = p_args->p_intf->p_sys;
char control[512];
#define ALLOC( l ) \
{ \
int __i__ = *pp_dst - *pp_data; \
*pi_data += (l); \
*pp_data = realloc( *pp_data, *pi_data ); \
*pp_dst = (*pp_data) + __i__; \
}
#define PRINT( str ) \
ALLOC( strlen( str ) + 1 ); \
*pp_dst += sprintf( *pp_dst, str );
if( p_intf->p_sys->p_httpd_host )
p_intf->p_sys->p_httpd->pf_unregister_host( p_intf->p_sys->p_httpd,
p_intf->p_sys->p_httpd_host );
#define PRINTS( str, s ) \
ALLOC( strlen( str ) + strlen( s ) + 1 ); \
*pp_dst += sprintf( *pp_dst, str, s );
if( p_intf->p_sys->p_httpd )
httpd_Release( p_intf->p_sys->p_httpd );
switch( StrToMacroType( m->id ) )
{
case MVLC_CONTROL:
if( i_request <= 0 )
{
break;
}
uri_extract_value( p_request, "control", control, 512 );
if( *m->param1 && !strstr( m->param1, control ) )
{
msg_Warn( p_intf, "unauthorized control=%s", control );
break;
}
switch( StrToMacroType( control ) )
{
case MVLC_PLAY:
{
int i_item;
char item[512];
uri_extract_value( p_request, "item", item, 512 );
i_item = atoi( item );
playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
msg_Dbg( p_intf, "requested playlist item: %i", i_item );
break;
}
case MVLC_STOP:
playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
msg_Dbg( p_intf, "requested playlist stop" );
break;
case MVLC_PAUSE:
playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
msg_Dbg( p_intf, "requested playlist pause" );
break;
case MVLC_NEXT:
playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
p_sys->p_playlist->i_index + 1 );
msg_Dbg( p_intf, "requested playlist next" );
break;
case MVLC_PREVIOUS:
playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
p_sys->p_playlist->i_index - 1 );
msg_Dbg( p_intf, "requested playlist next" );
break;
case MVLC_ADD:
{
char mrl[512];
uri_extract_value( p_request, "mrl", mrl, 512 );
uri_decode_url_encoded( mrl );
playlist_Add( p_sys->p_playlist, mrl,
PLAYLIST_APPEND, PLAYLIST_END );
msg_Dbg( p_intf, "requested playlist add: %s", mrl );
break;
}
/* admin function */
case MVLC_CLOSE:
{
char id[512];
uri_extract_value( p_request, "id", id, 512 );
msg_Dbg( p_intf, "requested close id=%s", id );
if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
{
msg_Warn( p_intf, "close failed for id=%s", id );
}
break;
}
case MVLC_SHUTDOWN:
{
msg_Dbg( p_intf, "requested shutdown" );
p_intf->p_vlc->b_die = VLC_TRUE;
break;
}
default:
PRINTS( "<!-- control param(%s) unsuported -->", control );
break;
}
break;
/* Destroy structure */
free( p_intf->p_sys );
}
case MVLC_SET:
{
char value[512];
int i;
float f;
/*****************************************************************************
* Run: http interface thread
*****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
input_thread_t *p_input = NULL;
playlist_t *p_playlist = NULL;
httpd_file_t *p_page_intf;
if( i_request <= 0 ||
*m->param1 == '\0' ||
strstr( p_request, m->param1 ) == NULL )
{
break;
}
uri_extract_value( p_request, m->param1, value, 512 );
uri_decode_url_encoded( value );
#if 0
input_info_category_t * p_category;
input_info_t * p_info;
switch( StrToMacroType( m->param2 ) )
{
case MVLC_INT:
i = atoi( value );
config_PutInt( p_intf, m->param1, i );
break;
case MVLC_FLOAT:
f = atof( value );
config_PutFloat( p_intf, m->param1, f );
break;
case MVLC_STRING:
config_PutPsz( p_intf, m->param1, value );
break;
default:
PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
}
break;
}
case MVLC_GET:
{
char value[512];
int i;
float f;
char *psz;
off_t i_oldpos = 0;
off_t i_newpos;
double f_ratio = 1.0;
#endif
if( *m->param1 == '\0' )
{
break;
}
/* Get bind address and port */
char *psz_bind_addr = config_GetPsz( p_intf, "http-addr" );
int i_bind_port = config_GetInt( p_intf, "http-port" );
switch( StrToMacroType( m->param2 ) )
{
case MVLC_INT:
i = config_GetInt( p_intf, m->param1 );
sprintf( value, "%i", i );
break;
case MVLC_FLOAT:
f = config_GetFloat( p_intf, m->param1 );
sprintf( value, "%f", f );
break;
case MVLC_STRING:
psz = config_GetPsz( p_intf, m->param1 );
sprintf( value, "%s", psz ? psz : "" );
break;
default:
sprintf( value, "invalid type(%s) in set", m->param2 );
break;
}
msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
PRINTS( "%s", value );
break;
}
case MVLC_VALUE:
{
char *s, *v;
char *psz_user = config_GetPsz( p_intf, "http-user" );
char *psz_pass = config_GetPsz( p_intf, "http-pass" );
if( !psz_bind_addr ) psz_bind_addr = strdup( "" );
EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
s = SSPop( &p_args->stack );
v = mvar_GetValue( p_args->vars, s );
p_intf->p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE );
if( !p_intf->p_sys->p_httpd )
{
msg_Err( p_intf, "cannot start httpd daemon" );
free( p_intf->p_sys );
return;
PRINTS( "%s", v );
free( s );
break;
}
case MVLC_RPN:
EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
break;
case MVLC_UNKNOWN:
default:
PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
break;
}
#undef PRINTS
#undef PRINT
#undef ALLOC
}
static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
{
int i_id;
int i_level = 0;
p_intf->p_sys->p_httpd_host =
p_intf->p_sys->p_httpd->pf_register_host( p_intf->p_sys->p_httpd,
psz_bind_addr, i_bind_port );
if( !p_intf->p_sys->p_httpd_host )
while( src < end )
{
msg_Err( p_intf, "cannot listen on %s:%d", psz_bind_addr, i_bind_port);
httpd_Release( p_intf->p_sys->p_httpd );
free( p_intf->p_sys );
return;
}
if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
{
int i_skip;
macro_t m;
msg_Info( p_intf, "http interface started on %s:%d",
*psz_bind_addr ? psz_bind_addr : "localhost" , i_bind_port );
/*
* Register our interface page with the httpd daemon
*/
if( !psz_user )
{
psz_pass = NULL; /* No password if no user given */
}
if( psz_user && !psz_pass)
{
psz_pass="";
}
p_page_intf = p_intf->p_sys->p_httpd->pf_register_file(
p_intf->p_sys->p_httpd, "/", "text/html",
psz_user, psz_pass, httpd_page_interface_get,
httpd_page_interface_get,
(httpd_file_callback_args_t*)p_intf );
i_skip = MacroParse( &m, src );
while( !p_intf->b_die )
{
i_id = StrToMacroType( m.id );
/* Manage the input part */
if( p_input == NULL )
{
if( p_playlist )
switch( i_id )
{
p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
FIND_CHILD );
case MVLC_IF:
case MVLC_FOREACH:
i_level++;
break;
case MVLC_END:
i_level--;
break;
default:
break;
}
else
if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
{
p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
FIND_ANYWHERE );
if( p_input )
{
p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
FIND_PARENT );
}
return src + ( b_after ? i_skip : 0 );
}
else if( i_level < 0 )
{
return NULL;
}
src += i_skip;
}
else if( p_input->b_dead )
else
{
vlc_object_release( p_input );
p_input = NULL;
src++;
}
}
return NULL;
}
static void Execute( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data,
uint8_t **pp_dst,
uint8_t *_src, uint8_t *_end )
{
intf_thread_t *p_intf = p_args->p_intf;
uint8_t *src, *dup, *end;
uint8_t *dst = *pp_dst;
src = dup = malloc( _end - _src + 1 );
end = src +( _end - _src );
memcpy( src, _src, _end - _src );
*end = '\0';
if( p_input )
/* we parse searching <vlc */
while( src < end )
{
uint8_t *p;
int i_copy;
p = strstr( src, "<vlc" );
if( p < end && p == src )
{
/* Get position */
vlc_mutex_lock( &p_input->stream.stream_lock );
if( !p_input->b_die && p_input->stream.i_mux_rate )
{
#if 0
#define A p_input->stream.p_selected_area
f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
i_newpos = A->i_tell * f_ratio;
macro_t m;
src += MacroParse( &m, src );
if( i_oldpos != i_newpos )
//msg_Dbg( p_intf, "macro_id=%s", m.id );
switch( StrToMacroType( m.id ) )
{
case MVLC_IF:
{
i_oldpos = i_newpos;
printf( "pos: %li s / %li s\n", (long int)i_newpos,
(long int)(f_ratio * A->i_size) );
vlc_bool_t i_test;
uint8_t *endif;
EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
if( SSPopN( &p_args->stack, p_args->vars ) )
{
i_test = 1;
}
else
{
i_test = 0;
}
endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
if( i_test == 0 )
{
uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
if( start )
{
uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
if( stop )
{
Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
}
}
}
else if( i_test == 1 )
{
uint8_t *stop;
if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
{
stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
}
if( stop )
{
Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
}
}
src = endif;
break;
}
#undef S
#endif
case MVLC_FOREACH:
{
uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
uint8_t *start = src;
uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
if( stop )
{
mvar_t *index;
int i_idx;
mvar_t *v;
if( !strncmp( m.param2, "integer=", 8 ) )
{
index = mvar_IntegerSetNew( m.param1, &m.param2[8] );
}
else if( !strcmp( m.param2, "playlist" ) )
{
index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
}
else if( !strcmp( m.param2, "informations" ) )
{
index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
}
else if( !strcmp( m.param2, "hosts" ) )
{
index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
}
else if( !strcmp( m.param2, "urls" ) )
{
index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
}
else if( !strcmp( m.param2, "connections" ) )
{
index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
}
else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
{
index = mvar_Duplicate( v );
}
else
{
msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
src = endfor;
break;
}
for( i_idx = 0; i_idx < index->i_field; i_idx++ )
{
mvar_t *f = mvar_Duplicate( index->field[i_idx] );
//msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
free( f->name );
f->name = strdup( m.param1 );
mvar_PushVar( p_args->vars, f );
Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
mvar_RemoveVar( p_args->vars, f );
mvar_Delete( f );
}
mvar_Delete( index );
src = endfor;
}
break;
}
default:
MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
break;
}
vlc_mutex_unlock( &p_input->stream.stream_lock );
MacroClean( &m );
continue;
}
/* Wait a bit */
msleep( INTF_IDLE_SLEEP );
i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
if( i_copy > 0 )
{
int i_index = dst - *pp_data;
*pi_data += i_copy;
*pp_data = realloc( *pp_data, *pi_data );
dst = (*pp_data) + i_index;
memcpy( dst, src, i_copy );
dst += i_copy;
src += i_copy;
}
}
p_intf->p_sys->p_httpd->pf_unregister_file( p_intf->p_sys->p_httpd,
p_page_intf );
*pp_dst = dst;
free( dup );
}
/****************************************************************************
* http_get:
****************************************************************************
* a file with mime == text/html is parsed and all "macro" replaced
* <vlc id="macro name" [param1="" [param2=""]] />
* valid id are
*
****************************************************************************/
static int http_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
{
char *p;
FILE *f;
if( p_input )
if( ( f = fopen( p_args->file, "r" ) ) == NULL )
{
vlc_object_release( p_input );
p_input = NULL;
p = *pp_data = malloc( 10240 );
p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
p += sprintf( p, "</html>\n" );
*pi_data = strlen( *pp_data ) + 1;
return VLC_SUCCESS;
}
if( p_playlist )
if( strcmp( p_args->mime, "text/html" ) )
{
vlc_object_release( p_playlist );
p_playlist = NULL;
FileLoad( f, pp_data, pi_data );
}
}
else
{
int i_buffer;
uint8_t *p_buffer;
uint8_t *dst;
/*****************************************************************************
* Local functions
*****************************************************************************/
static int httpd_page_interface_update( intf_thread_t *p_intf,
playlist_t *p_playlist,
uint8_t **pp_data, int *pi_data, vlc_bool_t b_redirect );
p_args->vars = mvar_New( "variables", "" );
mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
SSInit( &p_args->stack );
/* first we load in a temporary buffer */
FileLoad( f, &p_buffer, &i_buffer );
/* allocate output */
*pi_data = i_buffer + 1000;
dst = *pp_data = malloc( *pi_data );
/* we parse executing all <vlc /> macros */
Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
*dst++ = '\0';
*pi_data = dst - *pp_data;
SSClean( &p_args->stack );
mvar_Delete( p_args->vars );
}
fclose( f );
return VLC_SUCCESS;
}
/****************************************************************************
* uri parser
****************************************************************************/
static void uri_extract_value( char *psz_uri, char *psz_name,
char *psz_value, int i_value_max )
{
......@@ -370,160 +1723,282 @@ static void uri_decode_url_encoded( char *psz )
free( dup );
}
static int httpd_page_interface_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
/****************************************************************************
* Light RPN evaluator
****************************************************************************/
static void SSInit( stack_t *st )
{
st->i_stack = 0;
}
static void SSClean( stack_t *st )
{
intf_thread_t *p_intf = (intf_thread_t *)p_args;
playlist_t *p_playlist;
int i_ret;
while( st->i_stack > 0 )
{
free( st->stack[--st->i_stack] );
}
}
p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
if( !p_playlist )
static void SSPush( stack_t *st, char *s )
{
if( st->i_stack < STACK_MAX )
{
return VLC_EGENERIC;
st->stack[st->i_stack++] = strdup( s );
}
}
static char * SSPop( stack_t *st )
{
if( st->i_stack <= 0 )
{
return strdup( "" );
}
else
{
return st->stack[--st->i_stack];
}
}
static int SSPopN( stack_t *st, mvar_t *vars )
{
char *name;
char *value;
char *end;
int i;
name = SSPop( st );
i = strtol( name, &end, 0 );
if( end == name )
{
value = mvar_GetValue( vars, name );
i = atoi( value );
}
free( name );
return( i );
}
if( i_request > 0)
static void SSPushN( stack_t *st, int i )
{
char v[512];
sprintf( v, "%d", i );
SSPush( st, v );
}
static void EvaluateRPN( mvar_t *vars, stack_t *st, char *exp )
{
for( ;; )
{
char action[512];
char s[100], *p;
uri_extract_value( p_request, "action", action, 512 );
/* skip spcae */
while( *exp == ' ' )
{
exp++;
}
if( !strcmp( action, "play" ) )
if( *exp == '\'' )
{
int i_item;
uri_extract_value( p_request, "item", action, 512 );
i_item = atol( action );
playlist_Command( p_playlist, PLAYLIST_GOTO, i_item );
msg_Dbg( p_intf, "requested playlist item: %i", i_item );
/* extract string */
p = &s[0];
while( *exp && *exp != '\'' )
{
*p++ = *exp++;
}
*p = '\0';
SSPush( st, s );
continue;
}
else if( !strcmp( action, "stop" ) )
/* extract token */
p = strchr( exp, ' ' );
if( !p )
{
playlist_Command( p_playlist, PLAYLIST_STOP, 0 );
msg_Dbg( p_intf, "requested playlist stop" );
strcpy( s, exp );
exp += strlen( exp );
}
else if( !strcmp( action, "pause" ) )
else
{
playlist_Command( p_playlist, PLAYLIST_PAUSE, 0 );
msg_Dbg( p_intf, "requested playlist pause" );
int i = p -exp;
strncpy( s, exp, i );
s[i] = '\0';
exp = p + 1;
}
else if( !strcmp( action, "next" ) )
if( *s == '\0' )
{
playlist_Command( p_playlist, PLAYLIST_GOTO,
p_playlist->i_index + 1 );
msg_Dbg( p_intf, "requested playlist next" );
break;
}
else if( !strcmp( action, "previous" ) )
/* 1. Integer function */
if( !strcmp( s, "!" ) )
{
playlist_Command( p_playlist, PLAYLIST_GOTO,
p_playlist->i_index - 1 );
msg_Dbg( p_intf, "requested playlist previous" );
SSPushN( st, ~SSPopN( st, vars ) );
}
else if( !strcmp( action, "add" ) )
else if( !strcmp( s, "^" ) )
{
uri_extract_value( p_request, "mrl", action, 512 );
uri_decode_url_encoded( action );
playlist_Add( p_playlist, action,
PLAYLIST_APPEND, PLAYLIST_END );
msg_Dbg( p_intf, "requested playlist add: %s", action );
SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
}
}
else if( !strcmp( s, "&" ) )
{
SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
}
else if( !strcmp( s, "|" ) )
{
SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
}
else if( !strcmp( s, "+" ) )
{
SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
}
else if( !strcmp( s, "-" ) )
{
int i = SSPopN( st, vars );
int j = SSPopN( st, vars );
SSPushN( st, i - j );
}
else if( !strcmp( s, "*" ) )
{
SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
}
else if( !strcmp( s, "/" ) )
{
int i, j;
i_ret = httpd_page_interface_update( p_intf, p_playlist, pp_data, pi_data, i_request ? VLC_TRUE : VLC_FALSE );
i = SSPopN( st, vars );
j = SSPopN( st, vars );
vlc_object_release( p_playlist );
SSPushN( st, j != 0 ? i / j : 0 );
}
else if( !strcmp( s, "%" ) )
{
int i, j;
return i_ret;
}
i = SSPopN( st, vars );
j = SSPopN( st, vars );
static int httpd_page_interface_update( intf_thread_t *p_intf,
playlist_t *p_playlist,
uint8_t **pp_data, int *pi_data, vlc_bool_t b_redirect )
{
int i, i_size = 0;
char *p;
SSPushN( st, j != 0 ? i % j : 0 );
}
/* 2. integer tests */
else if( !strcmp( s, "=" ) )
{
SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
}
else if( !strcmp( s, "<" ) )
{
int i = SSPopN( st, vars );
int j = SSPopN( st, vars );
vlc_mutex_lock( &p_playlist->object_lock );
/*
* Count playlist items for memory allocation
*/
for ( i = 0; i < p_playlist->i_size; i++ )
{
i_size += sizeof("<a href=?action=play&item=?>? - </a><br />\n" );
i_size += strlen( p_playlist->pp_items[i]->psz_name );
}
/* add something for all the static strings below */
i_size += 8192;
p = *pp_data = malloc( i_size );
p += sprintf( p, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" );
p += sprintf( p, "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>VLC Media Player</title>\n" );
if( b_redirect )
{
p += sprintf( p, "<meta http-equiv=\"refresh\" content=\"0;URL=/\"/>\n" );
}
p += sprintf( p, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>\n" );
/* p += sprintf( p, "<link rel=\"shortcut icon\" href=\"http://www.videolan.org/favicon.ico\">\n" ); */
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h2 style=\"align: center;\"><a href=\"http://www.videolan.org\">"
"VLC Media Player</a> (http interface)</h2>\n" );
/*
* Display the controls
*/
p += sprintf( p, "<hr/>\n" );
p += sprintf( p, "<form method=\"get\" action=\"/\">"
"<p><input type=\"submit\" name=\"action\" value=\"stop\" />"
"<input type=\"submit\" name=\"action\" value=\"pause\" />"
"<input type=\"submit\" name=\"action\" value=\"previous\" />"
"<input type=\"submit\" name=\"action\" value=\"next\" /></p>"
"</form>\n" );
p += sprintf( p, "<form method=\"get\" action=\"/\" "
"enctype=\"text/plain\" >"
"<p>Media Resource Locator:<br/>"
"<input type=\"text\" name=\"mrl\" size=\"40\" />"
"<input type=\"submit\" name=\"action\" value=\"add\" /></p>"
"</form>\n" );
p += sprintf( p, "<hr/>\n" );
/*
* Display the playlist items
*/
if ( p_playlist->i_size )
{
p += sprintf( p, "<ol>\n" );
for ( i = 0; i < p_playlist->i_size; i++ )
{
if( i == p_playlist->i_index ) p += sprintf( p, "<b>" );
p += sprintf( p, "<li><a href=\"?action=play&amp;item=%i\">", i );
p += sprintf( p, "%s", p_playlist->pp_items[i]->psz_name );
p += sprintf( p, "</a></li>" );
if( i == p_playlist->i_index ) p += sprintf( p, "</b>" );
p += sprintf( p, "\n" );
}
p += sprintf( p, "</ol>\n" );
}
else
{
p += sprintf( p, "<p>no entries</p>\n" );
}
SSPushN( st, i < j ? -1 : 0 );
}
else if( !strcmp( s, ">" ) )
{
int i = SSPopN( st, vars );
int j = SSPopN( st, vars );
SSPushN( st, i > j ? -1 : 0 );
}
else if( !strcmp( s, "<=" ) )
{
int i = SSPopN( st, vars );
int j = SSPopN( st, vars );
SSPushN( st, i <= j ? -1 : 0 );
}
else if( !strcmp( s, ">=" ) )
{
int i = SSPopN( st, vars );
int j = SSPopN( st, vars );
p += sprintf( p, "</body>\n" );
p += sprintf( p, "</html>\n" );
SSPushN( st, i >= j ? -1 : 0 );
}
/* 3. string functions */
else if( !strcmp( s, "strcat" ) )
{
char *s1 = SSPop( st );
char *s2 = SSPop( st );
char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
*pi_data = strlen( *pp_data );
strcpy( str, s1 );
strcat( str, s2 );
SSPush( st, str );
free( s1 );
free( s2 );
free( str );
}
else if( !strcmp( s, "strcmp" ) )
{
char *s1 = SSPop( st );
char *s2 = SSPop( st );
vlc_mutex_unlock( &p_playlist->object_lock );
SSPushN( st, strcmp( s1, s2 ) );
free( s1 );
free( s2 );
}
else if( !strcmp( s, "strlen" ) )
{
char *str = SSPop( st );
return VLC_SUCCESS;
SSPushN( st, strlen( str ) );
free( str );
}
/* 4. stack functions */
else if( !strcmp( s, "dup" ) )
{
char *str = SSPop( st );
SSPush( st, str );
SSPush( st, str );
free( str );
}
else if( !strcmp( s, "drop" ) )
{
char *str = SSPop( st );
free( str );
}
else if( !strcmp( s, "swap" ) )
{
char *s1 = SSPop( st );
char *s2 = SSPop( st );
SSPush( st, s1 );
SSPush( st, s2 );
free( s1 );
free( s2 );
}
else if( !strcmp( s, "flush" ) )
{
SSClean( st );
SSInit( st );
}
else if( !strcmp( s, "store" ) )
{
char *name = SSPop( st );
char *value = SSPop( st );
mvar_PushNewVar( vars, name, value );
free( name );
free( value );
}
else if( !strcmp( s, "value" ) )
{
char *name = SSPop( st );
char *value = mvar_GetValue( vars, name );
SSPush( st, value );
free( name );
}
else
{
SSPush( st, s );
}
}
}
......@@ -2,7 +2,7 @@
* httpd.c
*****************************************************************************
* Copyright (C) 2001-2003 VideoLAN
* $Id: httpd.c,v 1.22 2003/07/10 18:29:41 zorglub Exp $
* $Id: httpd.c,v 1.23 2003/07/10 22:24:09 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
......@@ -25,15 +25,13 @@
* Preamble
*****************************************************************************/
#include <stdlib.h>
#include <vlc/vlc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <vlc/vlc.h>
#include "httpd.h"
#ifdef HAVE_SYS_TIME_H
......@@ -75,10 +73,6 @@
#define HTTPD_CONNECTION_MAX_UNUSED 10000000
#define HTTP_ADMIN_DEFAULT_USERNAME "admin"
#define HTTP_ADMIN_DEFAULT_PASSWORD "admin"
#define FREE( p ) if( p ) { free( p); (p) = NULL; }
#if defined( WIN32 ) || defined( UNDER_CE )
......@@ -96,24 +90,11 @@ static void Close ( vlc_object_t * );
/*****************************************************************************
* Module descriptor
*****************************************************************************/
#define ADMIN_USER_TEXT N_( "Admin page's username" )
#define ADMIN_USER_LONGTEXT N_( \
"You can set the username for the administration page \
If you do not set it, the default username is \"admin\"" )
#define ADMIN_PASS_TEXT N_( "Admin page's password" )
#define ADMIN_PASS_LONGTEXT N_( \
"You can set the password for the administration page \
If you do not set it, the default password is \"admin\"" )
vlc_module_begin();
set_description( _("HTTP 1.0 daemon") );
set_capability( "httpd", 42 );
add_string( "http-admin-user", NULL, NULL,
ADMIN_USER_TEXT, ADMIN_USER_LONGTEXT, VLC_TRUE );
add_string( "http-admin-pass", NULL, NULL,
ADMIN_PASS_TEXT, ADMIN_PASS_LONGTEXT, VLC_TRUE );
set_callbacks( Open, Close );
var_Create( p_module->p_libvlc, "httpd", VLC_VAR_MUTEX );
vlc_module_end();
/*****************************************************************************
......@@ -137,7 +118,7 @@ static httpd_stream_t *RegisterStream ( httpd_t *,
static int SendStream ( httpd_t *, httpd_stream_t *, uint8_t *, int );
static int HeaderStream ( httpd_t *, httpd_stream_t *, uint8_t *, int );
static void UnregisterStream( httpd_t *, httpd_stream_t* );
static int Control ( httpd_t *, int , void*, void* );
/*****************************************************************************
* Internal definitions
*****************************************************************************/
......@@ -268,6 +249,7 @@ static void httpd_ConnnectionClose( httpd_sys_t *, httpd_connection_t * );
static int httpd_UnbanIP( httpd_sys_t *, httpd_banned_ip_t *);
static int httpd_BanIP( httpd_sys_t *, char *);
static httpd_banned_ip_t *httpd_GetbannedIP( httpd_sys_t *, char * );
/*****************************************************************************
* Open:
*****************************************************************************/
......@@ -330,6 +312,7 @@ static int Open( vlc_object_t *p_this )
p_httpd->pf_header_stream = HeaderStream;
p_httpd->pf_send_stream = SendStream;
p_httpd->pf_unregister_stream=UnregisterStream;
p_httpd->pf_control = Control;
return( VLC_SUCCESS );
}
......@@ -957,224 +940,153 @@ static int HeaderStream( httpd_t *p_httpd, httpd_stream_t *p_stream,
return( VLC_SUCCESS );
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static int httpd_page_401_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
static void httpd_info_add_ss( httpd_info_t *p_info, char *name, char *value )
{
char *p;
p = *pp_data = malloc( 1024 );
p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>Error 401</title>\n" );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
p += sprintf( p, "</html>\n" );
*pi_data = strlen( *pp_data ) + 1;
return VLC_SUCCESS;
if( p_info->i_count == 0 )
{
p_info->info = malloc( sizeof( httpd_val_t ) );
}
else
{
p_info->info =
realloc( p_info->info,
sizeof( httpd_val_t ) * ( p_info->i_count + 1 ) );
}
p_info->info[p_info->i_count].psz_name = strdup( name );
p_info->info[p_info->i_count].psz_value = strdup( value );
p_info->i_count++;
}
static int httpd_page_404_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
{
char *p;
p = *pp_data = malloc( 1024 );
static void httpd_info_add_si( httpd_info_t *p_info, char *name, int i_value )
{
char v[40];
p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>Error 404</title>\n" );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
p += sprintf( p, "</html>\n" );
sprintf( v, "%d", i_value );
httpd_info_add_ss( p_info, name, v );
}
*pi_data = strlen( *pp_data ) + 1;
static void httpd_info_add_sp( httpd_info_t *p_info, char *name, void *value )
{
char v[40];
return VLC_SUCCESS;
sprintf( v, "%p", value );
httpd_info_add_ss( p_info, name, v );
}
static int _httpd_page_admin_get_status( httpd_file_callback_args_t *p_args,
uint8_t **pp_data, int *pi_data )
static int Control( httpd_t *p_httpd,
int i_query, void *arg1, void *arg2 )
{
httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
httpd_sys_t *p_httpt = p_httpd->p_sys;
httpd_info_t *p_info;
httpd_connection_t *p_con;
httpd_banned_ip_t *p_ip;
int i;
char *p;
void *id;
/* FIXME FIXME do not use static size FIXME FIXME*/
p = *pp_data = malloc( 8096 );
p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
p += sprintf( p, "<h2><center>Admin page</center></h2>\n" );
/* general */
p += sprintf( p, "<h3>General state</h3>\n" );
p += sprintf( p, "<ul>\n" );
p += sprintf( p, "<li>Connection count: %d</li>\n", p_httpt->i_connection_count );
//p += sprintf( p, "<li>Total bandwith: %d</li>\n", -1 );
/*p += sprintf( p, "<li></li>\n" );*/
p += sprintf( p, "<li>Ban count: %d</li>\n", p_httpt->i_banned_ip_count );
p += sprintf( p, "</ul>\n" );
/* ban list */
/* XXX do not lock on ban_lock */
p += sprintf( p, "<h3>Ban list</h3>\n" );
p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
p += sprintf( p, "<tr>\n<th>IP</th>\n<th>Action</th></tr>\n" );
for( p_ip = p_httpt->p_first_banned_ip;p_ip != NULL; p_ip = p_ip->p_next )
{
p += sprintf( p, "<tr>\n" );
p += sprintf( p, "<td>%s</td>\n", p_ip->psz_ip );
p += sprintf( p, "<td><form method=\"get\" action=\"\">"
"<select name=\"action\">"
"<option selected>unban_ip</option>"
"</select>"
"<input type=\"hidden\" name=\"id\" value=\"%s\"/>"
"<input type=\"submit\" value=\"Do it\" />"
"</form></td>\n", p_ip->psz_ip);
p += sprintf( p, "</tr>\n" );
}
p += sprintf( p, "</table>\n" );
/* host list */
vlc_mutex_lock( &p_httpt->host_lock );
p += sprintf( p, "<h3>Host list</h3>\n" );
p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
p += sprintf( p, "<tr>\n<th>Host</th><th>Port</th><th>IP</th>\n</tr>\n" );
for( i = 0; i < p_httpt->i_host_count; i++ )
switch( i_query )
{
p += sprintf( p, "<tr>\n" );
p += sprintf( p, "<td>%s</td>\n", p_httpt->host[i]->psz_host_addr );
p += sprintf( p, "<td>%d</td>\n", p_httpt->host[i]->i_port );
p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_httpt->host[i]->sock.sin_addr ) );
p += sprintf( p, "</tr>\n" );
}
p += sprintf( p, "</table>\n" );
vlc_mutex_unlock( &p_httpt->host_lock );
/* file list */
/* XXX do not take lock on file_lock */
p += sprintf( p, "<h3>File list</h3>\n" );
p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
for( i = 0; i < p_httpt->i_file_count; i++ )
{
if( !p_httpt->file[i]->b_stream )
{
p += sprintf( p, "<tr>\n" );
p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
p += sprintf( p, "</tr>\n" );
}
}
p += sprintf( p, "</table>\n" );
/* stream list */
/* XXX do not take lock on file_lock */
p += sprintf( p, "<h3>Stream list</h3>\n" );
p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
p += sprintf( p, "<tr>\n<th>Name</th><th>Mime</th><th>Protected</th><th>Used</th>\n</tr>\n" );
case HTTPD_GET_HOSTS:
p_info = arg1;
p_info->i_count = 0;
vlc_mutex_lock( &p_httpt->host_lock );
for( i = 0; i < p_httpt->i_host_count; i++ )
{
httpd_info_add_sp( p_info,
"id", p_httpt->host[i] );
httpd_info_add_ss( p_info,
"host", p_httpt->host[i]->psz_host_addr );
httpd_info_add_ss( p_info,
"ip",
inet_ntoa(p_httpt->host[i]->sock.sin_addr));
httpd_info_add_si( p_info,
"port", p_httpt->host[i]->i_port );
}
vlc_mutex_unlock( &p_httpt->host_lock );
return VLC_SUCCESS;
case HTTPD_GET_URLS:
p_info = arg1;
p_info->i_count = 0;
/* we can't take file_lock */
for( i = 0; i < p_httpt->i_file_count; i++ )
{
httpd_info_add_sp( p_info,
"id", p_httpt->file[i] );
httpd_info_add_si( p_info,
"stream", p_httpt->file[i]->b_stream ? 1 : 0 );
httpd_info_add_ss( p_info,
"url", p_httpt->file[i]->psz_file );
httpd_info_add_ss( p_info,
"mime", p_httpt->file[i]->psz_mime );
httpd_info_add_si( p_info,
"protected", p_httpt->file[i]->psz_user ? 1 : 0 );
httpd_info_add_si( p_info,
"used", p_httpt->file[i]->i_ref );
}
return VLC_SUCCESS;
case HTTPD_GET_CONNECTIONS:
p_info = arg1;
p_info->i_count = 0;
/* we can't take lock */
for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
{
httpd_info_add_sp( p_info,
"id", p_con );
httpd_info_add_ss( p_info,
"ip", inet_ntoa( p_con->sock.sin_addr ) );
httpd_info_add_ss( p_info,
"url", p_con->psz_file );
httpd_info_add_si( p_info,
"status", p_con->i_http_error );
}
return VLC_SUCCESS;
case HTTPD_GET_ACL:
p_info = arg1;
p_info->i_count = 0;
return VLC_EGENERIC;
for( i = 0; i < p_httpt->i_file_count; i++ )
{
if( p_httpt->file[i]->b_stream )
{
p += sprintf( p, "<tr>\n" );
p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_file );
p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_mime );
p += sprintf( p, "<td>%s</td>\n", p_httpt->file[i]->psz_user ? "Yes" : "No" );
p += sprintf( p, "<td>%d</td>\n", p_httpt->file[i]->i_ref);
p += sprintf( p, "</tr>\n" );
}
}
p += sprintf( p, "</table>\n" );
case HTTPD_SET_CLOSE:
sscanf( arg1, "%p", &id );
fprintf( stderr, "Control: HTTPD_SET_CLOSE: id=%p", id );
/* connection list */
/* XXX do not take lock on connection_lock */
p += sprintf( p, "<h3>Connection list</h3>\n" );
p += sprintf( p, "<table border=\"1\" cellspacing=\"0\" >\n" );
p += sprintf( p, "<tr>\n<th>IP</th><th>Requested File</th><th>Status</th><th>Action</th>\n</tr>\n" );
for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
{
if( (void*)p_con == id )
{
/* XXX don't free p_con as it could be the one that it is sending ... */
p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
return VLC_SUCCESS;
}
}
return VLC_EGENERIC;
for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
{
p += sprintf( p, "<tr>\n" );
p += sprintf( p, "<td>%s</td>\n", inet_ntoa( p_con->sock.sin_addr ) );
p += sprintf( p, "<td>%s</td>\n", p_con->psz_file );
p += sprintf( p, "<td>%d</td>\n", p_con->i_http_error );
p += sprintf( p, "<td><form method=\"get\" action=\"\">"
"<select name=\"action\">"
"<option selected>close_connection</option>"
"<option>ban_ip</option>"
"<option>close_connection_and_ban_ip</option>"
"</select>"
"<input type=\"hidden\" name=\"id\" value=\"%p\"/>"
"<input type=\"submit\" value=\"Do it\" />"
"</form></td>\n", p_con);
p += sprintf( p, "</tr>\n" );
case HTTPD_SET_ACL:
sscanf( arg1, "%p", &id );
fprintf( stderr, "Control: %p", id );
default:
return VLC_EGENERIC;
}
p += sprintf( p, "</table>\n" );
/* www.videolan.org */
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
p += sprintf( p, "</html>\n" );
*pi_data = strlen( *pp_data ) + 1;
return( VLC_SUCCESS );
}
static int _httpd_page_admin_get_success( httpd_file_callback_args_t *p_args,
uint8_t **pp_data, int *pi_data,
char *psz_msg )
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static int httpd_page_401_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
{
char *p;
p = *pp_data = malloc( 8096 );
p = *pp_data = malloc( 1024 );
p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
p += sprintf( p, "<title>Error 401</title>\n" );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
p += sprintf( p, "<p>Success=`%s'</p>", psz_msg );
p += sprintf( p, "<a href=\"admin.html\">Back to admin page</a>\n" );
p += sprintf( p, "<h1><center> 401 authentification needed</center></h1>\n" );
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
......@@ -1182,27 +1094,22 @@ static int _httpd_page_admin_get_success( httpd_file_callback_args_t *p_args,
*pi_data = strlen( *pp_data ) + 1;
return( VLC_SUCCESS );
return VLC_SUCCESS;
}
static int _httpd_page_admin_get_error( httpd_file_callback_args_t *p_args,
uint8_t **pp_data, int *pi_data,
char *psz_error )
static int httpd_page_404_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
{
char *p;
p = *pp_data = malloc( 8096 );
p = *pp_data = malloc( 1024 );
p += sprintf( p, "<html>\n" );
p += sprintf( p, "<head>\n" );
p += sprintf( p, "<title>VideoLAN Client Stream Output</title>\n" );
p += sprintf( p, "<title>Error 404</title>\n" );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
p += sprintf( p, "<h1><center>VideoLAN Client Stream Output</center></h1>\n" );
p += sprintf( p, "<p>Error=`%s'</p>", psz_error );
p += sprintf( p, "<a href=\"admin.html\">Back to admin page</a>\n" );
p += sprintf( p, "<h1><center> 404 Ressource not found</center></h1>\n" );
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
......@@ -1210,154 +1117,10 @@ static int _httpd_page_admin_get_error( httpd_file_callback_args_t *p_args,
*pi_data = strlen( *pp_data ) + 1;
return( VLC_SUCCESS );
}
static void _httpd_uri_extract_value( char *psz_uri, char *psz_name, char *psz_value, int i_value_max )
{
char *p;
p = strstr( psz_uri, psz_name );
if( p )
{
int i_len;
p += strlen( psz_name );
if( *p == '=' ) p++;
if( strchr( p, '&' ) )
{
i_len = strchr( p, '&' ) - p;
}
else
{
i_len = strlen( p );
}
i_len = __MIN( i_value_max - 1, i_len );
if( i_len > 0 )
{
strncpy( psz_value, p, i_len );
psz_value[i_len] = '\0';
}
else
{
strncpy( psz_value, "", i_value_max );
}
}
else
{
strncpy( psz_value, "", i_value_max );
}
}
static int httpd_page_admin_get( httpd_file_callback_args_t *p_args,
uint8_t *p_request, int i_request,
uint8_t **pp_data, int *pi_data )
{
httpd_sys_t *p_httpt = (httpd_sys_t*)p_args;
httpd_connection_t *p_con;
if( i_request > 0)
{
char action[512];
_httpd_uri_extract_value( p_request, "action", action, 512 );
if( !strcmp( action, "close_connection" ) )
{
char id[128];
void *i_id;
_httpd_uri_extract_value( p_request, "id", id, 512 );
i_id = (void*)strtol( id, NULL, 0 );
msg_Dbg( p_httpt, "requested closing connection id=%s %p", id, i_id );
for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
{
if( (void*)p_con == i_id )
{
/* XXX don't free p_con as it could be the one that it is sending ... */
p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "connection closed" ) );
}
}
return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
}
else if( !strcmp( action, "ban_ip" ) )
{
char id[128];
void *i_id;
_httpd_uri_extract_value( p_request, "id", id, 512 );
i_id = (void*)strtol( id, NULL, 0 );
msg_Dbg( p_httpt, "requested banning ip id=%s %p", id, i_id );
for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
{
if( (void*)p_con == i_id )
{
if( httpd_BanIP( p_httpt,inet_ntoa( p_con->sock.sin_addr ) ) == 0)
return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "IP banned" ) );
else
break;
}
}
return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
}
else if( !strcmp( action, "unban_ip" ) )
{
char id[128];
_httpd_uri_extract_value( p_request, "id", id, 512 );
msg_Dbg( p_httpt, "requested unbanning ip %s", id);
if( httpd_UnbanIP( p_httpt, httpd_GetbannedIP ( p_httpt, id ) ) == 0)
return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "IP Unbanned" ) );
else
return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
}
else if( !strcmp( action, "close_connection_and_ban_ip" ) )
{
char id[128];
void *i_id;
_httpd_uri_extract_value( p_request, "id", id, 512 );
i_id = (void*)strtol( id, NULL, 0 );
msg_Dbg( p_httpt, "requested closing connection and banning ip id=%s %p", id, i_id );
for( p_con = p_httpt->p_first_connection;p_con != NULL; p_con = p_con->p_next )
{
if( (void*)p_con == i_id )
{
/* XXX don't free p_con as it could be the one that it is sending ... */
p_con->i_state = HTTPD_CONNECTION_TO_BE_CLOSED;
if( httpd_BanIP( p_httpt,inet_ntoa( p_con->sock.sin_addr ) ) == 0)
return( _httpd_page_admin_get_success( p_args, pp_data, pi_data, "Connection closed and IP banned" ) );
else
break;
}
}
return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, "invalid id" ) );
return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
}
else
{
return( _httpd_page_admin_get_error( p_args, pp_data, pi_data, action ) );
}
}
else
{
return( _httpd_page_admin_get_status( p_args, pp_data, pi_data ) );
}
return VLC_SUCCESS;
}
static int httpd_BanIP( httpd_sys_t *p_httpt, char * psz_new_banned_ip)
{
httpd_banned_ip_t *p_new_banned_ip ;
......@@ -1883,25 +1646,11 @@ search_file:
#define HTTPD_STREAM_PACKET 10000
static void httpd_Thread( httpd_sys_t *p_httpt )
{
httpd_file_t *p_page_admin;
httpd_file_t *p_page_401;
httpd_file_t *p_page_404;
httpd_connection_t *p_con;
char *psz_user = config_GetPsz (p_httpt, "http-admin-user" );
char *psz_pass = config_GetPsz (p_httpt, "http-admin-pass" );
if( !psz_user )
{
psz_user = strdup(HTTP_ADMIN_DEFAULT_USERNAME);
}
if( !psz_pass )
{
psz_pass = strdup(HTTP_ADMIN_DEFAULT_PASSWORD);
}
msg_Info( p_httpt, "httpd started" );
p_page_401 = _RegisterFile( p_httpt,
......@@ -1916,12 +1665,6 @@ static void httpd_Thread( httpd_sys_t *p_httpt )
httpd_page_404_get,
NULL,
(httpd_file_callback_args_t*)NULL );
p_page_admin = _RegisterFile( p_httpt,
"/admin.html", "text/html",
psz_user , psz_pass ,
httpd_page_admin_get,
NULL,
(httpd_file_callback_args_t*)p_httpt );
while( !p_httpt->b_die )
{
......@@ -1998,9 +1741,9 @@ static void httpd_Thread( httpd_sys_t *p_httpt )
msleep( 1000 );
continue;
}
if( i_ret <= 0 )
{
// msg_Dbg( p_httpt, "waiting..." );
continue;
}
......@@ -2184,7 +1927,8 @@ static void httpd_Thread( httpd_sys_t *p_httpt )
/* check if this p_con aren't to late */
if( p_con->i_stream_pos + p_stream->i_buffer_size < p_stream->i_buffer_pos )
{
fprintf(stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n", p_con->i_stream_pos, p_stream->i_buffer_pos );
fprintf( stderr, "fixing i_stream_pos (old=%lld i_buffer_pos=%lld\n",
p_con->i_stream_pos, p_stream->i_buffer_pos );
p_con->i_stream_pos = p_stream->i_buffer_last_pos;
}
......@@ -2238,8 +1982,4 @@ static void httpd_Thread( httpd_sys_t *p_httpt )
_UnregisterFile( p_httpt, p_page_401 );
_UnregisterFile( p_httpt, p_page_404 );
_UnregisterFile( p_httpt, p_page_admin );
FREE( psz_user );
FREE( psz_pass );
}
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