| 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 <map> |
| 24 |
#include <string> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "absl/strings/str_cat.h" |
| 28 |
|
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
|
| 31 |
#include "src/core/lib/iomgr/error.h" |
| 32 |
|
| 33 |
const char* grpc_json_get_string_property(const grpc_core::Json& json, |
| 34 |
const char* prop_name, |
| 35 |
grpc_error_handle* error) { |
| 36 |
if (json.type() != grpc_core::Json::Type::OBJECT) { |
| 37 |
if (error != nullptr) { |
| 38 |
*error = GRPC_ERROR_CREATE("JSON value is not an object"); |
| 39 |
} |
| 40 |
return nullptr; |
| 41 |
} |
| 42 |
auto it = json.object_value().find(prop_name); |
| 43 |
if (it == json.object_value().end()) { |
| 44 |
if (error != nullptr) { |
| 45 |
*error = GRPC_ERROR_CREATE( |
| 46 |
absl::StrCat("Property ", prop_name, " not found in JSON object.")); |
| 47 |
} |
| 48 |
return nullptr; |
| 49 |
} |
| 50 |
if (it->second.type() != grpc_core::Json::Type::STRING) { |
| 51 |
if (error != nullptr) { |
| 52 |
*error = GRPC_ERROR_CREATE(absl::StrCat( |
| 53 |
"Property ", prop_name, " n JSON object is not a string.")); |
| 54 |
} |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
return it->second.string_value().c_str(); |
| 58 |
} |
| 59 |
|
| 60 |
bool grpc_copy_json_string_property(const grpc_core::Json& json, |
| 61 |
const char* prop_name, |
| 62 |
char** copied_value) { |
| 63 |
grpc_error_handle error; |
| 64 |
const char* prop_value = |
| 65 |
grpc_json_get_string_property(json, prop_name, &error); |
| 66 |
GRPC_LOG_IF_ERROR("Could not copy JSON property", error); |
| 67 |
if (prop_value == nullptr) return false; |
| 68 |
*copied_value = gpr_strdup(prop_value); |
| 69 |
return true; |
| 70 |
} |