http_request.c
Go to the documentation of this file.
1 
9 #include "http_request.h"
10 #include "definitions.h"
11 #include "string_util.h"
12 #include "mcl_core/mcl_assert.h"
13 #include "mcl_core/mcl_memory.h"
14 
15 // This function is used as callback to list_destroy_with_content() function.
16 static void _http_request_header_destroy(char **header);
17 
19 {
20  mcl_error_t code;
21 
22  MCL_DEBUG_ENTRY("mcl_http_request_t **http_request = <%p>", http_request);
23 
24  // Null check.
25  MCL_ASSERT_NOT_NULL(http_request, code);
26 
27  MCL_NEW(*http_request);
28 
29  if (MCL_NULL == *http_request)
30  {
31  code = MCL_OUT_OF_MEMORY;
32  }
33  else
34  {
35  // Set initial values.
36  (*http_request)->header = MCL_NULL;
37  (*http_request)->payload = MCL_NULL;
38  (*http_request)->uri = MCL_NULL;
39  (*http_request)->method = MCL_HTTP_GET;
40  (*http_request)->payload_size = 0;
41  (*http_request)->stream_callback = MCL_NULL;
42  (*http_request)->stream_data = MCL_NULL;
43 
44  code = mcl_list_initialize(&(*http_request)->header);
45  }
46 
47  if (MCL_OK != code)
48  {
49  mcl_http_request_destroy(http_request);
50  }
51 
53  MCL_DEBUG_LEAVE("retVal = <%d>", code);
54  return code;
55 }
56 
57 mcl_error_t mcl_http_request_add_header(mcl_http_request_t *http_request, const char *header_name, const char *header_value)
58 {
59  mcl_error_t code;
60  char *header_format = "%s: %s";
61  mcl_size_t header_line_length;
62  char *header_line;
63 
64  MCL_DEBUG_ENTRY("mcl_http_request_t *http_request = <%p>, const char *header_name = <%p>, const char *header_value = <%p>",
65  http_request, header_name, header_value);
66 
67  // Null check.
68  MCL_ASSERT_NOT_NULL(http_request, code);
69  MCL_ASSERT_NOT_NULL(header_name, code);
70  MCL_ASSERT_NOT_NULL(header_value, code);
71 
72  // Length of the header line, +2 is for the string ": ".
73  header_line_length = string_util_strlen(header_name) + string_util_strlen(header_value) + 2;
74 
75  header_line = MCL_MALLOC(header_line_length + 1);
76  MCL_ASSERT_CODE_MESSAGE(MCL_NULL != header_line, MCL_OUT_OF_MEMORY, "Memory can not be allocated for header line.");
77 
78  // Compose the header line.
79  code = string_util_snprintf(header_line, header_line_length + 1, header_format, header_name, header_value);
80 
81  // Add header line to the list of header lines.
82  if (MCL_OK == code)
83  {
84  code = mcl_list_add(http_request->header, header_line);
85  }
86 
87  if (MCL_OK != code)
88  {
89  MCL_FREE(header_line);
90  }
91 
93  MCL_DEBUG_LEAVE("retVal = <%d>", code);
94  return code;
95 }
96 
98 {
99  mcl_error_t code = MCL_OK;
100 
101  MCL_DEBUG_ENTRY("mcl_http_request_t *http_request = <%p>, E_MCL_HTTP_REQUEST_PARAMETER parameter = <%d>, const void *value = <%p>",
102  http_request, parameter, value);
103 
104  // Null check.
105  MCL_ASSERT_NOT_NULL(http_request, code);
106  MCL_ASSERT_NOT_NULL(value, code);
107 
108  switch (parameter)
109  {
111 
112  if (*((E_MCL_HTTP_METHOD *) value) < MCL_HTTP_END)
113  {
114  http_request->method = *((E_MCL_HTTP_METHOD *) value);
115  }
116  else
117  {
118  code = MCL_INVALID_PARAMETER;
119  }
120 
121  break;
122 
124  code = string_util_reset(value, &(http_request->uri));
125  break;
126 
128  http_request->payload = (mcl_uint8_t *) value;
129  http_request->stream_callback = MCL_NULL;
130  break;
131 
133  http_request->payload_size = *((mcl_size_t *) value);
134  break;
135 
137  http_request->stream_callback = (mcl_http_payload_callback) value;
138  http_request->payload = MCL_NULL;
139  break;
140 
142  http_request->stream_data = (void *) value;
143  break;
144 
145  default:
146  code = MCL_INVALID_PARAMETER;
147  break;
148  }
149 
151  MCL_DEBUG_LEAVE("retVal = <%d>", code);
152  return code;
153 }
154 
156 {
157  MCL_DEBUG_ENTRY("mcl_http_request_t **http_request = <%p>", http_request);
158 
159  if (MCL_NULL != *http_request)
160  {
161  // Free http request with its members.
163  MCL_FREE((*http_request)->uri);
164  MCL_FREE(*http_request);
165  }
166 
167  MCL_DEBUG_LEAVE("retVal = void");
168 }
169 
170 static void _http_request_header_destroy(char **header)
171 {
172  MCL_FREE(*header);
173 }
#define MCL_FUNCTION_LEAVE_LABEL
size_t mcl_size_t
static void _http_request_header_destroy(char **header)
Definition: http_request.c:170
Assert module header file.
Success.
char * uri
Uri of http request.
mcl_error_t mcl_http_request_initialize(mcl_http_request_t **http_request)
Definition: http_request.c:18
Size of the body of the http request in bytes as mcl_size_t.
mcl_int32_t mcl_error_t
mcl_error_t mcl_http_request_set_parameter(mcl_http_request_t *http_request, E_MCL_HTTP_REQUEST_PARAMETER parameter, const void *value)
Definition: http_request.c:97
#define MCL_DEBUG_ENTRY(...)
Definition: mcl_log_util.h:115
mcl_size_t payload_size
Payload size of http request.
void * stream_data
Stream data.
void(* mcl_list_item_destroy_callback)(void **item)
Definition: mcl_list.h:61
E_MCL_HTTP_REQUEST_PARAMETER
String utility module header file.
#define MCL_ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: mcl_assert.h:77
#define MCL_NEW(p)
Definition: mcl_memory.h:55
E_MCL_HTTP_METHOD
#define MCL_NULL
Url of the http request as char*.
Http get method.
MCL_CORE_EXPORT mcl_error_t mcl_list_add(mcl_list_t *list, void *data)
Definition: list.c:34
#define MCL_FREE(p)
Definition: mcl_memory.h:59
E_MCL_HTTP_METHOD method
Http method of http request.
MCL_CORE_EXPORT void mcl_list_destroy_with_content(mcl_list_t **list, mcl_list_item_destroy_callback callback)
Definition: list.c:302
Definitions module header file.
uint8_t mcl_uint8_t
#define MCL_ASSERT_NOT_NULL(argument, return_variable)
Definition: mcl_assert.h:38
Body of the http request as char*. HTTP Request neither copies the buffer, nor takes ownership...
mcl_error_t mcl_http_request_add_header(mcl_http_request_t *http_request, const char *header_name, const char *header_value)
Definition: http_request.c:57
mcl_error_t string_util_snprintf(char *string, mcl_size_t length, const char *format,...)
Definition: string_util.c:177
mcl_error_t string_util_reset(const void *value, char **target)
Definition: string_util.c:431
HTTP request module header file.
mcl_size_t(* mcl_http_payload_callback)(char *buffer, mcl_size_t size, mcl_size_t count, void *user_context)
mcl_http_payload_callback stream_callback
Callback to be used with chunked Transfer-Encoding. If not used, it must be NULL. ...
mcl_uint8_t * payload
Payload of http request.
void mcl_http_request_destroy(mcl_http_request_t **http_request)
Definition: http_request.c:155
Memory allocation fail.
MCL_CORE_EXPORT mcl_error_t mcl_list_initialize(mcl_list_t **list)
Definition: list.c:13
#define MCL_MALLOC(bytes)
Definition: mcl_memory.h:54
General invalid parameter fail.
#define MCL_DEBUG_LEAVE(...)
Definition: mcl_log_util.h:116
mcl_list_t * header
Header of http request.
Method of the http request as E_MCL_HTTP_METHOD.
mcl_size_t string_util_strlen(const char *buffer)
Definition: string_util.c:35
Memory module interface header file.