Internet Middleware API Reference  Rev 1.0
httpclient.h
1 /* Copyright (C) 2012 mbed.org, MIT License
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4  * and associated documentation files (the "Software"), to deal in the Software without restriction,
5  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
6  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in all copies or
10  * substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17  */
18 
19 #ifndef __HTTPCLIENT_H__
20 #define __HTTPCLIENT_H__
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "lwip/sockets.h"
25 
26 #ifdef MTK_HTTPCLIENT_SSL_ENABLE
27 //#include "mbedtls/compat-1.3.h"
28 #include "mbedtls/net.h"
29 #include "mbedtls/ssl.h"
30 #include "mbedtls/certs.h"
31 #include "mbedtls/entropy.h"
32 #include "mbedtls/ctr_drbg.h"
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
63 #define HTTP_PORT 80
64 
66 #define HTTPS_PORT 443
67 
76 typedef enum {
77  HTTPCLIENT_GET,
78  HTTPCLIENT_POST,
79  HTTPCLIENT_PUT,
80  HTTPCLIENT_DELETE,
81  HTTPCLIENT_HEAD
83 
85 typedef enum {
103 typedef struct {
104  int socket;
107  char *header;
108  char *auth_user;
110  bool is_http;
111 #ifdef MTK_HTTPCLIENT_SSL_ENABLE
112  const char *server_cert;
113  const char *client_cert;
114  const char *client_pk;
115  int server_cert_len;
116  int client_cert_len;
117  int client_pk_len;
118  void *ssl;
119 #endif
120 } httpclient_t;
121 
123 typedef struct {
124  bool is_more;
125  bool is_chunked;
132  char *post_buf;
133  char *response_buf;
134  char *header_buf;
136 
165 HTTPCLIENT_RESULT httpclient_get(httpclient_t *client, char *url, httpclient_data_t *client_data);
166 
167 
199 HTTPCLIENT_RESULT httpclient_post(httpclient_t *client, char *url, httpclient_data_t *client_data);
200 
230 HTTPCLIENT_RESULT httpclient_put(httpclient_t *client, char *url, httpclient_data_t *client_data);
231 
256 HTTPCLIENT_RESULT httpclient_delete(httpclient_t *client, char *url, httpclient_data_t *client_data);
257 
265 
274 HTTPCLIENT_RESULT httpclient_send_request(httpclient_t *client, char *url, int method, httpclient_data_t *client_data);
275 
283 
288 void httpclient_close(httpclient_t *client);
289 
296 
303 void httpclient_set_custom_header(httpclient_t *client, char *header);
304 
313 int httpclient_get_response_header_value(char *header_buf, char *name, int *val_pos, int *val_len);
314 
319 /* The following content is used in the HttpClient module. */
320 #ifndef MTK_DEBUG_LEVEL_NONE
321 #define HTTPCLIENT_DEBUG 1
322 #else
323 #define HTTPCLIENT_DEBUG 0
324 #endif
325 
326 #ifdef MTK_HTTPCLIENT_SSL_ENABLE
327 typedef struct {
328  mbedtls_ssl_context ssl_ctx; /* mbedtls ssl context */
329  mbedtls_net_context net_ctx; /* Fill in socket id */
330  mbedtls_ssl_config ssl_conf; /* SSL configuration */
331  mbedtls_entropy_context entropy;
332  mbedtls_ctr_drbg_context ctr_drbg;
333  mbedtls_x509_crt_profile profile;
334  mbedtls_x509_crt cacert;
335  mbedtls_x509_crt clicert;
336  mbedtls_pk_context pkey;
337 } httpclient_ssl_t;
338 #endif
339 
340 #ifdef __cplusplus
341 }
342 #endif
343 
344 #endif /* __HTTPCLIENT_H__ */
345 
346 
int socket
Socket ID.
Definition: httpclient.h:104
int response_code
Response code.
Definition: httpclient.h:106
Connection was closed by a remote host.
Definition: httpclient.h:90
void httpclient_close(httpclient_t *client)
This function closes the HTTP connection.
HTTPCLIENT_RESULT httpclient_post(httpclient_t *client, char *url, httpclient_data_t *client_data)
This function executes a POST request on a given URL.
HTTPCLIENT_RESULT httpclient_send_request(httpclient_t *client, char *url, int method, httpclient_data_t *client_data)
This function sends an HTTP(GET or POST) request to the given URL.
bool is_chunked
Response data is encoded in portions/chunks.
Definition: httpclient.h:125
This structure defines the HTTP data structure.
Definition: httpclient.h:123
Could not resolve the hostname.
Definition: httpclient.h:87
char * post_buf
User data to be posted.
Definition: httpclient.h:132
char * header
Request custom header.
Definition: httpclient.h:107
HTTPCLIENT_RESULT httpclient_get(httpclient_t *client, char *url, httpclient_data_t *client_data)
This function executes a GET request on a given URL.
A URL parse error occurred.
Definition: httpclient.h:86
HTTPCLIENT_RESULT httpclient_delete(httpclient_t *client, char *url, httpclient_data_t *client_data)
This function executes a DELETE request on a given URL.
More data needs to be retrieved.
Definition: httpclient.h:93
char * auth_password
Password for basic authentication.
Definition: httpclient.h:109
int retrieve_len
Content length to be retrieved.
Definition: httpclient.h:126
void httpclient_set_custom_header(httpclient_t *client, char *header)
This function sets a custom header.
int httpclient_get_response_header_value(char *header_buf, char *name, int *val_pos, int *val_len)
This function get specified response header value.
bool is_more
Indicates if more data needs to be retrieved.
Definition: httpclient.h:124
int remote_port
HTTP or HTTPS port.
Definition: httpclient.h:105
The operation was successful.
Definition: httpclient.h:92
int httpclient_get_response_code(httpclient_t *client)
This function gets the HTTP response code assigned to the last request.
HTTPCLIENT_RESULT
This enumeration defines the API return type.
Definition: httpclient.h:85
int response_content_len
Response content length.
Definition: httpclient.h:127
Connection failed.
Definition: httpclient.h:91
char * header_buf
Buffer to store the response head data.
Definition: httpclient.h:134
int response_buf_len
Response body buffer length.
Definition: httpclient.h:129
An unknown error occurred.
Definition: httpclient.h:89
This structure defines the httpclient_t structure.
Definition: httpclient.h:103
A protocol error occurred.
Definition: httpclient.h:88
bool is_http
Http connection? if 1, http; if 0, https.
Definition: httpclient.h:110
char * auth_user
Username for basic authentication.
Definition: httpclient.h:108
int header_buf_len
Response head buffer lehgth.
Definition: httpclient.h:130
HTTPCLIENT_REQUEST_TYPE
This enumeration defines the HTTP request type.
Definition: httpclient.h:76
char * post_content_type
Content type of the post data.
Definition: httpclient.h:131
HTTPCLIENT_RESULT httpclient_recv_response(httpclient_t *client, httpclient_data_t *client_data)
This function receives the response from a server for the last request.
int post_buf_len
Post data length.
Definition: httpclient.h:128
HTTPCLIENT_RESULT httpclient_put(httpclient_t *client, char *url, httpclient_data_t *client_data)
This function executes a PUT request on a given URL.
char * response_buf
Buffer to store the response body data.
Definition: httpclient.h:133
HTTPCLIENT_RESULT httpclient_connect(httpclient_t *client, char *url)
This function establishes an HTTP connection with the remote server by given URL. ...