Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/httpapi_tirtos.c

raw
1 // Copyright (c) Texas Instruments. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include <ti/net/http/httpcli.h>
9
10 #include "azure_c_shared_utility/httpapi.h"
11 #include "azure_c_shared_utility/strings.h"
12 #include "azure_c_shared_utility/xlogging.h"
13 #include "azure_c_shared_utility/safe_math.h"
14
15 #define CONTENT_BUF_LEN 128
16
17 static const char* getHttpMethod(HTTPAPI_REQUEST_TYPE requestType)
18 {
19 switch (requestType) {
20 case HTTPAPI_REQUEST_GET:
21 return (HTTPStd_GET);
22 case HTTPAPI_REQUEST_POST:
23 return (HTTPStd_POST);
24 case HTTPAPI_REQUEST_PUT:
25 return (HTTPStd_PUT);
26 case HTTPAPI_REQUEST_DELETE:
27 return (HTTPStd_DELETE);
28 case HTTPAPI_REQUEST_PATCH:
29 return (HTTPStd_PATCH);
30 default:
31 return (NULL);
32 }
33 }
34
35 static int splitHeader(char *headerName, char **headerValue)
36 {
37 *headerValue = strchr(headerName, ':');
38 if (*headerValue == NULL) {
39 return (-1);
40 }
41
42 **headerValue = '\0';
43 (*headerValue)++;
44 while (**headerValue == ' ') {
45 (*headerValue)++;
46 }
47
48 return (0);
49 }
50
51 HTTPAPI_RESULT HTTPAPI_Init(void)
52 {
53 return (HTTPAPI_OK);
54 }
55
56 void HTTPAPI_Deinit(void)
57 {
58 }
59
60 HTTP_HANDLE HTTPAPI_CreateConnection(const char* hostName)
61 {
62 int ret;
63 struct sockaddr addr;
64 HTTPCli_Handle cli;
65
66 ret = HTTPCli_initSockAddr(&addr, hostName, 0);
67 if (ret < 0) {
68 LogError("HTTPCli_initSockAddr failed, ret=%d", ret);
69 return (NULL);
70 }
71 ((struct sockaddr_in *) (&addr))->sin_port = htons(HTTPStd_SECURE_PORT);
72
73 cli = HTTPCli_create();
74 if (cli == NULL) {
75 LogError("HTTPCli_create failed");
76 return (NULL);
77 }
78
79 ret = HTTPCli_connect(cli, &addr, HTTPCli_TYPE_TLS, NULL);
80 if (ret < 0) {
81 LogError("HTTPCli_connect failed, ret=%d", ret);
82 HTTPCli_delete(&cli);
83 return (NULL);
84 }
85
86 return ((HTTP_HANDLE) cli);
87 }
88
89 void HTTPAPI_CloseConnection(HTTP_HANDLE handle)
90 {
91 HTTPCli_Handle cli = (HTTPCli_Handle) handle;
92
93 if (cli) {
94 HTTPCli_disconnect(cli);
95 HTTPCli_delete(&cli);
96 }
97 }
98
99 HTTPAPI_RESULT HTTPAPI_ExecuteRequest(HTTP_HANDLE handle,
100 HTTPAPI_REQUEST_TYPE requestType, const char* relativePath,
101 HTTP_HEADERS_HANDLE httpHeadersHandle, const unsigned char* content,
102 size_t contentLength, unsigned int* statusCode,
103 HTTP_HEADERS_HANDLE responseHeadersHandle,
104 BUFFER_HANDLE responseContent)
105 {
106 HTTPCli_Handle cli = (HTTPCli_Handle) handle;
107 int ret;
108 int offset;
109 size_t cnt;
110 char contentBuf[CONTENT_BUF_LEN] = {0};
111 char *hname;
112 char *hvalue;
113 const char *method;
114 bool moreFlag;
115
116 method = getHttpMethod(requestType);
117
118 if ((cli == NULL) || (method == NULL) || (relativePath == NULL)
119 || (statusCode == NULL) || (responseHeadersHandle == NULL)) {
120 LogError("Invalid arguments: handle=%p, requestType=%d, relativePath=%p, statusCode=%p, responseHeadersHandle=%p",
121 handle, (int)requestType, relativePath, statusCode, responseHeadersHandle);
122 return (HTTPAPI_INVALID_ARG);
123 }
124 else if (HTTPHeaders_GetHeaderCount(httpHeadersHandle, &cnt)
125 != HTTP_HEADERS_OK) {
126 LogError("Cannot get header count");
127 return (HTTPAPI_QUERY_HEADERS_FAILED);
128 }
129
130 /* Send the request line */
131 ret = HTTPCli_sendRequest(cli, method, relativePath, true);
132 if (ret < 0) {
133 LogError("HTTPCli_sendRequest failed, ret=%d", ret);
134 return (HTTPAPI_SEND_REQUEST_FAILED);
135 }
136
137 /* Send the request headers */
138 while (cnt--) {
139 ret = HTTPHeaders_GetHeader(httpHeadersHandle, cnt, &hname);
140 if (ret != HTTP_HEADERS_OK) {
141 LogError("Cannot get request header %d", cnt);
142 return (HTTPAPI_QUERY_HEADERS_FAILED);
143 }
144
145 ret = splitHeader(hname, &hvalue);
146
147 if (ret == 0) {
148 ret = HTTPCli_sendField(cli, hname, hvalue, false);
149 }
150
151 free(hname);
152 hname = NULL;
153
154 if (ret < 0) {
155 LogError("HTTP send field failed, ret=%d", ret);
156 return (HTTPAPI_SEND_REQUEST_FAILED);
157 }
158 }
159
160 /* Send the last header and request body */
161 ret = HTTPCli_sendField(cli, NULL, NULL, true);
162 if (ret < 0) {
163 LogError("HTTP send empty field failed, ret=%d", ret);
164 return (HTTPAPI_SEND_REQUEST_FAILED);
165 }
166
167 if (content && contentLength != 0) {
168 ret = HTTPCli_sendRequestBody(cli, (const char *)content,
169 contentLength);
170 if (ret < 0) {
171 LogError("HTTP send request body failed, ret=%d", ret);
172 return (HTTPAPI_SEND_REQUEST_FAILED);
173 }
174 }
175
176 /* Get the response status code */
177 ret = HTTPCli_getResponseStatus(cli);
178 if (ret < 0) {
179 LogError("HTTP receive response failed, ret=%d", ret);
180 return (HTTPAPI_RECEIVE_RESPONSE_FAILED);
181 }
182 *statusCode = (unsigned int)ret;
183
184 /* Get the response headers */
185 hname = NULL;
186 cnt = 0;
187 offset = 0;
188 do {
189 ret = HTTPCli_readResponseHeader(cli, contentBuf, CONTENT_BUF_LEN,
190 &moreFlag);
191 if (ret < 0) {
192 LogError("HTTP read response header failed, ret=%d", ret);
193 ret = HTTPAPI_RECEIVE_RESPONSE_FAILED;
194 goto headersDone;
195 }
196 else if (ret == 0) {
197 /* All headers read */
198 goto headersDone;
199 }
200
201 if (cnt < offset + ret) {
202 size_t malloc_size = safe_add_size_t(offset, ret);
203 if (malloc_size == SIZE_MAX)
204 {
205 LogError("invalid realloc size");
206 hname = NULL;
207 }
208 else
209 {
210 hname = (char*)realloc(hname, malloc_size);
211 }
212
213 if (hname == NULL) {
214 LogError("Failed reallocating memory");
215 ret = HTTPAPI_ALLOC_FAILED;
216 goto headersDone;
217 }
218 cnt = offset + ret;
219 }
220
221 memcpy(hname + offset, contentBuf, ret);
222 offset += ret;
223
224 if (moreFlag) {
225 continue;
226 }
227
228 ret = splitHeader(hname, &hvalue);
229 if (ret < 0) {
230 LogError("HTTP split header failed, ret=%d", ret);
231 ret = HTTPAPI_HTTP_HEADERS_FAILED;
232 goto headersDone;
233 }
234
235 ret = HTTPHeaders_AddHeaderNameValuePair(responseHeadersHandle,
236 hname, hvalue);
237 if (ret != HTTP_HEADERS_OK) {
238 LogError("Adding the response header failed");
239 ret = HTTPAPI_HTTP_HEADERS_FAILED;
240 goto headersDone;
241 }
242 offset = 0;
243 } while (1);
244
245 headersDone:
246 free(hname);
247 hname = NULL;
248 if (ret != 0) {
249 return ((HTTPAPI_RESULT)ret);
250 }
251
252 /* Get response body */
253 if (responseContent != NULL) {
254 offset = 0;
255 cnt = 0;
256
257 do {
258 ret = HTTPCli_readResponseBody(cli, contentBuf, CONTENT_BUF_LEN,
259 &moreFlag);
260
261 if (ret < 0) {
262 LogError("HTTP read response body failed, ret=%d", ret);
263 ret = HTTPAPI_RECEIVE_RESPONSE_FAILED;
264 goto contentDone;
265 }
266
267 if (ret != 0) {
268 cnt = ret;
269 ret = BUFFER_enlarge(responseContent, cnt);
270 if (ret != 0) {
271 LogError("Failed enlarging response buffer");
272 ret = HTTPAPI_ALLOC_FAILED;
273 goto contentDone;
274 }
275
276 ret = BUFFER_content(responseContent,
277 (const unsigned char **)&hname);
278 if (ret != 0) {
279 LogError("Failed getting the response buffer content");
280 ret = HTTPAPI_ALLOC_FAILED;
281 goto contentDone;
282 }
283
284 memcpy(hname + offset, contentBuf, cnt);
285 offset += cnt;
286 }
287 } while (moreFlag);
288
289 contentDone:
290 if (ret < 0) {
291 BUFFER_unbuild(responseContent);
292 return ((HTTPAPI_RESULT)ret);
293 }
294 }
295
296 return (HTTPAPI_OK);
297 }
298
299 HTTPAPI_RESULT HTTPAPI_SetOption(HTTP_HANDLE handle, const char* optionName,
300 const void* value)
301 {
302 return (HTTPAPI_INVALID_ARG);
303 }
304
305 HTTPAPI_RESULT HTTPAPI_CloneOption(const char* optionName, const void* value,
306 const void** savedValue)
307 {
308 return (HTTPAPI_INVALID_ARG);
309 }