diff mbox series

channel_curl: Set charset in Content-Type Header

Message ID 20230428193802.38596-1-christian.storm@siemens.com
State Changes Requested
Delegated to: Stefano Babic
Headers show
Series channel_curl: Set charset in Content-Type Header | expand

Commit Message

Storm, Christian April 28, 2023, 7:38 p.m. UTC
Replace 'charsets: utf-8' HTTP Header by 'Content-Type:' Header's
'charset=' parameter:
As per RFC 4627, the default encoding for application/json is UTF-8.
RFC 8259 demands that JSON text exchanged between non-closed systems
must be UTF-8 encoded. It continues to state that no 'charset'
parameter is defined for application/json. Hence, while adding one
has no effect on compliant recipients according to RFC 8259, do not
add a non-defined parameter.

This also fixes adding the Header 'charsets: utf-8' twice for
application/json and application/text content if no memory
allocation error occurs.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/channel_curl.c | 47 ++++++++++++++----------------------------
 1 file changed, 16 insertions(+), 31 deletions(-)
diff mbox series

Patch

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 438950b2..c94f5045 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -524,41 +524,26 @@  static channel_op_res_t channel_set_content_type(channel_t *this,
 	else
 		content = "application/json";
 
-	if (ENOMEM_ASPRINTF ==
-		    asprintf(&contenttype, "Content-Type: %s",
-			    content)) {
-			result = CHANNEL_EINIT;
-			ERROR("OOM when setting Content-type.");
-	}
-
-	if (ENOMEM_ASPRINTF ==
-		    asprintf(&accept, "Accept: %s",
-			    content)) {
-			result = CHANNEL_EINIT;
+	if (ENOMEM_ASPRINTF == asprintf(&contenttype, "Content-Type: %s%s", content,
+		!strcmp(content, "application/text") ? "; charset=utf-8" : "")) {
 			ERROR("OOM when setting Content-type.");
-	}
-
-	if (result == CHANNEL_OK) {
-		if (((channel_curl->header = curl_slist_append(
-			  channel_curl->header, contenttype)) ==
-			     NULL) ||
-			((channel_curl->header = curl_slist_append(
-				  channel_curl->header, accept)) == NULL) ||
-			((channel_curl->header = curl_slist_append(
-				  channel_curl->header, "charsets: utf-8")) == NULL)) {
-			ERROR("Set channel header failed.");
 			result = CHANNEL_EINIT;
+	} else {
+		if ((channel_curl->header = curl_slist_append(channel_curl->header,
+			contenttype)) == NULL) {
+				ERROR("Setting channel header Content-type failed.");
+				result = CHANNEL_EINIT;
 		}
 	}
-	/*
-	 * Add default charset for application content
-	 */
-	if ((!strcmp(content, "application/json") || !strcmp(content, "application/text")) &&
-             (result == CHANNEL_OK)) {
-		if ((channel_curl->header = curl_slist_append(
-			channel_curl->header, "charsets: utf-8")) == NULL) {
-			ERROR("Set channel charset header failed.");
-			result = CHANNEL_EINIT;
+
+	if (ENOMEM_ASPRINTF == asprintf(&accept, "Accept: %s", content)) {
+		ERROR("OOM when setting Accept.");
+		result = CHANNEL_EINIT;
+	} else {
+		if ((channel_curl->header = curl_slist_append(channel_curl->header,
+			accept)) == NULL) {
+				ERROR("Set channel header Accept failed.");
+				result = CHANNEL_EINIT;
 		}
 	}