source: azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/src/httpapiex.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 29.9 KB
Line 
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#include <stdbool.h>
5#include <stdlib.h>
6#include "azure_macro_utils/macro_utils.h"
7#include "azure_c_shared_utility/gballoc.h"
8#include "azure_c_shared_utility/httpapiex.h"
9#include "azure_c_shared_utility/optimize_size.h"
10#include "azure_c_shared_utility/xlogging.h"
11#include "azure_c_shared_utility/strings.h"
12#include "azure_c_shared_utility/crt_abstractions.h"
13#include "azure_c_shared_utility/vector.h"
14
15typedef struct HTTPAPIEX_SAVED_OPTION_TAG
16{
17 const char* optionName;
18 const void* value;
19}HTTPAPIEX_SAVED_OPTION;
20
21typedef struct HTTPAPIEX_HANDLE_DATA_TAG
22{
23 STRING_HANDLE hostName;
24 int k;
25 HTTP_HANDLE httpHandle;
26 VECTOR_HANDLE savedOptions;
27}HTTPAPIEX_HANDLE_DATA;
28
29MU_DEFINE_ENUM_STRINGS(HTTPAPIEX_RESULT, HTTPAPIEX_RESULT_VALUES);
30
31#define LOG_HTTAPIEX_ERROR() LogError("error code = %" PRI_MU_ENUM "", MU_ENUM_VALUE(HTTPAPIEX_RESULT, result))
32
33static int useGlobalInitialization = 0;
34
35HTTPAPIEX_RESULT HTTPAPIEX_Init(void)
36{
37 HTTPAPIEX_RESULT result;
38
39 /*Codes_SRS_HTTPAPIEX_21_045: [If HTTPAPIEX_Init is calling more than once, it shall initialize the HTTP by calling HTTAPI_Init only once, and return success for all calls.] */
40 if (useGlobalInitialization == 0)
41 {
42 /*Codes_SRS_HTTPAPIEX_21_044: [HTTPAPIEX_Init shall initialize the HTTP by calling HTTAPI_Init.] */
43 if (HTTPAPI_Init() == HTTPAPI_OK)
44 {
45 useGlobalInitialization++;
46 result = HTTPAPIEX_OK;
47 }
48 else
49 {
50 /*Codes_SRS_HTTPAPIEX_21_046: [If HTTAPI_Init, HTTPAPIEX_Init shall return HTTPAPIEX_ERROR.] */
51 result = HTTPAPIEX_ERROR;
52 }
53 }
54 else
55 {
56 useGlobalInitialization++;
57 result = HTTPAPIEX_OK;
58 }
59
60 return result;
61}
62
63void HTTPAPIEX_Deinit(void)
64{
65 /*Codes_SRS_HTTPAPIEX_21_047: [HTTPAPIEX_Deinit shall de-initialize the HTTP by calling HTTAPI_Deinit.] */
66 useGlobalInitialization--;
67 if (useGlobalInitialization == 0)
68 {
69 HTTPAPI_Deinit();
70 }
71}
72
73HTTPAPIEX_HANDLE HTTPAPIEX_Create(const char* hostName)
74{
75 HTTPAPIEX_HANDLE result;
76 /*Codes_SRS_HTTPAPIEX_02_001: [If parameter hostName is NULL then HTTPAPIEX_Create shall return NULL.]*/
77 if (hostName == NULL)
78 {
79 LogError("invalid (NULL) parameter");
80 result = NULL;
81 }
82 else
83 {
84 /*Codes_SRS_HTTPAPIEX_02_005: [If creating the handle fails for any reason, then HTTAPIEX_Create shall return NULL.] */
85 HTTPAPIEX_HANDLE_DATA* handleData = (HTTPAPIEX_HANDLE_DATA*)calloc(1, sizeof(HTTPAPIEX_HANDLE_DATA));
86 if (handleData == NULL)
87 {
88 LogError("malloc failed.");
89 result = NULL;
90 }
91 else
92 {
93 /*Codes_SRS_HTTPAPIEX_02_002: [Parameter hostName shall be saved.]*/
94 handleData->hostName = STRING_construct(hostName);
95 if (handleData->hostName == NULL)
96 {
97 free(handleData);
98 LogError("unable to STRING_construct");
99 result = NULL;
100 }
101 else
102 {
103 /*Codes_SRS_HTTPAPIEX_02_004: [Otherwise, HTTPAPIEX_Create shall return a HTTAPIEX_HANDLE suitable for further calls to the module.] */
104 handleData->savedOptions = VECTOR_create(sizeof(HTTPAPIEX_SAVED_OPTION));
105 if (handleData->savedOptions == NULL)
106 {
107 STRING_delete(handleData->hostName);
108 free(handleData);
109 result = NULL;
110 }
111 else
112 {
113 handleData->k = -1;
114 handleData->httpHandle = NULL;
115 result = handleData;
116 }
117 }
118 }
119 }
120 return result;
121}
122
123/*this function builds the default request http headers if none are specified*/
124/*returns 0 if no error*/
125/*any other code is error*/
126static int buildRequestHttpHeadersHandle(HTTPAPIEX_HANDLE_DATA *handleData, BUFFER_HANDLE requestContent, HTTP_HEADERS_HANDLE originalRequestHttpHeadersHandle, bool* isOriginalRequestHttpHeadersHandle, HTTP_HEADERS_HANDLE* toBeUsedRequestHttpHeadersHandle)
127{
128 int result;
129
130
131 if (originalRequestHttpHeadersHandle != NULL)
132 {
133 *toBeUsedRequestHttpHeadersHandle = originalRequestHttpHeadersHandle;
134 *isOriginalRequestHttpHeadersHandle = true;
135
136 }
137 else
138 {
139 /*Codes_SRS_HTTPAPIEX_02_009: [If parameter requestHttpHeadersHandle is NULL then HTTPAPIEX_ExecuteRequest shall allocate a temporary internal instance of HTTPHEADERS, shall add to that instance the following headers
140 Host:{hostname} - as it was indicated by the call to HTTPAPIEX_Create API call
141 Content-Length:the size of the requestContent parameter, and use this instance to all the subsequent calls to HTTPAPI_ExecuteRequest as parameter httpHeadersHandle.]
142 */
143 *isOriginalRequestHttpHeadersHandle = false;
144 *toBeUsedRequestHttpHeadersHandle = HTTPHeaders_Alloc();
145 }
146
147 if (*toBeUsedRequestHttpHeadersHandle == NULL)
148 {
149 result = MU_FAILURE;
150 LogError("unable to HTTPHeaders_Alloc");
151 }
152 else
153 {
154 char temp[22] = { 0 };
155 (void)size_tToString(temp, 22, BUFFER_length(requestContent)); /*cannot fail, MAX_uint64 has 19 digits*/
156 /*Codes_SRS_HTTPAPIEX_02_011: [If parameter requestHttpHeadersHandle is not NULL then HTTPAPIEX_ExecuteRequest shall create or update the following headers of the request:
157 Host:{hostname}
158 Content-Length:the size of the requestContent parameter, and shall use the so constructed HTTPHEADERS object to all calls to HTTPAPI_ExecuteRequest as parameter httpHeadersHandle.]
159 */
160 /*Codes_SRS_HTTPAPIEX_02_009: [If parameter requestHttpHeadersHandle is NULL then HTTPAPIEX_ExecuteRequest shall allocate a temporary internal instance of HTTPHEADERS, shall add to that instance the following headers
161 Host:{hostname} - as it was indicated by the call to HTTPAPIEX_Create API call
162 Content-Length:the size of the requestContent parameter, and use this instance to all the subsequent calls to HTTPAPI_ExecuteRequest as parameter httpHeadersHandle.]
163 */
164 if (!(
165 (HTTPHeaders_ReplaceHeaderNameValuePair(*toBeUsedRequestHttpHeadersHandle, "Host", STRING_c_str(handleData->hostName)) == HTTP_HEADERS_OK) &&
166 (HTTPHeaders_ReplaceHeaderNameValuePair(*toBeUsedRequestHttpHeadersHandle, "Content-Length", temp) == HTTP_HEADERS_OK)
167 ))
168 {
169 if (! *isOriginalRequestHttpHeadersHandle)
170 {
171 HTTPHeaders_Free(*toBeUsedRequestHttpHeadersHandle);
172 }
173 *toBeUsedRequestHttpHeadersHandle = NULL;
174 result = MU_FAILURE;
175 }
176 else
177 {
178 result = 0;
179 }
180 }
181 return result;
182}
183
184static int buildResponseHttpHeadersHandle(HTTP_HEADERS_HANDLE originalResponsetHttpHeadersHandle, bool* isOriginalResponseHttpHeadersHandle, HTTP_HEADERS_HANDLE* toBeUsedResponsetHttpHeadersHandle)
185{
186 int result;
187 if (originalResponsetHttpHeadersHandle == NULL)
188 {
189 if ((*toBeUsedResponsetHttpHeadersHandle = HTTPHeaders_Alloc()) == NULL)
190 {
191 result = MU_FAILURE;
192 }
193 else
194 {
195 *isOriginalResponseHttpHeadersHandle = false;
196 result = 0;
197 }
198 }
199 else
200 {
201 *isOriginalResponseHttpHeadersHandle = true;
202 *toBeUsedResponsetHttpHeadersHandle = originalResponsetHttpHeadersHandle;
203 result = 0;
204 }
205 return result;
206}
207
208
209static int buildBufferIfNotExist(BUFFER_HANDLE originalRequestContent, bool* isOriginalRequestContent, BUFFER_HANDLE* toBeUsedRequestContent)
210{
211 int result;
212 if (originalRequestContent == NULL)
213 {
214 *toBeUsedRequestContent = BUFFER_new();
215 if (*toBeUsedRequestContent == NULL)
216 {
217 result = MU_FAILURE;
218 }
219 else
220 {
221 *isOriginalRequestContent = false;
222 result = 0;
223 }
224 }
225 else
226 {
227 *isOriginalRequestContent = true;
228 *toBeUsedRequestContent = originalRequestContent;
229 result = 0;
230 }
231 return result;
232}
233
234static unsigned int dummyStatusCode;
235
236static int buildAllRequests(HTTPAPIEX_HANDLE_DATA* handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath,
237 HTTP_HEADERS_HANDLE requestHttpHeadersHandle, BUFFER_HANDLE requestContent, unsigned int* statusCode,
238 HTTP_HEADERS_HANDLE responseHttpHeadersHandle, BUFFER_HANDLE responseContent,
239
240 const char** toBeUsedRelativePath,
241 HTTP_HEADERS_HANDLE *toBeUsedRequestHttpHeadersHandle, bool *isOriginalRequestHttpHeadersHandle,
242 BUFFER_HANDLE *toBeUsedRequestContent, bool *isOriginalRequestContent,
243 unsigned int** toBeUsedStatusCode,
244 HTTP_HEADERS_HANDLE *toBeUsedResponseHttpHeadersHandle, bool *isOriginalResponseHttpHeadersHandle,
245 BUFFER_HANDLE *toBeUsedResponseContent, bool *isOriginalResponseContent)
246{
247 int result;
248 (void)requestType;
249 /*Codes_SRS_HTTPAPIEX_02_013: [If requestContent is NULL then HTTPAPIEX_ExecuteRequest shall behave as if a buffer of zero size would have been used, that is, it shall call HTTPAPI_ExecuteRequest with parameter content = NULL and contentLength = 0.]*/
250 /*Codes_SRS_HTTPAPIEX_02_014: [If requestContent is not NULL then its content and its size shall be used for parameters content and contentLength of HTTPAPI_ExecuteRequest.] */
251 if (buildBufferIfNotExist(requestContent, isOriginalRequestContent, toBeUsedRequestContent) != 0)
252 {
253 LogError("unable to build the request content");
254 result = MU_FAILURE;
255 }
256 else
257 {
258 if (buildRequestHttpHeadersHandle(handle, *toBeUsedRequestContent, requestHttpHeadersHandle, isOriginalRequestHttpHeadersHandle, toBeUsedRequestHttpHeadersHandle) != 0)
259 {
260 /*Codes_SRS_HTTPAPIEX_02_010: [If any of the operations in SRS_HTTAPIEX_02_009 fails, then HTTPAPIEX_ExecuteRequest shall return HTTPAPIEX_ERROR.] */
261 if (*isOriginalRequestContent == false)
262 {
263 BUFFER_delete(*toBeUsedRequestContent);
264 }
265 LogError("unable to build the request http headers handle");
266 result = MU_FAILURE;
267 }
268 else
269 {
270 /*Codes_SRS_HTTPAPIEX_02_008: [If parameter relativePath is NULL then HTTPAPIEX_INVALID_ARG shall not assume a relative path - that is, it will assume an empty path ("").] */
271 if (relativePath == NULL)
272 {
273 *toBeUsedRelativePath = "";
274 }
275 else
276 {
277 *toBeUsedRelativePath = relativePath;
278 }
279
280 /*Codes_SRS_HTTPAPIEX_02_015: [If statusCode is NULL then HTTPAPIEX_ExecuteRequest shall not write in statusCode the HTTP status code, and it will use a temporary internal int for parameter statusCode to the calls of HTTPAPI_ExecuteRequest.] */
281 if (statusCode == NULL)
282 {
283 /*Codes_SRS_HTTPAPIEX_02_016: [If statusCode is not NULL then If statusCode is NULL then HTTPAPIEX_ExecuteRequest shall use it for parameter statusCode to the calls of HTTPAPI_ExecuteRequest.] */
284 *toBeUsedStatusCode = &dummyStatusCode;
285 }
286 else
287 {
288 *toBeUsedStatusCode = statusCode;
289 }
290
291 /*Codes_SRS_HTTPAPIEX_02_017: [If responseHeaders handle is NULL then HTTPAPIEX_ExecuteRequest shall create a temporary internal instance of HTTPHEADERS object and use that for responseHeaders parameter of HTTPAPI_ExecuteRequest call.] */
292 /*Codes_SRS_HTTPAPIEX_02_019: [If responseHeaders is not NULL, then then HTTPAPIEX_ExecuteRequest shall use that object as parameter responseHeaders of HTTPAPI_ExecuteRequest call.] */
293 if (buildResponseHttpHeadersHandle(responseHttpHeadersHandle, isOriginalResponseHttpHeadersHandle, toBeUsedResponseHttpHeadersHandle) != 0)
294 {
295 /*Codes_SRS_HTTPAPIEX_02_018: [If creating the temporary http headers in SRS_HTTPAPIEX_02_017 fails then HTTPAPIEX_ExecuteRequest shall return HTTPAPIEX_ERROR.] */
296 if (*isOriginalRequestContent == false)
297 {
298 BUFFER_delete(*toBeUsedRequestContent);
299 }
300 if (*isOriginalRequestHttpHeadersHandle == false)
301 {
302 HTTPHeaders_Free(*toBeUsedRequestHttpHeadersHandle);
303 }
304 LogError("unable to build response content");
305 result = MU_FAILURE;
306 }
307 else
308 {
309 /*Codes_SRS_HTTPAPIEX_02_020: [If responseContent is NULL then HTTPAPIEX_ExecuteRequest shall create a temporary internal BUFFER object and use that as parameter responseContent of HTTPAPI_ExecuteRequest call.] */
310 /*Codes_SRS_HTTPAPIEX_02_022: [If responseContent is not NULL then HTTPAPIEX_ExecuteRequest use that as parameter responseContent of HTTPAPI_ExecuteRequest call.] */
311 if (buildBufferIfNotExist(responseContent, isOriginalResponseContent, toBeUsedResponseContent) != 0)
312 {
313 /*Codes_SRS_HTTPAPIEX_02_021: [If creating the BUFFER_HANDLE in SRS_HTTPAPIEX_02_020 fails, then HTTPAPIEX_ExecuteRequest shall return HTTPAPIEX_ERROR.] */
314 if (*isOriginalRequestContent == false)
315 {
316 BUFFER_delete(*toBeUsedRequestContent);
317 }
318 if (*isOriginalRequestHttpHeadersHandle == false)
319 {
320 HTTPHeaders_Free(*toBeUsedRequestHttpHeadersHandle);
321 }
322 if (*isOriginalResponseHttpHeadersHandle == false)
323 {
324 HTTPHeaders_Free(*toBeUsedResponseHttpHeadersHandle);
325 }
326 LogError("unable to build response content");
327 result = MU_FAILURE;
328 }
329 else
330 {
331 result = 0;
332 }
333 }
334 }
335 }
336 return result;
337}
338
339HTTPAPIEX_RESULT HTTPAPIEX_ExecuteRequest(HTTPAPIEX_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath,
340 HTTP_HEADERS_HANDLE requestHttpHeadersHandle, BUFFER_HANDLE requestContent, unsigned int* statusCode,
341 HTTP_HEADERS_HANDLE responseHttpHeadersHandle, BUFFER_HANDLE responseContent)
342{
343 HTTPAPIEX_RESULT result;
344 /*Codes_SRS_HTTPAPIEX_02_006: [If parameter handle is NULL then HTTPAPIEX_ExecuteRequest shall fail and return HTTPAPIEX_INVALID_ARG.]*/
345 if (handle == NULL)
346 {
347 result = HTTPAPIEX_INVALID_ARG;
348 LOG_HTTAPIEX_ERROR();
349 }
350 else
351 {
352 /*Codes_SRS_HTTPAPIEX_02_007: [If parameter requestType does not indicate a valid request, HTTPAPIEX_ExecuteRequest shall fail and return HTTPAPIEX_INVALID_ARG.] */
353 if (requestType >= MU_COUNT_ARG(HTTPAPI_REQUEST_TYPE_VALUES))
354 {
355 result = HTTPAPIEX_INVALID_ARG;
356 LOG_HTTAPIEX_ERROR();
357 }
358 else
359 {
360 HTTPAPIEX_HANDLE_DATA *handleData = (HTTPAPIEX_HANDLE_DATA *)handle;
361
362 /*call to buildAll*/
363 const char* toBeUsedRelativePath;
364 HTTP_HEADERS_HANDLE toBeUsedRequestHttpHeadersHandle; bool isOriginalRequestHttpHeadersHandle;
365 BUFFER_HANDLE toBeUsedRequestContent; bool isOriginalRequestContent;
366 unsigned int* toBeUsedStatusCode;
367 HTTP_HEADERS_HANDLE toBeUsedResponseHttpHeadersHandle; bool isOriginalResponseHttpHeadersHandle;
368 BUFFER_HANDLE toBeUsedResponseContent; bool isOriginalResponseContent;
369
370 if (buildAllRequests(handleData, requestType, relativePath, requestHttpHeadersHandle, requestContent, statusCode, responseHttpHeadersHandle, responseContent,
371 &toBeUsedRelativePath,
372 &toBeUsedRequestHttpHeadersHandle, &isOriginalRequestHttpHeadersHandle,
373 &toBeUsedRequestContent, &isOriginalRequestContent,
374 &toBeUsedStatusCode,
375 &toBeUsedResponseHttpHeadersHandle, &isOriginalResponseHttpHeadersHandle,
376 &toBeUsedResponseContent, &isOriginalResponseContent) != 0)
377 {
378 result = HTTPAPIEX_ERROR;
379 LOG_HTTAPIEX_ERROR();
380 }
381 else
382 {
383
384 /*Codes_SRS_HTTPAPIEX_02_023: [HTTPAPIEX_ExecuteRequest shall try to execute the HTTP call by ensuring the following API call sequence is respected:]*/
385 /*Codes_SRS_HTTPAPIEX_02_024: [If any point in the sequence fails, HTTPAPIEX_ExecuteRequest shall attempt to recover by going back to the previous step and retrying that step.]*/
386 /*Codes_SRS_HTTPAPIEX_02_025: [If the first step fails, then the sequence fails.]*/
387 /*Codes_SRS_HTTPAPIEX_02_026: [A step shall be retried at most once.]*/
388 /*Codes_SRS_HTTPAPIEX_02_027: [If a step has been retried then all subsequent steps shall be retried too.]*/
389 bool st[3] = { false, false, false }; /*the three levels of possible failure in resilient send: HTTAPI_Init, HTTPAPI_CreateConnection, HTTPAPI_ExecuteRequest*/
390 if (handleData->k == -1)
391 {
392 handleData->k = 0;
393 }
394
395 do
396 {
397 bool goOn;
398
399 if (handleData->k > 2)
400 {
401 /* error */
402 break;
403 }
404
405 if (st[handleData->k] == true) /*already been tried*/
406 {
407 goOn = false;
408 }
409 else
410 {
411 switch (handleData->k)
412 {
413 case 0:
414 {
415 if (useGlobalInitialization > 0)
416 {
417 /*Codes_SRS_HTTPAPIEX_21_048: [If HTTPAPIEX_Init was called, HTTPAPI_ExecuteRequest shall not call HTTPAPI_Init.] */
418 goOn = true;
419 }
420 else if (HTTPAPI_Init() != HTTPAPI_OK)
421 {
422 goOn = false;
423 }
424 else
425 {
426 goOn = true;
427 }
428 break;
429 }
430 case 1:
431 {
432 if ((handleData->httpHandle = HTTPAPI_CreateConnection(STRING_c_str(handleData->hostName))) == NULL)
433 {
434 goOn = false;
435 }
436 else
437 {
438 size_t i;
439 size_t vectorSize = VECTOR_size(handleData->savedOptions);
440 for (i = 0; i < vectorSize; i++)
441 {
442 /*Codes_SRS_HTTPAPIEX_02_035: [HTTPAPIEX_ExecuteRequest shall pass all the saved options (see HTTPAPIEX_SetOption) to the newly create HTTPAPI_HANDLE in step 2 by calling HTTPAPI_SetOption.]*/
443 /*Codes_SRS_HTTPAPIEX_02_036: [If setting the option fails, then the failure shall be ignored.] */
444 HTTPAPIEX_SAVED_OPTION* option = (HTTPAPIEX_SAVED_OPTION*)VECTOR_element(handleData->savedOptions, i);
445 if (HTTPAPI_SetOption(handleData->httpHandle, option->optionName, option->value) != HTTPAPI_OK)
446 {
447 LogError("HTTPAPI_SetOption failed when called for option %s", option->optionName);
448 }
449 }
450 goOn = true;
451 }
452 break;
453 }
454 case 2:
455 {
456 size_t length = BUFFER_length(toBeUsedRequestContent);
457 unsigned char* buffer = BUFFER_u_char(toBeUsedRequestContent);
458 if (HTTPAPI_ExecuteRequest(handleData->httpHandle, requestType, toBeUsedRelativePath, toBeUsedRequestHttpHeadersHandle, buffer, length, toBeUsedStatusCode, toBeUsedResponseHttpHeadersHandle, toBeUsedResponseContent) != HTTPAPI_OK)
459 {
460 goOn = false;
461 }
462 else
463 {
464 goOn = true;
465 }
466 break;
467 }
468 default:
469 {
470 /*serious error*/
471 goOn = false;
472 break;
473 }
474 }
475 }
476
477 if (goOn)
478 {
479 if (handleData->k == 2)
480 {
481 /*Codes_SRS_HTTPAPIEX_02_028: [HTTPAPIEX_ExecuteRequest shall return HTTPAPIEX_OK when a call to HTTPAPI_ExecuteRequest has been completed successfully.]*/
482 result = HTTPAPIEX_OK;
483 goto out;
484 }
485 else
486 {
487 st[handleData->k] = true;
488 handleData->k++;
489 st[handleData->k] = false;
490 }
491 }
492 else
493 {
494 st[handleData->k] = false;
495 handleData->k--;
496 switch (handleData->k)
497 {
498 case 0:
499 {
500 /*Codes_SRS_HTTPAPIEX_21_049: [If HTTPAPIEX_Init was called, HTTPAPI_ExecuteRequest shall not call HTTPAPI_Deinit.] */
501 if (useGlobalInitialization == 0)
502 {
503 HTTPAPI_Deinit();
504 }
505 break;
506 }
507 case 1:
508 {
509 HTTPAPI_CloseConnection(handleData->httpHandle);
510 handleData->httpHandle = NULL;
511 break;
512 }
513 case 2:
514 {
515 break;
516 }
517 default:
518 {
519 break;
520 }
521 }
522 }
523 } while (handleData->k >= 0);
524 /*Codes_SRS_HTTPAPIEX_02_029: [Otherwise, HTTAPIEX_ExecuteRequest shall return HTTPAPIEX_RECOVERYFAILED.] */
525 result = HTTPAPIEX_RECOVERYFAILED;
526 LogError("unable to recover sending to a working state");
527 out:;
528 /*in all cases, unbuild the temporaries*/
529 if (isOriginalRequestContent == false)
530 {
531 BUFFER_delete(toBeUsedRequestContent);
532 }
533 if (isOriginalRequestHttpHeadersHandle == false)
534 {
535 HTTPHeaders_Free(toBeUsedRequestHttpHeadersHandle);
536 }
537 if (isOriginalResponseContent == false)
538 {
539 BUFFER_delete(toBeUsedResponseContent);
540 }
541 if (isOriginalResponseHttpHeadersHandle == false)
542 {
543 HTTPHeaders_Free(toBeUsedResponseHttpHeadersHandle);
544 }
545 }
546 }
547 }
548 return result;
549}
550
551
552void HTTPAPIEX_Destroy(HTTPAPIEX_HANDLE handle)
553{
554 if (handle != NULL)
555 {
556 /*Codes_SRS_HTTPAPIEX_02_042: [HTTPAPIEX_Destroy shall free all the resources used by HTTAPIEX_HANDLE.]*/
557 size_t i;
558 size_t vectorSize;
559 HTTPAPIEX_HANDLE_DATA* handleData = (HTTPAPIEX_HANDLE_DATA*)handle;
560
561 if (handleData->k == 2)
562 {
563 HTTPAPI_CloseConnection(handleData->httpHandle);
564 /*Codes_SRS_HTTPAPIEX_21_050: [If HTTPAPIEX_Init was called, HTTPAPI_Destroy shall not call HTTPAPI_Deinit.] */
565 if (useGlobalInitialization == 0)
566 {
567 HTTPAPI_Deinit();
568 }
569 }
570 STRING_delete(handleData->hostName);
571
572 vectorSize = VECTOR_size(handleData->savedOptions);
573 for (i = 0; i < vectorSize; i++)
574 {
575 HTTPAPIEX_SAVED_OPTION* savedOption = (HTTPAPIEX_SAVED_OPTION*)VECTOR_element(handleData->savedOptions, i);
576 free((void*)savedOption->optionName);
577 free((void*)savedOption->value);
578 }
579 VECTOR_destroy(handleData->savedOptions);
580
581 free(handle);
582 }
583 else
584 {
585 /*Codes_SRS_HTTPAPIEX_02_043: [If parameter handle is NULL then HTTPAPIEX_Destroy shall take no action.] */
586 }
587}
588
589static bool sameName(const void* element, const void* value)
590{
591 return (strcmp(((HTTPAPIEX_SAVED_OPTION*)element)->optionName, (const char*)value) == 0) ? true : false;
592}
593
594/*return 0 on success, any other value is error*/
595/*obs: value is already cloned at the time of calling this function */
596static int createOrUpdateOption(HTTPAPIEX_HANDLE_DATA* handleData, const char* optionName, const void* value)
597{
598 /*this function is called after the option value has been saved (cloned)*/
599 int result;
600
601 /*decide bwtween update or create*/
602 HTTPAPIEX_SAVED_OPTION* whereIsIt = (HTTPAPIEX_SAVED_OPTION*)VECTOR_find_if(handleData->savedOptions, sameName, optionName);
603 if (whereIsIt != NULL)
604 {
605 free((void*)(whereIsIt->value));
606 whereIsIt->value = value;
607 result = 0;
608 }
609 else
610 {
611 HTTPAPIEX_SAVED_OPTION newOption;
612 if (mallocAndStrcpy_s((char**)&(newOption.optionName), optionName) != 0)
613 {
614 free((void*)value);
615 result = MU_FAILURE;
616 }
617 else
618 {
619 newOption.value = value;
620 if (VECTOR_push_back(handleData->savedOptions, &newOption, 1) != 0)
621 {
622 LogError("unable to VECTOR_push_back");
623 free((void*)newOption.optionName);
624 free((void*)value);
625 result = MU_FAILURE;
626 }
627 else
628 {
629 result = 0;
630 }
631 }
632 }
633
634 return result;
635}
636
637HTTPAPIEX_RESULT HTTPAPIEX_SetOption(HTTPAPIEX_HANDLE handle, const char* optionName, const void* value)
638{
639 HTTPAPIEX_RESULT result;
640 /*Codes_SRS_HTTPAPIEX_02_032: [If parameter handle is NULL then HTTPAPIEX_SetOption shall return HTTPAPIEX_INVALID_ARG.] */
641 /*Codes_SRS_HTTPAPIEX_02_033: [If parameter optionName is NULL then HTTPAPIEX_SetOption shall return HTTPAPIEX_INVALID_ARG.] */
642 /*Codes_SRS_HTTPAPIEX_02_034: [If parameter value is NULL then HTTPAPIEX_SetOption shall return HTTPAPIEX_INVALID_ARG.] */
643 if (
644 (handle == NULL) ||
645 (optionName == NULL) ||
646 (value == NULL)
647 )
648 {
649 result = HTTPAPIEX_INVALID_ARG;
650 LOG_HTTAPIEX_ERROR();
651 }
652 else
653 {
654 const void* savedOption;
655 HTTPAPI_RESULT saveOptionResult;
656
657 /*Codes_SRS_HTTPAPIEX_02_037: [HTTPAPIEX_SetOption shall attempt to save the value of the option by calling HTTPAPI_CloneOption passing optionName and value, irrespective of the existence of a HTTPAPI_HANDLE] */
658 saveOptionResult = HTTPAPI_CloneOption(optionName, value, &savedOption);
659
660 if(saveOptionResult == HTTPAPI_INVALID_ARG)
661 {
662 /*Codes_SRS_HTTPAPIEX_02_038: [If HTTPAPI_CloneOption returns HTTPAPI_INVALID_ARG then HTTPAPIEX shall return HTTPAPIEX_INVALID_ARG.] */
663 result = HTTPAPIEX_INVALID_ARG;
664 LOG_HTTAPIEX_ERROR();
665 }
666 else if (saveOptionResult != HTTPAPI_OK)
667 {
668 /*Codes_SRS_HTTPAPIEX_02_040: [For all other return values of HTTPAPI_SetOption, HTTPIAPIEX_SetOption shall return HTTPAPIEX_ERROR.] */
669 result = HTTPAPIEX_ERROR;
670 LOG_HTTAPIEX_ERROR();
671 }
672 else
673 {
674 HTTPAPIEX_HANDLE_DATA* handleData = (HTTPAPIEX_HANDLE_DATA*)handle;
675 /*Codes_SRS_HTTPAPIEX_02_039: [If HTTPAPI_CloneOption returns HTTPAPI_OK then HTTPAPIEX_SetOption shall create or update the pair optionName/value.]*/
676 if (createOrUpdateOption(handleData, optionName, savedOption) != 0)
677 {
678 /*Codes_SRS_HTTPAPIEX_02_041: [If creating or updating the pair optionName/value fails then shall return HTTPAPIEX_ERROR.] */
679 result = HTTPAPIEX_ERROR;
680 LOG_HTTAPIEX_ERROR();
681
682 }
683 else
684 {
685 /*Codes_SRS_HTTPAPIEX_02_031: [If HTTPAPI_HANDLE exists then HTTPAPIEX_SetOption shall call HTTPAPI_SetOption passing the same optionName and value and shall return a value conforming to the below table:] */
686 if (handleData->httpHandle != NULL)
687 {
688 HTTPAPI_RESULT HTTPAPI_result = HTTPAPI_SetOption(handleData->httpHandle, optionName, value);
689 if (HTTPAPI_result == HTTPAPI_OK)
690 {
691 result = HTTPAPIEX_OK;
692 }
693 else if (HTTPAPI_result == HTTPAPI_INVALID_ARG)
694 {
695 result = HTTPAPIEX_INVALID_ARG;
696 LOG_HTTAPIEX_ERROR();
697 }
698 else
699 {
700 result = HTTPAPIEX_ERROR;
701 LOG_HTTAPIEX_ERROR();
702 }
703 }
704 else
705 {
706 result = HTTPAPIEX_OK;
707 }
708 }
709 }
710 }
711 return result;
712}
Note: See TracBrowser for help on using the repository browser.