From 09b97a331329424c18d26b7dc7b8a743d07dd9e6 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 28 Apr 2016 00:19:30 +0900 Subject: [PATCH] nghttpx: Add mruby env.server_port to return frontend server side port --- doc/nghttpx.h2r | 5 +++++ src/shrpx_client_handler.cc | 2 ++ src/shrpx_client_handler.h | 2 ++ src/shrpx_mruby_module_env.cc | 14 ++++++++++++++ 4 files changed, 23 insertions(+) diff --git a/doc/nghttpx.h2r b/doc/nghttpx.h2r index 518b87e4..e79718f9 100644 --- a/doc/nghttpx.h2r +++ b/doc/nghttpx.h2r @@ -267,6 +267,11 @@ respectively. Return IP address of a remote client. + .. rb:attr_reader:: server_port + + Return port number of the server frontend which accepted the + connection from client. + .. rb:class:: Request Object to represent request from client. The modification to diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 44d624af..7a4d5a1d 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -1281,4 +1281,6 @@ StringRef ClientHandler::get_forwarded_for() const { return StringRef{forwarded_for_}; } +const UpstreamAddr *ClientHandler::get_upstream_addr() const { return faddr_; } + } // namespace shrpx diff --git a/src/shrpx_client_handler.h b/src/shrpx_client_handler.h index e883d6f0..f9ae6615 100644 --- a/src/shrpx_client_handler.h +++ b/src/shrpx_client_handler.h @@ -144,6 +144,8 @@ public: Http2Session *select_http2_session(DownstreamAddrGroup &group); + const UpstreamAddr *get_upstream_addr() const; + private: Connection conn_; ev_timer reneg_shutdown_timer_; diff --git a/src/shrpx_mruby_module_env.cc b/src/shrpx_mruby_module_env.cc index 1fb75781..eea32a49 100644 --- a/src/shrpx_mruby_module_env.cc +++ b/src/shrpx_mruby_module_env.cc @@ -92,6 +92,18 @@ mrb_value env_get_remote_addr(mrb_state *mrb, mrb_value self) { } } // namespace +namespace { +mrb_value env_get_server_port(mrb_state *mrb, mrb_value self) { + auto data = static_cast(mrb->ud); + auto downstream = data->downstream; + auto upstream = downstream->get_upstream(); + auto handler = upstream->get_client_handler(); + auto faddr = handler->get_upstream_addr(); + + return mrb_fixnum_value(faddr->port); +} +} // namespace + void init_env_class(mrb_state *mrb, RClass *module) { auto env_class = mrb_define_class_under(mrb, module, "Env", mrb->object_class); @@ -103,6 +115,8 @@ void init_env_class(mrb_state *mrb, RClass *module) { mrb_define_method(mrb, env_class, "phase", env_get_phase, MRB_ARGS_NONE()); mrb_define_method(mrb, env_class, "remote_addr", env_get_remote_addr, MRB_ARGS_NONE()); + mrb_define_method(mrb, env_class, "server_port", env_get_server_port, + MRB_ARGS_NONE()); } } // namespace mruby