http_request.c File Reference

HTTP request module implementation file. More...

#include "data_types.h"
#include "http_request.h"
#include "log_util.h"
#include "definitions.h"
#include "memory.h"
#include "random.h"
Include dependency graph for http_request.c:

Go to the source code of this file.

Enumerations

enum  E_MCL_BOUNDARY_TYPE { MCL_CLOSE_BOUNDARY, MCL_OPEN_BOUNDARY }
 Open and close boundary types. More...
 

Functions

static E_MCL_ERROR_CODE _generate_random_boundary (string_t **boundary)
 
static E_MCL_ERROR_CODE _add_boundary (mcl_uint8_t *payload, mcl_size_t *payload_offset, char *boundary, E_MCL_BOUNDARY_TYPE boundary_type)
 
static E_MCL_ERROR_CODE _resize_payload_buffer_if_necessary (mcl_size_t required_empty_size, http_request_t *http_request, mcl_bool_t finalize)
 
static void _add_meta (string_t *meta, http_request_t *http_request, mcl_size_t *payload_offset)
 
static void _add_payload (http_request_t *http_request, payload_copy_callback_t payload_copy_callback, void *user_context, mcl_uint8_t *payload, mcl_size_t payload_size, mcl_size_t *payload_offset)
 
static E_MCL_ERROR_CODE _add_content_info (http_request_t *http_request, char *content_info_name, char *content_info_value, mcl_size_t *payload_offset, mcl_size_t content_info_line_length, char *sub_boundary)
 
static void _add_blank_line (http_request_t *http_request, mcl_size_t *payload_offset)
 
static mcl_size_t _get_available_space (http_request_t *http_request, mcl_size_t overhead)
 
E_MCL_ERROR_CODE http_request_initialize (E_MCL_HTTP_METHOD method, string_t *uri, mcl_size_t header_size, mcl_size_t payload_size, mcl_bool_t resize_enabled, string_t *user_agent, mcl_size_t max_http_payload_size, http_request_t **http_request)
 HTTP Request Initializer. More...
 
E_MCL_ERROR_CODE http_request_add_header (http_request_t *http_request, string_t *header_name, string_t *header_value)
 To be used to add an HTTP header to the request with it's value. More...
 
E_MCL_ERROR_CODE http_request_add_single (http_request_t *http_request, string_t *content_type, string_t *content_id, string_t *meta_string)
 To be used to add a single to the HTTP Request. More...
 
E_MCL_ERROR_CODE http_request_add_tuple (http_request_t *http_request, string_t *meta, string_t *meta_content_type, payload_copy_callback_t payload_copy_callback, void *user_context, void *payload, mcl_size_t payload_size, string_t *payload_content_type)
 To be used to add a tuple to the HTTP Request. More...
 
E_MCL_ERROR_CODE http_request_start_tuple (http_request_t *http_request)
 To start a new tuple structure inside the http request body. More...
 
E_MCL_ERROR_CODE http_request_start_tuple_sub_section (http_request_t *http_request, string_t *content_type, string_t *content_id, string_t **sub_boundary)
 To start a sub tuple section inside of the http request body. More...
 
E_MCL_ERROR_CODE http_request_end_tuple_sub_section (http_request_t *http_request, string_t *sub_boundary)
 To end a sub tuple section previously started with http_request_start_tuple_sub_section(). More...
 
E_MCL_ERROR_CODE http_request_add_raw_data (http_request_t *http_request, payload_copy_callback_t copy_callback, void *user_context, void *data, mcl_size_t data_size, mcl_size_t *actual_written_size)
 To add raw data into an http_request. More...
 
mcl_size_t http_request_get_available_space_for_raw_data (http_request_t *http_request)
 To be used to get the available space left in the request buffer in order to add a raw data. More...
 
mcl_size_t http_request_get_available_space_for_tuple (http_request_t *http_request)
 To be used to get the available space left in the request buffer in order to add a tuple. More...
 
mcl_size_t http_request_get_available_space_for_single (http_request_t *http_request)
 To be used to get the available space left in the request buffer in order to add a single. More...
 
E_MCL_ERROR_CODE http_request_finalize (http_request_t *http_request)
 Adds closing boundary to the payload and resizes the payload buffer to release unused memory space. More...
 
void http_request_destroy (http_request_t **http_request)
 To destroy the HTTP Request Handler. More...
 

Variables

char * boundary_characters = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
mcl_size_t MCL_MAX_SIZE = (mcl_size_t)-1
 
