Class: RequestReplay

Inherits:
Object
  • Object
show all
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

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, options={}
  @env, (@host, @port), @options = env, host.split(':', 2), options
  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_headersObject



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

#headersObject



61
62
63
# File 'lib/request-replay.rb', line 61

def headers
  headers_hash.map{ |name, value| "#{name}: #{value}" }.join(NEWLINE)
end

#headers_hashObject



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_waitObject



26
27
28
# File 'lib/request-replay.rb', line 26

def read_wait
  @options[:read_wait] && Float(@options[:read_wait])
end

#requestObject



57
58
59
# File 'lib/request-replay.rb', line 57

def request
  "#{@env['REQUEST_METHOD'] || 'GET'} #{request_path} #{HTTP_VERSION}"
end

#request_pathObject



65
66
67
68
# File 'lib/request-replay.rb', line 65

def request_path
  "/#{@env['PATH_INFO']}?#{@env['QUERY_STRING']}".
    sub(%r{^//}, '/').sub(/\?$/, '')
end

#sockObject



84
85
86
# File 'lib/request-replay.rb', line 84

def sock
  @sock ||= TCPSocket.new(@host, Integer(@port || 80))
end

#startObject



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_headersObject



48
49
50
# File 'lib/request-replay.rb', line 48

def write_headers
  sock.write("#{headers}#{NEWLINE}#{NEWLINE}")
end

#write_payloadObject



52
53
54
55
# File 'lib/request-replay.rb', line 52

def write_payload
  return unless @buf
  IO.copy_stream(@buf, sock)
end

#write_requestObject



44
45
46
# File 'lib/request-replay.rb', line 44

def write_request
  sock.write("#{request}#{NEWLINE}")
end