Class: HTTPX::Connection::HTTP1
Constant Summary
collapse
- MAX_REQUESTS =
200
- CRLF =
"\r\n"
Constants included
from Loggable
Loggable::COLORS, Loggable::USE_DEBUG_LOG
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Loggable
#log, #log_exception
#callbacks_for?, #emit, #on, #once
Constructor Details
#initialize(buffer, options) ⇒ HTTP1
Returns a new instance of HTTP1.
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/httpx/connection/http1.rb', line 17
def initialize(buffer, options)
@options = options
@max_concurrent_requests = @options.max_concurrent_requests || MAX_REQUESTS
@max_requests = @options.max_requests
@parser = Parser::HTTP1.new(self)
@buffer = buffer
@version = [1, 1]
@pending = []
@requests = []
@handshake_completed = false
end
|
Instance Attribute Details
#max_concurrent_requests ⇒ Object
Returns the value of attribute max_concurrent_requests.
15
16
17
|
# File 'lib/httpx/connection/http1.rb', line 15
def max_concurrent_requests
@max_concurrent_requests
end
|
#pending ⇒ Object
Returns the value of attribute pending.
13
14
15
|
# File 'lib/httpx/connection/http1.rb', line 13
def pending
@pending
end
|
#requests ⇒ Object
Returns the value of attribute requests.
13
14
15
|
# File 'lib/httpx/connection/http1.rb', line 13
def requests
@requests
end
|
Instance Method Details
#<<(data) ⇒ Object
75
76
77
|
# File 'lib/httpx/connection/http1.rb', line 75
def <<(data)
@parser << data
end
|
#close ⇒ Object
55
56
57
58
|
# File 'lib/httpx/connection/http1.rb', line 55
def close
reset
emit(:close, true)
end
|
#consume ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/httpx/connection/http1.rb', line 91
def consume
requests_limit = [@max_requests, @requests.size].min
concurrent_requests_limit = [@max_concurrent_requests, requests_limit].min
@requests.each_with_index do |request, idx|
break if idx >= concurrent_requests_limit
next if request.state == :done
handle(request)
end
end
|
#dispatch ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/httpx/connection/http1.rb', line 163
def dispatch
request = @request
if request.expects?
@parser.reset!
return handle(request)
end
@request = nil
@requests.shift
response = request.response
response.finish! unless response.is_a?(ErrorResponse)
emit(:response, request, response)
if @parser.upgrade?
response << @parser.upgrade_data
throw(:called)
end
@parser.reset!
@max_requests -= 1
if response.is_a?(ErrorResponse)
disable
else
manage_connection(request, response)
end
if exhausted?
@pending.concat(@requests)
@requests.clear
emit(:exhausted)
else
send(@pending.shift) unless @pending.empty?
end
end
|
#empty? ⇒ Boolean
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/httpx/connection/http1.rb', line 64
def empty?
@requests.empty? || (
!@requests.first.response.nil? &&
(@requests.size == 1 || !@requests.last.response.nil?)
)
end
|
#exhausted? ⇒ Boolean
60
61
62
|
# File 'lib/httpx/connection/http1.rb', line 60
def exhausted?
!@max_requests.positive?
end
|
#handle_error(ex, request = nil) ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/httpx/connection/http1.rb', line 200
def handle_error(ex, request = nil)
if (ex.is_a?(EOFError) || ex.is_a?(TimeoutError)) && @request && @request.response &&
!@request.response..key?("content-length") &&
!@request.response..key?("transfer-encoding")
catch(:called) { on_complete }
return
end
if @pipelining
catch(:called) { disable }
else
@requests.each do |req|
next if request && request == req
emit(:error, req, ex)
end
@pending.each do |req|
next if request && request == req
emit(:error, req, ex)
end
end
end
|
#interests ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/httpx/connection/http1.rb', line 33
def interests
return :r if @request
return if @requests.empty?
request = @requests.first
return unless request
return :w if request.interests == :w || !@buffer.empty?
:r
end
|
#on_complete ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/httpx/connection/http1.rb', line 154
def on_complete
request = @request
return unless request
log(level: 2) { "parsing complete" }
dispatch
end
|
#on_data(chunk) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/httpx/connection/http1.rb', line 138
def on_data(chunk)
request = @request
return unless request
log(color: :green) { "-> DATA: #{chunk.bytesize} bytes..." }
log(level: 2, color: :green) { "-> #{chunk.inspect}" }
response = request.response
response << chunk
rescue StandardError => e
error_response = ErrorResponse.new(request, e)
request.response = error_response
dispatch
end
|
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/httpx/connection/http1.rb', line 110
def (h)
@request = @requests.first
return if @request.response
log(level: 2) { "headers received" }
= @request.options..new(h)
response = @request.options.response_class.new(@request,
@parser.status_code,
@parser.http_version.join("."),
)
log(color: :yellow) { "-> HEADLINE: #{response.status} HTTP/#{@parser.http_version.join(".")}" }
log(color: :yellow) { response..each.map { |f, v| "-> HEADER: #{f}: #{v}" }.join("\n") }
@request.response = response
on_complete if response.finished?
end
|
#on_start ⇒ Object
HTTP Parser callbacks
must be public methods, or else they won’t be reachable
106
107
108
|
# File 'lib/httpx/connection/http1.rb', line 106
def on_start
log(level: 2) { "parsing begins" }
end
|
#on_trailers(h) ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/httpx/connection/http1.rb', line 128
def on_trailers(h)
return unless @request
response = @request.response
log(level: 2) { "trailer headers received" }
log(color: :yellow) { h.each.map { |f, v| "-> HEADER: #{f}: #{v.join(", ")}" }.join("\n") }
response.(h)
end
|
#ping ⇒ Object
227
228
229
230
231
|
# File 'lib/httpx/connection/http1.rb', line 227
def ping
reset
emit(:reset)
emit(:exhausted)
end
|
#reset ⇒ Object
48
49
50
51
52
53
|
# File 'lib/httpx/connection/http1.rb', line 48
def reset
@max_requests = @options.max_requests || MAX_REQUESTS
@parser.reset!
@handshake_completed = false
@pending.concat(@requests) unless @requests.empty?
end
|
#send(request) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/httpx/connection/http1.rb', line 79
def send(request)
unless @max_requests.positive?
@pending << request
return
end
return if @requests.include?(request)
@requests << request
@pipelining = true if @requests.size > 1
end
|
#timeout ⇒ Object
29
30
31
|
# File 'lib/httpx/connection/http1.rb', line 29
def timeout
@options.timeout[:operation_timeout]
end
|