/
response.rb
56 lines (46 loc) · 1.42 KB
/
response.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module TestPuma
# A subclass of String, allows processing the response returned by
# `PumaSocket#send_http_read_response` and the `read_response` method added
# to native socket instances (created with `PumaSocket#new_socket` and
# `PumaSocket#send_http`.
#
class Response < String
attr_accessor :times
# Returns response headers as an array of lines
# @return [Array<String>]
def headers
@headers ||= begin
ary = self.split(RESP_SPLIT, 2).first.split LINE_SPLIT
@status = ary.shift
ary
end
end
# Returns response headers as a hash. All keys and values are strings.
# @return [Hash]
def headers_hash
@headers_hash ||= headers.map { |hdr| hdr.split ': ', 2 }.to_h
end
def status
headers
@status
end
def body
self.split(RESP_SPLIT, 2).last
end
# Decodes a chunked body
# @return [String] the decoded body
def decode_body
decoded = String.new # rubocop: disable Performance/UnfreezeString
body = self.split(RESP_SPLIT, 2).last
body = body.byteslice 0, body.bytesize - 5 # remove terminating bytes
loop do
size, body = body.split LINE_SPLIT, 2
size = size.to_i 16
decoded << body.byteslice(0, size)
body = body.byteslice (size+2)..-1 # remove segment ending "\r\n"
break if body.empty? || body.nil?
end
decoded
end
end
end