mirror of https://github.com/grpc/grpc.git
commit
5b6c9473cd
124 changed files with 1327 additions and 939 deletions
@ -1,98 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gpr/host_port.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
int gpr_join_host_port(char** out, const char* host, int port) { |
||||
if (host[0] != '[' && strchr(host, ':') != nullptr) { |
||||
/* IPv6 literals must be enclosed in brackets. */ |
||||
return gpr_asprintf(out, "[%s]:%d", host, port); |
||||
} else { |
||||
/* Ordinary non-bracketed host:port. */ |
||||
return gpr_asprintf(out, "%s:%d", host, port); |
||||
} |
||||
} |
||||
|
||||
int gpr_split_host_port(const char* name, char** host, char** port) { |
||||
const char* host_start; |
||||
size_t host_len; |
||||
const char* port_start; |
||||
|
||||
*host = nullptr; |
||||
*port = nullptr; |
||||
|
||||
if (name[0] == '[') { |
||||
/* Parse a bracketed host, typically an IPv6 literal. */ |
||||
const char* rbracket = strchr(name, ']'); |
||||
if (rbracket == nullptr) { |
||||
/* Unmatched [ */ |
||||
return 0; |
||||
} |
||||
if (rbracket[1] == '\0') { |
||||
/* ]<end> */ |
||||
port_start = nullptr; |
||||
} else if (rbracket[1] == ':') { |
||||
/* ]:<port?> */ |
||||
port_start = rbracket + 2; |
||||
} else { |
||||
/* ]<invalid> */ |
||||
return 0; |
||||
} |
||||
host_start = name + 1; |
||||
host_len = static_cast<size_t>(rbracket - host_start); |
||||
if (memchr(host_start, ':', host_len) == nullptr) { |
||||
/* Require all bracketed hosts to contain a colon, because a hostname or
|
||||
IPv4 address should never use brackets. */ |
||||
return 0; |
||||
} |
||||
} else { |
||||
const char* colon = strchr(name, ':'); |
||||
if (colon != nullptr && strchr(colon + 1, ':') == nullptr) { |
||||
/* Exactly 1 colon. Split into host:port. */ |
||||
host_start = name; |
||||
host_len = static_cast<size_t>(colon - name); |
||||
port_start = colon + 1; |
||||
} else { |
||||
/* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ |
||||
host_start = name; |
||||
host_len = strlen(name); |
||||
port_start = nullptr; |
||||
} |
||||
} |
||||
|
||||
/* Allocate return values. */ |
||||
*host = static_cast<char*>(gpr_malloc(host_len + 1)); |
||||
memcpy(*host, host_start, host_len); |
||||
(*host)[host_len] = '\0'; |
||||
|
||||
if (port_start != nullptr) { |
||||
*port = gpr_strdup(port_start); |
||||
} |
||||
|
||||
return 1; |
||||
} |
@ -0,0 +1,97 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/host_port.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gprpp/string_view.h" |
||||
|
||||
namespace grpc_core { |
||||
int JoinHostPort(UniquePtr<char>* out, const char* host, int port) { |
||||
char* tmp; |
||||
int ret; |
||||
if (host[0] != '[' && strchr(host, ':') != nullptr) { |
||||
/* IPv6 literals must be enclosed in brackets. */ |
||||
ret = gpr_asprintf(&tmp, "[%s]:%d", host, port); |
||||
} else { |
||||
/* Ordinary non-bracketed host:port. */ |
||||
ret = gpr_asprintf(&tmp, "%s:%d", host, port); |
||||
} |
||||
out->reset(tmp); |
||||
return ret; |
||||
} |
||||
|
||||
bool SplitHostPort(StringView name, StringView* host, StringView* port) { |
||||
if (name[0] == '[') { |
||||
/* Parse a bracketed host, typically an IPv6 literal. */ |
||||
const size_t rbracket = name.find(']', 1); |
||||
if (rbracket == grpc_core::StringView::npos) { |
||||
/* Unmatched [ */ |
||||
return false; |
||||
} |
||||
if (rbracket == name.size() - 1) { |
||||
/* ]<end> */ |
||||
port->clear(); |
||||
} else if (name[rbracket + 1] == ':') { |
||||
/* ]:<port?> */ |
||||
*port = name.substr(rbracket + 2, name.size() - rbracket - 2); |
||||
} else { |
||||
/* ]<invalid> */ |
||||
return false; |
||||
} |
||||
*host = name.substr(1, rbracket - 1); |
||||
if (host->find(':') == grpc_core::StringView::npos) { |
||||
/* Require all bracketed hosts to contain a colon, because a hostname or
|
||||
IPv4 address should never use brackets. */ |
||||
host->clear(); |
||||
return false; |
||||
} |
||||
} else { |
||||
size_t colon = name.find(':'); |
||||
if (colon != grpc_core::StringView::npos && |
||||
name.find(':', colon + 1) == grpc_core::StringView::npos) { |
||||
/* Exactly 1 colon. Split into host:port. */ |
||||
*host = name.substr(0, colon); |
||||
*port = name.substr(colon + 1, name.size() - colon - 1); |
||||
} else { |
||||
/* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ |
||||
*host = name; |
||||
port->clear(); |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
bool SplitHostPort(StringView name, UniquePtr<char>* host, |
||||
UniquePtr<char>* port) { |
||||
StringView host_view; |
||||
StringView port_view; |
||||
const bool ret = SplitHostPort(name, &host_view, &port_view); |
||||
host->reset(host_view.empty() ? nullptr : host_view.dup().release()); |
||||
port->reset(port_view.empty() ? nullptr : port_view.dup().release()); |
||||
return ret; |
||||
} |
||||
} // namespace grpc_core
|
@ -0,0 +1,143 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
#ifndef GRPC_CORE_LIB_GPRPP_STRING_VIEW_H |
||||
#define GRPC_CORE_LIB_GPRPP_STRING_VIEW_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/impl/codegen/slice.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include <algorithm> |
||||
#include <cstdint> |
||||
#include <cstring> |
||||
#include <limits> |
||||
|
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
// Provides a light-weight view over a char array or a slice, similar but not
|
||||
// identical to absl::string_view.
|
||||
//
|
||||
// Any method that has the same name as absl::string_view MUST HAVE identical
|
||||
// semantics to what absl::string_view provides.
|
||||
//
|
||||
// Methods that are not part of absl::string_view API, must be clearly
|
||||
// annotated.
|
||||
//
|
||||
// StringView does not own the buffers that back the view. Callers must ensure
|
||||
// the buffer stays around while the StringView is accessible.
|
||||
//
|
||||
// Pass StringView by value in functions, since it is exactly two pointers in
|
||||
// size.
|
||||
//
|
||||
// The interface used here is not identical to absl::string_view. Notably, we
|
||||
// need to support slices while we cannot support std::string, and gpr string
|
||||
// style functions such as strdup() and cmp(). Once we switch to
|
||||
// absl::string_view this class will inherit from absl::string_view and add the
|
||||
// gRPC-specific APIs.
|
||||
class StringView final { |
||||
public: |
||||
static constexpr size_t npos = std::numeric_limits<size_t>::max(); |
||||
|
||||
constexpr StringView(const char* ptr, size_t size) : ptr_(ptr), size_(size) {} |
||||
constexpr StringView(const char* ptr) |
||||
: StringView(ptr, ptr == nullptr ? 0 : strlen(ptr)) {} |
||||
// Not part of absl::string_view API.
|
||||
StringView(const grpc_slice& slice) |
||||
: StringView(reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)), |
||||
GRPC_SLICE_LENGTH(slice)) {} |
||||
constexpr StringView() : StringView(nullptr, 0) {} |
||||
|
||||
constexpr const char* data() const { return ptr_; } |
||||
constexpr size_t size() const { return size_; } |
||||
constexpr bool empty() const { return size_ == 0; } |
||||
|
||||
StringView substr(size_t start, size_t size = npos) { |
||||
GPR_DEBUG_ASSERT(start + size <= size_); |
||||
return StringView(ptr_ + start, std::min(size, size_ - start)); |
||||
} |
||||
|
||||
constexpr const char& operator[](size_t i) const { return ptr_[i]; } |
||||
|
||||
const char& front() const { return ptr_[0]; } |
||||
const char& back() const { return ptr_[size_ - 1]; } |
||||
|
||||
void remove_prefix(size_t n) { |
||||
GPR_DEBUG_ASSERT(n <= size_); |
||||
ptr_ += n; |
||||
size_ -= n; |
||||
} |
||||
|
||||
void remove_suffix(size_t n) { |
||||
GPR_DEBUG_ASSERT(n <= size_); |
||||
size_ -= n; |
||||
} |
||||
|
||||
size_t find(char c, size_t pos = 0) const { |
||||
if (empty() || pos >= size_) return npos; |
||||
const char* result = |
||||
static_cast<const char*>(memchr(ptr_ + pos, c, size_ - pos)); |
||||
return result != nullptr ? result - ptr_ : npos; |
||||
} |
||||
|
||||
void clear() { |
||||
ptr_ = nullptr; |
||||
size_ = 0; |
||||
} |
||||
|
||||
// Creates a dup of the string viewed by this class.
|
||||
// Return value is null-terminated and never nullptr.
|
||||
//
|
||||
// Not part of absl::string_view API.
|
||||
grpc_core::UniquePtr<char> dup() const { |
||||
char* str = static_cast<char*>(gpr_malloc(size_ + 1)); |
||||
if (size_ > 0) memcpy(str, ptr_, size_); |
||||
str[size_] = '\0'; |
||||
return grpc_core::UniquePtr<char>(str); |
||||
} |
||||
|
||||
// Not part of absl::string_view API.
|
||||
int cmp(StringView other) const { |
||||
const size_t len = GPR_MIN(size(), other.size()); |
||||
const int ret = strncmp(data(), other.data(), len); |
||||
if (ret != 0) return ret; |
||||
if (size() == other.size()) return 0; |
||||
if (size() < other.size()) return -1; |
||||
return 1; |
||||
} |
||||
|
||||
private: |
||||
const char* ptr_; |
||||
size_t size_; |
||||
}; |
||||
|
||||
inline bool operator==(StringView lhs, StringView rhs) { |
||||
return lhs.size() == rhs.size() && |
||||
strncmp(lhs.data(), rhs.data(), lhs.size()) == 0; |
||||
} |
||||
|
||||
inline bool operator!=(StringView lhs, StringView rhs) { return !(lhs == rhs); } |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_STRING_VIEW_H */ |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue