]> git.sesse.net Git - vlc/blobdiff - modules/access/http.c
- TLS API cleanup
[vlc] / modules / access / http.c
index 6d716ea9eee2883e2e49451783176fa7147d66ab..2371e5aa289cb7d8a0be3e19bccfee11707f5861 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * http.c: HTTP input module
  *****************************************************************************
- * Copyright (C) 2001-2004 VideoLAN
+ * Copyright (C) 2001-2005 VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -33,6 +33,7 @@
 #include "vlc_playlist.h"
 #include "vlc_meta.h"
 #include "network.h"
+#include "vlc_tls.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -67,9 +68,16 @@ static void Close( vlc_object_t * );
 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
     "in case it was untimely closed.")
 
+#define CONTINUOUS_TEXT N_("Continuous stream")
+#define CONTINUOUS_LONGTEXT N_("Enable this option to read a file that is " \
+    "being constantly updated (for example, a JPG file on a server)")
+
 vlc_module_begin();
     set_description( _("HTTP input") );
     set_capability( "access2", 0 );
+    set_shortname( _( "HTTP/HTTPS" ) );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_ACCESS );
 
     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
                 VLC_FALSE );
@@ -81,10 +89,14 @@ vlc_module_begin();
                 AGENT_LONGTEXT, VLC_FALSE );
     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
               RECONNECT_LONGTEXT, VLC_TRUE );
+    add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
+              CONTINUOUS_LONGTEXT, VLC_TRUE );
 
     add_shortcut( "http" );
     add_shortcut( "http4" );
     add_shortcut( "http6" );
+    add_shortcut( "https" );
+    add_shortcut( "unsv" );
     set_callbacks( Open, Close );
 vlc_module_end();
 
@@ -94,6 +106,8 @@ vlc_module_end();
 struct access_sys_t
 {
     int fd;
+    tls_session_t *p_tls;
+    v_socket_t    *p_vs;
 
     /* From uri */
     vlc_url_t url;
@@ -115,6 +129,7 @@ struct access_sys_t
     char       *psz_location;
     vlc_bool_t b_mms;
     vlc_bool_t b_icecast;
+    vlc_bool_t b_ssl;
 
     vlc_bool_t b_chunked;
     int64_t    i_chunk;
@@ -124,8 +139,11 @@ struct access_sys_t
     char       *psz_icy_genre;
     char       *psz_icy_title;
 
+    int i_remaining;
+
     vlc_bool_t b_seekable;
     vlc_bool_t b_reconnect;
+    vlc_bool_t b_continuous;
     vlc_bool_t b_pace_control;
 };
 
@@ -137,6 +155,8 @@ static int Control( access_t *, int, va_list );
 /* */
 static void ParseURL( access_sys_t *, char *psz_url );
 static int  Connect( access_t *, int64_t );
+static int Request( access_t *p_access, int64_t i_tell );
+static void Disconnect( access_t * );
 
 /*****************************************************************************
  * Open:
@@ -197,10 +217,14 @@ static int Open( vlc_object_t *p_this )
     p_sys->psz_location = NULL;
     p_sys->psz_user_agent = NULL;
     p_sys->b_pace_control = VLC_TRUE;
+    p_sys->b_ssl = VLC_FALSE;
+    p_sys->p_tls = NULL;
+    p_sys->p_vs = NULL;
     p_sys->i_icy_meta = 0;
     p_sys->psz_icy_name = NULL;
     p_sys->psz_icy_genre = NULL;
     p_sys->psz_icy_title = NULL;
+    p_sys->i_remaining = 0;
 
     /* Parse URI */
     ParseURL( p_sys, p_access->psz_path );
@@ -209,9 +233,17 @@ static int Open( vlc_object_t *p_this )
         msg_Warn( p_access, "invalid host" );
         goto error;
     }
-    if( p_sys->url.i_port <= 0 )
+    if( !strncmp( p_access->psz_access, "https", 5 ) )
+    {
+        /* SSL over HTTP */
+        p_sys->b_ssl = VLC_TRUE;
+        if( p_sys->url.i_port <= 0 )
+            p_sys->url.i_port = 443;
+    }
+    else
     {
-        p_sys->url.i_port = 80;
+        if( p_sys->url.i_port <= 0 )
+            p_sys->url.i_port = 80;
     }
     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
     {
@@ -269,6 +301,7 @@ static int Open( vlc_object_t *p_this )
     }
 
     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
+    p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
 
     /* Connect */
     if( Connect( p_access, 0 ) )
@@ -288,6 +321,7 @@ static int Open( vlc_object_t *p_this )
         p_sys->psz_location && *p_sys->psz_location )
     {
         playlist_t * p_playlist;
+        input_item_t *p_input_item;
 
         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
 
@@ -298,13 +332,34 @@ static int Open( vlc_object_t *p_this )
             msg_Err( p_access, "redirection failed: can't find playlist" );
             goto error;
         }
-        p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
-        playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
-                      PLAYLIST_INSERT,
-                      p_playlist->i_index + 1 );
+
+        /* Change the uri */
+        vlc_mutex_lock( &p_playlist->object_lock );
+        p_input_item = &p_playlist->status.p_item->input;
+        vlc_mutex_lock( &p_input_item->lock );
+        free( p_input_item->psz_uri );
+        free( p_access->psz_path );
+        p_input_item->psz_uri = strdup( p_sys->psz_location );
+        p_access->psz_path = strdup( p_sys->psz_location );
+        vlc_mutex_unlock( &p_input_item->lock );
+        vlc_mutex_unlock( &p_playlist->object_lock );
         vlc_object_release( p_playlist );
 
-        p_access->info.i_size = 0;  /* Force to stop reading */
+        /* Clean up current Open() run */
+        vlc_UrlClean( &p_sys->url );
+        vlc_UrlClean( &p_sys->proxy );
+        if( p_sys->psz_mime ) free( p_sys->psz_mime );
+        if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
+        if( p_sys->psz_location ) free( p_sys->psz_location );
+        if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
+        if( p_sys->psz_user ) free( p_sys->psz_user );
+        if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
+
+        Disconnect( p_access );
+        free( p_sys );
+
+        /* Do new Open() run with new data */
+        return Open( p_this );
     }
 
     if( p_sys->b_mms )
@@ -313,30 +368,34 @@ static int Open( vlc_object_t *p_this )
         goto error;
     }
 
-    if( p_sys->b_icecast )
-    {
-        if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
-            p_access->psz_demux = strdup( "mp3" );
-    }
-
-    if( !strcmp( p_sys->psz_protocol, "ICY" ) )
+    if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
     {
-        if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
-            p_access->psz_demux = strdup( "nsv" );
-        else if( p_sys->psz_mime &&
-                 ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
-                   !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
-            p_access->psz_demux = strdup( "m4a" );
-        else
-            p_access->psz_demux = strdup( "mp3" );
-
-        msg_Info( p_access, "ICY server found, %s demuxer selected",
-                  p_access->psz_demux );
-
-#if 0   /* Doesn't work really well because of the pre-buffering in shoutcast
-         * servers (the buffer content will be sent as fast as possible). */
-        p_sys->b_pace_control = VLC_FALSE;
+        if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
+        { 
+            if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
+                   !strcasecmp( p_sys->psz_mime, "video/nsa") )
+                p_access->psz_demux = strdup( "nsv" );
+            else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
+                     !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
+                p_access->psz_demux = strdup( "m4a" );
+            else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
+                p_access->psz_demux = strdup( "mp3" );
+
+            msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
+                      p_access->psz_demux );
+
+#if 0       /* Doesn't work really well because of the pre-buffering in
+             * shoutcast servers (the buffer content will be sent as fast as
+             * possible). */
+            p_sys->b_pace_control = VLC_FALSE;
 #endif
+        }
+        else if( !p_sys->psz_mime )
+        {
+             /* Shoutcast */
+             p_access->psz_demux = strdup( "mp3" );
+        }
+        /* else probably Ogg Vorbis */
     }
 
     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
