Class: SenorArmando::Endpoint::Proxy

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/senor_armando/endpoint/proxy.rb

Instance Method Summary collapse

Instance Method Details

#dest_url_and_params(env) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/senor_armando/endpoint/proxy.rb', line 20

def dest_url_and_params(env)
  dest_params = {:head => env['client-headers'], :query => env.params}

  # Set the target host correctly
  dest_url = PostRank::URI.normalize("#{Settings[:forwarder]}#{env[Goliath::Request::REQUEST_PATH]}")

  env.logger.debug ['proxy', dest_url, dest_params].join("\t")
  [dest_url, dest_params]
end

#on_headers(env, headers) ⇒ Object

Capture the headers when they roll in, to replay for the remote target



16
17
18
# File 'lib/senor_armando/endpoint/proxy.rb', line 16

def on_headers(env, headers)
  env['client-headers'] = headers
end

#response(env) ⇒ Object

Pass the call request on to the target host



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/senor_armando/endpoint/proxy.rb', line 31

def response(env)
  env.trace :response_beg

  dest_url, dest_params = dest_url_and_params(env)
  dest_params[:head]['Host'] = dest_url.host

  req = EM::HttpRequest.new(dest_url.to_s)
  resp =
    case(env[Goliath::Request::REQUEST_METHOD])
    when 'GET'  then req.get(dest_params)
    when 'POST' then req.post(dest_params.merge(:body => (env[Goliath::Request::RACK_INPUT].read || '')))
    when 'HEAD' then req.head(dest_params)
    else raise Goliath::Validation::BadRequestError.new("Uncool method #{env[Goliath::Request::REQUEST_METHOD]}")
    end

  env.trace :response_end
  [resp.response_header.status, response_header_hash(resp), resp.response]
end

#response_header_hash(resp) ⇒ Object

Need to convert from the CONTENT_TYPE we’ll get back from the server to the normal Content-Type header



52
53
54
55
56
57
58
# File 'lib/senor_armando/endpoint/proxy.rb', line 52

def response_header_hash(resp)
  hsh = {}
  resp.response_header.each_pair do |k, v|
    hsh[to_http_header(k)] = v
  end
  hsh
end

#to_http_header(k) ⇒ Object



60
61
62
# File 'lib/senor_armando/endpoint/proxy.rb', line 60

def to_http_header(k)
  k.downcase.split('_').map{|e| e.capitalize }.join('-')
end