Class: Rack::Reproxy::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/reproxy.rb

Direct Known Subclasses

Apache, Lighttpd, Nginx, Rack

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



77
78
79
80
81
# File 'lib/rack/reproxy.rb', line 77

def initialize(app, options = {})
  @app = app
  @header = options.fetch(:header, 'X-Reproxy-Url')
  @scrub_reproxy_header = "HTTP_#{@header.gsub('-', '_').upcase}"
end

Instance Method Details

#call(env) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rack/reproxy.rb', line 83

def call(env)
  # Don't let clients ask us to reproxy URLs.
  env.delete(@scrub_reproxy_header)

  # In case the Rack app would like to know which header to set.
  env['rack.reproxy.header'] ||= @header

  status, headers, body = @app.call(env)

  # Reproxy URI response bodies.
  if body.is_a?(URI)
    reproxy env, status, headers.merge(@header => body.to_s), body

  # Reproxy explicit requests to respond with a different URL.
  elsif headers.include?(@header)
    reproxy env, status, headers, body

  # Pass through the response, otherwise.
  else
    [status, headers, body]
  end
end