]> git.sesse.net Git - vlc/blobdiff - modules/access/http.c
SFTP: remove more unused strings
[vlc] / modules / access / http.c
index baa1a594f4b3f1460210315d7b8c546940e97669..583cf4a1258448a9a207a314c9a7ad0c3701b7cc 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * http.c: HTTP input module
  *****************************************************************************
- * Copyright (C) 2001-2008 the VideoLAN team
+ * Copyright (C) 2001-2008 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -9,19 +9,19 @@
  *          RĂ©mi Denis-Courmont <rem # videolan.org>
  *          Antoine Cellerier <dionoea at videolan dot org>
  *
- * 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <assert.h>
 #include <limits.h>
 
-#ifdef HAVE_LIBPROXY
-#    include <proxy.h>
-#endif
-
-#ifdef WIN32
-#   include <windows.h>
-#endif
-
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -139,7 +131,8 @@ struct access_sys_t
 {
     int fd;
     bool b_error;
-    vlc_tls_t  *p_tls;
+    vlc_tls_creds_t *p_creds;
+    vlc_tls_t *p_tls;
     v_socket_t *p_vs;
 
     /* From uri */
@@ -164,7 +157,6 @@ struct access_sys_t
     char       *psz_location;
     bool b_mms;
     bool b_icecast;
-    bool b_ssl;
 #ifdef HAVE_ZLIB_H
     bool b_compressed;
     struct
@@ -269,7 +261,6 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
     p_sys->psz_user_agent = NULL;
     p_sys->psz_referrer = NULL;
     p_sys->b_pace_control = true;
-    p_sys->b_ssl = false;
 #ifdef HAVE_ZLIB_H
     p_sys->b_compressed = false;
     /* 15 is the max windowBits, +32 to enable optional gzip decoding */
@@ -314,7 +305,9 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
     if( !strncmp( psz_access, "https", 5 ) )
     {
         /* HTTP over SSL */
-        p_sys->b_ssl = true;
+        p_sys->p_creds = vlc_tls_ClientCreate( p_this );
+        if( p_sys->p_creds == NULL )
+            goto error;
         if( p_sys->url.i_port <= 0 )
             p_sys->url.i_port = 443;
     }
@@ -366,117 +359,33 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
 
     /* Check proxy */
     psz = var_InheritString( p_access, "http-proxy" );
-    if( psz )
-    {
-        p_sys->b_proxy = true;
-        vlc_UrlParse( &p_sys->proxy, psz, 0 );
-        free( psz );
-    }
-#ifdef HAVE_LIBPROXY
-    else
+    if( psz == NULL )
     {
-        pxProxyFactory *pf = px_proxy_factory_new();
-        if (pf)
-        {
-            char *buf;
-            int i;
-            i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_location);
-            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
+        char *url;
+
+        if (likely(asprintf(&url, "%s://%s", psz_access,
+                            p_access->psz_location) != -1))
         {
-            msg_Err(p_access, "Allocating memory for libproxy failed");
+            msg_Dbg(p_access, "querying proxy for %s", url);
+            psz = vlc_getProxyUrl(url);
+            free(url);
         }
-    }
-#elif (0) // defined( WIN32 ) The parsing is not complete enough
-    else
-    {
-        /* Try to get the proxy server address from Windows internet settings using registry. */
-        HKEY h_key;
-        /* Open the key */
-        if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft"
-                          "\\Windows\\CurrentVersion\\Internet Settings",
-                          0, KEY_READ, &h_key ) == ERROR_SUCCESS )
-        {
-            DWORD len = sizeof( DWORD );
-            BYTE proxyEnable;
 
-            /* Get the proxy enable value */
-            if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
-                                 &proxyEnable, &len ) == ERROR_SUCCESS
-             && proxyEnable )
-            {
-                /* Proxy is enabled */
-                /* Get the proxy URL :
-                   Proxy server value in the registry can be something like "address:port"
-                   or "ftp=address1:port1;http=address2:port2 ..." depending of the
-                   confirguration. */
-                unsigned char key[256];
-
-                len = sizeof( key );
-                if( RegQueryValueEx( h_key, "ProxyServer", NULL, NULL,
-                                     key, &len ) == ERROR_SUCCESS )
-                {
-                    /* FIXME: This is lame. The string should be tokenized. */
-#warning FIXME.
-                    char *psz_proxy = strstr( (char *)key, "http=" );
-                    if( psz_proxy != NULL )
-                    {
-                        psz_proxy += 5;
-                        char *end = strchr( psz_proxy, ';' );
-                        if( end != NULL )
-                            *end = '\0';
-                    }
-                    else
-                        psz_proxy = (char *)key;
-                    /* Set proxy enable for this connection. */
-                    p_sys->b_proxy = true;
-                    vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
-                }
-            }
-            else
-                msg_Dbg( p_access, "HTTP proxy disabled (MSIE)" );
-            RegCloseKey( h_key );
-        }
+        if (psz != NULL)
+            msg_Dbg(p_access, "proxy: %s", psz);
+        else
+            msg_Dbg(p_access, "no proxy");
     }
