Class: MiniProxy::ProxyServer

Inherits:
WEBrick::HTTPProxyServer
  • Object
show all
Defined in:
lib/miniproxy/proxy_server.rb

Overview

MiniProxy server, which boots a WEBrick proxy server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, default = WEBrick::Config::HTTP) ⇒ ProxyServer

Returns a new instance of ProxyServer.



9
10
11
12
13
14
15
16
17
18
# File 'lib/miniproxy/proxy_server.rb', line 9

def initialize(config = {}, default = WEBrick::Config::HTTP)
  @allowed_hosts = ["127.0.0.1", "localhost", config[:MiniProxyHost]].compact

  config = config.merge({
    Logger: WEBrick::Log.new(nil, 0), # silence logging
    AccessLog: [], # silence logging
  })

  super(config, default)
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



7
8
9
# File 'lib/miniproxy/proxy_server.rb', line 7

def requests
  @requests
end

Instance Method Details

#do_DELETE(req, res) ⇒ Object



26
27
28
29
30
# File 'lib/miniproxy/proxy_server.rb', line 26

def do_DELETE(req, res)
  perform_proxy_request(req, res) do |http, path, header|
    http.delete(path, header)
  end
end

#do_PATCH(req, res) ⇒ Object



32
33
34
35
36
# File 'lib/miniproxy/proxy_server.rb', line 32

def do_PATCH(req, res)
  perform_proxy_request(req, res) do |http, path, header|
    http.patch(path, req.body || "", header)
  end
end

#do_PUT(req, res) ⇒ Object



20
21
22
23
24
# File 'lib/miniproxy/proxy_server.rb', line 20

def do_PUT(req, res)
  perform_proxy_request(req, res) do |http, path, header|
    http.put(path, req.body || "", header)
  end
end

#service(req, res) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/miniproxy/proxy_server.rb', line 38

def service(req, res)
  if @allowed_hosts.include?(req.host)
    super(req, res)
  else
    if req.request_method == "CONNECT"
      is_ssl = req.unparsed_uri.include?(":443")

      # If something is trying to initiate an SSL connection, rewrite
      # the URI to point to our fake server so we can stub SSL requests.
      if is_ssl
        req.instance_variable_set(:@unparsed_uri, "localhost:#{self.config[:FakeServerPort]}")
      end

      super(req, res)
    else
      # Otherwise, call our handler to respond with an appropriate
      # mock for the request.
      self.config[:MockHandlerCallback].call(req, res)
    end
  end
end