Skip to content

Commit 9d47772

Browse files
committed
Add iterations to the http mutual auth demo
1 parent 845e65e commit 9d47772

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

demos/http/http_demo_mutual_auth/http_demo_mutual_auth.c

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
#error "Please define a POST_PATH."
5959
#endif
6060

61+
/**
62+
* @brief Delay in seconds between each iteration of the demo.
63+
*/
64+
#define DEMO_LOOP_DELAY_SECONDS ( 5U )
65+
6166
/* Check that a path for Root CA certificate is defined. */
6267
#ifndef ROOT_CA_CERT_PATH
6368
#error "Please define a ROOT_CA_CERT_PATH."
@@ -108,6 +113,11 @@
108113
*/
109114
#define REQUEST_BODY_LENGTH ( sizeof( REQUEST_BODY ) - 1 )
110115

116+
/**
117+
* @brief Number of HTTP paths to request.
118+
*/
119+
#define NUMBER_HTTP_PATHS ( 4 )
120+
111121
/**
112122
* @brief A buffer used in the demo for storing HTTP request headers and
113123
* HTTP response headers and body.
@@ -312,10 +322,6 @@ static int32_t sendHttpRequest( const TransportInterface_t * pTransportInterface
312322
HTTPClient_strerror( httpStatus ) ) );
313323
}
314324

315-
LogInfo( ( "ABC" ) );
316-
fflush( stdout );
317-
fflush( stderr );
318-
319325
if( httpStatus != HTTPSuccess )
320326
{
321327
returnStatus = EXIT_FAILURE;
@@ -356,61 +362,75 @@ int main( int argc,
356362
/* Set the pParams member of the network context with desired transport. */
357363
networkContext.pParams = &opensslParams;
358364

359-
/**************************** Connect. ******************************/
360-
361-
/* Establish TLS connection on top of TCP connection using OpenSSL. */
362-
if( returnStatus == EXIT_SUCCESS )
365+
for( ; ; )
363366
{
364-
LogInfo( ( "Performing TLS handshake on top of the TCP connection." ) );
367+
int i = 0;
365368

366-
/* Attempt to connect to the HTTP server. If connection fails, retry after
367-
* a timeout. Timeout value will be exponentially increased till the maximum
368-
* attempts are reached or maximum timeout value is reached. The function
369-
* returns EXIT_FAILURE if the TCP connection cannot be established to
370-
* broker after configured number of attempts. */
371-
returnStatus = connectToServerWithBackoffRetries( connectToServer,
372-
&networkContext );
369+
/**************************** Connect. ******************************/
373370

374-
if( returnStatus == EXIT_FAILURE )
371+
/* Establish TLS connection on top of TCP connection using OpenSSL. */
372+
if( returnStatus == EXIT_SUCCESS )
375373
{
376-
/* Log error to indicate connection failure after all
377-
* reconnect attempts are over. */
378-
LogError( ( "Failed to connect to HTTP server %.*s.",
379-
( int32_t ) AWS_IOT_ENDPOINT_LENGTH,
380-
AWS_IOT_ENDPOINT ) );
374+
LogInfo( ( "Performing TLS handshake on top of the TCP connection." ) );
375+
376+
/* Attempt to connect to the HTTP server. If connection fails, retry after
377+
* a timeout. Timeout value will be exponentially increased till the maximum
378+
* attempts are reached or maximum timeout value is reached. The function
379+
* returns EXIT_FAILURE if the TCP connection cannot be established to
380+
* broker after configured number of attempts. */
381+
returnStatus = connectToServerWithBackoffRetries( connectToServer,
382+
&networkContext );
383+
384+
if( returnStatus == EXIT_FAILURE )
385+
{
386+
/* Log error to indicate connection failure after all
387+
* reconnect attempts are over. */
388+
LogError( ( "Failed to connect to HTTP server %.*s.",
389+
( int32_t ) AWS_IOT_ENDPOINT_LENGTH,
390+
AWS_IOT_ENDPOINT ) );
391+
}
381392
}
382-
}
383393

384-
/* Define the transport interface. */
385-
if( returnStatus == EXIT_SUCCESS )
386-
{
387-
( void ) memset( &transportInterface, 0, sizeof( transportInterface ) );
388-
transportInterface.recv = Openssl_Recv;
389-
transportInterface.send = Openssl_Send;
390-
transportInterface.pNetworkContext = &networkContext;
391-
}
394+
/* Define the transport interface. */
395+
if( returnStatus == EXIT_SUCCESS )
396+
{
397+
( void ) memset( &transportInterface, 0, sizeof( transportInterface ) );
398+
transportInterface.recv = Openssl_Recv;
399+
transportInterface.send = Openssl_Send;
400+
transportInterface.pNetworkContext = &networkContext;
401+
}
392402

393-
/*********************** Send HTTPS request. ************************/
403+
/*********************** Send HTTPS requests. ************************/
394404

395-
if( returnStatus == EXIT_SUCCESS )
396-
{
397-
returnStatus = sendHttpRequest( &transportInterface,
398-
HTTP_METHOD_POST,
399-
HTTP_METHOD_POST_LENGTH,
400-
POST_PATH,
401-
POST_PATH_LENGTH );
402-
}
405+
for( i = 0; i < NUMBER_HTTP_PATHS; i++ )
406+
{
407+
if( returnStatus == EXIT_SUCCESS )
408+
{
409+
returnStatus = sendHttpRequest( &transportInterface,
410+
HTTP_METHOD_POST,
411+
HTTP_METHOD_POST_LENGTH,
412+
POST_PATH,
413+
POST_PATH_LENGTH );
414+
}
415+
else
416+
{
417+
break;
418+
}
419+
}
403420

404-
if( returnStatus == EXIT_SUCCESS )
405-
{
406-
/* Log message indicating an iteration completed successfully. */
407-
LogInfo( ( "Demo completed successfully." ) );
408-
}
421+
if( returnStatus == EXIT_SUCCESS )
422+
{
423+
/* Log message indicating an iteration completed successfully. */
424+
LogInfo( ( "Demo completed successfully." ) );
425+
}
409426

410-
/************************** Disconnect. *****************************/
427+
/************************** Disconnect. *****************************/
411428

412-
/* End TLS session, then close TCP connection. */
413-
( void ) Openssl_Disconnect( &networkContext );
429+
/* End TLS session, then close TCP connection. */
430+
( void ) Openssl_Disconnect( &networkContext );
414431

415-
return returnStatus;
416-
}
432+
LogInfo( ( "Short delay before starting the next iteration....\n" ) );
433+
sleep( DEMO_LOOP_DELAY_SECONDS );
434+
435+
return returnStatus;
436+
}

0 commit comments

Comments
 (0)