|
Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
|
Multiprotocol HTTP client API. More...
Data Structures | |
| struct | http_client_header |
| An HTTP header field, a name and value pair. More... | |
| struct | http_client_transport_ops |
| HTTP transport API. More... | |
| struct | http_client_transport_tcp_ctx |
| TCP transport context for the HTTP client. More... | |
| struct | http_client_transport_tls_config |
| TLS configuration for the HTTP client. More... | |
| struct | http_client_transport_tls_ctx |
| TLS transport context for the HTTP client. More... | |
| struct | http_client_response |
| HTTP response context. More... | |
| struct | http_client_request |
| HTTP request context. More... | |
| struct | http_client_stream |
| HTTP stream context. More... | |
| struct | http_client_session |
| HTTP session context. More... | |
| struct | http_client_session_config |
| HTTP session configuration. More... | |
| struct | http_client |
| HTTP client context. More... | |
Typedefs | |
| typedef int(* | http_client_socket_create_cb_t) (struct http_client *client, int af, int proto) |
| A function to offload socket creation for the transport layer. | |
| typedef int(* | http_client_payload_cb_t) (size_t offset, const uint8_t **payload, size_t *len, bool *more, void *user_data) |
| Callback for providing payload for the HTTP request. | |
| typedef int(* | http_client_headers_cb_t) (const struct http_client_header **headers, size_t *count, bool *more, void *user_data) |
| Callback for providing additional headers for the HTTP request. | |
| typedef int(* | http_client_response_cb_t) (struct http_client *client, const struct http_client_request *request, struct http_client_response *response, enum http_client_transaction_status status, void *user_data) |
| Callback used when data is received from the server or an error occurred. | |
Enumerations | |
| enum | http_client_version { HTTP_CLIENT_VERSION_1_1 , HTTP_CLIENT_VERSION_2 , HTTP_CLIENT_VERSION_3 } |
| HTTP protocol version. More... | |
| enum | http_client_transaction_status { HTTP_CLIENT_TRANSACTION_ABORTED = -1 , HTTP_CLIENT_RESPONSE_DATA_MORE = 0 , HTTP_CLIENT_RESPONSE_DATA_FINAL = 1 } |
| State of the HTTP exchange passed to the callback. More... | |
| enum | http_client_session_state { HTTP_CLIENT_SESSION_CLOSED , HTTP_CLIENT_SESSION_OPEN , HTTP_CLIENT_SESSION_CLOSING } |
| HTTP client session state. More... | |
Functions | |
| void | http_client_received_data (struct http_client *client, int status) |
| Notify HTTP client about a transport reception event. | |
| int | http_client_init (struct http_client *client, enum http_client_version version, const struct http_client_transport_ops *transport_ops, void *transport_ctx) |
| Initialize HTTP client with a custom transport. | |
| int | http_client_init_tcp (struct http_client *client, enum http_client_version version, struct http_client_transport_tcp_ctx *transport_ctx) |
| Initialize HTTP client with TCP transport. | |
| int | http_client_init_tls (struct http_client *client, enum http_client_version version, struct http_client_transport_tls_ctx *transport_ctx) |
| Initialize HTTP client with TLS transport. | |
| int | http_client_connect (struct http_client *client, struct http_client_session_config *config) |
| Connect HTTP client to a HTTP server. | |
| int | http_client_send_request (struct http_client *client, const struct http_client_request *request, int32_t timeout, void *user_data) |
| Send HTTP request to a HTTP server. | |
| int | http_client_disconnect (struct http_client *client) |
| Disconnect HTTP client from a HTTP server. | |
Multiprotocol HTTP client API.
An HTTP client API designed to carry HTTP/1.1, HTTP/2 and HTTP/3 over pluggable transports. Only HTTP/2 is implemented so far.
| typedef int(* http_client_headers_cb_t) (const struct http_client_header **headers, size_t *count, bool *more, void *user_data) |
#include <zephyr/net/http/client_multiprotocol.h>
Callback for providing additional headers for the HTTP request.
An optional callback for providing additional headers for the HTTP client request. If set in http_client_request, the HTTP client library will call this callback when sending the request.
When called, the application sets the pointer to an array of headers, the number of entries in it and whether more headers are to follow. The library will keep calling the callback until the more parameter is set to false. The array must stay valid until the next call or the completion of the request.
The fields are sent as they are handed out. An invalid field, or one too large for the transmit buffer, found after part of the request is already on the wire cannot be recovered from: the session is closed, this request fails with the cause and the other requests in flight with -ECONNABORTED.
A Host header is only honoured in http_client_request::header_fields, as the authority is settled before the callback runs; one handed out here fails the request.
| headers | A pointer for the array containing additional headers |
| count | Number of entries in the header array |
| more | A pointer to the flag indicating whether callback should be called again |
| user_data | User provided context specified in http_client_send_request() |
| typedef int(* http_client_payload_cb_t) (size_t offset, const uint8_t **payload, size_t *len, bool *more, void *user_data) |
#include <zephyr/net/http/client_multiprotocol.h>
Callback for providing payload for the HTTP request.
An optional callback for providing a payload for the HTTP client request. If set in http_client_request, the HTTP client library will call this callback when sending the request.
When called, the library provides the application with the current payload offset for the transfer. In return, the application sets the payload pointer, payload size and information whether more data is expected. The library will keep calling the callback until the more parameter is set to false.
The final call may report a zero len, in case the application does not know upfront which chunk is the last one. A zero len is only valid when more is set to false.
| offset | Payload offset |
| payload | A pointer for the buffer containing the payload |
| len | A pointer to the payload length |
| more | A pointer to the flag indicating whether callback should be called again |
| user_data | User provided context specified in http_client_send_request() |
| typedef int(* http_client_response_cb_t) (struct http_client *client, const struct http_client_request *request, struct http_client_response *response, enum http_client_transaction_status status, void *user_data) |
#include <zephyr/net/http/client_multiprotocol.h>
Callback used when data is received from the server or an error occurred.
A response is reported as a sequence of callbacks: one for each header block, carrying the decoded headers and no body data (several when the block does not fit in the headers buffer), then one for each chunk of body data, carrying no headers. Header blocks are the response headers, preceded by any 1xx interim response and followed by trailers if the server sends them. The last callback carries HTTP_CLIENT_RESPONSE_DATA_FINAL. Pointers in response are only valid for the duration of the callback.
In a synchronous session the callback runs on the thread that called http_client_send_request(). It may call http_client_disconnect(), which ends the session at once: a transaction whose response is not complete yet is reported as aborted with -ECONNABORTED from inside the running callback, and its http_client_send_request() call returns that error. In an asynchronous session the callback runs on the transport's receive context or, on a timeout, the client's own work queue, and must neither block nor call back into the client API.
A negative return value aborts the transaction, which is then reported once more, with HTTP_CLIENT_TRANSACTION_ABORTED and that value as the error. This holds for the final chunk as well, although nothing is left to abort on the wire by then.
| client | HTTP client context |
| request | HTTP request the response belongs to |
| response | HTTP response data. On HTTP_CLIENT_TRANSACTION_ABORTED it holds whatever was received before the abort, with the reason in http_client_response::error. |
| status | Transaction status (incomplete/complete/aborted). |
| user_data | User specified data specified in http_client_send_request() |
| typedef int(* http_client_socket_create_cb_t) (struct http_client *client, int af, int proto) |
#include <zephyr/net/http/client_multiprotocol.h>
A function to offload socket creation for the transport layer.
The socket belongs to the transport from then on: it is configured, connected and eventually closed by it.
| client | HTTP client instance |
| af | Address family |
| proto | Protocol |
#include <zephyr/net/http/client_multiprotocol.h>
HTTP client session state.
| Enumerator | |
|---|---|
| HTTP_CLIENT_SESSION_CLOSED | Session closed. |
| HTTP_CLIENT_SESSION_OPEN | Session open. |
| HTTP_CLIENT_SESSION_CLOSING | Session being torn down. |
#include <zephyr/net/http/client_multiprotocol.h>
State of the HTTP exchange passed to the callback.
| enum http_client_version |
#include <zephyr/net/http/client_multiprotocol.h>
HTTP protocol version.
| Enumerator | |
|---|---|
| HTTP_CLIENT_VERSION_1_1 | HTTP/1.1. |
| HTTP_CLIENT_VERSION_2 | HTTP/2. |
| HTTP_CLIENT_VERSION_3 | HTTP/3. |
| int http_client_connect | ( | struct http_client * | client, |
| struct http_client_session_config * | config ) |
#include <zephyr/net/http/client_multiprotocol.h>
Connect HTTP client to a HTTP server.
Opens the transport connection and the HTTP session on it. A failure of the transport is reported with its own error code.
| client | HTTP client instance |
| config | HTTP session configuration |
| 0 | Success |
| -EINVAL | Invalid argument, such as a server address of the wrong length or an invalid host name |
| -EALREADY | A session is open already |
| -EBUSY | A request of the previous session is still being sent |
| -ETIMEDOUT | The server did not answer in time |
| -ECONNREFUSED | The server refused the connection |
| int http_client_disconnect | ( | struct http_client * | client | ) |
#include <zephyr/net/http/client_multiprotocol.h>
Disconnect HTTP client from a HTTP server.
Ends the session, notifying the server and aborting the requests in flight, and closes the transport. Also waits for a deadline handler of the client that may be running and for the other threads still inside http_client_send_request() to return, so that the client can be reinitialized or freed afterwards; call it before reinitializing a client whose session ended on its own as well.
The notifications to the server are a courtesy: the session is closed whether they could be sent or not. A failure to close the transport is reported with the transport's own error code.
| client | HTTP client instance |
| 0 | Success, also when no session was open |
| -EINVAL | Invalid argument |
| int http_client_init | ( | struct http_client * | client, |
| enum http_client_version | version, | ||
| const struct http_client_transport_ops * | transport_ops, | ||
| void * | transport_ctx ) |
#include <zephyr/net/http/client_multiprotocol.h>
Initialize HTTP client with a custom transport.
A client that was initialized before has to be disconnected with http_client_disconnect() first, even when its session ended on its own, so that no deadline handler is still running on it.
| client | HTTP client instance |
| version | HTTP version to use |
| transport_ops | Transport layer API to use |
| transport_ctx | A pointer to the context data for the transport layer |
| 0 | Success |
| -EINVAL | Invalid argument |
| -ENOTSUP | No protocol layer for the HTTP version |
| int http_client_init_tcp | ( | struct http_client * | client, |
| enum http_client_version | version, | ||
| struct http_client_transport_tcp_ctx * | transport_ctx ) |
#include <zephyr/net/http/client_multiprotocol.h>
Initialize HTTP client with TCP transport.
See http_client_init() for reinitializing a client.
| client | HTTP client instance |
| version | HTTP version to use |
| transport_ctx | A pointer to the TCP transport context |
| 0 | Success |
| -EINVAL | Invalid argument |
| -ENOTSUP | No protocol layer for the HTTP version |
| int http_client_init_tls | ( | struct http_client * | client, |
| enum http_client_version | version, | ||
| struct http_client_transport_tls_ctx * | transport_ctx ) |
#include <zephyr/net/http/client_multiprotocol.h>
Initialize HTTP client with TLS transport.
Available when CONFIG_HTTP_CLIENT_TRANSPORT_TLS is enabled. The transport creates a TLS socket, applies the configuration from the context and advertises the HTTP version in use via ALPN. See http_client_init() for reinitializing a client.
| client | HTTP client instance |
| version | HTTP version to use |
| transport_ctx | A pointer to the TLS transport context |
| 0 | Success |
| -EINVAL | Invalid argument |
| -ENOTSUP | No protocol layer for the HTTP version |
| void http_client_received_data | ( | struct http_client * | client, |
| int | status ) |
#include <zephyr/net/http/client_multiprotocol.h>
Notify HTTP client about a transport reception event.
Should be called by the transport implementation when new data is available to read, or when a transport-level error condition occurred.
| client | HTTP client instance |
| status | Reception status. 0 indicates that new data is available to read. A negative errno code indicates a transport-level error, in which case the affected transaction(s) are aborted and no data is read. |
| int http_client_send_request | ( | struct http_client * | client, |
| const struct http_client_request * | request, | ||
| int32_t | timeout, | ||
| void * | user_data ) |
#include <zephyr/net/http/client_multiprotocol.h>
Send HTTP request to a HTTP server.
If the HTTP session was configured to be synchronous, the function will block until the request/response exchange is complete.
If the HTTP session was configured to be asynchronous, the function will exit as soon as the entire request was sent, allowing to send multiple requests in parallel from a single thread. Sending a request payload may still block, in either mode, until the server grants the flow control credit for it or the timeout expires.
In both cases, the response is returned to the application via the callback provided in the req structure.
When the server shuts the connection down gracefully, requests it has not processed fail with -ESHUTDOWN and can be repeated on a new connection. The session closes by itself once the remaining transactions are complete.
A failure of the transport while sending ends the session, and is reported with the transport's own error code, a send that timed out as -ETIMEDOUT. In a synchronous session the call also returns how the transaction ended. In an asynchronous one it returns 0 once the request is sent, and the outcome reaches the callback instead, as http_client_response::error on an aborted transaction; only a transaction that ends while the request is still being sent, for example while waiting for flow control credit for its body, is reported by the call as well. A callback that aborts the transaction ends it with its own return value.
A request the call refuses, with -EINVAL or -EBUSY for example, is reported to the callback as aborted with the same error as well, so that every request passed in ends with a callback.
| client | HTTP client instance |
| request | HTTP request context. In async mode, should remain valid throughout the exchange. |
| timeout | Maximum time to wait for the response, in milliseconds, counted from the call. A negative value waits forever. |
| user_data | User specified data that is passed to the callback. |
| 0 | Success |
| -EINVAL | Invalid argument, such as a header field or request target that cannot be sent |
| -ENOTSUP | The request cannot be expressed in the protocol, such as the CONNECT method |
| -ENOTCONN | No session is open |
| -ENOMEM | No stream is free for the request |
| -EBUSY | The server allows no more concurrent requests, or a request is in flight already on a synchronous session |
| -ESHUTDOWN | The server is closing the connection, or the connection is used up; the request can be repeated on a new one |
| -ENOBUFS | The request does not fit the transmit buffer, or a response header field does not fit the receive buffer |
| -ETIMEDOUT | The response did not arrive in time, or a send timed out |
| -ECONNRESET | The server reset the request |
| -EPROTO | The response was malformed |
| -EBADMSG | The server violated the protocol, which ended the session |
| -ECONNABORTED | The session ended while the request was in flight |