rtspconnection: Add API for adding extra http request headers

This commit adds capability to add custom headers to any http requests
during http tunnel mode. If header exist new header will replace old.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5268>
This commit is contained in:
Daniel Moberg 2023-08-23 15:00:27 +02:00 committed by GStreamer Marge Bot
parent 4bda59f88d
commit 7446839e0d
3 changed files with 90 additions and 0 deletions

View file

@ -82,6 +82,30 @@ and/or use gtk-doc annotations. -->
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.c">This object manages the RTSP connection to the server. It provides function
to receive and send bytes and messages.</doc>
<source-position filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.h"/>
<method name="add_extra_http_request_header" c:identifier="gst_rtsp_connection_add_extra_http_request_header" version="1.24">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.c">Add header to be appended to any HTTP request made by connection.
If the header already exists then the old header is replaced by the new header.
Only applicable in HTTP tunnel mode.</doc>
<source-position filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.h"/>
<return-value transfer-ownership="none">
<type name="none" c:type="void"/>
</return-value>
<parameters>
<instance-parameter name="conn" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.c">a #GstRTSPConnection</doc>
<type name="RTSPConnection" c:type="GstRTSPConnection*"/>
</instance-parameter>
<parameter name="key" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.c">HTTP header name</doc>
<type name="utf8" c:type="const gchar*"/>
</parameter>
<parameter name="value" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.c">HTTP header value</doc>
<type name="utf8" c:type="const gchar*"/>
</parameter>
</parameters>
</method>
<method name="clear_auth_params" c:identifier="gst_rtsp_connection_clear_auth_params">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.c">Clear the list of authentication directives stored in @conn.</doc>
<source-position filename="../subprojects/gst-plugins-base/gst-libs/gst/rtsp/gstrtspconnection.h"/>

View file

@ -135,6 +135,12 @@ gst_rtsp_serialized_message_clear (GstRTSPSerializedMessage * msg)
#define SEND_FLAGS 0
#endif
typedef struct
{
gchar *key;
gchar *value;
} GstRTSPExtraHttpHeader;
typedef enum
{
TUNNEL_STATE_NONE,
@ -218,6 +224,9 @@ struct _GstRTSPConnection
gchar *proxy_host;
guint proxy_port;
/* HTTP tunneling */
GArray *extra_http_headers;
};
enum
@ -417,6 +426,9 @@ gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
newconn->content_length_limit = G_MAXUINT;
newconn->extra_http_headers =
g_array_new (FALSE, FALSE, sizeof (GstRTSPExtraHttpHeader));
*conn = newconn;
return GST_RTSP_OK;
@ -864,6 +876,21 @@ get_tunneled_connection_uri_strdup (GstRTSPUrl * url, guint16 port)
url->query ? url->query : "");
}
static void
add_extra_headers (GstRTSPMessage * msg, GArray * headers)
{
for (int i = 0; i < headers->len; i++) {
GstRTSPExtraHttpHeader *hdr =
&g_array_index (headers, GstRTSPExtraHttpHeader, i);
/* Remove any existing header */
gst_rtsp_message_remove_header_by_name (msg, hdr->key, -1);
/* and add... */
gst_rtsp_message_add_header_by_name (msg, hdr->key, hdr->value);
}
}
static GstRTSPResult
setup_tunneling (GstRTSPConnection * conn, gint64 timeout, gchar * uri,
GstRTSPMessage * response)
@ -905,6 +932,7 @@ setup_tunneling (GstRTSPConnection * conn, gint64 timeout, gchar * uri,
gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, host);
add_extra_headers (msg, conn->extra_http_headers);
/* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
* request from being base64 encoded */
@ -992,6 +1020,7 @@ setup_tunneling (GstRTSPConnection * conn, gint64 timeout, gchar * uri,
"Sun, 9 Jan 1972 00:00:00 GMT");
gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_LENGTH, "32767");
gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, host);
add_extra_headers (msg, conn->extra_http_headers);
/* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
* request from being base64 encoded */
@ -2994,6 +3023,15 @@ gst_rtsp_connection_free (GstRTSPConnection * conn)
g_timer_destroy (conn->timer);
gst_rtsp_url_free (conn->url);
g_free (conn->proxy_host);
for (gint i = 0; i < conn->extra_http_headers->len; i++) {
GstRTSPExtraHttpHeader *header =
&g_array_index (conn->extra_http_headers, GstRTSPExtraHttpHeader, i);
g_free (header->key);
g_free (header->value);
}
g_array_free (conn->extra_http_headers, TRUE);
g_free (conn);
return res;
@ -3635,6 +3673,31 @@ gst_rtsp_connection_get_ignore_x_server_reply (const GstRTSPConnection * conn)
return conn->ignore_x_server_reply;
}
/**
* gst_rtsp_connection_add_extra_http_request_header:
* @conn: a #GstRTSPConnection
* @key: HTTP header name
* @value: HTTP header value
*
* Add header to be appended to any HTTP request made by connection.
* If the header already exists then the old header is replaced by the new header.
*
* Only applicable in HTTP tunnel mode.
*
* Since: 1.24
*/
void
gst_rtsp_connection_add_extra_http_request_header (GstRTSPConnection * conn,
const gchar * key, const gchar * value)
{
GstRTSPExtraHttpHeader header;
header.key = strdup (key);
header.value = strdup (value);
g_array_append_val (conn->extra_http_headers, header);
}
/**
* gst_rtsp_connection_do_tunnel:
* @conn: a #GstRTSPConnection

View file

@ -241,6 +241,9 @@ void gst_rtsp_connection_set_ignore_x_server_reply (GstRTSPConnect
GST_RTSP_API
gboolean gst_rtsp_connection_get_ignore_x_server_reply (const GstRTSPConnection *conn);
GST_RTSP_API
void gst_rtsp_connection_add_extra_http_request_header (GstRTSPConnection *conn, const gchar *key, const gchar *value);
/* async IO */
/**