Class: ProxES::Forwarder
- Inherits:
-
Object
- Object
- ProxES::Forwarder
- Defined in:
- lib/proxes/forwarder.rb
Overview
A lot of code in this comes from Rack::Proxy
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#streaming ⇒ Object
readonly
Returns the value of attribute streaming.
Class Method Summary collapse
Instance Method Summary collapse
- #body_from(request) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(opts = {}) ⇒ Forwarder
constructor
A new instance of Forwarder.
- #request_from(env) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Forwarder
Returns a new instance of Forwarder.
9 10 11 |
# File 'lib/proxes/forwarder.rb', line 9 def initialize(opts = {}) @backend = URI(opts[:backend]) if opts[:backend] end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
7 8 9 |
# File 'lib/proxes/forwarder.rb', line 7 def backend @backend end |
#streaming ⇒ Object (readonly)
Returns the value of attribute streaming.
7 8 9 |
# File 'lib/proxes/forwarder.rb', line 7 def streaming @streaming end |
Class Method Details
.normalize_headers(headers) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/proxes/forwarder.rb', line 60 def normalize_headers(headers) mapped = headers.map do |k, v| [k, v.is_a?(Array) ? v.join("\n") : v] end Rack::Utils::HeaderHash.new Hash[mapped] end |
Instance Method Details
#body_from(request) ⇒ Object
54 55 56 57 |
# File 'lib/proxes/forwarder.rb', line 54 def body_from(request) return nil if request.body.nil? || (Kernel.const_defined?('::Puma::NullIO') && request.body.is_a?(Puma::NullIO)) request.body.read.tap { |_r| request.body.rewind } end |
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/proxes/forwarder.rb', line 13 def call(env) http = Net::HTTP.new(backend.host, backend.port) http.use_ssl = true if backend.is_a? URI::HTTPS if ENV['SSL_VERIFY_NONE'].to_i == 1 http.verify_mode = OpenSSL::SSL::VERIFY_NONE store = OpenSSL::X509::Store.new store.set_default_paths http.cert_store = store end request = request_from(env) request.basic_auth backend.user, backend.password response = http.request(request) headers = (response.respond_to?(:headers) && response.headers) || self.class.normalize_headers(response.to_hash) body = response.body || [''] body = [body] unless body.respond_to?(:each) # Not sure where this is coming from, but it causes timeouts on the client headers.delete('transfer-encoding') # Ensure that the content length rack middleware kicks in headers.delete('content-length') [response.code, headers, body] end |
#request_from(env) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/proxes/forwarder.rb', line 40 def request_from(env) source = Rack::Request.new(env) fullpath = source.fullpath == '' ? URI.parse(env['REQUEST_URI']).request_uri : source.fullpath target = Net::HTTP.const_get(source.request_method.capitalize).new(fullpath) body = body_from(source) if body target.body = body target.content_length = body.length target.content_type = source.content_type if source.content_type end target end |