diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-10-27 00:40:42 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-10-27 00:40:42 +0900 |
commit | 9377ca7be81185f5fb99e1d380718dd1c29beb8a (patch) | |
tree | 4c160c4547f0d7dec0b34f306a04462c4fb7f3fc | |
parent | 18043d0510439347a10bd18e57f998049bbd4ba2 (diff) | |
download | plum-9377ca7be81185f5fb99e1d380718dd1c29beb8a.tar.gz |
rack: add REMOTE_ADDR to request env
-rw-r--r-- | lib/plum/rack/connection.rb | 9 | ||||
-rw-r--r-- | lib/plum/rack/server.rb | 6 |
2 files changed, 11 insertions, 4 deletions
diff --git a/lib/plum/rack/connection.rb b/lib/plum/rack/connection.rb index 27813a5..feb04ac 100644 --- a/lib/plum/rack/connection.rb +++ b/lib/plum/rack/connection.rb @@ -8,11 +8,12 @@ module Plum class Connection attr_reader :app, :plum - def initialize(app, plum, logger, server_push:) + def initialize(app:, plum:, logger:, server_push: true, remote_addr: "127.0.0.1") @app = app @plum = plum @logger = logger @server_push = server_push + @remote_addr = remote_addr setup_plum end @@ -62,6 +63,7 @@ module Plum stream.send_data(part, end_stream: last == i) i += 1 } + stream.send_data(nil, end_stream: true) if i == 0 else body.each { |part| stream.send_data(part, end_stream: false) } stream.send_data(nil, end_stream: true) @@ -116,7 +118,6 @@ module Plum def new_env(h, data) ebase = { - "SCRIPT_NAME" => "", "rack.version" => ::Rack::VERSION, "rack.input" => StringIO.new(data), "rack.errors" => $stderr, @@ -124,6 +125,8 @@ module Plum "rack.multiprocess" => false, "rack.run_once" => false, "rack.hijack?" => false, + "SCRIPT_NAME" => "", + "REMOTE_ADDR" => @remote_addr, } h.each { |k, v| @@ -137,7 +140,7 @@ module Plum when ":authority" chost, cport = v.split(":", 2) ebase["SERVER_NAME"] = chost - ebase["SERVER_PORT"] = (cport || 443).to_i + ebase["SERVER_PORT"] = cport || "443" when ":scheme" ebase["rack.url_scheme"] = v else diff --git a/lib/plum/rack/server.rb b/lib/plum/rack/server.rb index b64e095..68be501 100644 --- a/lib/plum/rack/server.rb +++ b/lib/plum/rack/server.rb @@ -50,7 +50,11 @@ module Plum sock = sock.accept if sock.respond_to?(:accept) plum = svr.plum(sock) - con = Connection.new(@app, plum, @logger, server_push: @config[:server_push]) + con = Connection.new(app: @app, + plum: plum, + logger: @logger, + server_push: @config[:server_push], + remote_addr: sock.peeraddr.last) con.run rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINVAL => e # closed sock.close if sock |