Skip to content

Commit 7407689

Browse files
committed
Improve readability by removing #defined strings
1 parent 5be9e07 commit 7407689

File tree

4 files changed

+160
-243
lines changed

4 files changed

+160
-243
lines changed

source/core_http_client.c

+53-53
Original file line numberDiff line numberDiff line change
@@ -1268,19 +1268,19 @@ static char * httpHeaderStrncpy( char * pDest,
12681268

12691269
for( ; i < len; i++ )
12701270
{
1271-
if( pSrc[ i ] == CARRIAGE_RETURN_CHARACTER )
1271+
if( pSrc[ i ] == '\r' )
12721272
{
12731273
LogError( ( "Invalid character '\r' found in %.*s",
12741274
( int ) len, pSrc ) );
12751275
hasError = 1U;
12761276
}
1277-
else if( pSrc[ i ] == LINEFEED_CHARACTER )
1277+
else if( pSrc[ i ] == '\n' )
12781278
{
12791279
LogError( ( "Invalid character '\n' found in %.*s",
12801280
( int ) len, pSrc ) );
12811281
hasError = 1U;
12821282
}
1283-
else if( ( isField == 1U ) && ( pSrc[ i ] == COLON_CHARACTER ) )
1283+
else if( ( isField == 1U ) && ( pSrc[ i ] == ':' ) )
12841284
{
12851285
LogError( ( "Invalid character ':' found in %.*s",
12861286
( int ) len, pSrc ) );
@@ -1327,18 +1327,18 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,
13271327
/* Backtrack before trailing "\r\n" (HTTP header end) if it's already written.
13281328
* Note that this method also writes trailing "\r\n" before returning.
13291329
* The first condition prevents reading before start of the header. */
1330-
if( ( HTTP_HEADER_END_INDICATOR_LEN <= pRequestHeaders->headersLen ) &&
1331-
( strncmp( ( char * ) pBufferCur - HTTP_HEADER_END_INDICATOR_LEN,
1332-
HTTP_HEADER_END_INDICATOR, HTTP_HEADER_END_INDICATOR_LEN ) == 0 ) )
1330+
if( ( 4U <= pRequestHeaders->headersLen ) &&
1331+
( strncmp( ( char * ) pBufferCur - 4U,
1332+
"\r\n\r\n", 4U ) == 0 ) )
13331333
{
1334-
backtrackHeaderLen -= HTTP_HEADER_LINE_SEPARATOR_LEN;
1335-
pBufferCur -= HTTP_HEADER_LINE_SEPARATOR_LEN;
1334+
backtrackHeaderLen -= 2U;
1335+
pBufferCur -= 2U;
13361336
}
13371337

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

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

13591359
/* Copy the field separator, ": ", into the buffer. */
13601360
( void ) memcpy( pBufferCur,
1361-
HTTP_HEADER_FIELD_SEPARATOR,
1362-
HTTP_HEADER_FIELD_SEPARATOR_LEN );
1361+
": ",
1362+
2U );
13631363

1364-
pBufferCur += HTTP_HEADER_FIELD_SEPARATOR_LEN;
1364+
pBufferCur += 2U;
13651365

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

13771377
/* Copy the header end indicator, "\r\n\r\n" into the buffer. */
13781378
( void ) memcpy( pBufferCur,
1379-
HTTP_HEADER_END_INDICATOR,
1380-
HTTP_HEADER_END_INDICATOR_LEN );
1379+
"\r\n\r\n",
1380+
4U );
13811381

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

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

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

14381438
/* Write the rangeEnd value of the request range to the buffer. */
14391439
rangeValueLength += convertInt32ToAscii( rangeEnd,
@@ -1444,8 +1444,8 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
14441444
else if( rangeStartOrlastNbytes >= 0 )
14451445
{
14461446
/* Write the "-" character to the buffer.*/
1447-
*( rangeValueBuffer + rangeValueLength ) = DASH_CHARACTER;
1448-
rangeValueLength += DASH_CHARACTER_LEN;
1447+
*( rangeValueBuffer + rangeValueLength ) = '-';
1448+
rangeValueLength += 1U;
14491449
}
14501450
else
14511451
{
@@ -1454,8 +1454,8 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
14541454

14551455
/* Add the Range Request header field and value to the buffer. */
14561456
returnStatus = addHeader( pRequestHeaders,
1457-
HTTP_RANGE_REQUEST_HEADER_FIELD,
1458-
HTTP_RANGE_REQUEST_HEADER_FIELD_LEN,
1457+
"Range",
1458+
sizeof( "Range" ) - 1U,
14591459
rangeValueBuffer,
14601460
rangeValueLength );
14611461

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

14821482
toAddLen = methodLen + \
1483-
SPACE_CHARACTER_LEN + \
1484-
SPACE_CHARACTER_LEN + \
1485-
HTTP_PROTOCOL_VERSION_LEN + \
1486-
HTTP_HEADER_LINE_SEPARATOR_LEN;
1483+
1U + \
1484+
1U + \
1485+
sizeof( "HTTP/1.1" ) - 1U + \
1486+
2U;
14871487

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

14911491
if( ( toAddLen + pRequestHeaders->headersLen ) > pRequestHeaders->bufferLen )
14921492
{
@@ -1499,34 +1499,34 @@ static HTTPStatus_t writeRequestLine( HTTPRequestHeaders_t * pRequestHeaders,
14991499
( void ) strncpy( pBufferCur, pMethod, methodLen );
15001500
pBufferCur += methodLen;
15011501

1502-
*pBufferCur = SPACE_CHARACTER;
1503-
pBufferCur += SPACE_CHARACTER_LEN;
1502+
*pBufferCur = ' ';
1503+
pBufferCur += 1U;
15041504

15051505
/* Use "/" as default value if <PATH> is NULL. */
15061506
if( ( pPath == NULL ) || ( pathLen == 0U ) )
15071507
{
15081508
( void ) strncpy( pBufferCur,
1509-
HTTP_EMPTY_PATH,
1510-
HTTP_EMPTY_PATH_LEN );
1511-
pBufferCur += HTTP_EMPTY_PATH_LEN;
1509+
"/",
1510+
1U );
1511+
pBufferCur += 1U;
15121512
}
15131513
else
15141514
{
15151515
( void ) strncpy( pBufferCur, pPath, pathLen );
15161516
pBufferCur += pathLen;
15171517
}
15181518

1519-
*pBufferCur = SPACE_CHARACTER;
1520-
pBufferCur += SPACE_CHARACTER_LEN;
1519+
*pBufferCur = ' ';
1520+
pBufferCur += 1U;
15211521

15221522
( void ) strncpy( pBufferCur,
1523-
HTTP_PROTOCOL_VERSION,
1524-
HTTP_PROTOCOL_VERSION_LEN );
1525-
pBufferCur += HTTP_PROTOCOL_VERSION_LEN;
1523+
"HTTP/1.1",
1524+
sizeof( "HTTP/1.1" ) - 1U );
1525+
pBufferCur += sizeof( "HTTP/1.1" ) - 1U;
15261526

15271527
( void ) memcpy( pBufferCur,
1528-
HTTP_HEADER_LINE_SEPARATOR,
1529-
HTTP_HEADER_LINE_SEPARATOR_LEN );
1528+
"\r\n",
1529+
2U );
15301530
pRequestHeaders->headersLen = toAddLen;
15311531
}
15321532

@@ -1598,18 +1598,18 @@ HTTPStatus_t HTTPClient_InitializeRequestHeaders( HTTPRequestHeaders_t * pReques
15981598
{
15991599
/* Write "User-Agent: <Value>". */
16001600
returnStatus = addHeader( pRequestHeaders,
1601-
HTTP_USER_AGENT_FIELD,
1602-
HTTP_USER_AGENT_FIELD_LEN,
1601+
"User-Agent",
1602+
sizeof( "User-Agent" ) - 1U,
16031603
HTTP_USER_AGENT_VALUE,
1604-
HTTP_USER_AGENT_VALUE_LEN );
1604+
sizeof( HTTP_USER_AGENT_VALUE ) - 1U );
16051605
}
16061606

16071607
if( returnStatus == HTTPSuccess )
16081608
{
16091609
/* Write "Host: <Value>". */
16101610
returnStatus = addHeader( pRequestHeaders,
1611-
HTTP_HOST_FIELD,
1612-
HTTP_HOST_FIELD_LEN,
1611+
"Host",
1612+
sizeof( "Host" ) - 1U,
16131613
pRequestInfo->pHost,
16141614
pRequestInfo->hostLen );
16151615
}
@@ -1620,10 +1620,10 @@ HTTPStatus_t HTTPClient_InitializeRequestHeaders( HTTPRequestHeaders_t * pReques
16201620
{
16211621
/* Write "Connection: keep-alive". */
16221622
returnStatus = addHeader( pRequestHeaders,
1623-
HTTP_CONNECTION_FIELD,
1624-
HTTP_CONNECTION_FIELD_LEN,
1625-
HTTP_CONNECTION_KEEP_ALIVE_VALUE,
1626-
HTTP_CONNECTION_KEEP_ALIVE_VALUE_LEN );
1623+
"Connection",
1624+
sizeof( "Connection" ) - 1U,
1625+
"keep-alive",
1626+
sizeof( "keep-alive" ) - 1U );
16271627
}
16281628
}
16291629

