]> git.sesse.net Git - vlc/blobdiff - modules/access/http.c
No need to check HAVE_GETENV
[vlc] / modules / access / http.c
index 90b7d4867c13cbbb941753a8aa6f9c4e15250e88..e9950564899a3dcf04b2f277ab29ecf3de23bc12 100644 (file)
@@ -105,6 +105,12 @@ static void Close( vlc_object_t * );
     "server for all URL. Don't take into account bypasses settings and auto " \
     "configuration scripts.")
 
+#define REFERER_TEXT N_("HTTP referer value")
+#define REFERER_LONGTEXT N_("Customize the HTTP referer, simulating a previous document")
+
+#define UA_TEXT N_("User Agent")
+#define UA_LONGTEXT N_("You can use a custom User agent or use a known one")
+
 vlc_module_begin ()
     set_description( N_("HTTP input") )
     set_capability( "access", 0 )
@@ -112,27 +118,29 @@ vlc_module_begin ()
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_ACCESS )
 
-    add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
+    add_string( "http-proxy", NULL, PROXY_TEXT, PROXY_LONGTEXT,
                 false )
-    add_password( "http-proxy-pwd", NULL, NULL,
+    add_password( "http-proxy-pwd", NULL,
                   PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
-    add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
+    add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000,
                  CACHING_TEXT, CACHING_LONGTEXT, true )
         change_safe()
-    add_string( "http-user-agent", NULL, NULL, NULL, NULL, false )
+    add_string( "http-referrer", NULL, REFERER_TEXT, REFERER_LONGTEXT, false )
+        change_safe()
+    add_string( "http-user-agent", NULL, UA_TEXT, UA_LONGTEXT, false )
         change_safe()
         change_private()
-    add_bool( "http-reconnect", false, NULL, RECONNECT_TEXT,
+    add_bool( "http-reconnect", false, RECONNECT_TEXT,
               RECONNECT_LONGTEXT, true )
-    add_bool( "http-continuous", false, NULL, CONTINUOUS_TEXT,
+    add_bool( "http-continuous", false, CONTINUOUS_TEXT,
               CONTINUOUS_LONGTEXT, true )
         change_safe()
-    add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
+    add_bool( "http-forward-cookies", true, FORWARD_COOKIES_TEXT,
               FORWARD_COOKIES_LONGTEXT, true )
-    add_integer( "http-max-redirect", 5, NULL, MAX_REDIRECT_TEXT,
+    add_integer( "http-max-redirect", 5, MAX_REDIRECT_TEXT,
                  MAX_REDIRECT_LONGTEXT, true )
 #ifdef WIN32
-    add_bool( "http-use-IE-proxy", false, NULL, USE_IE_PROXY_TEXT,
+    add_bool( "http-use-IE-proxy", false, USE_IE_PROXY_TEXT,
               USE_IE_PROXY_LONGTEXT, true )
 #endif
     add_obsolete_string("http-user")
@@ -156,6 +164,7 @@ struct access_sys_t
     /* From uri */
     vlc_url_t url;
     char    *psz_user_agent;
+    char    *psz_referrer;
     http_auth_t auth;
 
     /* Proxy */
@@ -281,6 +290,7 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
     p_sys->b_icecast = false;
     p_sys->psz_location = NULL;
     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
@@ -340,13 +350,19 @@ 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 */
     p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
-    for( char *p = p_sys->psz_user_agent; *p; p++ )
+    if (p_sys->psz_user_agent)
     {
-        uint8_t c = *p;
-        if( c < 32 || strchr( "()<>@,;:\\\"[]?={}", c ) )
-            *p = '_'; /* remove potentially harmful characters */
+        for( char *p = p_sys->psz_user_agent; *p; p++ )
+        {
+            uint8_t c = *p;
+            if( c < 32 || strchr( "()<>@,;:\\\"[]?={}", c ) )
+                *p = '_'; /* remove potentially harmful characters */
+        }
     }
 
+    /* HTTP referrer */
+    p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );
+
     /* Check proxy */
     psz = var_InheritString( p_access, "http-proxy" );
     if( psz )
@@ -449,7 +465,6 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
                           "in registry." );
         }
     }
-#elif defined( HAVE_GETENV )
     else
     {
         psz = getenv( "http_proxy" );
@@ -459,7 +474,6 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
             vlc_UrlParse( &p_sys->proxy, psz, 0 );
         }
     }
