Class: RequestReplay
- Inherits:
-
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
'Content-Type' .freeze
'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
34
35
36
|
# File 'lib/request-replay.rb', line 34
def
@options[:add_headers] || {}
end
|
91
92
93
|
# File 'lib/request-replay.rb', line 91
def
.downcase.gsub(/[a-z]+/){ |s| s.capitalize }.tr('_', '-')
end
|
72
73
74
|
# File 'lib/request-replay.rb', line 72
def
.map{ |name, value| "#{name}: #{value}" }.join(NEWLINE)
end
|
81
82
83
84
85
86
87
88
89
|
# File 'lib/request-replay.rb', line 81
def
@headers_hash ||=
@env.inject({}){ |r, (k, v)|
r[(k[5..-1])] = v if k.start_with?('HTTP_')
r
}.merge(HEADER_CTYPE => @env[CONTENT_TYPE ],
HEADER_CLENGTH => @env[CONTENT_LENGTH]).
merge().select{ |_, v| v }
end
|
#read_wait ⇒ Object
38
39
40
|
# File 'lib/request-replay.rb', line 38
def read_wait
@options[:read_wait] && Float(@options[:read_wait])
end
|
#request ⇒ Object
68
69
70
|
# File 'lib/request-replay.rb', line 68
def request
"#{@env[REQUEST_METHOD] || 'GET'} #{request_path} #{HTTP_VERSION}"
end
|
#request_path ⇒ Object
76
77
78
79
|
# File 'lib/request-replay.rb', line 76
def request_path
"/#{@env[PATH_INFO]}?#{@env[QUERY_STRING]}".
sub(%r{^//}, '/').sub(/\?$/, '')
end
|
#sock ⇒ Object
95
96
97
|
# File 'lib/request-replay.rb', line 95
def sock
@sock ||= TCPSocket.new(@host, Integer(@port || 80))
end
|
#start ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/request-replay.rb', line 42
def start
write_request
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
|
59
60
61
|
# File 'lib/request-replay.rb', line 59
def
sock.write("#{}#{NEWLINE}#{NEWLINE}")
end
|
#write_payload ⇒ Object
63
64
65
66
|
# File 'lib/request-replay.rb', line 63
def write_payload
return unless @buf
IO.copy_stream(@buf, sock)
end
|
#write_request ⇒ Object
55
56
57
|
# File 'lib/request-replay.rb', line 55
def write_request
sock.write("#{request}#{NEWLINE}")
end
|