Class: RRProxy

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/rrproxy.rb,
lib/rrproxy/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.routesObject

Returns the value of attribute routes.



10
11
12
# File 'lib/rrproxy.rb', line 10

def routes
  @routes
end

Instance Method Details

#on_headers(env, headers) ⇒ Object



22
# File 'lib/rrproxy.rb', line 22

def on_headers(env, headers) env["untampered_headers"] = headers end

#options_parser(option_parser, options) ⇒ Object



15
16
17
18
19
20
# File 'lib/rrproxy.rb', line 15

def options_parser(option_parser, options)
  option_parser.on("-c", "--config CONFIG_FILE", "Ruby routes configuration file") do |filename|
    options[:config] = filename
  end
  abort "asdf" unless File.file?(options[:config])
end

#response(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rrproxy.rb', line 24

def response(env)
  uri = env["REQUEST_URI"]
  method = env["REQUEST_METHOD"]
  logger.info "Proxying #{method} #{uri}"
  method = method.downcase.to_sym
  RRProxy.routes.each do |pattern, target, replacement|
    if pattern.is_a?(Regexp)
      next unless uri =~ pattern
      uri.sub!($1, replacement) if replacement
    else
      next unless uri.start_with? pattern
      uri.sub!(pattern, replacement) if replacement
    end

    target_url = "http://#{target}#{uri}"
    headers = env["untampered_headers"]

    # Try to make it look as much as possible like the request originates with us. This removes a bunch of
    # security protections and is a reason rrproxy is only for development/testing puproses.
    headers["Host"] = target
    headers.delete "Referer"

    params = { :head => headers }
    params[:body] = env["params"] if [:put, :post, :patch].include? method

    http = EM::HttpRequest.new(target_url).send(method, params)
    response_headers = {}
    http.response_header.each do |k, v|
      response_headers[k.downcase.split("_").map(&:capitalize).join("-")] = v
    end

    return [http.response_header.status, response_headers, [http.response]]
  end
  [503, {}, "No target specified for this path."]
end