From 4f07db8bcb044cb31e4c57780125236f8a096b2f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 17 Jan 2016 11:33:45 +0900 Subject: [PATCH] src: Rename our new string classes --- src/http2.h | 2 +- src/shrpx.cc | 8 +-- src/shrpx_client_handler.cc | 8 +-- src/shrpx_config.cc | 4 +- src/shrpx_config.h | 6 +-- src/shrpx_http2_downstream_connection.cc | 2 +- src/shrpx_http_downstream_connection.cc | 4 +- src/shrpx_log.cc | 2 +- src/shrpx_log.h | 10 ++-- src/shrpx_ssl.cc | 4 +- src/template.h | 62 ++++++++++++++---------- 11 files changed, 61 insertions(+), 51 deletions(-) diff --git a/src/http2.h b/src/http2.h index a96a0fa0..0706b0ab 100644 --- a/src/http2.h +++ b/src/http2.h @@ -152,7 +152,7 @@ nghttp2_nv make_nv_ls_nocopy(const char(&name)[N], const std::string &value) { } template -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}; } diff --git a/src/shrpx.cc b/src/shrpx.cc index 356d9c0f..ae90fc20 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -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, diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index e9cc311b..6d05af72 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -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 diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index b3028b8a..43433f6a 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -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; } diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 39197676..ef0f66fc 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -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; diff --git a/src/shrpx_http2_downstream_connection.cc b/src/shrpx_http2_downstream_connection.cc index 350d618a..e46cefed 100644 --- a/src/shrpx_http2_downstream_connection.cc +++ b/src/shrpx_http2_downstream_connection.cc @@ -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; diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 9c3fc42b..cb129d5b 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -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()); diff --git a/src/shrpx_log.cc b/src/shrpx_log.cc index e880caa0..84498b72 100644 --- a/src/shrpx_log.cc +++ b/src/shrpx_log.cc @@ -184,7 +184,7 @@ std::pair copy(const std::string &src, size_t avail, namespace { template -std::pair copy(const StringAdaptor &src, size_t avail, +std::pair copy(const StringRef &src, size_t avail, OutputIterator oitr) { return copy(src.c_str(), src.size(), avail, oitr); } diff --git a/src/shrpx_log.h b/src/shrpx_log.h index 018206ab..84d509cf 100644 --- a/src/shrpx_log.h +++ b/src/shrpx_log.h @@ -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; }; diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index b6e613e2..61c75fcd 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -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"; diff --git a/src/template.h b/src/template.h index d0314679..3b617e54 100644 --- a/src/template.h +++ b/src/template.h @@ -211,22 +211,25 @@ inline std::unique_ptr strcopy(const std::unique_ptr &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 - 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 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 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 - 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 static StringAdaptor from_lit(const char(&s)[N]) { - return StringAdaptor(s, N - 1); + template 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; };