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 "tuya_hal_network.h"
67
68typedef void * http_session_t;
69
70/* Request methods */
71typedef enum {
72 HTTP_OPTIONS, /* request to server for communication options */
73 HTTP_GET, /* retrieve information */
74 HTTP_HEAD, /* get meta-info */
75 HTTP_POST, /* request to accept new sub-ordinate of resource */
76 HTTP_PUT, /* modify or create new resource referred to by URI */
77 HTTP_DELETE, /* delete the resource */
78 HTTP_TRACE, /* echo */
79 HTTP_CONNECT, /* do we need this ? */
80} http_method_t;
81
82typedef enum {
83 HTTP_VER_1_0,
84 HTTP_VER_1_1,
85} http_ver_t;
86
87
94typedef enum {
95 HDR_ADD_DEFAULT_USER_AGENT = 0x0001,
96 /* Note: This flag is not necessary to set up persistent
97 * connections in HTTP 1.1. However, if you want the server to
98 * respond with persistent connection timeout values you may need
99 * to add this flag. These timeout values are used to find out how
100 * long a persistent connection will be kept alive by the
101 * server. */
102 HDR_ADD_CONN_KEEP_ALIVE = 0x0002,
103 HDR_ADD_CONN_CLOSE = 0x0004,
104 HDR_ADD_TYPE_CHUNKED = 0x0008,
105
106 // add content type
107 HDR_ADD_CONTENT_TYPE_JSON = 0x0010,
108 HDR_ADD_CONTENT_TYPE_FORM_URLENCODE = 0x0020, // add by nzy 20150608
109 HRD_ADD_DOWNLOAD_RANGE = 0x0040, /* add downlaod offset */
110 HRD_ADD_HTTP_RAW = 0x0080, /* add downlaod offset */
111
113
114
115#define STANDARD_HDR_FLAGS \
116 (HDR_ADD_DEFAULT_USER_AGENT)
117
118// user http head add callback
119typedef void (*HTTP_HEAD_ADD_CB)(http_session_t session, VOID* data);
120
121
122typedef unsigned int (*HTTP_CUSTOM_GET_CONTENT_LEN_CB)(VOID *pri_data);
123typedef int (*HTTP_CUSTOM_BEFORE_READ_CONTENT_CB)(VOID *pri_data, unsigned int *p_malloc_buffer_size);
124typedef int (*HTTP_CUSTOM_READ_CONTENT_CB)(unsigned char *p_buffer, int buf_size, VOID *pri_data);
125typedef int (*HTTP_CUSTOM_AFTER_READ_CONTENT_CB)(VOID *pri_data);
126
127typedef struct {
128 HTTP_CUSTOM_GET_CONTENT_LEN_CB get_content_len_cb;
129 HTTP_CUSTOM_BEFORE_READ_CONTENT_CB before_read_cb;
130 HTTP_CUSTOM_READ_CONTENT_CB read_content_cb;
131 HTTP_CUSTOM_AFTER_READ_CONTENT_CB after_read_cb;
132 VOID *pri_data;
134
135
136void http_reset_session_state(http_session_t handle);
137
138/*
139 * Note 1: A resource is a part of the string immediately after the
140 * hostname[:portno] part of the URL. In the URL,
141 * [http://]hostname[:portno][/path/to/resource],
142 * "/path/to/resource" part is called as a resource. It starts with the
143 * character '/'.
144 */
154typedef struct {
156 http_method_t type;
160 const char *resource;
162 unsigned char redirect_cnt;
164 http_ver_t version;
166 const char *content;
171 HTTP_HEAD_ADD_CB add_head_cb;
172 VOID *add_head_data;
173 unsigned int download_offset;
174 unsigned int download_size;
175
176 http_custom_content_ctx_s *p_custom_content_ctx;
177} http_req_t;
178
182typedef struct {
185 const char *protocol;
187 http_ver_t version;
196 const char *reason_phrase;
198 const char *location;
200 const char *server;
202 const char *p_accept_ranges;
206 const char *content_type;
208 const char *content_encoding;
231 bool_t chunked;
235 unsigned int content_length;
237
238typedef struct {
239 char *name;
240 char *value;
242
243typedef struct {
244 const char *scheme;
245 const char *hostname;
246 unsigned portno;
247 const char *resource;
249
253 WM_SUCCESS = 0,
254 WM_E_INVAL,
255 WM_FAIL,
256 WM_E_IO,
257 WM_E_AGAIN,
258 WM_E_BADF,
259 WM_E_NOMEM, // 6
275
276 /* add by nzy 20150803 timeout */
277 WM_E_HTTPC_SOCKET_TIMEOUT,//14
278
279 /* dns parse failed */
280 WM_E_HTTPC_DNS_PARSE_FAILED,
281 /* socket creat failed */
282 WM_E_HTTPC_SOCKET_CREAT_FAILED,
283 /* parse url failed*/
284 WM_E_HTTPC_URL_PARSE_FAILED,//17
285};
286
287
288/* Status codes */
289#define HTTP_RESP_INFORMATIONAL(x) (x >=100 && < 200)
290#define HTTP_RESP_SUCCESS(x) (x >= 200 && x < 300)
291#define HTTP_RESP_REDIR(x) (x >= 300 && x < 400)
292#define HTTP_RESP_CLIENT_ERR(x) (x >= 400 && x < 500)
293#define HTTP_RESP_SERVER_ERR(x) (x >= 500 && x < 600)
294
295/*
296 * These macros are not of any use to the HTTP client itself. They are used
297 * by the users of the HTTP client. This list may be extended if required
298 */
299#define HTTP_OK 200
300#define HTTP_CREATED 201
301#define HTTP_ACCEPTED 202
302#define HTTP_FOUND 302
303#define HTTP_NOT_MODIFIED 304
304
305#define HTTP_BAD_REQUEST 400
306#define HTTP_NOT_AUTH 401
307#define HTTP_FORBIDDEN 403
308#define HTTP_NOT_FOUND 404
309
310/* max redirect count */
311#define REDIRECT_CNT_MAX 5
312/* default redirect count */
313#define REDIRECT_CNT_DEFAULT 3
314/* zero means disable http redirect */
315#define REDIRECT_CNT_DISABLED 0
316
322typedef enum {
326
359int http_open_session(http_session_t *handle, const char *hostname, \
360 int flags, int retry_cnt);
361
386int http_prepare_req(http_session_t handle, const http_req_t *req,
387 http_hdr_field_sel_t field_flags);
388
389
410int http_add_header(http_session_t handle, const http_req_t *req,
411 const char *name, const char *value);
412
433int http_send_request(http_session_t handle, const http_req_t * req, int send_content);
434
476int http_get_response_hdr(http_session_t handle, http_resp_t ** resp);
477
518int http_get_response_hdr_value(http_session_t handle,
519 const char *header_name, char **value);
565int http_get_response_hdr_all(http_session_t handle, http_header_pair_t *arr,
566 int *count);
567
602int http_read_content(http_session_t handle, void *buf, unsigned int max_len);
603
636int http_parse_URL(const char *URL, char *tmp_buf, int tmp_buf_len,
637 parsed_url_t *parsed_url);
665int http_lowlevel_read(http_session_t handle, void *buf, unsigned maxlen);
666
694int http_lowlevel_write(http_session_t handle, const void *buf, unsigned len);
695
696int http_write_standard(http_session_t handle, const void *buf, unsigned len);
697
716int httpc_write_chunked(http_session_t handle, const char *data, int len);
717
734void http_close_session(http_session_t *handle);
735
744OPERATE_RET http_redirect_limit_set(IN UINT8_T cnt);
745
754
755#endif /* _HTTPC_H_ */
int http_get_response_hdr_all(http_session_t handle, http_header_pair_t *arr, int *count)
int http_read_content(http_session_t handle, void *buf, unsigned int max_len)
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:94
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:252
@ WM_E_HTTPC_TCP_CONNECT_FAIL
Definition: httpc.h:261
@ WM_E_HTTPC_TCP_TLS_CONNECT_FAIL
Definition: httpc.h:263
@ WM_E_HTTPC_SOCKET_ERROR
Definition: httpc.h:272
@ WM_E_HTTPC_FILE_NOT_FOUND
Definition: httpc.h:265
@ WM_E_HTTPC_SOCKET_SHUTDOWN
Definition: httpc.h:274
@ WM_E_HTTPC_TLS_NOT_ENABLED
Definition: httpc.h:269
@ WM_E_HTTPC_BAD_REQUEST
Definition: httpc.h:267
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:322
@ TLS_ENABLE
Definition: httpc.h:324
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:127
Definition: httpc.h:238
Definition: httpc.h:154
http_ver_t version
Definition: httpc.h:164
int content_len
Definition: httpc.h:170
const char * content
Definition: httpc.h:166
const char * resource
Definition: httpc.h:160
unsigned char redirect_cnt
Definition: httpc.h:162
http_method_t type
Definition: httpc.h:156
Definition: httpc.h:182
const char * server
Definition: httpc.h:200
int keep_alive_timeout
Definition: httpc.h:219
const char * location
Definition: httpc.h:198
const char * content_type
Definition: httpc.h:206
http_ver_t version
Definition: httpc.h:187
const char * reason_phrase
Definition: httpc.h:196
int status_code
Definition: httpc.h:191
const char * protocol
Definition: httpc.h:185
bool_t keep_alive_ack
Definition: httpc.h:213
const char * content_encoding
Definition: httpc.h:208
unsigned int content_length
Definition: httpc.h:235
const char * p_accept_ranges
Definition: httpc.h:202
int keep_alive_max
Definition: httpc.h:226
bool_t chunked
Definition: httpc.h:231
time_t modify_time
Definition: httpc.h:204
Definition: httpc.h:243
tuya time service, support UTC time, local time and summer time
Common process - Initialization