@@ -1852,8 +1852,8 @@ static HTTPStatus_t addContentLengthHeader( HTTPRequestHeaders_t * pRequestHeade
18521852
sizeof( pContentLengthValue ) );
18531853

18541854
returnStatus = addHeader( pRequestHeaders,
1855-
HTTP_CONTENT_LENGTH_FIELD,
1856-
HTTP_CONTENT_LENGTH_FIELD_LEN,
1855+
"Content-Length",
1856+
sizeof( "Content-Length" ) - 1U,
18571857
pContentLengthValue,
18581858
contentLengthValueNumBytes );
18591859

source/include/core_http_client_private.h

+6-89
Original file line numberDiff line numberDiff line change
@@ -46,117 +46,34 @@
4646
#endif
4747
/* *INDENT-ON* */
4848

49-
/**
50-
* @brief The HTTP protocol version of this library is HTTP/1.1.
51-
*/
52-
#define HTTP_PROTOCOL_VERSION "HTTP/1.1"
53-
#define HTTP_PROTOCOL_VERSION_LEN ( sizeof( HTTP_PROTOCOL_VERSION ) - 1U ) /**< The length of #HTTP_PROTOCOL_VERSION. */
54-
55-
/**
56-
* @brief Default value when pRequestInfo->pPath == NULL.
57-
*/
58-
#define HTTP_EMPTY_PATH "/"
59-
#define HTTP_EMPTY_PATH_LEN ( sizeof( HTTP_EMPTY_PATH ) - 1U ) /**< The length of #HTTP_EMPTY_PATH. */
60-
61-
/* Constants for HTTP header formatting. */
62-
#define HTTP_HEADER_LINE_SEPARATOR "\r\n" /**< HTTP header field lines are separated by `\r\n`. */
63-
#define HTTP_HEADER_LINE_SEPARATOR_LEN ( sizeof( HTTP_HEADER_LINE_SEPARATOR ) - 1U ) /**< The length of #HTTP_HEADER_LINE_SEPARATOR. */
64-
#define HTTP_HEADER_END_INDICATOR "\r\n\r\n" /**< The HTTP header is complete when `\r\n\r\n` is found. */
65-
#define HTTP_HEADER_END_INDICATOR_LEN ( sizeof( HTTP_HEADER_END_INDICATOR ) - 1U ) /**< The length of #HTTP_HEADER_END_INDICATOR. */
66-
#define HTTP_HEADER_FIELD_SEPARATOR ": " /**< HTTP header field and values are separated by ": ". */
67-
#define HTTP_HEADER_FIELD_SEPARATOR_LEN ( sizeof( HTTP_HEADER_FIELD_SEPARATOR ) - 1U ) /**< The length of #HTTP_HEADER_FIELD_SEPARATOR. */
68-
#define SPACE_CHARACTER ' ' /**< A space character macro to help with serializing a request. */
69-
#define SPACE_CHARACTER_LEN ( 1U ) /**< The length of #SPACE_CHARACTER. */
70-
#define DASH_CHARACTER '-' /**< A dash character macro to help with serializing a request. */
71-
#define DASH_CHARACTER_LEN ( 1U ) /**< The length of #DASH_CHARACTER. */
72-
73-
/* Constants for HTTP header copy checks. */
74-
#define CARRIAGE_RETURN_CHARACTER '\r' /**< A carriage return character to help with header validation. */
75-
#define LINEFEED_CHARACTER '\n' /**< A linefeed character to help with header validation. */
76-
#define COLON_CHARACTER ':' /**< A colon character to help with header validation. */
77-
7849
/**
7950
* @brief Indicator for function #httpHeaderStrncpy that the pSrc parameter is a
8051
* header value.
8152
*/
82-
#define HTTP_HEADER_STRNCPY_IS_VALUE 0U
53+
#define HTTP_HEADER_STRNCPY_IS_VALUE 0U
8354

