mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-21 05:28:22 +00:00
webclient improvements
Highlights: * TLS support (a hook to allow users to provide TLS implementation) * ability to add extra request headers * ability to use PUT method * ability to report http status * error handling improvements Proposed on the ML while ago: https://www.mail-archive.com/dev@nuttx.apache.org/msg03803.html The original API is kept for now. I plan to remove them after adapting the existing users. (examples in this repo)
This commit is contained in:
parent
07f2318fae
commit
a1db71fa43
2 changed files with 565 additions and 147 deletions
|
|
@ -103,6 +103,140 @@
|
|||
typedef void (*wget_callback_t)(FAR char **buffer, int offset,
|
||||
int datend, FAR int *buflen, FAR void *arg);
|
||||
|
||||
/* webclient_sink_callback_t: callback to consume data
|
||||
*
|
||||
* Same as wget_callback_t, but allowed to fail.
|
||||
*
|
||||
* Input Parameters:
|
||||
* Same as wget_callback_t
|
||||
*
|
||||
* Return value:
|
||||
* 0 on success.
|
||||
* A negative errno on error.
|
||||
*/
|
||||
|
||||
typedef CODE int (*webclient_sink_callback_t)(FAR char **buffer, int offset,
|
||||
int datend, FAR int *buflen,
|
||||
FAR void *arg);
|
||||
|
||||
/* webclient_body_callback_t: a callback to provide request body
|
||||
*
|
||||
* This callback can be called multiple times to provide
|
||||
* webclient_context::bodylen bytes of the data.
|
||||
* It's the responsibility of this callback to maintain the current
|
||||
* "offset" of the data. (similarly to fread(3))
|
||||
*
|
||||
* An implementation of this callback should perform either of
|
||||
* the followings:
|
||||
*
|
||||
* - fill the buffer (specified by buffer and *sizep) with the data
|
||||
* - update *datap with a buffer filled with the data
|
||||
*
|
||||
* Either ways, it should update *sizep to the size of the data.
|
||||
*
|
||||
* A short result is allowed. In that case, this callback will be called
|
||||
* again to provide the remaining data.
|
||||
*
|
||||
* Input Parameters:
|
||||
* buffer - The buffer to fill.
|
||||
* sizep - The size of buffer/data in bytes.
|
||||
* datap - The data to return.
|
||||
* ctx - The value of webclient_context::body_callback_arg.
|
||||
*
|
||||
* Return value:
|
||||
* 0 on success.
|
||||
* A negative errno on error.
|
||||
*/
|
||||
|
||||
typedef CODE int (*webclient_body_callback_t)(
|
||||
FAR void *buffer,
|
||||
FAR size_t *sizep,
|
||||
FAR const void * FAR *datap,
|
||||
FAR void *ctx);
|
||||
|
||||
struct webclient_tls_connection;
|
||||
|
||||
struct webclient_tls_ops
|
||||
{
|
||||
CODE int (*connect)(FAR void *ctx,
|
||||
FAR const char *hostname, FAR const char *port,
|
||||
unsigned int timeout_second,
|
||||
FAR struct webclient_tls_connection **connp);
|
||||
CODE ssize_t (*send)(FAR void *ctx,
|
||||
FAR struct webclient_tls_connection *conn,
|
||||
FAR const void *buf, size_t len);
|
||||
CODE ssize_t (*recv)(FAR void *ctx,
|
||||
FAR struct webclient_tls_connection *conn,
|
||||
FAR void *buf, size_t len);
|
||||
CODE int (*close)(FAR void *ctx,
|
||||
FAR struct webclient_tls_connection *conn);
|
||||
};
|
||||
|
||||
struct webclient_context
|
||||
{
|
||||
/* request parameters
|
||||
*
|
||||
* method - HTTP method like "GET", "POST".
|
||||
* The default value is "GET".
|
||||
* url - A pointer to a string containing the full URL.
|
||||
* (e.g., http://www.nutt.org/index.html, or
|
||||
* http://192.168.23.1:80/index.html)
|
||||
* headers - An array of pointers to the extra headers.
|
||||
* nheaders - The number of elements in the "headers" array.
|
||||
* bodylen - The size of the request body.
|
||||
*/
|
||||
|
||||
FAR const char *method;
|
||||
FAR const char *url;
|
||||
FAR const char * FAR const *headers;
|
||||
unsigned int nheaders;
|
||||
size_t bodylen;
|
||||
|
||||
/* other parameters
|
||||
*
|
||||
* buffer - A user provided buffer to receive the file data
|
||||
* (also used for the outgoing GET request)
|
||||
* It should be large enough to hold the whole
|
||||
* request header. It should also be large enough
|
||||
* to hold the whole response header.
|
||||
* buflen - The size of the user provided buffer
|
||||
* sink_callback - As data is obtained from the host, this function
|
||||
* is to dispose of each block of file data as it is
|
||||
* received.
|
||||
* callback - a compat version of sink_callback.
|
||||
* sink_callback_arg - User argument passed to callback.
|
||||
* body_callback - A callback function to provide the request body.
|
||||
* body_callback_arg - User argument passed to body_callback.
|
||||
* tls_ops - A vector to implement TLS operations.
|
||||
* NULL means no https support.
|
||||
* tls_ctx - A user pointer to be passed to tls_ops as it is.
|
||||
*/
|
||||
|
||||
FAR char *buffer;
|
||||
int buflen;
|
||||
wget_callback_t callback;
|
||||
webclient_sink_callback_t sink_callback;
|
||||
FAR void *sink_callback_arg;
|
||||
webclient_body_callback_t body_callback;
|
||||
FAR void *body_callback_arg;
|
||||
FAR const struct webclient_tls_ops *tls_ops;
|
||||
FAR void *tls_ctx;
|
||||
|
||||
/* results
|
||||
*
|
||||
* http_status - HTTP status code
|
||||
* http_reason - A buffer to store HTTP reason phrase.
|
||||
* If NULL, the reason phrase will be discarded.
|
||||
* http_reason_len - The size of the http_reason buffer
|
||||
* A reason phrase longer than the buffer size
|
||||
* will be silently truncated.
|
||||
*/
|
||||
|
||||
unsigned int http_status;
|
||||
FAR char *http_reason;
|
||||
size_t http_reason_len;
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
|
@ -153,6 +287,12 @@ int wget(FAR const char *url, FAR char *buffer, int buflen,
|
|||
int wget_post(FAR const char *url, FAR const char *posts, FAR char *buffer,
|
||||
int buflen, wget_callback_t callback, FAR void *arg);
|
||||
|
||||
void webclient_set_defaults(FAR struct webclient_context *ctx);
|
||||
int webclient_perform(FAR struct webclient_context *ctx);
|
||||
void webclient_set_static_body(FAR struct webclient_context *ctx,
|
||||
FAR const void *body,
|
||||
size_t bodylen);
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue