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 */ |
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... | |
uint8_t | http_client_socket_no_content_length_handler (struct http_client_module *const module) |
End of transfer when content length is not received. 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) */ |
#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. |
int _http_client_handle_entity | ( | struct http_client_module *const | module | ) |
parse the HTTP entity part.
[in] | module | Module instance of HTTP. |
int _http_client_handle_header | ( | struct http_client_module *const | module | ) |
Parse the HTTP header part.
[in] | module | Module instance of HTTP. |
int _http_client_handle_response | ( | struct http_client_module *const | module | ) |
Parse the input data from the socket.
[in] | module | Module instance of HTTP. |
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. |
|
static |
References _http_client_clear_conn(), _http_client_move_buffer(), buffer, http_client_module::cb, http_client_module::config, http_client_data_recv_chunked_data::data, HTTP_CLIENT_CALLBACK_RECV_CHUNKED_DATA, if(), 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.
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. |
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. |
void _http_client_request | ( | struct http_client_module *const | module | ) |
Send HTTP request packet.
[in] | module | Module instance of HTTP. |
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. |
|
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().
uint8_t http_client_socket_no_content_length_handler | ( | struct http_client_module *const | module | ) |
End of transfer when content length is not received.
[in] | module | Module instance of HTTP. |
References _http_client_clear_conn(), http_client_module::cb, http_client_resp::content_length, HTTP_CLIENT_CALLBACK_RECV_NO_CONTENT_LENGTH_DATA, http_client_data_recv_chunked_data::is_complete, http_client_module::permanent, http_client_data::recv_chunked_data, http_client_module::resp, http_client_resp::response_code, http_client_resp::state, and STATE_PARSE_HEADER.
Referenced by http_client_socket_event_handler().
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. |
|
static |
Global reference of HTTP client instance.
Socket callback interface has not user private data. So it needed reference to HTTP client module instance.