]> git.sesse.net Git - vlc/blobdiff - modules/access/http.c
"#if HAVE_FOO" -> "#ifdef HAVE_FOO"
[vlc] / modules / access / http.c
index 384a58c19f100a8eac5029f1277412c4590ad4eb..d0a53d7a84af85189bd0e10fa55745a2ef97a1db 100644 (file)
@@ -123,8 +123,8 @@ vlc_module_begin ()
     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
                  CACHING_TEXT, CACHING_LONGTEXT, true )
         change_safe()
-    add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
-                AGENT_LONGTEXT, true )
+    add_string( "http-user-agent", PACKAGE_NAME" "PACKAGE_VERSION, NULL,
+                AGENT_TEXT, AGENT_LONGTEXT, true )
         change_safe()
     add_bool( "http-reconnect", false, NULL, RECONNECT_TEXT,
               RECONNECT_LONGTEXT, true )
@@ -156,6 +156,7 @@ vlc_module_end ()
 struct access_sys_t
 {
     int fd;
+    bool b_error;
     tls_session_t *p_tls;
     v_socket_t    *p_vs;
 
@@ -448,7 +449,7 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
                           "in registry." );
         }
     }
-#elif HAVE_GETENV
+#elif defined( HAVE_GETENV )
     else
     {
         psz = getenv( "http_proxy" );
@@ -480,8 +481,9 @@ static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
         }
     }
 
-    msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
-             p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
+    msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
+             p_sys->url.psz_host, p_sys->url.i_port,
+             p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
     if( p_sys->b_proxy )
     {
         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
@@ -760,14 +762,16 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
         return 0;
     }
 
-    if( p_sys->b_has_size &&
-        i_len + p_access->info.i_pos > p_access->info.i_size )
+    if( p_sys->b_has_size )
     {
-        if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
-        {
-            p_access->info.b_eof = true;
-            return 0;
-        }
+        /* 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 )
@@ -800,19 +804,14 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
         }
 
         if( i_len > p_sys->i_chunk )
-        {
             i_len = p_sys->i_chunk;
-        }
-    }
-    else if( p_sys->b_has_size && i_len > p_sys->i_remaining) {
-        /* Only ask for the remaining length */
-        i_len = p_sys->i_remaining;
-        if(i_len == 0) {
-            p_access->info.b_eof = true;
-            return 0;
-        }
     }
 
+    if( i_len == 0 )
+    {
+        p_access->info.b_eof = true;
+        return 0;
+    }
 
     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
     {
@@ -835,8 +834,6 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 
     if( i_read > 0 )
     {
-        p_access->info.i_pos += i_read;
-
         if( p_sys->b_chunked )
         {
             p_sys->i_chunk -= i_read;
@@ -848,7 +845,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
             }
         }
     }
-    else if( i_read <= 0 )
+    else
     {
         /*
          * I very much doubt that this will work.
@@ -876,18 +873,26 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
                 p_sys->b_reconnect = false;
                 i_read = Read( p_access, p_buffer, i_len );
                 p_sys->b_reconnect = true;
+
+                return i_read;
             }
         }
 
-        if( i_read == 0 )
+        if( i_read <= 0 )
+        {
             p_access->info.b_eof = true;
-        else if( i_read < 0 )
-            p_access->b_error = true;
+            if( i_read < 0 )
+                p_sys->b_error = true;
+            return 0;
+        }
     }
 
+    assert( i_read >= 0 );
+    p_access->info.i_pos += i_read;
     if( p_sys->b_has_size )
     {
-        assert( i_read <= p_sys->i_remaining );
+        assert( p_access->info.i_pos <= p_access->info.i_size );
+        assert( (unsigned)i_read <= p_sys->i_remaining );
         p_sys->i_remaining -= i_read;
     }
 
@@ -1163,7 +1168,7 @@ static int Connect( access_t *p_access, uint64_t i_tell )
                 return -1;
             }
 
-            net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
+            net_Printf( p_access, p_sys->fd, NULL,
                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
                         p_sys->url.psz_host, p_sys->url.i_port,
                         p_sys->i_version,
@@ -1202,7 +1207,7 @@ static int Connect( access_t *p_access, uint64_t i_tell )
 
                 free( psz );
 
-                if( !vlc_object_alive (p_access) || p_access->b_error )
+                if( !vlc_object_alive (p_access) || p_sys->b_error )
                 {
                     Disconnect( p_access );
                     return -1;
@@ -1239,14 +1244,14 @@ static int Request( access_t *p_access, uint64_t i_tell )
     {
         if( p_sys->url.psz_path )
         {
-            net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
+            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( VLC_OBJECT(p_access), p_sys->fd, NULL,
+            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 );
@@ -1261,26 +1266,26 @@ static int Request( access_t *p_access, uint64_t i_tell )
         }
         if( p_sys->url.i_port != (pvs ? 443 : 80) )
         {
-            net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
+            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( VLC_OBJECT(p_access), p_sys->fd, pvs,
+            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 );
         }
     }
     /* User Agent */
-    net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
+    net_Printf( p_access, p_sys->fd, pvs, "User-Agent: %s\r\n",
                 p_sys->psz_user_agent );
     /* Offset */
     if( p_sys->i_version == 1 && ! p_sys->b_continuous )
     {
         p_sys->b_persist = true;
-        net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
+        net_Printf( p_access, p_sys->fd, pvs,
                     "Range: bytes=%"PRIu64"-\r\n", i_tell );
     }
 
@@ -1302,7 +1307,7 @@ static int Request( access_t *p_access, uint64_t i_tell )
             if( is_in_right_domain )
             {
                 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
-                if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
+                if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
                     msg_Err( p_access, "failed to send Cookie" );
             }
             free( psz_cookie_content );
@@ -1319,10 +1324,10 @@ static int Request( access_t *p_access, uint64_t i_tell )
         AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
 
     /* ICY meta data request */
-    net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
+    net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
 
 
-    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
+    if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
     {
         msg_Err( p_access, "failed to send request" );
         Disconnect( p_access );
@@ -1387,7 +1392,7 @@ static int Request( access_t *p_access, uint64_t i_tell )
             goto error;
         }
 
-        if( !vlc_object_alive (p_access) || p_access->b_error )
+        if( !vlc_object_alive (p_access) || p_sys->b_error )
         {
             free( psz );
             goto error;
@@ -1425,6 +1430,7 @@ static int Request( access_t *p_access, uint64_t i_tell )
             sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
             if(i_nend > i_ntell ) {
                 p_access->info.i_pos = i_ntell;
+                p_sys->i_icy_offset  = i_ntell;
                 p_sys->i_remaining = i_nend+1-i_ntell;
                 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
                 if(i_size > p_access->info.i_size) {
@@ -1763,7 +1769,7 @@ static void AuthReply( access_t *p_access, const char *psz_prefix,
     if ( psz_value == NULL )
         return;
 
-    net_Printf( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs,
+    net_Printf( p_access, p_sys->fd, p_sys->p_vs,
                 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
     free( psz_value );
 }