From 99f7e7e2a58302ea6fd4621a0da72849fb52b433 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 29 Apr 2016 12:17:25 +0900 Subject: [PATCH] nghttpx: Add mruby env.server_addr and env.tls_used attributes --- doc/nghttpx.h2r | 14 +++++++++++++- src/shrpx_mruby_module_env.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/doc/nghttpx.h2r b/doc/nghttpx.h2r index e79718f9..d21c940e 100644 --- a/doc/nghttpx.h2r +++ b/doc/nghttpx.h2r @@ -265,13 +265,25 @@ respectively. .. rb:attr_reader:: remote_addr - Return IP address of a remote client. + Return IP address of a remote client. If connection is made + via UNIX domain socket, this returns the string "localhost". + + .. rb:attr_reader:: server_addr + + Return address of server that accepted the connection. This + is a string which specified in :option:`--frontend` option, + excluding port number, and not a resolved IP address. For + UNIX domain socket, this is a path to UNIX domain socket. .. rb:attr_reader:: server_port Return port number of the server frontend which accepted the connection from client. + .. rb:attr_reader:: tls_used + + Return true if TLS is used on the connection. + .. rb:class:: Request Object to represent request from client. The modification to diff --git a/src/shrpx_mruby_module_env.cc b/src/shrpx_mruby_module_env.cc index eea32a49..95691046 100644 --- a/src/shrpx_mruby_module_env.cc +++ b/src/shrpx_mruby_module_env.cc @@ -104,6 +104,29 @@ mrb_value env_get_server_port(mrb_state *mrb, mrb_value self) { } } // namespace +namespace { +mrb_value env_get_server_addr(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_str_new(mrb, faddr->host.c_str(), faddr->host.size()); +} +} // namespace + +namespace { +mrb_value env_get_tls_used(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(); + + return handler->get_ssl() ? mrb_true_value() : mrb_false_value(); +} +} // namespace + void init_env_class(mrb_state *mrb, RClass *module) { auto env_class = mrb_define_class_under(mrb, module, "Env", mrb->object_class); @@ -115,8 +138,12 @@ 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_addr", env_get_server_addr, + MRB_ARGS_NONE()); mrb_define_method(mrb, env_class, "server_port", env_get_server_port, MRB_ARGS_NONE()); + mrb_define_method(mrb, env_class, "tls_used", env_get_tls_used, + MRB_ARGS_NONE()); } } // namespace mruby