in ports/HTTP/src/aia_http_config.c [73:263]
bool AiaSendHttpsRequest( AiaHttpsRequest_t* httpsRequest,
AiaHttpsConnectionResponseCallback_t responseCallback,
void* responseCallbackUserData,
AiaHttpsConnectionFailureCallback_t failureCallback,
void* failureCallbackUserData )
{
IotHttpsReturnCode_t httpsClientStatus = IOT_HTTPS_OK;
IotHttpsConnectionInfo_t connConfig = { 0 };
IotHttpsRequestInfo_t reqConfig = { 0 };
IotHttpsResponseInfo_t respConfig = { 0 };
IotHttpsConnectionHandle_t connHandle =
IOT_HTTPS_CONNECTION_HANDLE_INITIALIZER;
IotHttpsRequestHandle_t reqHandle = IOT_HTTPS_REQUEST_HANDLE_INITIALIZER;
IotHttpsResponseHandle_t respHandle = IOT_HTTPS_RESPONSE_HANDLE_INITIALIZER;
IotHttpsSyncInfo_t reqSyncInfo = { 0 };
IotHttpsSyncInfo_t respSyncInfo = { 0 };
const char* pPath = NULL;
size_t pathLen = 0;
const char* pAddress = NULL;
size_t addressLen = 0;
uint16_t respStatus = IOT_HTTPS_STATUS_OK;
uint32_t retryNum = 0;
size_t rspBodyLen = 0;
uint8_t _pConnUserBuffer[ AIA_AFR_HTTPS_BUFFER_SIZE ] = { 0 };
uint8_t _pReqUserBuffer[ AIA_AFR_HTTPS_BUFFER_SIZE ] = { 0 };
uint8_t _pRespUserBuffer[ AIA_AFR_HTTPS_BUFFER_SIZE ] = { 0 };
uint8_t _pRespBodyBuffer[ AIA_AFR_HTTPS_BUFFER_SIZE ] = { 0 };
httpsClientStatus = IotHttpsClient_GetUrlPath(
httpsRequest->url, strlen( httpsRequest->url ), &pPath, &pathLen );
if( httpsClientStatus != IOT_HTTPS_OK )
{
AiaLogError(
"An error occurred in IotHttpsClient_GetUrlPath() on URL %s. Error "
"code: %d",
httpsRequest->url, httpsClientStatus );
return false;
}
httpsClientStatus = IotHttpsClient_GetUrlAddress(
httpsRequest->url, strlen( httpsRequest->url ), &pAddress,
&addressLen );
if( httpsClientStatus != IOT_HTTPS_OK )
{
AiaLogError(
"An error occurred in IotHttpsClient_GetUrlAddress() on URL "
"%s\r\n. Error code %d",
httpsRequest->url, httpsClientStatus );
return false;
}
if( httpsRequest->method != AIA_HTTPS_METHOD_POST )
{
AiaLogError( "Unsupported AiaHttpsMethod_t, method=%d",
httpsRequest->method );
return false;
}
httpsClientStatus = IotHttpsClient_Init();
if( httpsClientStatus != IOT_HTTPS_OK )
{
AiaLogError(
"An error occurred initializing the HTTPS library. Error code: %d",
httpsClientStatus );
return false;
}
connConfig.pAddress = pAddress;
connConfig.addressLen = addressLen;
connConfig.port = 443;
connConfig.pCaCert = AIA_AFR_HTTPS_TRUSTED_ROOT_CA;
connConfig.caCertLen = sizeof( AIA_AFR_HTTPS_TRUSTED_ROOT_CA );
connConfig.userBuffer.pBuffer = _pConnUserBuffer;
connConfig.userBuffer.bufferLen = sizeof( _pConnUserBuffer );
connConfig.pClientCert =
( (IotNetworkCredentials_t*)_pAiaNetCredentialInfo )->pClientCert;
connConfig.clientCertLen =
( (IotNetworkCredentials_t*)_pAiaNetCredentialInfo )->clientCertSize;
connConfig.pPrivateKey =
( (IotNetworkCredentials_t*)_pAiaNetCredentialInfo )->pPrivateKey;
connConfig.privateKeyLen =
( (IotNetworkCredentials_t*)_pAiaNetCredentialInfo )->privateKeySize;
connConfig.pNetworkInterface = _pAiaNetIf;
reqSyncInfo.pBody = (uint8_t*)( httpsRequest->body );
reqSyncInfo.bodyLen = strlen( httpsRequest->body );
respSyncInfo.pBody = (uint8_t*)_pRespBodyBuffer;
respSyncInfo.bodyLen = sizeof( _pRespBodyBuffer );
reqConfig.pPath = pPath;
reqConfig.pathLen = strlen( pPath );
reqConfig.pHost = pAddress;
reqConfig.hostLen = addressLen;
reqConfig.method = IOT_HTTPS_METHOD_POST;
reqConfig.isNonPersistent = false;
reqConfig.userBuffer.pBuffer = _pReqUserBuffer;
reqConfig.userBuffer.bufferLen = sizeof( _pReqUserBuffer );
reqConfig.isAsync = false;
reqConfig.u.pSyncInfo = &reqSyncInfo;
respConfig.userBuffer.pBuffer = _pRespUserBuffer;
respConfig.userBuffer.bufferLen = sizeof( _pRespUserBuffer );
respConfig.pSyncInfo = &respSyncInfo;
httpsClientStatus =
IotHttpsClient_InitializeRequest( &reqHandle, &reqConfig );
if( httpsClientStatus != IOT_HTTPS_OK )
{
AiaLogError(
"An error occurred in IotHttpsClient_InitializeRequest() with "
"error code: %d",
httpsClientStatus );
IotHttpsClient_Cleanup();
return false;
}
httpsClientStatus = IotHttpsClient_Connect( &connHandle, &connConfig );
if( httpsClientStatus != IOT_HTTPS_OK )
{
AiaLogError( "Failed to connect to the server. Error code: %d.",
httpsClientStatus );
IotHttpsClient_Cleanup();
return false;
}
httpsClientStatus =
IotHttpsClient_AddHeader( reqHandle, _reqHeader, strlen( _reqHeader ),
_reqHeaderVal, strlen( _reqHeaderVal ) );
if( httpsClientStatus == IOT_HTTPS_NETWORK_ERROR )
{
AiaLogError( "Failed to add header. Error code: %d.",
httpsClientStatus );
IotHttpsClient_Disconnect( connHandle );
IotHttpsClient_Cleanup();
return false;
}
httpsClientStatus = IotHttpsClient_SendSync( connHandle, reqHandle,
&respHandle, &respConfig, 0 );
if( httpsClientStatus != IOT_HTTPS_NETWORK_ERROR )
{
httpsClientStatus =
IotHttpsClient_ReadResponseStatus( respHandle, &respStatus );
if( httpsClientStatus != IOT_HTTPS_OK )
{
AiaLogError(
"Error in retreiving the response status. Error code %d",
httpsClientStatus );
failureCallback( failureCallbackUserData );
}
else if( respStatus != IOT_HTTPS_STATUS_OK )
{
AiaLogError( "Failed to register to AIS. Response status: %d",
respStatus );
failureCallback( failureCallbackUserData );
}
else
{
AiaHttpsResponse_t response;
httpsClientStatus =
IotHttpsClient_ReadContentLength( respHandle, &rspBodyLen );
if( httpsClientStatus == IOT_HTTPS_OK )
{
response.body = _pRespBodyBuffer;
response.bodyLen = rspBodyLen;
response.status = respStatus;
responseCallback( &response, responseCallbackUserData );
AiaLogInfo( "AIS registration success." );
}
else
{
AiaLogError(
"Failed to read the Content-Length from the response. "
"Error code %d",
httpsClientStatus );
}
}
}
else
{
AiaLogError( "Failed to SendSync to the server. Error code: %d.",
httpsClientStatus );
}
if( connHandle != NULL )
{
IotHttpsClient_Disconnect( connHandle );
}
IotHttpsClient_Cleanup();
return true;
}