Basic HTTP client implementation file. More...
#include "mcl_core/mcl_memory.h"
#include "mcl_core/mcl_list.h"
#include "mcl_core/mcl_string_util.h"
#include "http_client_basic.h"
#include <stdio.h>
Go to the source code of this file.
Functions | |
static mcl_error_t | start_http (mcl_tls_socket_handle tls_socket, mcl_http_client_t *http_client, mcl_http_request_t *request) |
static mcl_error_t | get_response (mcl_tls_socket_handle tls_socket, mcl_http_response_t **http_response) |
static mcl_error_t | get_chunked_response (mcl_tls_socket_handle tls_socket, char **response_body, mcl_size_t *body_size) |
static mcl_error_t | get_content_length_response (mcl_tls_socket_handle tls_socket, char **response_body, mcl_size_t body_size) |
static mcl_error_t | receive_to_buffer (mcl_tls_socket_handle tls_socket, void *buffer, mcl_size_t size) |
static mcl_error_t | receive_until_lf (mcl_tls_socket_handle tls_socket, void *buffer, mcl_size_t *size) |
static mcl_error_t | send_buffer (mcl_tls_socket_handle tls_socket, const void *buffer, mcl_size_t size) |
static mcl_error_t | send_header_list (mcl_tls_socket_handle tls_socket, mcl_list_t *header_list) |
static mcl_error_t | send_header (mcl_tls_socket_handle tls_socket, const char *header_name, const char *header_value) |
static mcl_error_t | send_with_callback (mcl_tls_socket_handle tls_socket, mcl_http_payload_callback callback, void *stream_data, mcl_size_t payload_size) |
static void | header_list_destroy_callback (void **item) |
mcl_error_t | mcl_http_client_initialize (mcl_http_client_configuration_t *configuration, mcl_http_client_t **http_client) |
mcl_error_t | mcl_http_client_add_certificate (mcl_http_client_t *http_client, const char *certificate, mcl_bool_t is_file) |
mcl_error_t | mcl_http_client_send (mcl_http_client_t *http_client, mcl_http_request_t *http_request, mcl_http_response_t **http_response) |
void | mcl_http_client_destroy (mcl_http_client_t **http_client) |
Variables | |
static const char | crlf [] = "\r\n" |
static const char | https_prefix [] = "https://" |
static const char | get_string [] = "GET " |
static const char | put_string [] = "PUT " |
static const char | post_string [] = "POST " |
static const char | http_string [] = " HTTP/1.1\r\n" |
static const char | content_length_header [] = "Content-Length" |
static const char | transfer_encoding_header [] = "Transfer-Encoding: chunked" |
Basic HTTP client implementation file.
Definition in file http_client_basic.c.
#define CHUNK_LENGTH_IN_HEX_BUFFER_SIZE (sizeof(TO_STRING(HTTP_CALLBACK_BUFFER_SIZE)) + CRLF_SIZE) |
Definition at line 44 of file http_client_basic.c.
Referenced by send_with_callback().
#define CONTENT_LENGTH_BUFFER_SIZE (sizeof(TO_STRING(SIZE_MAX))) |
Definition at line 41 of file http_client_basic.c.
Referenced by get_chunked_response(), and mcl_http_client_send().
#define CONTENT_LENGTH_SIZE (sizeof(content_length_header) - MCL_NULL_CHAR_SIZE) |
Definition at line 42 of file http_client_basic.c.
#define CRLF_SIZE (sizeof(crlf) - MCL_NULL_CHAR_SIZE) |
Definition at line 35 of file http_client_basic.c.
Referenced by get_chunked_response(), get_response(), mcl_http_client_send(), send_header(), send_header_list(), send_with_callback(), and start_http().
#define GET_STRING_SIZE (sizeof(get_string) - MCL_NULL_CHAR_SIZE) |
Definition at line 37 of file http_client_basic.c.
Referenced by start_http().
#define HTTP_CALLBACK_BUFFER_SIZE 1024 |
Definition at line 27 of file http_client_basic.c.
Referenced by send_with_callback().
#define HTTP_PARSE_BUFFER_SIZE 2048 |
Definition at line 28 of file http_client_basic.c.
Referenced by get_response().
#define HTTP_STRING_SIZE (sizeof(http_string) - MCL_NULL_CHAR_SIZE) |
Definition at line 40 of file http_client_basic.c.
Referenced by start_http().
#define HTTPS_PREFIX_SIZE (sizeof(https_prefix) - MCL_NULL_CHAR_SIZE) |
Definition at line 36 of file http_client_basic.c.
Referenced by mcl_http_client_send(), and start_http().
#define PORT_STRING_BUFFER_SIZE 6 |
Definition at line 34 of file http_client_basic.c.
Referenced by start_http().
#define POST_STRING_SIZE (sizeof(post_string) - MCL_NULL_CHAR_SIZE) |
Definition at line 39 of file http_client_basic.c.
Referenced by start_http().
#define PUT_STRING_SIZE (sizeof(put_string) - MCL_NULL_CHAR_SIZE) |
Definition at line 38 of file http_client_basic.c.
Referenced by start_http().
#define SIZE_OF_STRING | ( | x | ) | (sizeof(x) - MCL_NULL_CHAR_SIZE) |
Definition at line 29 of file http_client_basic.c.
Referenced by start_http().
#define TLS_DEFAULT_PORT 443 |
Definition at line 26 of file http_client_basic.c.
Referenced by start_http().
#define TO_STRING | ( | x | ) | TO_STRING_HELPER(x) |
Definition at line 32 of file http_client_basic.c.
#define TO_STRING_HELPER | ( | x | ) | (#x) |
Definition at line 31 of file http_client_basic.c.
#define TRANSFER_ENCODING_SIZE (sizeof(transfer_encoding_header) - MCL_NULL_CHAR_SIZE) |
Definition at line 43 of file http_client_basic.c.
Referenced by mcl_http_client_send().
|
static |
Definition at line 623 of file http_client_basic.c.
References CONTENT_LENGTH_BUFFER_SIZE, CRLF_SIZE, MCL_DEBUG, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_ERROR_STRING, MCL_FAIL, MCL_FREE, MCL_MALLOC, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, MCL_RESIZE, receive_to_buffer(), and receive_until_lf().
Referenced by get_response().
|
static |
Definition at line 696 of file http_client_basic.c.
References MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FREE, MCL_MALLOC, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, and receive_to_buffer().
Referenced by get_response().
|
static |
Definition at line 503 of file http_client_basic.c.
References CRLF_SIZE, get_chunked_response(), get_content_length_response(), header_list_destroy_callback(), HTTP_PARSE_BUFFER_SIZE, MCL_DEBUG, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_FALSE, MCL_FREE, mcl_http_response_initialize(), mcl_list_add(), mcl_list_destroy_with_content(), mcl_list_initialize(), MCL_MALLOC, MCL_NULL, MCL_NULL_CHAR, MCL_NULL_CHAR_SIZE, MCL_OK, MCL_OUT_OF_MEMORY, mcl_string_util_find_case_insensitive(), mcl_string_util_memcpy(), MCL_TRUE, receive_until_lf(), and transfer_encoding_header.
Referenced by mcl_http_client_send().
|
static |
Definition at line 783 of file http_client_basic.c.
References MCL_FREE.
Referenced by get_response().
mcl_error_t mcl_http_client_add_certificate | ( | mcl_http_client_t * | http_client, |
const char * | certificate, | ||
mcl_bool_t | is_file | ||
) |
This function adds server certificate to certificate list of http client. The certificate list is used by the http client to identify server(s).
[in] | http_client | HTTP client handle. |
[in] | certificate | Certificate. |
[in] | is_file | MCL_TRUE if certificate is given as file. |
Definition at line 109 of file http_client_basic.c.
References mcl_http_client_t::certificate_chain, MCL_ASSERT_NOT_NULL, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FUNCTION_LEAVE_LABEL, and mcl_tls_ca_chain_add_certificate().
void mcl_http_client_destroy | ( | mcl_http_client_t ** | http_client | ) |
This function will release the resources of HTTP client handle.
[in] | http_client | HTTP client handle. |
Definition at line 239 of file http_client_basic.c.
References MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FREE, MCL_NULL, and mcl_tls_ca_chain_destroy().
Referenced by mcl_http_client_initialize().
mcl_error_t mcl_http_client_initialize | ( | mcl_http_client_configuration_t * | configuration, |
mcl_http_client_t ** | http_client | ||
) |
This function initializes the underlying implementation (like libcurl) with the given configuration data.
[in] | configuration | Pointer to preinitialized mcl_http_client_configuration_t struct. |
[out] | http_client | Handle for the http client initialized. |
Definition at line 58 of file http_client_basic.c.
References mcl_http_client_configuration_t::certificate, mcl_http_client_configuration_t::certificate_is_file, mcl_http_client_configuration_t::http_request_timeout, MCL_ASSERT_NOT_NULL, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_ERROR_STRING, MCL_FUNCTION_LEAVE_LABEL, mcl_http_client_destroy(), MCL_INVALID_PARAMETER, MCL_NEW, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, mcl_tls_ca_chain_add_certificate(), mcl_tls_ca_chain_init(), MCL_TRIGGERED_WITH_NULL, mcl_http_client_configuration_t::port, mcl_http_client_configuration_t::proxy_hostname, and mcl_http_client_configuration_t::user_agent.
mcl_error_t mcl_http_client_send | ( | mcl_http_client_t * | http_client, |
mcl_http_request_t * | http_request, | ||
mcl_http_response_t ** | http_response | ||
) |
This function sends the given request to the pre-configured destination and returns the response using underlying implementation. It is blocking until response is received or timeout occurred.
[in] | http_client | HTTP client handle. |
[in] | http_request | HTTP request object. |
[out] | http_response | HTTP response object. |
Definition at line 125 of file http_client_basic.c.
References mcl_http_client_t::certificate_chain, CONTENT_LENGTH_BUFFER_SIZE, content_length_header, crlf, CRLF_SIZE, get_response(), mcl_http_request_t::header, https_prefix, HTTPS_PREFIX_SIZE, MCL_ASSERT_NOT_NULL, MCL_DEBUG_LEAVE, MCL_ERROR_STRING, MCL_FUNCTION_LEAVE_LABEL, MCL_HTTP_GET, MCL_INVALID_PARAMETER, MCL_NULL, MCL_OK, MCL_OPERATION_NOT_SUPPORTED, mcl_string_util_snprintf(), mcl_string_util_strncmp(), mcl_tls_socket_destroy(), mcl_tls_socket_init(), mcl_tls_socket_open(), MCL_TLS_SOCKET_PARAMETER_CERTIFICATE_CHAIN, MCL_TLS_SOCKET_PARAMETER_TIMEOUT, mcl_tls_socket_set_parameter(), mcl_http_request_t::method, mcl_http_request_t::payload, mcl_http_request_t::payload_size, send_buffer(), send_header(), send_header_list(), send_with_callback(), start_http(), mcl_http_request_t::stream_callback, mcl_http_request_t::stream_data, mcl_http_client_t::timeout, transfer_encoding_header, TRANSFER_ENCODING_SIZE, mcl_http_request_t::uri, and mcl_http_client_t::user_agent.
|
static |
Definition at line 722 of file http_client_basic.c.
References MCL_OK, mcl_tls_socket_receive(), MCL_VERBOSE_ENTRY, and MCL_VERBOSE_LEAVE.
Referenced by get_chunked_response(), and get_content_length_response().
|
static |
Definition at line 742 of file http_client_basic.c.
References MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_NULL_CHAR, MCL_NULL_CHAR_SIZE, MCL_OK, and mcl_tls_socket_receive().
Referenced by get_chunked_response(), and get_response().
|
static |
Definition at line 417 of file http_client_basic.c.
References MCL_OK, mcl_tls_socket_send(), MCL_VERBOSE_ENTRY, and MCL_VERBOSE_LEAVE.
Referenced by mcl_http_client_send(), send_header(), send_header_list(), send_with_callback(), and start_http().
|
static |
Definition at line 390 of file http_client_basic.c.
References crlf, CRLF_SIZE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_NULL_CHAR_SIZE, MCL_OK, mcl_string_util_strlen(), and send_buffer().
Referenced by mcl_http_client_send().
|
static |
Definition at line 358 of file http_client_basic.c.
References crlf, CRLF_SIZE, mcl_list_node_t::data, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, mcl_list_next(), mcl_list_reset(), MCL_NULL, MCL_OK, mcl_string_util_strlen(), and send_buffer().
Referenced by mcl_http_client_send().
|
static |
Definition at line 437 of file http_client_basic.c.
References CHUNK_LENGTH_IN_HEX_BUFFER_SIZE, crlf, CRLF_SIZE, HTTP_CALLBACK_BUFFER_SIZE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_ERROR_STRING, MCL_FAIL, MCL_FALSE, MCL_OK, mcl_string_util_snprintf(), mcl_string_util_strlen(), MCL_TRUE, and send_buffer().
Referenced by mcl_http_client_send().
|
static |
Definition at line 252 of file http_client_basic.c.
References crlf, CRLF_SIZE, get_string, GET_STRING_SIZE, http_string, HTTP_STRING_SIZE, HTTPS_PREFIX_SIZE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_FREE, MCL_HTTP_GET, MCL_HTTP_POST, MCL_HTTP_PUT, MCL_MALLOC, MCL_NULL, MCL_NULL_CHAR, MCL_NULL_CHAR_SIZE, MCL_OK, MCL_OUT_OF_MEMORY, mcl_string_util_memcpy(), mcl_string_util_snprintf(), mcl_string_util_strlen(), mcl_tls_socket_connect(), mcl_http_request_t::method, mcl_http_client_t::port, PORT_STRING_BUFFER_SIZE, post_string, POST_STRING_SIZE, put_string, PUT_STRING_SIZE, send_buffer(), SIZE_OF_STRING, TLS_DEFAULT_PORT, and mcl_http_request_t::uri.
Referenced by mcl_http_client_send().
|
static |
Definition at line 23 of file http_client_basic.c.
Referenced by mcl_http_client_send().
|
static |
Definition at line 15 of file http_client_basic.c.
Referenced by mcl_http_client_send(), send_header(), send_header_list(), send_with_callback(), and start_http().
|
static |
Definition at line 18 of file http_client_basic.c.
Referenced by start_http().
|
static |
Definition at line 21 of file http_client_basic.c.
Referenced by start_http().
|
static |
Definition at line 16 of file http_client_basic.c.
Referenced by mcl_http_client_send().
|
static |
Definition at line 20 of file http_client_basic.c.
Referenced by start_http().
|
static |
Definition at line 19 of file http_client_basic.c.
Referenced by start_http().
|
static |
Definition at line 24 of file http_client_basic.c.
Referenced by get_response(), and mcl_http_client_send().