]> git.sesse.net Git - vlc/commitdiff
- include/network.h: new vlc_UrlEncode and vlc_UrlIsNotEncoded helpers function
authorRémi Denis-Courmont <rem@videolan.org>
Thu, 10 Mar 2005 10:58:02 +0000 (10:58 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Thu, 10 Mar 2005 10:58:02 +0000 (10:58 +0000)
- modules/access/http.c: do not choke when there's a space in a URL
  (should probably be done for some others)

include/network.h
modules/access/http.c

index 153e9657c1e3151d0da5b9f63de0b34fe00ee90a..4fff09ec989a2d9526decd2cc473bc45a166ef5b 100644 (file)
@@ -1,11 +1,12 @@
 /*****************************************************************************
  * network.h: interface to communicate with network plug-ins
  *****************************************************************************
- * Copyright (C) 2002 VideoLAN
+ * Copyright (C) 2002-2005 VideoLAN
  * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *          Laurent Aimar <fenrir@via.ecp.fr>
+ *          Rémi Denis-Courmont <courmisch # 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
@@ -166,6 +167,71 @@ static inline void vlc_UrlClean( vlc_url_t *url )
     url->psz_path     = NULL;
     url->psz_option   = NULL;
 }
+
+/*****************************************************************************
+ * vlc_UrlEncode: 
+ *****************************************************************************
+ * perform URL encoding
+ * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
+ *****************************************************************************/
+static inline char *vlc_UrlEncode( const char *psz_url )
+{
+    char *psz_enc, *out;
+    const char *in;
+
+    psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
+    if( psz_enc == NULL )
+        return NULL;
+
+    out = psz_enc;
+    for( in = psz_url; *in; in++ )
+    {
+        char c = *in;
+
+        if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
+         || ( c == '+' ) )
+        {
+            *out++ = '%';   
+            *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
+                                           : '0' + ( c >> 4 );
+            *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
+                                           : '0' + ( c & 0xf );
+        }
+        else
+            *out++ = c;
+    }
+    *out++ = '\0';
+
+    return (char *)realloc( psz_enc, out - psz_enc );
+}
+
+/*****************************************************************************
+ * vlc_UrlIsNotEncoded:
+ *****************************************************************************
+ * check if given string is not a valid URL and must hence be encoded
+ *****************************************************************************/
+#include <ctype.h>
+
+static inline int vlc_UrlIsNotEncoded( const char *psz_url )
+{
+    const char *ptr;
+
+    for( ptr = psz_url; *ptr; ptr++ )
+    {
+        char c = *ptr;
+
+        if( c == '%' )
+        {
+            if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
+                return 1; /* not encoded */
+            ptr += 2;
+        }
+        else
+        if( c == ' ' )
+            return 1;
+    }
+    return 0; /* looks fine - but maybe it is not encoded */
+}
                     
 /*****************************************************************************
  * vlc_b64_encode:
index 2371e5aa289cb7d8a0be3e19bccfee11707f5861..e2194c32a97fefdf2bb2ef17ba0fd72cf05f3f59 100644 (file)
@@ -227,7 +227,21 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_remaining = 0;
 
     /* Parse URI */
-    ParseURL( p_sys, p_access->psz_path );
+    if( vlc_UrlIsNotEncoded( p_access->psz_path ) )
+    {
+        psz = vlc_UrlEncode( p_access->psz_path );
+        if( psz == NULL )
+        {
+            free( p_sys );
+            return VLC_ENOMEM;
+        }
+
+        ParseURL( p_sys, psz );
+        free( psz );
+    }
+    else
+        ParseURL( p_sys, p_access->psz_path );
+
     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
     {
         msg_Warn( p_access, "invalid host" );