From 65b0bd7775f262b925451a50f282f5536f59b60a Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Thu, 15 Jun 2023 14:25:57 +0530 Subject: [PATCH 1/3] handle upgrade header, update unit tests --- source/core_http_client.c | 7 ++++- test/unit-test/core_http_send_utest.c | 39 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/source/core_http_client.c b/source/core_http_client.c index 63836b53..d08373d2 100644 --- a/source/core_http_client.c +++ b/source/core_http_client.c @@ -1050,6 +1050,7 @@ static HTTPStatus_t processLlhttpError( const llhttp_t * pHttpParser ) switch( llhttp_get_errno( pHttpParser ) ) { case HPE_OK: + case HPE_PAUSED_UPGRADE: /* There were no errors. */ break; @@ -1203,7 +1204,11 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext, /* This will begin the parsing. Each of the callbacks set in * parserSettings will be invoked as parts of the HTTP response are * reached. The return code is parsed in #processLlhttpError so is not needed. */ - ( void ) llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen ); + + if( llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen ) == HPE_PAUSED_UPGRADE ) + { + llhttp_resume_after_upgrade( &( pParsingContext->llhttpParser ) ); + } /* The next location to parse will always be after what has already * been parsed. */ diff --git a/test/unit-test/core_http_send_utest.c b/test/unit-test/core_http_send_utest.c index 4e5174f6..d8d46649 100644 --- a/test/unit-test/core_http_send_utest.c +++ b/test/unit-test/core_http_send_utest.c @@ -561,6 +561,25 @@ static llhttp_errno_t llhttp_execute_whole_response( llhttp_t * pParser, return HPE_OK; } +/* Mocked llhttp_execute callback that expects upgrade header in HTTP response. */ +static llhttp_errno_t llhttp_execute_paused_upgrade( llhttp_t * pParser, + const char * pData, + size_t len, + int cmock_num_calls ) +{ + ( void ) cmock_num_calls; + llhttp_errno_t retVal = HPE_OK; + + if( httpParserExecuteCallCount == 0 ) + { + retVal = HPE_PAUSED_UPGRADE; + } + + llhttp_execute_whole_response( pParser, pData, len, 0 ); + + return retVal; +} + /* Mocked llhttp_execute callback that will be called the first time on the * response message up to the middle of the first header field, then the second * time on the response message from the middle of the first header field to the @@ -1374,6 +1393,26 @@ void test_HTTPClient_Send_less_bytes_request_body( void ) /*-----------------------------------------------------------*/ +/* Test upgrade header in HTTP response. */ +void test_HTTPClient_Send_paused_upgrade( void ) +{ + HTTPStatus_t returnStatus = HTTPSuccess; + + llhttp_execute_Stub( llhttp_execute_paused_upgrade ); + llhttp_resume_after_upgrade_ExpectAnyArgs(); + + returnStatus = HTTPClient_Send( &transportInterface, + &requestHeaders, + NULL, + 0, + &response, + 0 ); + + TEST_ASSERT_EQUAL( HTTPSuccess, returnStatus ); +} + +/*-----------------------------------------------------------*/ + /* Test when a network error is returned when receiving the response. */ void test_HTTPClient_Send_network_error_response( void ) { From 002060c4e3bbd0ed0a4ca5206632aaa0b86c5c46 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Wed, 21 Jun 2023 10:06:41 +0530 Subject: [PATCH 2/3] update review comments --- source/core_http_client.c | 10 ++++++++-- test/unit-test/core_http_send_utest.c | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/source/core_http_client.c b/source/core_http_client.c index d08373d2..4c38078e 100644 --- a/source/core_http_client.c +++ b/source/core_http_client.c @@ -1159,6 +1159,7 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext, { HTTPStatus_t returnStatus; const char * parsingStartLoc = NULL; + llhttp_errno_t eReturn; assert( pParsingContext != NULL ); assert( pResponse != NULL ); @@ -1203,12 +1204,17 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext, /* This will begin the parsing. Each of the callbacks set in * parserSettings will be invoked as parts of the HTTP response are - * reached. The return code is parsed in #processLlhttpError so is not needed. */ + * reached. The return code is parsed in #processLlhttpError. */ + eReturn = llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen ); - if( llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen ) == HPE_PAUSED_UPGRADE ) + if( eReturn == HPE_PAUSED_UPGRADE ) { llhttp_resume_after_upgrade( &( pParsingContext->llhttpParser ) ); } + else + { + /* Empty else MISRA 15.7 */ + } /* The next location to parse will always be after what has already * been parsed. */ diff --git a/test/unit-test/core_http_send_utest.c b/test/unit-test/core_http_send_utest.c index d8d46649..28197e9c 100644 --- a/test/unit-test/core_http_send_utest.c +++ b/test/unit-test/core_http_send_utest.c @@ -568,16 +568,16 @@ static llhttp_errno_t llhttp_execute_paused_upgrade( llhttp_t * pParser, int cmock_num_calls ) { ( void ) cmock_num_calls; - llhttp_errno_t retVal = HPE_OK; + llhttp_errno_t eReturn = HPE_OK; if( httpParserExecuteCallCount == 0 ) { - retVal = HPE_PAUSED_UPGRADE; + eReturn = HPE_PAUSED_UPGRADE; } llhttp_execute_whole_response( pParser, pData, len, 0 ); - return retVal; + return eReturn; } /* Mocked llhttp_execute callback that will be called the first time on the From 27e3372025f3115b62f8f7324ec52c770ffc3147 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Mon, 17 Jul 2023 10:02:28 +0530 Subject: [PATCH 3/3] fix review comments --- source/core_http_client.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/core_http_client.c b/source/core_http_client.c index 4c38078e..666e1f43 100644 --- a/source/core_http_client.c +++ b/source/core_http_client.c @@ -1211,10 +1211,6 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext, { llhttp_resume_after_upgrade( &( pParsingContext->llhttpParser ) ); } - else - { - /* Empty else MISRA 15.7 */ - } /* The next location to parse will always be after what has already * been parsed. */