Class: Rack::Proxy
- Inherits:
-
Object
- Object
- Rack::Proxy
- Defined in:
- lib/rack/proxy.rb
Overview
Subclass and bring your own #rewrite_request and #rewrite_response
Direct Known Subclasses
ExampleServiceProxy, ForwardHost, RackPhpProxy, TrustingProxy
Constant Summary collapse
- VERSION =
"0.7.7".freeze
- HOP_BY_HOP_HEADERS =
{ 'connection' => true, 'keep-alive' => true, 'proxy-authenticate' => true, 'proxy-authorization' => true, 'te' => true, 'trailer' => true, 'transfer-encoding' => true, 'upgrade' => true }.freeze
Class Method Summary collapse
- .build_header_hash(pairs) ⇒ Object
- .extract_http_request_headers(env) ⇒ Object
- .normalize_headers(headers) ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app = nil, opts = {}) ⇒ Proxy
constructor
A new instance of Proxy.
-
#rewrite_env(env) ⇒ Object
Return modified env.
-
#rewrite_response(triplet) ⇒ Object
Return a rack triplet [status, headers, body].
Constructor Details
#initialize(app = nil, opts = {}) ⇒ Proxy
Returns a new instance of Proxy.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rack/proxy.rb', line 63 def initialize(app = nil, opts= {}) if app.is_a?(Hash) opts = app @app = nil else @app = app end @streaming = opts.fetch(:streaming, true) @ssl_verify_none = opts.fetch(:ssl_verify_none, false) @backend = opts[:backend] ? URI(opts[:backend]) : nil @read_timeout = opts.fetch(:read_timeout, 60) @ssl_version = opts[:ssl_version] @cert = opts[:cert] @key = opts[:key] @verify_mode = opts[:verify_mode] @username = opts[:username] @password = opts[:password] @opts = opts end |
Class Method Details
.build_header_hash(pairs) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/rack/proxy.rb', line 41 def build_header_hash(pairs) if Rack.const_defined?(:Headers) # Rack::Headers is only available from Rack 3 onward Headers.new.tap { |headers| pairs.each { |k, v| headers[k] = v } } else # Rack::Utils::HeaderHash is deprecated from Rack 3 onward and is to be removed in 3.1 Utils::HeaderHash.new(pairs) end end |
.extract_http_request_headers(env) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rack/proxy.rb', line 22 def extract_http_request_headers(env) headers = env.reject do |k, v| !(/^HTTP_[A-Z0-9_\.]+$/ === k) || v.nil? end.map do |k, v| [reconstruct_header_name(k), v] end.then { |pairs| build_header_hash(pairs) } x_forwarded_for = (headers['X-Forwarded-For'].to_s.split(/, +/) << env['REMOTE_ADDR']).join(', ') headers.merge!('X-Forwarded-For' => x_forwarded_for) end |
.normalize_headers(headers) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/rack/proxy.rb', line 34 def normalize_headers(headers) mapped = headers.map do |k, v| [titleize(k), if v.is_a? Array then v.join("\n") else v end] end build_header_hash Hash[mapped] end |
Instance Method Details
#call(env) ⇒ Object
86 87 88 |
# File 'lib/rack/proxy.rb', line 86 def call(env) rewrite_response(perform_request(rewrite_env(env))) end |
#rewrite_env(env) ⇒ Object
Return modified env
91 92 93 |
# File 'lib/rack/proxy.rb', line 91 def rewrite_env(env) env end |
#rewrite_response(triplet) ⇒ Object
Return a rack triplet [status, headers, body]
96 97 98 |
# File 'lib/rack/proxy.rb', line 96 def rewrite_response(triplet) triplet end |