src: Rename our new string classes

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-17 11:33:45 +09:00
parent 959d378f2a
commit 4f07db8bcb
11 changed files with 61 additions and 51 deletions

View File

@ -152,7 +152,7 @@ nghttp2_nv make_nv_ls_nocopy(const char(&name)[N], const std::string &value) {
}
template <size_t N>
nghttp2_nv make_nv_ls_nocopy(const char(&name)[N], const StringAdaptor &value) {
nghttp2_nv make_nv_ls_nocopy(const char(&name)[N], const StringRef &value) {
return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
NGHTTP2_NV_FLAG_NO_COPY_NAME | NGHTTP2_NV_FLAG_NO_COPY_VALUE};
}

View File

@ -2474,7 +2474,7 @@ int main(int argc, char **argv) {
if (get_config()->downstream_addr_groups.empty()) {
DownstreamAddr addr;
addr.host = VString(DEFAULT_DOWNSTREAM_HOST);
addr.host = ImmutableString::from_lit(DEFAULT_DOWNSTREAM_HOST);
addr.port = DEFAULT_DOWNSTREAM_PORT;
DownstreamAddrGroup g("/");
@ -2536,8 +2536,8 @@ int main(int argc, char **argv) {
// for AF_UNIX socket, we use "localhost" as host for backend
// hostport. This is used as Host header field to backend and
// not going to be passed to any syscalls.
addr.hostport =
VString(util::make_hostport("localhost", get_config()->port));
addr.hostport = ImmutableString(
util::make_hostport("localhost", get_config()->port));
auto path = addr.host.c_str();
auto pathlen = addr.host.size();
@ -2560,7 +2560,7 @@ int main(int argc, char **argv) {
}
addr.hostport =
VString(util::make_hostport(addr.host.c_str(), addr.port));
ImmutableString(util::make_hostport(addr.host.c_str(), addr.port));
if (resolve_hostname(
&addr.addr, addr.host.c_str(), addr.port,

View File

@ -843,8 +843,8 @@ void ClientHandler::write_accesslog(Downstream *downstream) {
? construct_absolute_request_uri(req)
: req.path.empty()
? req.method == HTTP_OPTIONS
? StringAdaptor::from_lit("*")
: StringAdaptor::from_lit("-")
? StringRef::from_lit("*")
: StringRef::from_lit("-")
: req.path,
alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl),
@ -869,8 +869,8 @@ void ClientHandler::write_accesslog(int major, int minor, unsigned int status,
get_config()->accesslog_format,
LogSpec{
nullptr, ipaddr_,
StringAdaptor::from_lit("-"), // method
StringAdaptor::from_lit("-"), // path,
StringRef::from_lit("-"), // method
StringRef::from_lit("-"), // path,
alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl),
time_now,
highres_now, // request_start_time TODO is

View File

@ -1393,7 +1393,7 @@ int parse_config(const char *opt, const char *optarg,
DownstreamAddr addr;
if (util::istarts_with(optarg, SHRPX_UNIX_PATH_PREFIX)) {
auto path = optarg + str_size(SHRPX_UNIX_PATH_PREFIX);
addr.host = VString(path, pat_delim);
addr.host = ImmutableString(path, pat_delim);
addr.host_unix = true;
} else {
if (split_host_port(host, sizeof(host), &port, optarg,
@ -1401,7 +1401,7 @@ int parse_config(const char *opt, const char *optarg,
return -1;
}
addr.host = VString(host);
addr.host = ImmutableString(host);
addr.port = port;
}

View File

@ -245,8 +245,8 @@ struct DownstreamAddr {
Address addr;
// backend address. If |host_unix| is true, this is UNIX domain
// socket path.
VString host;
VString hostport;
ImmutableString host;
ImmutableString hostport;
// backend port. 0 if |host_unix| is true.
uint16_t port;
// true if |host| contains UNIX domain socket path.
@ -311,7 +311,7 @@ struct Config {
// string is provided.
std::string forwarded_for_obfuscated;
std::string backend_tls_sni_name;
StringAdaptor server_name;
StringRef server_name;
std::chrono::seconds tls_session_timeout;
ev_tstamp http2_upstream_read_timeout;
ev_tstamp upstream_read_timeout;

View File

@ -269,7 +269,7 @@ int Http2DownstreamConnection::push_request_headers() {
// For HTTP/1.0 request, there is no authority in request. In that
// case, we use backend server's host nonetheless.
auto authority = StringAdaptor(downstream_hostport);
auto authority = StringRef(downstream_hostport);
if (no_host_rewrite && !req.authority.empty()) {
authority = req.authority;

View File

@ -219,13 +219,13 @@ int HttpDownstreamConnection::push_request_headers() {
// For HTTP/1.0 request, there is no authority in request. In that
// case, we use backend server's host nonetheless.
auto authority = StringAdaptor(downstream_hostport);
auto authority = StringRef(downstream_hostport);
auto no_host_rewrite = get_config()->no_host_rewrite ||
get_config()->http2_proxy ||
get_config()->client_proxy || connect_method;
if (no_host_rewrite && !req.authority.empty()) {
authority = StringAdaptor(req.authority);
authority = StringRef(req.authority);
}
downstream_->set_request_downstream_host(authority.str());

View File

@ -184,7 +184,7 @@ std::pair<OutputIterator, size_t> copy(const std::string &src, size_t avail,
namespace {
template <typename OutputIterator>
std::pair<OutputIterator, size_t> copy(const StringAdaptor &src, size_t avail,
std::pair<OutputIterator, size_t> copy(const StringRef &src, size_t avail,
OutputIterator oitr) {
return copy(src.c_str(), src.size(), avail, oitr);
}

View File

@ -142,10 +142,10 @@ struct LogFragment {
struct LogSpec {
Downstream *downstream;
StringAdaptor remote_addr;
StringAdaptor method;
StringAdaptor path;
StringAdaptor alpn;
StringRef remote_addr;
StringRef method;
StringRef path;
StringRef alpn;
const nghttp2::ssl::TLSSessionInfo *tls_info;
std::chrono::system_clock::time_point time_now;
std::chrono::high_resolution_clock::time_point request_start_time;
@ -153,7 +153,7 @@ struct LogSpec {
int major, minor;
unsigned int status;
int64_t body_bytes_sent;
StringAdaptor remote_port;
StringRef remote_port;
uint16_t server_port;
pid_t pid;
};

View File

@ -972,8 +972,8 @@ int check_cert(SSL *ssl, const DownstreamAddr *addr) {
return -1;
}
auto hostname = !get_config()->backend_tls_sni_name.empty()
? StringAdaptor(get_config()->backend_tls_sni_name)
: StringAdaptor(addr->host);
? StringRef(get_config()->backend_tls_sni_name)
: StringRef(addr->host);
if (verify_hostname(cert, hostname.c_str(), hostname.size(), &addr->addr) !=
0) {
LOG(ERROR) << "Certificate verification failed: hostname does not match";

View File

@ -211,22 +211,25 @@ inline std::unique_ptr<char[]> strcopy(const std::unique_ptr<char[]> &val,
return strcopy(val.get(), val.get() + n);
}
// VString represents string. It has c_str() and size() functions to
// mimic std::string. It manages buffer by itself. Just like
// std::string, c_str() returns NULL-terminated string, but NULL
// character may appear before the final terminal NULL.
struct VString {
VString() : len(0) {}
VString(const char *s, size_t slen) : len(slen), base(strcopy(s, len)) {}
VString(const char *s) : len(strlen(s)), base(strcopy(s, len)) {}
VString(const std::string &s) : len(s.size()), base(strcopy(s)) {}
// ImmutableString represents string that is immutable unlike
// std::string. It has c_str() and size() functions to mimic
// std::string. It manages buffer by itself. Just like std::string,
// c_str() returns NULL-terminated string, but NULL character may
// appear before the final terminal NULL.
class ImmutableString {
public:
ImmutableString() : len(0) {}
ImmutableString(const char *s, size_t slen)
: len(slen), base(strcopy(s, len)) {}
ImmutableString(const char *s) : len(strlen(s)), base(strcopy(s, len)) {}
ImmutableString(const std::string &s) : len(s.size()), base(strcopy(s)) {}
template <typename InputIt>
VString(InputIt first, InputIt last)
ImmutableString(InputIt first, InputIt last)
: len(std::distance(first, last)), base(strcopy(first, last)) {}
VString(const VString &other)
ImmutableString(const ImmutableString &other)
: len(other.len), base(strcopy(other.base, other.len)) {}
VString(VString &&) = default;
VString &operator=(const VString &other) {
ImmutableString(ImmutableString &&) = default;
ImmutableString &operator=(const ImmutableString &other) {
if (this == &other) {
return *this;
}
@ -234,30 +237,36 @@ struct VString {
base = strcopy(other.base, other.len);
return *this;
}
VString &operator=(VString &&other) = default;
ImmutableString &operator=(ImmutableString &&other) = default;
template <size_t N> static ImmutableString from_lit(const char(&s)[N]) {
return ImmutableString(s, N - 1);
}
const char *c_str() const { return base.get(); }
size_t size() const { return len; }
private:
size_t len;
std::unique_ptr<char[]> base;
};
// StringAdaptor behaves like simple string, but it does not own
// pointer. When it is default constructed, it has empty string. You
// can freely copy or move around this struct, but never free its
// pointer. str() function can be used to export the content as
// std::string.
struct StringAdaptor {
StringAdaptor() : base(""), len(0) {}
// StringRef is a reference to a string owned by something else. So
// it behaves like simple string, but it does not own pointer. When
// it is default constructed, it has empty string. You can freely
// copy or move around this struct, but never free its pointer. str()
// function can be used to export the content as std::string.
class StringRef {
public:
StringRef() : base(""), len(0) {}
template <typename T>
StringAdaptor(const T &s)
StringRef(const T &s)
: base(s.c_str()), len(s.size()) {}
StringAdaptor(const char *s) : base(s), len(strlen(s)) {}
StringAdaptor(const char *s, size_t n) : base(s), len(n) {}
StringRef(const char *s) : base(s), len(strlen(s)) {}
StringRef(const char *s, size_t n) : base(s), len(n) {}
template <size_t N> static StringAdaptor from_lit(const char(&s)[N]) {
return StringAdaptor(s, N - 1);
template <size_t N> static StringRef from_lit(const char(&s)[N]) {
return StringRef(s, N - 1);
}
const char *c_str() const { return base; }
@ -265,6 +274,7 @@ struct StringAdaptor {
std::string str() const { return std::string(base, len); }
private:
const char *base;
size_t len;
};