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
# File 'lib/miniproxy/proxy_server.rb', line 9

def initialize(config = {}, default = WEBrick::Config::HTTP)
  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



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

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



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

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



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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/miniproxy/proxy_server.rb', line 50

def service(req, res)
  if self.config[:AllowedRequestCheck].call(req)
    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