Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle upgrade header #159

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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