Class: Rack::Proxy::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ Server

Returns a new instance of Server.



5
6
7
8
# File 'lib/rack/proxy/server.rb', line 5

def initialize(host, options = {})
  @host = URI(host)
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/proxy/server.rb', line 10

def call(env)
  request = Rack::Request.new(env)
  uri = URI(request.url)
  
  uri.scheme, uri.userinfo, uri.host, uri.port = @host.scheme, @host.userinfo, @host.host, @host.port
  uri.path = env['PATH_INFO']
  
  forward_request = Net::HTTP.const_get(request_method(request)).new(uri.request_uri)
  if forward_request.request_body_permitted? and request.body
    forward_request.body_stream = request.body
    forward_request.content_length = request.content_length
    forward_request.content_type = request.content_type
  end

  forward_request["Referer"] = request.referer

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https' ? true : false
  forward_response = http.request(forward_request)

  headers = {}
  forward_response.each_header do |k,v|
    headers[k] = v unless k.to_s =~ /status/i
  end

  [forward_response.code.to_i, headers, [forward_response.read_body]]      
end