/src/open5gs/lib/sbi/openapi/model/context_info.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "context_info.h" |
6 | | |
7 | | OpenAPI_context_info_t *OpenAPI_context_info_create( |
8 | | OpenAPI_list_t *orig_headers, |
9 | | OpenAPI_list_t *request_headers |
10 | | ) |
11 | 0 | { |
12 | 0 | OpenAPI_context_info_t *context_info_local_var = ogs_malloc(sizeof(OpenAPI_context_info_t)); |
13 | 0 | ogs_assert(context_info_local_var); |
14 | | |
15 | 0 | context_info_local_var->orig_headers = orig_headers; |
16 | 0 | context_info_local_var->request_headers = request_headers; |
17 | |
|
18 | 0 | return context_info_local_var; |
19 | 0 | } |
20 | | |
21 | | void OpenAPI_context_info_free(OpenAPI_context_info_t *context_info) |
22 | 0 | { |
23 | 0 | OpenAPI_lnode_t *node = NULL; |
24 | |
|
25 | 0 | if (NULL == context_info) { |
26 | 0 | return; |
27 | 0 | } |
28 | 0 | if (context_info->orig_headers) { |
29 | 0 | OpenAPI_list_for_each(context_info->orig_headers, node) { |
30 | 0 | ogs_free(node->data); |
31 | 0 | } |
32 | 0 | OpenAPI_list_free(context_info->orig_headers); |
33 | 0 | context_info->orig_headers = NULL; |
34 | 0 | } |
35 | 0 | if (context_info->request_headers) { |
36 | 0 | OpenAPI_list_for_each(context_info->request_headers, node) { |
37 | 0 | ogs_free(node->data); |
38 | 0 | } |
39 | 0 | OpenAPI_list_free(context_info->request_headers); |
40 | 0 | context_info->request_headers = NULL; |
41 | 0 | } |
42 | 0 | ogs_free(context_info); |
43 | 0 | } |
44 | | |
45 | | cJSON *OpenAPI_context_info_convertToJSON(OpenAPI_context_info_t *context_info) |
46 | 0 | { |
47 | 0 | cJSON *item = NULL; |
48 | 0 | OpenAPI_lnode_t *node = NULL; |
49 | |
|
50 | 0 | if (context_info == NULL) { |
51 | 0 | ogs_error("OpenAPI_context_info_convertToJSON() failed [ContextInfo]"); |
52 | 0 | return NULL; |
53 | 0 | } |
54 | | |
55 | 0 | item = cJSON_CreateObject(); |
56 | 0 | if (context_info->orig_headers) { |
57 | 0 | cJSON *orig_headersList = cJSON_AddArrayToObject(item, "origHeaders"); |
58 | 0 | if (orig_headersList == NULL) { |
59 | 0 | ogs_error("OpenAPI_context_info_convertToJSON() failed [orig_headers]"); |
60 | 0 | goto end; |
61 | 0 | } |
62 | 0 | OpenAPI_list_for_each(context_info->orig_headers, node) { |
63 | 0 | if (cJSON_AddStringToObject(orig_headersList, "", (char*)node->data) == NULL) { |
64 | 0 | ogs_error("OpenAPI_context_info_convertToJSON() failed [orig_headers]"); |
65 | 0 | goto end; |
66 | 0 | } |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | 0 | if (context_info->request_headers) { |
71 | 0 | cJSON *request_headersList = cJSON_AddArrayToObject(item, "requestHeaders"); |
72 | 0 | if (request_headersList == NULL) { |
73 | 0 | ogs_error("OpenAPI_context_info_convertToJSON() failed [request_headers]"); |
74 | 0 | goto end; |
75 | 0 | } |
76 | 0 | OpenAPI_list_for_each(context_info->request_headers, node) { |
77 | 0 | if (cJSON_AddStringToObject(request_headersList, "", (char*)node->data) == NULL) { |
78 | 0 | ogs_error("OpenAPI_context_info_convertToJSON() failed [request_headers]"); |
79 | 0 | goto end; |
80 | 0 | } |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | 0 | end: |
85 | 0 | return item; |
86 | 0 | } |
87 | | |
88 | | OpenAPI_context_info_t *OpenAPI_context_info_parseFromJSON(cJSON *context_infoJSON) |
89 | 0 | { |
90 | 0 | OpenAPI_context_info_t *context_info_local_var = NULL; |
91 | 0 | OpenAPI_lnode_t *node = NULL; |
92 | 0 | cJSON *orig_headers = NULL; |
93 | 0 | OpenAPI_list_t *orig_headersList = NULL; |
94 | 0 | cJSON *request_headers = NULL; |
95 | 0 | OpenAPI_list_t *request_headersList = NULL; |
96 | 0 | orig_headers = cJSON_GetObjectItemCaseSensitive(context_infoJSON, "origHeaders"); |
97 | 0 | if (orig_headers) { |
98 | 0 | cJSON *orig_headers_local = NULL; |
99 | 0 | if (!cJSON_IsArray(orig_headers)) { |
100 | 0 | ogs_error("OpenAPI_context_info_parseFromJSON() failed [orig_headers]"); |
101 | 0 | goto end; |
102 | 0 | } |
103 | | |
104 | 0 | orig_headersList = OpenAPI_list_create(); |
105 | |
|
106 | 0 | cJSON_ArrayForEach(orig_headers_local, orig_headers) { |
107 | 0 | double *localDouble = NULL; |
108 | 0 | int *localInt = NULL; |
109 | 0 | if (!cJSON_IsString(orig_headers_local)) { |
110 | 0 | ogs_error("OpenAPI_context_info_parseFromJSON() failed [orig_headers]"); |
111 | 0 | goto end; |
112 | 0 | } |
113 | 0 | OpenAPI_list_add(orig_headersList, ogs_strdup(orig_headers_local->valuestring)); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | request_headers = cJSON_GetObjectItemCaseSensitive(context_infoJSON, "requestHeaders"); |
118 | 0 | if (request_headers) { |
119 | 0 | cJSON *request_headers_local = NULL; |
120 | 0 | if (!cJSON_IsArray(request_headers)) { |
121 | 0 | ogs_error("OpenAPI_context_info_parseFromJSON() failed [request_headers]"); |
122 | 0 | goto end; |
123 | 0 | } |
124 | | |
125 | 0 | request_headersList = OpenAPI_list_create(); |
126 | |
|
127 | 0 | cJSON_ArrayForEach(request_headers_local, request_headers) { |
128 | 0 | double *localDouble = NULL; |
129 | 0 | int *localInt = NULL; |
130 | 0 | if (!cJSON_IsString(request_headers_local)) { |
131 | 0 | ogs_error("OpenAPI_context_info_parseFromJSON() failed [request_headers]"); |
132 | 0 | goto end; |
133 | 0 | } |
134 | 0 | OpenAPI_list_add(request_headersList, ogs_strdup(request_headers_local->valuestring)); |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | 0 | context_info_local_var = OpenAPI_context_info_create ( |
139 | 0 | orig_headers ? orig_headersList : NULL, |
140 | 0 | request_headers ? request_headersList : NULL |
141 | 0 | ); |
142 | |
|
143 | 0 | return context_info_local_var; |
144 | 0 | end: |
145 | 0 | if (orig_headersList) { |
146 | 0 | OpenAPI_list_for_each(orig_headersList, node) { |
147 | 0 | ogs_free(node->data); |
148 | 0 | } |
149 | 0 | OpenAPI_list_free(orig_headersList); |
150 | 0 | orig_headersList = NULL; |
151 | 0 | } |
152 | 0 | if (request_headersList) { |
153 | 0 | OpenAPI_list_for_each(request_headersList, node) { |
154 | 0 | ogs_free(node->data); |
155 | 0 | } |
156 | 0 | OpenAPI_list_free(request_headersList); |
157 | 0 | request_headersList = NULL; |
158 | 0 | } |
159 | 0 | return NULL; |
160 | 0 | } |
161 | | |
162 | | OpenAPI_context_info_t *OpenAPI_context_info_copy(OpenAPI_context_info_t *dst, OpenAPI_context_info_t *src) |
163 | 0 | { |
164 | 0 | cJSON *item = NULL; |
165 | 0 | char *content = NULL; |
166 | |
|
167 | 0 | ogs_assert(src); |
168 | 0 | item = OpenAPI_context_info_convertToJSON(src); |
169 | 0 | if (!item) { |
170 | 0 | ogs_error("OpenAPI_context_info_convertToJSON() failed"); |
171 | 0 | return NULL; |
172 | 0 | } |
173 | | |
174 | 0 | content = cJSON_Print(item); |
175 | 0 | cJSON_Delete(item); |
176 | |
|
177 | 0 | if (!content) { |
178 | 0 | ogs_error("cJSON_Print() failed"); |
179 | 0 | return NULL; |
180 | 0 | } |
181 | | |
182 | 0 | item = cJSON_Parse(content); |
183 | 0 | ogs_free(content); |
184 | 0 | if (!item) { |
185 | 0 | ogs_error("cJSON_Parse() failed"); |
186 | 0 | return NULL; |
187 | 0 | } |
188 | | |
189 | 0 | OpenAPI_context_info_free(dst); |
190 | 0 | dst = OpenAPI_context_info_parseFromJSON(item); |
191 | 0 | cJSON_Delete(item); |
192 | |
|
193 | 0 | return dst; |
194 | 0 | } |
195 | | |