From 39e438e2cbd87b4b0e7213da74a00ea5876b6e16 Mon Sep 17 00:00:00 2001 From: Joshua Crawford Date: Fri, 28 Apr 2023 10:37:26 +1200 Subject: [PATCH] net: lib: aws_fota: Add single url to aws_fota job doc parsing This functionality was previously proposed as part of #1986 but that PR was closed as stale. The HTTPS parts have since been added to download client, but the single url for aws_fota has not. The single url capability is required to use placeholder pre-signed S3 urls with AWS IoT https://docs.aws.amazon.com/iot/latest/developerguide/create-manage-jobs.html Signed-off-by: Joshua Crawford --- subsys/net/lib/aws_fota/src/aws_fota_json.c | 23 ++++++++++++++- .../net/lib/aws_fota/aws_fota_json/prj.conf | 2 ++ .../net/lib/aws_fota/aws_fota_json/src/main.c | 29 ++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/subsys/net/lib/aws_fota/src/aws_fota_json.c b/subsys/net/lib/aws_fota/src/aws_fota_json.c index d873924b656..a05fd072a56 100644 --- a/subsys/net/lib/aws_fota/src/aws_fota_json.c +++ b/subsys/net/lib/aws_fota/src/aws_fota_json.c @@ -12,6 +12,10 @@ #include "aws_fota_json.h" +#if defined(CONFIG_HTTP_PARSER_URL) +#include +#endif + /**@brief Copy max maxlen bytes from src to dst. Insert null-terminator. */ static void strncpy_nullterm(char *dst, const char *src, size_t maxlen) @@ -115,7 +119,24 @@ int aws_fota_parse_DescribeJobExecution_rsp(const char *job_document, cJSON *hostname = cJSON_GetObjectItemCaseSensitive(location, "host"); cJSON *path = cJSON_GetObjectItemCaseSensitive(location, "path"); - +#if defined(CONFIG_HTTP_PARSER_URL) + cJSON *url = cJSON_GetObjectItemCaseSensitive(location, "url"); + + if (cJSON_GetStringValue(url) != NULL) { + struct http_parser_url u; + + http_parser_url_init(&u); + http_parser_parse_url(url->valuestring, + strlen(url->valuestring), false, &u); + strncpy_nullterm(hostname_buf, url->valuestring + + u.field_data[UF_HOST].off, + u.field_data[UF_HOST].len + 1); + strncpy_nullterm(file_path_buf, url->valuestring + + u.field_data[UF_PATH].off + 1, + u.field_data[UF_PATH].len + + u.field_data[UF_QUERY].len + 1); + } else +#endif if ((cJSON_GetStringValue(hostname) != NULL) && (cJSON_GetStringValue(path) != NULL)) { strncpy_nullterm(hostname_buf, hostname->valuestring, diff --git a/tests/subsys/net/lib/aws_fota/aws_fota_json/prj.conf b/tests/subsys/net/lib/aws_fota/aws_fota_json/prj.conf index 0829623a1dc..5154ae7da0e 100644 --- a/tests/subsys/net/lib/aws_fota/aws_fota_json/prj.conf +++ b/tests/subsys/net/lib/aws_fota/aws_fota_json/prj.conf @@ -6,6 +6,8 @@ CONFIG_UNITY=y CONFIG_MAIN_STACK_SIZE=4096 CONFIG_CJSON_LIB=y +CONFIG_NETWORKING=y +CONFIG_HTTP_PARSER_URL=y CONFIG_NEWLIB_LIBC=y CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y CONFIG_HEAP_MEM_POOL_SIZE=4096 diff --git a/tests/subsys/net/lib/aws_fota/aws_fota_json/src/main.c b/tests/subsys/net/lib/aws_fota/aws_fota_json/src/main.c index 3bf9714b44d..8610452da2e 100644 --- a/tests/subsys/net/lib/aws_fota/aws_fota_json/src/main.c +++ b/tests/subsys/net/lib/aws_fota/aws_fota_json/src/main.c @@ -40,7 +40,34 @@ void test_parse_job_execution(void) TEST_ASSERT_EQUAL_STRING(expected_file_path, file_path); } -void test_parse_malformed_job_execution(void) +#if !defined(CONFIG_HTTP_PARSER_URL) +void test_parse_job_execution_single_url(void) +{ + int ret; + int version_number; + int expected_version_number = 1; + char expected_job_id[] = "9b5caac6-3e8a-45dd-9273-c1b995762f4a"; + char expected_hostname[] = "fota-update-bucket.s3.eu-central-1.amazonaws.com"; + char expected_file_path[] = "update.bin?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAWXEL53DXIU7W72AE%2F20190606%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20190606T081505Z&X-Amz-Expires=604800&X-Amz-Signature=913e00b97efe5565a901df4ff0b87e4878a406941d711f59d45915035989adcc&X-Amz-SignedHeaders=host"; + char encoded[] = "{\"timestamp\":1559808907,\"execution\":{\"jobId\":\"9b5caac6-3e8a-45dd-9273-c1b995762f4a\",\"status\":\"QUEUED\",\"queuedAt\":1559808906,\"lastUpdatedAt\":1559808906,\"versionNumber\":1,\"executionNumber\":1,\"jobDocument\":{\"operation\":\"app_fw_update\",\"fwversion\":\"2\",\"size\":181124,\"location\":{\"url\":\"https://fota-update-bucket.s3.eu-central-1.amazonaws.com/update.bin?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAWXEL53DXIU7W72AE%2F20190606%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20190606T081505Z&X-Amz-Expires=604800&X-Amz-Signature=913e00b97efe5565a901df4ff0b87e4878a406941d711f59d45915035989adcc&X-Amz-SignedHeaders=host\"}}}}"; + char job_id[100]; + char hostname[100]; + char file_path[1000]; + + ret = aws_fota_parse_DescribeJobExecution_rsp(encoded, + sizeof(encoded) - 1, + job_id, hostname, + file_path, + &version_number); + TEST_ASSERT_EQUAL(ret, 1); + TEST_ASSERT_EQUAL_STRING(job_id, expected_job_id); + TEST_ASSERT_EQUAL(version_number, expected_version_number); + TEST_ASSERT_EQUAL_STRING(hostname, expected_hostname); + TEST_ASSERT_EQUAL_STRING(file_path, expected_file_path); +} +#endif + +static void test_parse_malformed_job_execution(void) { int ret; char job_id[100];