diff mbox series

channel_curl: Support for custom headers

Message ID 20200924085308.14011-1-christian.storm@siemens.com
State Accepted
Headers show
Series channel_curl: Support for custom headers | expand

Commit Message

Storm, Christian Sept. 24, 2020, 8:53 a.m. UTC
In addition to the auth_token and content-specifying headers,
allow custom headers not only to be received but also be sent.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/channel_curl.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

Comments

Stefano Babic Sept. 24, 2020, 9:46 a.m. UTC | #1
On 24.09.20 10:53, Christian Storm wrote:
> In addition to the auth_token and content-specifying headers,
> allow custom headers not only to be received but also be sent.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/channel_curl.c | 33 ++++++++++++++++++++++++++-------
>  1 file changed, 26 insertions(+), 7 deletions(-)
> 
> diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
> index 0f09682..ea16ba8 100644
> --- a/corelib/channel_curl.c
> +++ b/corelib/channel_curl.c
> @@ -546,14 +546,13 @@ channel_op_res_t channel_set_options(channel_t *this,
>  		goto cleanup;
>  	}
>  
> -	/*
> -	 * if the caller wants to parse the headers,
> -	 * collect them in a dictionary.
> -	 * The caller must provide a valid dictionary
> -	 * for it
> -	 */
> -
>  	if (channel_data->headers) {
> +		/*
> +		 * Setup supply request and receive reply HTTP headers.
> +		 * A LIST_INIT()'d dictionary is expected at channel_data->headers.
> +		 * The dictionary is modified in-place with the received headers,
> +		 * if any, by channel_callback_headers().
> +		 */
>  		if ((curl_easy_setopt(channel_curl->handle,
>  			      CURLOPT_HEADERFUNCTION,
>  			      channel_callback_headers) != CURLE_OK) ||
> @@ -562,6 +561,26 @@ channel_op_res_t channel_set_options(channel_t *this,
>  			result = CHANNEL_EINIT;
>  			goto cleanup;
>  		}
> +
> +		struct dict_entry *entry;
> +		char *header;
> +		LIST_FOREACH(entry, channel_data->headers, next)
> +		{
> +			if (ENOMEM_ASPRINTF ==
> +			    asprintf(&header, "%s: %s",
> +				     dict_entry_get_key(entry),
> +				     dict_entry_get_value(entry))) {
> +				result = CHANNEL_EINIT;
> +				goto cleanup;
> +			}
> +			if ((channel_curl->header = curl_slist_append(
> +				  channel_curl->header, header)) == NULL) {
> +				free(header);
> +				result = CHANNEL_EINIT;
> +				goto cleanup;
> +			}
> +			free(header);
> +		}
>  	}
>  
>  	if (channel_data->strictssl == true) {
> 

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 0f09682..ea16ba8 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -546,14 +546,13 @@  channel_op_res_t channel_set_options(channel_t *this,
 		goto cleanup;
 	}
 
-	/*
-	 * if the caller wants to parse the headers,
-	 * collect them in a dictionary.
-	 * The caller must provide a valid dictionary
-	 * for it
-	 */
-
 	if (channel_data->headers) {
+		/*
+		 * Setup supply request and receive reply HTTP headers.
+		 * A LIST_INIT()'d dictionary is expected at channel_data->headers.
+		 * The dictionary is modified in-place with the received headers,
+		 * if any, by channel_callback_headers().
+		 */
 		if ((curl_easy_setopt(channel_curl->handle,
 			      CURLOPT_HEADERFUNCTION,
 			      channel_callback_headers) != CURLE_OK) ||
@@ -562,6 +561,26 @@  channel_op_res_t channel_set_options(channel_t *this,
 			result = CHANNEL_EINIT;
 			goto cleanup;
 		}
+
+		struct dict_entry *entry;
+		char *header;
+		LIST_FOREACH(entry, channel_data->headers, next)
+		{
+			if (ENOMEM_ASPRINTF ==
+			    asprintf(&header, "%s: %s",
+				     dict_entry_get_key(entry),
+				     dict_entry_get_value(entry))) {
+				result = CHANNEL_EINIT;
+				goto cleanup;
+			}
+			if ((channel_curl->header = curl_slist_append(
+				  channel_curl->header, header)) == NULL) {
+				free(header);
+				result = CHANNEL_EINIT;
+				goto cleanup;
+			}
+			free(header);
+		}
 	}
 
 	if (channel_data->strictssl == true) {