Class: RequestReplay
- Inherits:
-
Object
- Object
- RequestReplay
- Defined in:
- lib/request-replay.rb
Defined Under Namespace
Classes: Middleware
Constant Summary collapse
- NEWLINE =
"\r\n"
- HTTP_VERSION =
'HTTP/1.1'
- RACK_INPUT =
'rack.input'
Instance Method Summary collapse
- #add_headers ⇒ Object
- #capitalize_headers(header) ⇒ Object
- #headers ⇒ Object
- #headers_hash ⇒ Object
-
#initialize(env, host, options = {}) ⇒ RequestReplay
constructor
A new instance of RequestReplay.
- #read_wait ⇒ Object
- #request ⇒ Object
- #request_path ⇒ Object
- #sock ⇒ Object
- #start ⇒ Object
- #write_headers ⇒ Object
- #write_payload ⇒ Object
- #write_request ⇒ Object
Constructor Details
#initialize(env, host, options = {}) ⇒ RequestReplay
Returns a new instance of RequestReplay.
12 13 14 15 16 17 18 19 20 |
# File 'lib/request-replay.rb', line 12 def initialize env, host, ={} @env, (@host, @port), @options = env, host.split(':', 2), if env[RACK_INPUT] env[RACK_INPUT].rewind @buf = StringIO.new IO.copy_stream(env[RACK_INPUT], @buf) @buf.rewind end end |
Instance Method Details
#add_headers ⇒ Object
22 23 24 |
# File 'lib/request-replay.rb', line 22 def add_headers @options[:add_headers] || {} end |
#capitalize_headers(header) ⇒ Object
80 81 82 |
# File 'lib/request-replay.rb', line 80 def capitalize_headers header header.downcase.gsub(/[a-z]+/){ |s| s.capitalize }.tr('_', '-') end |
#headers ⇒ Object
61 62 63 |
# File 'lib/request-replay.rb', line 61 def headers headers_hash.map{ |name, value| "#{name}: #{value}" }.join(NEWLINE) end |
#headers_hash ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/request-replay.rb', line 70 def headers_hash @headers_hash ||= @env.inject({}){ |r, (k, v)| r[capitalize_headers(k[5..-1])] = v if k.start_with?('HTTP_') r }.merge('Content-Type' => @env['CONTENT_TYPE' ], 'Content-Length' => @env['CONTENT_LENGTH']). merge(add_headers).select{ |_, v| v } end |
#read_wait ⇒ Object
26 27 28 |
# File 'lib/request-replay.rb', line 26 def read_wait @options[:read_wait] && Float(@options[:read_wait]) end |
#request ⇒ Object
57 58 59 |
# File 'lib/request-replay.rb', line 57 def request "#{@env['REQUEST_METHOD'] || 'GET'} #{request_path} #{HTTP_VERSION}" end |
#request_path ⇒ Object
65 66 67 68 |
# File 'lib/request-replay.rb', line 65 def request_path "/#{@env['PATH_INFO']}?#{@env['QUERY_STRING']}". sub(%r{^//}, '/').sub(/\?$/, '') end |
#sock ⇒ Object
84 85 86 |
# File 'lib/request-replay.rb', line 84 def sock @sock ||= TCPSocket.new(@host, Integer(@port || 80)) end |
#start ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/request-replay.rb', line 30 def start write_request write_headers write_payload sock.close_write IO.select([sock], [], [], read_wait) if read_wait yield(sock) if block_given? rescue => e @env['rack.errors'].puts("[#{self.class.name}] Error: #{e.inspect}") if @env['rack.errors'] ensure sock.close end |
#write_headers ⇒ Object
48 49 50 |
# File 'lib/request-replay.rb', line 48 def write_headers sock.write("#{headers}#{NEWLINE}#{NEWLINE}") end |
#write_payload ⇒ Object
52 53 54 55 |
# File 'lib/request-replay.rb', line 52 def write_payload return unless @buf IO.copy_stream(@buf, sock) end |
#write_request ⇒ Object
44 45 46 |
# File 'lib/request-replay.rb', line 44 def write_request sock.write("#{request}#{NEWLINE}") end |