[vlc-devel] [PATCH 2/3] access/oldhttp: Implement STREAM_GET_HTTP_HEADER

Marvin Scholz epirat07 at gmail.com
Mon Aug 12 11:56:07 CEST 2019


---
 modules/access/http.c | 73 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/modules/access/http.c b/modules/access/http.c
index a1d0593e0f..0893033acc 100644
--- a/modules/access/http.c
+++ b/modules/access/http.c
@@ -48,6 +48,7 @@
 #include <vlc_interrupt.h>
 #include <vlc_keystore.h>
 #include <vlc_memstream.h>
+#include <vlc_vector.h>
 
 #include <assert.h>
 #include <limits.h>
@@ -81,6 +82,13 @@ vlc_module_end ()
  * Local prototypes
  *****************************************************************************/
 
+struct http_header {
+    char *key;
+    char *val; 
+};
+
+typedef struct VLC_VECTOR(struct http_header) http_header_vector;
+
 typedef struct
 {
     vlc_tls_t *stream;
@@ -117,6 +125,8 @@ typedef struct
 
     bool b_reconnect;
     bool b_has_size;
+
+    http_header_vector headers;
 } access_sys_t;
 
 /* */
@@ -132,6 +142,52 @@ static void Disconnect( stream_t * );
 static int AuthCheckReply( stream_t *p_access, const char *psz_header,
                            vlc_url_t *p_url, vlc_http_auth_t *p_auth );
 
+/*
+ * Header list management
+ */
+
+static void http_headers_Init( http_header_vector *headers )
+{
+    vlc_vector_init( headers );
+}
+
+static int http_headers_Add( http_header_vector *headers,
+    const char *key, const char *val )
+{
+    struct http_header h = {
+        .key = strdup( key ),
+        .val = strdup( val ),
+    };
+    
+    if (!h.key || !h.val || !vlc_vector_push( headers, h )) {
+        free( h.key );
+        free( h.val );
+        return VLC_ENOMEM;
+    }
+
+    return VLC_SUCCESS;
+}
+
+static char *http_headers_Get( http_header_vector *headers, const char *key )
+{
+    struct http_header h;
+    vlc_vector_foreach( h, headers ) {
+        if (strcasecmp( h.key, key ) == 0)
+            return strdup( h.val );
+    }
+    return NULL;
+}
+
+static void http_headers_Clear( http_header_vector *headers )
+{
+    struct http_header h;
+    vlc_vector_foreach( h, headers ) {
+        free( h.key );
+        free( h.val );
+    }
+    vlc_vector_clear( headers );
+}
+
 /*****************************************************************************
  * Open:
  *****************************************************************************/
@@ -176,6 +232,7 @@ static int Open( vlc_object_t *p_this )
     if( p_sys->url.i_port <= 0 )
         p_sys->url.i_port = 80;
 
+    http_headers_Init( &p_sys->headers );
     vlc_credential_init( &credential, &p_sys->url );
 
     /* Determine the HTTP user agent */
@@ -348,6 +405,7 @@ disconnect:
 
 error:
     vlc_credential_clean( &credential );
+    http_headers_Clear( &p_sys->headers );
     vlc_UrlClean( &p_sys->url );
     if( p_sys->b_proxy )
         vlc_UrlClean( &p_sys->proxy );
@@ -374,6 +432,8 @@ static void Close( vlc_object_t *p_this )
     if( p_sys->b_proxy )
         vlc_UrlClean( &p_sys->proxy );
 
+    http_headers_Clear( &p_sys->headers );
+
     free( p_sys->psz_mime );
     free( p_sys->psz_location );
 
@@ -600,6 +660,13 @@ static int Control( stream_t *p_access, int i_query, va_list args )
             break;
         }
 
+        case STREAM_GET_HTTP_HEADER:
+        {
+            const char *header = va_arg( args, const char* );
+            *va_arg(args, char **) = http_headers_Get( &p_sys->headers, header );
+            break;
+        }
+
         default:
             return VLC_EGENERIC;
 
@@ -624,6 +691,8 @@ static int Connect( stream_t *p_access )
     free( p_sys->psz_icy_name );
     free( p_sys->psz_icy_title );
 
+    http_headers_Clear( &p_sys->headers );
+
     vlc_http_auth_Init( &p_sys->auth );
     vlc_http_auth_Init( &p_sys->proxy_auth );
     p_sys->psz_location = NULL;
@@ -802,6 +871,10 @@ static int Connect( stream_t *p_access )
             }
         }
 
+        if (p && p[0] != '\0') {
+            http_headers_Add( &p_sys->headers, psz, p);
+        }
+
         if( !strcasecmp( psz, "Content-Length" ) )
         {
             uint64_t i_size = (uint64_t)atoll( p );
-- 
2.19.1



More information about the vlc-devel mailing list