Remove grpc_core::StringView.

pull/22708/head
Mark D. Roth 5 years ago
parent d704da61a6
commit fc6f26c89b
  1. 9
      src/core/ext/filters/client_channel/backend_metric.cc
  2. 9
      src/core/ext/filters/client_channel/client_channel.cc
  3. 27
      src/core/ext/filters/client_channel/lb_policy.h
  4. 3
      src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc
  5. 8
      src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc
  6. 7
      src/core/ext/filters/client_channel/lb_policy/priority/priority.cc
  7. 5
      src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc
  8. 18
      src/core/ext/filters/client_channel/lb_policy/xds/cds.cc
  9. 10
      src/core/ext/filters/client_channel/lb_policy/xds/eds.cc
  10. 6
      src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc
  11. 5
      src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc
  12. 1
      src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc
  13. 5
      src/core/ext/filters/client_channel/resolving_lb_policy.cc
  14. 4
      src/core/ext/filters/client_channel/service_config.cc
  15. 2
      src/core/ext/filters/client_channel/service_config.h
  16. 50
      src/core/ext/filters/client_channel/xds/xds_api.cc
  17. 14
      src/core/ext/filters/client_channel/xds/xds_api.h
  18. 4
      src/core/ext/filters/client_channel/xds/xds_bootstrap.cc
  19. 47
      src/core/ext/filters/client_channel/xds/xds_client.cc
  20. 31
      src/core/ext/filters/client_channel/xds/xds_client.h
  21. 10
      src/core/ext/filters/client_channel/xds/xds_client_stats.cc
  22. 22
      src/core/ext/filters/client_channel/xds/xds_client_stats.h
  23. 29
      src/core/lib/gprpp/host_port.cc
  24. 10
      src/core/lib/gprpp/host_port.h
  25. 3
      src/core/lib/gprpp/map.h
  26. 12
      src/core/lib/gprpp/string_view.h
  27. 5
      src/core/lib/http/httpcli_security_connector.cc
  28. 5
      src/core/lib/json/json.h
  29. 8
      src/core/lib/json/json_reader.cc
  30. 25
      src/core/lib/json/json_writer.cc
  31. 6
      src/core/lib/security/credentials/google_default/google_default_credentials.cc
  32. 6
      src/core/lib/security/credentials/jwt/jwt_verifier.cc
  33. 2
      src/core/lib/security/security_connector/alts/alts_security_connector.cc
  34. 14
      src/core/lib/security/security_connector/fake/fake_security_connector.cc
  35. 2
      src/core/lib/security/security_connector/local/local_security_connector.cc
  36. 2
      src/core/lib/security/security_connector/security_connector.h
  37. 8
      src/core/lib/security/security_connector/ssl/ssl_security_connector.cc
  38. 24
      src/core/lib/security/security_connector/ssl_utils.cc
  39. 21
      src/core/lib/security/security_connector/ssl_utils.h
  40. 9
      src/core/lib/security/security_connector/tls/tls_security_connector.cc
  41. 3
      src/core/lib/security/security_connector/tls/tls_security_connector.h
  42. 3
      src/core/lib/security/transport/client_auth_filter.cc
  43. 9
      src/core/lib/slice/slice_utils.h
  44. 22
      src/core/tsi/ssl_transport_security.cc
  45. 5
      src/core/tsi/ssl_transport_security.h
  46. 130
      test/core/gprpp/string_view_test.cc
  47. 2
      test/core/json/fuzzer.cc
  48. 4
      test/core/security/credentials_test.cc
  49. 4
      test/core/security/json_token_test.cc
  50. 8
      test/core/slice/slice_test.cc
  51. 3
      test/core/util/test_lb_policies.cc
  52. 1
      test/cpp/end2end/client_lb_end2end_test.cc
  53. 4
      test/cpp/qps/driver.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 <typename EntryType>
