]> git.sesse.net Git - vlc/blobdiff - modules/access/http.c
access_http: do not close the stream when seeking out of range
[vlc] / modules / access / http.c
index 4a1e7845d0890317be34262d9c25f09d19ca7c3e..f7a2995d5a66dcd7b3ceae2d5a888f1c4ed92a6e 100644 (file)
@@ -31,7 +31,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 
 
 #   include <zlib.h>
 #endif
 
+#include <assert.h>
+
+#ifdef HAVE_PROXY_H
+#    include "proxy.h"
+#endif
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -59,9 +64,13 @@ static void Close( vlc_object_t * );
 #define PROXY_TEXT N_("HTTP proxy")
 #define PROXY_LONGTEXT N_( \
     "HTTP proxy to be used It must be of the form " \
-    "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
+    "http://[user@]myproxy.mydomain:myport/ ; " \
     "if empty, the http_proxy environment variable will be tried." )
 
+#define PROXY_PASS_TEXT N_("HTTP proxy password")
+#define PROXY_PASS_LONGTEXT N_( \
+    "If your HTTP proxy requires a password, set it here." )
+
 #define CACHING_TEXT N_("Caching value in ms")
 #define CACHING_LONGTEXT N_( \
     "Caching value for HTTP streams. This " \
@@ -94,6 +103,8 @@ vlc_module_begin();
 
     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
                 false );
+    add_password( "http-proxy-pwd", NULL, NULL,
+                  PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false );
     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
                  CACHING_TEXT, CACHING_LONGTEXT, true );
     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
@@ -147,6 +158,7 @@ struct access_sys_t
     bool b_proxy;
     vlc_url_t  proxy;
     http_auth_t proxy_auth;
+    char       *psz_proxy_passbuf;
 
     /* */
     int        i_code;
@@ -239,6 +251,7 @@ static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
 #endif
     p_sys->fd = -1;
     p_sys->b_proxy = false;
+    p_sys->psz_proxy_passbuf = NULL;
     p_sys->i_version = 1;
     p_sys->b_seekable = true;
     p_sys->psz_mime = NULL;
@@ -298,24 +311,63 @@ static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
 
     /* Check proxy */
-    psz = var_CreateGetString( p_access, "http-proxy" );
-    if( *psz )
+    psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
+    if( psz )
     {
         p_sys->b_proxy = true;
         vlc_UrlParse( &p_sys->proxy, psz, 0 );
+        free( psz );
     }
-#ifdef HAVE_GETENV
+#ifdef HAVE_PROXY_H
     else
     {
-        char *psz_proxy = getenv( "http_proxy" );
-        if( psz_proxy && *psz_proxy )
+        pxProxyFactory *pf = px_proxy_factory_new();
+        if (pf)
+        {
+            char *buf;
+            int i;
+            i=asprintf(&buf, "%s://%s", p_access->psz_access, p_access->psz_path);
+            if (i >= 0)
+            {
+                msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
+                char **proxies = px_proxy_factory_get_proxies(pf, buf);
+                if (proxies[0])
+                {
+                    msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
+                    if(strcmp(proxies[0],"direct://") != 0) 
+                    {
+                        p_sys->b_proxy = true;
+                        vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
+                    }
+                }
+                for(i=0;proxies[i];i++) free(proxies[i]);
+                free(proxies);
+                free(buf);
+            }
+            px_proxy_factory_free(pf);
+        }
+        else
+        {
+            msg_Err(p_access, "Allocating memory for libproxy failed");
+        }
+    }
+#elif HAVE_GETENV
+    else
+    {
+        psz = getenv( "http_proxy" );
+        if( psz )
         {
             p_sys->b_proxy = true;
-            vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
+            vlc_UrlParse( &p_sys->proxy, psz, 0 );
         }
     }
 #endif
-    free( psz );
+    if( psz ) /* No, this is NOT a use-after-free error */
+    {
+        psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
+        if( psz )
+            p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
+    }
 
     if( p_sys->b_proxy )
     {
@@ -339,8 +391,7 @@ static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
     }
     if( p_sys->url.psz_username && *p_sys->url.psz_username )
     {
-        msg_Dbg( p_access, "      user='%s', pwd='%s'",
-                 p_sys->url.psz_username, p_sys->url.psz_password );
+        msg_Dbg( p_access, "      user='%s'", p_sys->url.psz_username );
     }
 
     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
@@ -374,7 +425,7 @@ connect:
 
     if( p_sys->i_code == 401 )
     {
-        char *psz_login = NULL; char *psz_password = NULL;
+        char *psz_login = NULL, *psz_password = NULL;
         char psz_msg[250];
         int i_ret;
         /* FIXME ? */
@@ -428,6 +479,7 @@ connect:
         vlc_UrlClean( &p_sys->url );
         AuthReset( &p_sys->auth );
         vlc_UrlClean( &p_sys->proxy );
+        free( p_sys->psz_proxy_passbuf );
         AuthReset( &p_sys->proxy_auth );
         free( p_sys->psz_mime );
         free( p_sys->psz_pragma );
@@ -518,6 +570,7 @@ connect:
 error:
     vlc_UrlClean( &p_sys->url );
     vlc_UrlClean( &p_sys->proxy );
+    free( p_sys->psz_proxy_passbuf );
     free( p_sys->psz_mime );
     free( p_sys->psz_pragma );
     free( p_sys->psz_location );
@@ -828,6 +881,17 @@ static int Seek( access_t *p_access, int64_t i_pos )
 
     Disconnect( p_access );
 
+    if( p_access->info.i_size
+     && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
+        msg_Err( p_access, "seek to far" );
+        int retval = Seek( p_access, p_access->info.i_size - 1 );
+        if( retval == VLC_SUCCESS ) {
+            uint8_t p_buffer[2];
+            Read( p_access, p_buffer, 1);
+            p_access->info.b_eof  = false;
+        }
+        return retval;
+    }
     if( Connect( p_access, i_pos ) )
     {
         msg_Err( p_access, "seek failed" );
@@ -1088,7 +1152,7 @@ static int Request( access_t *p_access, int64_t i_tell )
     if( p_sys->i_version == 1 )
     {
         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
-                    "Range: bytes=%"PRId64"-\r\n", i_tell );
+                    "Range: bytes=%"PRIu64"-\r\n", i_tell );
     }
 
     /* Cookies */