From ef5d981ab1c13d3fb10457dd869b98a411dce0ac Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 17 Jan 2016 17:04:16 +0900 Subject: [PATCH] nghttpx: Simplify --- src/memchunk.h | 1 + src/shrpx_http2_session.cc | 13 +++++-------- src/shrpx_http2_upstream.cc | 2 +- src/shrpx_http_downstream_connection.cc | 8 ++++---- src/shrpx_https_upstream.cc | 22 +++++++++------------- src/shrpx_spdy_upstream.cc | 2 +- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/memchunk.h b/src/memchunk.h index 6245f61f..54381bee 100644 --- a/src/memchunk.h +++ b/src/memchunk.h @@ -165,6 +165,7 @@ template struct Memchunks { return append(s, N - 1); } size_t append(const std::string &s) { return append(s.c_str(), s.size()); } + size_t append(const StringRef &s) { return append(s.c_str(), s.size()); } size_t remove(void *dest, size_t count) { if (!tail || count == 0) { return 0; diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 57f406f1..b67a1a0d 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -334,18 +334,15 @@ int Http2Session::initiate_connection() { conn_.set_ssl(ssl); } - const char *sni_name = nullptr; - if (!get_config()->backend_tls_sni_name.empty()) { - sni_name = get_config()->backend_tls_sni_name.c_str(); - } else { - sni_name = downstream_addr.host.c_str(); - } + StringRef sni_name = !get_config()->backend_tls_sni_name.empty() + ? get_config()->backend_tls_sni_name + : downstream_addr.host; - if (sni_name && !util::numeric_host(sni_name)) { + if (!util::numeric_host(sni_name.c_str())) { // TLS extensions: SNI. There is no documentation about the return // code for this function (actually this is macro wrapping SSL_ctrl // at the time of this writing). - SSL_set_tlsext_host_name(conn_.tls.ssl, sni_name); + SSL_set_tlsext_host_name(conn_.tls.ssl, sni_name.c_str()); } // If state_ == PROXY_CONNECTED, we has connected to the proxy // using conn_.fd and tunnel has been established. diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 24dbf85c..03de4fc7 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -1365,7 +1365,7 @@ int Http2Upstream::error_reply(Downstream *downstream, auto html = http::create_error_html(status_code); resp.http_status = status_code; auto body = downstream->get_response_buf(); - body->append(html.c_str(), html.size()); + body->append(html); downstream->set_response_state(Downstream::MSG_COMPLETE); nghttp2_data_provider data_prd; diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index cb129d5b..4790a84d 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -238,14 +238,14 @@ int HttpDownstreamConnection::push_request_headers() { buf->append(" "); if (connect_method) { - buf->append(authority.c_str(), authority.size()); + buf->append(authority); } else if (get_config()->http2_proxy || get_config()->client_proxy) { // Construct absolute-form request target because we are going to // send a request to a HTTP/1 proxy. assert(!req.scheme.empty()); buf->append(req.scheme); buf->append("://"); - buf->append(authority.c_str(), authority.size()); + buf->append(authority); buf->append(req.path); } else if (req.method == HTTP_OPTIONS && req.path.empty()) { // Server-wide OPTIONS @@ -254,7 +254,7 @@ int HttpDownstreamConnection::push_request_headers() { buf->append(req.path); } buf->append(" HTTP/1.1\r\nHost: "); - buf->append(authority.c_str(), authority.size()); + buf->append(authority); buf->append("\r\n"); http2::build_http1_headers_from_headers(buf, req.fs.headers()); @@ -402,7 +402,7 @@ int HttpDownstreamConnection::push_upload_data_chunk(const uint8_t *data, if (chunked) { auto chunk_size_hex = util::utox(datalen); - output->append(chunk_size_hex.c_str(), chunk_size_hex.size()); + output->append(chunk_size_hex); output->append("\r\n"); } diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index e38fe526..c4466fa5 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -802,8 +802,7 @@ int HttpsUpstream::send_reply(Downstream *downstream, const uint8_t *body, if (!resp.fs.header(http2::HD_SERVER)) { output->append("Server: "); - const auto &server_name = get_config()->server_name; - output->append(server_name.c_str(), server_name.size()); + output->append(get_config()->server_name); output->append("\r\n"); } @@ -838,21 +837,21 @@ void HttpsUpstream::error_reply(unsigned int status_code) { output->append("HTTP/1.1 "); auto status_str = http2::get_status_string(status_code); - output->append(status_str.c_str(), status_str.size()); + output->append(status_str); output->append("\r\nServer: "); const auto &server_name = get_config()->server_name; - output->append(server_name.c_str(), server_name.size()); + output->append(server_name); output->append("\r\nContent-Length: "); auto cl = util::utos(html.size()); - output->append(cl.c_str(), cl.size()); + output->append(cl); output->append("\r\nDate: "); auto lgconf = log_config(); lgconf->update_tstamp(std::chrono::system_clock::now()); auto &date = lgconf->time_http_str; - output->append(date.c_str(), date.size()); + output->append(date); output->append("\r\nContent-Type: text/html; " "charset=UTF-8\r\nConnection: close\r\n\r\n"); - output->append(html.c_str(), html.size()); + output->append(html); downstream->response_sent_body_length += html.size(); downstream->set_response_state(Downstream::MSG_COMPLETE); @@ -1000,8 +999,7 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) { if (!get_config()->http2_proxy && !get_config()->client_proxy) { buf->append("Server: "); - const auto &server_name = get_config()->server_name; - buf->append(server_name.c_str(), server_name.size()); + buf->append(get_config()->server_name); buf->append("\r\n"); } else { auto server = resp.fs.header(http2::HD_SERVER); @@ -1054,10 +1052,8 @@ int HttpsUpstream::on_downstream_body(Downstream *downstream, } auto output = downstream->get_response_buf(); if (downstream->get_chunked_response()) { - auto chunk_size_hex = util::utox(len); - chunk_size_hex += "\r\n"; - - output->append(chunk_size_hex.c_str(), chunk_size_hex.size()); + output->append(util::utox(len)); + output->append("\r\n"); } output->append(data, len); diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index a9dc4a04..2ed20c52 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -905,7 +905,7 @@ int SpdyUpstream::error_reply(Downstream *downstream, auto html = http::create_error_html(status_code); resp.http_status = status_code; auto body = downstream->get_response_buf(); - body->append(html.c_str(), html.size()); + body->append(html); downstream->set_response_state(Downstream::MSG_COMPLETE); spdylay_data_provider data_prd;