http_client_basic.c File Reference

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>
Include dependency graph for http_client_basic.c:

Go to the source code of this file.

Macros

#define TLS_DEFAULT_PORT   443
 
#define HTTP_CALLBACK_BUFFER_SIZE   1024
 
#define HTTP_PARSE_BUFFER_SIZE   2048
 
#define SIZE_OF_STRING(x)   (sizeof(x) - MCL_NULL_CHAR_SIZE)
 
#define TO_STRING_HELPER(x)   (#x)
 
#define TO_STRING(x)   TO_STRING_HELPER(x)
 
#define PORT_STRING_BUFFER_SIZE   6
 
#define CRLF_SIZE   (sizeof(crlf) - MCL_NULL_CHAR_SIZE)
 
#define HTTPS_PREFIX_SIZE   (sizeof(https_prefix) - MCL_NULL_CHAR_SIZE)
 
#define GET_STRING_SIZE   (sizeof(get_string) - MCL_NULL_CHAR_SIZE)
 
#define PUT_STRING_SIZE   (sizeof(put_string) - MCL_NULL_CHAR_SIZE)
 
#define POST_STRING_SIZE   (sizeof(post_string) - MCL_NULL_CHAR_SIZE)
 
#define HTTP_STRING_SIZE   (sizeof(http_string) - MCL_NULL_CHAR_SIZE)
 
#define CONTENT_LENGTH_BUFFER_SIZE   (sizeof(TO_STRING(SIZE_MAX)))
 
#define CONTENT_LENGTH_SIZE   (sizeof(content_length_header) - MCL_NULL_CHAR_SIZE)
 
#define TRANSFER_ENCODING_SIZE   (sizeof(transfer_encoding_header) - MCL_NULL_CHAR_SIZE)
 
#define CHUNK_LENGTH_IN_HEX_BUFFER_SIZE   (sizeof(TO_STRING(HTTP_CALLBACK_BUFFER_SIZE)) + CRLF_SIZE)
 

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"
 

Detailed Description

Basic HTTP client implementation file.

Definition in file http_client_basic.c.

Macro Definition Documentation

#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 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().

Function Documentation

static mcl_error_t get_chunked_response ( mcl_tls_socket_handle  tls_socket,
char **  response_body,
mcl_size_t body_size 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t get_content_length_response ( mcl_tls_socket_handle  tls_socket,
char **  response_body,
mcl_size_t  body_size 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static void header_list_destroy_callback ( void **  item)
static

Definition at line 783 of file http_client_basic.c.

References MCL_FREE.

Referenced by get_response().

Here is the caller graph for this function:

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).

Parameters
[in]http_clientHTTP client handle.
[in]certificateCertificate.
[in]is_fileMCL_TRUE if certificate is given as file.
Returns

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().

Here is the call graph for this function:

void mcl_http_client_destroy ( mcl_http_client_t **  http_client)

This function will release the resources of HTTP client handle.

Parameters
[in]http_clientHTTP 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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters
[in]configurationPointer to preinitialized mcl_http_client_configuration_t struct.
[out]http_clientHandle for the http client initialized.
Returns

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.

Here is the call graph for this function:

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.

Parameters
[in]http_clientHTTP client handle.
[in]http_requestHTTP request object.
[out]http_responseHTTP response object.
Returns

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.

Here is the call graph for this function:

static mcl_error_t receive_to_buffer ( mcl_tls_socket_handle  tls_socket,
void *  buffer,
mcl_size_t  size 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t receive_until_lf ( mcl_tls_socket_handle  tls_socket,
void *  buffer,
mcl_size_t size 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t send_buffer ( mcl_tls_socket_handle  tls_socket,
const void *  buffer,
mcl_size_t  size 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t send_header ( mcl_tls_socket_handle  tls_socket,
const char *  header_name,
const char *  header_value 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t send_header_list ( mcl_tls_socket_handle  tls_socket,
mcl_list_t header_list 
)
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().

Here is the call graph for this function:

Here is the caller graph for this function:

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

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().

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

const char content_length_header[] = "Content-Length"
static

Definition at line 23 of file http_client_basic.c.

Referenced by mcl_http_client_send().

const char crlf[] = "\r\n"
static
const char get_string[] = "GET "
static

Definition at line 18 of file http_client_basic.c.

Referenced by start_http().

const char http_string[] = " HTTP/1.1\r\n"
static

Definition at line 21 of file http_client_basic.c.

Referenced by start_http().

const char https_prefix[] = "https://"
static

Definition at line 16 of file http_client_basic.c.

Referenced by mcl_http_client_send().

const char post_string[] = "POST "
static

Definition at line 20 of file http_client_basic.c.

Referenced by start_http().

const char put_string[] = "PUT "
static

Definition at line 19 of file http_client_basic.c.

Referenced by start_http().

const char transfer_encoding_header[] = "Transfer-Encoding: chunked"
static

Definition at line 24 of file http_client_basic.c.

Referenced by get_response(), and mcl_http_client_send().