1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/security/util/json_util.h"
22
23 #include <string.h>
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27
grpc_json_get_string_property(const grpc_json * json,const char * prop_name)28 const char* grpc_json_get_string_property(const grpc_json* json,
29 const char* prop_name) {
30 grpc_json* child;
31 for (child = json->child; child != nullptr; child = child->next) {
32 if (child->key == nullptr) {
33 gpr_log(GPR_ERROR, "Invalid (null) JSON key encountered");
34 return nullptr;
35 }
36 if (strcmp(child->key, prop_name) == 0) break;
37 }
38 if (child == nullptr || child->type != GRPC_JSON_STRING) {
39 gpr_log(GPR_ERROR, "Invalid or missing %s property.", prop_name);
40 return nullptr;
41 }
42 return child->value;
43 }
44
grpc_copy_json_string_property(const grpc_json * json,const char * prop_name,char ** copied_value)45 bool grpc_copy_json_string_property(const grpc_json* json,
46 const char* prop_name,
47 char** copied_value) {
48 const char* prop_value = grpc_json_get_string_property(json, prop_name);
49 if (prop_value == nullptr) return false;
50 *copied_value = gpr_strdup(prop_value);
51 return true;
52 }
53