Class: StackMob::Middleware::Proxy
- Inherits:
-
Rack::Proxy
- Object
- Rack::Proxy
- StackMob::Middleware::Proxy
- Defined in:
- lib/stackmob/middleware/proxy.rb
Constant Summary collapse
- HEADER_NAME =
'X-StackMob-Proxy'
- RACK_ENV_NAME =
'HTTP_X_STACKMOB_PROXY_PLAIN'
- LEGACY_RACK_ENV_NAME =
'HTTP_X_STACKMOB_PROXY'
- VALID_HEADER_VALUES =
['stackmob-api']
- STACKMOB_FORWARDED_HOST_ENV =
'HTTP_X_STACKMOB_FORWARDED_HOST'
- STACKMOB_FORWARDED_PORT_ENV =
'HTTP_X_STACKMOB_FORWARDED_PORT'
- EXCLUDED_HEADERS =
["VERSION", "DATE", "HOST", "ACCEPT"].map { |s| "HTTP_#{s}" }
Instance Method Summary collapse
- #call(env) ⇒ Object
- #http_headers(env) ⇒ Object
- #http_method(env) ⇒ Object
-
#initialize(app) ⇒ Proxy
constructor
A new instance of Proxy.
- #normalize(key) ⇒ Object
- #rewrite_env(env) ⇒ Object
Constructor Details
#initialize(app) ⇒ Proxy
Returns a new instance of Proxy.
31 32 33 |
# File 'lib/stackmob/middleware/proxy.rb', line 31 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/stackmob/middleware/proxy.rb', line 35 def call(env) if VALID_HEADER_VALUES.include?(env[RACK_ENV_NAME]) || env['PATH_INFO'] =~ /^\/?.*\/accessToken/ super(env) elsif VALID_HEADER_VALUES.include?(env[LEGACY_RACK_ENV_NAME]) req = ::Rack::Request.new(env) method = http_method(env) headers = http_headers(env) params = [:put,:post].include?(method) ? req.body : req.query_string response = client.request(method, :api, env['PATH_INFO'], params, true, headers) [response.code.to_i, response.to_hash, response.body] else @app.call(env) end end |
#http_headers(env) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/stackmob/middleware/proxy.rb', line 73 def http_headers(env) headers = {} for headerArr in env.select { |k, v| k.start_with? 'HTTP_' } if !EXCLUDED_HEADERS.include?(headerArr[0]) headers[normalize(headerArr[0])] = headerArr[1] end end headers["Accept"] = "application/json" headers end |
#http_method(env) ⇒ Object
69 70 71 |
# File 'lib/stackmob/middleware/proxy.rb', line 69 def http_method(env) env['REQUEST_METHOD'].downcase.to_sym end |
#normalize(key) ⇒ Object
84 85 86 |
# File 'lib/stackmob/middleware/proxy.rb', line 84 def normalize(key) key.sub('HTTP_', '').split('_').map! { |p| p.downcase }.join("-") end |
#rewrite_env(env) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/stackmob/middleware/proxy.rb', line 52 def rewrite_env(env) env[STACKMOB_FORWARDED_HOST_ENV] = "127.0.0.1" env[STACKMOB_FORWARDED_PORT_ENV] = env['SERVER_PORT'] env['HTTP_HOST'] = StackMob.plain_proxy_host if StackMob.plain_proxy_host != ENV['STACKMOB_DEV_URL'] # rewrite port for api.stackmob.com env['SERVER_PORT'] = 80 end env end |