Created
June 23, 2009 00:55
-
-
Save moro/134297 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'pathname' | |
class DumpRequst | |
def initialize(app, dir, &out_strategy) | |
@app = app | |
@dir = dir | |
@out_strategy = out_strategy | |
Pathname.new(dir).mkpath unless File.directory?(dir) | |
end | |
def call(env) | |
e = env.dup | |
body = e.delete("rack.input") | |
rack, http = separate_env(e) | |
begin | |
File.open(filename, "w") do |f| | |
http.each{|k,v| f.puts "#{k} : #{v}" } | |
f.puts("\n") | |
rack.each{|k,v| f.puts "#{k} : #{v}" } | |
f.puts("\n") | |
f.write body.read | |
end | |
rescue => ignore | |
ensure | |
body.rewind if body.respond_to?(:rewind) | |
end | |
@app.call(env) | |
end | |
private | |
def filename | |
if @out_strategy | |
@out_strategy.call(@dir) | |
else | |
t = Time.now | |
fname = "%s.%03d.dat" % [t.strftime("%Y%m%d-%H%M%S"), (t.usec/1000.0).round] | |
File.expand_path(fname, @dir) | |
end | |
end | |
def separate_env(env) | |
env.to_a.sort_by(&:first).partition{|k,v| k =~ /\Arack\./ } | |
end | |
end | |
ActionController::Dispatcher.middleware.use ::DumpRequst, File.expand_path("tmp/dump-req", Rails.root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment