TuyaOS
httpc.h
浏览该文件的文档.
1
56/*
57 * Copyright 2008-2013, Marvell International Ltd.
58 * All Rights Reserved.
59 */
60
61#ifndef _HTTPC_H_
62#define _HTTPC_H_
63
64#include <time.h>
65#include "tal_time_service.h"
66#include "tal_network.h"
67#include "tuya_tls.h"
68
69typedef void * http_session_t;
70
71/* Request methods */
72typedef enum {
73 HTTP_OPTIONS, /* request to server for communication options */
74 HTTP_GET, /* retrieve information */
75 HTTP_HEAD, /* get meta-info */
76 HTTP_POST, /* request to accept new sub-ordinate of resource */
77 HTTP_PUT, /* modify or create new resource referred to by URI */
78 HTTP_DELETE, /* delete the resource */
79 HTTP_TRACE, /* echo */
80 HTTP_CONNECT, /* do we need this ? */
81} http_method_t;
82
83typedef enum {
84 HTTP_VER_1_0,
85 HTTP_VER_1_1,
86} http_ver_t;
87
88
95typedef enum {
96 HDR_ADD_DEFAULT_USER_AGENT = 0x0001,
97 /* Note: This flag is not necessary to set up persistent
98 * connections in HTTP 1.1. However, if you want the server to
99 * respond with persistent connection timeout values you may need
100 * to add this flag. These timeout values are used to find out how
101 * long a persistent connection will be kept alive by the
102 * server. */
103 HDR_ADD_CONN_KEEP_ALIVE = 0x0002,
104 HDR_ADD_CONN_CLOSE = 0x0004,
105 HDR_ADD_TYPE_CHUNKED = 0x0008,
106
107 // add content type
108 HDR_ADD_CONTENT_TYPE_JSON = 0x0010,
109 HDR_ADD_CONTENT_TYPE_FORM_URLENCODE = 0x0020, // add by nzy 20150608
110 HRD_ADD_DOWNLOAD_RANGE = 0x0040, /* add downlaod offset */
111 HRD_ADD_HTTP_RAW = 0x0080, /* add downlaod offset */
112
114
115
116#define STANDARD_HDR_FLAGS \
117 (HDR_ADD_DEFAULT_USER_AGENT)
118
119#define PARSE_EXTRA_BYTES 10
120
121// user http head add callback
122typedef void (*HTTP_HEAD_ADD_CB)(http_session_t session, VOID* data);
123
124
125typedef unsigned int (*HTTP_CUSTOM_GET_CONTENT_LEN_CB)(VOID *pri_data);
126typedef int (*HTTP_CUSTOM_BEFORE_READ_CONTENT_CB)(VOID *pri_data, unsigned int *p_malloc_buffer_size);
127typedef int (*HTTP_CUSTOM_READ_CONTENT_CB)(unsigned char *p_buffer, int buf_size, VOID *pri_data);
128typedef int (*HTTP_CUSTOM_AFTER_READ_CONTENT_CB)(VOID *pri_data);
129
130typedef struct {
131 HTTP_CUSTOM_GET_CONTENT_LEN_CB get_content_len_cb;
132 HTTP_CUSTOM_BEFORE_READ_CONTENT_CB before_read_cb;
133 HTTP_CUSTOM_READ_CONTENT_CB read_content_cb;
134 HTTP_CUSTOM_AFTER_READ_CONTENT_CB after_read_cb;
135 VOID *pri_data;
137
138
139void http_reset_session_state(http_session_t handle);
140
141/*
142 * Note 1: A resource is a part of the string immediately after the
143 * hostname[:portno] part of the URL. In the URL,
144 * [http://]hostname[:portno][/path/to/resource],
145 * "/path/to/resource" part is called as a resource. It starts with the
146 * character '/'.
147 */
157typedef struct {
159 http_method_t type;
163 const char *resource;
165 unsigned char redirect_cnt;
167 http_ver_t version;
169 const char *content;
174 HTTP_HEAD_ADD_CB add_head_cb;
175 VOID *add_head_data;
176 unsigned int download_offset;
177 unsigned int download_size;
178
179 http_custom_content_ctx_s *p_custom_content_ctx;
180} http_req_t;
181
185typedef struct {
188 const char *protocol;
190 http_ver_t version;
199 const char *reason_phrase;
201 const char *location;
203 const char *server;
205 const char *p_accept_ranges;
209 const char *content_type;
211 const char *content_encoding;
234 bool_t chunked;
238 unsigned int content_length;
240
241typedef struct {
242 char *name;
243 char *value;
245
246typedef struct {
247 char *scheme;
248 char *hostname;
249 unsigned portno;
250 char *resource;
252
256 WM_SUCCESS = 0,
257 WM_E_INVAL,
258 WM_FAIL,
259 WM_E_IO,
260 WM_E_AGAIN,
261 WM_E_BADF,
262 WM_E_NOMEM, // 6
278
279 /* add by nzy 20150803 timeout */
280 WM_E_HTTPC_SOCKET_TIMEOUT,//14
281
282 /* dns parse failed */
283 WM_E_HTTPC_DNS_PARSE_FAILED,
284 /* socket creat failed */
285 WM_E_HTTPC_SOCKET_CREAT_FAILED,
286 /* parse url failed*/
287 WM_E_HTTPC_URL_PARSE_FAILED,//17
288};
289
290
291/* Status codes */
292#define HTTP_RESP_INFORMATIONAL(x) (x >=100 && < 200)
293#define HTTP_RESP_SUCCESS(x) (x >= 200 && x < 300)
294#define HTTP_RESP_REDIR(x) (x >= 300 && x < 400)
295#define HTTP_RESP_CLIENT_ERR(x) (x >= 400 && x < 500)
296#define HTTP_RESP_SERVER_ERR(x) (x >= 500 && x < 600)
297
298/*
299 * These macros are not of any use to the HTTP client itself. They are used
300 * by the users of the HTTP client. This list may be extended if required
301 */
302#define HTTP_OK 200
303#define HTTP_CREATED 201
304#define HTTP_ACCEPTED 202
305#define HTTP_FOUND 302
306#define HTTP_NOT_MODIFIED 304
307
308#define HTTP_BAD_REQUEST 400
309#define HTTP_NOT_AUTH 401
310#define HTTP_FORBIDDEN 403
311#define HTTP_NOT_FOUND 404
312
313/* max redirect count */
314#define REDIRECT_CNT_MAX 5
315/* default redirect count */
316#define REDIRECT_CNT_DEFAULT 3
317/* zero means disable http redirect */
318#define REDIRECT_CNT_DISABLED 0
319
325typedef enum {
329
362int http_open_session(http_session_t *handle, const char *hostname, \
363 int flags, int retry_cnt);
364
365int http_open_session_with_config(http_session_t *handle, const char *hostname, \
366 tuya_tls_config_t* tls_config, int flags, int retry_cnt);
367
392int http_prepare_req(http_session_t handle, const http_req_t *req,
393 http_hdr_field_sel_t field_flags);
394
395
416int http_add_header(http_session_t handle, const http_req_t *req,
417 const char *name, const char *value);
418
439int http_send_request(http_session_t handle, const http_req_t * req, int send_content);
440
482int http_get_response_hdr(http_session_t handle, http_resp_t ** resp);
483
524int http_get_response_hdr_value(http_session_t handle,
525 const char *header_name, char **value);
571int http_get_response_hdr_all(http_session_t handle, http_header_pair_t *arr,
572 int *count);
573
608int http_read_content(http_session_t handle, void *buf, unsigned int max_len);
609
642int http_parse_URL(const char *URL, char *tmp_buf, int tmp_buf_len,
643 parsed_url_t *parsed_url);
671int http_lowlevel_read(http_session_t handle, void *buf, unsigned maxlen);
672
700int http_lowlevel_write(http_session_t handle, const void *buf, unsigned len);
701
702int http_write_standard(http_session_t handle, const void *buf, unsigned len);
703
722int httpc_write_chunked(http_session_t handle, const char *data, int len);
723
740void http_close_session(http_session_t *handle);
741
751BOOL_T http_is_session_closed(http_session_t *handle);
752
761OPERATE_RET http_redirect_limit_set(IN UINT8_T cnt);
762
778OPERATE_RET http_recv_timeout_set(IN UINT8_T timeout_s);
779
788
789#endif /* _HTTPC_H_ */
int http_get_response_hdr_all(http_session_t handle, http_header_pair_t *arr, int *count)
OPERATE_RET http_recv_timeout_set(IN UINT8_T timeout_s)
This API is used to SET HTTP recv timeout
int http_read_content(http_session_t handle, void *buf, unsigned int max_len)
UINT8_T http_recv_timeout_get(void)
This API is used to GET HTTP recv timeout
int http_get_response_hdr(http_session_t handle, http_resp_t **resp)
void http_close_session(http_session_t *handle)
int http_send_request(http_session_t handle, const http_req_t *req, int send_content)
int http_lowlevel_write(http_session_t handle, const void *buf, unsigned len)
UINT8_T http_redirect_limit_get(void)
This API is used to GET HTTP Redirect Limit Count
http_hdr_field_sel_t
Definition: httpc.h:95
OPERATE_RET http_redirect_limit_set(IN UINT8_T cnt)
This API is used to SET HTTP Redirect Limit Count
BOOL_T http_is_session_closed(http_session_t *handle)
int http_open_session(http_session_t *handle, const char *hostname, int flags, int retry_cnt)
wm_httpc_errno
Definition: httpc.h:255
@ WM_E_HTTPC_TCP_CONNECT_FAIL
Definition: httpc.h:264
@ WM_E_HTTPC_TCP_TLS_CONNECT_FAIL
Definition: httpc.h:266
@ WM_E_HTTPC_SOCKET_ERROR
Definition: httpc.h:275
@ WM_E_HTTPC_FILE_NOT_FOUND
Definition: httpc.h:268
@ WM_E_HTTPC_SOCKET_SHUTDOWN
Definition: httpc.h:277
@ WM_E_HTTPC_TLS_NOT_ENABLED
Definition: httpc.h:272
@ WM_E_HTTPC_BAD_REQUEST
Definition: httpc.h:270
int http_add_header(http_session_t handle, const http_req_t *req, const char *name, const char *value)
int http_get_response_hdr_value(http_session_t handle, const char *header_name, char **value)
http_open_flags_t
Definition: httpc.h:325
@ TLS_ENABLE
Definition: httpc.h:327
int http_parse_URL(const char *URL, char *tmp_buf, int tmp_buf_len, parsed_url_t *parsed_url)
int http_lowlevel_read(http_session_t handle, void *buf, unsigned maxlen)
int http_prepare_req(http_session_t handle, const http_req_t *req, http_hdr_field_sel_t field_flags)
int httpc_write_chunked(http_session_t handle, const char *data, int len)
Definition: httpc.h:130
Definition: httpc.h:241
Definition: httpc.h:157
http_ver_t version
Definition: httpc.h:167
int content_len
Definition: httpc.h:173
const char * content
Definition: httpc.h:169
const char * resource
Definition: httpc.h:163
unsigned char redirect_cnt
Definition: httpc.h:165
http_method_t type
Definition: httpc.h:159
Definition: httpc.h:185
const char * server
Definition: httpc.h:203
int keep_alive_timeout
Definition: httpc.h:222
const char * location
Definition: httpc.h:201
const char * content_type
Definition: httpc.h:209
http_ver_t version
Definition: httpc.h:190
const char * reason_phrase
Definition: httpc.h:199
int status_code
Definition: httpc.h:194
const char * protocol
Definition: httpc.h:188
bool_t keep_alive_ack
Definition: httpc.h:216
const char * content_encoding
Definition: httpc.h:211
unsigned int content_length
Definition: httpc.h:238
const char * p_accept_ranges
Definition: httpc.h:205
int keep_alive_max
Definition: httpc.h:229
bool_t chunked
Definition: httpc.h:234
time_t modify_time
Definition: httpc.h:207
Definition: httpc.h:246
Definition: tuya_tls.h:45
Common process - Initialization
tuya time service, support UTC time, local time and summer time
Common process - tls include