@@ -356,10 +415,7 @@ error:
     if( p_sys->psz_user ) free( p_sys->psz_user );
     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
 
-    if( p_sys->fd > 0 )
-    {
-        net_Close( p_sys->fd );
-    }
+    Disconnect( p_access );
     free( p_sys );
     return VLC_EGENERIC;
 }
@@ -388,10 +444,7 @@ static void Close( vlc_object_t *p_this )
 
     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
 
-    if( p_sys->fd > 0 )
-    {
-        net_Close( p_sys->fd );
-    }
+    Disconnect( p_access );
     free( p_sys );
 }
 
@@ -420,6 +473,7 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
             return 0;
         }
     }
+
     if( p_sys->b_chunked )
     {
         if( p_sys->i_chunk < 0 )
@@ -430,7 +484,7 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
 
         if( p_sys->i_chunk <= 0 )
         {
-            char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
+            char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
             /* read the chunk header */
             if( psz == NULL )
             {
@@ -454,11 +508,24 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
         }
     }
 
+    if( p_sys->b_continuous && i_len > p_sys->i_remaining )
+    {
+        /* Only ask for the remaining length */
+        int i_new_len = p_sys->i_remaining;
+        if( i_new_len == 0 )
+        {
+            Request( p_access, 0 );
+            i_read = Read( p_access, p_buffer, i_len );
+            return i_read;
+        }
+        i_len = i_new_len;
+    }
+
     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
     {
-        int64_t i_next = p_sys->i_icy_meta - 
+        int64_t i_next = p_sys->i_icy_meta -
                                     p_access->info.i_pos % p_sys->i_icy_meta;
-        
+
         if( i_next == p_sys->i_icy_meta )
         {
             if( ReadICYMeta( p_access ) )
@@ -471,7 +538,8 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
             i_len = i_next;
     }
 
-    i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len, VLC_FALSE );
+    i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
+
     if( i_read > 0 )
     {
         p_access->info.i_pos += i_read;
@@ -482,17 +550,30 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
             if( p_sys->i_chunk <= 0 )
             {
                 /* read the empty line */
-                char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
+                char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
                 if( psz ) free( psz );
             }
         }
     }
     else if( i_read == 0 )
     {
+        /*
+         * I very much doubt that this will work.
+         * If i_read == 0, the connection *IS* dead, so the only
+         * sensible thing to do is Disconnect() and then retry.
+         * Otherwise, I got recv() completely wrong. -- Courmisch
+         */
+        if( p_sys->b_continuous )
+        {
+            Request( p_access, 0 );
+            p_sys->b_continuous = VLC_FALSE;
+            i_read = Read( p_access, p_buffer, i_len );
+            p_sys->b_continuous = VLC_TRUE;
+        }
+        Disconnect( p_access );
         if( p_sys->b_reconnect )
         {
             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
-            net_Close( p_sys->fd ); p_sys->fd = -1;
             if( Connect( p_access, p_access->info.i_pos ) )
             {
                 msg_Dbg( p_access, "reconnection failed" );
@@ -508,6 +589,11 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
     }
 
+    if( p_sys->b_continuous )
+    {
+        p_sys->i_remaining -= i_read;
+    }
+
     return i_read;
 }
 
@@ -521,7 +607,8 @@ static int ReadICYMeta( access_t *p_access )
     char *p;
 
     /* Read meta data length */
-    i_read = net_Read( p_access, p_sys->fd, NULL, buffer, 1, VLC_TRUE );
+    i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, buffer, 1,
+                       VLC_TRUE );
     if( i_read <= 0 )
         return VLC_EGENERIC;
 
@@ -532,7 +619,7 @@ static int ReadICYMeta( access_t *p_access )
     msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
 
     psz_meta = malloc( buffer[0] * 16 + 1 );
-    i_read = net_Read( p_access, p_sys->fd, NULL,
+    i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs,
                        psz_meta, buffer[0] * 16, VLC_TRUE );
 
     if( i_read != buffer[0] * 16 )
@@ -540,7 +627,7 @@ static int ReadICYMeta( access_t *p_access )
 
     psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
 
-    msg_Warn( p_access, "icy-meta=%s", psz_meta );
+    msg_Dbg( p_access, "icy-meta=%s", psz_meta );
 
     /* Now parse the meta */
     /* Look for StreamTitle= */
@@ -571,7 +658,7 @@ static int ReadICYMeta( access_t *p_access )
 
     free( psz_meta );
 
-    msg_Warn( p_access, "New Title=%s", p_sys->psz_icy_title );
+    msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
 
     return VLC_SUCCESS;
 }
@@ -581,11 +668,9 @@ static int ReadICYMeta( access_t *p_access )
  *****************************************************************************/
 static int Seek( access_t *p_access, int64_t i_pos )
 {
-    access_sys_t *p_sys = p_access->p_sys;
-
     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
 
-    net_Close( p_sys->fd ); p_sys->fd = -1;
+    Disconnect( p_access );
 
     if( Connect( p_access, i_pos ) )
     {
@@ -621,7 +706,12 @@ static int Control( access_t *p_access, int i_query, va_list args )
         case ACCESS_CAN_PAUSE:
         case ACCESS_CAN_CONTROL_PACE:
             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
+
+#if 0       /* Disable for now until we have a clock synchro algo
+             * which works with something else than MPEG over UDP */
             *pb_bool = p_sys->b_pace_control;
+#endif
+            *pb_bool = VLC_TRUE;
             break;
 
         /* */
@@ -724,7 +814,6 @@ static int Connect( access_t *p_access, int64_t i_tell )
 {
     access_sys_t   *p_sys = p_access->p_sys;
     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
-    char           *psz;
 
     /* Clean info */
     if( p_sys->psz_location ) free( p_sys->psz_location );
@@ -760,6 +849,37 @@ static int Connect( access_t *p_access, int64_t i_tell )
         return VLC_EGENERIC;
     }
 
+    /* Initialize TLS/SSL session */
+    /* FIXME: support proxy CONNECT for HTTP/SSL */
+    if( p_sys->b_ssl == VLC_TRUE )
+    {
+        if( p_sys->b_proxy )
+        {
+            msg_Err( p_access, "HTTP/SSL through HTTP proxy not supported yet" );
+            Disconnect( p_access );
+            return VLC_EGENERIC;
+        }
+
+        p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd, NULL );
+        if( p_sys->p_tls == NULL )
+        {
+            msg_Err( p_access, "cannot establish HTTP/SSL session" );
+            Disconnect( p_access );
+            return VLC_EGENERIC;
+        }
+        p_sys->p_vs = &p_sys->p_tls->sock;
+    }
+
+    return Request( p_access,i_tell );
+}
+
+
+static int Request( access_t *p_access, int64_t i_tell )
+{
+    access_sys_t   *p_sys = p_access->p_sys;
+    char           *psz ;
+    v_socket_t     *pvs = p_sys->p_vs;
+
     if( p_sys->b_proxy )
     {
         if( p_sys->url.psz_path )
@@ -786,27 +906,28 @@ static int Connect( access_t *p_access, int64_t i_tell )
         }
         if( p_sys->url.i_port != 80)
         {
-            net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
+            net_Printf( VLC_OBJECT(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, NULL,
+        {
+            net_Printf( VLC_OBJECT(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, NULL, "User-Agent: %s\r\n",
+    net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
                 p_sys->psz_user_agent );
     /* Offset */
     if( p_sys->i_version == 1 )
     {
-        net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
+        net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
                     "Range: bytes="I64Fd"-\r\n", i_tell );
     }
+
     /* Authentification */
     if( p_sys->psz_user && *p_sys->psz_user )
     {
@@ -817,27 +938,38 @@ static int Connect( access_t *p_access, int64_t i_tell )
                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
 
         b64 = vlc_b64_encode( buf );
+        free( buf );
 
-        net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
+        net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
                     "Authorization: Basic %s\r\n", b64 );
         free( b64 );
     }
 
     /* ICY meta data request */
-    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "Icy-MetaData: 1\r\n" );
+    net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
 
 
-    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "Connection: Close\r\n");
+    if( p_sys->b_continuous && p_sys->i_version == 1 )
+    {
+        net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
+                    "Connection: keep-alive\r\n" );
+    }
+    else
+    {
+        net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
+                    "Connection: Close\r\n");
+        p_sys->b_continuous = VLC_FALSE;
+    }
 
-    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
+    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
     {
         msg_Err( p_access, "failed to send request" );
-        net_Close( p_sys->fd ); p_sys->fd = -1;
+        Disconnect( p_access );
         return VLC_EGENERIC;
     }
 
     /* Read Answer */
-    if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
+    if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
     {
         msg_Err( p_access, "failed to read answer" );
         goto error;
@@ -879,7 +1011,7 @@ static int Connect( access_t *p_access, int64_t i_tell )
 
     for( ;; )
     {
-        char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
+        char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
         char *p;
 
         if( psz == NULL )
@@ -907,8 +1039,17 @@ static int Connect( access_t *p_access, int64_t i_tell )
 
         if( !strcasecmp( psz, "Content-Length" ) )
         {
-            p_access->info.i_size = i_tell + atoll( p );
-            msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
+            if( p_sys->b_continuous )
+            {
+                p_access->info.i_size = -1;
+                msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
+                p_sys->i_remaining = atoll( p );
+            }
+            else
+            {
+                p_access->info.i_size = i_tell + atoll( p );
+                msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
+            }
         }
         else if( !strcasecmp( psz, "Location" ) )
         {
@@ -924,7 +1065,7 @@ static int Connect( access_t *p_access, int64_t i_tell )
         else if( !strcasecmp( psz, "Pragma" ) )
         {
             if( !strcasecmp( psz, "Pragma: features" ) )
-               p_sys->b_mms = VLC_TRUE;
+                p_sys->b_mms = VLC_TRUE;
             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
             p_sys->psz_pragma = strdup( p );
             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
@@ -935,11 +1076,12 @@ static int Connect( access_t *p_access, int64_t i_tell )
             if( !strncasecmp( p, "Icecast", 7 ) ||
                 !strncasecmp( p, "Nanocaster", 10 ) )
             {
-                /* Remember if this is Icecast 
-                 * we need to force mp3 in some cases without breaking autodetection */
+                /* Remember if this is Icecast
+                 * we need to force demux in this case without breaking
+                 *  autodetection */
 
-                /* Let live365 streams (nanocaster) piggyback on the icecast routine. 
-                 * They look very similar */
+                /* Let live 365 streams (nanocaster) piggyback on the icecast
+                 * routine. They look very similar */
 
                 p_sys->b_reconnect = VLC_TRUE;
                 p_sys->b_pace_control = VLC_FALSE;
@@ -968,6 +1110,10 @@ static int Connect( access_t *p_access, int64_t i_tell )
             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
             p_sys->psz_icy_name = strdup( p );
             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
+
+            p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
+            p_sys->b_reconnect = VLC_TRUE;
+            p_sys->b_pace_control = VLC_FALSE;
         }
         else if( !strcasecmp( psz, "Icy-Genre" ) )
         {
@@ -991,7 +1137,27 @@ static int Connect( access_t *p_access, int64_t i_tell )
     return VLC_SUCCESS;
 
 error:
-    net_Close( p_sys->fd ); p_sys->fd = -1;
+    Disconnect( p_access );
     return VLC_EGENERIC;
 }
 
+/*****************************************************************************
+ * Disconnect:
+ *****************************************************************************/
+static void Disconnect( access_t *p_access )
+{
+    access_sys_t *p_sys = p_access->p_sys;
+
+    if( p_sys->p_tls != NULL)
+    {
+        tls_ClientDelete( p_sys->p_tls );
+        p_sys->p_tls = NULL;
+        p_sys->p_vs = NULL;
+    }
+    if( p_sys->fd != -1)
+    {
+        net_Close(p_sys->fd);
+        p_sys->fd = -1;
+    }
+    
+}