Class: AutoResp::ProxyServer

Inherits:
WEBrick::HTTPProxyServer
  • Object
show all
Includes:
Parser
Defined in:
lib/ar/proxy_server.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.



15
16
17
18
19
20
21
22
23
# File 'lib/ar/proxy_server.rb', line 15

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

Instance Attribute Details

#sessionsObject (readonly)

Returns the value of attribute sessions.



13
14
15
# File 'lib/ar/proxy_server.rb', line 13

def sessions
  @sessions
end

Instance Method Details

#before_filter(&block) ⇒ Object



68
69
70
71
# File 'lib/ar/proxy_server.rb', line 68

def before_filter(&block)
  logger.info "add before_filter".green
  before_filters << block
end

#before_filtersObject



64
65
66
# File 'lib/ar/proxy_server.rb', line 64

def before_filters
  @before_filters ||= []
end

#clear_before_filtersObject



73
74
75
# File 'lib/ar/proxy_server.rb', line 73

def clear_before_filters
  @before_filters.clear
end

#fetch(txt, declare, uri) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ar/proxy_server.rb', line 94

def fetch(txt, declare, uri)

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

#find_auto_res(url) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ar/proxy_server.rb', line 77

def find_auto_res(url)
  rule = @core.rules.find do |condition, map_to|
    matched? url, condition
  end
      
  @rule = rule
  if rule
    condition, handlers = *rule
    handlers.each {|proc| proc.call if proc.respond_to?(:call) }
    fetch(handlers.last, condition, url)
  end
end

#matched?(url, rule) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ar/proxy_server.rb', line 90

def matched?( url, rule )
  trim_url(rule) === trim_url(url)
end

#service(req, res) ⇒ Object



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
59
60
61
62
# File 'lib/ar/proxy_server.rb', line 25

def service(req, res)
  logger.info "[#{req.unparsed_uri}]"
  info = { start: Time.now }

  before_filters.each do |block|
    if block.call( req, res ) == false
      super req, res
      return
    end
  end

  header, body, status = find_auto_res(req.unparsed_uri)
  res.status = status if status

  if @rule

    logger.debug "match".ljust(8)   << ": #{req.unparsed_uri}"
    logger.debug "header".ljust(8)  << ": #{header}"
    logger.debug "body".ljust(8)    << ": \n#{body}"
    logger.debug "-"*50

    res['x-auto-response-condition']  = @rule.first.to_s
    res['x-auto-response-with']       = @rule.last.to_s
    if header
      header.each do |k,v|
        v = v.join("\n") if v.respond_to?(:join)
        res[k] = v
      end
    end
    res.body = body
    info[:matched] = true
  else
    super(req, res)
  end

  info[:end] = Time.now
  sessions << [req, res, info]
end