HTTP client service.
Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
#include "iot/http/http_client.h"
#include <string.h>
#include "driver/include/m2m_wifi.h"
#include "iot/stream_writer.h"
#include <stdio.h>
#include <errno.h>
Macros | |
#define | DEFAULT_USER_AGENT "atmel/1.0.2" |
#define | HTTP_CHUNKED_MAX_LENGTH 3 /*TCP MTU is 1400(0x578) */ |
#define | MIN_SEND_BUFFER_SIZE 18 + HTTP_MAX_URI_LENGTH /* DELETE {URI} HTTP/1.1\r\n */ |
Enumerations | |
enum | http_client_req_state { STATE_INIT = 0, STATE_TRY_SOCK_CONNECT, STATE_SOCK_CONNECTED, STATE_REQ_SEND_HEADER, STATE_REQ_SEND_ENTITY } |
enum | http_client_resp_state { STATE_PARSE_HEADER, STATE_PARSE_ENTITY } |
Functions | |
void | _http_client_clear_conn (struct http_client_module *const module, int reason) |
Clear the HTTP instance. More... | |
int | _http_client_handle_entity (struct http_client_module *const module) |
parse the HTTP entity part. More... | |
int | _http_client_handle_header (struct http_client_module *const module) |
Parse the HTTP header part. More... | |
int | _http_client_handle_response (struct http_client_module *const module) |
Parse the input data from the socket. More... | |
void | _http_client_move_buffer (struct http_client_module *const module, char *base) |
Move remain part of the buffer to the start position in the buffer. More... | |
static void | _http_client_read_chuked_entity (struct http_client_module *const module) |
int | _http_client_read_wait (void *module, char *buffer, size_t buffer_len) |
void | _http_client_recv_packet (struct http_client_module *const module) |
Start receiving HTTP packet. More... | |
void | _http_client_recved_packet (struct http_client_module *const module, int read_len) |
Perform the post processing of the received packet. More... | |
void | _http_client_request (struct http_client_module *const module) |
Send HTTP request packet. More... | |
int | _http_client_send_wait (void *module, char *buffer, size_t buffer_len) |
Sending the packet in blocking mode. More... | |
static int | _hwerr_to_stderr (int err) |
change HW error type to standard error. More... | |
static int | _is_ip (const char *host) |
int | http_client_close (struct http_client_module *const module) |
Force close HTTP connection. More... | |
int | http_client_deinit (struct http_client_module *const module) |
Terminate HTTP client service. More... | |
void | http_client_get_config_defaults (struct http_client_config *const config) |
Get default configuration of HTTP client module. More... | |
int | http_client_init (struct http_client_module *const module, struct http_client_config *config) |
Initialize HTTP client service. More... | |
int | http_client_register_callback (struct http_client_module *const module, http_client_callback_t callback) |
Register and enable the callback. More... | |
int | http_client_send_request (struct http_client_module *const module, const char *url, enum http_method method, struct http_entity *const entity, const char *ext_header) |
Event handler of gethostbyname. More... | |
void | http_client_socket_event_handler (SOCKET sock, uint8_t msg_type, void *msg_data) |
Event handler of socket event. More... | |
void | http_client_socket_resolve_handler (uint8_t *doamin_name, uint32_t server_ip) |
Event handler of gethostbyname. More... | |
void | http_client_timer_callback (struct sw_timer_module *const module, int timer_id, void *context, int period) |
Timer callback entry of HTTP client. More... | |
int | http_client_unregister_callback (struct http_client_module *const module) |
Unregister callback. More... | |
Variables | |
static struct http_client_module * | module_ref_inst [TCP_SOCK_MAX] = {NULL,} |
Global reference of HTTP client instance. More... | |
#define DEFAULT_USER_AGENT "atmel/1.0.2" |
Referenced by http_client_get_config_defaults().
#define HTTP_CHUNKED_MAX_LENGTH 3 /*TCP MTU is 1400(0x578) */ |
Referenced by _http_client_request().
#define MIN_SEND_BUFFER_SIZE 18 + HTTP_MAX_URI_LENGTH /* DELETE {URI} HTTP/1.1\r\n */ |
Referenced by http_client_get_config_defaults(), and http_client_init().
void _http_client_clear_conn | ( | struct http_client_module *const | module, |
int | reason | ||
) |
Clear the HTTP instance.
[in] | module | Module instance of HTTP. |
References http_client_module::cb, http_entity::close, close(), http_client_data::disconnected, http_client_req::entity, HTTP_CLIENT_CALLBACK_DISCONNECTED, NULL, http_client_module::permanent, http_entity::priv_data, http_client_data_disconnected::reason, http_client_module::req, http_client_module::resp, http_client_module::sending, http_client_module::sock, http_client_req::state, http_client_resp::state, STATE_INIT, STATE_PARSE_HEADER, and STATE_TRY_SOCK_CONNECT.
Referenced by _http_client_handle_entity(), _http_client_handle_header(), _http_client_read_chuked_entity(), _http_client_recv_packet(), _http_client_request(), http_client_close(), http_client_send_request(), http_client_socket_event_handler(), http_client_socket_resolve_handler(), and http_client_timer_callback().
int _http_client_handle_entity | ( | struct http_client_module *const | module | ) |
parse the HTTP entity part.
[in] | module | Module instance of HTTP. |
References _http_client_clear_conn(), _http_client_move_buffer(), _http_client_read_chuked_entity(), http_client_module::cb, http_client_module::config, http_client_data_recv_response::content, http_client_data_recv_response::content_length, http_client_resp::content_length, http_client_data_recv_chunked_data::data, HTTP_CLIENT_CALLBACK_RECV_CHUNKED_DATA, HTTP_CLIENT_CALLBACK_RECV_RESPONSE, http_client_data_recv_response::is_chunked, http_client_data_recv_chunked_data::is_complete, http_client_data_recv_chunked_data::length, http_client_module::permanent, http_client_resp::read_length, http_client_config::recv_buffer, http_client_config::recv_buffer_size, http_client_data::recv_chunked_data, http_client_data::recv_response, http_client_module::recved_size, http_client_module::resp, http_client_data_recv_response::response_code, http_client_resp::response_code, http_client_resp::state, and STATE_PARSE_HEADER.
Referenced by _http_client_handle_response().
int _http_client_handle_header | ( | struct http_client_module *const | module | ) |
Parse the HTTP header part.
[in] | module | Module instance of HTTP. |
References _http_client_clear_conn(), _http_client_move_buffer(), http_client_module::cb, http_client_module::config, http_client_data_recv_response::content, http_client_data_recv_response::content_length, http_client_req::content_length, http_client_resp::content_length, HTTP_CLIENT_CALLBACK_RECV_RESPONSE, http_client_data_recv_response::is_chunked, NULL, http_client_module::permanent, http_client_resp::read_length, http_client_config::recv_buffer, http_client_config::recv_buffer_size, http_client_data::recv_response, http_client_module::recved_size, http_client_module::req, http_client_module::resp, http_client_data_recv_response::response_code, http_client_resp::response_code, http_client_resp::state, and STATE_PARSE_ENTITY.
Referenced by _http_client_handle_response().
int _http_client_handle_response | ( | struct http_client_module *const | module | ) |
Parse the input data from the socket.
[in] | module | Module instance of HTTP. |
References _http_client_handle_entity(), _http_client_handle_header(), http_client_module::resp, http_client_resp::state, STATE_PARSE_ENTITY, and STATE_PARSE_HEADER.
Referenced by _http_client_recved_packet().
void _http_client_move_buffer | ( | struct http_client_module *const | module, |
char * | base | ||
) |
Move remain part of the buffer to the start position in the buffer.
[in] | module | Module instance of HTTP. |
[in] | base | Current position of the buffer pointer. |
References http_client_module::config, http_client_config::recv_buffer, and http_client_module::recved_size.
Referenced by _http_client_handle_entity(), _http_client_handle_header(), and _http_client_read_chuked_entity().
|
static |
References _http_client_clear_conn(), _http_client_move_buffer(), http_client_module::cb, http_client_module::config, http_client_data_recv_chunked_data::data, HTTP_CLIENT_CALLBACK_RECV_CHUNKED_DATA, http_client_data_recv_chunked_data::is_complete, http_client_data_recv_chunked_data::length, NULL, http_client_module::permanent, http_client_resp::read_length, http_client_config::recv_buffer, http_client_config::recv_buffer_size, http_client_data::recv_chunked_data, http_client_module::recved_size, http_client_module::resp, http_client_resp::response_code, http_client_resp::state, and STATE_PARSE_HEADER.
Referenced by _http_client_handle_entity().
int _http_client_read_wait | ( | void * | module, |
char * | buffer, | ||
size_t | buffer_len | ||
) |
void _http_client_recv_packet | ( | struct http_client_module *const | module | ) |
Start receiving HTTP packet.
[in] | module | Module instance of HTTP. |
References _http_client_clear_conn(), http_client_module::config, NULL, recv(), http_client_config::recv_buffer, http_client_config::recv_buffer_size, http_client_module::recved_size, and http_client_module::sock.
Referenced by http_client_socket_event_handler().
void _http_client_recved_packet | ( | struct http_client_module *const | module, |
int | read_len | ||
) |
Perform the post processing of the received packet.
[in] | module | Module instance of MQTT. |
[in] | read_len | Read size from the recv function. |
References _http_client_handle_response(), http_client_module::config, http_client_module::recved_size, sw_timer_disable_callback(), http_client_config::timeout, http_client_module::timer_id, and http_client_config::timer_inst.
Referenced by http_client_socket_event_handler().
void _http_client_request | ( | struct http_client_module *const | module | ) |
Send HTTP request packet.
[in] | module | Module instance of HTTP. |
References _http_client_clear_conn(), _http_client_send_wait(), http_client_module::cb, http_entity::close, http_client_module::config, http_client_req::content_length, http_client_req::entity, http_client_req::ext_header, http_entity::get_contents_length, http_client_module::host, HTTP_CHUNKED_MAX_LENGTH, HTTP_CLIENT_CALLBACK_REQUESTED, HTTP_METHOD_DELETE, HTTP_METHOD_GET, HTTP_METHOD_HEAD, HTTP_METHOD_OPTIONS, HTTP_METHOD_POST, HTTP_METHOD_PUT, HTTP_PROTO_NAME, http_entity::is_chunked, http_client_req::method, NULL, http_entity::priv_data, http_entity::read, http_client_module::req, send(), http_client_config::send_buffer_size, http_client_module::sending, http_client_req::sent_length, http_client_module::sock, http_client_req::state, STATE_REQ_SEND_ENTITY, STATE_REQ_SEND_HEADER, STATE_SOCK_CONNECTED, stream_writer_init(), stream_writer_send_buffer(), stream_writer_send_remain(), http_client_req::uri, and http_client_config::user_agent.
Referenced by http_client_send_request(), and http_client_socket_event_handler().
int _http_client_send_wait | ( | void * | module, |
char * | buffer, | ||
size_t | buffer_len | ||
) |
Sending the packet in blocking mode.
[in] | module | Module instance of HTTP. |
[in] | buffer | Buffer to be sent. |
[in] | buffer_len | Buffer size. |
References http_client_module::config, m2m_wifi_handle_events(), NULL, http_client_module::req, send(), http_client_module::sending, http_client_module::sock, http_client_req::state, STATE_SOCK_CONNECTED, sw_timer_task(), and http_client_config::timer_inst.
Referenced by _http_client_request().
|
inlinestatic |
change HW error type to standard error.
References SOCK_ERR_ADDR_ALREADY_IN_USE, SOCK_ERR_ADDR_IS_REQUIRED, SOCK_ERR_BUFFER_FULL, SOCK_ERR_CONN_ABORTED, SOCK_ERR_INVALID, SOCK_ERR_INVALID_ADDRESS, SOCK_ERR_INVALID_ARG, SOCK_ERR_MAX_LISTEN_SOCK, SOCK_ERR_MAX_TCP_SOCK, SOCK_ERR_MAX_UDP_SOCK, and SOCK_ERR_TIMEOUT.
Referenced by http_client_socket_event_handler().
|
static |
Referenced by http_client_send_request().
void http_client_timer_callback | ( | struct sw_timer_module *const | module, |
int | timer_id, | ||
void * | context, | ||
int | period | ||
) |
Timer callback entry of HTTP client.
[in] | timer_id | ID of timer. |
[in] | context | Private data of timer. |
[in] | context | Period of timer handle. |
References _http_client_clear_conn(), and NULL.
Referenced by http_client_init().
|
static |
Global reference of HTTP client instance.
Socket callback interface has not user private data. So it needed reference to HTTP client module instance.