http_request.h
Go to the documentation of this file.
1 /*!**********************************************************************
2  *
3  * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4  * All rights reserved.
5  *
6  *************************************************************************
7  *
8  * @file http_request.h
9  * @date Jun 28, 2016
10  * @brief HTTP request module header file.
11  *
12  * HTTP request messages are built with this module. It has the abilities like adding headers and adding boundaries
13  * automatically when inserting a single or tuple. Manages the multipart/mixed message structure.
14  *
15  ************************************************************************/
16 
17 #ifndef HTTP_REQUEST_H_
18 #define HTTP_REQUEST_H_
19 
20 #include "http_definitions.h"
21 #include "string_array.h"
22 
23 // "--".
24 #define BOUNDARY_SIGN_LENGTH 2
25 #define BOUNDARY_SIGN "--"
26 
27 // ": ".
28 #define COLON_FORMAT_LENGTH 2
29 
30 // When boundary length is changed, test should be updated with the new length. Otherwise length test fails.
31 #define BOUNDARY_LENGTH 22
32 #define BOUNDARY_CHARACTER_COUNT 62
33 
34 #define GROWTH_FACTOR 1.5
35 
36 // Length of the string "Content-Type: multipart/related;boundary=5c6d7e29ef6868d0eb73ck" + "\r\n".
37 #define CONTENT_TYPE_LINE_LENGTH 65
38 
39 // Length of the string "Content-Type: "
40 #define CONTENT_TYPE_HEADER_LENGTH 14
41 
42 // Length of the string "Content-ID: "
43 #define CONTENT_ID_HEADER_LENGTH 12
44 
45 // Length of "\r\n".
46 #define NEW_LINE_LENGTH 2
47 #define NEW_LINE "\r\n"
48 
49 // Both opening and closing boundary line lengths are equal to boundary_line_length.
50 // opening_boundary = \r\n--5c6d7e29ef6868d0eb73\r\n , closing_boundary = \r\n--5c6d7e29ef6868d0eb73--
51 #define BOUNDARY_LINE_LENGTH (BOUNDARY_SIGN_LENGTH + BOUNDARY_LENGTH + NEW_LINE_LENGTH)
52 
53 // For http_request_initialize(), to state resizing of its buffer is enabled or not :
54 #define HTTP_REQUEST_RESIZE_ENABLED MCL_TRUE
55 #define HTTP_REQUEST_RESIZE_DISABLED MCL_FALSE
56 
62 typedef enum E_MCL_HTTP_METHOD
63 {
73 
79 typedef struct http_request_t
80 {
92 
93 typedef mcl_size_t (*payload_copy_callback_t)(void *destination, void *source, mcl_size_t size, void *user_context);
94 
116  string_t *user_agent, mcl_size_t max_http_payload_size, http_request_t **http_request);
117 
131 E_MCL_ERROR_CODE http_request_add_header(http_request_t *http_request, string_t *header_name, string_t *header_value);
132 
155 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);
156 
184 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,
185  void *user_context, void *payload, mcl_size_t payload_size, string_t *payload_content_type);
186 
202 
223 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);
224 
240 
260 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,
261  mcl_size_t *actual_written_size);
262 
272 
282 
293 
307 
315 void http_request_destroy(http_request_t **http_request);
316 
317 #endif //HTTP_REQUEST_H_
HTTP definitions module header file.
HTTP Request Handle.
Definition: http_request.h:79
string_array_t * header
Header of http request.
Definition: http_request.h:81
mcl_size_t(* payload_copy_callback_t)(void *destination, void *source, mcl_size_t size, void *user_context)
Definition: http_request.h:93
E_MCL_HTTP_METHOD method
Http method of http request.
Definition: http_request.h:85
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.
Definition: http_request.c:321
Http put method.
Definition: http_request.h:67
mcl_uint8_t * payload
Payload of http request.
Definition: http_request.h:82
mcl_bool_t finalized
The state of http request.
Definition: http_request.h:90
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
mcl_size_t max_http_payload_size
Maximum http payload size of http request.
Definition: http_request.h:88
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.
Definition: http_request.c:407
Http delete method.
Definition: http_request.h:68
E_MCL_HTTP_METHOD
HTTP Method Types.
Definition: http_request.h:62
uint8_t mcl_uint8_t
Definition: mcl_common.h:43
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.
Definition: http_request.c:131
Http connect method.
Definition: http_request.h:69
Http post method.
Definition: http_request.h:66
mcl_size_t payload_offset
Payload offset of http request.
Definition: http_request.h:84
Http get method.
Definition: http_request.h:64
Http head method.
Definition: http_request.h:65
Http trace method.
Definition: http_request.h:71
size_t mcl_size_t
Definition: mcl_common.h:38
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
void http_request_destroy(http_request_t **http_request)
To destroy the HTTP Request Handler.
Definition: http_request.c:497
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...
Definition: http_request.c:469
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...
Definition: http_request.c:448
Http options method.
Definition: http_request.h:70
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.
Definition: http_request.c:172
String array module header file.
mcl_bool_t resize_enabled
The state or condition of being resizable.
Definition: http_request.h:89
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...
Definition: http_request.c:438
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.
Definition: http_request.c:79
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.
Definition: http_request.c:214
string_t * boundary
Boundary of http request.
Definition: http_request.h:87
E_MCL_ERROR_CODE http_request_start_tuple(http_request_t *http_request)
To start a new tuple structure inside the http request body.
Definition: http_request.c:294
mcl_size_t payload_size
Payload size of http request.
Definition: http_request.h:83
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().
Definition: http_request.c:382
string_t * uri
Uri of http request.
Definition: http_request.h:86
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...
Definition: http_request.c:458