Class: Slowproxy::Server

Inherits:
WEBrick::HTTPProxyServer
  • Object
show all
Defined in:
lib/slowproxy/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, default = WEBrick::Config::HTTP) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
# File 'lib/slowproxy/server.rb', line 5

def initialize(config, default = WEBrick::Config::HTTP)
  @bps = config.delete(:BPS)
  SlowBufferedIO.bps = @bps if @bps
  super
  logger.info "#{number_to_human_size(@bps)}bps"
  SlowBufferedIO.logger = logger
end

Instance Method Details

#number_to_human_size(n) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/slowproxy/server.rb', line 13

def number_to_human_size(n)
  suffixes = ["", "K", "M", "G"]
  suffixes.each_with_index.to_a.reverse.each do |suffix, index|
    one = 1024 ** index
    return "#{n / one} #{suffix}" if n >= one
  end
end

#perform_proxy_request(req, res) ⇒ Object



21
22
23
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/slowproxy/server.rb', line 21

def perform_proxy_request(req, res)
  uri = req.request_uri
  path = uri.path.dup
  path << "?" << uri.query if uri.query
  header = setup_proxy_header(req, res)
  upstream = setup_upstream_proxy_authentication(req, res, header)
  response = nil

  http = Net::HTTP.new(uri.host, uri.port, upstream.host, upstream.port)
  http.start do
    ########## prepend Net::SlowBufferedIO
    http.instance_eval do
      class << @socket
        prepend Slowproxy::SlowBufferedIO
      end
    end
    ########## /prepend Net::SlowBufferedIO
    if @config[:ProxyTimeout]
      ##################################   these issues are
      http.open_timeout = 30   # secs  #   necessary (maybe because
      http.read_timeout = 60   # secs  #   Ruby's bug, but why?)
      ##################################
    end
    response = yield(http, path, header)
  end

  # Persistent connection requirements are mysterious for me.
  # So I will close the connection in every response.
  res['proxy-connection'] = "close"
  res['connection'] = "close"

  # Convert Net::HTTP::HTTPResponse to WEBrick::HTTPResponse
  res.status = response.code.to_i
  choose_header(response, res)
  set_cookie(response, res)
  set_via(res)
  res.body = response.body
end