VSF Documented
vsf_http_client.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __VSF_HTTP_CLIENT_H__
19#define __VSF_HTTP_CLIENT_H__
20
21/*============================ INCLUDES ======================================*/
22
24// for vsf_err_t return type of vsf_http_client_init/fini
26
27#if VSF_USE_MBEDTLS == ENABLED
28// DO NOT include mbedtls_tls_session.h here, because it will undefine _WIN32 for VSF,
29// but some windows based environment will need this
30//# include "component/3rd-party/mbedtls/extension/tls_session/mbedtls_tls_session.h"
31#endif
32
33#if defined(__VSF_HTTP_CLIENT_CLASS_IMPLEMENT)
34# define __VSF_CLASS_IMPLEMENT__
35#elif defined(__VSF_HTTP_CLIENT_CLASS_INHERIT__)
36# define __VSF_CLASS_INHERIT__
37#endif
38
39#include "utilities/ooc_class.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/*============================ MACROS ========================================*/
46
47#ifndef VSF_HTTP_CLIENT_CFG_BUFFER_SIZE
48# define VSF_HTTP_CLIENT_CFG_BUFFER_SIZE 4096
49#endif
50
51#ifndef VSF_HTTP_CLIENT_CFG_USER_AGENT
52// for some website, not all user agent are accepted, so use fake curl here
53# define VSF_HTTP_CLIENT_CFG_USER_AGENT "curl/8.5.0"
54#endif
55
56/*============================ MACROFIED FUNCTIONS ===========================*/
57/*============================ TYPES =========================================*/
58
60
61vsf_class(vsf_http_op_t) {
62 protected_member(
63 int (*fn_connect)(void *param, const char *host, const char *port);
64 void (*fn_close)(void *param);
65 // len is total bytes to transfer in this call. Implementations MAY
66 // short-write/short-read (return value < len); callers use
67 // vsf_http_client_write/read to loop. Return <= 0 indicates error.
68 // Note on signedness: return type is int while len is size_t. In
69 // practice the HTTP request/response buffers used by this library
70 // are far below INT_MAX, so the narrowing is always safe; this
71 // mirrors the signature convention of mbedtls_ssl_read/write.
72 int (*fn_write)(void *param, uint8_t *buf, size_t len);
73 int (*fn_read)(void *param, uint8_t *buf, size_t len);
74
75 // Optional. Update the transport's read timeout (ms); semantics of
76 // timeout_ms <= 0 is "no timeout / blocking". Both TCP and TLS ports
77 // should accept calls before and after a connection is established:
78 // upper layers (e.g. esp_http_client_set_timeout_ms) may set the
79 // value either way round. Return 0 on success, < 0 on failure.
80 // When NULL, vsf_http_client_set_timeout is a no-op (feature absent).
81 int (*fn_set_timeout)(void *param, int timeout_ms);
82
83 // Optional. Return the underlying socket fd for integration with
84 // select()/poll(). ONLY meaningful when the port runs on top of the
85 // VSF linux socket layer (VSF_USE_LINUX == ENABLED); ports on other
86 // net backends (bare winsock, raw lwip, custom bio) should leave
87 // this field NULL -- callers must then treat "no fd available"
88 // (vsf_http_client_get_fd returns -1).
89 int (*fn_get_fd)(void *param);
90 )
91};
92
95 const char *host;
96 const char *port;
97 const char *verb;
98 const char *connect_mode;
99
100 char *header;
101 char *path;
102
105
106 // IMPORTANT: line is NULL terminated string ending with \r
108};
109
111 public_member(
112 const vsf_http_op_t *op;
113 void *param;
114 int resp_status;
115 int content_length;
116 char *redirect_path;
117
118 // Working buffer for header assembly / response parsing.
119 // Caller may provide their own (static / pre-allocated) buffer by
120 // setting both fields before vsf_http_client_init(); otherwise
121 // init() allocates VSF_HTTP_CLIENT_CFG_BUFFER_SIZE bytes from the
122 // VSF heap and vsf_http_client_fini() releases it.
123 // buffer == NULL && buffer_size == 0 -> init allocates (CFG_BUFFER_SIZE)
124 // buffer == NULL && buffer_size > 0 -> init allocates buffer_size bytes
125 // buffer != NULL && buffer_size > 0 -> caller-owned, never freed by fini
126 uint8_t *buffer;
127 size_t buffer_size;
129 private_member(
130 size_t cur_size;
131 bool is_chunked;
132 int cur_chunk_size;
133 uint8_t *cur_buffer;
134 // true when init() allocated ->buffer from the heap; fini() will free it.
135 bool _buffer_owned;
136 )
137};
138
139/*============================ GLOBAL VARIABLES ==============================*/
140
141#if VSF_USE_MBEDTLS == ENABLED
142// to use mbedtls_http_op, user source code(.c) MUST include
143// "component/3rd-party/mbedtls/extension/tls_session/mbedtls_tls_session.h",
144// and set mbedtls_session_t instance to param of vsf_http_client_t.
145extern const vsf_http_op_t vsf_mbedtls_http_op;
146#endif
147
148/*============================ PROTOTYPES ====================================*/
149
151// Initialise an http client. Must be called on a zero-initialised handle.
152// See the buffer / buffer_size fields in vsf_http_client_t for buffer ownership.
153// Returns VSF_ERR_NONE on success, or a negative vsf_err_t on failure
154// (currently only VSF_ERR_NOT_ENOUGH_RESOURCES when the internal buffer malloc fails).
156// Release resources owned by init() (i.e. the heap-allocated buffer if any).
157// Does NOT close the transport -- call vsf_http_client_close() first if the
158// connection is still open. Safe to call on an init()-failed or already-fini'd handle.
159extern void vsf_http_client_fini(vsf_http_client_t *http);
161extern int vsf_http_client_read(vsf_http_client_t *http, uint8_t *buf, size_t len);
162extern int vsf_http_client_write(vsf_http_client_t *http, uint8_t *buf, size_t len);
163
164// Update the transport's read timeout (ms). Delegates to op->fn_set_timeout
165// when provided; returns VSF_ERR_NOT_SUPPORT if the port lacks this op.
166extern vsf_err_t vsf_http_client_set_timeout(vsf_http_client_t *http, int timeout_ms);
167// Return underlying socket fd for select()/poll(), or -1 when the port
168// does not expose one (see vsf_http_op_t::fn_get_fd for backend requirements).
170
171// ---- Stream-style sub-APIs ---------------------------------------------
172// vsf_http_client_request() is a composition of the APIs below. They are
173// exposed so that upper layers (e.g. esp_http_client compat shim) can drive
174// the transfer in discrete stages: connect -> send_header -> write(body) ->
175// fetch_headers -> read(body) -> close.
176
177// Establish underlying connection (TCP or TLS), thin wrapper on op->fn_connect.
178// Returns 0 on success, non-zero on failure (transport error code is passed through).
180 const char *host, const char *port);
181
182// Send request line + headers only (no body). Uses req->txdata_len as
183// Content-Length when > 0. Caller sends body afterwards via vsf_http_client_write.
184// req->header / req->connect_mode use defaults when NULL (same as request()).
185// Returns bytes written (> 0) on success, < 0 on failure.
188
189// Read & parse response headers, populate resp_status / content_length /
190// is_chunked / redirect_path; any body bytes already consumed are retained
191// in the internal buffer for subsequent vsf_http_client_read().
192// Returns 0 on success, <= 0 from underlying fn_read on failure, -1 on overflow.
195
196#ifdef __cplusplus
197}
198#endif
199
200#endif // __VSF_HTTP_CLIENT_H__
vsf_err_t
Definition __type.h:42
Definition vsf_http_client.h:110
#define vsf_class(__name)
Definition ooc_class.h:52
#define vsf_declare_class(__name)
Definition ooc_class.h:49
unsigned char uint8_t
Definition stdint.h:5
Definition vsf_http_client.h:94
const char * verb
Definition vsf_http_client.h:97
uint8_t * txdata
Definition vsf_http_client.h:103
char * header
Definition vsf_http_client.h:100
const char * host
Definition vsf_http_client.h:95
size_t txdata_len
Definition vsf_http_client.h:104
const char * port
Definition vsf_http_client.h:96
void(* on_response_header)(vsf_http_client_t *http, vsf_http_client_req_t *req, char *line)
Definition vsf_http_client.h:107
char * path
Definition vsf_http_client.h:101
const char * connect_mode
Definition vsf_http_client.h:98
int vsf_http_client_write(vsf_http_client_t *http, uint8_t *buf, size_t len)
Definition vsf_http_client.c:376
void vsf_http_client_close(vsf_http_client_t *http)
Definition vsf_http_client.c:127
int vsf_http_client_connect(vsf_http_client_t *http, const char *host, const char *port)
Definition vsf_http_client.c:132
void vsf_http_client_fini(vsf_http_client_t *http)
Definition vsf_http_client.c:117
vsf_err_t vsf_http_client_init(vsf_http_client_t *http)
Definition vsf_http_client.c:82
const vsf_http_op_t vsf_mbedtls_http_op
Definition vsf_http_client.c:52
int vsf_http_client_send_header(vsf_http_client_t *http, vsf_http_client_req_t *req)
Definition vsf_http_client.c:154
vsf_err_t vsf_http_client_set_timeout(vsf_http_client_t *http, int timeout_ms)
Definition vsf_http_client.c:137
int vsf_http_client_get_fd(vsf_http_client_t *http)
Definition vsf_http_client.c:146
int vsf_http_client_request(vsf_http_client_t *http, vsf_http_client_req_t *req)
Definition vsf_http_client.c:285
int vsf_http_client_fetch_headers(vsf_http_client_t *http, vsf_http_client_req_t *req)
Definition vsf_http_client.c:193
int vsf_http_client_read(vsf_http_client_t *http, uint8_t *buf, size_t len)
Definition vsf_http_client.c:312
Generated from commit: vsfteam/vsf@c3767bf