FreeRTOS: HTTPS Client
HTTPS Client v1.0.0 library
Return to main page ↑
IotHttpsClient_Connect

Explicitly connect to the HTTPS server given the connection configuration pConnConfig.

This routine blocks until the connection is complete.

This function opens a new HTTPS connection with the server specified in IotHttpsConnectionInfo_t.pAddress. The connection is established by default on top of TLS over TCP. If the application wants to connect over TCP only, then it must add the IOT_HTTPS_IS_NON_TLS_FLAG to IotHttpsConnectionInfo_t.flags. This is done at the application's own risk.

When the the last HTTP request sent on the connection is specified as persistent and we want to close the connection, IotHttpsClient_Disconnect must always be called on the valid IotHttpsConnectionHandle_t. For more information about persistent HTTP connections please see IotHttpsRequestInfo_t.isNonPersistent.

If the application receives a IOT_HTTPS_NETWORK_ERROR from IotHttpsClient_SendSync or IotHttpsClient_SendAsync, on a persistent request, then the connection will be closed. The application can call this this function again to reestablish the connection.

If pConnHandle passed in is valid and represents a previously opened connection, this function will disconnect, then reconnect. Before calling this function make sure that all outstanding requests on the connection have completed. Outstanding requests are completed when IotHttpsClient_SendSync has returned or when IotHttpsClientCallbacks_t.responseCompleteCallback has been invoked for requests scheduled with IotHttpsClient_SendAsync.

Keep in mind that many HTTP servers will close a connection, if it does not receive any requests, after a certain amount of time. Many web servers may close the connection after 30-60 seconds. The state of pConnHandle will still be in a connected state if this happens. If the server closed the connection, then the next request on the connection will fail to send with a network error and the connection will move to a closed state.

Also keep in mind that some HTTP servers do not accept persistent requests. Some HTTP servers will ignore that the request contains the "Connection: keep-alive" header and close the connection immediately after sending the response. If this happens, then the next request on the connection will fail to send with a network error and the connection will close.

To know if the connection was closed by the server, debug logging can be turned on to view the network error code received. Debug logging is configured when IOT_LOG_LEVEL_HTTPS is set to IOT_LOG_DEBUG in iot_config.h.

IotHttpsConnectionInfo_t.userBuffer is used to store the internal context and therefore, multiple threads calling this function simultaneously must ensure to use different IotHttpsConnectionInfo_t objects.

See connectionUserBufferMinimumSize for information about the user buffer configured in IotHttpsConnectionInfo_t.userBuffer needed to create a valid connection handle.

Parameters
[out]pConnHandle- Handle returned representing the open connection. NULL if the function failed.
[in]pConnInfo- Configurations for the HTTPS connection.
Returns
One of the following:

Example