mcl_size_t OVERHEAD_FOR_TUPLE = (5 * BOUNDARY_LINE_LENGTH) + CONTENT_TYPE_LINE_LENGTH + (2 * CONTENT_TYPE_HEADER_LENGTH) + (4 * NEW_LINE_LENGTH) + MCL_NULL_CHAR_SIZE
 
mcl_size_t OVERHEAD_FOR_SINGLE
 
mcl_size_t OVERHEAD_FOR_BOUNDARY = BOUNDARY_LINE_LENGTH + MCL_NULL_CHAR_SIZE
 
mcl_size_t OVERHEAD_FOR_TUPLE_SUBSECTION_WITH_BOUNDARY_DEFINITION
 
mcl_size_t OVERHEAD_FOR_TUPLE_SUBSECTION = BOUNDARY_LINE_LENGTH + CONTENT_TYPE_HEADER_LENGTH + (2 * NEW_LINE_LENGTH) + MCL_NULL_CHAR_SIZE
 

Detailed Description

HTTP request module implementation file.


Date
Jun 28, 2016

Definition in file http_request.c.

Enumeration Type Documentation

Open and close boundary types.

Enumerator
MCL_CLOSE_BOUNDARY 

Closing boundary.

MCL_OPEN_BOUNDARY 

Opening boundary.

Definition at line 24 of file http_request.c.

Function Documentation

static void _add_blank_line ( http_request_t http_request,
mcl_size_t payload_offset 
)
static

Definition at line 738 of file http_request.c.

References DEBUG_ENTRY, DEBUG_LEAVE, MCL_NULL_CHAR, NEW_LINE, NEW_LINE_LENGTH, http_request_t::payload, and string_util_memcpy().

Referenced by _add_meta(), http_request_add_single(), http_request_add_tuple(), and http_request_start_tuple_sub_section().

Here is the call graph for this function:

Here is the caller graph for this function:

static E_MCL_ERROR_CODE _add_boundary ( mcl_uint8_t payload,
mcl_size_t payload_offset,
char *  boundary,
E_MCL_BOUNDARY_TYPE  boundary_type 
)
static
static E_MCL_ERROR_CODE _add_content_info ( http_request_t http_request,
char *  content_info_name,
char *  content_info_value,
mcl_size_t payload_offset,
mcl_size_t  content_info_line_length,
char *  sub_boundary 
)
static

Definition at line 709 of file http_request.c.

References ASSERT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, MCL_NULL, MCL_OK, http_request_t::payload, and string_util_snprintf().

Referenced by http_request_add_single(), http_request_add_tuple(), and http_request_start_tuple_sub_section().

Here is the call graph for this function:

Here is the caller graph for this function:

static void _add_meta ( string_t meta,
http_request_t http_request,
mcl_size_t payload_offset 
)
static

Definition at line 680 of file http_request.c.

References _add_blank_line(), string_t::buffer, DEBUG_ENTRY, DEBUG_LEAVE, string_t::length, http_request_t::payload, and string_util_memcpy().

Referenced by http_request_add_single(), and http_request_add_tuple().

Here is the call graph for this function:

Here is the caller graph for this function:

static void _add_payload ( http_request_t http_request,
payload_copy_callback_t  payload_copy_callback,
void *  user_context,
mcl_uint8_t payload,
mcl_size_t  payload_size,
mcl_size_t payload_offset 
)
static

Definition at line 694 of file http_request.c.

References DEBUG_ENTRY, DEBUG_LEAVE, and http_request_t::payload.

Referenced by http_request_add_raw_data(), and http_request_add_tuple().

Here is the caller graph for this function:

static E_MCL_ERROR_CODE _generate_random_boundary ( string_t **  boundary)
static
E_MCL_ERROR_CODE http_request_add_header ( http_request_t http_request,
string_t header_name,
string_t header_value 
)

To be used to add an HTTP header to the request with it's value.

Parameters
[in]http_requestHTTP Request handle to be used.
[in]header_nameHeader name.
[in]header_valueHeader value.
Returns

Definition at line 131 of file http_request.c.

References ASSERT_CODE_MESSAGE, ASSERT_STATEMENT_CODE_MESSAGE, http_request_t::boundary, string_t::buffer, CONTENT_TYPE_MULTIPART_MIXED, content_type_values, DEBUG_ENTRY, DEBUG_LEAVE, http_request_t::header, HTTP_HEADER_CONTENT_TYPE, http_header_names, string_t::length, MCL_NULL, MCL_OK, MCL_TRUE, string_array_add(), string_compare(), string_destroy(), string_initialize_new(), and string_util_snprintf().

