Skip to content

Instantly share code, notes, and snippets.

@bdewater
Created November 27, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdewater/a921555125651f28b791 to your computer and use it in GitHub Desktop.
Save bdewater/a921555125651f28b791 to your computer and use it in GitHub Desktop.
Conditionally call Rack middleware (e.g. ContentLength) depending on path
# config/application.rb
config.middleware.use 'ConditionalContentLength', ['/foo', '/bar']
# 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