From fc6f26c89be961fa464b5103d87289b94e6546b2 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Fri, 24 Apr 2020 15:35:45 -0700 Subject: [PATCH] Remove grpc_core::StringView. --- .../filters/client_channel/backend_metric.cc | 9 +- .../filters/client_channel/client_channel.cc | 9 +- .../ext/filters/client_channel/lb_policy.h | 27 ++-- .../lb_policy/child_policy_handler.cc | 3 +- .../client_channel/lb_policy/grpclb/grpclb.cc | 8 +- .../lb_policy/priority/priority.cc | 7 +- .../weighted_target/weighted_target.cc | 5 +- .../client_channel/lb_policy/xds/cds.cc | 18 +-- .../client_channel/lb_policy/xds/eds.cc | 10 +- .../client_channel/lb_policy/xds/lrs.cc | 6 +- .../lb_policy/xds/xds_routing.cc | 5 +- .../resolver/xds/xds_resolver.cc | 1 - .../client_channel/resolving_lb_policy.cc | 5 +- .../filters/client_channel/service_config.cc | 4 +- .../filters/client_channel/service_config.h | 2 +- .../ext/filters/client_channel/xds/xds_api.cc | 50 ++++--- .../ext/filters/client_channel/xds/xds_api.h | 14 +- .../client_channel/xds/xds_bootstrap.cc | 4 +- .../filters/client_channel/xds/xds_client.cc | 47 ++++--- .../filters/client_channel/xds/xds_client.h | 31 +++-- .../client_channel/xds/xds_client_stats.cc | 10 +- .../client_channel/xds/xds_client_stats.h | 22 +-- src/core/lib/gprpp/host_port.cc | 29 ++-- src/core/lib/gprpp/host_port.h | 10 +- src/core/lib/gprpp/map.h | 3 +- src/core/lib/gprpp/string_view.h | 12 +- .../lib/http/httpcli_security_connector.cc | 5 +- src/core/lib/json/json.h | 5 +- src/core/lib/json/json_reader.cc | 8 +- src/core/lib/json/json_writer.cc | 25 ++-- .../google_default_credentials.cc | 6 +- .../security/credentials/jwt/jwt_verifier.cc | 6 +- .../alts/alts_security_connector.cc | 2 +- .../fake/fake_security_connector.cc | 14 +- .../local/local_security_connector.cc | 2 +- .../security_connector/security_connector.h | 2 +- .../ssl/ssl_security_connector.cc | 8 +- .../security/security_connector/ssl_utils.cc | 24 ++-- .../security/security_connector/ssl_utils.h | 21 +-- .../tls/tls_security_connector.cc | 9 +- .../tls/tls_security_connector.h | 3 +- .../security/transport/client_auth_filter.cc | 3 +- src/core/lib/slice/slice_utils.h | 9 ++ src/core/tsi/ssl_transport_security.cc | 22 +-- src/core/tsi/ssl_transport_security.h | 5 +- test/core/gprpp/string_view_test.cc | 130 +----------------- test/core/json/fuzzer.cc | 2 +- test/core/security/credentials_test.cc | 4 +- test/core/security/json_token_test.cc | 4 +- test/core/slice/slice_test.cc | 8 ++ test/core/util/test_lb_policies.cc | 3 +- test/cpp/end2end/client_lb_end2end_test.cc | 1 + test/cpp/qps/driver.cc | 4 +- 53 files changed, 304 insertions(+), 382 deletions(-) diff --git a/src/core/ext/filters/client_channel/backend_metric.cc b/src/core/ext/filters/client_channel/backend_metric.cc index b36614f5b80..cfb18c172d0 100644 --- a/src/core/ext/filters/client_channel/backend_metric.cc +++ b/src/core/ext/filters/client_channel/backend_metric.cc @@ -18,7 +18,8 @@ #include "src/core/ext/filters/client_channel/backend_metric.h" -#include "src/core/lib/gprpp/string_view.h" +#include "absl/strings/string_view.h" + #include "udpa/data/orca/v1/orca_load_report.upb.h" namespace grpc_core { @@ -26,19 +27,19 @@ namespace grpc_core { namespace { template -std::map ParseMap( +std::map ParseMap( udpa_data_orca_v1_OrcaLoadReport* msg, EntryType** (*entry_func)(udpa_data_orca_v1_OrcaLoadReport*, size_t*), upb_strview (*key_func)(const EntryType*), double (*value_func)(const EntryType*), Arena* arena) { - std::map result; + std::map result; size_t size; const auto* const* entries = entry_func(msg, &size); for (size_t i = 0; i < size; ++i) { upb_strview key_view = key_func(entries[i]); char* key = static_cast(arena->Alloc(key_view.size + 1)); memcpy(key, key_view.data, key_view.size); - result[StringView(key, key_view.size)] = value_func(entries[i]); + result[absl::string_view(key, key_view.size)] = value_func(entries[i]); } return result; } diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index a66a3a615c7..673546e84e2 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -28,6 +28,8 @@ #include +#include "absl/strings/string_view.h" + #include #include #include @@ -372,7 +374,7 @@ class CallData { Metadata(CallData* calld, grpc_metadata_batch* batch) : calld_(calld), batch_(batch) {} - void Add(StringView key, StringView value) override { + void Add(absl::string_view key, absl::string_view value) override { grpc_linked_mdelem* linked_mdelem = static_cast( calld_->arena_->Alloc(sizeof(grpc_linked_mdelem))); linked_mdelem->md = grpc_mdelem_from_slices( @@ -407,7 +409,7 @@ class CallData { reinterpret_cast(handle); return reinterpret_cast(linked_mdelem->next); } - std::pair IteratorHandleGet( + std::pair IteratorHandleGet( intptr_t handle) const override { grpc_linked_mdelem* linked_mdelem = reinterpret_cast(handle); @@ -1306,7 +1308,8 @@ class ChannelData::ClientChannelControlHelper // No-op -- we should never get this from ResolvingLoadBalancingPolicy. void RequestReresolution() override {} - void AddTraceEvent(TraceSeverity severity, StringView message) override { + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override { if (chand_->channelz_node_ != nullptr) { chand_->channelz_node_->AddTraceEvent( ConvertSeverityEnum(severity), diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h index 4f269e9d383..f76708c4e65 100644 --- a/src/core/ext/filters/client_channel/lb_policy.h +++ b/src/core/ext/filters/client_channel/lb_policy.h @@ -24,13 +24,14 @@ #include #include +#include "absl/strings/string_view.h" + #include "src/core/ext/filters/client_channel/server_address.h" #include "src/core/ext/filters/client_channel/service_config.h" #include "src/core/ext/filters/client_channel/subchannel_interface.h" #include "src/core/lib/gprpp/map.h" #include "src/core/lib/gprpp/orphanable.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" -#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/iomgr/polling_entity.h" #include "src/core/lib/iomgr/work_serializer.h" #include "src/core/lib/transport/connectivity_state.h" @@ -93,11 +94,11 @@ class LoadBalancingPolicy : public InternallyRefCounted { /// Application-specific requests cost metrics. Metric names are /// determined by the application. Each value is an absolute cost /// (e.g. 3487 bytes of storage) associated with the request. - std::map request_cost; + std::map request_cost; /// Application-specific resource utilization metrics. Metric names /// are determined by the application. Each value is expressed as a /// fraction of total resources available. - std::map utilization; + std::map utilization; }; /// Interface for accessing per-call state. @@ -123,12 +124,13 @@ class LoadBalancingPolicy : public InternallyRefCounted { class MetadataInterface { public: class iterator - : public std::iterator, // value_type - std::ptrdiff_t, // difference_type - std::pair*, // pointer - std::pair& // reference - > { + : public std::iterator< + std::input_iterator_tag, + std::pair, // value_type + std::ptrdiff_t, // difference_type + std::pair*, // pointer + std::pair& // reference + > { public: iterator(const MetadataInterface* md, intptr_t handle) : md_(md), handle_(handle) {} @@ -155,7 +157,7 @@ class LoadBalancingPolicy : public InternallyRefCounted { /// Implementations must ensure that the key and value remain alive /// until the call ends. If desired, they may be allocated via /// CallState::Alloc(). - virtual void Add(StringView key, StringView value) = 0; + virtual void Add(absl::string_view key, absl::string_view value) = 0; /// Iteration interface. virtual iterator begin() const = 0; @@ -172,7 +174,7 @@ class LoadBalancingPolicy : public InternallyRefCounted { friend class iterator; virtual intptr_t IteratorHandleNext(intptr_t handle) const = 0; - virtual std::pair + virtual std::pair IteratorHandleGet(intptr_t handle) const = 0; }; @@ -276,7 +278,8 @@ class LoadBalancingPolicy : public InternallyRefCounted { /// Adds a trace message associated with the channel. enum TraceSeverity { TRACE_INFO, TRACE_WARNING, TRACE_ERROR }; - virtual void AddTraceEvent(TraceSeverity severity, StringView message) = 0; + virtual void AddTraceEvent(TraceSeverity severity, + absl::string_view message) = 0; }; /// Interface for configuration data used by an LB policy implementation. diff --git a/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc b/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc index 94338ff0ca6..ac5b82e98b8 100644 --- a/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc +++ b/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc @@ -86,7 +86,8 @@ class ChildPolicyHandler::Helper parent_->channel_control_helper()->RequestReresolution(); } - void AddTraceEvent(TraceSeverity severity, StringView message) override { + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override { if (parent_->shutting_down_) return; if (!CalledByPendingChild() && !CalledByCurrentChild()) return; parent_->channel_control_helper()->AddTraceEvent(severity, message); diff --git a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc index 5a7a3840fa7..786332e3902 100644 --- a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +++ b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc @@ -297,7 +297,8 @@ class GrpcLb : public LoadBalancingPolicy { void UpdateState(grpc_connectivity_state state, std::unique_ptr picker) override; void RequestReresolution() override; - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr parent_; @@ -628,7 +629,7 @@ GrpcLb::PickResult GrpcLb::Picker::Pick(PickArgs args) { // how to interpret it. args.initial_metadata->Add( kGrpcLbClientStatsMetadataKey, - StringView(reinterpret_cast(client_stats), 0)); + absl::string_view(reinterpret_cast(client_stats), 0)); // Update calls-started. client_stats->AddCallStarted(); } @@ -721,7 +722,8 @@ void GrpcLb::Helper::RequestReresolution() { } } -void GrpcLb::Helper::AddTraceEvent(TraceSeverity severity, StringView message) { +void GrpcLb::Helper::AddTraceEvent(TraceSeverity severity, + absl::string_view message) { if (parent_->shutting_down_) return; parent_->channel_control_helper()->AddTraceEvent(severity, message); } diff --git a/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc b/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc index 6bcb9bf2231..19fc45f4f87 100644 --- a/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +++ b/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc @@ -152,7 +152,8 @@ class PriorityLb : public LoadBalancingPolicy { void UpdateState(grpc_connectivity_state state, std::unique_ptr picker) override; void RequestReresolution() override; - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr priority_; @@ -730,8 +731,8 @@ void PriorityLb::ChildPriority::Helper::UpdateState( priority_->OnConnectivityStateUpdateLocked(state, std::move(picker)); } -void PriorityLb::ChildPriority::Helper::AddTraceEvent(TraceSeverity severity, - StringView message) { +void PriorityLb::ChildPriority::Helper::AddTraceEvent( + TraceSeverity severity, absl::string_view message) { if (priority_->priority_policy_->shutting_down_) return; priority_->priority_policy_->channel_control_helper()->AddTraceEvent(severity, message); diff --git a/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc b/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc index 5a59dc00226..a0b0fef04a3 100644 --- a/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +++ b/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc @@ -148,7 +148,8 @@ class WeightedTargetLb : public LoadBalancingPolicy { void UpdateState(grpc_connectivity_state state, std::unique_ptr picker) override; void RequestReresolution() override; - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr weighted_child_; @@ -589,7 +590,7 @@ void WeightedTargetLb::WeightedChild::Helper::RequestReresolution() { } void WeightedTargetLb::WeightedChild::Helper::AddTraceEvent( - TraceSeverity severity, StringView message) { + TraceSeverity severity, absl::string_view message) { if (weighted_child_->weighted_target_policy_->shutting_down_) return; weighted_child_->weighted_target_policy_->channel_control_helper() ->AddTraceEvent(severity, message); diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc b/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc index 90e36d1320f..3c1a38a8c0c 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +++ b/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc @@ -79,7 +79,8 @@ class CdsLb : public LoadBalancingPolicy { void UpdateState(grpc_connectivity_state state, std::unique_ptr picker) override; void RequestReresolution() override; - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr parent_; @@ -239,7 +240,8 @@ void CdsLb::Helper::RequestReresolution() { parent_->channel_control_helper()->RequestReresolution(); } -void CdsLb::Helper::AddTraceEvent(TraceSeverity severity, StringView message) { +void CdsLb::Helper::AddTraceEvent(TraceSeverity severity, + absl::string_view message) { if (parent_->shutting_down_) return; parent_->channel_control_helper()->AddTraceEvent(severity, message); } @@ -279,8 +281,7 @@ void CdsLb::ShutdownLocked() { gpr_log(GPR_INFO, "[cdslb %p] cancelling watch for cluster %s", this, config_->cluster().c_str()); } - xds_client_->CancelClusterDataWatch( - StringView(config_->cluster().c_str()), cluster_watcher_); + xds_client_->CancelClusterDataWatch(config_->cluster(), cluster_watcher_); } xds_client_.reset(); } @@ -309,9 +310,9 @@ void CdsLb::UpdateLocked(UpdateArgs args) { gpr_log(GPR_INFO, "[cdslb %p] cancelling watch for cluster %s", this, old_config->cluster().c_str()); } - xds_client_->CancelClusterDataWatch( - StringView(old_config->cluster().c_str()), cluster_watcher_, - /*delay_unsubscription=*/true); + xds_client_->CancelClusterDataWatch(old_config->cluster(), + cluster_watcher_, + /*delay_unsubscription=*/true); } if (GRPC_TRACE_FLAG_ENABLED(grpc_cds_lb_trace)) { gpr_log(GPR_INFO, "[cdslb %p] starting watch for cluster %s", this, @@ -319,8 +320,7 @@ void CdsLb::UpdateLocked(UpdateArgs args) { } auto watcher = absl::make_unique(Ref()); cluster_watcher_ = watcher.get(); - xds_client_->WatchClusterData(StringView(config_->cluster().c_str()), - std::move(watcher)); + xds_client_->WatchClusterData(config_->cluster(), std::move(watcher)); } } diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc b/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc index c4235a6305f..9a159b3f4d0 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc +++ b/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc @@ -137,7 +137,8 @@ class EdsLb : public LoadBalancingPolicy { // This is a no-op, because we get the addresses from the xds // client, which is a watch-based API. void RequestReresolution() override {} - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr eds_policy_; @@ -158,7 +159,7 @@ class EdsLb : public LoadBalancingPolicy { void MaybeUpdateDropPickerLocked(); // Caller must ensure that config_ is set before calling. - const StringView GetEdsResourceName() const { + const absl::string_view GetEdsResourceName() const { if (xds_client_from_channel_ == nullptr) return server_name_; if (!config_->eds_service_name().empty()) { return config_->eds_service_name(); @@ -169,7 +170,7 @@ class EdsLb : public LoadBalancingPolicy { // Returns a pair containing the cluster and eds_service_name to use // for LRS load reporting. // Caller must ensure that config_ is set before calling. - std::pair GetLrsClusterKey() const { + std::pair GetLrsClusterKey() const { if (xds_client_from_channel_ == nullptr) return {server_name_, nullptr}; return {config_->cluster_name(), config_->eds_service_name()}; } @@ -275,7 +276,8 @@ void EdsLb::Helper::UpdateState(grpc_connectivity_state state, eds_policy_->MaybeUpdateDropPickerLocked(); } -void EdsLb::Helper::AddTraceEvent(TraceSeverity severity, StringView message) { +void EdsLb::Helper::AddTraceEvent(TraceSeverity severity, + absl::string_view message) { if (eds_policy_->shutting_down_) return; eds_policy_->channel_control_helper()->AddTraceEvent(severity, message); } diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc b/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc index 30dbd20b9ad..28a9d76c12d 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc +++ b/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc @@ -123,7 +123,8 @@ class LrsLb : public LoadBalancingPolicy { void UpdateState(grpc_connectivity_state state, std::unique_ptr picker) override; void RequestReresolution() override; - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr lrs_policy_; @@ -341,7 +342,8 @@ void LrsLb::Helper::RequestReresolution() { lrs_policy_->channel_control_helper()->RequestReresolution(); } -void LrsLb::Helper::AddTraceEvent(TraceSeverity severity, StringView message) { +void LrsLb::Helper::AddTraceEvent(TraceSeverity severity, + absl::string_view message) { if (lrs_policy_->shutting_down_) return; lrs_policy_->channel_control_helper()->AddTraceEvent(severity, message); } diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc b/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc index b72ac3bab55..226c7ec3e12 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc +++ b/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc @@ -161,7 +161,8 @@ class XdsRoutingLb : public LoadBalancingPolicy { void UpdateState(grpc_connectivity_state state, std::unique_ptr picker) override; void RequestReresolution() override; - void AddTraceEvent(TraceSeverity severity, StringView message) override; + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override; private: RefCountedPtr xds_routing_child_; @@ -579,7 +580,7 @@ void XdsRoutingLb::XdsRoutingChild::Helper::RequestReresolution() { } void XdsRoutingLb::XdsRoutingChild::Helper::AddTraceEvent( - TraceSeverity severity, StringView message) { + TraceSeverity severity, absl::string_view message) { if (xds_routing_child_->xds_routing_policy_->shutting_down_) return; xds_routing_child_->xds_routing_policy_->channel_control_helper() ->AddTraceEvent(severity, message); diff --git a/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc b/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc index c83a5cca2e4..5dec92ba7ef 100644 --- a/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +++ b/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc @@ -20,7 +20,6 @@ #include "src/core/ext/filters/client_channel/resolver_registry.h" #include "src/core/ext/filters/client_channel/xds/xds_client.h" -#include "src/core/lib/gprpp/string_view.h" namespace grpc_core { diff --git a/src/core/ext/filters/client_channel/resolving_lb_policy.cc b/src/core/ext/filters/client_channel/resolving_lb_policy.cc index f0c29ec0183..bc69bae01cc 100644 --- a/src/core/ext/filters/client_channel/resolving_lb_policy.cc +++ b/src/core/ext/filters/client_channel/resolving_lb_policy.cc @@ -126,7 +126,8 @@ class ResolvingLoadBalancingPolicy::ResolvingControlHelper parent_->resolver_->RequestReresolutionLocked(); } - void AddTraceEvent(TraceSeverity severity, StringView message) override { + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override { if (parent_->resolver_ == nullptr) return; // Shutting down. parent_->channel_control_helper()->AddTraceEvent(severity, message); } @@ -287,7 +288,7 @@ void ResolvingLoadBalancingPolicy::ConcatenateAndAddChannelTraceLocked( size_t len = 0; grpc_core::UniquePtr message(gpr_strvec_flatten(&v, &len)); channel_control_helper()->AddTraceEvent(ChannelControlHelper::TRACE_INFO, - StringView(message.get())); + absl::string_view(message.get())); gpr_strvec_destroy(&v); } } diff --git a/src/core/ext/filters/client_channel/service_config.cc b/src/core/ext/filters/client_channel/service_config.cc index 04ef477204b..b2847c20e7b 100644 --- a/src/core/ext/filters/client_channel/service_config.cc +++ b/src/core/ext/filters/client_channel/service_config.cc @@ -40,8 +40,8 @@ typedef absl::InlinedVector, ServiceConfigParserList* g_registered_parsers; } // namespace -RefCountedPtr ServiceConfig::Create(StringView json_string, - grpc_error** error) { +RefCountedPtr ServiceConfig::Create( + absl::string_view json_string, grpc_error** error) { GPR_DEBUG_ASSERT(error != nullptr); Json json = Json::Parse(json_string, error); if (*error != GRPC_ERROR_NONE) return nullptr; diff --git a/src/core/ext/filters/client_channel/service_config.h b/src/core/ext/filters/client_channel/service_config.h index 73157db50d3..2718c93b625 100644 --- a/src/core/ext/filters/client_channel/service_config.h +++ b/src/core/ext/filters/client_channel/service_config.h @@ -129,7 +129,7 @@ class ServiceConfig : public RefCounted { /// Creates a new service config from parsing \a json_string. /// Returns null on parse error. - static RefCountedPtr Create(StringView json_string, + static RefCountedPtr Create(absl::string_view json_string, grpc_error** error); ServiceConfig(std::string json_string, Json json, grpc_error** error); diff --git a/src/core/ext/filters/client_channel/xds/xds_api.cc b/src/core/ext/filters/client_channel/xds/xds_api.cc index 08ba9f925a0..3c977d6152f 100644 --- a/src/core/ext/filters/client_channel/xds/xds_api.cc +++ b/src/core/ext/filters/client_channel/xds/xds_api.cc @@ -31,6 +31,7 @@ #include #include "src/core/ext/filters/client_channel/xds/xds_api.h" +#include "src/core/lib/gpr/useful.h" #include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/sockaddr_utils.h" @@ -514,10 +515,10 @@ grpc_slice XdsApi::CreateRdsRequest(const std::string& route_config_name, return SerializeDiscoveryRequest(arena.ptr(), request); } -grpc_slice XdsApi::CreateCdsRequest(const std::set& cluster_names, - const std::string& version, - const std::string& nonce, grpc_error* error, - bool populate_node) { +grpc_slice XdsApi::CreateCdsRequest( + const std::set& cluster_names, + const std::string& version, const std::string& nonce, grpc_error* error, + bool populate_node) { upb::Arena arena; envoy_api_v2_DiscoveryRequest* request = CreateDiscoveryRequest(arena.ptr(), kCdsTypeUrl, version, nonce, error); @@ -539,8 +540,9 @@ grpc_slice XdsApi::CreateCdsRequest(const std::set& cluster_names, } grpc_slice XdsApi::CreateEdsRequest( - const std::set& eds_service_names, const std::string& version, - const std::string& nonce, grpc_error* error, bool populate_node) { + const std::set& eds_service_names, + const std::string& version, const std::string& nonce, grpc_error* error, + bool populate_node) { upb::Arena arena; envoy_api_v2_DiscoveryRequest* request = CreateDiscoveryRequest(arena.ptr(), kEdsTypeUrl, version, nonce, error); @@ -923,16 +925,18 @@ bool DomainMatch(MatchType match_type, std::string domain_pattern, } else if (match_type == SUFFIX_MATCH) { // Asterisk must match at least one char. if (expected_host_name.size() < domain_pattern.size()) return false; - StringView pattern_suffix(domain_pattern.c_str() + 1); - StringView host_suffix(expected_host_name.c_str() + - expected_host_name.size() - pattern_suffix.size()); + absl::string_view pattern_suffix(domain_pattern.c_str() + 1); + absl::string_view host_suffix(expected_host_name.c_str() + + expected_host_name.size() - + pattern_suffix.size()); return pattern_suffix == host_suffix; } else if (match_type == PREFIX_MATCH) { // Asterisk must match at least one char. if (expected_host_name.size() < domain_pattern.size()) return false; - StringView pattern_prefix(domain_pattern.c_str(), - domain_pattern.size() - 1); - StringView host_prefix(expected_host_name.c_str(), pattern_prefix.size()); + absl::string_view pattern_prefix(domain_pattern.c_str(), + domain_pattern.size() - 1); + absl::string_view host_prefix(expected_host_name.c_str(), + pattern_prefix.size()); return pattern_prefix == host_prefix; } else { return match_type == UNIVERSE_MATCH; @@ -1228,11 +1232,11 @@ grpc_error* RdsResponseParse(XdsClient* client, TraceFlag* tracer, return GRPC_ERROR_NONE; } -grpc_error* CdsResponseParse(XdsClient* client, TraceFlag* tracer, - const envoy_api_v2_DiscoveryResponse* response, - const std::set& expected_cluster_names, - XdsApi::CdsUpdateMap* cds_update_map, - upb_arena* arena) { +grpc_error* CdsResponseParse( + XdsClient* client, TraceFlag* tracer, + const envoy_api_v2_DiscoveryResponse* response, + const std::set& expected_cluster_names, + XdsApi::CdsUpdateMap* cds_update_map, upb_arena* arena) { // Get the resources from the response. size_t size; const google_protobuf_Any* const* resources = @@ -1255,7 +1259,8 @@ grpc_error* CdsResponseParse(XdsClient* client, TraceFlag* tracer, MaybeLogCluster(client, tracer, cluster); // Ignore unexpected cluster names. upb_strview cluster_name = envoy_api_v2_Cluster_name(cluster); - StringView cluster_name_strview(cluster_name.data, cluster_name.size); + absl::string_view cluster_name_strview(cluster_name.data, + cluster_name.size); if (expected_cluster_names.find(cluster_name_strview) == expected_cluster_names.end()) { continue; @@ -1418,7 +1423,7 @@ grpc_error* DropParseAndAppend( grpc_error* EdsResponseParse( XdsClient* client, TraceFlag* tracer, const envoy_api_v2_DiscoveryResponse* response, - const std::set& expected_eds_service_names, + const std::set& expected_eds_service_names, XdsApi::EdsUpdateMap* eds_update_map, upb_arena* arena) { // Get the resources from the response. size_t size; @@ -1447,7 +1452,8 @@ grpc_error* EdsResponseParse( // unexpected names. upb_strview cluster_name = envoy_api_v2_ClusterLoadAssignment_cluster_name( cluster_load_assignment); - StringView cluster_name_strview(cluster_name.data, cluster_name.size); + absl::string_view cluster_name_strview(cluster_name.data, + cluster_name.size); if (expected_eds_service_names.find(cluster_name_strview) == expected_eds_service_names.end()) { continue; @@ -1501,8 +1507,8 @@ grpc_error* XdsApi::ParseAdsResponse( const grpc_slice& encoded_response, const std::string& expected_server_name, const std::string& expected_route_config_name, const bool xds_routing_enabled, - const std::set& expected_cluster_names, - const std::set& expected_eds_service_names, + const std::set& expected_cluster_names, + const std::set& expected_eds_service_names, absl::optional* lds_update, absl::optional* rds_update, CdsUpdateMap* cds_update_map, EdsUpdateMap* eds_update_map, std::string* version, std::string* nonce, diff --git a/src/core/ext/filters/client_channel/xds/xds_api.h b/src/core/ext/filters/client_channel/xds/xds_api.h index 9117466fe84..a2d2d629f69 100644 --- a/src/core/ext/filters/client_channel/xds/xds_api.h +++ b/src/core/ext/filters/client_channel/xds/xds_api.h @@ -239,17 +239,17 @@ class XdsApi { // Creates a CDS request querying \a cluster_names. // Takes ownership of \a error. - grpc_slice CreateCdsRequest(const std::set& cluster_names, + grpc_slice CreateCdsRequest(const std::set& cluster_names, const std::string& version, const std::string& nonce, grpc_error* error, bool populate_node); // Creates an EDS request querying \a eds_service_names. // Takes ownership of \a error. - grpc_slice CreateEdsRequest(const std::set& eds_service_names, - const std::string& version, - const std::string& nonce, grpc_error* error, - bool populate_node); + grpc_slice CreateEdsRequest( + const std::set& eds_service_names, + const std::string& version, const std::string& nonce, grpc_error* error, + bool populate_node); // Parses the ADS response and outputs the validated update for either CDS or // EDS. If the response can't be parsed at the top level, \a type_url will @@ -259,8 +259,8 @@ class XdsApi { const std::string& expected_server_name, const std::string& expected_route_config_name, const bool xds_routing_enabled, - const std::set& expected_cluster_names, - const std::set& expected_eds_service_names, + const std::set& expected_cluster_names, + const std::set& expected_eds_service_names, absl::optional* lds_update, absl::optional* rds_update, CdsUpdateMap* cds_update_map, EdsUpdateMap* eds_update_map, std::string* version, std::string* nonce, diff --git a/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc b/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc index 9caee17a852..0b5c9510b07 100644 --- a/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +++ b/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc @@ -26,6 +26,8 @@ #include #include "src/core/lib/gpr/env.h" +#include "src/core/lib/gpr/string.h" +#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/iomgr/load_file.h" #include "src/core/lib/slice/slice_internal.h" @@ -96,7 +98,7 @@ std::unique_ptr XdsBootstrap::ReadFromFile(XdsClient* client, grpc_slice contents; *error = grpc_load_file(path.get(), /*add_null_terminator=*/true, &contents); if (*error != GRPC_ERROR_NONE) return nullptr; - StringView contents_str_view = StringViewFromSlice(contents); + absl::string_view contents_str_view = StringViewFromSlice(contents); if (GRPC_TRACE_FLAG_ENABLED(*tracer)) { UniquePtr str = StringViewToCString(contents_str_view); gpr_log(GPR_DEBUG, "[xds_client %p] Bootstrap file contents: %s", client, diff --git a/src/core/ext/filters/client_channel/xds/xds_client.cc b/src/core/ext/filters/client_channel/xds/xds_client.cc index 9dc7b97773c..246f5be8e64 100644 --- a/src/core/ext/filters/client_channel/xds/xds_client.cc +++ b/src/core/ext/filters/client_channel/xds/xds_client.cc @@ -49,6 +49,7 @@ #include "src/core/lib/gprpp/memory.h" #include "src/core/lib/gprpp/orphanable.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" +#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/gprpp/sync.h" #include "src/core/lib/iomgr/sockaddr.h" #include "src/core/lib/iomgr/sockaddr_utils.h" @@ -254,8 +255,8 @@ class XdsClient::ChannelState::AdsCallState bool IsCurrentCallOnChannel() const; - std::set ClusterNamesForRequest(); - std::set EdsServiceNamesForRequest(); + std::set ClusterNamesForRequest(); + std::set EdsServiceNamesForRequest(); // The owning RetryableCall<>. RefCountedPtr> parent_; @@ -802,7 +803,7 @@ void XdsClient::ChannelState::AdsCallState::SendMessageLocked( } auto& state = state_map_[type_url]; grpc_slice request_payload_slice; - std::set resource_names; + std::set resource_names; if (type_url == XdsApi::kLdsTypeUrl) { resource_names.insert(xds_client()->server_name_); request_payload_slice = xds_client()->api_.CreateLdsRequest( @@ -1345,9 +1346,9 @@ bool XdsClient::ChannelState::AdsCallState::IsCurrentCallOnChannel() const { return this == chand()->ads_calld_->calld(); } -std::set +std::set XdsClient::ChannelState::AdsCallState::ClusterNamesForRequest() { - std::set cluster_names; + std::set cluster_names; for (auto& p : state_map_[XdsApi::kCdsTypeUrl].subscribed_resources) { cluster_names.insert(p.first); OrphanablePtr& state = p.second; @@ -1356,9 +1357,9 @@ XdsClient::ChannelState::AdsCallState::ClusterNamesForRequest() { return cluster_names; } -std::set +std::set XdsClient::ChannelState::AdsCallState::EdsServiceNamesForRequest() { - std::set eds_names; + std::set eds_names; for (auto& p : state_map_[XdsApi::kEdsTypeUrl].subscribed_resources) { eds_names.insert(p.first); OrphanablePtr& state = p.second; @@ -1800,7 +1801,7 @@ bool GetXdsRoutingEnabled(const grpc_channel_args& args) { XdsClient::XdsClient(std::shared_ptr work_serializer, grpc_pollset_set* interested_parties, - StringView server_name, + absl::string_view server_name, std::unique_ptr watcher, const grpc_channel_args& channel_args, grpc_error** error) : InternallyRefCounted(&grpc_xds_client_trace), @@ -1867,7 +1868,8 @@ void XdsClient::Orphan() { } void XdsClient::WatchClusterData( - StringView cluster_name, std::unique_ptr watcher) { + absl::string_view cluster_name, + std::unique_ptr watcher) { std::string cluster_name_str = std::string(cluster_name); ClusterState& cluster_state = cluster_map_[cluster_name_str]; ClusterWatcherInterface* w = watcher.get(); @@ -1884,7 +1886,7 @@ void XdsClient::WatchClusterData( chand_->Subscribe(XdsApi::kCdsTypeUrl, cluster_name_str); } -void XdsClient::CancelClusterDataWatch(StringView cluster_name, +void XdsClient::CancelClusterDataWatch(absl::string_view cluster_name, ClusterWatcherInterface* watcher, bool delay_unsubscription) { if (shutting_down_) return; @@ -1902,7 +1904,7 @@ void XdsClient::CancelClusterDataWatch(StringView cluster_name, } void XdsClient::WatchEndpointData( - StringView eds_service_name, + absl::string_view eds_service_name, std::unique_ptr watcher) { std::string eds_service_name_str = std::string(eds_service_name); EndpointState& endpoint_state = endpoint_map_[eds_service_name_str]; @@ -1920,7 +1922,7 @@ void XdsClient::WatchEndpointData( chand_->Subscribe(XdsApi::kEdsTypeUrl, eds_service_name_str); } -void XdsClient::CancelEndpointDataWatch(StringView eds_service_name, +void XdsClient::CancelEndpointDataWatch(absl::string_view eds_service_name, EndpointWatcherInterface* watcher, bool delay_unsubscription) { if (shutting_down_) return; @@ -1938,13 +1940,13 @@ void XdsClient::CancelEndpointDataWatch(StringView eds_service_name, } RefCountedPtr XdsClient::AddClusterDropStats( - StringView lrs_server, StringView cluster_name, - StringView eds_service_name) { + absl::string_view lrs_server, absl::string_view cluster_name, + absl::string_view eds_service_name) { // TODO(roth): When we add support for direct federation, use the // server name specified in lrs_server. auto key = std::make_pair(std::string(cluster_name), std::string(eds_service_name)); - // We jump through some hoops here to make sure that the StringViews + // We jump through some hoops here to make sure that the absl::string_views // stored in the XdsClusterDropStats object point to the strings // in the load_report_map_ key, so that they have the same lifetime. auto it = load_report_map_ @@ -1959,8 +1961,9 @@ RefCountedPtr XdsClient::AddClusterDropStats( } void XdsClient::RemoveClusterDropStats( - StringView /*lrs_server*/, StringView cluster_name, - StringView eds_service_name, XdsClusterDropStats* cluster_drop_stats) { + absl::string_view /*lrs_server*/, absl::string_view cluster_name, + absl::string_view eds_service_name, + XdsClusterDropStats* cluster_drop_stats) { auto load_report_it = load_report_map_.find( std::make_pair(std::string(cluster_name), std::string(eds_service_name))); if (load_report_it == load_report_map_.end()) return; @@ -1979,13 +1982,14 @@ void XdsClient::RemoveClusterDropStats( } RefCountedPtr XdsClient::AddClusterLocalityStats( - StringView lrs_server, StringView cluster_name, StringView eds_service_name, + absl::string_view lrs_server, absl::string_view cluster_name, + absl::string_view eds_service_name, RefCountedPtr locality) { // TODO(roth): When we add support for direct federation, use the // server name specified in lrs_server. auto key = std::make_pair(std::string(cluster_name), std::string(eds_service_name)); - // We jump through some hoops here to make sure that the StringViews + // We jump through some hoops here to make sure that the absl::string_views // stored in the XdsClusterLocalityStats object point to the strings // in the load_report_map_ key, so that they have the same lifetime. auto it = load_report_map_ @@ -2002,8 +2006,9 @@ RefCountedPtr XdsClient::AddClusterLocalityStats( } void XdsClient::RemoveClusterLocalityStats( - StringView /*lrs_server*/, StringView cluster_name, - StringView eds_service_name, const RefCountedPtr& locality, + absl::string_view /*lrs_server*/, absl::string_view cluster_name, + absl::string_view eds_service_name, + const RefCountedPtr& locality, XdsClusterLocalityStats* cluster_locality_stats) { auto load_report_it = load_report_map_.find( std::make_pair(std::string(cluster_name), std::string(eds_service_name))); diff --git a/src/core/ext/filters/client_channel/xds/xds_client.h b/src/core/ext/filters/client_channel/xds/xds_client.h index 267a0fe9e19..2b0e3281b86 100644 --- a/src/core/ext/filters/client_channel/xds/xds_client.h +++ b/src/core/ext/filters/client_channel/xds/xds_client.h @@ -21,6 +21,7 @@ #include +#include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "src/core/ext/filters/client_channel/service_config.h" @@ -32,7 +33,6 @@ #include "src/core/lib/gprpp/orphanable.h" #include "src/core/lib/gprpp/ref_counted.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" -#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/iomgr/work_serializer.h" namespace grpc_core { @@ -75,7 +75,7 @@ class XdsClient : public InternallyRefCounted { // If *error is not GRPC_ERROR_NONE after construction, then there was // an error initializing the client. XdsClient(std::shared_ptr work_serializer, - grpc_pollset_set* interested_parties, StringView server_name, + grpc_pollset_set* interested_parties, absl::string_view server_name, std::unique_ptr watcher, const grpc_channel_args& channel_args, grpc_error** error); ~XdsClient(); @@ -89,9 +89,9 @@ class XdsClient : public InternallyRefCounted { // pointer must not be used for any other purpose.) // If the caller is going to start a new watch after cancelling the // old one, it should set delay_unsubscription to true. - void WatchClusterData(StringView cluster_name, + void WatchClusterData(absl::string_view cluster_name, std::unique_ptr watcher); - void CancelClusterDataWatch(StringView cluster_name, + void CancelClusterDataWatch(absl::string_view cluster_name, ClusterWatcherInterface* watcher, bool delay_unsubscription = false); @@ -102,29 +102,30 @@ class XdsClient : public InternallyRefCounted { // pointer must not be used for any other purpose.) // If the caller is going to start a new watch after cancelling the // old one, it should set delay_unsubscription to true. - void WatchEndpointData(StringView eds_service_name, + void WatchEndpointData(absl::string_view eds_service_name, std::unique_ptr watcher); - void CancelEndpointDataWatch(StringView eds_service_name, + void CancelEndpointDataWatch(absl::string_view eds_service_name, EndpointWatcherInterface* watcher, bool delay_unsubscription = false); // Adds and removes drop stats for cluster_name and eds_service_name. RefCountedPtr AddClusterDropStats( - StringView lrs_server, StringView cluster_name, - StringView eds_service_name); - void RemoveClusterDropStats(StringView /*lrs_server*/, - StringView cluster_name, - StringView eds_service_name, + absl::string_view lrs_server, absl::string_view cluster_name, + absl::string_view eds_service_name); + void RemoveClusterDropStats(absl::string_view /*lrs_server*/, + absl::string_view cluster_name, + absl::string_view eds_service_name, XdsClusterDropStats* cluster_drop_stats); // Adds and removes locality stats for cluster_name and eds_service_name // for the specified locality. RefCountedPtr AddClusterLocalityStats( - StringView lrs_server, StringView cluster_name, - StringView eds_service_name, RefCountedPtr locality); + absl::string_view lrs_server, absl::string_view cluster_name, + absl::string_view eds_service_name, + RefCountedPtr locality); void RemoveClusterLocalityStats( - StringView /*lrs_server*/, StringView cluster_name, - StringView eds_service_name, + absl::string_view /*lrs_server*/, absl::string_view cluster_name, + absl::string_view eds_service_name, const RefCountedPtr& locality, XdsClusterLocalityStats* cluster_locality_stats); diff --git a/src/core/ext/filters/client_channel/xds/xds_client_stats.cc b/src/core/ext/filters/client_channel/xds/xds_client_stats.cc index cf115603450..1cfac0a0d30 100644 --- a/src/core/ext/filters/client_channel/xds/xds_client_stats.cc +++ b/src/core/ext/filters/client_channel/xds/xds_client_stats.cc @@ -34,9 +34,9 @@ namespace grpc_core { // XdsClusterDropStats::XdsClusterDropStats(RefCountedPtr xds_client, - StringView lrs_server_name, - StringView cluster_name, - StringView eds_service_name) + absl::string_view lrs_server_name, + absl::string_view cluster_name, + absl::string_view eds_service_name) : xds_client_(std::move(xds_client)), lrs_server_name_(lrs_server_name), cluster_name_(cluster_name), @@ -64,8 +64,8 @@ void XdsClusterDropStats::AddCallDropped(const std::string& category) { // XdsClusterLocalityStats::XdsClusterLocalityStats( - RefCountedPtr xds_client, StringView lrs_server_name, - StringView cluster_name, StringView eds_service_name, + RefCountedPtr xds_client, absl::string_view lrs_server_name, + absl::string_view cluster_name, absl::string_view eds_service_name, RefCountedPtr name) : xds_client_(std::move(xds_client)), lrs_server_name_(lrs_server_name), diff --git a/src/core/ext/filters/client_channel/xds/xds_client_stats.h b/src/core/ext/filters/client_channel/xds/xds_client_stats.h index 7a358d47b91..b977bd9b7df 100644 --- a/src/core/ext/filters/client_channel/xds/xds_client_stats.h +++ b/src/core/ext/filters/client_channel/xds/xds_client_stats.h @@ -99,8 +99,9 @@ class XdsClusterDropStats : public RefCounted { using DroppedRequestsMap = std::map; XdsClusterDropStats(RefCountedPtr xds_client, - StringView lrs_server_name, StringView cluster_name, - StringView eds_service_name); + absl::string_view lrs_server_name, + absl::string_view cluster_name, + absl::string_view eds_service_name); ~XdsClusterDropStats(); // Returns a snapshot of this instance and resets all the counters. @@ -110,9 +111,9 @@ class XdsClusterDropStats : public RefCounted { private: RefCountedPtr xds_client_; - StringView lrs_server_name_; - StringView cluster_name_; - StringView eds_service_name_; + absl::string_view lrs_server_name_; + absl::string_view cluster_name_; + absl::string_view eds_service_name_; // Protects dropped_requests_. A mutex is necessary because the length of // dropped_requests_ can be accessed by both the picker (from data plane // mutex) and the load reporting thread (from the control plane combiner). @@ -170,8 +171,9 @@ class XdsClusterLocalityStats : public RefCounted { }; XdsClusterLocalityStats(RefCountedPtr xds_client, - StringView lrs_server_name, StringView cluster_name, - StringView eds_service_name, + absl::string_view lrs_server_name, + absl::string_view cluster_name, + absl::string_view eds_service_name, RefCountedPtr name); ~XdsClusterLocalityStats(); @@ -183,9 +185,9 @@ class XdsClusterLocalityStats : public RefCounted { private: RefCountedPtr xds_client_; - StringView lrs_server_name_; - StringView cluster_name_; - StringView eds_service_name_; + absl::string_view lrs_server_name_; + absl::string_view cluster_name_; + absl::string_view eds_service_name_; RefCountedPtr name_; Atomic total_successful_requests_{0}; diff --git a/src/core/lib/gprpp/host_port.cc b/src/core/lib/gprpp/host_port.cc index d5aa6e1f64e..4a3efc38b30 100644 --- a/src/core/lib/gprpp/host_port.cc +++ b/src/core/lib/gprpp/host_port.cc @@ -22,6 +22,8 @@ #include +#include "absl/strings/string_view.h" + #include #include #include @@ -45,19 +47,19 @@ int JoinHostPort(grpc_core::UniquePtr* out, const char* host, int port) { } namespace { -bool DoSplitHostPort(StringView name, StringView* host, StringView* port, - bool* has_port) { +bool DoSplitHostPort(absl::string_view name, absl::string_view* host, + absl::string_view* port, bool* has_port) { *has_port = false; if (!name.empty() && name[0] == '[') { /* Parse a bracketed host, typically an IPv6 literal. */ const size_t rbracket = name.find(']', 1); - if (rbracket == grpc_core::StringView::npos) { + if (rbracket == absl::string_view::npos) { /* Unmatched [ */ return false; } if (rbracket == name.size() - 1) { /* ] */ - *port = StringView(); + *port = absl::string_view(); } else if (name[rbracket + 1] == ':') { /* ]: */ *port = name.substr(rbracket + 2, name.size() - rbracket - 2); @@ -67,16 +69,16 @@ bool DoSplitHostPort(StringView name, StringView* host, StringView* port, return false; } *host = name.substr(1, rbracket - 1); - if (host->find(':') == grpc_core::StringView::npos) { + if (host->find(':') == absl::string_view::npos) { /* Require all bracketed hosts to contain a colon, because a hostname or IPv4 address should never use brackets. */ - *host = StringView(); + *host = absl::string_view(); return false; } } else { size_t colon = name.find(':'); - if (colon != grpc_core::StringView::npos && - name.find(':', colon + 1) == grpc_core::StringView::npos) { + if (colon != absl::string_view::npos && + name.find(':', colon + 1) == absl::string_view::npos) { /* Exactly 1 colon. Split into host:port. */ *host = name.substr(0, colon); *port = name.substr(colon + 1, name.size() - colon - 1); @@ -84,24 +86,25 @@ bool DoSplitHostPort(StringView name, StringView* host, StringView* port, } else { /* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ *host = name; - *port = StringView(); + *port = absl::string_view(); } } return true; } } // namespace -bool SplitHostPort(StringView name, StringView* host, StringView* port) { +bool SplitHostPort(absl::string_view name, absl::string_view* host, + absl::string_view* port) { bool unused; return DoSplitHostPort(name, host, port, &unused); } -bool SplitHostPort(StringView name, grpc_core::UniquePtr* host, +bool SplitHostPort(absl::string_view name, grpc_core::UniquePtr* host, grpc_core::UniquePtr* port) { GPR_DEBUG_ASSERT(host != nullptr && *host == nullptr); GPR_DEBUG_ASSERT(port != nullptr && *port == nullptr); - StringView host_view; - StringView port_view; + absl::string_view host_view; + absl::string_view port_view; bool has_port; const bool ret = DoSplitHostPort(name, &host_view, &port_view, &has_port); if (ret) { diff --git a/src/core/lib/gprpp/host_port.h b/src/core/lib/gprpp/host_port.h index c692f99ed13..de397f7f3fa 100644 --- a/src/core/lib/gprpp/host_port.h +++ b/src/core/lib/gprpp/host_port.h @@ -21,8 +21,9 @@ #include +#include "absl/strings/string_view.h" + #include "src/core/lib/gprpp/memory.h" -#include "src/core/lib/gprpp/string_view.h" namespace grpc_core { @@ -41,7 +42,7 @@ int JoinHostPort(grpc_core::UniquePtr* out, const char* host, int port); and port number. There are two variants of this method: - 1) StringView output: port and host are returned as views on name. + 1) absl::string_view output: port and host are returned as views on name. 2) char* output: port and host are copied into newly allocated strings. Prefer variant (1) over (2), because no allocation or copy is performed in @@ -50,8 +51,9 @@ int JoinHostPort(grpc_core::UniquePtr* out, const char* host, int port); Return true on success, false on failure. Guarantees *host and *port are cleared on failure. */ -bool SplitHostPort(StringView name, StringView* host, StringView* port); -bool SplitHostPort(StringView name, grpc_core::UniquePtr* host, +bool SplitHostPort(absl::string_view name, absl::string_view* host, + absl::string_view* port); +bool SplitHostPort(absl::string_view name, grpc_core::UniquePtr* host, grpc_core::UniquePtr* port); } // namespace grpc_core diff --git a/src/core/lib/gprpp/map.h b/src/core/lib/gprpp/map.h index f14f3f6fc84..51778c02892 100644 --- a/src/core/lib/gprpp/map.h +++ b/src/core/lib/gprpp/map.h @@ -38,7 +38,8 @@ struct StringLess { const grpc_core::UniquePtr& b) const { return strcmp(a.get(), b.get()) < 0; } - bool operator()(const StringView& a, const StringView& b) const { + bool operator()(const absl::string_view& a, + const absl::string_view& b) const { const size_t min_size = std::min(a.size(), b.size()); int c = strncmp(a.data(), b.data(), min_size); if (c != 0) return c < 0; diff --git a/src/core/lib/gprpp/string_view.h b/src/core/lib/gprpp/string_view.h index bd4724bc1e9..0b8cf311354 100644 --- a/src/core/lib/gprpp/string_view.h +++ b/src/core/lib/gprpp/string_view.h @@ -37,18 +37,10 @@ namespace grpc_core { -using StringView = absl::string_view; - -// Converts grpc_slice to StringView. -inline absl::string_view StringViewFromSlice(const grpc_slice& slice) { - return absl::string_view( - reinterpret_cast(GRPC_SLICE_START_PTR(slice)), - GRPC_SLICE_LENGTH(slice)); -} - // Creates a dup of the string viewed by this class. // Return value is null-terminated and never nullptr. -inline grpc_core::UniquePtr StringViewToCString(const StringView sv) { +inline grpc_core::UniquePtr StringViewToCString( + const absl::string_view sv) { char* str = static_cast(gpr_malloc(sv.size() + 1)); if (sv.size() > 0) memcpy(str, sv.data(), sv.size()); str[sv.size()] = '\0'; diff --git a/src/core/lib/http/httpcli_security_connector.cc b/src/core/lib/http/httpcli_security_connector.cc index 3d95cdfea9a..92f3c45b0df 100644 --- a/src/core/lib/http/httpcli_security_connector.cc +++ b/src/core/lib/http/httpcli_security_connector.cc @@ -22,6 +22,8 @@ #include +#include "absl/strings/string_view.h" + #include #include #include @@ -30,7 +32,6 @@ #include "src/core/lib/channel/handshaker_registry.h" #include "src/core/lib/gpr/string.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" -#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/iomgr/pollset.h" #include "src/core/lib/security/credentials/credentials.h" #include "src/core/lib/security/security_connector/ssl_utils.h" @@ -111,7 +112,7 @@ class grpc_httpcli_ssl_channel_security_connector final return strcmp(secure_peer_name_, other->secure_peer_name_); } - bool check_call_host(grpc_core::StringView /*host*/, + bool check_call_host(absl::string_view /*host*/, grpc_auth_context* /*auth_context*/, grpc_closure* /*on_call_host_checked*/, grpc_error** error) override { diff --git a/src/core/lib/json/json.h b/src/core/lib/json/json.h index a949ae502b1..8e26a462a62 100644 --- a/src/core/lib/json/json.h +++ b/src/core/lib/json/json.h @@ -27,7 +27,8 @@ #include #include -#include "src/core/lib/gprpp/string_view.h" +#include "absl/strings/string_view.h" + #include "src/core/lib/iomgr/error.h" namespace grpc_core { @@ -55,7 +56,7 @@ class Json { using Array = std::vector; // Parses JSON string from json_str. On error, sets *error. - static Json Parse(StringView json_str, grpc_error** error); + static Json Parse(absl::string_view json_str, grpc_error** error); Json() = default; diff --git a/src/core/lib/json/json_reader.cc b/src/core/lib/json/json_reader.cc index 6a1aaa33687..208e9721db4 100644 --- a/src/core/lib/json/json_reader.cc +++ b/src/core/lib/json/json_reader.cc @@ -34,7 +34,7 @@ namespace { class JsonReader { public: - static grpc_error* Parse(StringView input, Json* output); + static grpc_error* Parse(absl::string_view input, Json* output); private: enum class Status { @@ -80,7 +80,7 @@ class JsonReader { */ static constexpr uint32_t GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0; - explicit JsonReader(StringView input) + explicit JsonReader(absl::string_view input) : original_input_(reinterpret_cast(input.data())), input_(original_input_), remaining_input_(input.size()) {} @@ -815,7 +815,7 @@ JsonReader::Status JsonReader::Run() { GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR); } -grpc_error* JsonReader::Parse(StringView input, Json* output) { +grpc_error* JsonReader::Parse(absl::string_view input, Json* output) { JsonReader reader(input); Status status = reader.Run(); if (reader.truncated_errors_) { @@ -846,7 +846,7 @@ grpc_error* JsonReader::Parse(StringView input, Json* output) { } // namespace -Json Json::Parse(StringView json_str, grpc_error** error) { +Json Json::Parse(absl::string_view json_str, grpc_error** error) { Json value; *error = JsonReader::Parse(json_str, &value); return value; diff --git a/src/core/lib/json/json_writer.cc b/src/core/lib/json/json_writer.cc index 3f371902f88..6235a3d2b4d 100644 --- a/src/core/lib/json/json_writer.cc +++ b/src/core/lib/json/json_writer.cc @@ -21,13 +21,13 @@ #include #include +#include "absl/strings/string_view.h" + #include #include #include "src/core/lib/json/json.h" -#include "src/core/lib/gprpp/string_view.h" - namespace grpc_core { namespace { @@ -52,7 +52,7 @@ class JsonWriter { void OutputCheck(size_t needed); void OutputChar(char c); - void OutputString(const StringView str); + void OutputString(const absl::string_view str); void OutputIndent(); void ValueEnd(); void EscapeUtf16(uint16_t utf16); @@ -92,7 +92,7 @@ void JsonWriter::OutputChar(char c) { output_.push_back(c); } -void JsonWriter::OutputString(const StringView str) { +void JsonWriter::OutputString(const absl::string_view str) { OutputCheck(str.size()); output_.append(str.data(), str.size()); } @@ -110,11 +110,12 @@ void JsonWriter::OutputIndent() { return; } while (spaces >= (sizeof(spacesstr) - 1)) { - OutputString(StringView(spacesstr, sizeof(spacesstr) - 1)); + OutputString(absl::string_view(spacesstr, sizeof(spacesstr) - 1)); spaces -= static_cast(sizeof(spacesstr) - 1); } if (spaces == 0) return; - OutputString(StringView(spacesstr + sizeof(spacesstr) - 1 - spaces, spaces)); + OutputString( + absl::string_view(spacesstr + sizeof(spacesstr) - 1 - spaces, spaces)); } void JsonWriter::ValueEnd() { @@ -131,7 +132,7 @@ void JsonWriter::ValueEnd() { void JsonWriter::EscapeUtf16(uint16_t utf16) { static const char hex[] = "0123456789abcdef"; - OutputString(StringView("\\u", 2)); + OutputString(absl::string_view("\\u", 2)); OutputChar(hex[(utf16 >> 12) & 0x0f]); OutputChar(hex[(utf16 >> 8) & 0x0f]); OutputChar(hex[(utf16 >> 4) & 0x0f]); @@ -150,19 +151,19 @@ void JsonWriter::EscapeString(const std::string& string) { } else if (c < 32 || c == 127) { switch (c) { case '\b': - OutputString(StringView("\\b", 2)); + OutputString(absl::string_view("\\b", 2)); break; case '\f': - OutputString(StringView("\\f", 2)); + OutputString(absl::string_view("\\f", 2)); break; case '\n': - OutputString(StringView("\\n", 2)); + OutputString(absl::string_view("\\n", 2)); break; case '\r': - OutputString(StringView("\\r", 2)); + OutputString(absl::string_view("\\r", 2)); break; case '\t': - OutputString(StringView("\\t", 2)); + OutputString(absl::string_view("\\t", 2)); break; default: EscapeUtf16(c); diff --git a/src/core/lib/security/credentials/google_default/google_default_credentials.cc b/src/core/lib/security/credentials/google_default/google_default_credentials.cc index 58e550ed694..8bf61c221ef 100644 --- a/src/core/lib/security/credentials/google_default/google_default_credentials.cc +++ b/src/core/lib/security/credentials/google_default/google_default_credentials.cc @@ -224,17 +224,13 @@ static grpc_error* create_default_creds_from_path( grpc_slice creds_data = grpc_empty_slice(); grpc_error* error = GRPC_ERROR_NONE; Json json; - grpc_core::StringView str; if (creds_path == nullptr) { error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("creds_path unset"); goto end; } error = grpc_load_file(creds_path, 0, &creds_data); if (error != GRPC_ERROR_NONE) goto end; - str = grpc_core::StringView( - reinterpret_cast(GRPC_SLICE_START_PTR(creds_data)), - GRPC_SLICE_LENGTH(creds_data)); - json = Json::Parse(str, &error); + json = Json::Parse(grpc_core::StringViewFromSlice(creds_data), &error); if (error != GRPC_ERROR_NONE) goto end; if (json.type() != Json::Type::OBJECT) { error = grpc_error_set_str( diff --git a/src/core/lib/security/credentials/jwt/jwt_verifier.cc b/src/core/lib/security/credentials/jwt/jwt_verifier.cc index 1d082b258c7..a2ea9132193 100644 --- a/src/core/lib/security/credentials/jwt/jwt_verifier.cc +++ b/src/core/lib/security/credentials/jwt/jwt_verifier.cc @@ -86,9 +86,7 @@ static Json parse_json_part_from_jwt(const char* str, size_t len) { gpr_log(GPR_ERROR, "Invalid base64."); return Json(); // JSON null } - grpc_core::StringView string( - reinterpret_cast(GRPC_SLICE_START_PTR(slice)), - GRPC_SLICE_LENGTH(slice)); + absl::string_view string = grpc_core::StringViewFromSlice(slice); grpc_error* error = GRPC_ERROR_NONE; Json json = Json::Parse(string, &error); if (error != GRPC_ERROR_NONE) { @@ -418,7 +416,7 @@ static Json json_from_http(const grpc_httpcli_response* response) { } grpc_error* error = GRPC_ERROR_NONE; Json json = Json::Parse( - grpc_core::StringView(response->body, response->body_length), &error); + absl::string_view(response->body, response->body_length), &error); if (error != GRPC_ERROR_NONE) { gpr_log(GPR_ERROR, "Invalid JSON found in response."); return Json(); // JSON null diff --git a/src/core/lib/security/security_connector/alts/alts_security_connector.cc b/src/core/lib/security/security_connector/alts/alts_security_connector.cc index 6bfe6ea74c3..47c357f8fab 100644 --- a/src/core/lib/security/security_connector/alts/alts_security_connector.cc +++ b/src/core/lib/security/security_connector/alts/alts_security_connector.cc @@ -111,7 +111,7 @@ class grpc_alts_channel_security_connector final return strcmp(target_name_, other->target_name_); } - bool check_call_host(grpc_core::StringView host, + bool check_call_host(absl::string_view host, grpc_auth_context* /*auth_context*/, grpc_closure* /*on_call_host_checked*/, grpc_error** error) override { diff --git a/src/core/lib/security/security_connector/fake/fake_security_connector.cc b/src/core/lib/security/security_connector/fake/fake_security_connector.cc index 7a81e68d618..f0f515c53e1 100644 --- a/src/core/lib/security/security_connector/fake/fake_security_connector.cc +++ b/src/core/lib/security/security_connector/fake/fake_security_connector.cc @@ -103,20 +103,20 @@ class grpc_fake_channel_security_connector final tsi_create_fake_handshaker(/*is_client=*/true), this, args)); } - bool check_call_host(grpc_core::StringView host, + bool check_call_host(absl::string_view host, grpc_auth_context* /*auth_context*/, grpc_closure* /*on_call_host_checked*/, grpc_error** /*error*/) override { - grpc_core::StringView authority_hostname; - grpc_core::StringView authority_ignored_port; - grpc_core::StringView target_hostname; - grpc_core::StringView target_ignored_port; + absl::string_view authority_hostname; + absl::string_view authority_ignored_port; + absl::string_view target_hostname; + absl::string_view target_ignored_port; grpc_core::SplitHostPort(host, &authority_hostname, &authority_ignored_port); grpc_core::SplitHostPort(target_, &target_hostname, &target_ignored_port); if (target_name_override_ != nullptr) { - grpc_core::StringView fake_security_target_name_override_hostname; - grpc_core::StringView fake_security_target_name_override_ignored_port; + absl::string_view fake_security_target_name_override_hostname; + absl::string_view fake_security_target_name_override_ignored_port; grpc_core::SplitHostPort( target_name_override_, &fake_security_target_name_override_hostname, &fake_security_target_name_override_ignored_port); diff --git a/src/core/lib/security/security_connector/local/local_security_connector.cc b/src/core/lib/security/security_connector/local/local_security_connector.cc index f2e4e0c36d9..585c170e937 100644 --- a/src/core/lib/security/security_connector/local/local_security_connector.cc +++ b/src/core/lib/security/security_connector/local/local_security_connector.cc @@ -181,7 +181,7 @@ class grpc_local_channel_security_connector final creds->connect_type()); } - bool check_call_host(grpc_core::StringView host, + bool check_call_host(absl::string_view host, grpc_auth_context* /*auth_context*/, grpc_closure* /*on_call_host_checked*/, grpc_error** error) override { diff --git a/src/core/lib/security/security_connector/security_connector.h b/src/core/lib/security/security_connector/security_connector.h index 32033ec5c92..0f1ac64a44f 100644 --- a/src/core/lib/security/security_connector/security_connector.h +++ b/src/core/lib/security/security_connector/security_connector.h @@ -98,7 +98,7 @@ class grpc_channel_security_connector : public grpc_security_connector { /// Returns true if completed synchronously, in which case \a error will /// be set to indicate the result. Otherwise, \a on_call_host_checked /// will be invoked when complete. - virtual bool check_call_host(grpc_core::StringView host, + virtual bool check_call_host(absl::string_view host, grpc_auth_context* auth_context, grpc_closure* on_call_host_checked, grpc_error** error) = 0; diff --git a/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc b/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc index 13180724cdc..54f538c9032 100644 --- a/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +++ b/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc @@ -31,6 +31,7 @@ #include "src/core/lib/gpr/string.h" #include "src/core/lib/gprpp/host_port.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" +#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/security/context/security_context.h" #include "src/core/lib/security/credentials/credentials.h" #include "src/core/lib/security/credentials/ssl/ssl_credentials.h" @@ -76,8 +77,8 @@ class grpc_ssl_channel_security_connector final ? nullptr : gpr_strdup(overridden_target_name)), verify_options_(&config->verify_options) { - grpc_core::StringView host; - grpc_core::StringView port; + absl::string_view host; + absl::string_view port; grpc_core::SplitHostPort(target_name, &host, &port); target_name_ = grpc_core::StringViewToCString(host); } @@ -186,8 +187,7 @@ class grpc_ssl_channel_security_connector final other->overridden_target_name_.get()); } - bool check_call_host(grpc_core::StringView host, - grpc_auth_context* auth_context, + bool check_call_host(absl::string_view host, grpc_auth_context* auth_context, grpc_closure* /*on_call_host_checked*/, grpc_error** error) override { return grpc_ssl_check_call_host( diff --git a/src/core/lib/security/security_connector/ssl_utils.cc b/src/core/lib/security/security_connector/ssl_utils.cc index 2a3f3005ab2..4f043c35c07 100644 --- a/src/core/lib/security/security_connector/ssl_utils.cc +++ b/src/core/lib/security/security_connector/ssl_utils.cc @@ -149,7 +149,7 @@ grpc_error* grpc_ssl_check_alpn(const tsi_peer* peer) { return GRPC_ERROR_NONE; } -grpc_error* grpc_ssl_check_peer_name(grpc_core::StringView peer_name, +grpc_error* grpc_ssl_check_peer_name(absl::string_view peer_name, const tsi_peer* peer) { /* Check the peer name if specified. */ if (!peer_name.empty() && !grpc_ssl_host_matches_name(peer, peer_name)) { @@ -163,9 +163,9 @@ grpc_error* grpc_ssl_check_peer_name(grpc_core::StringView peer_name, return GRPC_ERROR_NONE; } -bool grpc_ssl_check_call_host(grpc_core::StringView host, - grpc_core::StringView target_name, - grpc_core::StringView overridden_target_name, +bool grpc_ssl_check_call_host(absl::string_view host, + absl::string_view target_name, + absl::string_view overridden_target_name, grpc_auth_context* auth_context, grpc_error** error) { grpc_security_status status = GRPC_SECURITY_ERROR; @@ -197,24 +197,24 @@ const char** grpc_fill_alpn_protocol_strings(size_t* num_alpn_protocols) { } int grpc_ssl_host_matches_name(const tsi_peer* peer, - grpc_core::StringView peer_name) { - grpc_core::StringView allocated_name; - grpc_core::StringView ignored_port; + absl::string_view peer_name) { + absl::string_view allocated_name; + absl::string_view ignored_port; grpc_core::SplitHostPort(peer_name, &allocated_name, &ignored_port); if (allocated_name.empty()) return 0; // IPv6 zone-id should not be included in comparisons. const size_t zone_id = allocated_name.find('%'); - if (zone_id != grpc_core::StringView::npos) { + if (zone_id != absl::string_view::npos) { allocated_name.remove_suffix(allocated_name.size() - zone_id); } return tsi_ssl_peer_matches_name(peer, allocated_name); } -int grpc_ssl_cmp_target_name( - grpc_core::StringView target_name, grpc_core::StringView other_target_name, - grpc_core::StringView overridden_target_name, - grpc_core::StringView other_overridden_target_name) { +int grpc_ssl_cmp_target_name(absl::string_view target_name, + absl::string_view other_target_name, + absl::string_view overridden_target_name, + absl::string_view other_overridden_target_name) { int c = target_name.compare(other_target_name); if (c != 0) return c; return overridden_target_name.compare(other_overridden_target_name); diff --git a/src/core/lib/security/security_connector/ssl_utils.h b/src/core/lib/security/security_connector/ssl_utils.h index c521b1f6c60..abbb4e953bf 100644 --- a/src/core/lib/security/security_connector/ssl_utils.h +++ b/src/core/lib/security/security_connector/ssl_utils.h @@ -23,12 +23,13 @@ #include +#include "absl/strings/string_view.h" + #include #include #include "src/core/lib/gprpp/global_config.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" -#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/iomgr/error.h" #include "src/core/lib/security/security_connector/security_connector.h" #include "src/core/lib/security/security_connector/ssl_utils_config.h" @@ -45,17 +46,17 @@ grpc_error* grpc_ssl_check_alpn(const tsi_peer* peer); /* Check peer name information returned from SSL handshakes. */ -grpc_error* grpc_ssl_check_peer_name(grpc_core::StringView peer_name, +grpc_error* grpc_ssl_check_peer_name(absl::string_view peer_name, const tsi_peer* peer); /* Compare targer_name information extracted from SSL security connectors. */ -int grpc_ssl_cmp_target_name( - grpc_core::StringView target_name, grpc_core::StringView other_target_name, - grpc_core::StringView overridden_target_name, - grpc_core::StringView other_overridden_target_name); +int grpc_ssl_cmp_target_name(absl::string_view target_name, + absl::string_view other_target_name, + absl::string_view overridden_target_name, + absl::string_view other_overridden_target_name); /* Check the host that will be set for a call is acceptable.*/ -bool grpc_ssl_check_call_host(grpc_core::StringView host, - grpc_core::StringView target_name, - grpc_core::StringView overridden_target_name, +bool grpc_ssl_check_call_host(absl::string_view host, + absl::string_view target_name, + absl::string_view overridden_target_name, grpc_auth_context* auth_context, grpc_error** error); /* Return HTTP2-compliant cipher suites that gRPC accepts by default. */ @@ -101,7 +102,7 @@ tsi_peer grpc_shallow_peer_from_ssl_auth_context( const grpc_auth_context* auth_context); void grpc_shallow_peer_destruct(tsi_peer* peer); int grpc_ssl_host_matches_name(const tsi_peer* peer, - grpc_core::StringView peer_name); + absl::string_view peer_name); /* --- Default SSL Root Store. --- */ namespace grpc_core { diff --git a/src/core/lib/security/security_connector/tls/tls_security_connector.cc b/src/core/lib/security/security_connector/tls/tls_security_connector.cc index ca361aa074f..4c142e2edfb 100644 --- a/src/core/lib/security/security_connector/tls/tls_security_connector.cc +++ b/src/core/lib/security/security_connector/tls/tls_security_connector.cc @@ -29,6 +29,7 @@ #include #include "src/core/lib/gprpp/host_port.h" +#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/security/credentials/ssl/ssl_credentials.h" #include "src/core/lib/security/credentials/tls/tls_credentials.h" #include "src/core/lib/security/security_connector/ssl_utils.h" @@ -149,10 +150,10 @@ TlsChannelSecurityConnector::TlsChannelSecurityConnector( : gpr_strdup(overridden_target_name)) { key_materials_config_ = grpc_tls_key_materials_config_create()->Ref(); check_arg_ = ServerAuthorizationCheckArgCreate(this); - grpc_core::StringView host; - grpc_core::StringView port; + absl::string_view host; + absl::string_view port; grpc_core::SplitHostPort(target_name, &host, &port); - target_name_ = grpc_core::StringViewToCString(host); + target_name_ = StringViewToCString(host); } TlsChannelSecurityConnector::~TlsChannelSecurityConnector() { @@ -277,7 +278,7 @@ int TlsChannelSecurityConnector::cmp( } bool TlsChannelSecurityConnector::check_call_host( - grpc_core::StringView host, grpc_auth_context* auth_context, + absl::string_view host, grpc_auth_context* auth_context, grpc_closure* on_call_host_checked, grpc_error** error) { return grpc_ssl_check_call_host(host, target_name_.get(), overridden_target_name_.get(), auth_context, diff --git a/src/core/lib/security/security_connector/tls/tls_security_connector.h b/src/core/lib/security/security_connector/tls/tls_security_connector.h index 4ac831f1ec1..69ac8eef58c 100644 --- a/src/core/lib/security/security_connector/tls/tls_security_connector.h +++ b/src/core/lib/security/security_connector/tls/tls_security_connector.h @@ -57,8 +57,7 @@ class TlsChannelSecurityConnector final int cmp(const grpc_security_connector* other_sc) const override; - bool check_call_host(grpc_core::StringView host, - grpc_auth_context* auth_context, + bool check_call_host(absl::string_view host, grpc_auth_context* auth_context, grpc_closure* on_call_host_checked, grpc_error** error) override; diff --git a/src/core/lib/security/transport/client_auth_filter.cc b/src/core/lib/security/transport/client_auth_filter.cc index c3658a99d2e..ed24b4f6622 100644 --- a/src/core/lib/security/transport/client_auth_filter.cc +++ b/src/core/lib/security/transport/client_auth_filter.cc @@ -379,8 +379,7 @@ static void client_auth_start_transport_stream_op_batch( GRPC_CALL_STACK_REF(calld->owning_call, "check_call_host"); GRPC_CLOSURE_INIT(&calld->async_result_closure, on_host_checked, batch, grpc_schedule_on_exec_ctx); - grpc_core::StringView call_host( - grpc_core::StringViewFromSlice(calld->host)); + absl::string_view call_host(grpc_core::StringViewFromSlice(calld->host)); grpc_error* error = GRPC_ERROR_NONE; if (chand->security_connector->check_call_host( call_host, chand->auth_context.get(), diff --git a/src/core/lib/slice/slice_utils.h b/src/core/lib/slice/slice_utils.h index b203a1662a6..d9a4253c1aa 100644 --- a/src/core/lib/slice/slice_utils.h +++ b/src/core/lib/slice/slice_utils.h @@ -23,6 +23,8 @@ #include +#include "absl/strings/string_view.h" + #include #include "src/core/lib/gpr/murmur_hash.h" @@ -186,6 +188,13 @@ struct InternedSlice : public ManagedMemorySlice { explicit InternedSlice(InternedSliceRefcount* s); }; +// Converts grpc_slice to absl::string_view. +inline absl::string_view StringViewFromSlice(const grpc_slice& slice) { + return absl::string_view( + reinterpret_cast(GRPC_SLICE_START_PTR(slice)), + GRPC_SLICE_LENGTH(slice)); +} + } // namespace grpc_core #endif /* GRPC_CORE_LIB_SLICE_SLICE_UTILS_H */ diff --git a/src/core/tsi/ssl_transport_security.cc b/src/core/tsi/ssl_transport_security.cc index e53e1b1faf0..bd82dc1db47 100644 --- a/src/core/tsi/ssl_transport_security.cc +++ b/src/core/tsi/ssl_transport_security.cc @@ -53,6 +53,7 @@ extern "C" { } #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/string_view.h" #include "src/core/tsi/ssl/session_cache/ssl_session_cache.h" #include "src/core/tsi/ssl_types.h" #include "src/core/tsi/transport_security.h" @@ -238,7 +239,7 @@ static void ssl_info_callback(const SSL* ssl, int where, int ret) { /* Returns 1 if name looks like an IP address, 0 otherwise. This is a very rough heuristic, and only handles IPv6 in hexadecimal form. */ -static int looks_like_ip_address(grpc_core::StringView name) { +static int looks_like_ip_address(absl::string_view name) { size_t dot_count = 0; size_t num_size = 0; for (size_t i = 0; i < name.size(); ++i) { @@ -1645,8 +1646,8 @@ static void tsi_ssl_server_handshaker_factory_destroy( gpr_free(self); } -static int does_entry_match_name(grpc_core::StringView entry, - grpc_core::StringView name) { +static int does_entry_match_name(absl::string_view entry, + absl::string_view name) { if (entry.empty()) return 0; /* Take care of '.' terminations. */ @@ -1669,13 +1670,13 @@ static int does_entry_match_name(grpc_core::StringView entry, return 0; } size_t name_subdomain_pos = name.find('.'); - if (name_subdomain_pos == grpc_core::StringView::npos) return 0; + if (name_subdomain_pos == absl::string_view::npos) return 0; if (name_subdomain_pos >= name.size() - 2) return 0; - grpc_core::StringView name_subdomain = + absl::string_view name_subdomain = name.substr(name_subdomain_pos + 1); /* Starts after the dot. */ entry.remove_prefix(2); /* Remove *. */ size_t dot = name_subdomain.find('.'); - if (dot == grpc_core::StringView::npos || dot == name_subdomain.size() - 1) { + if (dot == absl::string_view::npos || dot == name_subdomain.size() - 1) { grpc_core::UniquePtr name_subdomain_cstr( grpc_core::StringViewToCString(name_subdomain)); gpr_log(GPR_ERROR, "Invalid toplevel subdomain: %s", @@ -2058,8 +2059,7 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options( /* --- tsi_ssl utils. --- */ -int tsi_ssl_peer_matches_name(const tsi_peer* peer, - grpc_core::StringView name) { +int tsi_ssl_peer_matches_name(const tsi_peer* peer, absl::string_view name) { size_t i = 0; size_t san_count = 0; const tsi_peer_property* cn_property = nullptr; @@ -2073,7 +2073,7 @@ int tsi_ssl_peer_matches_name(const tsi_peer* peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) { san_count++; - grpc_core::StringView entry(property->value.data, property->value.length); + absl::string_view entry(property->value.data, property->value.length); if (!like_ip && does_entry_match_name(entry, name)) { return 1; } else if (like_ip && name == entry) { @@ -2088,8 +2088,8 @@ int tsi_ssl_peer_matches_name(const tsi_peer* peer, /* If there's no SAN, try the CN, but only if its not like an IP Address */ if (san_count == 0 && cn_property != nullptr && !like_ip) { - if (does_entry_match_name(grpc_core::StringView(cn_property->value.data, - cn_property->value.length), + if (does_entry_match_name(absl::string_view(cn_property->value.data, + cn_property->value.length), name)) { return 1; } diff --git a/src/core/tsi/ssl_transport_security.h b/src/core/tsi/ssl_transport_security.h index ae1e413aad3..4c3b9b68b38 100644 --- a/src/core/tsi/ssl_transport_security.h +++ b/src/core/tsi/ssl_transport_security.h @@ -21,7 +21,8 @@ #include -#include "src/core/lib/gprpp/string_view.h" +#include "absl/strings/string_view.h" + #include "src/core/tsi/transport_security_interface.h" extern "C" { @@ -317,7 +318,7 @@ void tsi_ssl_server_handshaker_factory_unref( - handle mixed case. - handle %encoded chars. - handle public suffix wildchar more strictly (e.g. *.co.uk) */ -int tsi_ssl_peer_matches_name(const tsi_peer* peer, grpc_core::StringView name); +int tsi_ssl_peer_matches_name(const tsi_peer* peer, absl::string_view name); /* --- Testing support. --- diff --git a/test/core/gprpp/string_view_test.cc b/test/core/gprpp/string_view_test.cc index a48967ee170..5f9f8df76b5 100644 --- a/test/core/gprpp/string_view_test.cc +++ b/test/core/gprpp/string_view_test.cc @@ -27,135 +27,11 @@ namespace grpc_core { namespace testing { -TEST(StringViewTest, Empty) { - grpc_core::StringView empty; - EXPECT_TRUE(empty.empty()); - EXPECT_EQ(empty.size(), 0lu); - - grpc_core::StringView empty_buf(""); - EXPECT_TRUE(empty_buf.empty()); - EXPECT_EQ(empty_buf.size(), 0lu); - - grpc_core::StringView empty_trimmed("foo", 0); - EXPECT_TRUE(empty_trimmed.empty()); - EXPECT_EQ(empty_trimmed.size(), 0lu); - - grpc_core::StringView empty_slice( - grpc_core::StringViewFromSlice(grpc_empty_slice())); - EXPECT_TRUE(empty_slice.empty()); - EXPECT_EQ(empty_slice.size(), 0lu); -} - -TEST(StringViewTest, Size) { - constexpr char kStr[] = "foo"; - grpc_core::StringView str1(kStr); - EXPECT_EQ(str1.size(), strlen(kStr)); - grpc_core::StringView str2(kStr, 2); - EXPECT_EQ(str2.size(), 2lu); -} - -TEST(StringViewTest, Data) { - constexpr char kStr[] = "foo-bar"; - grpc_core::StringView str(kStr); - EXPECT_EQ(str.size(), strlen(kStr)); - for (size_t i = 0; i < strlen(kStr); ++i) { - EXPECT_EQ(str[i], kStr[i]); - } -} - -TEST(StringViewTest, Slice) { - constexpr char kStr[] = "foo"; - grpc_core::StringView slice( - grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr))); - EXPECT_EQ(slice.size(), strlen(kStr)); -} - TEST(StringViewTest, Dup) { constexpr char kStr[] = "foo"; - grpc_core::StringView slice( - grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr))); - grpc_core::UniquePtr dup = grpc_core::StringViewToCString(slice); - EXPECT_EQ(0, strcmp(kStr, dup.get())); - EXPECT_EQ(slice.size(), strlen(kStr)); -} - -TEST(StringViewTest, Eq) { - constexpr char kStr1[] = "foo"; - constexpr char kStr2[] = "bar"; - grpc_core::StringView str1(kStr1); - EXPECT_EQ(kStr1, str1); - EXPECT_EQ(str1, kStr1); - grpc_core::StringView slice1( - grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr1))); - EXPECT_EQ(slice1, str1); - EXPECT_EQ(str1, slice1); - EXPECT_NE(slice1, kStr2); - EXPECT_NE(kStr2, slice1); - grpc_core::StringView slice2( - grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr2))); - EXPECT_NE(slice2, str1); - EXPECT_NE(str1, slice2); -} - -TEST(StringViewTest, Cmp) { - constexpr char kStr1[] = "abc"; - constexpr char kStr2[] = "abd"; - constexpr char kStr3[] = "abcd"; - grpc_core::StringView str1(kStr1); - grpc_core::StringView str2(kStr2); - grpc_core::StringView str3(kStr3); - EXPECT_EQ(str1.compare(str1), 0); - EXPECT_LT(str1.compare(str2), 0); - EXPECT_LT(str1.compare(str3), 0); - EXPECT_EQ(str2.compare(str2), 0); - EXPECT_GT(str2.compare(str1), 0); - EXPECT_GT(str2.compare(str3), 0); - EXPECT_EQ(str3.compare(str3), 0); - EXPECT_GT(str3.compare(str1), 0); - EXPECT_LT(str3.compare(str2), 0); -} - -TEST(StringViewTest, RemovePrefix) { - constexpr char kStr[] = "abcd"; - grpc_core::StringView str(kStr); - str.remove_prefix(1); - EXPECT_EQ("bcd", str); - str.remove_prefix(2); - EXPECT_EQ("d", str); - str.remove_prefix(1); - EXPECT_EQ("", str); -} - -TEST(StringViewTest, RemoveSuffix) { - constexpr char kStr[] = "abcd"; - grpc_core::StringView str(kStr); - str.remove_suffix(1); - EXPECT_EQ("abc", str); - str.remove_suffix(2); - EXPECT_EQ("a", str); - str.remove_suffix(1); - EXPECT_EQ("", str); -} - -TEST(StringViewTest, Substring) { - constexpr char kStr[] = "abcd"; - grpc_core::StringView str(kStr); - EXPECT_EQ("bcd", str.substr(1)); - EXPECT_EQ("bc", str.substr(1, 2)); -} - -TEST(StringViewTest, Find) { - // Passing StringView::npos directly to GTEST macros result in link errors. - // Store the value in a local variable and use it in the test. - const size_t npos = grpc_core::StringView::npos; - constexpr char kStr[] = "abacad"; - grpc_core::StringView str(kStr); - EXPECT_EQ(0ul, str.find('a')); - EXPECT_EQ(2ul, str.find('a', 1)); - EXPECT_EQ(4ul, str.find('a', 3)); - EXPECT_EQ(1ul, str.find('b')); - EXPECT_EQ(npos, str.find('b', 2)); - EXPECT_EQ(npos, str.find('z')); + absl::string_view sv = kStr; + grpc_core::UniquePtr dup = grpc_core::StringViewToCString(sv); + EXPECT_EQ(std::string(sv), dup.get()); } } // namespace testing diff --git a/test/core/json/fuzzer.cc b/test/core/json/fuzzer.cc index 6d27ee2c566..e9eecf2332b 100644 --- a/test/core/json/fuzzer.cc +++ b/test/core/json/fuzzer.cc @@ -31,7 +31,7 @@ bool leak_check = true; extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { grpc_error* error = GRPC_ERROR_NONE; grpc_core::Json::Parse( - grpc_core::StringView(reinterpret_cast(data), size), &error); + absl::string_view(reinterpret_cast(data), size), &error); GRPC_ERROR_UNREF(error); return 0; } diff --git a/test/core/security/credentials_test.cc b/test/core/security/credentials_test.cc index fd0cf6cc403..75eb00ec83d 100644 --- a/test/core/security/credentials_test.cc +++ b/test/core/security/credentials_test.cc @@ -788,8 +788,8 @@ static void test_valid_sts_creds_options(void) { grpc_core::ValidateStsCredentialsOptions(&valid_options, &sts_url); GPR_ASSERT(error == GRPC_ERROR_NONE); GPR_ASSERT(sts_url != nullptr); - grpc_core::StringView host; - grpc_core::StringView port; + absl::string_view host; + absl::string_view port; GPR_ASSERT(grpc_core::SplitHostPort(sts_url->authority, &host, &port)); GPR_ASSERT(host == "foo.com"); GPR_ASSERT(port == "5555"); diff --git a/test/core/security/json_token_test.cc b/test/core/security/json_token_test.cc index d93be88aba5..6b032bb7430 100644 --- a/test/core/security/json_token_test.cc +++ b/test/core/security/json_token_test.cc @@ -216,9 +216,7 @@ static Json parse_json_part_from_jwt(const char* str, size_t len) { gpr_free(b64); GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(slice)); grpc_error* error = GRPC_ERROR_NONE; - grpc_core::StringView string( - reinterpret_cast(GRPC_SLICE_START_PTR(slice)), - GRPC_SLICE_LENGTH(slice)); + absl::string_view string = grpc_core::StringViewFromSlice(slice); Json json = Json::Parse(string, &error); if (error != GRPC_ERROR_NONE) { gpr_log(GPR_ERROR, "JSON parse error: %s", grpc_error_string(error)); diff --git a/test/core/slice/slice_test.cc b/test/core/slice/slice_test.cc index 83a6db4a113..975bedfcd47 100644 --- a/test/core/slice/slice_test.cc +++ b/test/core/slice/slice_test.cc @@ -330,6 +330,13 @@ static void test_moved_string_slice(void) { grpc_shutdown(); } +void test_string_view_from_slice() { + constexpr char kStr[] = "foo"; + absl::string_view sv( + grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr))); + GPR_ASSERT(std::string(sv) == kStr); +} + int main(int argc, char** argv) { unsigned length; grpc::testing::TestEnvironment env(argc, argv); @@ -348,6 +355,7 @@ int main(int argc, char** argv) { test_static_slice_interning(); test_static_slice_copy_interning(); test_moved_string_slice(); + test_string_view_from_slice(); grpc_shutdown(); return 0; } diff --git a/test/core/util/test_lb_policies.cc b/test/core/util/test_lb_policies.cc index 46ddf59351d..eae25bc4265 100644 --- a/test/core/util/test_lb_policies.cc +++ b/test/core/util/test_lb_policies.cc @@ -161,7 +161,8 @@ class InterceptRecvTrailingMetadataLoadBalancingPolicy parent_->channel_control_helper()->RequestReresolution(); } - void AddTraceEvent(TraceSeverity severity, StringView message) override { + void AddTraceEvent(TraceSeverity severity, + absl::string_view message) override { parent_->channel_control_helper()->AddTraceEvent(severity, message); } diff --git a/test/cpp/end2end/client_lb_end2end_test.cc b/test/cpp/end2end/client_lb_end2end_test.cc index d066a00a29d..295afb312b6 100644 --- a/test/cpp/end2end/client_lb_end2end_test.cc +++ b/test/cpp/end2end/client_lb_end2end_test.cc @@ -48,6 +48,7 @@ #include "src/core/lib/gpr/env.h" #include "src/core/lib/gprpp/debug_location.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" +#include "src/core/lib/gprpp/string_view.h" #include "src/core/lib/iomgr/tcp_client.h" #include "src/core/lib/security/credentials/fake/fake_credentials.h" #include "src/cpp/client/secure_credentials.h" diff --git a/test/cpp/qps/driver.cc b/test/cpp/qps/driver.cc index 03a4477b4d8..1f3176f5548 100644 --- a/test/cpp/qps/driver.cc +++ b/test/cpp/qps/driver.cc @@ -52,8 +52,8 @@ using std::vector; namespace grpc { namespace testing { static std::string get_host(const std::string& worker) { - grpc_core::StringView host; - grpc_core::StringView port; + absl::string_view host; + absl::string_view port; grpc_core::SplitHostPort(worker.c_str(), &host, &port); return std::string(host.data(), host.size()); }