Add esp_http_client

Add error handling for http client

set ssid password correct with Example_WIFI test, and clear password before free

Fixed the CI failure due to HTTP errror names
This commit is contained in:
Tuan PM
2017-11-14 10:16:20 +07:00
parent 9c53b599b2
commit ff528d13c7
30 changed files with 3915 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _HTTP_BASIC_AUTH_H_
#define _HTTP_BASIC_AUTH_H_
/**
* HTTP Digest authentication data
*/
typedef struct {
char *method; /*!< Request method, example: GET */
char *algorithm; /*!< Authentication algorithm */
char *uri; /*!< URI of request example: /path/to/file.html */
char *realm; /*!< Authentication realm */
char *nonce; /*!< Authentication nonce */
char *qop; /*!< Authentication qop */
char *opaque; /*!< Authentication opaque */
uint64_t cnonce; /*!< Authentication cnonce */
int nc; /*!< Authentication nc */
} esp_http_auth_data_t;
/**
* @brief This use for Http digest authentication method, create http header for digest authentication.
* The returned string need to free after use
*
* @param[in] username The username
* @param[in] password The password
* @param auth_data The auth data
*
* @return
* - HTTP Header value of Authorization, valid for digest authentication, must be freed after usage
* - NULL
*/
char *http_auth_digest(const char *username, const char *password, esp_http_auth_data_t *auth_data);
/**
* @brief This use for Http basic authentication method, create header value for basic http authentication
* The returned string need to free after use
*
* @param[in] username The username
* @param[in] password The password
*
* @return
* - HTTP Header value of Authorization, valid for basic authentication, must be free after use
* - NULL
*/
char *http_auth_basic(const char *username, const char *password);
#endif

View File

@@ -0,0 +1,128 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _HTTP_HEADER_H_
#define _HTTP_HEADER_H_
#include "rom/queue.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct http_header *http_header_handle_t;
typedef struct http_header_item *http_header_item_handle_t;
/**
* @brief initialize and allocate the memory for the header object
*
* @return
* - http_header_handle_t
* - NULL if any errors
*/
http_header_handle_t http_header_init();
/**
* @brief Cleanup and free all http header pairs
*
* @param[in] header The header
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t http_header_clean(http_header_handle_t header);
/**
* @brief Cleanup with http_header_clean and destroy http header handle object
*
* @param[in] header The header
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t http_header_destroy(http_header_handle_t header);
/**
* @brief Add a key-value pair of http header to the list,
* note that with value = NULL, this function will remove the header with `key` already exists in the list.
*
* @param[in] header The header
* @param[in] key The key
* @param[in] value The value
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value);
/**
* @brief Sample as `http_header_set` but the value can be formated
*
* @param[in] header The header
* @param[in] key The key
* @param[in] format The format
* @param[in] ... format parameters
*
* @return Total length of value
*/
int http_header_set_format(http_header_handle_t header, const char *key, const char *format, ...);
/**
* @brief Get a value of header in header list
* The address of the value will be assign set to `value` parameter or NULL if no header with the key exists in the list
*
* @param[in] header The header
* @param[in] key The key
* @param[out] value The value
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t http_header_get(http_header_handle_t header, const char *key, char **value);
/**
* @brief Create HTTP header string from the header with index, output string to buffer with buffer_len
* Also return the last index of header was generated
*
* @param[in] header The header
* @param[in] index The index
* @param buffer The buffer
* @param buffer_len The buffer length
*
* @return The last index of header was generated
*/
int http_header_generate_string(http_header_handle_t header, int index, char *buffer, int *buffer_len);
/**
* @brief Remove the header with key from the headers list
*
* @param[in] header The header
* @param[in] key The key
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t http_header_delete(http_header_handle_t header, const char *key);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,95 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _HTTP_UTILS_H_
#define _HTTP_UTILS_H_
#include <sys/time.h>
/**
* @brief Assign new_str to *str pointer, and realloc *str if it not NULL
*
* @param str pointer to string pointer
* @param new_str assign this tring to str
* @param len length of string, 0 if new_str is zero terminated
*
* @return
* - new_str pointer
* - NULL
*/
char *http_utils_assign_string(char **str, const char *new_str, int len);
/**
* @brief Remove white space at begin and end of string
*
* @param[in] str The string
*
* @return New strings have been trimmed
*/
void http_utils_trim_whitespace(char **str);
/**
* @brief Gets the string between 2 string.
* It will allocate a new memory space for this string, so you need to free it when no longer use
*
* @param[in] str The source string
* @param[in] begin The begin string
* @param[in] end The end string
*
* @return The string between begin and end
*/
char *http_utils_get_string_between(const char *str, const char *begin, const char *end);
/**
* @brief Join 2 strings to one
* It will allocate a new memory space for this string, so you need to free it when no longer use
*
* @param[in] first_str The first string
* @param[in] len_first The length first
* @param[in] second_str The second string
* @param[in] len_second The length second
*
* @return
* - New string pointer
* - NULL: Invalid input
*/
char *http_utils_join_string(const char *first_str, int len_first, const char *second_str, int len_second);
/**
* @brief Check if ``str`` is start with ``start``
*
* @param[in] str The string
* @param[in] start The start
*
* @return
* - (-1) if length of ``start`` larger than length of ``str``
* - (1) if ``start`` NOT starts with ``start``
* - (0) if ``str`` starts with ``start``
*/
int http_utils_str_starts_with(const char *str, const char *start);
/**
* @brief Convert milliseconds to timeval struct
*
* @param[in] timeout_ms The timeout milliseconds
* @param[out] tv Timeval struct
*/
void http_utils_ms_to_timeval(int timeout_ms, struct timeval *tv);
#define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
action; \
}
#endif

