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
77 78 79 |
# File 'lib/request-replay.rb', line 77 def capitalize_headers header header.downcase.gsub(/[a-z]+/){ |s| s.capitalize }.tr('_', '-') end |
#headers ⇒ Object
58 59 60 |
# File 'lib/request-replay.rb', line 58 def headers headers_hash.map{ |name, value| "#{name}: #{value}" }.join(NEWLINE) end |
#headers_hash ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/request-replay.rb', line 67 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
54 55 56 |
# File 'lib/request-replay.rb', line 54 def request "#{@env['REQUEST_METHOD'] || 'GET'} #{request_path} #{HTTP_VERSION}" end |
#request_path ⇒ Object
62 63 64 65 |
# File 'lib/request-replay.rb', line 62 def request_path "/#{@env['PATH_INFO']}?#{@env['QUERY_STRING']}". sub(%r{^//}, '/').sub(/\?$/, '') end |
#sock ⇒ Object
81 82 83 |
# File 'lib/request-replay.rb', line 81 def sock @sock ||= TCPSocket.new(@host, Integer(@port || 80)) end |
#start ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# 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? ensure sock.close end |
#write_headers ⇒ Object
45 46 47 |
# File 'lib/request-replay.rb', line 45 def write_headers sock.write("#{headers}#{NEWLINE}#{NEWLINE}") end |
#write_payload ⇒ Object
49 50 51 52 |
# File 'lib/request-replay.rb', line 49 def write_payload return unless @buf IO.copy_stream(@buf, sock) end |
#write_request ⇒ Object
41 42 43 |
# File 'lib/request-replay.rb', line 41 def write_request sock.write("#{request}#{NEWLINE}") end |