blob: fe9f5fe3d357b3d2a04996bf960227785c26cf1b [file] [log] [blame]
Julien Boeufcd9b1c82015-02-20 17:40:41 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Julien Boeufcd9b1c82015-02-20 17:40:41 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Boeufcd9b1c82015-02-20 17:40:41 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Julien Boeufcd9b1c82015-02-20 17:40:41 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Boeufcd9b1c82015-02-20 17:40:41 -080016 *
17 */
18
Alexander Polcyndb3e8982018-02-21 16:59:24 -080019#include <grpc/support/port_platform.h>
20
Julien Boeuf8ca294e2016-05-02 14:56:30 -070021#include "src/core/lib/security/util/json_util.h"
Julien Boeufcd9b1c82015-02-20 17:40:41 -080022
Julien Boeuf8ca294e2016-05-02 14:56:30 -070023#include <string.h>
Julien Boeufcd9b1c82015-02-20 17:40:41 -080024
Julien Boeufcd9b1c82015-02-20 17:40:41 -080025#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070026#include <grpc/support/string_util.h>
Julien Boeufcd9b1c82015-02-20 17:40:41 -080027
Craig Tillerbaa14a92017-11-03 09:09:36 -070028const char* grpc_json_get_string_property(const grpc_json* json,
29 const char* prop_name) {
30 grpc_json* child;
Craig Tiller4782d922017-11-10 09:53:21 -080031 for (child = json->child; child != nullptr; child = child->next) {
ncteisen1b83d322018-06-06 16:55:42 -070032 if (child->key == nullptr) {
33 gpr_log(GPR_ERROR, "Invalid (null) JSON key encountered");
34 return nullptr;
35 }
Julien Boeuf8ca294e2016-05-02 14:56:30 -070036 if (strcmp(child->key, prop_name) == 0) break;
37 }
Craig Tiller4782d922017-11-10 09:53:21 -080038 if (child == nullptr || child->type != GRPC_JSON_STRING) {
Julien Boeuf8ca294e2016-05-02 14:56:30 -070039 gpr_log(GPR_ERROR, "Invalid or missing %s property.", prop_name);
Craig Tiller4782d922017-11-10 09:53:21 -080040 return nullptr;
Julien Boeufcd9b1c82015-02-20 17:40:41 -080041 }
Julien Boeuf8ca294e2016-05-02 14:56:30 -070042 return child->value;
Julien Boeufcd9b1c82015-02-20 17:40:41 -080043}
44
Craig Tillerbaa14a92017-11-03 09:09:36 -070045bool 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 Tiller4782d922017-11-10 09:53:21 -080049 if (prop_value == nullptr) return false;
Julien Boeuf8ca294e2016-05-02 14:56:30 -070050 *copied_value = gpr_strdup(prop_value);
51 return true;
52}