Class: AutoResp::ProxyServer

Inherits:
WEBrick::HTTPProxyServer
  • Object
show all
Includes:
Parser
Defined in:
lib/ar/proxyserver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse, #parse_header_item

Constructor Details

#initialize(core, config = {}) ⇒ ProxyServer

Returns a new instance of ProxyServer.



14
15
16
17
18
19
20
21
# File 'lib/ar/proxyserver.rb', line 14

def initialize(core, config={})
  @core = core
  super(config.update({
    :AccessLog  => [],
    :Logger     => WEBrick::Log.new("/dev/null")
  }))
  @sessions = []
end

Instance Attribute Details

#sessionsObject (readonly)

Returns the value of attribute sessions.



12
13
14
# File 'lib/ar/proxyserver.rb', line 12

def sessions
  @sessions
end

Instance Method Details

#fetch(txt, declare, uri) ⇒ Object



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

def fetch(txt, declare, uri)

  case txt
  when Proc
    mtc = uri.match(declare) if Regexp === declare
    fetch( txt.call(*mtc), declare, uri )
  when String
    if goto = redirect_path(txt)
      if is_uri?(goto)
        Net::HTTP.get_response(URI(goto)) do |res|
          return [nil, res.body]
        end
      elsif File.exist?(goto)
        return parse(IO.read(goto))
      end
    else
      parse(txt)
    end
  when Fixnum
    [{}, "", txt]
  when Array
    [txt[1], txt[2], txt[0]]
  end
end

#find_auto_res(url) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ar/proxyserver.rb', line 41

def find_auto_res(url)
  @core.rules.find do |tar, map|
    if trim_url(tar) === trim_url(url)
      return fetch(map, tar, url)
    end
  end
end

#service(req, res) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ar/proxyserver.rb', line 23

def service(req, res)
  puts "[#{req.unparsed_uri}]"
  sessions << [req, res]
  header, body, status = find_auto_res(req.unparsed_uri)
  res.status = status if status

  if header or body
    puts "match".ljust(8)   << ": #{req.unparsed_uri}"
    puts "header".ljust(8)  << ": #{header}"
    puts "body".ljust(8)    << ": \n#{body}"
    puts "-"*50
    res.header.merge!(header || {})
    res.body = body
  else
    super(req, res)
  end
end