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// user http head add callback
120typedef void (*HTTP_HEAD_ADD_CB)(http_session_t session, VOID* data);
121
122
123typedef unsigned int (*HTTP_CUSTOM_GET_CONTENT_LEN_CB)(VOID *pri_data);
124typedef int (*HTTP_CUSTOM_BEFORE_READ_CONTENT_CB)(VOID *pri_data, unsigned int *p_malloc_buffer_size);
125typedef int (*HTTP_CUSTOM_READ_CONTENT_CB)(unsigned char *p_buffer, int buf_size, VOID *pri_data);
126typedef int (*HTTP_CUSTOM_AFTER_READ_CONTENT_CB)(VOID *pri_data);
127
128typedef struct {
129 HTTP_CUSTOM_GET_CONTENT_LEN_CB get_content_len_cb;
130 HTTP_CUSTOM_BEFORE_READ_CONTENT_CB before_read_cb;
131 HTTP_CUSTOM_READ_CONTENT_CB read_content_cb;
132 HTTP_CUSTOM_AFTER_READ_CONTENT_CB after_read_cb;
133 VOID *pri_data;
135
136
137void http_reset_session_state(http_session_t handle);
138
139/*
140 * Note 1: A resource is a part of the string immediately after the
141 * hostname[:portno] part of the URL. In the URL,
142 * [http://]hostname[:portno][/path/to/resource],
143 * "/path/to/resource" part is called as a resource. It starts with the
144 * character '/'.
145 */
155typedef struct {
157 http_method_t type;
161 const char *resource;
163 unsigned char redirect_cnt;
165 http_ver_t version;
167 const char *content;
172 HTTP_HEAD_ADD_CB add_head_cb;
173 VOID *add_head_data;
174 unsigned int download_offset;
175 unsigned int download_size;
176
177 http_custom_content_ctx_s *p_custom_content_ctx;
178} http_req_t;
179
183typedef struct {
186 const char *protocol;
188 http_ver_t version;
197 const char *reason_phrase;
199 const char *location;
201 const char *server;
203 const char *p_accept_ranges;
207 const char *content_type;
209 const char *content_encoding;
232 bool_t chunked;
236 unsigned int content_length;
238
239typedef struct {
240 char *name;
241 char *value;
243
244typedef struct {
245 char *scheme;
246 char *hostname;
247 unsigned portno;
248 char *resource;
250
254 WM_SUCCESS = 0,
255 WM_E_INVAL,
256 WM_FAIL,
257 WM_E_IO,
258 WM_E_AGAIN,
259 WM_E_BADF,
260 WM_E_NOMEM, // 6
276
277 /* add by nzy 20150803 timeout */
278 WM_E_HTTPC_SOCKET_TIMEOUT,//14
279
280 /* dns parse failed */
281 WM_E_HTTPC_DNS_PARSE_FAILED,
282 /* socket creat failed */
283 WM_E_HTTPC_SOCKET_CREAT_FAILED,
284 /* parse url failed*/
285 WM_E_HTTPC_URL_PARSE_FAILED,//17
286};
287
288
289/* Status codes */
290#define HTTP_RESP_INFORMATIONAL(x) (x >=100 && < 200)
291#define HTTP_RESP_SUCCESS(x) (x >= 200 && x < 300)
292#define HTTP_RESP_REDIR(x) (x >= 300 && x < 400)
293#define HTTP_RESP_CLIENT_ERR(x) (x >= 400 && x < 500)
294#define HTTP_RESP_SERVER_ERR(x) (x >= 500 && x < 600)
295
296/*
297 * These macros are not of any use to the HTTP client itself. They are used
298 * by the users of the HTTP client. This list may be extended if required
299 */
300#define HTTP_OK 200
301#define HTTP_CREATED 201
302#define HTTP_ACCEPTED 202
303#define HTTP_FOUND 302
304#define HTTP_NOT_MODIFIED 304
305
306#define HTTP_BAD_REQUEST 400
307#define HTTP_NOT_AUTH 401
308#define HTTP_FORBIDDEN 403
309#define HTTP_NOT_FOUND 404
310
311/* max redirect count */
312#define REDIRECT_CNT_MAX 5
313/* default redirect count */
314#define REDIRECT_CNT_DEFAULT 3
315/* zero means disable http redirect */
316#define REDIRECT_CNT_DISABLED 0
317
323typedef enum {
327
360int http_open_session(http_session_t *handle, const char *hostname, \
361 int flags, int retry_cnt);
362
363int http_open_session_with_config(http_session_t *handle, const char *hostname, \
364 tuya_tls_config_t* tls_config, int flags, int retry_cnt);
365
390int http_prepare_req(http_session_t handle, const http_req_t *req,
391 http_hdr_field_sel_t field_flags);
392
393
414int http_add_header(http_session_t handle, const http_req_t *req,
415 const char *name, const char *value);
416
437int http_send_request(http_session_t handle, const http_req_t * req, int send_content);
438
480int http_get_response_hdr(http_session_t handle, http_resp_t ** resp);
481
522int http_get_response_hdr_value(http_session_t handle,
523 const char *header_name, char **value);
569int http_get_response_hdr_all(http_session_t handle, http_header_pair_t *arr,
570 int *count);
571
606int http_read_content(http_session_t handle, void *buf, unsigned int max_len);
607
640int http_parse_URL(const char *URL, char *tmp_buf, int tmp_buf_len,
641 parsed_url_t *parsed_url);
669int http_lowlevel_read(http_session_t handle, void *buf, unsigned maxlen);
670
698int http_lowlevel_write(http_session_t handle, const void *buf, unsigned len);
699
700int http_write_standard(http_session_t handle, const void *buf, unsigned len);
701
720int httpc_write_chunked(http_session_t handle, const char *data, int len);
721
738void http_close_session(http_session_t *handle);
739
748OPERATE_RET http_redirect_limit_set(IN UINT8_T cnt);
749
765OPERATE_RET http_recv_timeout_set(IN UINT8_T timeout_s);
766
775
776#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
int http_open_session(http_session_t *handle, const char *hostname, int flags, int retry_cnt)
wm_httpc_errno
Definition: httpc.h:253
@ WM_E_HTTPC_TCP_CONNECT_FAIL
Definition: httpc.h:262
@ WM_E_HTTPC_TCP_TLS_CONNECT_FAIL
Definition: httpc.h:264
@ WM_E_HTTPC_SOCKET_ERROR
Definition: httpc.h:273
@ WM_E_HTTPC_FILE_NOT_FOUND
Definition: httpc.h:266
@ WM_E_HTTPC_SOCKET_SHUTDOWN
Definition: httpc.h:275
@ WM_E_HTTPC_TLS_NOT_ENABLED
Definition: httpc.h:270
@ WM_E_HTTPC_BAD_REQUEST
Definition: httpc.h:268
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:323
@ TLS_ENABLE
Definition: httpc.h:325
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:128
Definition: httpc.h:239
Definition: httpc.h:155
http_ver_t version
Definition: httpc.h:165
int content_len
Definition: httpc.h:171
const char * content
Definition: httpc.h:167
const char * resource
Definition: httpc.h:161
unsigned char redirect_cnt
Definition: httpc.h:163
http_method_t type
Definition: httpc.h:157
Definition: httpc.h:183
const char * server
Definition: httpc.h:201
int keep_alive_timeout
Definition: httpc.h:220
const char * location
Definition: httpc.h:199
const char * content_type
Definition: httpc.h:207
http_ver_t version
Definition: httpc.h:188
const char * reason_phrase
Definition: httpc.h:197
int status_code
Definition: httpc.h:192
const char * protocol
Definition: httpc.h:186
bool_t keep_alive_ack
Definition: httpc.h:214
const char * content_encoding
Definition: httpc.h:209
unsigned int content_length
Definition: httpc.h:236
const char * p_accept_ranges
Definition: httpc.h:203
int keep_alive_max
Definition: httpc.h:227
bool_t chunked
Definition: httpc.h:232
time_t modify_time
Definition: httpc.h:205
Definition: httpc.h:244
Definition: tuya_tls.h:45
Common process - Initialization
tuya time service, support UTC time, local time and summer time
Common process - tls include