http_response.c
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_response.c
9  * @date Jul 19, 2016
10  * @brief HTTP response module implementation file.
11  *
12  ************************************************************************/
13 
14 #include "http_response.h"
15 #include "definitions.h"
16 #include "memory.h"
17 #include "log_util.h"
18 
20 {
21  DEBUG_ENTRY("string_array_t *header = <%p>, mcl_uint8_t *payload = <%p>, mcl_size_t payload_size = <%u>, E_MCL_HTTP_RESULT_CODE result_code = <%d>, http_response_t **http_response = <%p>",
22  header, payload, payload_size, result_code, http_response)
23 
24  // Create a new http response object.
25  MCL_NEW(*http_response);
26  ASSERT_CODE_MESSAGE(MCL_NULL != *http_response, MCL_OUT_OF_MEMORY, "Memory can not be allocated for http response object.");
27 
28  // Set members of the http_response object.
29  (*http_response)->header = header;
30  (*http_response)->payload = payload;
31  (*http_response)->payload_size = payload_size;
32  (*http_response)->result_code = result_code;
33 
34  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
35  return MCL_OK;
36 }
37 
38 E_MCL_ERROR_CODE http_response_get_header(http_response_t *http_response, char *header_name, string_t **header_value)
39 {
40  DEBUG_ENTRY("http_response_t *http_response = <%p>, char *header_name = <%s>", http_response, header_name)
41 
42  E_MCL_ERROR_CODE result = MCL_FAIL;
43  mcl_size_t header_count = http_response->header->index;
44  mcl_size_t index;
45 
46  for (index = 0; index < header_count; index++)
47  {
48  mcl_size_t match_index = 0;
49  mcl_bool_t found;
50  string_t *header = string_array_get(http_response->header, index);
51 
52  ASSERT_CODE(MCL_NULL != header, MCL_FAIL);
53 
54  found = string_util_find_case_insensitive(header->buffer, header_name, &match_index);
55 
56  // If correct header is found then get the value
57  if (MCL_TRUE == found)
58  {
59  list_t *tokens = MCL_NULL;
60  result = string_split(header, ':', &tokens);
61  ASSERT_CODE_MESSAGE(MCL_OK == result, result, "string_split() failed for %s", header->buffer);
62 
63  // We expect token number to be 2. (key:value splits to 2)
64  if (2 != tokens->count)
65  {
66  MCL_DEBUG("Number of colons that header contains is not 1.");
67  result = MCL_FAIL;
68  }
69  else
70  {
71  result = string_initialize(tokens->head->next->data, header_value);
72  }
73 
75  break;
76  }
77  }
78 
79  DEBUG_LEAVE("retVal = <%d>", result);
80  return result;
81 }
82 
84 {
85  DEBUG_ENTRY("http_response_t *http_response = <%p>", http_response)
86 
87  DEBUG_LEAVE("retVal = <%p>", http_response->payload);
88  return http_response->payload;
89 }
90 
92 {
93  DEBUG_ENTRY("http_response_t *http_response = <%p>", http_response)
94 
95  DEBUG_LEAVE("retVal = <%d>", http_response->result_code);
96  return http_response->result_code;
97 }
98 
100 {
101  DEBUG_ENTRY("http_response_t **http_response = <%p>", http_response)
102 
103  if (MCL_NULL != *http_response)
104  {
105  // Destroy http_response together with its members.
106  string_array_destroy(&((*http_response)->header));
107  MCL_FREE((*http_response)->payload);
108  MCL_FREE(*http_response);
109 
110  MCL_DEBUG("Http response is successfully destroyed.");
111  }
112  else
113  {
114  MCL_DEBUG("Http response is already NULL.");
115  }
116 
117  DEBUG_LEAVE("retVal = void");
118 }
void string_destroy(string_t **string)
Destroys the allocated resources of the string.
Definition: string_type.c:326
Internal failure in MCL.
Definition: mcl_common.h:141
Memory module header file.
void http_response_destroy(http_response_t **http_response)
To destroy the HTTP Response Handler.
Definition: http_response.c:99
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
E_MCL_ERROR_CODE http_response_get_header(http_response_t *http_response, char *header_name, string_t **header_value)
Get the value of a specified HTTP Header.
Definition: http_response.c:38
mcl_size_t index
Item index.
Definition: string_array.h:47
#define MCL_NEW(p)
Definition: memory.h:121
#define MCL_TRUE
Definition: mcl_common.h:54
char * buffer
Buffer of string handle.
Definition: string_type.h:45
E_MCL_ERROR_CODE string_split(string_t *string, const char token, list_t **string_list)
Splits the string with the given char and returns the result as an list_t of string_t&#39;s.
Definition: string_type.c:179
void string_array_destroy(string_array_t **array)
Destroys the string array handle.
Definition: string_array.c:174
string_array_t * header
Header of http response.
Definition: http_response.h:66
E_MCL_HTTP_RESULT_CODE result_code
Result code of http response.
Definition: http_response.h:69
#define MCL_DEBUG(...)
Definition: log_util.h:70
string_t * string_array_get(string_array_t *array, mcl_size_t index)
To get the string_t string object at a specified index.
Definition: string_array.c:114
Log utility module header file.
mcl_bool_t string_util_find_case_insensitive(const char *source, const char *target, mcl_size_t *start_index)
Finds the first occurence of target in source and puts it&#39;s first index to start_index.
Definition: string_util.c:189
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
#define MCL_FREE(p)
Definition: memory.h:125
struct mcl_list_node_t * next
Next node in the list.
Definition: mcl_list.h:34
mcl_list_node_t * head
Head node of the list.
Definition: mcl_list.h:42
uint8_t mcl_uint8_t
Definition: mcl_common.h:43
#define ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: definitions.h:105
Definitions module header file.
E_MCL_HTTP_RESULT_CODE http_response_get_result_code(http_response_t *http_response)
Get the result code of the HTTTP Response.
Definition: http_response.c:91
E_MCL_ERROR_CODE string_initialize(const string_t *other, string_t **string)
Initializes an string_t object with another one.
Definition: string_type.c:24
mcl_uint8_t * http_response_get_payload(http_response_t *http_response)
Get the payload section of the HTTP Response.
Definition: http_response.c:83
#define ASSERT_CODE(condition, return_code)
Definition: definitions.h:97
size_t mcl_size_t
Definition: mcl_common.h:38
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
Success.
Definition: mcl_common.h:140
void list_destroy_with_content(list_t **list, list_item_destroy_callback callback)
To destroy the list and its items with a given callback function.
Definition: list.c:373
E_MCL_HTTP_RESULT_CODE
HTTP Result Codes.
Definition: http_response.h:26
mcl_size_t count
Node count of the list.
Definition: mcl_list.h:45
HTTP Response Handle.
Definition: http_response.h:64
Memory allocation fail.
Definition: mcl_common.h:143
#define MCL_NULL
Definition: definitions.h:24
mcl_list_item_destroy_callback list_item_destroy_callback
Definition: list.h:24
void * data
Data of the node.
Definition: mcl_list.h:32
HTTP response module header file.
E_MCL_ERROR_CODE http_response_initialize(string_array_t *header, mcl_uint8_t *payload, mcl_size_t payload_size, E_MCL_HTTP_RESULT_CODE result_code, http_response_t **http_response)
HTTP Response Module Initialize function.
Definition: http_response.c:19
mcl_uint8_t * payload
Payload of http response.
Definition: http_response.h:67