• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <grpc/support/port_platform.h>
18 
19 #include "src/core/lib/transport/service_config.h"
20 
21 #include <string.h>
22 
23 #include <grpc/impl/codegen/grpc_types.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27 
28 #include "src/core/lib/gpr/string.h"
29 #include "src/core/lib/json/json.h"
30 #include "src/core/lib/slice/slice_hash_table.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/lib/slice/slice_string_helpers.h"
33 
34 namespace grpc_core {
35 
Create(const char * json)36 UniquePtr<ServiceConfig> ServiceConfig::Create(const char* json) {
37   UniquePtr<char> json_string(gpr_strdup(json));
38   grpc_json* json_tree = grpc_json_parse_string(json_string.get());
39   if (json_tree == nullptr) {
40     gpr_log(GPR_INFO, "failed to parse JSON for service config");
41     return nullptr;
42   }
43   return MakeUnique<ServiceConfig>(std::move(json_string), json_tree);
44 }
45 
ServiceConfig(UniquePtr<char> json_string,grpc_json * json_tree)46 ServiceConfig::ServiceConfig(UniquePtr<char> json_string, grpc_json* json_tree)
47     : json_string_(std::move(json_string)), json_tree_(json_tree) {}
48 
~ServiceConfig()49 ServiceConfig::~ServiceConfig() { grpc_json_destroy(json_tree_); }
50 
GetLoadBalancingPolicyName() const51 const char* ServiceConfig::GetLoadBalancingPolicyName() const {
52   if (json_tree_->type != GRPC_JSON_OBJECT || json_tree_->key != nullptr) {
53     return nullptr;
54   }
55   const char* lb_policy_name = nullptr;
56   for (grpc_json* field = json_tree_->child; field != nullptr;
57        field = field->next) {
58     if (field->key == nullptr) return nullptr;
59     if (strcmp(field->key, "loadBalancingPolicy") == 0) {
60       if (lb_policy_name != nullptr) return nullptr;  // Duplicate.
61       if (field->type != GRPC_JSON_STRING) return nullptr;
62       lb_policy_name = field->value;
63     }
64   }
65   return lb_policy_name;
66 }
67 
CountNamesInMethodConfig(grpc_json * json)68 int ServiceConfig::CountNamesInMethodConfig(grpc_json* json) {
69   int num_names = 0;
70   for (grpc_json* field = json->child; field != nullptr; field = field->next) {
71     if (field->key != nullptr && strcmp(field->key, "name") == 0) {
72       if (field->type != GRPC_JSON_ARRAY) return -1;
73       for (grpc_json* name = field->child; name != nullptr; name = name->next) {
74         if (name->type != GRPC_JSON_OBJECT) return -1;
75         ++num_names;
76       }
77     }
78   }
79   return num_names;
80 }
81 
ParseJsonMethodName(grpc_json * json)82 UniquePtr<char> ServiceConfig::ParseJsonMethodName(grpc_json* json) {
83   if (json->type != GRPC_JSON_OBJECT) return nullptr;
84   const char* service_name = nullptr;
85   const char* method_name = nullptr;
86   for (grpc_json* child = json->child; child != nullptr; child = child->next) {
87     if (child->key == nullptr) return nullptr;
88     if (child->type != GRPC_JSON_STRING) return nullptr;
89     if (strcmp(child->key, "service") == 0) {
90       if (service_name != nullptr) return nullptr;  // Duplicate.
91       if (child->value == nullptr) return nullptr;
92       service_name = child->value;
93     } else if (strcmp(child->key, "method") == 0) {
94       if (method_name != nullptr) return nullptr;  // Duplicate.
95       if (child->value == nullptr) return nullptr;
96       method_name = child->value;
97     }
98   }
99   if (service_name == nullptr) return nullptr;  // Required field.
100   char* path;
101   gpr_asprintf(&path, "/%s/%s", service_name,
102                method_name == nullptr ? "*" : method_name);
103   return UniquePtr<char>(path);
104 }
105 
106 }  // namespace grpc_core
107