Referenced by _add_authentication_header_to_request(), _exchange_initialize_http_request_headers(), _process_registration_response_rsa_3072(), http_processor_exchange(), http_processor_get_access_token(), http_processor_register(), http_processor_stream(), and http_request_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE http_request_add_raw_data ( http_request_t http_request,
payload_copy_callback_t  copy_callback,
void *  user_context,
void *  data,
mcl_size_t  data_size,
mcl_size_t actual_written_size 
)

To add raw data into an http_request.

To accommodate server format needs, caller must make sure that the format is suitable. Usually called between start and end tuple sections.

Parameters
[in]http_requestHTTP Request Handle to be used.
[in]copy_callbackCallback function to be used to copy the payload to http_request.
[in]user_contextUser context pointer to pass to the callback function.
[in]dataData to be added. Should be something that the callback can operate on. It will be passed to callback function.
[in]data_sizeSize of the data.
[out]actual_written_sizeActually written data size.
Returns

Definition at line 407 of file http_request.c.

References _add_payload(), _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, MCL_FALSE, MCL_NULL, MCL_OK, and http_request_t::payload_offset.

Referenced by _exchange_add_current_data_to_request_by_streaming().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE http_request_add_single ( http_request_t http_request,
string_t content_type,
string_t content_id,
string_t meta_string 
)

To be used to add a single to the HTTP Request.

Please refer to the MindConnect API Specification document for the detailed information on Singles.

With this function; a new meta section will be added in the HTTP Request message body without opening a multipart/related section and the given meta string will be inserted in this meta section.

Note: Content-Type header should be included in the received meta string.

Parameters
[in]http_requestHTTP Request Handle to be used.
[in]content_typeHTTP Content Type for this single. Sample : "application/vnd.siemens.mindsphere.meta+json".
[in]content_idA unique random id string containing 36 chars.
[in]meta_stringThe pre-generated meta string to add.
Returns

Definition at line 172 of file http_request.c.

References _add_blank_line(), _add_boundary(), _add_content_info(), _add_meta(), _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, http_request_t::boundary, string_t::buffer, CONTENT_ID_HEADER_LENGTH, CONTENT_TYPE_HEADER_LENGTH, DEBUG_ENTRY, DEBUG_LEAVE, HTTP_HEADER_CONTENT_ID, HTTP_HEADER_CONTENT_TYPE, http_header_names, string_t::length, MCL_FALSE, MCL_NULL, MCL_NULL_CHAR_SIZE, MCL_OK, MCL_OPEN_BOUNDARY, NEW_LINE_LENGTH, http_request_t::payload, and http_request_t::payload_offset.

Here is the call graph for this function:

E_MCL_ERROR_CODE http_request_add_tuple ( http_request_t http_request,
string_t meta,
string_t meta_content_type,
payload_copy_callback_t  payload_copy_callback,
void *  user_context,
void *  payload,
mcl_size_t  payload_size,
string_t payload_content_type 
)

To be used to add a tuple to the HTTP Request.

Please refer to the MindConnect API Specification document for the detailed information on tuples.

With this function; a new multipart/related section will be opened in the HTTP Request message body. The received meta string will be inserted in the fist section and the payload to the second section. The boundary id will be generated for this section and managed.

Note: Content-Type header should be included in the received meta and payload strings.

Parameters
[in]http_requestHTTP Request Handle to be used.
[in]metaMeta string to be added.
[in]meta_content_typeHTTP Content Type header for meta section of the tuple. Sample : "application/vnd.siemens.mindsphere.meta+json".
[in]payload_copy_callbackCallback function to be used to copy the payload to http_request.
[in]user_contextUser context pointer to pass to the callback function.
[in]payloadPayload string to be added.
[in]payload_sizeSize of the payload string to be added.
[in]payload_content_typeHTTP Content Type header for the payload section of the tuple. Sample : "application/octet-stream ".
Returns

Definition at line 214 of file http_request.c.

References _add_blank_line(), _add_boundary(), _add_content_info(), _add_meta(), _add_payload(), _generate_random_boundary(), _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, ASSERT_STATEMENT_CODE_MESSAGE, http_request_t::boundary, string_t::buffer, CONTENT_TYPE_HEADER_LENGTH, CONTENT_TYPE_LINE_LENGTH, CONTENT_TYPE_MULTIPART_RELATED, content_type_values, DEBUG_ENTRY, DEBUG_LEAVE, HTTP_HEADER_CONTENT_TYPE, http_header_names, string_t::length, MCL_CLOSE_BOUNDARY, MCL_FALSE, MCL_NULL, MCL_OK, MCL_OPEN_BOUNDARY, NEW_LINE_LENGTH, OVERHEAD_FOR_TUPLE, http_request_t::payload, http_request_t::payload_offset, and string_destroy().

