Skip to content

Commit

Permalink
Improve readability by removing #defined strings
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbartell committed Aug 2, 2022
1 parent 5be9e07 commit d834a62
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 244 deletions.
106 changes: 53 additions & 53 deletions source/core_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,19 +1268,19 @@ static char * httpHeaderStrncpy( char * pDest,

for( ; i < len; i++ )
{
if( pSrc[ i ] == CARRIAGE_RETURN_CHARACTER )
if( pSrc[ i ] == '\r' )
{
LogError( ( "Invalid character '\r' found in %.*s",
( int ) len, pSrc ) );
hasError = 1U;
}
else if( pSrc[ i ] == LINEFEED_CHARACTER )
else if( pSrc[ i ] == '\n' )
{
LogError( ( "Invalid character '\n' found in %.*s",
( int ) len, pSrc ) );
hasError = 1U;
}
else if( ( isField == 1U ) && ( pSrc[ i ] == COLON_CHARACTER ) )
else if( ( isField == 1U ) && ( pSrc[ i ] == ':' ) )
{
LogError( ( "Invalid character ':' found in %.*s",
( int ) len, pSrc ) );
Expand Down Expand Up @@ -1327,18 +1327,18 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,
/* Backtrack before trailing "\r\n" (HTTP header end) if it's already written.
* Note that this method also writes trailing "\r\n" before returning.
* The first condition prevents reading before start of the header. */
if( ( HTTP_HEADER_END_INDICATOR_LEN <= pRequestHeaders->headersLen ) &&
( strncmp( ( char * ) pBufferCur - HTTP_HEADER_END_INDICATOR_LEN,
HTTP_HEADER_END_INDICATOR, HTTP_HEADER_END_INDICATOR_LEN ) == 0 ) )
if( ( 4U <= pRequestHeaders->headersLen ) &&
( strncmp( ( char * ) pBufferCur - 4U,
"\r\n\r\n", 4U ) == 0 ) )
{
backtrackHeaderLen -= HTTP_HEADER_LINE_SEPARATOR_LEN;
pBufferCur -= HTTP_HEADER_LINE_SEPARATOR_LEN;
backtrackHeaderLen -= 2U;
pBufferCur -= 2U;
}

/* Check if there is enough space in buffer for additional header. */
toAddLen = fieldLen + HTTP_HEADER_FIELD_SEPARATOR_LEN + valueLen +
HTTP_HEADER_LINE_SEPARATOR_LEN +
HTTP_HEADER_LINE_SEPARATOR_LEN;
toAddLen = fieldLen + 2U + valueLen +
2U +
2U;

/* If we have enough room for the new header line, then write it to the
* header buffer. */
Expand All @@ -1358,10 +1358,10 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,

/* Copy the field separator, ": ", into the buffer. */
( void ) memcpy( pBufferCur,
HTTP_HEADER_FIELD_SEPARATOR,
HTTP_HEADER_FIELD_SEPARATOR_LEN );
": ",
2U );

pBufferCur += HTTP_HEADER_FIELD_SEPARATOR_LEN;
pBufferCur += 2U;

/* Copy the header value into the buffer. */
if( httpHeaderStrncpy( pBufferCur, pValue, valueLen, HTTP_HEADER_STRNCPY_IS_VALUE ) == NULL )
Expand All @@ -1376,8 +1376,8 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,

/* Copy the header end indicator, "\r\n\r\n" into the buffer. */
( void ) memcpy( pBufferCur,
HTTP_HEADER_END_INDICATOR,
HTTP_HEADER_END_INDICATOR_LEN );
"\r\n\r\n",
4U );

/* Update the headers length value only when everything is successful. */
pRequestHeaders->headersLen = backtrackHeaderLen + toAddLen;
Expand Down Expand Up @@ -1417,9 +1417,9 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,

/* Write the range value prefix in the buffer. */
( void ) strncpy( rangeValueBuffer,
HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX,
HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN );
rangeValueLength += HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN;
"bytes=",
sizeof( "bytes=" ) - 1U );
rangeValueLength += sizeof( "bytes=" ) - 1U;

