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 <stdlib.h>
|
---|
5 | #include <limits.h>
|
---|
6 |
|
---|
7 | #include "azure_c_shared_utility/gballoc.h"
|
---|
8 | #include "azure_c_shared_utility/xlogging.h"
|
---|
9 | #include "azure_c_shared_utility/crt_abstractions.h"
|
---|
10 | #include "azure_c_shared_utility/strings.h"
|
---|
11 | #include "parson.h"
|
---|
12 |
|
---|
13 | #include "methodreturn.h"
|
---|
14 |
|
---|
15 | typedef struct METHODRETURN_HANDLE_DATA_TAG
|
---|
16 | {
|
---|
17 | METHODRETURN_DATA data;
|
---|
18 | }METHODRETURN_HANDLE_DATA;
|
---|
19 |
|
---|
20 | bool is_json_present_and_unparsable(const char* jsonValue)
|
---|
21 | {
|
---|
22 | bool is_present_and_unparsable;
|
---|
23 | if (jsonValue == NULL)
|
---|
24 | {
|
---|
25 | // Null json is not considered invalid here
|
---|
26 | is_present_and_unparsable = false;
|
---|
27 | }
|
---|
28 | else
|
---|
29 | {
|
---|
30 | JSON_Value* temp = json_parse_string(jsonValue);
|
---|
31 | if (temp == NULL)
|
---|
32 | {
|
---|
33 | is_present_and_unparsable = true;
|
---|
34 | }
|
---|
35 | else
|
---|
36 | {
|
---|
37 | json_value_free(temp);
|
---|
38 | is_present_and_unparsable = false;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | return is_present_and_unparsable;
|
---|
42 | }
|
---|
43 |
|
---|
44 | METHODRETURN_HANDLE MethodReturn_Create(int statusCode, const char* jsonValue)
|
---|
45 | {
|
---|
46 | METHODRETURN_HANDLE result;
|
---|
47 | /*Codes_SRS_METHODRETURN_02_009: [ If jsonValue is not a JSON value then MethodReturn_Create shall fail and return NULL. ]*/
|
---|
48 | if (is_json_present_and_unparsable(jsonValue))
|
---|
49 | {
|
---|
50 | LogError("%s is not JSON", jsonValue);
|
---|
51 | result = NULL;
|
---|
52 | }
|
---|
53 | else
|
---|
54 | {
|
---|
55 | result = (METHODRETURN_HANDLE_DATA*)calloc(1, sizeof(METHODRETURN_HANDLE_DATA));
|
---|
56 | if (result == NULL)
|
---|
57 | {
|
---|
58 | /*Codes_SRS_METHODRETURN_02_002: [ If any failure is encountered then MethodReturn_Create shall return NULL ]*/
|
---|
59 | LogError("unable to malloc");
|
---|
60 | /*return as is*/
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | if (jsonValue == NULL)
|
---|
65 | {
|
---|
66 | /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/
|
---|
67 | result->data.jsonValue = NULL;
|
---|
68 | result->data.statusCode = statusCode;
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | if (mallocAndStrcpy_s(&(result->data.jsonValue), jsonValue) != 0)
|
---|
73 | {
|
---|
74 | LogError("failure in mallocAndStrcpy_s");
|
---|
75 | free(result);
|
---|
76 | result = NULL;
|
---|
77 | }
|
---|
78 | else
|
---|
79 | {
|
---|
80 | /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/
|
---|
81 | result->data.statusCode = statusCode;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | return result;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void MethodReturn_Destroy(METHODRETURN_HANDLE handle)
|
---|
91 | {
|
---|
92 | if (handle == NULL)
|
---|
93 | {
|
---|
94 | /*Codes_SRS_METHODRETURN_02_003: [ If handle is NULL then MethodReturn_Destroy shall return. ]*/
|
---|
95 | LogError("invalid argument METHODRETURN_HANDLE handle=%p", handle);
|
---|
96 | }
|
---|
97 | else
|
---|
98 | {
|
---|
99 | /*Codes_SRS_METHODRETURN_02_004: [ Otherwise, MethodReturn_Destroy shall free all used resources by handle. ]*/
|
---|
100 | if (handle->data.jsonValue != NULL)
|
---|
101 | {
|
---|
102 | free(handle->data.jsonValue);
|
---|
103 | }
|
---|
104 | free(handle);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | const METHODRETURN_DATA* MethodReturn_GetReturn(METHODRETURN_HANDLE handle)
|
---|
110 | {
|
---|
111 | const METHODRETURN_DATA* result;
|
---|
112 | if (handle == NULL)
|
---|
113 | {
|
---|
114 | /*Codes_SRS_METHODRETURN_02_010: [ If handle is NULL then MethodReturn_GetReturn shall fail and return NULL. ]*/
|
---|
115 | result = NULL;
|
---|
116 | }
|
---|
117 | else
|
---|
118 | {
|
---|
119 | /*Codes_SRS_METHODRETURN_02_011: [ Otherwise, MethodReturn_GetReturn shall return a non-NULL const pointer to a METHODRETURN_DATA. ]*/
|
---|
120 | result = &(handle->data);
|
---|
121 | }
|
---|
122 | return result;
|
---|
123 | }
|
---|