Created
November 27, 2015 15:54
-
-
Save bdewater/a921555125651f28b791 to your computer and use it in GitHub Desktop.
Conditionally call Rack middleware (e.g. ContentLength) depending on path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/application.rb | |
config.middleware.use 'ConditionalContentLength', ['/foo', '/bar'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/conditional_content_length.rb | |
class ConditionalContentLength | |
def initialize(app, paths) | |
@app = app | |
@paths = paths | |
end | |
def call(env) | |
path_start_matches = @paths.inject(false) do |matched, path| | |
matched or env['PATH_INFO'].start_with? path | |
end | |
if path_start_matches | |
Rack::ContentLength.new(@app).call(env) | |
else | |
@app.call(env) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment