FreeRTOS:
HTTPS Client
HTTPS Client v1.0.0 library
|
Return to main page ↑ |
User-facing functions of the FreeRTOS HTTPS Client library. More...
Go to the source code of this file.
Functions | |
IotHttpsReturnCode_t | IotHttpsClient_Init (void) |
One-time initialization of the IoT HTTPS Client library. More... | |
void | IotHttpsClient_Cleanup (void) |
One time clean up of the IoT HTTPS Client library. More... | |
IotHttpsReturnCode_t | IotHttpsClient_Connect (IotHttpsConnectionHandle_t *pConnHandle, IotHttpsConnectionInfo_t *pConnInfo) |
Explicitly connect to the HTTPS server given the connection configuration pConnConfig. More... | |
IotHttpsReturnCode_t | IotHttpsClient_Disconnect (IotHttpsConnectionHandle_t connHandle) |
Disconnect from the HTTPS server given the connection handle connHandle. More... | |
IotHttpsReturnCode_t | IotHttpsClient_InitializeRequest (IotHttpsRequestHandle_t *pReqHandle, IotHttpsRequestInfo_t *pReqInfo) |
Initializes the request by adding a formatted Request-Line to the start of HTTPS request header buffer. More... | |
IotHttpsReturnCode_t | IotHttpsClient_AddHeader (IotHttpsRequestHandle_t reqHandle, char *pName, uint32_t nameLen, char *pValue, uint32_t valueLen) |
Add a header to the current HTTPS request represented by reqHandle. More... | |
IotHttpsReturnCode_t | IotHttpsClient_WriteRequestBody (IotHttpsRequestHandle_t reqHandle, uint8_t *pBuf, uint32_t len, int isComplete) |
Writes the request body to the network for the request represented by reqHandle. More... | |
IotHttpsReturnCode_t | IotHttpsClient_SendSync (IotHttpsConnectionHandle_t connHandle, IotHttpsRequestHandle_t reqHandle, IotHttpsResponseHandle_t *pRespHandle, IotHttpsResponseInfo_t *pRespInfo, uint32_t timeoutMs) |
Synchronous send of the HTTPS request. More... | |
IotHttpsReturnCode_t | IotHttpsClient_SendAsync (IotHttpsConnectionHandle_t connHandle, IotHttpsRequestHandle_t reqHandle, IotHttpsResponseHandle_t *pRespHandle, IotHttpsResponseInfo_t *pRespInfo) |
Asynchronous send of the the HTTPS request. More... | |
IotHttpsReturnCode_t | IotHttpsClient_CancelRequestAsync (IotHttpsRequestHandle_t reqHandle) |
Cancel an Asynchronous request. More... | |
IotHttpsReturnCode_t | IotHttpsClient_CancelResponseAsync (IotHttpsResponseHandle_t respHandle) |
Cancel an Asynchronous response. More... | |
IotHttpsReturnCode_t | IotHttpsClient_ReadResponseStatus (IotHttpsResponseHandle_t respHandle, uint16_t *pStatus) |
Retrieve the HTTPS response status. More... | |
IotHttpsReturnCode_t | IotHttpsClient_ReadContentLength (IotHttpsResponseHandle_t respHandle, uint32_t *pContentLength) |
Retrieve the HTTPS response content length. More... | |
IotHttpsReturnCode_t | IotHttpsClient_ReadHeader (IotHttpsResponseHandle_t respHandle, char *pName, uint32_t nameLen, char *pValue, uint32_t valueLen) |
Retrieve the header of interest from the response represented by respHandle. More... | |
IotHttpsReturnCode_t | IotHttpsClient_ReadResponseBody (IotHttpsResponseHandle_t respHandle, uint8_t *pBuf, uint32_t *pLen) |
Read the HTTPS response body from the network. More... | |
User-facing functions of the FreeRTOS HTTPS Client library.
IotHttpsReturnCode_t IotHttpsClient_Init | ( | void | ) |
One-time initialization of the IoT HTTPS Client library.
This must be called once before calling any API.
void IotHttpsClient_Cleanup | ( | void | ) |
One time clean up of the IoT HTTPS Client library.
This function frees resources taken in in IotHttpsClient_Init. It should be called after all HTTPS Connections have been close. HTTPS Connections are represented by IotHttpsConnectionHandle_t and returned by IotHttpsClient_Connect. After this function returns IotHttpsClient_Init must be called again to use this library.
IotHttpsReturnCode_t IotHttpsClient_Connect | ( | IotHttpsConnectionHandle_t * | pConnHandle, |
IotHttpsConnectionInfo_t * | pConnInfo | ||
) |
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.
[out] | pConnHandle | - Handle returned representing the open connection. NULL if the function failed. |
[in] | pConnInfo | - Configurations for the HTTPS connection. |
Example
IotHttpsReturnCode_t IotHttpsClient_Disconnect | ( | IotHttpsConnectionHandle_t | connHandle | ) |
Disconnect from the HTTPS server given the connection handle connHandle.
This routine blocks until the disconnect is complete. If the connection handle is not valid, the behavior is undefined. If the connection handle is already disconnected then this routine will return IOT_HTTPS_OK.
When the HTTP request is specified as persistent and we want to close the connection, this API must always be called on the valid IotHttpsConnectionHandle_t. For more information about persistent HTTP connections please see IotHttpsRequestInfo_t.isNonPersistent.
When the HTTP request is specified as non-persistent, by setting IotHttpsRequestInfo_t.isNonPersistent to true, then this function will be called automatically on the valid IotHttpsConnectionHandle_t after receiving the response. There is no need to call this function in case of a non-persistent request.
This will put the internal connection state in IotHttpsConnectionHandle_t to disconnected.
If the application receives a IOT_HTTPS_NETWORK_ERROR from IotHttpsClient_SendSync or IotHttpsClient_SendAsync, on a persistent request, that does not always mean the connection has been disconnected. This function MUST be called to close the connection and clean up connection resources taken by IotHttpsConnectionHandle_t.
This function will cancel all pending requests on the connection. If a request currently being sent on the connection, then this function will disconnect the connection, but it will not free network connection resource and will return with IOT_HTTPS_BUSY. The application may call this function again later to try again.
Multiple threads must not call this function for the same IotHttpsConnectionHandle_t. Multiple threads can call this function for different IotHttpsConnectionHandle_t. Make sure that all request/responses have finished on the connection before calling this API. Outstanding requests are completed when IotHttpsClient_SendSync has returned or when IotHttpsClientCallbacks_t.responseCompleteCallback has been invoked for requests scheduled with IotHttpsClient_SendAsync.
[in] | connHandle | - Valid handle representing an open connection. |
IotHttpsReturnCode_t IotHttpsClient_InitializeRequest | ( | IotHttpsRequestHandle_t * | pReqHandle, |
IotHttpsRequestInfo_t * | pReqInfo | ||
) |
Initializes the request by adding a formatted Request-Line to the start of HTTPS request header buffer.
This function will initialize the HTTP request context by setting where to write the next headers to the start of the configured header buffer in IotHttpsRequestInfo_t.userBuffer.
The Request-Line will be added to the start of the headers space in IotHttpsRequestInfo_t.userBuffer. The header space follows the request context in the user buffer. See requestUserBufferMinimumSize for more information on sizing the IotHttpsRequestInfo_t.userBuffer so that this function does not fail.
The Request-Line generated is of the following format:
Example:
The initial required headers are also added to the IotHttpsRequestInfo_t.userBuffer. These headers are User-Agent and Host. The User-Agent value is configured in iot_config.h using IOT_HTTPS_USER_AGENT. The Host value is the DNS resolvable server address.
[out] | pReqHandle | - request handle representing the internal request context is returned. NULL if the function failed. |
[in] | pReqInfo | - HTTPS request information. |
Example
IotHttpsReturnCode_t IotHttpsClient_AddHeader | ( | IotHttpsRequestHandle_t | reqHandle, |
char * | pName, | ||
uint32_t | nameLen, | ||
char * | pValue, | ||
uint32_t | valueLen | ||
) |
Add a header to the current HTTPS request represented by reqHandle.
The header line is appended to the request header buffer space in IotHttpsRequestInfo_t.userBuffer. Please see requestUserBufferMinimumSize for information about sizing the IotHttpsRequestInfo_t.userBuffer so that this function does not fail.
Header lines are appended in the following format:
Example:
The last header line must be followed by a "\r\n" to separate the last header line from the entity body. These 2 characters are accounted for in requestUserBufferMinimumSize.
The remaining length, after the header is added, is printed to the system configured standard debug output when IOT_LOG_LEVEL_HTTPS is set to IOT_LOG_DEBUG in iot_config.h.
For an asynchronous request, this function can be invoked before the request is sent with IotHttpsClient_SendAsync, or during IotHttpsClientCallbacks_t.appendHeaderCallback. It is recommended to invoke this function in IotHttpsClientCallbacks_t.appendHeaderCallback.
Asynchronous Example
For a synchronous request, if extra headers are desired to be added, this function must be invoked before IotHttpsClient_SendSync. Synchronous Example
The following header fields are automatically added to the request header buffer and must NOT be added again with this routine:
The reqHandle is not thread safe. If two threads have the same reqHandle and attempt to add headers at the same time, garbage strings may be written to the reqHandle.
[in] | reqHandle | - HTTPS request to write the header line to. |
[in] | pName | - String header field name to write. |
[in] | nameLen | - The length of the header name to write. |
[in] | pValue | - https header value buffer pointer. Do not include token name. |
[in] | valueLen | - length of header value to write. |
IotHttpsReturnCode_t IotHttpsClient_WriteRequestBody | ( | IotHttpsRequestHandle_t | reqHandle, |
uint8_t * | pBuf, | ||
uint32_t | len, | ||
int | isComplete | ||
) |
Writes the request body to the network for the request represented by reqHandle.
This function is intended to be used by an asynchronous request. It must be called within the IotHttpsClientCallbacks_t.writeCallback.
In HTTP/1.1 the headers are sent on the network first before any body can be sent. The auto-generated header Content-Length is taken from the len parameter and sent first before the data in parameter pBuf is sent. This library does not support Transfer-Encoding: chunked or other requests where the Content-Length is unknown, so this function cannot be called more than once in IotHttpsClientCallbacks_t.writeCallback for an HTTP/1.1 request.
isComplete must always be set to 1 in this current version of the HTTPS client library.
If there are network errors in sending the HTTP headers, then the IotHttpsClientCallbacks_t.errorCallback will be invoked following a return from the IotHttpsClientCallbacks_t.writeCallback.
Example Asynchronous Code
[in] | reqHandle | - identifier of the connection. |
[in] | pBuf | - client write data buffer pointer. |
[in] | len | - length of data to write. |
[in] | isComplete | - This parameter parameter must be set to 1. |
IotHttpsReturnCode_t IotHttpsClient_SendSync | ( | IotHttpsConnectionHandle_t | connHandle, |
IotHttpsRequestHandle_t | reqHandle, | ||
IotHttpsResponseHandle_t * | pRespHandle, | ||
IotHttpsResponseInfo_t * | pRespInfo, | ||
uint32_t | timeoutMs | ||
) |
Synchronous send of the HTTPS request.
This function blocks waiting for the entirety of sending the request and receiving the response.
If IotHttpsRequestInfo_t.isNonPersistent is set to true, then the connection will disconnect, close, and clean all taken resources automatically after receiving the first response.
See connectionUserBufferMinimumSize for information about the user buffer configured in IotHttpsConnectionInfo_t.userBuffer needed to create a valid connection handle.
To retrieve the response body applications must directly refer IotHttpsSyncInfo_t.pBody configured in IotHttpsRequestInfo_t.u.
If the response body does not fit in the configured IotHttpsSyncInfo_t.pBody, then this function will return with error IOT_HTTPS_MESSAGE_TOO_LARGE. To avoid this issue, the application needs to determine beforehand how large the file to download is. This can be done with a HEAD request first, then extracting the "Content-Length" with IotHttpsClient_ReadContentLength. This could also be done with a GET request with the header "Range: bytes=0-0", then extracting the "Content-Range" with IotHttpsClient_ReadHeader. Keep in mind that not all HTTP servers support Partial Content responses.
Once a the file size is known, the application can initialize the request with a large enough buffer or the application can make a partial content request with the header "Range: bytes=N-M", where N is the starting byte requested and M is the ending byte requested.
The response headers as received from the network will be stored in the header buffer space in IotHttpsResponseInfo_t.userBuffer. If the configured IotHttpsResponseInfo_t.userBuffer is too small to fit the headers received, then headers that don't fit will be thrown away. Please see responseUserBufferMinimumSize for information about sizing the IotHttpsResponseInfo_t.userBuffer. To receive feedback on headers discarded, debug logging must be turned on in iot_config.h by setting IOT_LOG_LEVEL_HTTPS to IOT_LOG_DEBUG.
Multiple threads must not call this function for the same IotHttpsRequestHandle_t. Multiple threads can call this function for a different IotHttpsRequestHandle_t, even on the same IotHttpsConnectionHandle_t. An application must wait util a request is fully sent, before scheduling it again. A request is fully sent when this function has returned.
[in] | connHandle | - Handle from an HTTPS connection created with IotHttpsClient_Connect. |
[in] | reqHandle | - Handle from a request created with IotHttpsClient_InitializeRequest. |
[out] | pRespHandle | - HTTPS response handle resulting from a successful send and receive. |
[in] | pRespInfo | - HTTP response configuration information. |
[in] | timeoutMs | - Timeout waiting for the sync request to finish. Set this to 0 to wait forever. |
IotHttpsReturnCode_t IotHttpsClient_SendAsync | ( | IotHttpsConnectionHandle_t | connHandle, |
IotHttpsRequestHandle_t | reqHandle, | ||
IotHttpsResponseHandle_t * | pRespHandle, | ||
IotHttpsResponseInfo_t * | pRespInfo | ||
) |
Asynchronous send of the the HTTPS request.
This function will invoke, as needed, each of the non-NULL callbacks configured in IotHttpsAsyncInfo_t.callbacks when the scheduled asynchronous request is progress. Please see IotHttpsClientCallbacks_t for information on each of the callbacks.
After this API is executed, the scheduled async response will store the response headers as received from the network, in the header buffer space configured in IotHttpsResponseInfo_t.userBuffer. If the configured IotHttpsResponseInfo_t.userBuffer is too small to fit the headers received, then headers that don't fit will be thrown away. Please see responseUserBufferMinimumSize for information about sizing the IotHttpsResponseInfo_t.userBuffer.
If IotHttpsRequestInfo_t.isNonPersistent is set to true, then the connection will disconnect, close, and clean all taken resources automatically after receiving the first response.
See connectionUserBufferMinimumSize for information about the user buffer configured in IotHttpsConnectionInfo_t.userBuffer needed to create a valid connection handle.
A IotHttpsRequestHandle_t cannot be schedule again or reused until the request has finished sending. The request has safely finished sending once IotHttpsClientCallbacks_t.readReadyCallback is invoked. After the IotHttpsClientCallbacks_t.readReadyCallback is invoked the IotHttpsRequestInfo_t.userBuffer can freed, modified, or reused.
[in] | connHandle | - Handle from an HTTPS connection. |
[in] | reqHandle | - Handle from a request created with IotHttpsClient_initialize_request. |
[out] | pRespHandle | - HTTPS response handle. |
[in] | pRespInfo | - HTTP response configuration information. |
IotHttpsReturnCode_t IotHttpsClient_CancelRequestAsync | ( | IotHttpsRequestHandle_t | reqHandle | ) |
Cancel an Asynchronous request.
This will stop an asynchronous request. When an asynchronous request is stopped it will not proceed to do any of the following: send headers, send body, receive headers, or receive body. This depends on where in the process the request is. For example, if the request is cancelled after sending the headers, then it will not attempt tp send the body. A cancelled return code will be returned to the application.
If this is called before the scheduled asynchronous request actually runs, then request will not be sent. If this is called during any of the asynchronous callbacks, then the library will stop processing the request when the callback returns. This is useful for any error conditions, found during the asynchronous callbacks, where the application wants to stop the rest of the request processing.
If the asynchronous request stops processing, the buffers in IotHttpsRequestInfo_t.userBuffer can be safely freed, modified, or reused, only once IotHttpsClientCallbacks_t.readReadyCallback is invoked.
Example Asynchronous Code
[in] | reqHandle | - Request handle associated with the request. |
IotHttpsReturnCode_t IotHttpsClient_CancelResponseAsync | ( | IotHttpsResponseHandle_t | respHandle | ) |
Cancel an Asynchronous response.
This will stop an asynchronous response. When an asynchronous response is stopped it will not proceed to do any of the following: send headers, send body, receive headers, or receive body. This depends on where in the process the response is. For example, if the response is cancelled after receiving the headers, then it will not attempt tp receive the body. A cancelled return code will be returned to the application.
If this is called during ANY of the asynchronous callbacks, then the library will stop processing the response when the callback returns. This is useful for any error conditions, found during the asynchronous callbacks, where the application wants to stop the rest of the response processing.
If the asynchronous response stops processing, the buffers configured in IotHttpsResponseInfo_t.userBuffer can be freed, modified, or reused only after the IotHttpsClientCallbacks_t.responseCompleteCallback in invoked.
Example Asynchronous Code
[in] | respHandle | - Response handle associated with the response. |
IotHttpsReturnCode_t IotHttpsClient_ReadResponseStatus | ( | IotHttpsResponseHandle_t | respHandle, |
uint16_t * | pStatus | ||
) |
Retrieve the HTTPS response status.
The HTTP response status code is contained in the Status-Line of the response header buffer configured in IotHttpsResponseInfo_t.userBuffer. It is the first line of a standard HTTP response message. If the response Status-Line could not fit into IotHttpsResponseInfo_t.userBuffer, then this function will return an error code. Please see responseUserBufferMinimumSize for information about sizing the IotHttpsResponseInfo_t.userBuffer.
This routine can be used for both a synchronous and asynchronous response.
Example Synchronous Code
For an asynchronous response the response status is the status parameter in IotHttpsClientCallbacks_t.readReadyCallback and IotHttpsClientCallbacks_t.responseCompleteCallback. The application should refer to that instead of using this function. Example Asynchronous Code
[in] | respHandle | - Unique handle representing the HTTPS response. |
[out] | pStatus | - Integer status returned by the server. |
IotHttpsReturnCode_t IotHttpsClient_ReadContentLength | ( | IotHttpsResponseHandle_t | respHandle, |
uint32_t * | pContentLength | ||
) |
Retrieve the HTTPS response content length.
If the "Content-Length" header is available in IotHttpsResponseInfo_t.userBuffer, this routine extracts that value. In some cases the "Content-Length" header is not available. This could be because the server sent a multi-part encoded response (For example, "Transfer-Encoding: chunked") or the "Content-Length" header was far down in the list of response headers and could not fit into the header buffer configured in IotHttpsResponseInfo_t.userBuffer. Please see responseUserBufferMinimumSize for information about sizing the IotHttpsResponseInfo_t.userBuffer.
In the asynchronous request process, the Content-Length is not available until the IotHttpsClientCallbacks_t.readReadyCallback. Before the IotHttpsClientCallbacks_t.readReadyCallback is invoked, the headers are read into as much as can fit in in the header buffer space of IotHttpsResponseInfo_t.userBuffer. Example Asynchronous Code
In a synchronous request process, the Content-Length is available after IotHttpsClient_SendSync has returned successfully. Example Synchronous Code
[in] | respHandle | - Unique handle representing the HTTPS response. |
[out] | pContentLength | - Integer content length from the Content-Length header from the server. If the content length is not found this will be 0. |
IotHttpsReturnCode_t IotHttpsClient_ReadHeader | ( | IotHttpsResponseHandle_t | respHandle, |
char * | pName, | ||
uint32_t | nameLen, | ||
char * | pValue, | ||
uint32_t | valueLen | ||
) |
Retrieve the header of interest from the response represented by respHandle.
The response headers as received from the network will be stored in the header buffer space in IotHttpsResponseInfo_t.userBuffer. If the configured IotHttpsResponseInfo_t.userBuffer is too small to fit the headers received, then headers that don't fit will be thrown away. Please see responseUserBufferMinimumSize for information about sizing the IotHttpsResponseInfo_t.userBuffer.
This routine parses the formatted HTTPS header lines in the header buffer for the header field name specified. If the header is not available, then IOT_HTTPS_NOT_FOUND is returned.
For an asynchronous response, this routine is to be called during the IotHttpsClientCallbacks_t.readReadyCallback. Before the IotHttpsClientCallbacks_t.readReadyCallback is invoked, the headers are read into as much as can fit in in the header buffer space of IotHttpsResponseInfo_t.userBuffer. Example Asynchronous Code
For a syncrhonous response, this routine is to be called after IotHttpsClient_SendSync has returned successfully. Example Synchronous Code
[in] | respHandle | - Unique handle representing the HTTPS response. |
[in] | pName | - HTTPS Header field name we want the value of. This must be NULL terminated. |
[in] | nameLen | - The length of the name string. |
[out] | pValue | - Buffer to hold the HTTPS field's value. The returned value will be NULL terminated and therfore the buffer must be large enough to hold the terminating NULL character. |
[in] | valueLen | - The length of the value buffer. |
IotHttpsReturnCode_t IotHttpsClient_ReadResponseBody | ( | IotHttpsResponseHandle_t | respHandle, |
uint8_t * | pBuf, | ||
uint32_t * | pLen | ||
) |
Read the HTTPS response body from the network.
This is intended to be used with an asynchronous response, this is to be invoked during the IotHttpsClientCallbacks_t.readReadyCallback to read data directly from the network into pBuf. Example Asynchronous Code
For a syncrhonous response, to retrieve the response body applications must directly refer to the memory configured to receive the response body: IotHttpsSyncInfo_t.pBody in IotHttpsResponseInfo_t.pSyncInfo. Otherwise this function will return an IOT_HTTPS_INVALID_PARAMETER error code. This function is intended to read the response entity body from the network and the synchronous response process handles all of that in IotHttpsClient_SendSync.
[in] | respHandle | - Unique handle representing the HTTPS response. |
[out] | pBuf | - Pointer to the response body memory location. This is not a char* because the body may have binary data. |
[in,out] | pLen | - The length of the response to read. This should not exceed the size of the buffer that we are reading into. This will be replace with the amount of data read upon return. |