Class: ProxyServer
- Inherits:
-
Goliath::API
- Object
- Goliath::API
- ProxyServer
- Defined in:
- lib/proxy/proxy_server.rb
Constant Summary collapse
- DEFAULT_PORT =
8080
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#requests ⇒ Object
readonly
Returns the value of attribute requests.
-
#substitute_requests ⇒ Object
readonly
Returns the value of attribute substitute_requests.
-
#track_requests ⇒ Object
readonly
Returns the value of attribute track_requests.
-
#upstream_proxy ⇒ Object
readonly
Returns the value of attribute upstream_proxy.
Instance Method Summary collapse
- #get_proxy(options) ⇒ Object
- #get_substituted_response(options) ⇒ Object
- #get_uri(env) ⇒ Object
- #has_substitute?(uri) ⇒ Boolean
-
#initialize(options = {}) ⇒ ProxyServer
constructor
A new instance of ProxyServer.
- #request_uri(uri) ⇒ Object
- #reset ⇒ Object
- #response(env) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #substitute(uri) ⇒ Object
- #substitute_request(pattern, options) ⇒ Object
- #track(uri) ⇒ Object
- #track_request(pattern) ⇒ Object
- #tracking_enabled?(uri) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ ProxyServer
Returns a new instance of ProxyServer.
9 10 11 12 13 14 15 16 17 |
# File 'lib/proxy/proxy_server.rb', line 9 def initialize( = {}) @upstream_proxy = URI.parse([:proxy]) if [:proxy] @port = .fetch(:port, DEFAULT_PORT) @substitute_requests = {} @requests = [] @track_requests = [] end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/proxy/proxy_server.rb', line 5 def port @port end |
#requests ⇒ Object (readonly)
Returns the value of attribute requests.
6 7 8 |
# File 'lib/proxy/proxy_server.rb', line 6 def requests @requests end |
#substitute_requests ⇒ Object (readonly)
Returns the value of attribute substitute_requests.
6 7 8 |
# File 'lib/proxy/proxy_server.rb', line 6 def substitute_requests @substitute_requests end |
#track_requests ⇒ Object (readonly)
Returns the value of attribute track_requests.
6 7 8 |
# File 'lib/proxy/proxy_server.rb', line 6 def track_requests @track_requests end |
#upstream_proxy ⇒ Object (readonly)
Returns the value of attribute upstream_proxy.
5 6 7 |
# File 'lib/proxy/proxy_server.rb', line 5 def upstream_proxy @upstream_proxy end |
Instance Method Details
#get_proxy(options) ⇒ Object
71 72 73 |
# File 'lib/proxy/proxy_server.rb', line 71 def get_proxy() [:proxy] = {:type => 'http', :host => @upstream_proxy.host, :port => @upstream_proxy.port} if @upstream_proxy end |
#get_substituted_response(options) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/proxy/proxy_server.rb', line 75 def get_substituted_response() if [:body] [200, {}, [[:body]]] elsif [:url] request_uri [:url] end end |
#get_uri(env) ⇒ Object
57 58 59 |
# File 'lib/proxy/proxy_server.rb', line 57 def get_uri(env) env['REQUEST_URI'] || "http://#{env['SERVER_NAME']}#{env['PATH_INFO']}#{env['QUERY_STRING'].length > 0 ? '?'+env['QUERY_STRING'] : ''}" end |
#has_substitute?(uri) ⇒ Boolean
92 93 94 95 96 97 98 99 |
# File 'lib/proxy/proxy_server.rb', line 92 def has_substitute?(uri) @substitute_requests.each do |pattern, | if Regexp.new(pattern) =~ uri return true end end false end |
#request_uri(uri) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/proxy/proxy_server.rb', line 61 def request_uri(uri) = { :redirects => 1 } get_proxy() client= EM::HttpRequest.new(uri, ) request = client.get [request.response_header.status, request.response_header, request.response] end |
#reset ⇒ Object
44 45 46 47 48 |
# File 'lib/proxy/proxy_server.rb', line 44 def reset @requests.clear @track_requests.clear @substitute_requests.clear end |
#response(env) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/proxy/proxy_server.rb', line 50 def response(env) uri = get_uri env track uri if tracking_enabled? uri return substitute uri if has_substitute? uri request_uri uri end |
#start ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/proxy/proxy_server.rb', line 19 def start Thread.abort_on_exception = true require 'log4r' @proxy_thread = Thread.new do Goliath.env = :development server = Goliath::Server.new('0.0.0.0', self.port) server.app = Goliath::Rack::Builder.build(self.class, self) server.logger = Log4r::Logger.new 'proxy-server' server. = { :daemonize => false, :verbose => true, :log_stdout => true, :env => :development } server.start do p "Proxy started" end end end |
#stop ⇒ Object
40 41 42 |
# File 'lib/proxy/proxy_server.rb', line 40 def stop Thread.kill @proxy_thread if @proxy_thread end |
#substitute(uri) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/proxy/proxy_server.rb', line 83 def substitute(uri) @substitute_requests.each do |pattern, | if Regexp.new(pattern) =~ uri return get_substituted_response() end end nil end |
#substitute_request(pattern, options) ⇒ Object
101 102 103 |
# File 'lib/proxy/proxy_server.rb', line 101 def substitute_request(pattern, ) @substitute_requests[pattern] = end |
#track(uri) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/proxy/proxy_server.rb', line 109 def track(uri) @requests << uri @track_requests.each do |pattern| @requests << uri if Regexp.new(pattern) =~ uri end end |
#track_request(pattern) ⇒ Object
105 106 107 |
# File 'lib/proxy/proxy_server.rb', line 105 def track_request(pattern) @track_requests << pattern end |
#tracking_enabled?(uri) ⇒ Boolean
116 117 118 119 120 121 |
# File 'lib/proxy/proxy_server.rb', line 116 def tracking_enabled?(uri) @track_requests.each do |pattern| return true if Regexp.new(pattern) =~ uri end false end |