// An initialized network interface.
IotNetworkInterface_t* pNetworkInterface;
// Parameters to HTTPS Client connect.
uint8_t* pConnUserBuffer = (uint8_t*)malloc(connectionUserBufferMinimumSize);
// Set the connection configuration information.
connInfo.pAddress = "www.amazon.com";
connInfo.addressLen = strlen("www.amazon.com");
connInfo.port = 443;
connInfo.flags = 0;
connInfo.pAlpnProtocols = "alpnproto0,alpnproto1"
connInfo.pCaCert = HTTPS_TRUSTED_ROOT_CA; // defined elsewhere
connInfo.caCertLen = sizeof( HTTPS_TRUSTED_ROOT_CA );
connInfo.userBuffer.pBuffer = pConnUserBuffer;
connInfo.pClientCert = TLS_CLIENT_CERT;
connInfo.clientCertLen = sizeof( TLS_CLIENT_CERT );
connInfo.pPrivateKey = TLS_CLIENT_PRIV_KEY;
connInfo.privateKeyLen = sizeof( TLS_CLIENT_PRIV_KEY );
connInfo.pNetworkInterface = pNetworkInterface;
IotHttpsReturnCode_t returnCode = IotHttpsClient_Connect(&connHandle, &connInfo);
if( returnCode == IOT_HTTPS_OK )
{
// Do something with the HTTPS connection...
// Clean up and close the HTTPS connection once it's no longer needed.
}
IotHttpsConnectionInfo_t::pAlpnProtocols
char * pAlpnProtocols
String of all the ALPN protocols separated by commas needed for this connection.
Definition: iot_https_types.h:763
IotHttpsConnectionInfo_t::pNetworkInterface
IOT_HTTPS_NETWORK_INTERFACE_TYPE pNetworkInterface
The IOT network abstraction interface.
Definition: iot_https_types.h:781
IotHttpsConnectionInfo_t::flags
uint32_t flags
Flags to configure the HTTPS connection.
Definition: iot_https_types.h:739
IotHttpsConnectionInfo_t::pPrivateKey
const char * pPrivateKey
Client private key store for this connection.
Definition: iot_https_types.h:754
IotHttpsUserBuffer_t::pBuffer
uint8_t * pBuffer
Application provided buffer pointer.
Definition: iot_https_types.h:651
IotHttpsUserBuffer_t::bufferLen
uint32_t bufferLen
The length of the application provided buffer.
Definition: iot_https_types.h:652
IotHttpsConnectionInfo_t::userBuffer
IotHttpsUserBuffer_t userBuffer
User buffer to store the internal connection context.
Definition: iot_https_types.h:772
IotHttpsConnectionInfo_t::pClientCert
const char * pClientCert
Client certificate store for this connection.
Definition: iot_https_types.h:751
IotHttpsConnectionInfo_t::port
uint16_t port
Remote port number.
Definition: iot_https_types.h:730
IOT_HTTPS_CONNECTION_HANDLE_INITIALIZER
#define IOT_HTTPS_CONNECTION_HANDLE_INITIALIZER
Initializer for IotHttpsConnectionHandle_t.
Definition: iot_https_types.h:190
IotHttpsConnectionHandle_t
struct _httpsConnection * IotHttpsConnectionHandle_t
Opaque handle of an HTTP connection.
Definition: iot_https_types.h:248
IotNetworkInterface_t
IotHttpsConnectionInfo_t::pAddress
const char * pAddress
Remote server address that is DNS discoverable.
Definition: iot_https_types.h:727
IotHttpsConnectionInfo_t::caCertLen
uint32_t caCertLen
Server trusted certificate store size.
Definition: iot_https_types.h:749
IotHttpsConnectionInfo_t::privateKeyLen
uint32_t privateKeyLen
Client private key store size.
Definition: iot_https_types.h:755
IOT_HTTPS_CONNECTION_INFO_INITIALIZER
#define IOT_HTTPS_CONNECTION_INFO_INITIALIZER
Initializer for IotHttpsConnectionInfo_t.
Definition: iot_https_types.h:202
IotHttpsClient_Disconnect
IotHttpsReturnCode_t IotHttpsClient_Disconnect(IotHttpsConnectionHandle_t connHandle)
Disconnect from the HTTPS server given the connection handle connHandle.
Definition: iot_https_client.c:2629
connectionUserBufferMinimumSize
const uint32_t connectionUserBufferMinimumSize
The minimum user buffer size for the HTTP connection context and headers.
Definition: iot_https_client.c:138
IotHttpsClient_Connect
IotHttpsReturnCode_t IotHttpsClient_Connect(IotHttpsConnectionHandle_t *pConnHandle, IotHttpsConnectionInfo_t *pConnInfo)
Explicitly connect to the HTTPS server given the connection configuration pConnConfig.
Definition: iot_https_client.c:2589
IOT_HTTPS_OK
@ IOT_HTTPS_OK
Returned for a successful operation.
Definition: iot_https_types.h:298
IotHttpsConnectionInfo_t::addressLen
uint32_t addressLen
remote address length.
Definition: iot_https_types.h:728
IotHttpsReturnCode_t
IotHttpsReturnCode_t
Return codes of HTTPS Client functions.
Definition: iot_https_types.h:294
IotHttpsConnectionInfo_t::clientCertLen
uint32_t clientCertLen
Client certificate store size.
Definition: iot_https_types.h:752
IotHttpsConnectionInfo_t
HTTP connection configuration.
Definition: iot_https_types.h:721
IotHttpsConnectionInfo_t::pCaCert
const char * pCaCert
Server trusted certificate store for this connection.
Definition: iot_https_types.h:748