Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Alexander Polcyn | db3e898 | 2018-02-21 16:59:24 -0800 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 21 | #include "src/core/lib/security/util/json_util.h" |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 22 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 23 | #include <string.h> |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 24 | |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 25 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 26 | #include <grpc/support/string_util.h> |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 27 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 28 | const char* grpc_json_get_string_property(const grpc_json* json, |
| 29 | const char* prop_name) { |
| 30 | grpc_json* child; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 31 | for (child = json->child; child != nullptr; child = child->next) { |
ncteisen | 1b83d32 | 2018-06-06 16:55:42 -0700 | [diff] [blame^] | 32 | if (child->key == nullptr) { |
| 33 | gpr_log(GPR_ERROR, "Invalid (null) JSON key encountered"); |
| 34 | return nullptr; |
| 35 | } |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 36 | if (strcmp(child->key, prop_name) == 0) break; |
| 37 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 38 | if (child == nullptr || child->type != GRPC_JSON_STRING) { |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 39 | gpr_log(GPR_ERROR, "Invalid or missing %s property.", prop_name); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 40 | return nullptr; |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 41 | } |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 42 | return child->value; |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 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); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 49 | if (prop_value == nullptr) return false; |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 50 | *copied_value = gpr_strdup(prop_value); |
| 51 | return true; |
| 52 | } |