Class: RequestReplay

Inherits:
Object
  • Object
show all
Defined in:
lib/request-replay.rb

Defined Under Namespace

Classes: Middleware, Proxy

Constant Summary collapse

NEWLINE =
"\r\n"          .freeze
HTTP_VERSION =
'HTTP/1.1'      .freeze
RACK_INPUT =
'rack.input'    .freeze
RACK_ERRORS =
'rack.errors'   .freeze
REQUEST_METHOD =
'REQUEST_METHOD'.freeze
PATH_INFO =
'PATH_INFO'     .freeze
QUERY_STRING =
'QUERY_STRING'  .freeze
CONTENT_TYPE =
'CONTENT_TYPE'  .freeze
CONTENT_LENGTH =
'CONTENT_LENGTH'.freeze
HEADER_CTYPE =
'Content-Type'  .freeze
HEADER_CLENGTH =
'Content-Length'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(env, host, options = {}) ⇒ RequestReplay

Returns a new instance of RequestReplay.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/request-replay.rb', line 21

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
    env[RACK_INPUT].rewind
  else
    @buf = nil
  end
end

Instance Method Details

#add_headersObject



34
35
36
# File 'lib/request-replay.rb', line 34

def add_headers
  @options[:add_headers] || {}
end

#capitalize_headers(header) ⇒ Object



91
92
93
# File 'lib/request-replay.rb', line 91

def capitalize_headers header
  header.downcase.gsub(/[a-z]+/){ |s| s.capitalize }.tr('_', '-')
end

#headersObject



72
73
74
# File 'lib/request-replay.rb', line 72

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

#headers_hashObject



81
82
83
84
85
86
87
88
89
# File 'lib/request-replay.rb', line 81

def headers_hash
  @headers_hash ||=
    @env.inject({}){ |r, (k, v)|
      r[capitalize_headers(k[5..-1])] = v if k.start_with?('HTTP_')
      r
    }.merge(HEADER_CTYPE   => @env[CONTENT_TYPE  ],
            HEADER_CLENGTH => @env[CONTENT_LENGTH]).
      merge(add_headers).select{ |_, v| v }
end

#read_waitObject



38
39
40
# File 'lib/request-replay.rb', line 38

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

#requestObject



68
69
70
# File 'lib/request-replay.rb', line 68

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

#request_pathObject



76
77
78
79
# File 'lib/request-replay.rb', line 76

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

#sockObject



95
96
97
# File 'lib/request-replay.rb', line 95

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

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/request-replay.rb', line 42

def start
  write_request
  write_headers
  write_payload
  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



59
60
61
# File 'lib/request-replay.rb', line 59

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

#write_payloadObject



63
64
65
66
# File 'lib/request-replay.rb', line 63

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

#write_requestObject



55
56
57
# File 'lib/request-replay.rb', line 55

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