Skip to content

Commit

Permalink
flag for headers complete & clenaup
Browse files Browse the repository at this point in the history
  • Loading branch information
cryi committed Nov 4, 2023
1 parent 7011be2 commit 96a9f32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
49 changes: 14 additions & 35 deletions source/core_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static uint32_t getZeroTimestampMs( void );
* bytes than what were specified were sent, then #HTTPNetworkError is
* returned.
*/
static HTTPStatus_t sendHttpData( const TransportInterface_t * pTransport,
HTTPStatus_t HTTPClient_SendHttpData( const TransportInterface_t * pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
const uint8_t * pData,
size_t dataLen );
Expand All @@ -75,7 +75,7 @@ static HTTPStatus_t sendHttpData( const TransportInterface_t * pTransport,
* bytes than what were specified were sent, then #HTTPNetworkError is
* returned.
*/
static HTTPStatus_t sendHttpHeaders( const TransportInterface_t * pTransport,
HTTPStatus_t HTTPClient_SendHttpHeaders( const TransportInterface_t * pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
HTTPRequestHeaders_t * pRequestHeaders,
size_t reqBodyLen,
Expand Down Expand Up @@ -198,7 +198,7 @@ static HTTPStatus_t getFinalResponseStatus( HTTPParsingState_t parsingState,
* receive error. Please see #parseHttpResponse and #getFinalResponseStatus for
* other statuses returned.
*/
static HTTPStatus_t receiveAndParseHttpResponse( const TransportInterface_t * pTransport,
HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse( const TransportInterface_t * pTransport,
HTTPResponse_t * pResponse,
const HTTPRequestHeaders_t * pRequestHeaders );

Expand All @@ -212,7 +212,7 @@ static HTTPStatus_t receiveAndParseHttpResponse( const TransportInterface_t * pT
* @param[in] reqBodyBufLen Length of the request body buffer.
* @param[in] sendFlags Application provided flags to #HTTPClient_Send.
*
* @return Returns #HTTPSuccess if successful. Please see #sendHttpHeaders and
* @return Returns #HTTPSuccess if successful. Please see #HTTPClient_SendHttpHeaders and
* #sendHttpBody for other statuses returned.
*/
static HTTPStatus_t sendHttpRequest( const TransportInterface_t * pTransport,
Expand Down Expand Up @@ -833,6 +833,9 @@ static int httpParserOnHeadersCompleteCallback( llhttp_t * pHttpParser )
assert( pResponse != NULL );
assert( pParsingContext->pBufferCur != NULL );

/* Flag indicating that the headers have been completely signed - useful for libraries built on top of corehttp. */
pResponse->areHeadersComplete = 1;

/* The current location to parse was updated in previous callbacks and MUST
* always be within the response buffer. */
assert( pParsingContext->pBufferCur >= ( const char * ) ( pResponse->pBuffer ) );
Expand Down Expand Up @@ -1793,7 +1796,7 @@ HTTPStatus_t HTTPClient_AddRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,

/*-----------------------------------------------------------*/

static HTTPStatus_t sendHttpData( const TransportInterface_t * pTransport,
HTTPStatus_t HTTPClient_SendHttpData( const TransportInterface_t * pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
const uint8_t * pData,
size_t dataLen )
Expand Down Expand Up @@ -1905,7 +1908,7 @@ static HTTPStatus_t addContentLengthHeader( HTTPRequestHeaders_t * pRequestHeade

/*-----------------------------------------------------------*/

static HTTPStatus_t sendHttpHeaders( const TransportInterface_t * pTransport,
HTTPStatus_t HTTPClient_SendHttpHeaders( const TransportInterface_t * pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
HTTPRequestHeaders_t * pRequestHeaders,
size_t reqBodyLen,
Expand All @@ -1932,7 +1935,7 @@ static HTTPStatus_t sendHttpHeaders( const TransportInterface_t * pTransport,
{
LogDebug( ( "Sending HTTP request headers: HeaderBytes=%lu",
( unsigned long ) ( pRequestHeaders->headersLen ) ) );
returnStatus = sendHttpData( pTransport,
returnStatus = HTTPClient_SendHttpData( pTransport,
getTimestampMs,
pRequestHeaders->pBuffer,
pRequestHeaders->headersLen );
Expand All @@ -1941,15 +1944,6 @@ static HTTPStatus_t sendHttpHeaders( const TransportInterface_t * pTransport,
return returnStatus;
}

HTTPStatus_t HTTPClient_InternalSendHttpHeaders( const TransportInterface_t* pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
HTTPRequestHeaders_t* pRequestHeaders,
size_t reqBodyLen,
uint32_t sendFlags)
{
return sendHttpHeaders(pTransport, getTimestampMs, pRequestHeaders, reqBodyLen, sendFlags);
}

/*-----------------------------------------------------------*/

static HTTPStatus_t sendHttpBody( const TransportInterface_t * pTransport,
Expand All @@ -1966,19 +1960,11 @@ static HTTPStatus_t sendHttpBody( const TransportInterface_t * pTransport,
/* Send the request body. */
LogDebug( ( "Sending the HTTP request body: BodyBytes=%lu",
( unsigned long ) reqBodyBufLen ) );
returnStatus = sendHttpData( pTransport, getTimestampMs, pRequestBodyBuf, reqBodyBufLen );
returnStatus = HTTPClient_SendHttpData( pTransport, getTimestampMs, pRequestBodyBuf, reqBodyBufLen );

return returnStatus;
}

HTTPStatus_t HTTPClient_InternalSendHttpData( const TransportInterface_t* pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
const uint8_t* pData,
size_t dataLen)
{
return sendHttpData(pTransport, getTimestampMs, pData, dataLen);
}

/*-----------------------------------------------------------*/

static HTTPStatus_t getFinalResponseStatus( HTTPParsingState_t parsingState,
Expand Down Expand Up @@ -2028,7 +2014,7 @@ static HTTPStatus_t getFinalResponseStatus( HTTPParsingState_t parsingState,

/*-----------------------------------------------------------*/

static HTTPStatus_t receiveAndParseHttpResponse( const TransportInterface_t * pTransport,
HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse( const TransportInterface_t * pTransport,
HTTPResponse_t * pResponse,
const HTTPRequestHeaders_t * pRequestHeaders )
{
Expand Down Expand Up @@ -2145,13 +2131,6 @@ static HTTPStatus_t receiveAndParseHttpResponse( const TransportInterface_t * pT
return returnStatus;
}

HTTPStatus_t HTTPClient_InternalReceiveAndParseHttpResponse( const TransportInterface_t* pTransport,
HTTPResponse_t* pResponse,
const HTTPRequestHeaders_t* pRequestHeaders)
{
return receiveAndParseHttpResponse(pTransport, pResponse, pRequestHeaders);
}

/*-----------------------------------------------------------*/

static HTTPStatus_t sendHttpRequest( const TransportInterface_t * pTransport,
Expand All @@ -2170,7 +2149,7 @@ static HTTPStatus_t sendHttpRequest( const TransportInterface_t * pTransport,
assert( getTimestampMs != NULL );

/* Send the headers, which are at one location in memory. */
returnStatus = sendHttpHeaders( pTransport,
returnStatus = HTTPClient_SendHttpHeaders( pTransport,
getTimestampMs,
pRequestHeaders,
reqBodyBufLen,
Expand Down Expand Up @@ -2290,7 +2269,7 @@ HTTPStatus_t HTTPClient_Send( const TransportInterface_t * pTransport,

if( returnStatus == HTTPSuccess )
{
returnStatus = receiveAndParseHttpResponse( pTransport,
returnStatus = HTTPClient_ReceiveAndParseHttpResponse( pTransport,
pResponse,
pRequestHeaders );
}
Expand Down
37 changes: 25 additions & 12 deletions source/include/core_http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ typedef struct HTTPResponse
*/
size_t headerCount;

/**
* @brief Indicates whether the HTTP response headers have been fully received.
*
* This variable is set to 1 after all headers have been received and processed by #HTTPClient_Send.
*/
uint8_t areHeadersComplete;

/**
* @brief Flags of useful headers found in the response.
*
Expand Down Expand Up @@ -719,6 +726,19 @@ HTTPStatus_t HTTPClient_AddRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
int32_t rangeEnd );
/* @[declare_httpclient_addrangeheader] */

/* @[declare_httpclient_sendhttpheaders] */
HTTPStatus_t HTTPClient_SendHttpHeaders( const TransportInterface_t* pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
HTTPRequestHeaders_t* pRequestHeaders, size_t reqBodyLen,
uint32_t sendFlags);
/* @[declare_httpclient_sendhttpheaders] */

/* @[declare_httpclient_sendhttpdata] */
HTTPStatus_t HTTPClient_SendHttpData( const TransportInterface_t* pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs, const uint8_t* pData,
size_t dataLen);
/* @[declare_httpclient_sendhttpdata] */

/**
* @brief Send the request headers in #HTTPRequestHeaders_t.pBuffer and request
* body in @p pRequestBodyBuf over the transport. The response is received in
Expand Down Expand Up @@ -821,18 +841,11 @@ HTTPStatus_t HTTPClient_Send( const TransportInterface_t * pTransport,
uint32_t sendFlags );
/* @[declare_httpclient_send] */

HTTPStatus_t HTTPClient_InternalReceiveAndParseHttpResponse( const TransportInterface_t* pTransport,^M
HTTPResponse_t* pResponse,^M
const HTTPRequestHeaders_t* pRequestHeaders);

HTTPStatus_t HTTPClient_InternalSendHttpHeaders( const TransportInterface_t* pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
HTTPRequestHeaders_t* pRequestHeaders, size_t reqBodyLen,
uint32_t sendFlags);

HTTPStatus_t HTTPClient_InternalSendHttpData( const TransportInterface_t* pTransport,
HTTPClient_GetCurrentTimeFunc_t getTimestampMs, const uint8_t* pData,
size_t dataLen);
/* @[declare_httpclient_receiveandparsehttpresponse] */
HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse( const TransportInterface_t* pTransport,
HTTPResponse_t* pResponse,
const HTTPRequestHeaders_t* pRequestHeaders);
/* @[declare_httpclient_receiveandparsehttpresponse] */

/**
* @brief Read a header from a buffer containing a complete HTTP response.
Expand Down

0 comments on commit 96a9f32

Please sign in to comment.