Referenced by _exchange_add_current_data_to_request().

Here is the call graph for this function:

Here is the caller graph for this function:

void http_request_destroy ( http_request_t **  http_request)

To destroy the HTTP Request Handler.

Will release the resources of HTTP Request. After destroy operation, handler shouldn't be used.

Parameters
[in]http_requestHTTP Request handle to be freed.

Definition at line 497 of file http_request.c.

References DEBUG_ENTRY, DEBUG_LEAVE, MCL_DEBUG, MCL_FREE, MCL_NULL, string_array_destroy(), and string_destroy().

Referenced by http_processor_exchange(), http_processor_get_access_token(), http_processor_register(), http_processor_stream(), and http_request_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE http_request_end_tuple_sub_section ( http_request_t http_request,
string_t sub_boundary 
)

To end a sub tuple section previously started with http_request_start_tuple_sub_section().

Parameters
[in]http_requestHTTP Request Handle to be used
[in]sub_boundaryThe sub boundary to be used for this tuple section.
Returns

Definition at line 382 of file http_request.c.

References _add_boundary(), _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, BOUNDARY_LINE_LENGTH, string_t::buffer, DEBUG_ENTRY, DEBUG_LEAVE, MCL_CLOSE_BOUNDARY, MCL_FALSE, MCL_NULL_CHAR_SIZE, MCL_OK, http_request_t::payload, and http_request_t::payload_offset.

Referenced by _exchange_add_current_data_to_request_by_streaming().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE http_request_finalize ( http_request_t http_request)

Adds closing boundary to the payload and resizes the payload buffer to release unused memory space.

Parameters
[in]http_requestHTTP Request Handle to be used.
Returns

Definition at line 469 of file http_request.c.

References _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, http_request_t::boundary, BOUNDARY_LENGTH, BOUNDARY_SIGN, BOUNDARY_SIGN_LENGTH, string_t::buffer, DEBUG_ENTRY, DEBUG_LEAVE, MCL_HTTP_REQUEST_FINALIZE_FAILED, MCL_OK, MCL_TRUE, http_request_t::payload, http_request_t::payload_offset, and string_util_memcpy().

Referenced by _exchange_finalize_http_request().

Here is the call graph for this function:

Here is the caller graph for this function:

mcl_size_t http_request_get_available_space_for_raw_data ( http_request_t http_request)

To be used to get the available space left in the request buffer in order to add a raw data.

Differently than other get available space functions, this one doesn't calculate the overhead.

Parameters
[in]http_requestHTTP Request Handle to be used.
Returns
Returns the available size.

Definition at line 438 of file http_request.c.

References _get_available_space(), DEBUG_ENTRY, and DEBUG_LEAVE.

Referenced by _exchange_add_current_data_to_request_by_streaming().

Here is the call graph for this function:

Here is the caller graph for this function:

mcl_size_t http_request_get_available_space_for_single ( http_request_t http_request)

To be used to get the available space left in the request buffer in order to add a single.

Please refer to the MindConnect API Specification document for the detailed information on singles. Returns the free available space after deducting the necessary overhead for the single add operation.

Parameters
[in]http_requestHTTP Request Handle to be used.
Returns
Returns the available size.

Definition at line 458 of file http_request.c.

References _get_available_space(), DEBUG_ENTRY, and DEBUG_LEAVE.

Here is the call graph for this function:

mcl_size_t http_request_get_available_space_for_tuple ( http_request_t http_request)

To be used to get the available space left in the request buffer in order to add a tuple.

Please refer to the MindConnect API Specification document for the detailed information on tuples.

Parameters
[in]http_requestHTTP Request Handle to be used.
Returns
Returns the free available space after deducting the necessary overhead for the tuple add operation.

Definition at line 448 of file http_request.c.

References _get_available_space(), DEBUG_ENTRY, DEBUG_LEAVE, and OVERHEAD_FOR_TUPLE.

Here is the call graph for this function:

E_MCL_ERROR_CODE http_request_initialize ( E_MCL_HTTP_METHOD  method,
string_t uri,
mcl_size_t  header_size,
mcl_size_t  payload_size,
mcl_bool_t  resize_enabled,
string_t user_agent,
mcl_size_t  max_http_payload_size,
http_request_t **  http_request 
)

HTTP Request Initializer.

Creates, initializes and returns a http_request_t. Owner is responsible of calling http_request_destroy() when the job is done.