-#endif
 
     if( psz ) /* No, this is NOT a use-after-free error */
     {
@@ -593,6 +607,7 @@ connect:
         free( p_sys->psz_pragma );
         free( p_sys->psz_location );
         free( p_sys->psz_user_agent );
+        free( p_sys->psz_referrer );
 
         Disconnect( p_access );
         cookies = p_sys->cookies;
@@ -687,6 +702,7 @@ error:
     free( p_sys->psz_pragma );
     free( p_sys->psz_location );
     free( p_sys->psz_user_agent );
+    free( p_sys->psz_referrer );
 
     Disconnect( p_access );
 
@@ -727,6 +743,7 @@ static void Close( vlc_object_t *p_this )
     free( p_sys->psz_icy_title );
 
     free( p_sys->psz_user_agent );
+    free( p_sys->psz_referrer );
 
     Disconnect( p_access );
 
@@ -757,10 +774,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
     int i_read;
 
     if( p_sys->fd == -1 )
-    {
-        p_access->info.b_eof = true;
-        return 0;
-    }
+        goto fatal;
 
     if( p_sys->b_has_size )
     {
@@ -777,10 +791,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
     if( p_sys->b_chunked )
     {
         if( p_sys->i_chunk < 0 )
-        {
-            p_access->info.b_eof = true;
-            return 0;
-        }
+            goto fatal;
 
         if( p_sys->i_chunk <= 0 )
         {
@@ -798,8 +809,7 @@ 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;
-                p_access->info.b_eof = true;
-                return 0;
+                goto fatal;
             }
         }
 
@@ -808,10 +818,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
     }
 
     if( i_len == 0 )
-    {
-        p_access->info.b_eof = true;
-        return 0;
-    }
+        goto fatal;
 
     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
     {
@@ -821,10 +828,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
         if( i_next == p_sys->i_icy_meta )
         {
             if( ReadICYMeta( p_access ) )
-            {
-                p_access->info.b_eof = true;
-                return -1;
-            }
+                goto fatal;
         }
         if( i_len > i_next )
             i_len = i_next;
@@ -880,10 +884,9 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 
         if( i_read <= 0 )
         {
-            p_access->info.b_eof = true;
             if( i_read < 0 )
                 p_sys->b_error = true;
-            return 0;
+            goto fatal;
         }
     }
 
@@ -897,6 +900,10 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
     }
 
     return i_read;
+
+fatal:
+    p_access->info.b_eof = true;
+    return 0;
 }
 
 static int ReadICYMeta( access_t *p_access )
@@ -984,7 +991,7 @@ static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
 
         if( p_sys->inflate.stream.avail_in == 0 )
         {
-            ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
+            ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
             if( i_read <= 0 ) return i_read;
             p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
             p_sys->inflate.stream.avail_in = i_read;
@@ -1282,6 +1289,13 @@ static int Request( access_t *p_access, uint64_t i_tell )
     net_Printf( p_access, p_sys->fd, pvs,
                 "User-Agent: %s\r\n",
                 p_sys->psz_user_agent );
+    /* Referrer */
+    if (p_sys->psz_referrer)
+    {
+        net_Printf( p_access, p_sys->fd, pvs,
+                    "Referer: %s\r\n",
+                    p_sys->psz_referrer);
+    }
     /* Offset */
     if( p_sys->i_version == 1 && ! p_sys->b_continuous )
     {