Class: Async::HTTP::Protocol::HTTP11
- Inherits:
-
IO::Protocol::Line
- Object
- IO::Protocol::Line
- Async::HTTP::Protocol::HTTP11
show all
- Defined in:
- lib/async/http/protocol/http11.rb
Overview
Implements basic HTTP/1.1 request/response.
Direct Known Subclasses
HTTP10
Defined Under Namespace
Classes: Request, Response
Constant Summary
collapse
- CRLF =
"\r\n".freeze
- CONNECTION =
'connection'.freeze
- HOST =
'host'.freeze
- CLOSE =
'close'.freeze
- VERSION =
"HTTP/1.1".freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(stream) ⇒ HTTP11
Returns a new instance of HTTP11.
44
45
46
47
48
49
|
# File 'lib/async/http/protocol/http11.rb', line 44
def initialize(stream)
super(stream, CRLF)
@persistent = true
@count = 0
end
|
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
55
56
57
|
# File 'lib/async/http/protocol/http11.rb', line 55
def count
@count
end
|
Instance Method Details
#call(request) ⇒ Object
Used by the client to send requests to the remote server.
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/async/http/protocol/http11.rb', line 162
def call(request)
Async.logger.debug(self) {"#{request.method} #{request.path} #{request..inspect}"}
begin
write_request(request.authority, request.method, request.path, self.version, request.)
rescue
raise RequestFailed.new
end
write_body(request.body)
return Response.new(self, request)
rescue
@stream.close
raise
end
|
#good? ⇒ Boolean
Can we use this connection to make requests?
63
64
65
|
# File 'lib/async/http/protocol/http11.rb', line 63
def good?
@stream.connected?
end
|
#hijack ⇒ Async::Wrapper
Returns the underlying non-blocking IO.
89
90
91
92
93
94
95
|
# File 'lib/async/http/protocol/http11.rb', line 89
def hijack
@persistent = false
@stream.flush
return @stream.io
end
|
#multiplex ⇒ Object
Only one simultaneous connection at a time.
58
59
60
|
# File 'lib/async/http/protocol/http11.rb', line 58
def multiplex
1
end
|
#next_request ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/async/http/protocol/http11.rb', line 113
def next_request
return nil unless @persistent
request = Request.new(self)
unless persistent?(request.)
@persistent = false
end
return request
rescue
write_response(self.version, 400, {}, nil)
raise
end
|
#peer ⇒ Object
51
52
53
|
# File 'lib/async/http/protocol/http11.rb', line 51
def peer
@stream.io
end
|
#persistent?(headers) ⇒ Boolean
80
81
82
83
84
85
86
|
# File 'lib/async/http/protocol/http11.rb', line 80
def persistent?()
if connection = [CONNECTION]
return !connection.include?(CLOSE)
else
return true
end
end
|
#read_request ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/async/http/protocol/http11.rb', line 207
def read_request
method, path, version = read_line.split(/\s+/, 3)
=
@persistent = persistent?()
body = read_request_body()
@count += 1
return .delete(HOST), method, path, version, , body
end
|
#read_response(request) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/async/http/protocol/http11.rb', line 192
def read_response(request)
version, status, reason = read_line.split(/\s+/, 3)
Async.logger.debug(self) {"#{version} #{status} #{reason}"}
=
@persistent = persistent?()
body = read_response_body(request, status, )
@count += 1
return version, Integer(status), reason, , body
end
|
#receive_requests(task: Task.current) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/async/http/protocol/http11.rb', line 132
def receive_requests(task: Task.current)
while request = next_request
response = yield(request, self)
return if @stream.closed?
if response
write_response(self.version, response.status, response., response.body)
else
write_response(self.version, 500, {}, nil)
end
request.finish
task.yield
end
end
|
#reusable? ⇒ Boolean
67
68
69
|
# File 'lib/async/http/protocol/http11.rb', line 67
def reusable?
@persistent && !@stream.closed?
end
|
#version ⇒ Object
76
77
78
|
# File 'lib/async/http/protocol/http11.rb', line 76
def version
VERSION
end
|
#write_request(authority, method, path, version, headers) ⇒ Object
184
185
186
187
188
189
190
|
# File 'lib/async/http/protocol/http11.rb', line 184
def write_request(authority, method, path, version, )
@stream.write("#{method} #{path} #{version}\r\n")
@stream.write("host: #{authority}\r\n")
()
@stream.flush
end
|
#write_response(version, status, headers, body) ⇒ Object
220
221
222
223
224
225
226
|
# File 'lib/async/http/protocol/http11.rb', line 220
def write_response(version, status, , body)
@stream.write("#{version} #{status}\r\n")
()
write_body(body)
@stream.flush
end
|