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"
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 |
HTTP request module implementation file.
Definition in file http_request.c.
enum E_MCL_BOUNDARY_TYPE |
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.
|
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().
|
static |
Definition at line 568 of file http_request.c.
References ASSERT_CODE_MESSAGE, BOUNDARY_LINE_LENGTH, BOUNDARY_SIGN_LENGTH, DEBUG_ENTRY, DEBUG_LEAVE, MCL_NULL, MCL_OK, MCL_OPEN_BOUNDARY, and string_util_snprintf().
Referenced by http_request_add_single(), http_request_add_tuple(), http_request_end_tuple_sub_section(), http_request_start_tuple(), and http_request_start_tuple_sub_section().
|
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().
|
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().
|
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().
|
static |
Definition at line 539 of file http_request.c.
References ASSERT_CODE_MESSAGE, BOUNDARY_CHARACTER_COUNT, boundary_characters, BOUNDARY_LENGTH, DEBUG_ENTRY, DEBUG_LEAVE, MCL_NULL, MCL_OK, random_generate_number(), string_destroy(), and string_initialize_new().
Referenced by http_request_add_tuple(), http_request_initialize(), and http_request_start_tuple_sub_section().
|
static |
Definition at line 520 of file http_request.c.
References http_request_t::max_http_payload_size, MCL_MAX_SIZE, http_request_t::payload_offset, http_request_t::payload_size, and http_request_t::resize_enabled.
Referenced by http_request_get_available_space_for_raw_data(), http_request_get_available_space_for_single(), and http_request_get_available_space_for_tuple().
|
static |
Definition at line 597 of file http_request.c.
References ASSERT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, GROWTH_FACTOR, http_request_t::max_http_payload_size, MCL_DEBUG, MCL_ERROR_RETURN, MCL_HTTP_REQUEST_NO_MORE_SPACE, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, MCL_RESIZE, MCL_TRUE, http_request_t::payload, http_request_t::payload_offset, http_request_t::payload_size, and http_request_t::resize_enabled.
Referenced by http_request_add_raw_data(), http_request_add_single(), http_request_add_tuple(), http_request_end_tuple_sub_section(), http_request_finalize(), http_request_start_tuple(), and http_request_start_tuple_sub_section().
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.
[in] | http_request | HTTP Request handle to be used. |
[in] | header_name | Header name. |
[in] | header_value | Header value. |
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().
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.
[in] | http_request | HTTP Request Handle to be used. |
[in] | copy_callback | Callback function to be used to copy the payload to http_request . |
[in] | user_context | User context pointer to pass to the callback function. |
[in] | data | Data to be added. Should be something that the callback can operate on. It will be passed to callback function. |
[in] | data_size | Size of the data. |
[out] | actual_written_size | Actually written data size. |
http_request
has no more space for adding new content. 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().
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.
[in] | http_request | HTTP Request Handle to be used. |
[in] | content_type | HTTP Content Type for this single. Sample : "application/vnd.siemens.mindsphere.meta+json". |
[in] | content_id | A unique random id string containing 36 chars. |
[in] | meta_string | The pre-generated meta string to add. |
http_request
has no more space for adding new content. 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.
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.
[in] | http_request | HTTP Request Handle to be used. |
[in] | meta | Meta string to be added. |
[in] | meta_content_type | HTTP Content Type header for meta section of the tuple. Sample : "application/vnd.siemens.mindsphere.meta+json". |
[in] | payload_copy_callback | Callback function to be used to copy the payload to http_request . |
[in] | user_context | User context pointer to pass to the callback function. |
[in] | payload | Payload string to be added. |
[in] | payload_size | Size of the payload string to be added. |
[in] | payload_content_type | HTTP Content Type header for the payload section of the tuple. Sample : "application/octet-stream ". |
http_request
has no more space for adding new content. 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().
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.
[in] | http_request | HTTP 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().
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()
.
[in] | http_request | HTTP Request Handle to be used |
[in] | sub_boundary | The sub boundary to be used for this tuple section. |
http_request
has no more space for adding new content. 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().
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.
[in] | http_request | HTTP Request Handle to be used. |
http_request
has no more space for adding new content. 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().
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.
[in] | http_request | HTTP Request Handle to be used. |
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().
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.
[in] | http_request | HTTP Request Handle to be used. |
Definition at line 458 of file http_request.c.
References _get_available_space(), DEBUG_ENTRY, and DEBUG_LEAVE.
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.
[in] | http_request | HTTP Request Handle to be used. |
Definition at line 448 of file http_request.c.
References _get_available_space(), DEBUG_ENTRY, DEBUG_LEAVE, and OVERHEAD_FOR_TUPLE.
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.
[in] | method | HTTP method for this request. |
[in] | uri | HTTP Request URI. Should be started with the root. |
[in] | header_size | Required memory space for the header string. |
[in] | payload_size | Required memory space for the payload string. |
[in] | resize_enabled | Indicates that the http_request should resize its buffer in case necessary. Give this as MCL_FALSE in case the buffer is static. |
[in] | user_agent | Value of HTTP header User-Agent for this request. |
[in] | max_http_payload_size | Maximum payload size for one http request. |
[out] | http_request | The newly initialized HTTP request. |
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().
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.
http_request | HTTP Request Handle to be used. |
http_request
has no more space for adding new content. 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().
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.
[in] | http_request | HTTP Request Handle to be used. |
[in] | content_type | HTTP Content Type header for this section of the tuple. Sample : "application/vnd.siemens.mindsphere.meta+json". |
[in] | content_id | A unique random id. |
[out] | sub_boundary | The 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. |
http_request
has no more space for adding new content. 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().
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().
mcl_size_t OVERHEAD_FOR_BOUNDARY = BOUNDARY_LINE_LENGTH + MCL_NULL_CHAR_SIZE |
Definition at line 51 of file http_request.c.
Referenced by http_request_start_tuple().
mcl_size_t OVERHEAD_FOR_SINGLE |
Definition at line 46 of file http_request.c.
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 |
Definition at line 38 of file http_request.c.
Referenced by http_request_add_tuple(), and http_request_get_available_space_for_tuple().
mcl_size_t OVERHEAD_FOR_TUPLE_SUBSECTION = BOUNDARY_LINE_LENGTH + CONTENT_TYPE_HEADER_LENGTH + (2 * NEW_LINE_LENGTH) + MCL_NULL_CHAR_SIZE |
Definition at line 65 of file http_request.c.
mcl_size_t OVERHEAD_FOR_TUPLE_SUBSECTION_WITH_BOUNDARY_DEFINITION |
Definition at line 58 of file http_request.c.