Commit 1a2bccd7 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Share nghttp2_session_callbacks between objects

parent 84172753
......@@ -65,6 +65,8 @@
#include "shrpx_worker_config.h"
#include "shrpx_worker.h"
#include "shrpx_accept_handler.h"
#include "shrpx_http2_upstream.h"
#include "shrpx_http2_session.h"
#include "util.h"
#include "app_helper.h"
#include "ssl.h"
......@@ -889,6 +891,10 @@ void fill_default_config() {
mod_config()->padding = 0;
mod_config()->worker_frontend_connections = 0;
mod_config()->http2_upstream_callbacks = create_http2_upstream_callbacks();
mod_config()->http2_downstream_callbacks =
create_http2_downstream_callbacks();
nghttp2_option_new(&mod_config()->http2_option);
nghttp2_option_set_no_auto_window_update(get_config()->http2_option, 1);
......
......@@ -247,6 +247,8 @@ struct Config {
std::unique_ptr<char[]> errorlog_file;
FILE *http2_upstream_dump_request_header;
FILE *http2_upstream_dump_response_header;
nghttp2_session_callbacks *http2_upstream_callbacks;
nghttp2_session_callbacks *http2_downstream_callbacks;
nghttp2_option *http2_option;
nghttp2_option *http2_client_option;
char **argv;
......
......@@ -1096,46 +1096,16 @@ int on_frame_not_send_callback(nghttp2_session *session,
}
} // namespace
int Http2Session::on_connect() {
nghttp2_session_callbacks *create_http2_downstream_callbacks() {
int rv;
state_ = Http2Session::CONNECTED;
if (ssl_ctx_) {
const unsigned char *next_proto = nullptr;
unsigned int next_proto_len;
SSL_get0_next_proto_negotiated(conn_.tls.ssl, &next_proto, &next_proto_len);
for (int i = 0; i < 2; ++i) {
if (next_proto) {
if (LOG_ENABLED(INFO)) {
std::string proto(next_proto, next_proto + next_proto_len);
SSLOG(INFO, this) << "Negotiated next protocol: " << proto;
}
if (!util::check_h2_is_selected(next_proto, next_proto_len)) {
return -1;
}
break;
}
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
SSL_get0_alpn_selected(conn_.tls.ssl, &next_proto, &next_proto_len);
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
break;
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
}
if (!next_proto) {
return -1;
}
}
nghttp2_session_callbacks *callbacks;
rv = nghttp2_session_callbacks_new(&callbacks);
if (rv != 0) {
return -1;
return nullptr;
}
auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
......@@ -1162,8 +1132,43 @@ int Http2Session::on_connect() {
callbacks, http::select_padding_callback);
}
rv = nghttp2_session_client_new2(&session_, callbacks, this,
get_config()->http2_client_option);
return callbacks;
}
int Http2Session::on_connect() {
int rv;
state_ = Http2Session::CONNECTED;
if (ssl_ctx_) {
const unsigned char *next_proto = nullptr;
unsigned int next_proto_len;
SSL_get0_next_proto_negotiated(conn_.tls.ssl, &next_proto, &next_proto_len);
for (int i = 0; i < 2; ++i) {
if (next_proto) {
if (LOG_ENABLED(INFO)) {
std::string proto(next_proto, next_proto + next_proto_len);
SSLOG(INFO, this) << "Negotiated next protocol: " << proto;
}
if (!util::check_h2_is_selected(next_proto, next_proto_len)) {
return -1;
}
break;
}
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
SSL_get0_alpn_selected(conn_.tls.ssl, &next_proto, &next_proto_len);
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
break;
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
}
if (!next_proto) {
return -1;
}
}
rv = nghttp2_session_client_new2(&session_,
get_config()->http2_downstream_callbacks,
this, get_config()->http2_client_option);
if (rv != 0) {
return -1;
......
......@@ -194,6 +194,8 @@ private:
ReadBuf rb_;
};
nghttp2_session_callbacks *create_http2_downstream_callbacks();
} // namespace shrpx
#endif // SHRPX_HTTP2_SESSION_H
......@@ -605,25 +605,15 @@ void Http2Upstream::check_shutdown() {
}
}
Http2Upstream::Http2Upstream(ClientHandler *handler)
: downstream_queue_(
get_config()->http2_proxy
? get_config()->downstream_connections_per_host
: get_config()->downstream_proto == PROTO_HTTP
? get_config()->downstream_connections_per_frontend
: 0,
!get_config()->http2_proxy),
handler_(handler), session_(nullptr), data_pending_(nullptr),
data_pendinglen_(0), shutdown_handled_(false) {
nghttp2_session_callbacks *create_http2_upstream_callbacks() {
int rv;
nghttp2_session_callbacks *callbacks;
rv = nghttp2_session_callbacks_new(&callbacks);
assert(rv == 0);
rv = nghttp2_session_callbacks_new(&callbacks);
auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
if (rv != 0) {
return nullptr;
}
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
......@@ -651,7 +641,24 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
callbacks, http::select_padding_callback);
}
rv = nghttp2_session_server_new2(&session_, callbacks, this,
return callbacks;
}
Http2Upstream::Http2Upstream(ClientHandler *handler)
: downstream_queue_(
get_config()->http2_proxy
? get_config()->downstream_connections_per_host
: get_config()->downstream_proto == PROTO_HTTP
? get_config()->downstream_connections_per_frontend
: 0,
!get_config()->http2_proxy),
handler_(handler), session_(nullptr), data_pending_(nullptr),
data_pendinglen_(0), shutdown_handled_(false) {
int rv;
rv = nghttp2_session_server_new2(&session_,
get_config()->http2_upstream_callbacks, this,
get_config()->http2_option);
assert(rv == 0);
......
......@@ -119,6 +119,8 @@ private:
bool shutdown_handled_;
};
nghttp2_session_callbacks *create_http2_upstream_callbacks();
} // namespace shrpx
#endif // SHRPX_HTTP2_UPSTREAM_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment