nmcs-http: add param to GET API to set custom HTTP headers

https://bugzilla.redhat.com/show_bug.cgi?id=1821787
This commit is contained in:
Antonio Cardace 2020-06-18 18:19:45 +02:00
parent 1095cef9a1
commit 053bce438b
No known key found for this signature in database
GPG key ID: 6BF80ABD43E377D3
3 changed files with 35 additions and 0 deletions

View file

@ -119,6 +119,7 @@ typedef struct {
CURL *ehandle;
char *url;
GString *recv_data;
struct curl_slist *headers;
gssize max_data;
gulong cancellable_id;
} EHandleData;
@ -145,6 +146,8 @@ _ehandle_free (EHandleData *edata)
if (edata->recv_data)
g_string_free (edata->recv_data, TRUE);
if (edata->headers)
curl_slist_free_all (edata->headers);
g_free (edata->url);
nm_g_slice_free (edata);
}
@ -260,12 +263,14 @@ nm_http_client_get (NMHttpClient *self,
const char *url,
int timeout_msec,
gssize max_data,
const char *const *http_headers,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
NMHttpClientPrivate *priv;
EHandleData *edata;
guint i;
g_return_if_fail (NM_IS_HTTP_CLIENT (self));
g_return_if_fail (url);
@ -281,6 +286,7 @@ nm_http_client_get (NMHttpClient *self,
.recv_data = g_string_sized_new (NM_MIN (max_data, 245)),
.max_data = max_data,
.url = g_strdup (url),
.headers = NULL,
};
nmcs_wait_for_objects_register (edata->task);
@ -302,6 +308,23 @@ nm_http_client_get (NMHttpClient *self,
curl_easy_setopt (edata->ehandle, CURLOPT_WRITEDATA, edata);
curl_easy_setopt (edata->ehandle, CURLOPT_PRIVATE, edata);
if (http_headers) {
for (i = 0; http_headers[i]; ++i) {
struct curl_slist *tmp;
tmp = curl_slist_append (edata->headers,
http_headers[i]);
if (!tmp) {
curl_slist_free_all (tmp);
_LOGE ("curl: curl_slist_append() failed adding %s", http_headers[i]);
continue;
}
edata->headers = tmp;
}
curl_easy_setopt (edata->ehandle, CURLOPT_HTTPHEADER, edata->headers);
}
if (timeout_msec > 0) {
edata->timeout_source = _source_attach (self,
nm_g_timeout_source_new (timeout_msec,
@ -362,6 +385,7 @@ nm_http_client_get_finish (NMHttpClient *self,
typedef struct {
GTask *task;
char *uri;
const char *const *http_headers;
NMHttpClientPollGetCheckFcn check_fcn;
gpointer check_user_data;
GBytes *response_data;
@ -378,6 +402,7 @@ _poll_get_data_free (gpointer data)
g_free (poll_get_data->uri);
nm_clear_pointer (&poll_get_data->response_data, g_bytes_unref);
g_strfreev ((char **) poll_get_data->http_headers);
nm_g_slice_free (poll_get_data);
}
@ -397,6 +422,7 @@ _poll_get_probe_start_fcn (GCancellable *cancellable,
poll_get_data->uri,
poll_get_data->request_timeout_ms,
poll_get_data->request_max_data,
poll_get_data->http_headers,
cancellable,
callback,
user_data);
@ -476,6 +502,7 @@ nm_http_client_poll_get (NMHttpClient *self,
gssize request_max_data,
int poll_timeout_ms,
int ratelimit_timeout_ms,
const char *const *http_headers,
GCancellable *cancellable,
NMHttpClientPollGetCheckFcn check_fcn,
gpointer check_user_data,
@ -502,6 +529,7 @@ nm_http_client_poll_get (NMHttpClient *self,
.check_fcn = check_fcn,
.check_user_data = check_user_data,
.response_code = -1,
.http_headers = NM_CAST_STRV_CC (g_strdupv ((char **) http_headers)),
};
nmcs_wait_for_objects_register (poll_get_data->task);
@ -684,6 +712,7 @@ static void
nm_http_client_init (NMHttpClient *self)
{
NMHttpClientPrivate *priv = NM_HTTP_CLIENT_GET_PRIVATE (self);
priv->source_sockets_hashtable = g_hash_table_new_full (nm_direct_hash,
NULL,
NULL,

View file

@ -29,6 +29,7 @@ void nm_http_client_get (NMHttpClient *self,
const char *uri,
int timeout_msec,
gssize max_data,
const char *const *http_headers,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
@ -50,6 +51,7 @@ void nm_http_client_poll_get (NMHttpClient *self,
gssize request_max_data,
int poll_timeout_ms,
int ratelimit_timeout_ms,
const char *const *http_headers,
GCancellable *cancellable,
NMHttpClientPollGetCheckFcn check_fcn,
gpointer check_user_data,

View file

@ -138,6 +138,7 @@ detect (NMCSProvider *provider,
256*1024,
7000,
1000,
NULL,
g_task_get_cancellable (task),
_detect_get_meta_data_check_cb,
NULL,
@ -396,6 +397,7 @@ _get_config_metadata_ready_cb (GObject *source,
512*1024,
10000,
1000,
NULL,
iface_data->cancellable,
NULL,
NULL,
@ -413,6 +415,7 @@ _get_config_metadata_ready_cb (GObject *source,
512*1024,
10000,
1000,
NULL,
iface_data->cancellable,
NULL,
NULL,
@ -529,6 +532,7 @@ get_config (NMCSProvider *provider,
256 * 1024,
15000,
1000,
NULL,
g_task_get_cancellable (get_config_data->task),
_get_config_metadata_ready_check,
metadata_data,