Class: Strobe::Middleware::Proxy
- Inherits:
-
Object
- Object
- Strobe::Middleware::Proxy
- Defined in:
- lib/strobe/middleware/proxy.rb
Defined Under Namespace
Classes: ProxyRequest
Constant Summary collapse
- PROXY_PATH =
%r[/_strobe/proxy/([^/]+)(/.*)?$]
Class Method Summary collapse
- .parse_header_list(str) ⇒ Object
- .proxy_response?(status, hdrs) ⇒ Boolean
- .rackify(key) ⇒ Object
- .redirect_env(is_head, hdrs, body, callback) ⇒ Object
- .redirect_opts(hdrs) ⇒ Object
- .strobe_redirect(is_head, _hdrs, body, callback) ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #scheme_from_env(env) ⇒ Object
Constructor Details
#initialize(app, opts = {}) ⇒ Proxy
Returns a new instance of Proxy.
86 87 88 |
# File 'lib/strobe/middleware/proxy.rb', line 86 def initialize(app, opts = {}) @app, @opts = app, opts end |
Class Method Details
.parse_header_list(str) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/strobe/middleware/proxy.rb', line 61 def self.parse_header_list(str) return {} unless str parsed = {} str.downcase.split(',').each do |item| from, to, _ = item.split('=') to ||= from parsed[from.strip] = to.strip end parsed end |
.proxy_response?(status, hdrs) ⇒ Boolean
8 9 10 |
# File 'lib/strobe/middleware/proxy.rb', line 8 def self.proxy_response?(status, hdrs) status == 302 && hdrs.any? { |k,v| k =~ /x-strobe-location/i } end |
.rackify(key) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/strobe/middleware/proxy.rb', line 74 def self.rackify(key) key = key.to_s.gsub('-', '_').upcase key = "HTTP_#{key}" unless %w(REQUEST_METHOD CONTENT_LENGTH QUERY_STRING SERVER_NAME SERVER_PORT PATH_INFO SCRIPT_NAME).include?(key) key end |
.redirect_env(is_head, hdrs, body, callback) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/strobe/middleware/proxy.rb', line 25 def self.redirect_env(is_head, hdrs, body, callback) env = {} parse_header_list(hdrs['x-strobe-forward-headers']).each do |from, to| env[rackify(to)] = hdrs[from] end env['REQUEST_METHOD'] = hdrs['x-strobe-redirect-method'] env['REQUEST_METHOD'] ||= is_head ? 'HEAD' : 'GET' env['async.callback'] = callback # Rack::Request doesn't have join method body_str = "" body.each { |chunk| body_str << chunk } env['rack.input'] = StringIO.new(body_str) env end |
.redirect_opts(hdrs) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/strobe/middleware/proxy.rb', line 44 def self.redirect_opts(hdrs) extra_headers = {} block_headers = [] parse_header_list(hdrs['x-strobe-add-headers']).each do |from, to| extra_headers[to] = hdrs[from] end parse_header_list(hdrs['x-strobe-block-headers']).each do |key, _| block_headers << key end { :extra_headers => extra_headers, :block_headers => block_headers, :block_cookies => true } end |
.strobe_redirect(is_head, _hdrs, body, callback) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/strobe/middleware/proxy.rb', line 12 def self.strobe_redirect(is_head, _hdrs, body, callback) hdrs = {} _hdrs.each { |k,v| hdrs[k.to_s.downcase] = v } location = hdrs['x-strobe-location'] env = redirect_env(is_head, hdrs, body, callback) = redirect_opts(hdrs) request = ProxyRequest.new(env, location, ) request.handle! end |
Instance Method Details
#call(env) ⇒ Object
90 91 92 93 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 |
# File 'lib/strobe/middleware/proxy.rb', line 90 def call(env) if env['PATH_INFO'] =~ PROXY_PATH p1, p2 = $1, $2 scheme = scheme_from_env(env) host, port = p1.split(':') path = p2 || '/' port = (port || (scheme == 'http' ? 80 : 443)).to_i url = "#{scheme}://#{host}" if port if (scheme == 'http' && port != 80) || (scheme == 'https' && port != 443) url << ":#{port}" end end url << path if !env['QUERY_STRING'].empty? url << "?#{env['QUERY_STRING']}" end request = ProxyRequest.new(env, url, @opts) request.handle! throw :async else @app.call(env) end end |
#scheme_from_env(env) ⇒ Object
121 122 123 |
# File 'lib/strobe/middleware/proxy.rb', line 121 def scheme_from_env(env) env['HTTP_X_STROBE_PROXY_PROTOCOL'] || env['rack.url_scheme'] end |