/* Write the range start value in the buffer. */
rangeValueLength += convertInt32ToAscii( rangeStartOrlastNbytes,
Expand All @@ -1432,8 +1432,8 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
if( rangeEnd != HTTP_RANGE_REQUEST_END_OF_FILE )
{
/* Write the "-" character to the buffer.*/
*( rangeValueBuffer + rangeValueLength ) = DASH_CHARACTER;
rangeValueLength += DASH_CHARACTER_LEN;
*( rangeValueBuffer + rangeValueLength ) = '-';
rangeValueLength += 1U;

/* Write the rangeEnd value of the request range to the buffer. */
rangeValueLength += convertInt32ToAscii( rangeEnd,
Expand All @@ -1444,8 +1444,8 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
else if( rangeStartOrlastNbytes >= 0 )
{
/* Write the "-" character to the buffer.*/
*( rangeValueBuffer + rangeValueLength ) = DASH_CHARACTER;
rangeValueLength += DASH_CHARACTER_LEN;
*( rangeValueBuffer + rangeValueLength ) = '-';
rangeValueLength += 1U;
}
else
{
Expand All @@ -1454,8 +1454,8 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,

/* Add the Range Request header field and value to the buffer. */
returnStatus = addHeader( pRequestHeaders,
HTTP_RANGE_REQUEST_HEADER_FIELD,
HTTP_RANGE_REQUEST_HEADER_FIELD_LEN,
"Range",
sizeof( "Range" ) - 1U,
rangeValueBuffer,
rangeValueLength );

Expand All @@ -1480,13 +1480,13 @@ static HTTPStatus_t writeRequestLine( HTTPRequestHeaders_t * pRequestHeaders,
assert( methodLen != 0U );

toAddLen = methodLen + \
SPACE_CHARACTER_LEN + \
SPACE_CHARACTER_LEN + \
HTTP_PROTOCOL_VERSION_LEN + \
HTTP_HEADER_LINE_SEPARATOR_LEN;
1U + \
1U + \
sizeof( "HTTP/1.1" ) - 1U + \
2U;

pBufferCur = ( char * ) ( pRequestHeaders->pBuffer );
toAddLen += ( ( pPath == NULL ) || ( pathLen == 0U ) ) ? HTTP_EMPTY_PATH_LEN : pathLen;
toAddLen += ( ( pPath == NULL ) || ( pathLen == 0U ) ) ? 1U : pathLen;

if( ( toAddLen + pRequestHeaders->headersLen ) > pRequestHeaders->bufferLen )
{
Expand All @@ -1499,34 +1499,34 @@ static HTTPStatus_t writeRequestLine( HTTPRequestHeaders_t * pRequestHeaders,
( void ) strncpy( pBufferCur, pMethod, methodLen );
pBufferCur += methodLen;

*pBufferCur = SPACE_CHARACTER;
pBufferCur += SPACE_CHARACTER_LEN;
*pBufferCur = ' ';
pBufferCur += 1U;

/* Use "/" as default value if <PATH> is NULL. */
if( ( pPath == NULL ) || ( pathLen == 0U ) )
{
( void ) strncpy( pBufferCur,
HTTP_EMPTY_PATH,
HTTP_EMPTY_PATH_LEN );
pBufferCur += HTTP_EMPTY_PATH_LEN;
"/",
1U );
pBufferCur += 1U;
}
else
{
( void ) strncpy( pBufferCur, pPath, pathLen );
pBufferCur += pathLen;
}

*pBufferCur = SPACE_CHARACTER;
pBufferCur += SPACE_CHARACTER_LEN;
*pBufferCur = ' ';
pBufferCur += 1U;

( void ) strncpy( pBufferCur,
HTTP_PROTOCOL_VERSION,
HTTP_PROTOCOL_VERSION_LEN );
pBufferCur += HTTP_PROTOCOL_VERSION_LEN;
"HTTP/1.1",
sizeof( "HTTP/1.1" ) - 1U );
pBufferCur += sizeof( "HTTP/1.1" ) - 1U;

( void ) memcpy( pBufferCur,
HTTP_HEADER_LINE_SEPARATOR,
HTTP_HEADER_LINE_SEPARATOR_LEN );
"\r\n",
2U );
pRequestHeaders->headersLen = toAddLen;
}

Expand Down Expand Up @@ -1598,18 +1598,18 @@ HTTPStatus_t HTTPClient_InitializeRequestHeaders( HTTPRequestHeaders_t * pReques
{
/* Write "User-Agent: <Value>". */
returnStatus = addHeader( pRequestHeaders,
HTTP_USER_AGENT_FIELD,
HTTP_USER_AGENT_FIELD_LEN,
"User-Agent",
sizeof( "User-Agent" ) - 1U,
HTTP_USER_AGENT_VALUE,
HTTP_USER_AGENT_VALUE_LEN );
sizeof( HTTP_USER_AGENT_VALUE ) - 1U );
}

if( returnStatus == HTTPSuccess )
{
/* Write "Host: <Value>". */
returnStatus = addHeader( pRequestHeaders,
HTTP_HOST_FIELD,
HTTP_HOST_FIELD_LEN,
"Host",
sizeof( "Host" ) - 1U,
pRequestInfo->pHost,
pRequestInfo->hostLen );
}
Expand All @@ -1620,10 +1620,10 @@ HTTPStatus_t HTTPClient_InitializeRequestHeaders( HTTPRequestHeaders_t * pReques
{
/* Write "Connection: keep-alive". */
returnStatus = addHeader( pRequestHeaders,
HTTP_CONNECTION_FIELD,
HTTP_CONNECTION_FIELD_LEN,
HTTP_CONNECTION_KEEP_ALIVE_VALUE,
HTTP_CONNECTION_KEEP_ALIVE_VALUE_LEN );
"Connection",
sizeof( "Connection" ) - 1U,
"keep-alive",
sizeof( "keep-alive" ) - 1U );
}
}

Expand Down Expand Up @@ -1852,8 +1852,8 @@ static HTTPStatus_t addContentLengthHeader( HTTPRequestHeaders_t * pRequestHeade
sizeof( pContentLengthValue ) );

returnStatus = addHeader( pRequestHeaders,
HTTP_CONTENT_LENGTH_FIELD,
HTTP_CONTENT_LENGTH_FIELD_LEN,
"Content-Length",
sizeof( "Content-Length" ) - 1U,
pContentLengthValue,
contentLengthValueNumBytes );

Expand Down
2 changes: 1 addition & 1 deletion source/include/core_http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ typedef struct HTTPResponse
* of bytes written.
*
* Each line in the header is listed below and written in this order:
* <#HTTPRequestInfo_t.pMethod> <#HTTPRequestInfo_t.pPath> <#HTTP_PROTOCOL_VERSION>
* <#HTTPRequestInfo_t.pMethod> <#HTTPRequestInfo_t.pPath> HTTP/1.1
* User-Agent: <#HTTP_USER_AGENT_VALUE>
* Host: <#HTTPRequestInfo_t.pHost>
*
Expand Down
95 changes: 6 additions & 89 deletions source/include/core_http_client_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,117 +46,34 @@
#endif
/* *INDENT-ON* */

/**
* @brief The HTTP protocol version of this library is HTTP/1.1.
*/
#define HTTP_PROTOCOL_VERSION "HTTP/1.1"
#define HTTP_PROTOCOL_VERSION_LEN ( sizeof( HTTP_PROTOCOL_VERSION ) - 1U ) /**< The length of #HTTP_PROTOCOL_VERSION. */

/**
* @brief Default value when pRequestInfo->pPath == NULL.
*/
#define HTTP_EMPTY_PATH "/"
#define HTTP_EMPTY_PATH_LEN ( sizeof( HTTP_EMPTY_PATH ) - 1U ) /**< The length of #HTTP_EMPTY_PATH. */

/* Constants for HTTP header formatting. */
#define HTTP_HEADER_LINE_SEPARATOR "\r\n" /**< HTTP header field lines are separated by `\r\n`. */
#define HTTP_HEADER_LINE_SEPARATOR_LEN ( sizeof( HTTP_HEADER_LINE_SEPARATOR ) - 1U ) /**< The length of #HTTP_HEADER_LINE_SEPARATOR. */
#define HTTP_HEADER_END_INDICATOR "\r\n\r\n" /**< The HTTP header is complete when `\r\n\r\n` is found. */
#define HTTP_HEADER_END_INDICATOR_LEN ( sizeof( HTTP_HEADER_END_INDICATOR ) - 1U ) /**< The length of #HTTP_HEADER_END_INDICATOR. */
#define HTTP_HEADER_FIELD_SEPARATOR ": " /**< HTTP header field and values are separated by ": ". */
#define HTTP_HEADER_FIELD_SEPARATOR_LEN ( sizeof( HTTP_HEADER_FIELD_SEPARATOR ) - 1U ) /**< The length of #HTTP_HEADER_FIELD_SEPARATOR. */
#define SPACE_CHARACTER ' ' /**< A space character macro to help with serializing a request. */
#define SPACE_CHARACTER_LEN ( 1U ) /**< The length of #SPACE_CHARACTER. */
#define DASH_CHARACTER '-' /**< A dash character macro to help with serializing a request. */
#define DASH_CHARACTER_LEN ( 1U ) /**< The length of #DASH_CHARACTER. */

/* Constants for HTTP header copy checks. */
#define CARRIAGE_RETURN_CHARACTER '\r' /**< A carriage return character to help with header validation. */
#define LINEFEED_CHARACTER '\n' /**< A linefeed character to help with header validation. */
#define COLON_CHARACTER ':' /**< A colon character to help with header validation. */

/**
* @brief Indicator for function #httpHeaderStrncpy that the pSrc parameter is a
* header value.
*/
#define HTTP_HEADER_STRNCPY_IS_VALUE 0U
#define HTTP_HEADER_STRNCPY_IS_VALUE 0U

/**
* @brief Indicator for function #httpHeaderStrncpy that the pSrc parameter is a
* header field.
*/
#define HTTP_HEADER_STRNCPY_IS_FIELD 1U

/* Constants for header fields added automatically during the request
* initialization. */
#define HTTP_USER_AGENT_FIELD "User-Agent" /**< HTTP header field "User-Agent". */
#define HTTP_USER_AGENT_FIELD_LEN ( sizeof( HTTP_USER_AGENT_FIELD ) - 1U ) /**< The length of #HTTP_USER_AGENT_FIELD. */
#define HTTP_HOST_FIELD "Host" /**< HTTP header field "Host". */
#define HTTP_HOST_FIELD_LEN ( sizeof( HTTP_HOST_FIELD ) - 1U ) /**< The length of #HTTP_HOST_FIELD. */
#define HTTP_USER_AGENT_VALUE_LEN ( sizeof( HTTP_USER_AGENT_VALUE ) - 1U ) /**< The length of #HTTP_USER_AGENT_VALUE. */

/* Constants for header fields added based on flags. */
#define HTTP_CONNECTION_FIELD "Connection" /**< HTTP header field "Connection". */
#define HTTP_CONNECTION_FIELD_LEN ( sizeof( HTTP_CONNECTION_FIELD ) - 1U ) /**< The length of #HTTP_CONNECTION_FIELD. */
#define HTTP_CONTENT_LENGTH_FIELD "Content-Length" /**< HTTP header field "Content-Length". */
#define HTTP_CONTENT_LENGTH_FIELD_LEN ( sizeof( HTTP_CONTENT_LENGTH_FIELD ) - 1U ) /**< The length of #HTTP_CONTENT_LENGTH_FIELD. */

/* Constants for header values added based on flags. */

/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the
* one postfixed with _LEN. This rule is suppressed for naming consistency with
* other HTTP header field and value string and length macros in this file.*/
/* coverity[other_declaration] */
#define HTTP_CONNECTION_KEEP_ALIVE_VALUE "keep-alive" /**< HTTP header value "keep-alive" for the "Connection" header field. */

/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the one
* above it. This rule is suppressed for naming consistency with other HTTP
* header field and value string and length macros in this file.*/
/* coverity[misra_c_2012_rule_5_4_violation] */
#define HTTP_CONNECTION_KEEP_ALIVE_VALUE_LEN ( sizeof( HTTP_CONNECTION_KEEP_ALIVE_VALUE ) - 1U ) /**< The length of #HTTP_CONNECTION_KEEP_ALIVE_VALUE. */

/* Constants relating to Range Requests. */

/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the
* one postfixed with _LEN. This rule is suppressed for naming consistency with
* other HTTP header field and value string and length macros in this file.*/
/* coverity[other_declaration] */
#define HTTP_RANGE_REQUEST_HEADER_FIELD "Range" /**< HTTP header field "Range". */

/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the one
* above it. This rule is suppressed for naming consistency with other HTTP
* header field and value string and length macros in this file.*/
/* coverity[misra_c_2012_rule_5_4_violation] */
#define HTTP_RANGE_REQUEST_HEADER_FIELD_LEN ( sizeof( HTTP_RANGE_REQUEST_HEADER_FIELD ) - 1U ) /**< The length of #HTTP_RANGE_REQUEST_HEADER_FIELD. */

/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the
* one postfixed with _LEN. This rule is suppressed for naming consistency with
* other HTTP header field and value string and length macros in this file.*/
/* coverity[other_declaration] */
#define HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX "bytes=" /**< HTTP required header value prefix when specifying a byte range for partial content. */

/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the one
* above it. This rule is suppressed for naming consistency with other HTTP
* header field and value string and length macros in this file.*/
/* coverity[misra_c_2012_rule_5_4_violation] */
#define HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN ( sizeof( HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX ) - 1U ) /**< The length of #HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX. */
#define HTTP_HEADER_STRNCPY_IS_FIELD 1U

/**
* @brief Maximum value of a 32 bit signed integer is 2,147,483,647.
*
* Used for calculating buffer space for ASCII representation of range values.
*/
#define MAX_INT32_NO_OF_DECIMAL_DIGITS 10U
#define MAX_INT32_NO_OF_DECIMAL_DIGITS 10U

/**
* @brief Maximum buffer space for storing a Range Request Value.
*
* The largest Range Request value is of the form:
* "bytes=<Max-Integer-Value>-<Max-Integer-Value>"
*/
#define HTTP_MAX_RANGE_REQUEST_VALUE_LEN \
( HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN + MAX_INT32_NO_OF_DECIMAL_DIGITS + \
1U /* Dash character '-' */ + MAX_INT32_NO_OF_DECIMAL_DIGITS )
#define HTTP_MAX_RANGE_REQUEST_VALUE_LEN \
( sizeof( "bytes=" ) - 1U + MAX_INT32_NO_OF_DECIMAL_DIGITS + \
1U + MAX_INT32_NO_OF_DECIMAL_DIGITS )

/**
* @brief Return value for llhttp registered callback to signal
Expand Down
Loading

0 comments on commit d834a62

Please sign in to comment.