* util.c : Utility functions for HTTP interface
*****************************************************************************
* Copyright (C) 2001-2005 the VideoLAN team
- * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
+ * $Id$
*
* Authors: Gildas Bazin <gbazin@netcourrier.com>
* Laurent Aimar <fenrir@via.ecp.fr>
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
+#include <vlc/vlc.h>
+#include <stdio.h>
#include "http.h"
+#include "vlc_strings.h"
/****************************************************************************
* File and directory functions
while( *name )
{
if( *name == '\\' )
- {
- *p++ = '/';
- }
+ *name = '/';
name++;
}
#endif
/* Parse a directory and recursively add files */
int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
- char *psz_dir )
+ char *psz_dir )
{
intf_sys_t *p_sys = p_intf->p_sys;
char dir[MAX_DIR_SIZE];
struct stat stat_info;
#endif
DIR *p_dir;
- struct dirent *p_dir_content;
vlc_acl_t *p_acl;
FILE *file;
int i_dirlen;
+ char sep;
+
+#if defined( WIN32 )
+ sep = '\\';
+#else
+ sep = '/';
+#endif
+
#ifdef HAVE_SYS_STAT_H
- if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
+ if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
{
return VLC_EGENERIC;
}
#endif
- if( ( p_dir = opendir( psz_dir ) ) == NULL )
+ if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
{
- msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
+ msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
return VLC_EGENERIC;
}
i_dirlen = strlen( psz_dir );
if( i_dirlen + 10 > MAX_DIR_SIZE )
{
- msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
+ msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
return 0;
}
msg_Dbg( p_intf, "dir=%s", psz_dir );
- sprintf( dir, "%s/.access", psz_dir );
- if( ( file = fopen( dir, "r" ) ) != NULL )
+ sprintf( dir, "%s%c.access", psz_dir, sep );
+ if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
{
char line[1024];
int i_size;
fclose( file );
}
- sprintf( dir, "%s/.hosts", psz_dir );
+ sprintf( dir, "%s%c.hosts", psz_dir, sep );
p_acl = ACL_Create( p_intf, VLC_FALSE );
if( ACL_LoadFile( p_acl, dir ) )
{
for( ;; )
{
+ const char *psz_filename;
/* parse psz_src dir */
- if( ( p_dir_content = readdir( p_dir ) ) == NULL )
+ if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
{
break;
}
- if( ( p_dir_content->d_name[0] == '.' )
- || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
+ if( ( psz_filename[0] == '.' )
+ || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
continue;
- sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
+ sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
+ LocaleFree( psz_filename );
+
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;
- 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 );
- f->file = E_(FromUTF8)( p_intf, psz_tmp );
+ psz_file = E_(FromUTF8)( p_intf, psz_tmp );
free( psz_tmp );
psz_tmp = vlc_fix_readdir_charset( p_intf,
&dir[strlen( psz_root )] );
- f->name = E_(FileToUrl)( psz_tmp, &b_index );
+ psz_name = E_(FileToUrl)( psz_tmp, &b_index );
free( psz_tmp );
- f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
+ 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" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
if( !f->name )
{
msg_Dbg( p_intf, "file=%s (url=%s)",
f->file, f->name );
- f->p_file = httpd_FileNew( p_sys->p_httpd_host,
- f->name,
- f->b_html ? p_sys->psz_html_type : NULL,
- user, password, p_acl,
- E_(HttpCallback), f );
-
- if( f->p_file )
+ if( !f->b_handler )
+ {
+ char *psz_type = strdup( p_sys->psz_html_type );
+ if( strstr( &dir[strlen( psz_root )], ".xml" ) )
+ {
+ char *psz = strstr( psz_type, "html;" );
+ if( psz )
+ {
+ psz[0] = 'x';
+ psz[1] = 'm';
+ psz[2] = 'l';
+ psz[3] = ';';
+ psz[4] = ' ';
+ }
+ }
+ f->p_file = httpd_FileNew( p_sys->p_httpd_host,
+ f->name,
+ f->b_html ? psz_type : NULL,
+ user, password, p_acl,
+ E_(HttpCallback), f );
+ free( psz_type );
+ if( f->p_file != NULL )
+ {
+ TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
+ }
+ }
+ else
{
- TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
+ 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
* - a redirect from rep to rep/
* - in case of index.* rep/index.html to rep/ */
if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
{
- char *psz_in = psz_utf8;
- size_t i_in = strlen(psz_in);
+ size_t i_in = strlen(psz_utf8);
size_t i_out = i_in * 2;
char *psz_local = malloc(i_out + 1);
char *psz_out = psz_local;
+ size_t i_ret;
+ char psz_tmp[i_in + 1];
+ const char *psz_in = psz_tmp;
+ strcpy( psz_tmp, psz_utf8 );
- size_t i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
- &psz_out, &i_out );
+ i_in = strlen( psz_tmp );
+
+ i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
+ &psz_out, &i_out );
if( i_ret == (size_t)-1 || i_in )
{
msg_Warn( p_intf,
if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
{
- char *psz_in = psz_local;
+ const char *psz_in = psz_local;
size_t i_in = strlen(psz_in);
size_t i_out = i_in * 6;
char *psz_utf8 = malloc(i_out + 1);
{
char value[512];
char *psz;
- mvar_t *itm = mvar_New( name, "set" );
+ mvar_t *itm = E_(mvar_New)( name, "set" );
- sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
- mvar_AppendNewVar( itm, "current", value );
+ if( p_pl->status.p_item && p_node &&
+ p_pl->status.p_item->p_input && p_node->p_input &&
+ p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
+ {
+ E_(mvar_AppendNewVar)( itm, "current", "1" );
+ }
+ else
+ {
+ E_(mvar_AppendNewVar)( itm, "current", "0" );
+ }
- sprintf( value, "%d", p_node->input.i_id );
- mvar_AppendNewVar( itm, "index", value );
+ sprintf( value, "%d", p_node->p_input->i_id );
+ E_(mvar_AppendNewVar)( itm, "index", value );
- psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
- mvar_AppendNewVar( itm, "name", psz );
+ psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
+ E_(mvar_AppendNewVar)( itm, "name", psz );
free( psz );
- psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
- mvar_AppendNewVar( itm, "uri", psz );
+ psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_uri );
+ E_(mvar_AppendNewVar)( itm, "uri", psz );
free( psz );
sprintf( value, "Item");
- mvar_AppendNewVar( itm, "type", value );
+ E_(mvar_AppendNewVar)( itm, "type", value );
sprintf( value, "%d", i_depth );
- mvar_AppendNewVar( itm, "depth", value );
+ E_(mvar_AppendNewVar)( itm, "depth", value );
+
+ if( p_node->i_flags & PLAYLIST_RO_FLAG )
+ {
+ E_(mvar_AppendNewVar)( itm, "ro", "ro" );
+ }
+ else
+ {
+ E_(mvar_AppendNewVar)( itm, "ro", "rw" );
+ }
- mvar_AppendVar( s, itm );
+ sprintf( value, "%ld", (long)p_node->p_input->i_duration );
+ E_(mvar_AppendNewVar)( itm, "duration", value );
+
+ E_(mvar_AppendVar)( s, itm );
}
else
{
char value[512];
char *psz;
int i_child;
- mvar_t *itm = mvar_New( name, "set" );
+ mvar_t *itm = E_(mvar_New)( name, "set" );
- psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
- mvar_AppendNewVar( itm, "name", psz );
- mvar_AppendNewVar( itm, "uri", psz );
+ psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
+ E_(mvar_AppendNewVar)( itm, "name", psz );
+ E_(mvar_AppendNewVar)( itm, "uri", psz );
free( psz );
sprintf( value, "Node" );
- mvar_AppendNewVar( itm, "type", value );
+ E_(mvar_AppendNewVar)( itm, "type", value );
- sprintf( value, "%d", p_node->input.i_id );
- mvar_AppendNewVar( itm, "index", value );
+ sprintf( value, "%d", p_node->p_input->i_id );
+ E_(mvar_AppendNewVar)( itm, "index", value );
sprintf( value, "%d", p_node->i_children);
- mvar_AppendNewVar( itm, "i_children", value );
+ E_(mvar_AppendNewVar)( itm, "i_children", value );
sprintf( value, "%d", i_depth );
- mvar_AppendNewVar( itm, "depth", value );
+ E_(mvar_AppendNewVar)( itm, "depth", value );
+
+ if( p_node->i_flags & PLAYLIST_RO_FLAG )
+ {
+ E_(mvar_AppendNewVar)( itm, "ro", "ro" );
+ }
+ else
+ {
+ E_(mvar_AppendNewVar)( itm, "ro", "rw" );
+ }
- mvar_AppendVar( s, itm );
+ E_(mvar_AppendVar)( s, itm );
for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
E_(PlaylistListNode)( p_intf, p_pl,
{
i_value += 3600 * i_stock;
i_stock = 0;
- /* other characters which are not numbers are not important */
- while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
+ /* other characters which are not numbers are not
+ * important */
+ while( ((p_value[0] < '0') || (p_value[0] > '9'))
+ && (p_value[0] != '\0') )
{
p_value++;
}
i_value += 60 * i_stock;
i_stock = 0;
p_value++;
- while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
+ while( ((p_value[0] < '0') || (p_value[0] > '9'))
+ && (p_value[0] != '\0') )
{
p_value++;
}
{
i_value += i_stock;
i_stock = 0;
- while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
+ while( ((p_value[0] < '0') || (p_value[0] > '9'))
+ && (p_value[0] != '\0') )
{
p_value++;
}
}
}
- /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
+ /* if there is no known symbol, I consider it as seconds.
+ * Otherwise, i_stock = 0 */
i_value += i_stock;
switch(i_relative)
}
case POSITION_ABSOLUTE:
{
- val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
+ val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
+ 0.0 ), 100.0 );
var_Set( p_sys->p_input, "position", val );
- msg_Dbg( p_intf, "requested seek percent: %d", i_value );
+ msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
break;
}
case POSITION_REL_FOR:
{
var_Get( p_sys->p_input, "position", &val );
- val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
+ val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
+ 0.0 ) , 100.0 );
var_Set( p_sys->p_input, "position", val );
- msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
+ msg_Dbg( p_intf, "requested seek percent forward: %d%%",
+ i_value );
break;
}
case POSITION_REL_BACK:
{
var_Get( p_sys->p_input, "position", &val );
- val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
+ val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
+ 0.0 ) , 100.0 );
var_Set( p_sys->p_input, "position", val );
- msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
+ msg_Dbg( p_intf, "requested seek percent backward: %d%%",
+ i_value );
break;
}
default:
{
- msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
+ msg_Dbg( p_intf, "invalid seek request" );
break;
}
}
return p;
}
-void E_(DecodeEncodedURI)( char *psz )
+/* Since the resulting string is smaller we can work in place, so it is
+ * permitted to have psz == new. new points to the first word of the
+ * string, the function returns the remaining string. */
+char *E_(FirstWord)( char *psz, char *new )
{
- char *dup = strdup( psz );
- char *p = dup;
+ vlc_bool_t b_end;
+
+ while( *psz == ' ' )
+ psz++;
- while( *p )
+ while( *psz != '\0' && *psz != ' ' )
{
- if( *p == '%' )
+ if( *psz == '\'' )
{
- char val[3];
- p++;
- if( !*p )
+ char c = *psz++;
+ while( *psz != '\0' && *psz != c )
{
- break;
+ if( *psz == '\\' && psz[1] != '\0' )
+ psz++;
+ *new++ = *psz++;
}
-
- val[0] = *p++;
- val[1] = *p++;
- val[2] = '\0';
-
- *psz++ = strtol( val, NULL, 16 );
- }
- else if( *p == '+' )
- {
- *psz++ = ' ';
- p++;
+ if( *psz == c )
+ psz++;
}
else
{
- *psz++ = *p++;
+ if( *psz == '\\' && psz[1] != '\0' )
+ psz++;
+ *new++ = *psz++;
}
}
- *psz++ = '\0';
- free( dup );
+ b_end = !*psz;
+
+ *new++ = '\0';
+ if( !b_end )
+ return psz + 1;
+ else
+ return NULL;
}
-/* Since the resulting string is smaller we can work in place, so it is
- * permitted to have psz == new. new points to the first word of the
- * string, the function returns the remaining string. */
-char *E_(FirstWord)( char *psz, char *new )
+/**********************************************************************
+ * MRLParse: parse the MRL, find the MRL string and the options,
+ * create an item with all information in it, and return the item.
+ * return NULL if there is an error.
+ **********************************************************************/
+
+/* Function analog to FirstWord except that it relies on colon instead
+ * of space to delimit option boundaries. */
+static char *FirstOption( char *psz, char *new )
{
- vlc_bool_t b_end;
+ vlc_bool_t b_end, b_start = VLC_TRUE;
while( *psz == ' ' )
psz++;
- while( *psz != '\0' && *psz != ' ' )
+ while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
{
if( *psz == '\'' )
{
if( *psz == '\\' && psz[1] != '\0' )
psz++;
*new++ = *psz++;
+ b_start = VLC_FALSE;
}
if( *psz == c )
psz++;
if( *psz == '\\' && psz[1] != '\0' )
psz++;
*new++ = *psz++;
+ b_start = VLC_FALSE;
}
}
b_end = !*psz;
+ if ( !b_start )
+ while (new[-1] == ' ')
+ new--;
+
*new++ = '\0';
if( !b_end )
return psz + 1;
return NULL;
}
-/**********************************************************************
- * parse_MRL: parse the MRL, find the mrl string and the options,
- * create an item with all information in it, and return the item.
- * return NULL if there is an error.
- **********************************************************************/
-playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
+input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
char *psz_name )
{
char *psz = strdup( _psz );
char *s_mrl = psz;
char *s_temp;
- playlist_item_t * p_item = NULL;
+ input_item_t * p_input = NULL;
/* extract the mrl */
- s_temp = E_(FirstWord)( s_mrl, s_mrl );
+ s_temp = FirstOption( s_mrl, s_mrl );
if( s_temp == NULL )
{
s_temp = s_mrl + strlen( s_mrl );
}
- p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
+ p_input = input_ItemNew( p_intf, s_mrl, psz_name );
s_mrl = s_temp;
/* now we can take care of the options */
- while( *s_mrl != '\0' )
+ while ( *s_mrl != '\0' )
{
- s_temp = E_(FirstWord)( s_mrl, s_mrl );
+ s_temp = FirstOption( s_mrl, s_mrl );
if( s_mrl == '\0' )
break;
if( s_temp == NULL )
{
s_temp = s_mrl + strlen( s_mrl );
}
- if( *s_mrl != ':' )
- msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
- else
- playlist_ItemAddOption( p_item, s_mrl );
+ vlc_input_item_AddOption( p_input, s_mrl );
s_mrl = s_temp;
}
free( psz );
- return p_item;
+ return p_input;
+}
+
+/**********************************************************************
+ * RealPath: parse ../, ~ and path stuff
+ **********************************************************************/
+char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
+{
+ char *psz_dir;
+ char *p;
+ int i_len = strlen(psz_src);
+ char sep;
+
+#if defined( WIN32 )
+ sep = '\\';
+#else
+ sep = '/';
+#endif
+
+ psz_dir = malloc( i_len + 2 );
+ strcpy( psz_dir, psz_src );
+
+ /* Add a trailing sep to ease the .. step */
+ psz_dir[i_len] = sep;
+ psz_dir[i_len + 1] = '\0';
+
+#ifdef WIN32
+ /* Convert all / to native separator */
+ p = psz_dir;
+ while( (p = strchr( p, '/' )) != NULL )
+ {
+ *p = sep;
+ }
+#endif
+
+ /* Remove multiple separators and /./ */
+ p = psz_dir;
+ while( (p = strchr( p, sep )) != NULL )
+ {
+ if( p[1] == sep )
+ memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
+ else if( p[1] == '.' && p[2] == sep )
+ memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
+ else
+ p++;
+ }
+
+ if( psz_dir[0] == '~' )
+ {
+ char *dir = malloc( strlen(psz_dir)
+ + strlen(p_intf->p_libvlc->psz_userdir) );
+ /* This is incomplete : we should also support the ~cmassiot/ syntax. */
+ sprintf( dir, "%s%s", p_intf->p_libvlc->psz_userdir, psz_dir + 1 );
+ free( psz_dir );
+ psz_dir = dir;
+ }
+
+ if( strlen(psz_dir) > 2 )
+ {
+ /* Fix all .. dir */
+ p = psz_dir + 3;
+ while( (p = strchr( p, sep )) != NULL )
+ {
+ if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
+ {
+ char *q;
+ p[-3] = '\0';
+ if( (q = strrchr( psz_dir, sep )) != NULL )
+ {
+ memmove( q + 1, p + 1, strlen(p + 1) + 1 );
+ p = q + 1;
+ }
+ else
+ {
+ memmove( psz_dir, p, strlen(p) + 1 );
+ p = psz_dir + 3;
+ }
+ }
+ else
+ p++;
+ }
+ }
+
+ /* Remove trailing sep if there are at least 2 sep in the string
+ * (handles the C:\ stuff) */
+ p = strrchr( psz_dir, sep );
+ if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
+ *p = '\0';
+
+ return psz_dir;
}