Class: AutoResp::ProxyServer
- Inherits:
-
WEBrick::HTTPProxyServer
- Object
- WEBrick::HTTPProxyServer
- AutoResp::ProxyServer
- Includes:
- Parser
- Defined in:
- lib/ar/proxy_server.rb
Instance Attribute Summary collapse
-
#sessions ⇒ Object
readonly
Returns the value of attribute sessions.
Instance Method Summary collapse
- #before_filter(&block) ⇒ Object
- #before_filters ⇒ Object
- #clear_before_filters ⇒ Object
- #fetch(handler, declare, uri) ⇒ Object
- #find_auto_res(url) ⇒ Object
-
#initialize(core, config = {}) ⇒ ProxyServer
constructor
A new instance of ProxyServer.
- #matched?(url, rule) ⇒ Boolean
- #service(req, res) ⇒ Object
Methods included from Parser
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
#sessions ⇒ Object (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
80 81 82 83 |
# File 'lib/ar/proxy_server.rb', line 80 def before_filter(&block) logger.info "add before_filter".green before_filters << block end |
#before_filters ⇒ Object
76 77 78 |
# File 'lib/ar/proxy_server.rb', line 76 def before_filters @before_filters ||= [] end |
#clear_before_filters ⇒ Object
85 86 87 |
# File 'lib/ar/proxy_server.rb', line 85 def clear_before_filters @before_filters.clear end |
#fetch(handler, declare, uri) ⇒ Object
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 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ar/proxy_server.rb', line 107 def fetch(handler, declare, uri) case handler 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( handler.call(*mtc), declare, uri ) else fetch( handler.call, declare, uri ) end when String if goto = redirect_path(handler) 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.( 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(handler) end when Fixnum [{}, "", handler] when Array [handler[1], handler[2], handler[0]] end end |
#find_auto_res(url) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ar/proxy_server.rb', line 89 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 = [handlers] unless handlers.is_a?(Array) handlers.each {|proc| proc.call if proc.respond_to?(:call) } fetch(handlers.last, condition, url) end end |
#matched?(url, rule) ⇒ Boolean
103 104 105 |
# File 'lib/ar/proxy_server.rb', line 103 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 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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 begin header, body, status = find_auto_res(req.unparsed_uri) rescue => e header = {} body = <<-MSG ERROR: #{e.} #{e.backtrace.join "\n"} MSG status = 500 end 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[0..200]}#{'...' if body.size > 200}" 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 |