Class: Rack::StreamingProxy
- Inherits:
-
Object
- Object
- Rack::StreamingProxy
- Defined in:
- lib/rack/streaming_proxy.rb,
lib/rack/streaming_proxy/proxy_request.rb
Defined Under Namespace
Classes: Error, ProxyRequest
Constant Summary collapse
- VERSION =
:stopdoc:
'1.0.3'
- LIBPATH =
::File.(::File.dirname(__FILE__)) + ::File::SEPARATOR
- PATH =
::File.(::File.join(::File.dirname(__FILE__), "..", "..")) + ::File::SEPARATOR
Class Method Summary collapse
-
.libpath(*args) ⇒ Object
Returns the library path for the module.
-
.path(*args) ⇒ Object
Returns the lpath for the module.
-
.require_all_libs_relative_to(fname, dir = nil) ⇒ Object
Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in.
-
.version ⇒ Object
Returns the version string for the library.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, &block) ⇒ StreamingProxy
constructor
The block provided to the initializer is given a Rack::Request and should return:.
Constructor Details
#initialize(app, &block) ⇒ StreamingProxy
The block provided to the initializer is given a Rack::Request and should return:
* nil/false to skip the proxy and continue down the stack
* a complete uri (with query string if applicable) to proxy to
E.g.
use Rack::StreamingProxy do |req|
if req.path.start_with?("/search")
"http://some_other_service/search?#{req.query}"
end
end
Most headers, request body, and HTTP method are preserved.
63 64 65 66 |
# File 'lib/rack/streaming_proxy.rb', line 63 def initialize(app, &block) @request_uri = block @app = app end |
Class Method Details
.libpath(*args) ⇒ Object
Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join
.
22 23 24 |
# File 'lib/rack/streaming_proxy.rb', line 22 def self.libpath( *args ) args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten) end |
.path(*args) ⇒ Object
Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join
.
30 31 32 |
# File 'lib/rack/streaming_proxy.rb', line 30 def self.path( *args ) args.empty? ? PATH : ::File.join(PATH, args.flatten) end |
.require_all_libs_relative_to(fname, dir = nil) ⇒ Object
Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.
39 40 41 42 43 44 45 |
# File 'lib/rack/streaming_proxy.rb', line 39 def self.require_all_libs_relative_to( fname, dir = nil ) dir ||= ::File.basename(fname, '.*') search_me = ::File.( ::File.join(::File.dirname(fname), dir, '**', '*.rb')) Dir.glob(search_me).sort.each {|rb| require rb} end |
.version ⇒ Object
Returns the version string for the library.
14 15 16 |
# File 'lib/rack/streaming_proxy.rb', line 14 def self.version VERSION end |
Instance Method Details
#call(env) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rack/streaming_proxy.rb', line 68 def call(env) req = Rack::Request.new(env) unless uri = request_uri.call(req) code, headers, body = app.call(env) unless headers['X-Accel-Redirect'] return [code, headers, body] else proxy_env = env.merge("PATH_INFO" => headers['X-Accel-Redirect']) unless uri = request_uri.call(Rack::Request.new(proxy_env)) raise "Could not proxy #{headers['X-Accel-Redirect']}: Path does not map to any uri" end end end begin # only want to catch proxy errors, not app errors proxy = ProxyRequest.new(req, uri) [proxy.status, proxy.headers, proxy] rescue => e msg = "Proxy error when proxying to #{uri}: #{e.class}: #{e.}" env["rack.errors"].puts msg env["rack.errors"].puts e.backtrace.map { |l| "\t" + l } env["rack.errors"].flush raise Error, msg end end |