8455
/**
8556
* @brief Indicator for function #httpHeaderStrncpy that the pSrc parameter is a
8657
* header field.
8758
*/
88-
#define HTTP_HEADER_STRNCPY_IS_FIELD 1U
89-
90-
/* Constants for header fields added automatically during the request
91-
* initialization. */
92-
#define HTTP_USER_AGENT_FIELD "User-Agent" /**< HTTP header field "User-Agent". */
93-
#define HTTP_USER_AGENT_FIELD_LEN ( sizeof( HTTP_USER_AGENT_FIELD ) - 1U ) /**< The length of #HTTP_USER_AGENT_FIELD. */
94-
#define HTTP_HOST_FIELD "Host" /**< HTTP header field "Host". */
95-
#define HTTP_HOST_FIELD_LEN ( sizeof( HTTP_HOST_FIELD ) - 1U ) /**< The length of #HTTP_HOST_FIELD. */
96-
#define HTTP_USER_AGENT_VALUE_LEN ( sizeof( HTTP_USER_AGENT_VALUE ) - 1U ) /**< The length of #HTTP_USER_AGENT_VALUE. */
97-
98-
/* Constants for header fields added based on flags. */
99-
#define HTTP_CONNECTION_FIELD "Connection" /**< HTTP header field "Connection". */
100-
#define HTTP_CONNECTION_FIELD_LEN ( sizeof( HTTP_CONNECTION_FIELD ) - 1U ) /**< The length of #HTTP_CONNECTION_FIELD. */
101-
#define HTTP_CONTENT_LENGTH_FIELD "Content-Length" /**< HTTP header field "Content-Length". */
102-
#define HTTP_CONTENT_LENGTH_FIELD_LEN ( sizeof( HTTP_CONTENT_LENGTH_FIELD ) - 1U ) /**< The length of #HTTP_CONTENT_LENGTH_FIELD. */
103-
104-
/* Constants for header values added based on flags. */
105-
106-
/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the
107-
* one postfixed with _LEN. This rule is suppressed for naming consistency with
108-
* other HTTP header field and value string and length macros in this file.*/
109-
/* coverity[other_declaration] */
110-
#define HTTP_CONNECTION_KEEP_ALIVE_VALUE "keep-alive" /**< HTTP header value "keep-alive" for the "Connection" header field. */
111-
112-
/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the one
113-
* above it. This rule is suppressed for naming consistency with other HTTP
114-
* header field and value string and length macros in this file.*/
115-
/* coverity[misra_c_2012_rule_5_4_violation] */
116-
#define HTTP_CONNECTION_KEEP_ALIVE_VALUE_LEN ( sizeof( HTTP_CONNECTION_KEEP_ALIVE_VALUE ) - 1U ) /**< The length of #HTTP_CONNECTION_KEEP_ALIVE_VALUE. */
117-
118-
/* Constants relating to Range Requests. */
119-
120-
/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the
121-
* one postfixed with _LEN. This rule is suppressed for naming consistency with
122-
* other HTTP header field and value string and length macros in this file.*/
123-
/* coverity[other_declaration] */
124-
#define HTTP_RANGE_REQUEST_HEADER_FIELD "Range" /**< HTTP header field "Range". */
125-
126-
/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the one
127-
* above it. This rule is suppressed for naming consistency with other HTTP
128-
* header field and value string and length macros in this file.*/
129-
/* coverity[misra_c_2012_rule_5_4_violation] */
130-
#define HTTP_RANGE_REQUEST_HEADER_FIELD_LEN ( sizeof( HTTP_RANGE_REQUEST_HEADER_FIELD ) - 1U ) /**< The length of #HTTP_RANGE_REQUEST_HEADER_FIELD. */
131-
132-
/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the
133-
* one postfixed with _LEN. This rule is suppressed for naming consistency with
134-
* other HTTP header field and value string and length macros in this file.*/
135-
/* coverity[other_declaration] */
136-
#define HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX "bytes=" /**< HTTP required header value prefix when specifying a byte range for partial content. */
137-
138-
/* MISRA Rule 5.4 flags the following macro's name as ambiguous from the one
139-
* above it. This rule is suppressed for naming consistency with other HTTP
140-
* header field and value string and length macros in this file.*/
141-
/* coverity[misra_c_2012_rule_5_4_violation] */
142-
#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. */
59+
#define HTTP_HEADER_STRNCPY_IS_FIELD 1U
14360

