diff mbox series

[09/14] channel_curl: Make CHANNEL_PARSE_RAW actually "parse"

Message ID 20200914145712.4989-9-christian.storm@siemens.com
State Accepted
Headers show
Series [01/14] channel_curl: Make setting request_body symmetric | expand

Commit Message

Storm, Christian Sept. 14, 2020, 2:57 p.m. UTC
A CHANNEL_PARSE_RAW-flagged channel operation returns the response body raw
and unparsed to the caller via channel_data's new 'raw_reply' field. It has
to be free()'d by the caller just like the json_reply field.

For not returning the response body to the caller, the newly introduced
CHANNEL_PARSE_NONE flag should be used instead.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/channel_curl.c | 19 ++++++++++++++-----
 include/channel_curl.h |  2 ++
 2 files changed, 16 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 53ebc2d..8c4efa9 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -767,18 +767,27 @@  static channel_op_res_t parse_reply(channel_data_t *channel_data, output_data_t
 			    json_tokenizer, chunk->memory, (int)chunk->size);
 		} while ((json_res = json_tokener_get_error(json_tokenizer)) ==
 			 json_tokener_continue);
+		/* json_tokenizer is not used for json_tokener_error_desc, hence can be freed here. */
+		json_tokener_free(json_tokenizer);
 		if (json_res != json_tokener_success) {
 			ERROR("Error while parsing channel's returned JSON data: %s",
 			      json_tokener_error_desc(json_res));
-			json_tokener_free(json_tokenizer);
 			return CHANNEL_EBADMSG;
 		}
-		if (channel_data->debug) {
-			TRACE("Got JSON: %s", chunk->memory);
-		}
-		json_tokener_free(json_tokenizer);
 	}
 #endif
+	if (channel_data->format == CHANNEL_PARSE_RAW) {
+		/* strndup is strnlen + malloc + memcpy, seems more appropriate than just malloc + memcpy. */
+		if ((channel_data->raw_reply = strndup(chunk->memory, chunk->size)) == NULL) {
+			ERROR("Channel reply buffer memory allocation failed with OOM.");
+			return CHANNEL_ENOMEM;
+		}
+	}
+
+	if (channel_data->debug) {
+		/* If reply is not \0 terminated, impose an upper bound. */
+		TRACE("Got channel reply: %s", strndupa(chunk->memory, chunk->size));
+	}
 	return CHANNEL_OK;
 }
 
diff --git a/include/channel_curl.h b/include/channel_curl.h
index 321526b..5f312ea 100644
--- a/include/channel_curl.h
+++ b/include/channel_curl.h
@@ -28,6 +28,7 @@  typedef enum {
 } channel_method_t;
 
 typedef enum {
+	CHANNEL_PARSE_NONE,
 	CHANNEL_PARSE_JSON,
 	CHANNEL_PARSE_RAW
 } channel_body_t;
@@ -42,6 +43,7 @@  typedef struct {
 #ifdef CONFIG_JSON
 	json_object *json_reply;
 #endif
+	char *raw_reply;
 	char *cafile;
 	char *sslkey;
 	char *sslcert;