-#else
-    else
+    if( psz != NULL )
     {
-        psz = getenv( "http_proxy" );
-        if( psz )
-        {
-            p_sys->b_proxy = true;
-            vlc_UrlParse( &p_sys->proxy, psz, 0 );
-        }
-    }
-#endif
+        p_sys->b_proxy = true;
+        vlc_UrlParse( &p_sys->proxy, psz, 0 );
+        free( psz );
 
-    if( psz ) /* No, this is NOT a use-after-free error */
-    {
         psz = var_InheritString( p_access, "http-proxy-pwd" );
         if( psz )
             p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
-    }
 
-    if( p_sys->b_proxy )
-    {
         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
         {
             msg_Warn( p_access, "invalid proxy host" );
@@ -529,6 +438,11 @@ connect:
 
     if( p_sys->i_code == 401 )
     {
+        if( p_sys->auth.psz_realm == NULL )
+        {
+            msg_Err( p_access, "authentication failed without realm" );
+            goto error;
+        }
         char *psz_login, *psz_password;
         /* FIXME ? */
         if( p_sys->url.psz_username && p_sys->url.psz_password &&
@@ -573,20 +487,19 @@ connect:
             goto error;
         }
 
-
-        /* Do not accept redirection outside of HTTP works */
         const char *psz_protocol;
-        if( !strncmp( p_sys->psz_location, "http:", 5 ) )
+        if( !strncmp( p_sys->psz_location, "http://", 7 ) )
             psz_protocol = "http";
-        else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
+        else if( !strncmp( p_sys->psz_location, "https://", 8 ) )
             psz_protocol = "https";
         else
-        {
-            msg_Err( p_access, "insecure redirection ignored" );
+        {   /* Do not accept redirection outside of HTTP */
+            msg_Err( p_access, "unsupported redirection ignored" );
             goto error;
         }
         free( p_access->psz_location );
-        p_access->psz_location = strdup( p_sys->psz_location );
+        p_access->psz_location = strdup( p_sys->psz_location
+                                       + strlen( psz_protocol ) + 3 );
         /* Clean up current Open() run */
         vlc_UrlClean( &p_sys->url );
         http_auth_Reset( &p_sys->auth );
@@ -600,6 +513,7 @@ connect:
         free( p_sys->psz_referrer );
 
         Disconnect( p_access );
+        vlc_tls_Delete( p_sys->p_creds );
         cookies = p_sys->cookies;
 #ifdef HAVE_ZLIB_H
         inflateEnd( &p_sys->inflate.stream );
@@ -692,6 +606,7 @@ error:
     free( p_sys->psz_referrer );
 
     Disconnect( p_access );
+    vlc_tls_Delete( p_sys->p_creds );
 
     if( p_sys->cookies )
     {
@@ -733,6 +648,7 @@ static void Close( vlc_object_t *p_this )
     free( p_sys->psz_referrer );
 
     Disconnect( p_access );
+    vlc_tls_Delete( p_sys->p_creds );
 
     if( p_sys->cookies )
     {
@@ -996,7 +912,8 @@ static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
         p_sys->inflate.stream.next_out = p_buffer;
 
         i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
-        msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
+        if ( i_ret != Z_OK && i_ret != Z_STREAM_END )
+            msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
 
         return i_len - p_sys->inflate.stream.avail_out;
     }
@@ -1156,7 +1073,7 @@ static int Connect( access_t *p_access, uint64_t i_tell )
     setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
 
     /* Initialize TLS/SSL session */
-    if( p_sys->b_ssl )
+    if( p_sys->p_creds != NULL )
     {
         /* CONNECT to establish TLS tunnel through HTTP proxy */
         if( p_sys->b_proxy )
@@ -1220,8 +1137,8 @@ static int Connect( access_t *p_access, uint64_t i_tell )
         }
 
         /* TLS/SSL handshake */
-        p_sys->p_tls = vlc_tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
-                                             p_sys->url.psz_host );
+        p_sys->p_tls = vlc_tls_ClientSessionCreate( p_sys->p_creds, p_sys->fd,
+                                                p_sys->url.psz_host, "https" );
         if( p_sys->p_tls == NULL )
         {
             msg_Err( p_access, "cannot establish HTTP/TLS session" );
@@ -1270,9 +1187,6 @@ static int Request( access_t *p_access, uint64_t i_tell )
         net_Printf( p_access, p_sys->fd, pvs, "Referer: %s\r\n",
                     p_sys->psz_referrer);
     }
-#ifdef HAVE_ZLIB_H
-    net_Printf( p_access, p_sys->fd, pvs, "Accept-Encoding: gzip, deflate\r\n" );
-#endif
     /* Offset */
     if( p_sys->i_version == 1 && ! p_sys->b_continuous )
     {
@@ -1378,6 +1292,7 @@ static int Request( access_t *p_access, uint64_t i_tell )
     {
         char *psz = net_Gets( p_access, p_sys->fd, pvs );
         char *p;
+        char *p_trailing;
 
         if( psz == NULL )
         {
@@ -1405,7 +1320,19 @@ static int Request( access_t *p_access, uint64_t i_tell )
             goto error;
         }
         *p++ = '\0';
-        while( *p == ' ' ) p++;
+        p += strspn( p, " \t" );
+
+        /* trim trailing white space */
+        p_trailing = p + strlen( p );
+        if( p_trailing > p )
+        {
+            p_trailing--;
+            while( ( *p_trailing == ' ' || *p_trailing == '\t' ) && p_trailing > p )
+            {
+                *p_trailing = '\0';
+                p_trailing--;
+            }
+        }
 
         if( !strcasecmp( psz, "Content-Length" ) )
         {
@@ -1450,9 +1377,9 @@ static int Request( access_t *p_access, uint64_t i_tell )
              * handle it as everyone does. */
             if( p[0] == '/' )
             {
-                const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
+                const char *psz_http_ext = p_sys->p_tls ? "s" : "" ;
 
-                if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
+                if( p_sys->url.i_port == ( p_sys->p_tls ? 443 : 80 ) )
                 {
                     if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
                                  p_sys->url.psz_host, p) < 0 )
@@ -1632,7 +1559,7 @@ static void Disconnect( access_t *p_access )
 
     if( p_sys->p_tls != NULL)
     {
-        vlc_tls_ClientDelete( p_sys->p_tls );
+        vlc_tls_SessionDelete( p_sys->p_tls );
         p_sys->p_tls = NULL;
         p_sys->p_vs = NULL;
     }