class Scrubber
def initialize(app, options)
@app = app
@routes = options[:routes]
end
def call(env)
scrub(env)
@app.call(env)
end
private
def scrub(env)
return unless @routes.include?(env["PATH_INFO"])
rack_input = env["rack.input"].read
params = Rack::Utils.parse_query(rack_input, "&")
params["xml"] = Rack::Utils.unescape(params["xml"])
env["rack.input"] = StringIO.new(Rack::Utils.build_query(params))
rescue
ensure
env["rack.input"].rewind
end
end
# 注意,这里直接引用相关类
config.middleware.insert_before ActionController::ParamsParser,
"Scrubber",
:routes => [ "/examples/scrubbed" ]
class ResponseTimer
def initialize(app, message = "Response Time")
@app = app
@message = message
end
def call(env)
dup._call(env)
end
def _call(env)
@start = Time.now
@status, @headers, @response = @app.call(env)
@stop = Time.now
[@status, @headers, self]
end
def each(&block)
if @headers["Content-Type"].include? "text/html"
block.call("<!-- #{@message}: #{@stop - @start} -->\n")
end
@response.each(&block)
end
end
# 注意,这里以字符串的方式引用
config.middleware.use "ResponseTimer", "Load Time"