1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _INFRA_HTTPC_H_
6 #define _INFRA_HTTPC_H_
7 
8 #include "linkkit/infra/infra_net.h"
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /**
15  * @addtogroup HttpClient
16  * @{
17  * HttpClient API implements the client-side of HTTP/1.1. It provides base
18  * interfaces to execute an HTTP request on a given URL. It also supports HTTPS
19  * (HTTP over TLS) to provide secure communication.\n
20  * @section HttpClient_Usage_Chapter How to use this module
21  * In this release, MediaTek provides two types of APIs: high level APIs and low
22  * level APIs.\n
23  * - \b The \b high \b level \b APIs
24  *  - Enables to execute a single HTTP request on a given URL.
25  *  - Call #httpclient_get(), #httpclient_post(), #httpclient_put() or
26  * #httpclient_delete() to get, post, put or delete and HTTP request.\n
27  * - \b The \b low \b level \b APIs
28  *  - Enables to execute more than one HTTP requests during a Keep-Alive
29  * connection. Keep-alive is the idea of using a single TCP connection to send
30  * and receive multiple HTTP requests/responses, as opposed to opening a new
31  * connection for every single request/response pair.
32  *  - Step1: Call #httpclient_connect() to connect to a remote server.
33  *  - Step2: Call #httpclient_send_request() to send an HTTP request to the
34  * server.
35  *  - Step3: Call #httpclient_recv_response() to receive an HTTP response from
36  * the server.
37  *  - Step4: Repeat Steps 2 and 3 to execute more requests.
38  *  - Step5: Call #httpclient_close() to close the connection.
39  *  - Sample code: Please refer to the example under
40  * <sdk_root>/project/mt7687_hdk/apps/http_client/http_client_keepalive folder.
41  */
42 
43 /** @defgroup httpclient_define Define
44  * @{
45  */
46 /** @brief   This macro defines the HTTP port.  */
47 #define HTTP_PORT  80
48 
49 /** @brief   This macro defines the HTTPS port.  */
50 #define HTTPS_PORT 443
51 /**
52  * @}
53  */
54 
55 /** @defgroup httpclient_enum Enum
56  *  @{
57  */
58 /** @brief   This enumeration defines the HTTP request type.  */
59 typedef enum {
60     HTTPCLIENT_GET,
61     HTTPCLIENT_POST,
62     HTTPCLIENT_PUT,
63     HTTPCLIENT_DELETE,
64     HTTPCLIENT_HEAD
65 } HTTPCLIENT_REQUEST_TYPE;
66 
67 /** @defgroup httpclient_struct Struct
68  * @{
69  */
70 /** @brief   This structure defines the httpclient_t structure.  */
71 typedef struct {
72     int remote_port; /**< HTTP or HTTPS port. */
73     utils_network_t net;
74     int response_code;   /**< Response code. */
75     char *header;        /**< Custom header. */
76     char *auth_user;     /**< Username for basic authentication. */
77     char *auth_password; /**< Password for basic authentication. */
78 } httpclient_t;
79 
80 /** @brief   This structure defines the HTTP data structure.  */
81 typedef struct {
82     int is_more;      /**< Indicates if more data needs to be retrieved. */
83     int is_chunked;   /**< Response data is encoded in portions/chunks.*/
84     int retrieve_len; /**< Content length to be retrieved. */
85     int response_content_len;  /**< Response content length. */
86     int response_received_len; /**< Response have received length. */
87     int post_buf_len;          /**< Post data length. */
88     int response_buf_len;      /**< Response buffer length. */
89     char *post_content_type;   /**< Content type of the post data. */
90     char *post_buf;            /**< User data to be posted. */
91     char *response_buf;        /**< Buffer to store the response data. */
92 } httpclient_data_t;
93 
94 int iotx_post(httpclient_t *client, const char *url, int port,
95               const char *ca_crt, httpclient_data_t *client_data);
96 
97 int httpclient_recv_response(httpclient_t *client, uint32_t timeout_ms,
98                              httpclient_data_t *client_data);
99 
100 int httpclient_common(httpclient_t *client, const char *url, int port,
101                       const char *ca_crt, HTTPCLIENT_REQUEST_TYPE method,
102                       uint32_t timeout_ms, httpclient_data_t *client_data);
103 
104 void httpclient_close(httpclient_t *client);
105 
106 /**********************************************************************************/
107 
108 typedef enum {
109     IOTX_HTTP_GET,
110     IOTX_HTTP_POST,
111     IOTX_HTTP_PUT,
112     IOTX_HTTP_DELETE
113 } iotx_http_method_t;
114 
115 typedef enum {
116     IOTX_HTTPOPT_URL,
117     IOTX_HTTPOPT_PORT,
118     IOTX_HTTPOPT_METHOD,
119     IOTX_HTTPOPT_HEADER,
120     IOTX_HTTPOPT_CERT,
121     IOTX_HTTPOPT_TIMEOUT,
122     IOTX_HTTPOPT_RECVCALLBACK,
123     IOTX_HTTPOPT_RECVMAXLEN,
124     IOTX_HTTPOPT_RECVCONTEXT
125 } iotx_http_option_t;
126 
127 typedef int (*recvcallback)(char *ptr, int length, int total_length,
128                             void *userdata);
129 
130 void *wrapper_http_init(void);
131 int wrapper_http_setopt(void *handle, iotx_http_option_t option, void *data);
132 int wrapper_http_perform(void *handle, void *data, int length);
133 void wrapper_http_deinit(void **handle);
134 /**********************************************************************************/
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* __HTTPCLIENT_H__ */
140