blob: 9e50f0211dc6cc1e059dc385502a67128b87827f (
plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# -*- frozen-string-literal: true -*-
module Plum
class Response
# The response headers
# @return [Hash<String, String>]
attr_reader :headers
# @api private
def initialize(auto_decode: true, **options)
@body = Queue.new
@finished = false
@failed = false
@body = []
@auto_decode = auto_decode
end
# Returns the HTTP status code.
# @return [String] the HTTP status code
def status
@headers && @headers[":status"]
end
# Returns the header value that correspond to the header name.
# @param key [String] the header name
# @return [String] the header value
def [](key)
@headers[key.to_s.downcase]
end
# Returns whether the response is complete or not.
# @return [Boolean]
def finished?
@finished
end
# Returns whether the request has failed or not.
# @return [Boolean]
def failed?
@failed
end
# Set callback tha called when received a chunk of response body.
# @yield [chunk] A chunk of the response body.
def on_chunk(&block)
raise "Body already read" if @on_chunk
raise ArgumentError, "block must be given" unless block_given?
@on_chunk = block
unless @body.empty?
@body.each(&block)
@body.clear
end
end
# Set callback that will be called when the response finished.
def on_finish(&block)
raise ArgumentError, "block must be given" unless block_given?
if finished?
yield
else
@on_finish = block
end
end
# Returns the complete response body. Use #each_body instead if the body can be very large.
# @return [String] the whole response body
def body
raise "Body already read" if @on_chunk
raise "Response body is not complete" unless finished?
@body.join
end
# @api private
def _headers(raw_headers)
# response headers should not have duplicates
@headers = raw_headers.to_h.freeze
@decoder = setup_decoder
end
# @api private
def _chunk(encoded)
chunk = @decoder.decode(encoded)
if @on_chunk
@on_chunk.call(chunk)
else
@body << chunk
end
end
# @api private
def _finish
@finished = true
@decoder.finish
@on_finish.call if @on_finish
end
# @api private
def _fail
@failed = true
end
private
def setup_decoder
if @auto_decode
klass = Decoders::DECODERS[@headers["content-encoding"]]
end
klass ||= Decoders::Base
klass.new
end
end
end
|