14461
/**
14562
* @brief Maximum value of a 32 bit signed integer is 2,147,483,647.
14663
*
14764
* Used for calculating buffer space for ASCII representation of range values.
14865
*/
149-
#define MAX_INT32_NO_OF_DECIMAL_DIGITS 10U
66+
#define MAX_INT32_NO_OF_DECIMAL_DIGITS 10U
15067

15168
/**
15269
* @brief Maximum buffer space for storing a Range Request Value.
15370
*
15471
* The largest Range Request value is of the form:
15572
* "bytes=<Max-Integer-Value>-<Max-Integer-Value>"
15673
*/
157-
#define HTTP_MAX_RANGE_REQUEST_VALUE_LEN \
158-
( HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN + MAX_INT32_NO_OF_DECIMAL_DIGITS + \
159-
1U /* Dash character '-' */ + MAX_INT32_NO_OF_DECIMAL_DIGITS )
74+
#define HTTP_MAX_RANGE_REQUEST_VALUE_LEN \
75+
( sizeof( "bytes=" ) - 1U + MAX_INT32_NO_OF_DECIMAL_DIGITS + \
76+
1U + MAX_INT32_NO_OF_DECIMAL_DIGITS )
16077

16178
/**
16279
* @brief Return value for llhttp registered callback to signal

0 commit comments

Comments
 (0)