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> 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(), 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}; 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()) { if (get_config()->downstream_addr_groups.empty()) {
DownstreamAddr addr; DownstreamAddr addr;
addr.host = VString(DEFAULT_DOWNSTREAM_HOST); addr.host = ImmutableString::from_lit(DEFAULT_DOWNSTREAM_HOST);
addr.port = DEFAULT_DOWNSTREAM_PORT; addr.port = DEFAULT_DOWNSTREAM_PORT;
DownstreamAddrGroup g("/"); DownstreamAddrGroup g("/");
@ -2536,8 +2536,8 @@ int main(int argc, char **argv) {
// for AF_UNIX socket, we use "localhost" as host for backend // for AF_UNIX socket, we use "localhost" as host for backend
// hostport. This is used as Host header field to backend and // hostport. This is used as Host header field to backend and
// not going to be passed to any syscalls. // not going to be passed to any syscalls.
addr.hostport = addr.hostport = ImmutableString(
VString(util::make_hostport("localhost", get_config()->port)); util::make_hostport("localhost", get_config()->port));
auto path = addr.host.c_str(); auto path = addr.host.c_str();
auto pathlen = addr.host.size(); auto pathlen = addr.host.size();
@ -2560,7 +2560,7 @@ int main(int argc, char **argv) {
} }
addr.hostport = 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( if (resolve_hostname(
&addr.addr, addr.host.c_str(), addr.port, &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) ? construct_absolute_request_uri(req)
: req.path.empty() : req.path.empty()
? req.method == HTTP_OPTIONS ? req.method == HTTP_OPTIONS
? StringAdaptor::from_lit("*") ? StringRef::from_lit("*")
: StringAdaptor::from_lit("-") : StringRef::from_lit("-")
: req.path, : req.path,
alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl), 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, get_config()->accesslog_format,
LogSpec{ LogSpec{
nullptr, ipaddr_, nullptr, ipaddr_,
StringAdaptor::from_lit("-"), // method StringRef::from_lit("-"), // method
StringAdaptor::from_lit("-"), // path, StringRef::from_lit("-"), // path,
alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl), alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl),
time_now, time_now,
highres_now, // request_start_time TODO is highres_now, // request_start_time TODO is

View File

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

View File

@ -245,8 +245,8 @@ struct DownstreamAddr {
Address addr; Address addr;
// backend address. If |host_unix| is true, this is UNIX domain // backend address. If |host_unix| is true, this is UNIX domain
// socket path. // socket path.
VString host; ImmutableString host;
VString hostport; ImmutableString hostport;
// backend port. 0 if |host_unix| is true. // backend port. 0 if |host_unix| is true.
uint16_t port; uint16_t port;
// true if |host| contains UNIX domain socket path. // true if |host| contains UNIX domain socket path.
@ -311,7 +311,7 @@ struct Config {
// string is provided. // string is provided.
std::string forwarded_for_obfuscated; std::string forwarded_for_obfuscated;
std::string backend_tls_sni_name; std::string backend_tls_sni_name;
StringAdaptor server_name; StringRef server_name;
std::chrono::seconds tls_session_timeout; std::chrono::seconds tls_session_timeout;
ev_tstamp http2_upstream_read_timeout; ev_tstamp http2_upstream_read_timeout;
ev_tstamp 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 // For HTTP/1.0 request, there is no authority in request. In that
// case, we use backend server's host nonetheless. // 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()) { if (no_host_rewrite && !req.authority.empty()) {
authority = req.authority; 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 // For HTTP/1.0 request, there is no authority in request. In that
// case, we use backend server's host nonetheless. // 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 || auto no_host_rewrite = get_config()->no_host_rewrite ||
get_config()->http2_proxy || get_config()->http2_proxy ||
get_config()->client_proxy || connect_method; get_config()->client_proxy || connect_method;
if (no_host_rewrite && !req.authority.empty()) { if (no_host_rewrite && !req.authority.empty()) {
authority = StringAdaptor(req.authority); authority = StringRef(req.authority);
} }
downstream_->set_request_downstream_host(authority.str()); 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 { namespace {
template <typename OutputIterator> 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) { OutputIterator oitr) {
return copy(src.c_str(), src.size(), avail, oitr); return copy(src.c_str(), src.size(), avail, oitr);
} }

View File

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

View File

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