]> git.sesse.net Git - vlc/blobdiff - modules/access/http.c
Add ranges to font sizes
[vlc] / modules / access / http.c
index c4cb7f398088570387e2eb5ae7a8c1bf50ade569..9817bcaee8476d2961f98ecf033dd8a673810caf 100644 (file)
@@ -53,6 +53,7 @@
 #endif
 
 #include <assert.h>
+#include <limits.h>
 
 #ifdef HAVE_LIBPROXY
 #    include <proxy.h>
@@ -109,6 +110,7 @@ vlc_module_begin ()
                 false )
     add_password( "http-proxy-pwd", NULL,
                   PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
+    add_obsolete_bool( "http-use-IE-proxy" )
     add_string( "http-referrer", NULL, REFERER_TEXT, REFERER_LONGTEXT, false )
         change_safe()
     add_string( "http-user-agent", NULL, UA_TEXT, UA_LONGTEXT, false )
@@ -320,15 +322,39 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
     }
 
     /* Determine the HTTP user agent */
-    /* See RFC2616 §2.2 token definition and §3.8 user-agent header */
+    /* See RFC2616 §2.2 token and comment definition, and §3.8 and
+     * §14.43 user-agent header */
     p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
     if (p_sys->psz_user_agent)
     {
+        unsigned comment_level = 0;
         for( char *p = p_sys->psz_user_agent; *p; p++ )
         {
             uint8_t c = *p;
-            if( c < 32 || strchr( "()<>@,;:\\\"[]?={}", c ) )
-                *p = '_'; /* remove potentially harmful characters */
+            if (comment_level == 0)
+            {
+                if( c < 32 || strchr( ")<>@,;:\\\"[]?={}", c ) )
+                    *p = '_'; /* remove potentially harmful characters */
+            }
+            else
+            {
+                if (c == ')')
+                    comment_level--;
+                else if( c < 32 && strchr( "\t\r\n", c ) == NULL)
+                    *p = '_'; /* remove potentially harmful characters */
+            }
+            if (c == '(')
+            {
+                if (comment_level == UINT_MAX)
+                    break;
+                comment_level++;
+            }
+        }
+        /* truncate evil unclosed comments */
+        if (comment_level > 0)
+        {
+            char *p = strchr(p_sys->psz_user_agent, '(');
+            *p = '\0';
         }
     }
 
@@ -376,7 +402,7 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
             msg_Err(p_access, "Allocating memory for libproxy failed");
         }
     }
-#elif defined( WIN32 )
+#elif (0) // defined( WIN32 ) The parsing is not complete enough
     else
     {
         /* Try to get the proxy server address from Windows internet settings using registry. */
@@ -424,6 +450,7 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
             }
             else
                 msg_Dbg( p_access, "HTTP proxy disabled (MSIE)" );
+            RegCloseKey( h_key );
         }
     }
 #else
@@ -490,14 +517,11 @@ connect:
             if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
                 goto error;
 
-#ifndef NDEBUG
         case 0:
             break;
 
         default:
-            msg_Err( p_access, "You should not be here" );
-            abort();
-#endif
+            assert(0);
     }
 
     if( p_sys->i_code == 401 )
@@ -723,35 +747,15 @@ static void Close( vlc_object_t *p_this )
     free( p_sys );
 }
 
-/*****************************************************************************
- * Read: Read up to i_len bytes from the http connection and place in
- * p_buffer. Return the actual number of bytes read
- *****************************************************************************/
-static int ReadICYMeta( access_t *p_access );
-static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
+/* Read data from the socket taking care of chunked transfer if needed */
+static int ReadData( access_t *p_access, int *pi_read,
+                     uint8_t *p_buffer, size_t i_len )
 {
     access_sys_t *p_sys = p_access->p_sys;
-    int i_read;
-
-    if( p_sys->fd == -1 )
-        goto fatal;
-
-    if( p_sys->b_has_size )
-    {
-        /* Remaining bytes in the file */
-        uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
-        if( remainder < i_len )
-            i_len = remainder;
-
-        /* Remaining bytes in the response */
-        if( p_sys->i_remaining < i_len )
-            i_len = p_sys->i_remaining;
-    }
-
     if( p_sys->b_chunked )
     {
         if( p_sys->i_chunk < 0 )
-            goto fatal;
+            return VLC_EGENERIC;
 
         if( p_sys->i_chunk <= 0 )
         {
@@ -761,7 +765,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
             {
                 /* fatal error - end of file */
                 msg_Dbg( p_access, "failed reading chunk-header line" );
-                return 0;
+                return VLC_EGENERIC;
             }
             p_sys->i_chunk = strtoll( psz, NULL, 16 );
             free( psz );
@@ -769,14 +773,54 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
             if( p_sys->i_chunk <= 0 )   /* eof */
             {
                 p_sys->i_chunk = -1;
-                goto fatal;
+                return VLC_EGENERIC;
             }
         }
 
         if( i_len > p_sys->i_chunk )
             i_len = p_sys->i_chunk;
     }
+    *pi_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
+    if( *pi_read <= 0 )
+        return VLC_SUCCESS;
+
+    if( p_sys->b_chunked )
+    {
+        p_sys->i_chunk -= *pi_read;
+        if( p_sys->i_chunk <= 0 )
+        {
+            /* read the empty line */
+            char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
+            free( psz );
+        }
+    }
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Read: Read up to i_len bytes from the http connection and place in
+ * p_buffer. Return the actual number of bytes read
+ *****************************************************************************/
+static int ReadICYMeta( access_t *p_access );
+static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+    int i_read;
 
+    if( p_sys->fd == -1 )
+        goto fatal;
+
+    if( p_sys->b_has_size )
+    {
+        /* Remaining bytes in the file */
+        uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
+        if( remainder < i_len )
+            i_len = remainder;
+
+        /* Remaining bytes in the response */
+        if( p_sys->i_remaining < i_len )
+            i_len = p_sys->i_remaining;
+    }
     if( i_len == 0 )
         goto fatal;
 
@@ -794,22 +838,10 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
             i_len = i_next;
     }
 
-    i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
+    if( ReadData( p_access, &i_read, p_buffer, i_len ) )
+        goto fatal;
 
-    if( i_read > 0 )
-    {
-        if( p_sys->b_chunked )
-        {
-            p_sys->i_chunk -= i_read;
-            if( p_sys->i_chunk <= 0 )
-            {
-                /* read the empty line */
-                char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
-                free( psz );
-            }
-        }
-    }
-    else
+    if( i_read <= 0 )
     {
         /*
          * I very much doubt that this will work.
@@ -875,24 +907,24 @@ static int ReadICYMeta( access_t *p_access )
     int i_read;
 
     /* Read meta data length */
-    i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
-                       true );
-    if( i_read <= 0 )
+    if( ReadData( p_access, &i_read, &buffer, 1 ) )
         return VLC_EGENERIC;
-    if( buffer == 0 )
-        return VLC_SUCCESS;
-
-    i_read = buffer << 4;
-    /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
+    if( i_read != 1 )
+        return VLC_EGENERIC;
+    const int i_size = buffer << 4;
+    /* msg_Dbg( p_access, "ICY meta size=%u", i_size); */
 
-    psz_meta = malloc( i_read + 1 );
-    if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
-                  (uint8_t *)psz_meta, i_read, true ) != i_read )
+    psz_meta = malloc( i_size + 1 );
+    for( i_read = 0; i_read < i_size; )
     {
-        free( psz_meta );
-        return VLC_EGENERIC;
+        int i_tmp;
+        if( ReadData( p_access, &i_tmp, (uint8_t *)&psz_meta[i_read], i_size - i_read ) || i_tmp <= 0 )
+        {
+            free( psz_meta );
+            return VLC_EGENERIC;
+        }
+        i_read += i_tmp;
     }
-
     psz_meta[i_read] = '\0'; /* Just in case */
 
     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
@@ -1208,44 +1240,22 @@ static int Request( access_t *p_access, uint64_t i_tell )
     p_sys->b_persist = false;
 
     p_sys->i_remaining = 0;
-    if( p_sys->b_proxy )
-    {
-        if( p_sys->url.psz_path )
-        {
-            net_Printf( p_access, p_sys->fd, NULL,
-                        "GET http://%s:%d%s HTTP/1.%d\r\n",
-                        p_sys->url.psz_host, p_sys->url.i_port,
-                        p_sys->url.psz_path, p_sys->i_version );
-        }
-        else
-        {
-            net_Printf( p_access, p_sys->fd, NULL,
-                        "GET http://%s:%d/ HTTP/1.%d\r\n",
-                        p_sys->url.psz_host, p_sys->url.i_port,
-                        p_sys->i_version );
-        }
-    }
+
+    const char *psz_path = p_sys->url.psz_path;
+    if( !psz_path || !*psz_path )
+        psz_path = "/";
+    net_Write( p_access, p_sys->fd, pvs, "GET ", 4 );
+    if( p_sys->b_proxy && pvs == NULL )
+        net_Printf( p_access, p_sys->fd, NULL, "http://%s:%d",
+                    p_sys->url.psz_host, p_sys->url.i_port );
+    net_Printf( p_access, p_sys->fd, pvs, "%s HTTP/1.%d\r\n",
+                psz_path, p_sys->i_version );
+    if( p_sys->url.i_port != (pvs ? 443 : 80) )
+        net_Printf( p_access, p_sys->fd, pvs, "Host: %s:%d\r\n",
+                    p_sys->url.psz_host, p_sys->url.i_port );
     else
-    {
-        const char *psz_path = p_sys->url.psz_path;
-        if( !psz_path || !*psz_path )
-        {
-            psz_path = "/";
-        }
-        if( p_sys->url.i_port != (pvs ? 443 : 80) )
-        {
-            net_Printf( p_access, p_sys->fd, pvs,
-                        "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
-                        psz_path, p_sys->i_version, p_sys->url.psz_host,
-                        p_sys->url.i_port );
-        }
-        else
-        {
-            net_Printf( p_access, p_sys->fd, pvs,
-                        "GET %s HTTP/1.%d\r\nHost: %s\r\n",
-                        psz_path, p_sys->i_version, p_sys->url.psz_host );
-        }
-    }
+        net_Printf( p_access, p_sys->fd, pvs, "Host: %s\r\n",
+                    p_sys->url.psz_host );
     /* User Agent */
     net_Printf( p_access, p_sys->fd, pvs,
                 "User-Agent: %s\r\n",
@@ -1586,6 +1596,11 @@ static int Request( access_t *p_access, uint64_t i_tell )
             if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
                 goto error;
         }
+        else if( !strcasecmp( psz, "Accept-Ranges" ) )
+        {
+            if( !strcasecmp( p, "bytes" ) )
+                p_sys->b_seekable = true;
+        }
 
         free( psz );
     }