Parameters
[in]methodHTTP method for this request.
[in]uriHTTP Request URI. Should be started with the root.
[in]header_sizeRequired memory space for the header string.
[in]payload_sizeRequired memory space for the payload string.
[in]resize_enabledIndicates that the http_request should resize its buffer in case necessary. Give this as MCL_FALSE in case the buffer is static.
[in]user_agentValue of HTTP header User-Agent for this request.
[in]max_http_payload_sizeMaximum payload size for one http request.
[out]http_requestThe newly initialized HTTP request.
Returns

Definition at line 79 of file http_request.c.

References _generate_random_boundary(), ASSERT_CODE_MESSAGE, ASSERT_STATEMENT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, http_header_names, HTTP_HEADER_USER_AGENT, http_request_add_header(), http_request_destroy(), MCL_FALSE, MCL_MALLOC, MCL_NEW, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, string_array_initialize(), and string_initialize().

Referenced by http_processor_exchange(), http_processor_get_access_token(), http_processor_register(), and http_processor_stream().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE http_request_start_tuple ( http_request_t http_request)

To start a new tuple structure inside the http request body.

Caller should pay attention for the status of the http request body. It is the callers responsibility to maintain integrity.

Parameters
http_requestHTTP Request Handle to be used.
Returns

Definition at line 294 of file http_request.c.

References _add_boundary(), _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, http_request_t::boundary, string_t::buffer, DEBUG_ENTRY, DEBUG_LEAVE, MCL_FALSE, MCL_OK, MCL_OPEN_BOUNDARY, OVERHEAD_FOR_BOUNDARY, http_request_t::payload, and http_request_t::payload_offset.

Referenced by _exchange_add_current_data_to_request_by_streaming().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE http_request_start_tuple_sub_section ( http_request_t http_request,
string_t content_type,
string_t content_id,
string_t **  sub_boundary 
)

To start a sub tuple section inside of the http request body.

To accommodate tuple needs two sub sections must be started consequently. One is for meta and one is for payload. It is the callers responsibility to maintain integrity.

Parameters
[in]http_requestHTTP Request Handle to be used.
[in]content_typeHTTP Content Type header for this section of the tuple. Sample : "application/vnd.siemens.mindsphere.meta+json".
[in]content_idA unique random id.
[out]sub_boundaryThe sub boundary to be used for this tuple section. If a value for this param is not provided ( as NULL ), it will be created and returned to the caller to be used in consecutive calls.
Returns

Definition at line 321 of file http_request.c.

References _add_blank_line(), _add_boundary(), _add_content_info(), _generate_random_boundary(), _resize_payload_buffer_if_necessary(), ASSERT_CODE_MESSAGE, string_t::buffer, CONTENT_TYPE_HEADER_LENGTH, CONTENT_TYPE_LINE_LENGTH, CONTENT_TYPE_MULTIPART_RELATED, content_type_values, DEBUG_ENTRY, DEBUG_LEAVE, HTTP_HEADER_CONTENT_TYPE, http_header_names, string_t::length, MCL_FALSE, MCL_NULL, MCL_OK, MCL_OPEN_BOUNDARY, NEW_LINE_LENGTH, http_request_t::payload, and http_request_t::payload_offset.

Referenced by _exchange_add_current_data_to_request_by_streaming().

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

char* boundary_characters = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Definition at line 30 of file http_request.c.

Referenced by _generate_random_boundary().

mcl_size_t MCL_MAX_SIZE = (mcl_size_t)-1

Definition at line 31 of file http_request.c.

Referenced by _get_available_space().

Definition at line 51 of file http_request.c.

Referenced by http_request_start_tuple().

mcl_size_t OVERHEAD_FOR_SINGLE
Initial value:
#define CONTENT_ID_HEADER_LENGTH
Definition: http_request.h:43
#define CONTENT_TYPE_HEADER_LENGTH
Definition: http_request.h:40
#define NEW_LINE_LENGTH
Definition: http_request.h:46
#define BOUNDARY_LINE_LENGTH
Definition: http_request.h:51
#define MCL_NULL_CHAR_SIZE
Definition: definitions.h:27
#define CONTENT_TYPE_LINE_LENGTH
Definition: http_request.h:37

Definition at line 46 of file http_request.c.

Definition at line 65 of file http_request.c.

mcl_size_t OVERHEAD_FOR_TUPLE_SUBSECTION_WITH_BOUNDARY_DEFINITION
Initial value:
#define CONTENT_TYPE_HEADER_LENGTH
Definition: http_request.h:40
#define NEW_LINE_LENGTH
Definition: http_request.h:46
#define BOUNDARY_LINE_LENGTH
Definition: http_request.h:51
#define MCL_NULL_CHAR_SIZE
Definition: definitions.h:27
#define CONTENT_TYPE_LINE_LENGTH
Definition: http_request.h:37

Definition at line 58 of file http_request.c.