Skip to content

Commit 0e67b12

Browse files
Handle upgrade header (FreeRTOS#159)
* handle upgrade header, update unit tests * update review comments * fix review comments
1 parent 48ccceb commit 0e67b12

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

source/core_http_client.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ static HTTPStatus_t processLlhttpError( const llhttp_t * pHttpParser )
10501050
switch( llhttp_get_errno( pHttpParser ) )
10511051
{
10521052
case HPE_OK:
1053+
case HPE_PAUSED_UPGRADE:
10531054
/* There were no errors. */
10541055
break;
10551056

@@ -1158,6 +1159,7 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext,
11581159
{
11591160
HTTPStatus_t returnStatus;
11601161
const char * parsingStartLoc = NULL;
1162+
llhttp_errno_t eReturn;
11611163

11621164
assert( pParsingContext != NULL );
11631165
assert( pResponse != NULL );
@@ -1202,8 +1204,13 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext,
12021204

12031205
/* This will begin the parsing. Each of the callbacks set in
12041206
* parserSettings will be invoked as parts of the HTTP response are
1205-
* reached. The return code is parsed in #processLlhttpError so is not needed. */
1206-
( void ) llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen );
1207+
* reached. The return code is parsed in #processLlhttpError. */
1208+
eReturn = llhttp_execute( &( pParsingContext->llhttpParser ), parsingStartLoc, parseLen );
1209+
1210+
if( eReturn == HPE_PAUSED_UPGRADE )
1211+
{
1212+
llhttp_resume_after_upgrade( &( pParsingContext->llhttpParser ) );
1213+
}
12071214

12081215
/* The next location to parse will always be after what has already
12091216
* been parsed. */

test/unit-test/core_http_send_utest.c

+39
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,25 @@ static llhttp_errno_t llhttp_execute_whole_response( llhttp_t * pParser,
561561
return HPE_OK;
562562
}
563563

564+
/* Mocked llhttp_execute callback that expects upgrade header in HTTP response. */
565+
static llhttp_errno_t llhttp_execute_paused_upgrade( llhttp_t * pParser,
566+
const char * pData,
567+
size_t len,
568+
int cmock_num_calls )
569+
{
570+
( void ) cmock_num_calls;
571+
llhttp_errno_t eReturn = HPE_OK;
572+
573+
if( httpParserExecuteCallCount == 0 )
574+
{
575+
eReturn = HPE_PAUSED_UPGRADE;
576+
}
577+
578+
llhttp_execute_whole_response( pParser, pData, len, 0 );
579+
580+
return eReturn;
581+
}
582+
564583
/* Mocked llhttp_execute callback that will be called the first time on the
565584
* response message up to the middle of the first header field, then the second
566585
* 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 )
13741393

13751394
/*-----------------------------------------------------------*/
13761395

1396+
/* Test upgrade header in HTTP response. */
1397+
void test_HTTPClient_Send_paused_upgrade( void )
1398+
{
1399+
HTTPStatus_t returnStatus = HTTPSuccess;
1400+
1401+
llhttp_execute_Stub( llhttp_execute_paused_upgrade );
1402+
llhttp_resume_after_upgrade_ExpectAnyArgs();
1403+
1404+
returnStatus = HTTPClient_Send( &transportInterface,
1405+
&requestHeaders,
1406+
NULL,
1407+
0,
1408+
&response,
1409+
0 );
1410+
1411+
TEST_ASSERT_EQUAL( HTTPSuccess, returnStatus );
1412+
}
1413+
1414+
/*-----------------------------------------------------------*/
1415+
13771416
/* Test when a network error is returned when receiving the response. */
13781417
void test_HTTPClient_Send_network_error_response( void )
13791418
{

0 commit comments

Comments
 (0)