View File

@@ -0,0 +1,251 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _TRANSPORT_H_
#define _TRANSPORT_H_
#include <esp_err.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct transport_list_t* transport_list_handle_t;
typedef struct transport_item_t* transport_handle_t;
typedef int (*connect_func)(transport_handle_t t, const char *host, int port, int timeout_ms);
typedef int (*io_func)(transport_handle_t t, const char *buffer, int len, int timeout_ms);
typedef int (*io_read_func)(transport_handle_t t, char *buffer, int len, int timeout_ms);
typedef int (*trans_func)(transport_handle_t t);
typedef int (*poll_func)(transport_handle_t t, int timeout_ms);
/**
* @brief Create transport list
*
* @return A handle can hold all transports
*/
transport_list_handle_t transport_list_init();
/**
* @brief Cleanup and free all transports, include itself,
* this function will invoke transport_destroy of every transport have added this the list
*
* @param[in] list The list
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t transport_list_destroy(transport_list_handle_t list);
/**
* @brief Add a transport to the list, and define a scheme to indentify this transport in the list
*
* @param[in] list The list
* @param[in] t The Transport
* @param[in] scheme The scheme
*
* @return
* - ESP_OK
*/
esp_err_t transport_list_add(transport_list_handle_t list, transport_handle_t t, const char *scheme);
/**
* @brief This function will remove all transport from the list,
* invoke transport_destroy of every transport have added this the list
*
* @param[in] list The list
*
* @return
* - ESP_OK
* - ESP_ERR_INVALID_ARG
*/
esp_err_t transport_list_clean(transport_list_handle_t list);
/**
* @brief Get the transport by scheme, which has been defined when calling function `transport_list_add`
*
* @param[in] list The list
* @param[in] tag The tag
*
* @return The transport handle
*/
transport_handle_t transport_list_get_transport(transport_list_handle_t list, const char *scheme);
/**
* @brief Initialize a transport handle object
*
* @return The transport handle
*/
transport_handle_t transport_init();
/**
* @brief Cleanup and free memory the transport
*
* @param[in] t The transport handle
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t transport_destroy(transport_handle_t t);
/**
* @brief Get default port number used by this transport
*
* @param[in] t The transport handle
*
* @return the port number
*/
int transport_get_default_port(transport_handle_t t);
/**
* @brief Set default port number that can be used by this transport
*
* @param[in] t The transport handle
* @param[in] port The port number
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t transport_set_default_port(transport_handle_t t, int port);
/**
* @brief Transport connection function, to make a connection to server
*
* @param t The transport handle
* @param[in] host Hostname
* @param[in] port Port
* @param[in] timeout_ms The timeout milliseconds
*
* @return
* - socket for will use by this transport
* - (-1) if there are any errors, should check errno
*/
int transport_connect(transport_handle_t t, const char *host, int port, int timeout_ms);
/**
* @brief Transport read function
*
* @param t The transport handle
* @param buffer The buffer
* @param[in] len The length
* @param[in] timeout_ms The timeout milliseconds
*
* @return
* - Number of bytes was read
* - (-1) if there are any errors, should check errno
*/
int transport_read(transport_handle_t t, char *buffer, int len, int timeout_ms);
/**
* @brief Poll the transport until readable or timeout
*
* @param[in] t The transport handle
* @param[in] timeout_ms The timeout milliseconds
*
* @return
* - 0 Timeout
* - (-1) If there are any errors, should check errno
* - other The transport can read
*/
int transport_poll_read(transport_handle_t t, int timeout_ms);
/**
* @brief Transport write function
*
* @param t The transport handle
* @param buffer The buffer
* @param[in] len The length
* @param[in] timeout_ms The timeout milliseconds
*
* @return
* - Number of bytes was written
* - (-1) if there are any errors, should check errno
*/
int transport_write(transport_handle_t t, const char *buffer, int len, int timeout_ms);
/**
* @brief Poll the transport until writeable or timeout
*
* @param[in] t The transport handle
* @param[in] timeout_ms The timeout milliseconds
*
* @return
* - 0 Timeout
* - (-1) If there are any errors, should check errno
* - other The transport can write
*/
int transport_poll_write(transport_handle_t t, int timeout_ms);
/**
* @brief Transport close
*
* @param t The transport handle
*
* @return
* - 0 if ok
* - (-1) if there are any errors, should check errno
*/
int transport_close(transport_handle_t t);
/**
* @brief Get user data context of this transport
*
* @param[in] t The transport handle
*
* @return The user data context
*/
void *transport_get_context_data(transport_handle_t t);
/**
* @brief Set the user context data for this transport
*
* @param[in] t The transport handle
* @param data The user data context
*
* @return
* - ESP_OK
*/
esp_err_t transport_set_context_data(transport_handle_t t, void *data);
/**
* @brief Set transport functions for the transport handle
*
* @param[in] t The transport handle
* @param[in] _connect The connect function pointer
* @param[in] _read The read function pointer
* @param[in] _write The write function pointer
* @param[in] _close The close function pointer
* @param[in] _poll_read The poll read function pointer
* @param[in] _poll_write The poll write function pointer
* @param[in] _destroy The destroy function pointer
*
* @return
* - ESP_OK
*/
esp_err_t transport_set_func(transport_handle_t t,
connect_func _connect,
io_read_func _read,
io_func _write,
trans_func _close,
poll_func _poll_read,
poll_func _poll_write,
trans_func _destroy);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,48 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _TRANSPORT_SSL_H_
#define _TRANSPORT_SSL_H_
#include "transport.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Create new SSL transport, the transport handle must be release transport_destroy callback
*
* @return the allocated transport_handle_t, or NULL if the handle can not be allocated
*/
transport_handle_t transport_ssl_init();
/**
* @brief Set SSL certificate data (as PEM format).
* Note that, this function stores the pointer to data, rather than making a copy.
* So we need to make sure to keep the data lifetime before cleanup the connection
*
* @param t ssl transport
* @param[in] data The pem data
* @param[in] len The length
*/
void transport_ssl_set_cert_data(transport_handle_t t, const char *data, int len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,36 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _TRANSPORT_TCP_H_
#define _TRANSPORT_TCP_H_
#include "transport.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Create TCP transport, the transport handle must be release transport_destroy callback
*
* @return the allocated transport_handle_t, or NULL if the handle can not be allocated
*/
transport_handle_t transport_tcp_init();
#ifdef __cplusplus
}
#endif
#endif