std::map<StringView, double, StringLess> ParseMap(
std::map<absl::string_view, double, StringLess> 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<StringView, double, StringLess> result;
std::map<absl::string_view, double, StringLess> 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<char*>(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;
}

@ -28,6 +28,8 @@
#include <set>
#include "absl/strings/string_view.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
@ -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<grpc_linked_mdelem*>(
calld_->arena_->Alloc(sizeof(grpc_linked_mdelem)));
linked_mdelem->md = grpc_mdelem_from_slices(
@ -407,7 +409,7 @@ class CallData {
reinterpret_cast<grpc_linked_mdelem*>(handle);
return reinterpret_cast<intptr_t>(linked_mdelem->next);
}
std::pair<StringView, StringView> IteratorHandleGet(
std::pair<absl::string_view, absl::string_view> IteratorHandleGet(
intptr_t handle) const override {
grpc_linked_mdelem* linked_mdelem =
reinterpret_cast<grpc_linked_mdelem*>(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),

@ -24,13 +24,14 @@
#include <functional>
#include <iterator>
#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<LoadBalancingPolicy> {
/// 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<StringView, double, StringLess> request_cost;
std::map<absl::string_view, double, StringLess> 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<StringView, double, StringLess> utilization;
std::map<absl::string_view, double, StringLess> utilization;
};
/// Interface for accessing per-call state.
@ -123,12 +124,13 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
class MetadataInterface {
public:
class iterator
: public std::iterator<std::input_iterator_tag,
std::pair<StringView, StringView>, // value_type
std::ptrdiff_t, // difference_type
std::pair<StringView, StringView>*, // pointer
std::pair<StringView, StringView>& // reference
> {
: public std::iterator<
std::input_iterator_tag,
std::pair<absl::string_view, absl::string_view>, // value_type
std::ptrdiff_t, // difference_type
std::pair<absl::string_view, absl::string_view>*, // pointer
std::pair<absl::string_view, absl::string_view>& // reference
> {
public:
iterator(const MetadataInterface* md, intptr_t handle)
: md_(md), handle_(handle) {}
@ -155,7 +157,7 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
/// 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<LoadBalancingPolicy> {
friend class iterator;
virtual intptr_t IteratorHandleNext(intptr_t handle) const = 0;
virtual std::pair<StringView /*key*/, StringView /*value */>
virtual std::pair<absl::string_view /*key*/, absl::string_view /*value */>
IteratorHandleGet(intptr_t handle) const = 0;
};
@ -276,7 +278,8 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
/// 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.

@ -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);

@ -297,7 +297,8 @@ class GrpcLb : public LoadBalancingPolicy {
void UpdateState(grpc_connectivity_state state,
std::unique_ptr<SubchannelPicker> picker) override;
void RequestReresolution() override;
void AddTraceEvent(TraceSeverity severity, StringView message) override;
void AddTraceEvent(TraceSeverity severity,
absl::string_view message) override;
private:
RefCountedPtr<GrpcLb> parent_;
@ -628,7 +629,7 @@ GrpcLb::PickResult GrpcLb::Picker::Pick(PickArgs args) {
// how to interpret it.
args.initial_metadata->Add(
kGrpcLbClientStatsMetadataKey,
StringView(reinterpret_cast<const char*>(client_stats), 0));
absl::string_view(reinterpret_cast<const char*>(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);
}

@ -152,7 +152,8 @@ class PriorityLb : public LoadBalancingPolicy {
void UpdateState(grpc_connectivity_state state,
std::unique_ptr<SubchannelPicker> picker) override;
void RequestReresolution() override;
void AddTraceEvent(TraceSeverity severity, StringView message) override;
void AddTraceEvent(TraceSeverity severity,
absl::string_view message) override;
private:
RefCountedPtr<ChildPriority> 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);

@ -148,7 +148,8 @@ class WeightedTargetLb : public LoadBalancingPolicy {
void UpdateState(grpc_connectivity_state state,
std::unique_ptr<SubchannelPicker> picker) override;
void RequestReresolution() override;
void AddTraceEvent(TraceSeverity severity, StringView message) override;
void AddTraceEvent(TraceSeverity severity,
absl::string_view message) override;
private:
RefCountedPtr<WeightedChild> 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);

@ -79,7 +79,8 @@ class CdsLb : public LoadBalancingPolicy {
void UpdateState(grpc_connectivity_state state,
std::unique_ptr<SubchannelPicker> picker) override;
void RequestReresolution() override;
void AddTraceEvent(TraceSeverity severity, StringView message) override;
void AddTraceEvent(TraceSeverity severity,
absl::string_view message) override;
private:
RefCountedPtr<CdsLb> 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<ClusterWatcher>(Ref());
cluster_watcher_ = watcher.get();
xds_client_->WatchClusterData(StringView(config_->cluster().c_str()),
std::move(watcher));
xds_client_->WatchClusterData(config_->cluster(), std::move(watcher));
}
}

@ -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<EdsLb> 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<StringView, StringView> GetLrsClusterKey() const {
std::pair<absl::string_view, absl::string_view> 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);
}

@ -123,7 +123,8 @@ class LrsLb : public LoadBalancingPolicy {
void UpdateState(grpc_connectivity_state state,
std::unique_ptr<SubchannelPicker> picker) override;
void RequestReresolution() override;
void AddTraceEvent(TraceSeverity severity, StringView message) override;
void AddTraceEvent(TraceSeverity severity,
absl::string_view message) override;
private:
RefCountedPtr<LrsLb> 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);
}

@ -161,7 +161,8 @@ class XdsRoutingLb : public LoadBalancingPolicy {
void UpdateState(grpc_connectivity_state state,
std::unique_ptr<SubchannelPicker> picker) override;
void RequestReresolution() override;
void AddTraceEvent(TraceSeverity severity, StringView message) override;
void AddTraceEvent(TraceSeverity severity,
absl::string_view message) override;
private:
RefCountedPtr<XdsRoutingChild> 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);

@ -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 {

@ -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<char> 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);
}
}

@ -40,8 +40,8 @@ typedef absl::InlinedVector<std::unique_ptr<ServiceConfig::Parser>,
ServiceConfigParserList* g_registered_parsers;
} // namespace
RefCountedPtr<ServiceConfig> ServiceConfig::Create(StringView json_string,
grpc_error** error) {
RefCountedPtr<ServiceConfig> 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;

@ -129,7 +129,7 @@ class ServiceConfig : public RefCounted<ServiceConfig> {
/// Creates a new service config from parsing \a json_string.
/// Returns null on parse error.
static RefCountedPtr<ServiceConfig> Create(StringView json_string,
static RefCountedPtr<ServiceConfig> Create(absl::string_view json_string,
grpc_error** error);
ServiceConfig(std::string json_string, Json json, grpc_error** error);

@ -31,6 +31,7 @@
#include <grpc/support/string_util.h>
#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<StringView>& cluster_names,
const std::string& version,
const std::string& nonce, grpc_error* error,
bool populate_node) {
grpc_slice XdsApi::CreateCdsRequest(
const std::set<absl::string_view>& 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<StringView>& cluster_names,
}
grpc_slice XdsApi::CreateEdsRequest(
const std::set<StringView>& eds_service_names, const std::string& version,
const std::string& nonce, grpc_error* error, bool populate_node) {
const std::set<absl::string_view>& 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<StringView>& 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<absl::string_view>& 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<StringView>& expected_eds_service_names,
const std::set<absl::string_view>& 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<StringView>& expected_cluster_names,
const std::set<StringView>& expected_eds_service_names,
const std::set<absl::string_view>& expected_cluster_names,
const std::set<absl::string_view>& expected_eds_service_names,
absl::optional<LdsUpdate>* lds_update,
absl::optional<RdsUpdate>* rds_update, CdsUpdateMap* cds_update_map,
EdsUpdateMap* eds_update_map, std::string* version, std::string* nonce,

@ -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<StringView>& cluster_names,
grpc_slice CreateCdsRequest(const std::set<absl::string_view>& 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<StringView>& eds_service_names,
const std::string& version,
const std::string& nonce, grpc_error* error,
bool populate_node);
grpc_slice CreateEdsRequest(
const std::set<absl::string_view>& 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<StringView>& expected_cluster_names,
const std::set<StringView>& expected_eds_service_names,
const std::set<absl::string_view>& expected_cluster_names,
const std::set<absl::string_view>& expected_eds_service_names,
absl::optional<LdsUpdate>* lds_update,
absl::optional<RdsUpdate>* rds_update, CdsUpdateMap* cds_update_map,
EdsUpdateMap* eds_update_map, std::string* version, std::string* nonce,

@ -26,6 +26,8 @@
#include <grpc/support/string_util.h>
#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> 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<char> str = StringViewToCString(contents_str_view);
gpr_log(GPR_DEBUG, "[xds_client %p] Bootstrap file contents: %s", client,

@ -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<StringView> ClusterNamesForRequest();
std::set<StringView> EdsServiceNamesForRequest();
std::set<absl::string_view> ClusterNamesForRequest();
std::set<absl::string_view> EdsServiceNamesForRequest();
// The owning RetryableCall<>.
RefCountedPtr<RetryableCall<AdsCallState>> parent_;
@ -802,7 +803,7 @@ void XdsClient::ChannelState::AdsCallState::SendMessageLocked(
}
auto& state = state_map_[type_url];
grpc_slice request_payload_slice;
std::set<StringView> resource_names;
std::set<absl::string_view> 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<StringView>
std::set<absl::string_view>
XdsClient::ChannelState::AdsCallState::ClusterNamesForRequest() {
std::set<StringView> cluster_names;
std::set<absl::string_view> cluster_names;
for (auto& p : state_map_[XdsApi::kCdsTypeUrl].subscribed_resources) {
cluster_names.insert(p.first);
OrphanablePtr<ResourceState>& state = p.second;
@ -1356,9 +1357,9 @@ XdsClient::ChannelState::AdsCallState::ClusterNamesForRequest() {
return cluster_names;
}
std::set<StringView>
std::set<absl::string_view>
XdsClient::ChannelState::AdsCallState::EdsServiceNamesForRequest() {
std::set<StringView> eds_names;
std::set<absl::string_view> eds_names;
for (auto& p : state_map_[XdsApi::kEdsTypeUrl].subscribed_resources) {
eds_names.insert(p.first);
OrphanablePtr<ResourceState>& state = p.second;
@ -1800,7 +1801,7 @@ bool GetXdsRoutingEnabled(const grpc_channel_args& args) {
XdsClient::XdsClient(std::shared_ptr<WorkSerializer> work_serializer,
grpc_pollset_set* interested_parties,
StringView server_name,
absl::string_view server_name,
std::unique_ptr<ServiceConfigWatcherInterface> watcher,
const grpc_channel_args& channel_args, grpc_error** error)
: InternallyRefCounted<XdsClient>(&grpc_xds_client_trace),
@ -1867,7 +1868,8 @@ void XdsClient::Orphan() {
}
void XdsClient::WatchClusterData(
StringView cluster_name, std::unique_ptr<ClusterWatcherInterface> watcher) {
absl::string_view cluster_name,
std::unique_ptr<ClusterWatcherInterface> 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<EndpointWatcherInterface> 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<XdsClusterDropStats> 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<XdsClusterDropStats> 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<XdsClusterLocalityStats> 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<XdsLocalityName> 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<XdsClusterLocalityStats> XdsClient::AddClusterLocalityStats(
}
void XdsClient::RemoveClusterLocalityStats(
StringView /*lrs_server*/, StringView cluster_name,
StringView eds_service_name, const RefCountedPtr<XdsLocalityName>& locality,
absl::string_view /*lrs_server*/, absl::string_view cluster_name,
absl::string_view eds_service_name,
const RefCountedPtr<XdsLocalityName>& 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)));

@ -21,6 +21,7 @@
#include <set>
#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<XdsClient> {
// If *error is not GRPC_ERROR_NONE after construction, then there was
// an error initializing the client.
XdsClient(std::shared_ptr<WorkSerializer> work_serializer,
grpc_pollset_set* interested_parties, StringView server_name,
grpc_pollset_set* interested_parties, absl::string_view server_name,
std::unique_ptr<ServiceConfigWatcherInterface> watcher,
const grpc_channel_args& channel_args, grpc_error** error);
~XdsClient();
@ -89,9 +89,9 @@ class XdsClient : public InternallyRefCounted<XdsClient> {
// 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<ClusterWatcherInterface> 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<XdsClient> {
// 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<EndpointWatcherInterface> 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<XdsClusterDropStats> 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<XdsClusterLocalityStats> AddClusterLocalityStats(
StringView lrs_server, StringView cluster_name,
StringView eds_service_name, RefCountedPtr<XdsLocalityName> locality);
absl::string_view lrs_server, absl::string_view cluster_name,
absl::string_view eds_service_name,
RefCountedPtr<XdsLocalityName> 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<XdsLocalityName>& locality,
XdsClusterLocalityStats* cluster_locality_stats);

@ -34,9 +34,9 @@ namespace grpc_core {
//
XdsClusterDropStats::XdsClusterDropStats(RefCountedPtr<XdsClient> 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<XdsClient> xds_client, StringView lrs_server_name,
StringView cluster_name, StringView eds_service_name,
RefCountedPtr<XdsClient> xds_client, absl::string_view lrs_server_name,
absl::string_view cluster_name, absl::string_view eds_service_name,
RefCountedPtr<XdsLocalityName> name)
: xds_client_(std::move(xds_client)),
lrs_server_name_(lrs_server_name),

@ -99,8 +99,9 @@ class XdsClusterDropStats : public RefCounted<XdsClusterDropStats> {
using DroppedRequestsMap = std::map<std::string /* category */, uint64_t>;
XdsClusterDropStats(RefCountedPtr<XdsClient> 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<XdsClusterDropStats> {
private:
RefCountedPtr<XdsClient> 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> {
};
XdsClusterLocalityStats(RefCountedPtr<XdsClient> 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<XdsLocalityName> name);
~XdsClusterLocalityStats();
@ -183,9 +185,9 @@ class XdsClusterLocalityStats : public RefCounted<XdsClusterLocalityStats> {
private:
RefCountedPtr<XdsClient> 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<XdsLocalityName> name_;
Atomic<uint64_t> total_successful_requests_{0};

@ -22,6 +22,8 @@
#include <string.h>
#include "absl/strings/string_view.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
@ -45,19 +47,19 @@ int JoinHostPort(grpc_core::UniquePtr<char>* 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) {
/* ]<end> */
*port = StringView();
*port = absl::string_view();
} else if (name[rbracket + 1] == ':') {
/* ]:<port?> */
*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<char>* host,
bool SplitHostPort(absl::string_view name, grpc_core::UniquePtr<char>* host,
grpc_core::UniquePtr<char>* 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) {

@ -21,8 +21,9 @@
#include <grpc/support/port_platform.h>
#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<char>* 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<char>* 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<char>* host,
bool SplitHostPort(absl::string_view name, absl::string_view* host,
absl::string_view* port);
bool SplitHostPort(absl::string_view name, grpc_core::UniquePtr<char>* host,
grpc_core::UniquePtr<char>* port);
} // namespace grpc_core

@ -38,7 +38,8 @@ struct StringLess {
const grpc_core::UniquePtr<char>& 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;

@ -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<const char*>(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<char> StringViewToCString(const StringView sv) {
inline grpc_core::UniquePtr<char> StringViewToCString(
const absl::string_view sv) {
char* str = static_cast<char*>(gpr_malloc(sv.size() + 1));
if (sv.size() > 0) memcpy(str, sv.data(), sv.size());
str[sv.size()] = '\0';

@ -22,6 +22,8 @@
#include <string.h>
#include "absl/strings/string_view.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
@ -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 {

@ -27,7 +27,8 @@
#include <string>
#include <vector>
#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<Json>;
// 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;

@ -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<const uint8_t*>(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;

@ -21,13 +21,13 @@
#include <stdlib.h>
#include <string.h>
#include "absl/strings/string_view.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#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<unsigned>(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);

@ -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<char*>(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(

@ -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<char*>(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

@ -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 {

@ -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);

@ -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 {

@ -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;

@ -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(

@ -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);

@ -23,12 +23,13 @@
#include <stdbool.h>
#include "absl/strings/string_view.h"
#include <grpc/grpc_security.h>
#include <grpc/slice_buffer.h>
#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 {

@ -29,6 +29,7 @@
#include <grpc/support/string_util.h>
#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,

@ -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;

@ -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(),

@ -23,6 +23,8 @@
#include <cstring>
#include "absl/strings/string_view.h"
#include <grpc/slice.h>
#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<const char*>(GRPC_SLICE_START_PTR(slice)),
GRPC_SLICE_LENGTH(slice));
}
} // namespace grpc_core
#endif /* GRPC_CORE_LIB_SLICE_SLICE_UTILS_H */

@ -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<char> 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;
}

@ -21,7 +21,8 @@
#include <grpc/support/port_platform.h>
#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. ---

@ -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<char> 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<char> dup = grpc_core::StringViewToCString(sv);
EXPECT_EQ(std::string(sv), dup.get());
}
} // namespace testing

@ -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<const char*>(data), size), &error);
absl::string_view(reinterpret_cast<const char*>(data), size), &error);
GRPC_ERROR_UNREF(error);
return 0;
}

@ -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");

@ -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<const char*>(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));

@ -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;
}

@ -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);
}

@ -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"

@ -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());
}

Loading…
Cancel
Save