src: Move make_unique to nghttp2 namespace

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-05 23:21:53 +09:00
parent f8f9b36acd
commit 54851ef7a6
19 changed files with 127 additions and 79 deletions

View File

@ -47,6 +47,7 @@
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "ssl.h" #include "ssl.h"
#include "template.h"
#ifndef O_BINARY #ifndef O_BINARY
#define O_BINARY (0) #define O_BINARY (0)
@ -223,7 +224,7 @@ public:
} }
} }
auto handler = auto handler =
util::make_unique<Http2Handler>(this, fd, ssl, get_next_session_id()); make_unique<Http2Handler>(this, fd, ssl, get_next_session_id());
handler->setup_bev(); handler->setup_bev();
if (!ssl) { if (!ssl) {
if (handler->on_connect() != 0) { if (handler->on_connect() != 0) {
@ -754,7 +755,7 @@ int Http2Handler::submit_push_promise(Stream *stream,
return promised_stream_id; return promised_stream_id;
} }
auto promised_stream = util::make_unique<Stream>(this, promised_stream_id); auto promised_stream = make_unique<Stream>(this, promised_stream_id);
append_nv(promised_stream.get(), nva); append_nv(promised_stream.get(), nva);
add_stream(promised_stream_id, std::move(promised_stream)); add_stream(promised_stream_id, std::move(promised_stream));
@ -1083,7 +1084,7 @@ int on_begin_headers_callback(nghttp2_session *session,
return 0; return 0;
} }
auto stream = util::make_unique<Stream>(hd, frame->hd.stream_id); auto stream = make_unique<Stream>(hd, frame->hd.stream_id);
add_stream_read_timeout(stream.get()); add_stream_read_timeout(stream.get());
@ -1362,10 +1363,10 @@ public:
if (config_->verbose) { if (config_->verbose) {
std::cerr << "spawning thread #" << i << std::endl; std::cerr << "spawning thread #" << i << std::endl;
} }
auto worker = util::make_unique<Worker>(); auto worker = make_unique<Worker>();
auto loop = ev_loop_new(0); auto loop = ev_loop_new(0);
worker->sessions = worker->sessions =
util::make_unique<Sessions>(loop, config_, sessions_->get_ssl_ctx()); make_unique<Sessions>(loop, config_, sessions_->get_ssl_ctx());
ev_async_init(&worker->w, worker_acceptcb); ev_async_init(&worker->w, worker_acceptcb);
worker->w.data = worker.get(); worker->w.data = worker.get();
ev_async_start(loop, &worker->w); ev_async_start(loop, &worker->w);

View File

@ -120,7 +120,7 @@ NGHTTPX_SRCS = \
shrpx_downstream_connection_pool.cc shrpx_downstream_connection_pool.h \ shrpx_downstream_connection_pool.cc shrpx_downstream_connection_pool.h \
shrpx_rate_limit.cc shrpx_rate_limit.h \ shrpx_rate_limit.cc shrpx_rate_limit.h \
shrpx_connection.cc shrpx_connection.h \ shrpx_connection.cc shrpx_connection.h \
buffer.h memchunk.h buffer.h memchunk.h template.h
if HAVE_SPDYLAY if HAVE_SPDYLAY
NGHTTPX_SRCS += shrpx_spdy_upstream.cc shrpx_spdy_upstream.h NGHTTPX_SRCS += shrpx_spdy_upstream.cc shrpx_spdy_upstream.h

View File

@ -28,12 +28,13 @@
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "template.h"
namespace nghttp2 { namespace nghttp2 {
namespace asio_http2 { namespace asio_http2 {
channel::channel() : impl_(util::make_unique<channel_impl>()) {} channel::channel() : impl_(make_unique<channel_impl>()) {}
void channel::post(void_cb cb) { impl_->post(std::move(cb)); } void channel::post(void_cb cb) { impl_->post(std::move(cb)); }
@ -51,7 +52,7 @@ namespace server {
extern std::shared_ptr<std::string> cached_date; extern std::shared_ptr<std::string> cached_date;
request::request() : impl_(util::make_unique<request_impl>()) {} request::request() : impl_(make_unique<request_impl>()) {}
const std::vector<header> &request::headers() const { return impl_->headers(); } const std::vector<header> &request::headers() const { return impl_->headers(); }
@ -84,7 +85,7 @@ bool request::run_task(thread_cb start) {
request_impl &request::impl() { return *impl_; } request_impl &request::impl() { return *impl_; }
response::response() : impl_(util::make_unique<response_impl>()) {} response::response() : impl_(make_unique<response_impl>()) {}
void response::write_head(unsigned int status_code, void response::write_head(unsigned int status_code,
std::vector<header> headers) { std::vector<header> headers) {

View File

@ -33,6 +33,7 @@
#include "asio_server.h" #include "asio_server.h"
#include "util.h" #include "util.h"
#include "ssl.h" #include "ssl.h"
#include "template.h"
namespace nghttp2 { namespace nghttp2 {
@ -40,7 +41,7 @@ namespace asio_http2 {
namespace server { namespace server {
http2::http2() : impl_(util::make_unique<http2_impl>()) {} http2::http2() : impl_(make_unique<http2_impl>()) {}
http2::~http2() {} http2::~http2() {}
@ -75,7 +76,7 @@ void http2_impl::listen(const std::string &address, uint16_t port,
std::unique_ptr<boost::asio::ssl::context> ssl_ctx; std::unique_ptr<boost::asio::ssl::context> ssl_ctx;
if (!private_key_file_.empty() && !certificate_file_.empty()) { if (!private_key_file_.empty() && !certificate_file_.empty()) {
ssl_ctx = util::make_unique<boost::asio::ssl::context>( ssl_ctx = make_unique<boost::asio::ssl::context>(
boost::asio::ssl::context::sslv23); boost::asio::ssl::context::sslv23);
ssl_ctx->use_private_key_file(private_key_file_, ssl_ctx->use_private_key_file(private_key_file_,

View File

@ -55,6 +55,7 @@
#include "ssl.h" #include "ssl.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -399,13 +400,13 @@ int Client::on_connect() {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
if (next_proto) { if (next_proto) {
if (util::check_h2_is_selected(next_proto, next_proto_len)) { if (util::check_h2_is_selected(next_proto, next_proto_len)) {
session = util::make_unique<Http2Session>(this); session = make_unique<Http2Session>(this);
} else { } else {
#ifdef HAVE_SPDYLAY #ifdef HAVE_SPDYLAY
auto spdy_version = auto spdy_version =
spdylay_npn_get_version(next_proto, next_proto_len); spdylay_npn_get_version(next_proto, next_proto_len);
if (spdy_version) { if (spdy_version) {
session = util::make_unique<SpdySession>(this, spdy_version); session = make_unique<SpdySession>(this, spdy_version);
} else { } else {
debug_nextproto_error(); debug_nextproto_error();
fail(); fail();
@ -434,17 +435,17 @@ int Client::on_connect() {
} else { } else {
switch (config.no_tls_proto) { switch (config.no_tls_proto) {
case Config::PROTO_HTTP2: case Config::PROTO_HTTP2:
session = util::make_unique<Http2Session>(this); session = make_unique<Http2Session>(this);
break; break;
#ifdef HAVE_SPDYLAY #ifdef HAVE_SPDYLAY
case Config::PROTO_SPDY2: case Config::PROTO_SPDY2:
session = util::make_unique<SpdySession>(this, SPDYLAY_PROTO_SPDY2); session = make_unique<SpdySession>(this, SPDYLAY_PROTO_SPDY2);
break; break;
case Config::PROTO_SPDY3: case Config::PROTO_SPDY3:
session = util::make_unique<SpdySession>(this, SPDYLAY_PROTO_SPDY3); session = make_unique<SpdySession>(this, SPDYLAY_PROTO_SPDY3);
break; break;
case Config::PROTO_SPDY3_1: case Config::PROTO_SPDY3_1:
session = util::make_unique<SpdySession>(this, SPDYLAY_PROTO_SPDY3_1); session = make_unique<SpdySession>(this, SPDYLAY_PROTO_SPDY3_1);
break; break;
#endif // HAVE_SPDYLAY #endif // HAVE_SPDYLAY
default: default:
@ -704,7 +705,7 @@ Worker::Worker(uint32_t id, SSL_CTX *ssl_ctx, size_t req_todo, size_t nclients,
++req_todo; ++req_todo;
--nreqs_rem; --nreqs_rem;
} }
clients.push_back(util::make_unique<Client>(this, req_todo)); clients.push_back(make_unique<Client>(this, req_todo));
} }
} }
@ -1327,7 +1328,7 @@ int main(int argc, char **argv) {
<< " concurrent clients, " << nreqs << " total requests" << " concurrent clients, " << nreqs << " total requests"
<< std::endl; << std::endl;
workers.push_back( workers.push_back(
util::make_unique<Worker>(i, ssl_ctx, nreqs, nclients, &config)); make_unique<Worker>(i, ssl_ctx, nreqs, nclients, &config));
auto &worker = workers.back(); auto &worker = workers.back();
futures.push_back( futures.push_back(
std::async(std::launch::async, [&worker]() { worker->run(); })); std::async(std::launch::async, [&worker]() { worker->run(); }));
@ -1339,8 +1340,8 @@ int main(int argc, char **argv) {
std::cout << "spawning thread #" << (config.nthreads - 1) << ": " std::cout << "spawning thread #" << (config.nthreads - 1) << ": "
<< nclients_last << " concurrent clients, " << nreqs_last << nclients_last << " concurrent clients, " << nreqs_last
<< " total requests" << std::endl; << " total requests" << std::endl;
workers.push_back(util::make_unique<Worker>( workers.push_back(make_unique<Worker>(config.nthreads - 1, ssl_ctx,
config.nthreads - 1, ssl_ctx, nreqs_last, nclients_last, &config)); nreqs_last, nclients_last, &config));
workers.back()->run(); workers.back()->run();
#ifndef NOTHREADS #ifndef NOTHREADS

View File

@ -29,11 +29,12 @@
#include <sys/uio.h> #include <sys/uio.h>
#include <cassert>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <array> #include <array>
#include "util.h" #include "template.h"
namespace nghttp2 { namespace nghttp2 {
@ -67,7 +68,7 @@ template <typename T> struct Pool {
return m; return m;
} }
pool = util::make_unique<T>(std::move(pool)); pool = make_unique<T>(std::move(pool));
poolsize += T::size; poolsize += T::size;
return pool.get(); return pool.get();
} }

View File

@ -29,6 +29,7 @@
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "memchunk.h" #include "memchunk.h"
#include "util.h"
namespace nghttp2 { namespace nghttp2 {

View File

@ -53,6 +53,7 @@
#include "util.h" #include "util.h"
#include "base64.h" #include "base64.h"
#include "ssl.h" #include "ssl.h"
#include "template.h"
#ifndef O_BINARY #ifndef O_BINARY
#define O_BINARY (0) #define O_BINARY (0)
@ -731,7 +732,7 @@ int HttpClient::connected() {
writefn = &HttpClient::write_clear; writefn = &HttpClient::write_clear;
if (need_upgrade()) { if (need_upgrade()) {
htp = util::make_unique<http_parser>(); htp = make_unique<http_parser>();
http_parser_init(htp.get(), HTTP_RESPONSE); http_parser_init(htp.get(), HTTP_RESPONSE);
htp->data = this; htp->data = this;
@ -1234,8 +1235,8 @@ bool HttpClient::add_request(const std::string &uri,
path_cache.insert(uri); path_cache.insert(uri);
} }
reqvec.push_back(util::make_unique<Request>( reqvec.push_back(make_unique<Request>(uri, u, data_prd, data_length, pri_spec,
uri, u, data_prd, data_length, pri_spec, std::move(dep), pri, level)); std::move(dep), pri, level));
return true; return true;
} }
@ -1632,7 +1633,7 @@ int on_begin_headers_callback(nghttp2_session *session,
nghttp2_priority_spec_default_init(&pri_spec); nghttp2_priority_spec_default_init(&pri_spec);
auto req = util::make_unique<Request>("", u, nullptr, 0, pri_spec, nullptr); auto req = make_unique<Request>("", u, nullptr, 0, pri_spec, nullptr);
req->stream_id = stream_id; req->stream_id = stream_id;
nghttp2_session_set_stream_user_data(session, stream_id, req.get()); nghttp2_session_set_stream_user_data(session, stream_id, req.get());

View File

@ -66,6 +66,7 @@
#include "util.h" #include "util.h"
#include "app_helper.h" #include "app_helper.h"
#include "ssl.h" #include "ssl.h"
#include "template.h"
extern char **environ; extern char **environ;
@ -153,7 +154,7 @@ std::unique_ptr<AcceptHandler> create_acceptor(ConnectionHandler *handler,
if (port == get_config()->port) { if (port == get_config()->port) {
LOG(NOTICE) << "Listening on port " << get_config()->port; LOG(NOTICE) << "Listening on port " << get_config()->port;
return util::make_unique<AcceptHandler>(fd, handler); return make_unique<AcceptHandler>(fd, handler);
} }
LOG(WARN) << "Port was changed between old binary (" << port LOG(WARN) << "Port was changed between old binary (" << port
@ -201,7 +202,7 @@ std::unique_ptr<AcceptHandler> create_acceptor(ConnectionHandler *handler,
if (fd == -1) { if (fd == -1) {
continue; continue;
} }
util::make_socket_nonblocking(fd); make_socket_nonblocking(fd);
#endif // !SOCK_NONBLOCK #endif // !SOCK_NONBLOCK
int val = 1; int val = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
@ -260,7 +261,7 @@ std::unique_ptr<AcceptHandler> create_acceptor(ConnectionHandler *handler,
LOG(NOTICE) << "Listening on " << host << ", port " << get_config()->port; LOG(NOTICE) << "Listening on " << host << ", port " << get_config()->port;
return util::make_unique<AcceptHandler>(fd, handler); return make_unique<AcceptHandler>(fd, handler);
} }
} // namespace } // namespace
@ -354,7 +355,7 @@ void exec_binary_signal_cb(struct ev_loop *loop, ev_signal *w, int revents) {
return; return;
} }
auto argv = util::make_unique<char *[]>(get_config()->argc + 1); auto argv = make_unique<char *[]>(get_config()->argc + 1);
argv[0] = exec_path; argv[0] = exec_path;
for (int i = 1; i < get_config()->argc; ++i) { for (int i = 1; i < get_config()->argc; ++i) {
@ -366,7 +367,7 @@ void exec_binary_signal_cb(struct ev_loop *loop, ev_signal *w, int revents) {
for (char **p = environ; *p; ++p, ++envlen) for (char **p = environ; *p; ++p, ++envlen)
; ;
// 3 for missing fd4, fd6 and port. // 3 for missing fd4, fd6 and port.
auto envp = util::make_unique<char *[]>(envlen + 3 + 1); auto envp = make_unique<char *[]>(envlen + 3 + 1);
size_t envidx = 0; size_t envidx = 0;
auto acceptor4 = conn_handler->get_acceptor4(); auto acceptor4 = conn_handler->get_acceptor4();
@ -511,7 +512,7 @@ namespace {
int event_loop() { int event_loop() {
auto loop = EV_DEFAULT; auto loop = EV_DEFAULT;
auto conn_handler = util::make_unique<ConnectionHandler>(loop); auto conn_handler = make_unique<ConnectionHandler>(loop);
if (get_config()->daemon) { if (get_config()->daemon) {
if (daemon(0, 0) == -1) { if (daemon(0, 0) == -1) {
auto error = errno; auto error = errno;
@ -1720,7 +1721,7 @@ int main(int argc, char **argv) {
#ifndef NOTHREADS #ifndef NOTHREADS
std::unique_ptr<nghttp2::ssl::LibsslGlobalLock> lock; std::unique_ptr<nghttp2::ssl::LibsslGlobalLock> lock;
if (!get_config()->tls_ctx_per_worker) { if (!get_config()->tls_ctx_per_worker) {
lock = util::make_unique<nghttp2::ssl::LibsslGlobalLock>(); lock = make_unique<nghttp2::ssl::LibsslGlobalLock>();
} }
#endif // NOTHREADS #endif // NOTHREADS

View File

@ -42,6 +42,7 @@
#include "shrpx_spdy_upstream.h" #include "shrpx_spdy_upstream.h"
#endif // HAVE_SPDYLAY #endif // HAVE_SPDYLAY
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -377,7 +378,7 @@ ClientHandler::ClientHandler(struct ev_loop *loop, int fd, SSL *ssl,
// For non-TLS version, first create HttpsUpstream. It may be // For non-TLS version, first create HttpsUpstream. It may be
// upgraded to HTTP/2 through HTTP Upgrade or direct HTTP/2 // upgraded to HTTP/2 through HTTP Upgrade or direct HTTP/2
// connection. // connection.
upstream_ = util::make_unique<HttpsUpstream>(this); upstream_ = make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1"; alpn_ = "http/1.1";
read_ = &ClientHandler::read_clear; read_ = &ClientHandler::read_clear;
write_ = &ClientHandler::write_clear; write_ = &ClientHandler::write_clear;
@ -455,7 +456,7 @@ int ClientHandler::validate_next_proto() {
on_read_ = &ClientHandler::upstream_http2_connhd_read; on_read_ = &ClientHandler::upstream_http2_connhd_read;
auto http2_upstream = util::make_unique<Http2Upstream>(this); auto http2_upstream = make_unique<Http2Upstream>(this);
if (!ssl::check_http2_requirement(conn_.tls.ssl)) { if (!ssl::check_http2_requirement(conn_.tls.ssl)) {
rv = http2_upstream->terminate_session(NGHTTP2_INADEQUATE_SECURITY); rv = http2_upstream->terminate_session(NGHTTP2_INADEQUATE_SECURITY);
@ -480,7 +481,7 @@ int ClientHandler::validate_next_proto() {
#ifdef HAVE_SPDYLAY #ifdef HAVE_SPDYLAY
uint16_t version = spdylay_npn_get_version(next_proto, next_proto_len); uint16_t version = spdylay_npn_get_version(next_proto, next_proto_len);
if (version) { if (version) {
upstream_ = util::make_unique<SpdyUpstream>(version, this); upstream_ = make_unique<SpdyUpstream>(version, this);
switch (version) { switch (version) {
case SPDYLAY_PROTO_SPDY2: case SPDYLAY_PROTO_SPDY2:
@ -507,7 +508,7 @@ int ClientHandler::validate_next_proto() {
} }
#endif // HAVE_SPDYLAY #endif // HAVE_SPDYLAY
if (next_proto_len == 8 && memcmp("http/1.1", next_proto, 8) == 0) { if (next_proto_len == 8 && memcmp("http/1.1", next_proto, 8) == 0) {
upstream_ = util::make_unique<HttpsUpstream>(this); upstream_ = make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1"; alpn_ = "http/1.1";
// At this point, input buffer is already filled with some // At this point, input buffer is already filled with some
@ -532,7 +533,7 @@ int ClientHandler::validate_next_proto() {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "No protocol negotiated. Fallback to HTTP/1.1"; CLOG(INFO, this) << "No protocol negotiated. Fallback to HTTP/1.1";
} }
upstream_ = util::make_unique<HttpsUpstream>(this); upstream_ = make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1"; alpn_ = "http/1.1";
// At this point, input buffer is already filled with some bytes. // At this point, input buffer is already filled with some bytes.
@ -594,11 +595,10 @@ ClientHandler::get_downstream_connection() {
} }
if (http2session_) { if (http2session_) {
dconn = util::make_unique<Http2DownstreamConnection>(dconn_pool_,
http2session_);
} else {
dconn = dconn =
util::make_unique<HttpDownstreamConnection>(dconn_pool_, conn_.loop); make_unique<Http2DownstreamConnection>(dconn_pool_, http2session_);
} else {
dconn = make_unique<HttpDownstreamConnection>(dconn_pool_, conn_.loop);
} }
dconn->set_client_handler(this); dconn->set_client_handler(this);
return dconn; return dconn;
@ -632,7 +632,7 @@ ConnectBlocker *ClientHandler::get_http1_connect_blocker() const {
} }
void ClientHandler::direct_http2_upgrade() { void ClientHandler::direct_http2_upgrade() {
upstream_ = util::make_unique<Http2Upstream>(this); upstream_ = make_unique<Http2Upstream>(this);
// TODO We don't know exact h2 draft version in direct upgrade. We // TODO We don't know exact h2 draft version in direct upgrade. We
// just use library default for now. // just use library default for now.
alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID; alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID;
@ -640,7 +640,7 @@ void ClientHandler::direct_http2_upgrade() {
} }
int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) { int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) {
auto upstream = util::make_unique<Http2Upstream>(this); auto upstream = make_unique<Http2Upstream>(this);
if (upstream->upgrade_upstream(http) != 0) { if (upstream->upgrade_upstream(http) != 0) {
return -1; return -1;
} }

View File

@ -46,6 +46,7 @@
#include "shrpx_http.h" #include "shrpx_http.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -208,7 +209,7 @@ bool is_secure(const char *filename) {
std::unique_ptr<TicketKeys> std::unique_ptr<TicketKeys>
read_tls_ticket_key_file(const std::vector<std::string> &files) { read_tls_ticket_key_file(const std::vector<std::string> &files) {
auto ticket_keys = util::make_unique<TicketKeys>(); auto ticket_keys = make_unique<TicketKeys>();
auto &keys = ticket_keys->keys; auto &keys = ticket_keys->keys;
keys.resize(files.size()); keys.resize(files.size());
size_t i = 0; size_t i = 0;
@ -251,7 +252,7 @@ FILE *open_file_for_write(const char *filename) {
// We get race condition if execve is called at the same time. // We get race condition if execve is called at the same time.
if (fd != -1) { if (fd != -1) {
util::make_socket_closeonexec(fd); make_socket_closeonexec(fd);
} }
#endif #endif
if (fd == -1) { if (fd == -1) {
@ -293,7 +294,7 @@ std::unique_ptr<char[]> strcopy(const char *val) {
} }
std::unique_ptr<char[]> strcopy(const char *val, size_t len) { std::unique_ptr<char[]> strcopy(const char *val, size_t len) {
auto res = util::make_unique<char[]>(len + 1); auto res = make_unique<char[]>(len + 1);
memcpy(res.get(), val, len); memcpy(res.get(), val, len);
res[len] = '\0'; res[len] = '\0';
return res; return res;

View File

@ -39,6 +39,7 @@
#include "shrpx_downstream_connection.h" #include "shrpx_downstream_connection.h"
#include "shrpx_accept_handler.h" #include "shrpx_accept_handler.h"
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -62,8 +63,7 @@ ConnectionHandler::ConnectionHandler(struct ev_loop *loop)
: loop_(loop), sv_ssl_ctx_(nullptr), cl_ssl_ctx_(nullptr), : loop_(loop), sv_ssl_ctx_(nullptr), cl_ssl_ctx_(nullptr),
// rate_limit_group_(bufferevent_rate_limit_group_new( // rate_limit_group_(bufferevent_rate_limit_group_new(
// evbase, get_config()->worker_rate_limit_cfg)), // evbase, get_config()->worker_rate_limit_cfg)),
worker_stat_(util::make_unique<WorkerStat>()), worker_stat_(make_unique<WorkerStat>()), worker_round_robin_cnt_(0) {
worker_round_robin_cnt_(0) {
ev_timer_init(&disable_acceptor_timer_, acceptor_disable_cb, 0., 0.); ev_timer_init(&disable_acceptor_timer_, acceptor_disable_cb, 0., 0.);
disable_acceptor_timer_.data = this; disable_acceptor_timer_.data = this;
} }
@ -107,9 +107,9 @@ void ConnectionHandler::create_worker_thread(size_t num) {
assert(workers_.size() == 0); assert(workers_.size() == 0);
for (size_t i = 0; i < num; ++i) { for (size_t i = 0; i < num; ++i) {
workers_.push_back(util::make_unique<Worker>(sv_ssl_ctx_, cl_ssl_ctx_, workers_.push_back(make_unique<Worker>(sv_ssl_ctx_, cl_ssl_ctx_,
worker_config->cert_tree, worker_config->cert_tree,
worker_config->ticket_keys)); worker_config->ticket_keys));
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
LLOG(INFO, this) << "Created thread #" << workers_.size() - 1; LLOG(INFO, this) << "Created thread #" << workers_.size() - 1;
@ -209,11 +209,11 @@ struct ev_loop *ConnectionHandler::get_loop() const {
} }
void ConnectionHandler::create_http2_session() { void ConnectionHandler::create_http2_session() {
http2session_ = util::make_unique<Http2Session>(loop_, cl_ssl_ctx_); http2session_ = make_unique<Http2Session>(loop_, cl_ssl_ctx_);
} }
void ConnectionHandler::create_http1_connect_blocker() { void ConnectionHandler::create_http1_connect_blocker() {
http1_connect_blocker_ = util::make_unique<ConnectBlocker>(loop_); http1_connect_blocker_ = make_unique<ConnectBlocker>(loop_);
} }
const WorkerStat *ConnectionHandler::get_worker_stat() const { const WorkerStat *ConnectionHandler::get_worker_stat() const {

View File

@ -43,6 +43,7 @@
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "base64.h" #include "base64.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -265,7 +266,7 @@ int Http2Session::initiate_connection() {
on_read_ = &Http2Session::downstream_read_proxy; on_read_ = &Http2Session::downstream_read_proxy;
on_write_ = &Http2Session::downstream_connect_proxy; on_write_ = &Http2Session::downstream_connect_proxy;
proxy_htp_ = util::make_unique<http_parser>(); proxy_htp_ = make_unique<http_parser>();
http_parser_init(proxy_htp_.get(), HTTP_RESPONSE); http_parser_init(proxy_htp_.get(), HTTP_RESPONSE);
proxy_htp_->data = this; proxy_htp_->data = this;
@ -505,7 +506,7 @@ int Http2Session::submit_request(Http2DownstreamConnection *dconn, int32_t pri,
const nghttp2_nv *nva, size_t nvlen, const nghttp2_nv *nva, size_t nvlen,
const nghttp2_data_provider *data_prd) { const nghttp2_data_provider *data_prd) {
assert(state_ == CONNECTED); assert(state_ == CONNECTED);
auto sd = util::make_unique<StreamData>(); auto sd = make_unique<StreamData>();
// TODO Specify nullptr to pri_spec for now // TODO Specify nullptr to pri_spec for now
auto stream_id = auto stream_id =
nghttp2_submit_request(session_, nullptr, nva, nvlen, data_prd, sd.get()); nghttp2_submit_request(session_, nullptr, nva, nvlen, data_prd, sd.get());

View File

@ -40,6 +40,7 @@
#include "util.h" #include "util.h"
#include "base64.h" #include "base64.h"
#include "app_helper.h" #include "app_helper.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -244,8 +245,7 @@ int on_begin_headers_callback(nghttp2_session *session,
} }
// TODO Use priority 0 for now // TODO Use priority 0 for now
auto downstream = auto downstream = make_unique<Downstream>(upstream, frame->hd.stream_id, 0);
util::make_unique<Downstream>(upstream, frame->hd.stream_id, 0);
downstream->reset_upstream_rtimer(); downstream->reset_upstream_rtimer();

View File

@ -31,13 +31,13 @@
#include "shrpx_client_handler.h" #include "shrpx_client_handler.h"
#include "shrpx_downstream.h" #include "shrpx_downstream.h"
#include "shrpx_downstream_connection.h" #include "shrpx_downstream_connection.h"
//#include "shrpx_http2_downstream_connection.h"
#include "shrpx_http.h" #include "shrpx_http.h"
#include "shrpx_config.h" #include "shrpx_config.h"
#include "shrpx_error.h" #include "shrpx_error.h"
#include "shrpx_worker_config.h" #include "shrpx_worker_config.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -64,7 +64,7 @@ int htp_msg_begin(http_parser *htp) {
} }
upstream->reset_current_header_length(); upstream->reset_current_header_length();
// TODO specify 0 as priority for now // TODO specify 0 as priority for now
upstream->attach_downstream(util::make_unique<Downstream>(upstream, 0, 0)); upstream->attach_downstream(make_unique<Downstream>(upstream, 0, 0));
return 0; return 0;
} }
} // namespace } // namespace
@ -582,7 +582,7 @@ void HttpsUpstream::error_reply(unsigned int status_code) {
auto downstream = get_downstream(); auto downstream = get_downstream();
if (!downstream) { if (!downstream) {
attach_downstream(util::make_unique<Downstream>(this, 1, 1)); attach_downstream(make_unique<Downstream>(this, 1, 1));
downstream = get_downstream(); downstream = get_downstream();
} }

View File

@ -39,6 +39,7 @@
#include "shrpx_worker_config.h" #include "shrpx_worker_config.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -794,7 +795,7 @@ int SpdyUpstream::error_reply(Downstream *downstream,
Downstream *SpdyUpstream::add_pending_downstream(int32_t stream_id, Downstream *SpdyUpstream::add_pending_downstream(int32_t stream_id,
int32_t priority) { int32_t priority) {
auto downstream = util::make_unique<Downstream>(this, stream_id, priority); auto downstream = make_unique<Downstream>(this, stream_id, priority);
auto res = downstream.get(); auto res = downstream.get();
downstream_queue_.add_pending(std::move(downstream)); downstream_queue_.add_pending(std::move(downstream));
@ -844,7 +845,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) {
} }
size_t nheader = downstream->get_response_headers().size(); size_t nheader = downstream->get_response_headers().size();
// 8 means server, :status, :version and possible via header field. // 8 means server, :status, :version and possible via header field.
auto nv = util::make_unique<const char *[]>( auto nv = make_unique<const char *[]>(
nheader * 2 + 8 + get_config()->add_response_headers.size() * 2 + 1); nheader * 2 + 8 + get_config()->add_response_headers.size() * 2 + 1);
size_t hdidx = 0; size_t hdidx = 0;

View File

@ -35,6 +35,7 @@
#include "shrpx_worker_config.h" #include "shrpx_worker_config.h"
#include "shrpx_connect_blocker.h" #include "shrpx_connect_blocker.h"
#include "util.h" #include "util.h"
#include "template.h"
using namespace nghttp2; using namespace nghttp2;
@ -51,7 +52,7 @@ Worker::Worker(SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx,
ssl::CertLookupTree *cert_tree, ssl::CertLookupTree *cert_tree,
const std::shared_ptr<TicketKeys> &ticket_keys) const std::shared_ptr<TicketKeys> &ticket_keys)
: loop_(ev_loop_new(0)), sv_ssl_ctx_(sv_ssl_ctx), cl_ssl_ctx_(cl_ssl_ctx), : loop_(ev_loop_new(0)), sv_ssl_ctx_(sv_ssl_ctx), cl_ssl_ctx_(cl_ssl_ctx),
worker_stat_(util::make_unique<WorkerStat>()) { worker_stat_(make_unique<WorkerStat>()) {
ev_async_init(&w_, eventcb); ev_async_init(&w_, eventcb);
w_.data = this; w_.data = this;
ev_async_start(loop_, &w_); ev_async_start(loop_, &w_);
@ -66,9 +67,9 @@ Worker::Worker(SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx,
} }
if (get_config()->downstream_proto == PROTO_HTTP2) { if (get_config()->downstream_proto == PROTO_HTTP2) {
http2session_ = util::make_unique<Http2Session>(loop_, cl_ssl_ctx_); http2session_ = make_unique<Http2Session>(loop_, cl_ssl_ctx_);
} else { } else {
http1_connect_blocker_ = util::make_unique<ConnectBlocker>(loop_); http1_connect_blocker_ = make_unique<ConnectBlocker>(loop_);
} }
worker_config->ticket_keys = ticket_keys; worker_config->ticket_keys = ticket_keys;

48
src/template.h Normal file
View File

@ -0,0 +1,48 @@
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2015 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef TEMPLATE_H
#define TEMPLATE_H
#include "nghttp2_config.h"
#include <memory>
namespace nghttp2 {
template <typename T, typename... U>
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
make_unique(U &&... u) {
return std::unique_ptr<T>(new T(std::forward<U>(u)...));
}
template <typename T>
typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
make_unique(size_t size) {
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
}
} // namespace nghttp2
#endif // TEMPLATE_H

View File

@ -403,18 +403,6 @@ template <typename T> std::string utox(T n) {
return res; return res;
} }
template <typename T, typename... U>
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
make_unique(U &&... u) {
return std::unique_ptr<T>(new T(std::forward<U>(u)...));
}
template <typename T>
typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
make_unique(size_t size) {
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
}
void to_token68(std::string &base64str); void to_token68(std::string &base64str);
void to_base64(std::string &token68str); void to_base64(std::string &token68str);