1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
# frozen_string_literal: true
module Gitlab
class Experiment
class Middleware
def self.redirect(id, url)
raise Error, 'no url to redirect to' if url.blank?
experiment = Gitlab::Experiment.from_param(id)
[303, { 'Location' => experiment.process_redirect_url(url) || raise(Error, 'not redirecting') }, []]
end
def initialize(app, base_path)
@app = app
@matcher = %r{^#{base_path}/(?<id>.+)}
end
def call(env)
return @app.call(env) if env['REQUEST_METHOD'] != 'GET' || (match = @matcher.match(env['PATH_INFO'])).nil?
Middleware.redirect(match[:id], env['QUERY_STRING'])
rescue Error
@app.call(env)
end
end
end
end
|