| // |
| // Copyright 2015 gRPC authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // |
| |
| #include <grpc/support/port_platform.h> |
| |
| #include "src/core/ext/filters/client_channel/service_config.h" |
| |
| #include <string.h> |
| |
| #include <grpc/impl/codegen/grpc_types.h> |
| #include <grpc/support/alloc.h> |
| #include <grpc/support/log.h> |
| #include <grpc/support/string_util.h> |
| |
| #include "src/core/lib/gpr/string.h" |
| #include "src/core/lib/json/json.h" |
| #include "src/core/lib/slice/slice_hash_table.h" |
| #include "src/core/lib/slice/slice_internal.h" |
| #include "src/core/lib/slice/slice_string_helpers.h" |
| |
| namespace grpc_core { |
| |
| ServiceConfig::ServiceConfigParserList* ServiceConfig::registered_parsers_ = |
| nullptr; |
| |
| RefCountedPtr<ServiceConfig> ServiceConfig::Create(const char* json, |
| grpc_error** error) { |
| UniquePtr<char> service_config_json(gpr_strdup(json)); |
| UniquePtr<char> json_string(gpr_strdup(json)); |
| grpc_json* json_tree = grpc_json_parse_string(json_string.get()); |
| if (json_tree == nullptr) { |
| gpr_log(GPR_INFO, "failed to parse JSON for service config"); |
| return nullptr; |
| } |
| |
| grpc_error* create_error; |
| auto return_value = MakeRefCounted<ServiceConfig>( |
| std::move(service_config_json), std::move(json_string), json_tree, |
| &create_error); |
| if (create_error != GRPC_ERROR_NONE) { |
| if (error != nullptr) { |
| *error = create_error; |
| } else { |
| GRPC_ERROR_UNREF(create_error); |
| } |
| return nullptr; |
| } |
| if (error != nullptr) { |
| *error = GRPC_ERROR_NONE; |
| } |
| return return_value; |
| } |
| |
| ServiceConfig::ServiceConfig(UniquePtr<char> service_config_json, |
| UniquePtr<char> json_string, grpc_json* json_tree, |
| grpc_error** error) |
| : service_config_json_(std::move(service_config_json)), |
| json_string_(std::move(json_string)), |
| json_tree_(json_tree) { |
| if (json_tree->type != GRPC_JSON_OBJECT || json_tree->key != nullptr) { |
| if (error != nullptr) { |
| *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| "Malformed service Config JSON object"); |
| } |
| return; |
| } |
| grpc_error* parser_error = ParseGlobalParams(json_tree); |
| if (parser_error != GRPC_ERROR_NONE) { |
| parser_error = ParsePerMethodParams(json_tree); |
| } |
| if (error != nullptr) { |
| *error = parser_error; |
| } else { |
| GRPC_ERROR_UNREF(parser_error); |
| } |
| } |
| |
| grpc_error* ServiceConfig::ParseGlobalParams(const grpc_json* json_tree) { |
| GPR_DEBUG_ASSERT(json_tree_->type == GRPC_JSON_OBJECT); |
| GPR_DEBUG_ASSERT(json_tree_->key == nullptr); |
| for (size_t i = 0; i < registered_parsers_->size(); i++) { |
| grpc_error* error; |
| auto parsed_obj = |
| (*registered_parsers_)[i].ParseGlobalParams(json_tree, &error); |
| if (error != GRPC_ERROR_NONE) { |
| return error; |
| } |
| parsed_global_service_config_objects_.push_back(std::move(parsed_obj)); |
| } |
| return GRPC_ERROR_NONE; |
| } |
| |
| grpc_error* ServiceConfig::ParseJsonMethodConfigToServiceConfigObjectsTable( |
| const grpc_json* json, |
| SliceHashTable<const ServiceConfigObjectsVector*>::Entry* entries, |
| size_t* idx) { |
| auto objs_vector = MakeUnique<ServiceConfigObjectsVector>(); |
| for (size_t i = 0; i < registered_parsers_->size(); i++) { |
| grpc_error* error; |
| auto parsed_obj = |
| (*registered_parsers_)[i].ParsePerMethodParams(json, &error); |
| if (error != GRPC_ERROR_NONE) { |
| return error; |
| } |
| objs_vector->push_back(std::move(parsed_obj)); |
| } |
| const auto* vector_ptr = objs_vector.get(); |
| service_config_objects_vectors_storage_.push_back(std::move(objs_vector)); |
| // Construct list of paths. |
| InlinedVector<UniquePtr<char>, 10> paths; |
| for (grpc_json* child = json->child; child != nullptr; child = child->next) { |
| if (child->key == nullptr) continue; |
| if (strcmp(child->key, "name") == 0) { |
| if (child->type != GRPC_JSON_ARRAY) { |
| return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| "name should be of type Array"); |
| } |
| for (grpc_json* name = child->child; name != nullptr; name = name->next) { |
| UniquePtr<char> path = ParseJsonMethodName(name); |
| if (path == nullptr) { |
| return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed to parse name"); |
| } |
| paths.push_back(std::move(path)); |
| } |
| } |
| } |
| if (paths.size() == 0) { |
| return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| "No names specified in methodConfig"); |
| } |
| // Add entry for each path. |
| for (size_t i = 0; i < paths.size(); ++i) { |
| entries[*idx].key = grpc_slice_from_copied_string(paths[i].get()); |
| entries[*idx].value = vector_ptr; // Takes a new ref. |
| ++*idx; |
| } |
| return GRPC_ERROR_NONE; |
| } |
| |
| grpc_error* ServiceConfig::ParsePerMethodParams(const grpc_json* json_tree) { |
| GPR_DEBUG_ASSERT(json_tree_->type == GRPC_JSON_OBJECT); |
| GPR_DEBUG_ASSERT(json_tree_->key == nullptr); |
| SliceHashTable<const ServiceConfigObjectsVector*>::Entry* entries = nullptr; |
| size_t num_entries = 0; |
| for (grpc_json* field = json_tree->child; field != nullptr; |
| field = field->next) { |
| if (field->key == nullptr) { |
| return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Illegal key value - NULL"); |
| } |
| if (strcmp(field->key, "methodConfig") == 0) { |
| if (entries != nullptr) { |
| GPR_ASSERT(false); |
| } |
| if (field->type != GRPC_JSON_ARRAY) { |
| return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| "methodConfig is not of type Array"); |
| } |
| for (grpc_json* method = field->child; method != nullptr; |
| method = method->next) { |
| int count = CountNamesInMethodConfig(method); |
| if (count <= 0) { |
| return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| "No names found in methodConfig"); |
| } |
| num_entries += static_cast<size_t>(count); |
| } |
| entries = static_cast< |
| SliceHashTable<const ServiceConfigObjectsVector*>::Entry*>(gpr_zalloc( |
| num_entries * |
| sizeof(SliceHashTable<const ServiceConfigObjectsVector*>::Entry))); |
| size_t idx = 0; |
| for (grpc_json* method = field->child; method != nullptr; |
| method = method->next) { |
| grpc_error* error = ParseJsonMethodConfigToServiceConfigObjectsTable( |
| method, entries, &idx); |
| if (error != GRPC_ERROR_NONE) { |
| for (size_t i = 0; i < idx; ++i) { |
| grpc_slice_unref_internal(entries[i].key); |
| } |
| gpr_free(entries); |
| return error; |
| } |
| } |
| GPR_DEBUG_ASSERT(idx == num_entries); |
| break; |
| } |
| } |
| if (entries != nullptr) { |
| parsed_method_service_config_objects_table_ = |
| SliceHashTable<const ServiceConfigObjectsVector*>::Create( |
| num_entries, entries, nullptr); |
| gpr_free(entries); |
| } |
| return GRPC_ERROR_NONE; |
| } |
| |
| ServiceConfig::~ServiceConfig() { grpc_json_destroy(json_tree_); } |
| |
| const char* ServiceConfig::GetLoadBalancingPolicyName() const { |
| if (json_tree_->type != GRPC_JSON_OBJECT || json_tree_->key != nullptr) { |
| return nullptr; |
| } |
| const char* lb_policy_name = nullptr; |
| for (grpc_json* field = json_tree_->child; field != nullptr; |
| field = field->next) { |
| if (field->key == nullptr) return nullptr; |
| if (strcmp(field->key, "loadBalancingPolicy") == 0) { |
| if (lb_policy_name != nullptr) return nullptr; // Duplicate. |
| if (field->type != GRPC_JSON_STRING) return nullptr; |
| lb_policy_name = field->value; |
| } |
| } |
| return lb_policy_name; |
| } |
| |
| int ServiceConfig::CountNamesInMethodConfig(grpc_json* json) { |
| int num_names = 0; |
| for (grpc_json* field = json->child; field != nullptr; field = field->next) { |
| if (field->key != nullptr && strcmp(field->key, "name") == 0) { |
| if (field->type != GRPC_JSON_ARRAY) return -1; |
| for (grpc_json* name = field->child; name != nullptr; name = name->next) { |
| if (name->type != GRPC_JSON_OBJECT) return -1; |
| ++num_names; |
| } |
| } |
| } |
| return num_names; |
| } |
| |
| UniquePtr<char> ServiceConfig::ParseJsonMethodName(grpc_json* json) { |
| if (json->type != GRPC_JSON_OBJECT) return nullptr; |
| const char* service_name = nullptr; |
| const char* method_name = nullptr; |
| for (grpc_json* child = json->child; child != nullptr; child = child->next) { |
| if (child->key == nullptr) return nullptr; |
| if (child->type != GRPC_JSON_STRING) return nullptr; |
| if (strcmp(child->key, "service") == 0) { |
| if (service_name != nullptr) return nullptr; // Duplicate. |
| if (child->value == nullptr) return nullptr; |
| service_name = child->value; |
| } else if (strcmp(child->key, "method") == 0) { |
| if (method_name != nullptr) return nullptr; // Duplicate. |
| if (child->value == nullptr) return nullptr; |
| method_name = child->value; |
| } |
| } |
| if (service_name == nullptr) return nullptr; // Required field. |
| char* path; |
| gpr_asprintf(&path, "/%s/%s", service_name, |
| method_name == nullptr ? "*" : method_name); |
| return UniquePtr<char>(path); |
| } |
| |
| const ServiceConfig::ServiceConfigObjectsVector* const* |
| ServiceConfig::GetMethodServiceConfigObjectsVector(const grpc_slice& path) { |
| const auto* value = parsed_method_service_config_objects_table_->Get(path); |
| // If we didn't find a match for the path, try looking for a wildcard |
| // entry (i.e., change "/service/method" to "/service/*"). |
| if (value == nullptr) { |
| char* path_str = grpc_slice_to_c_string(path); |
| const char* sep = strrchr(path_str, '/') + 1; |
| const size_t len = (size_t)(sep - path_str); |
| char* buf = (char*)gpr_malloc(len + 2); // '*' and NUL |
| memcpy(buf, path_str, len); |
| buf[len] = '*'; |
| buf[len + 1] = '\0'; |
| grpc_slice wildcard_path = grpc_slice_from_copied_string(buf); |
| gpr_free(buf); |
| value = parsed_method_service_config_objects_table_->Get(wildcard_path); |
| grpc_slice_unref_internal(wildcard_path); |
| gpr_free(path_str); |
| if (value == nullptr) return nullptr; |
| } |
| return value; |
| } |
| |
| } // namespace grpc_core |