Skip to content

Commit

Permalink
Handle upgrade header (FreeRTOS#159)
Browse files Browse the repository at this point in the history
* handle upgrade header, update unit tests

* update review comments

* fix review comments
  • Loading branch information
tony-josi-aws committed Jul 18, 2023
1 parent 48ccceb commit 0e67b12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/core_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1158,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 );
Expand Down Expand Up @@ -1202,8 +1204,13 @@ 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 );
* reached. The return code is parsed in #processLlhttpError. */
eReturn = llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen );

if( eReturn == HPE_PAUSED_UPGRADE )
{
llhttp_resume_after_upgrade( &( pParsingContext->llhttpParser ) );
}

/* The next location to parse will always be after what has already
* been parsed. */
Expand Down
39 changes: 39 additions & 0 deletions test/unit-test/core_http_send_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 eReturn = HPE_OK;

if( httpParserExecuteCallCount == 0 )
{
eReturn = HPE_PAUSED_UPGRADE;
}

llhttp_execute_whole_response( pParser, pData, len, 0 );

return eReturn;
}

/* 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
Expand Down Expand Up @@ -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 )
{
Expand Down

0 comments on commit 0e67b12

Please sign in to comment.