Class: HTTP::Protocol::HTTP11::Connection
- Inherits:
-
Object
- Object
- HTTP::Protocol::HTTP11::Connection
show all
- Defined in:
- lib/http/protocol/http11/connection.rb
Constant Summary
collapse
- CRLF =
"\r\n".freeze
- CONNECTION =
'connection'.freeze
- HOST =
'host'.freeze
- CLOSE =
'close'.freeze
- VERSION =
"HTTP/1.1".freeze
- HEAD =
"HEAD".freeze
- CONNECT =
"CONNECT".freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#close ⇒ Object
Close the connection and underlying stream.
-
#each_line ⇒ Object
-
#hijack ⇒ IO
Effectively close the connection and return the underlying IO.
-
#initialize(stream, persistent = true) ⇒ Connection
constructor
A new instance of Connection.
-
#persistent?(headers) ⇒ Boolean
-
#read_body(headers) ⇒ Object
-
#read_chunk ⇒ Object
-
#read_chunked_body ⇒ Object
-
#read_fixed_body(length) ⇒ Object
-
#read_headers ⇒ Object
-
#read_line ⇒ Object
-
#read_remainder_body ⇒ Object
-
#read_request ⇒ Object
-
#read_request_body(headers) ⇒ Object
-
#read_response(method) ⇒ Object
-
#read_response_body(method, status, headers) ⇒ Object
-
#read_tunnel_body ⇒ Object
-
#version ⇒ Object
-
#write_body(body, chunked = true) ⇒ Object
-
#write_body_and_close(body) ⇒ Object
-
#write_body_head(body) ⇒ Object
-
#write_chunk(chunk) ⇒ Object
-
#write_chunked_body(body) ⇒ Object
-
#write_empty_body(body) ⇒ Object
-
#write_fixed_length_body(body, length) ⇒ Object
-
#write_headers(headers) ⇒ Object
-
#write_persistent_header ⇒ Object
Write the appropriate header for connection persistence.
-
#write_request(authority, method, path, version, headers) ⇒ Object
-
#write_response(version, status, headers, body = nil, head = false) ⇒ Object
Constructor Details
#initialize(stream, persistent = true) ⇒ Connection
Returns a new instance of Connection.
33
34
35
36
37
38
39
|
# File 'lib/http/protocol/http11/connection.rb', line 33
def initialize(stream, persistent = true)
@stream = stream
@persistent = persistent
@count = 0
end
|
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
43
44
45
|
# File 'lib/http/protocol/http11/connection.rb', line 43
def count
@count
end
|
#persistent ⇒ Object
Returns the value of attribute persistent.
42
43
44
|
# File 'lib/http/protocol/http11/connection.rb', line 42
def persistent
@persistent
end
|
#stream ⇒ Object
Returns the value of attribute stream.
41
42
43
|
# File 'lib/http/protocol/http11/connection.rb', line 41
def stream
@stream
end
|
Instance Method Details
#close ⇒ Object
Close the connection and underlying stream.
73
74
75
|
# File 'lib/http/protocol/http11/connection.rb', line 73
def close
@stream.close
end
|
#each_line ⇒ Object
105
106
107
108
109
|
# File 'lib/http/protocol/http11/connection.rb', line 105
def each_line
while line = read_line
yield line
end
end
|
#hijack ⇒ IO
Effectively close the connection and return the underlying IO.
59
60
61
62
63
64
65
|
# File 'lib/http/protocol/http11/connection.rb', line 59
def hijack
@persistent = false
@stream.flush
return @stream
end
|
#persistent?(headers) ⇒ Boolean
49
50
51
52
53
54
55
|
# File 'lib/http/protocol/http11/connection.rb', line 49
def persistent?()
if connection = [CONNECTION]
return !connection.include?(CLOSE)
else
return true
end
end
|
#read_body(headers) ⇒ Object
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
# File 'lib/http/protocol/http11/connection.rb', line 345
def read_body()
if transfer_encoding = [TRANSFER_ENCODING]
if [CONTENT_LENGTH]
raise BadRequest, "Message contains both transfer encoding and content length!"
end
if transfer_encoding.last == CHUNKED
return read_chunked_body
else
return read_body_remainder
end
end
if content_length = [CONTENT_LENGTH]
length = Integer(content_length)
if length >= 0
return read_fixed_body(length)
else
raise BadRequest, "Invalid content length: #{content_length}"
end
end
end
|
#read_chunk ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/http/protocol/http11/connection.rb', line 163
def read_chunk
length = self.read_line.to_i(16)
if length == 0
self.read_line
return nil
end
chunk = @stream.read(length)
crlf = @stream.read(2)
return chunk
end
|
#read_chunked_body ⇒ Object
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/http/protocol/http11/connection.rb', line 280
def read_chunked_body
buffer = String.new.b
while chunk = read_chunk
buffer << chunk
chunk.clear
end
return buffer
end
|
#read_fixed_body(length) ⇒ Object
291
292
293
|
# File 'lib/http/protocol/http11/connection.rb', line 291
def read_fixed_body(length)
@stream.read(length)
end
|
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/http/protocol/http11/connection.rb', line 149
def
fields = []
self.each_line do |line|
if line =~ /^([a-zA-Z\-\d]+):\s*(.+?)\s*$/
fields << [$1, $2]
else
break
end
end
return Headers.new(fields)
end
|
#read_line ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/http/protocol/http11/connection.rb', line 111
def read_line
if line = @stream.gets(CRLF)
return line.chomp!(CRLF)
else
raise EOFError
end
end
|
#read_remainder_body ⇒ Object
299
300
301
|
# File 'lib/http/protocol/http11/connection.rb', line 299
def read_remainder_body
@stream.read
end
|
#read_request ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/http/protocol/http11/connection.rb', line 121
def read_request
method, path, version = read_line.split(/\s+/, 3)
=
@persistent = persistent?()
body = read_request_body()
@count += 1
return [HOST], method, path, version, , body
end
|
#read_request_body(headers) ⇒ Object
337
338
339
340
341
342
343
|
# File 'lib/http/protocol/http11/connection.rb', line 337
def read_request_body()
if body = read_body()
return body
end
end
|
#read_response(method) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/http/protocol/http11/connection.rb', line 134
def read_response(method)
version, status, reason = read_line.split(/\s+/, 3)
Async.logger.debug(self) {"#{version} #{status} #{reason}"}
=
@persistent = persistent?()
body = read_response_body(method, status, )
@count += 1
return version, Integer(status), reason, , body
end
|
#read_response_body(method, status, headers) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/http/protocol/http11/connection.rb', line 306
def read_response_body(method, status, )
if method == "HEAD" or status == 204 or status == 304
return nil
end
if method == "CONNECT" and status == 200
return read_tunnel_body
end
if body = read_body()
return body
else
return read_remainder_body
end
end
|
#read_tunnel_body ⇒ Object
295
296
297
|
# File 'lib/http/protocol/http11/connection.rb', line 295
def read_tunnel_body
read_remainder_body
end
|
#version ⇒ Object
45
46
47
|
# File 'lib/http/protocol/http11/connection.rb', line 45
def version
VERSION
end
|
#write_body(body, chunked = true) ⇒ Object
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/http/protocol/http11/connection.rb', line 256
def write_body(body, chunked = true)
if body.nil? or body.empty?
write_empty_body(body)
elsif length = body.length
write_fixed_length_body(body, length)
elsif chunked
write_chunked_body(body)
else
write_body_and_close(body)
end
end
|
#write_body_and_close(body) ⇒ Object
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/http/protocol/http11/connection.rb', line 241
def write_body_and_close(body)
@persistent = false
@stream.write("\r\n")
body.each do |chunk|
@stream.write(chunk)
@stream.flush
end
@stream.stream.close_write
end
|
#write_body_head(body) ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/http/protocol/http11/connection.rb', line 268
def write_body_head(body)
if body.nil? or body.empty?
@stream.write("content-length: 0\r\n\r\n")
elsif length = body.length
@stream.write("content-length: #{length}\r\n\r\n")
else
@stream.write("\r\n")
end
end
|
#write_chunk(chunk) ⇒ Object
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/http/protocol/http11/connection.rb', line 181
def write_chunk(chunk)
if chunk.nil?
@stream.write("0\r\n\r\n")
elsif !chunk.empty?
@stream.write("#{chunk.bytesize.to_s(16).upcase}\r\n")
@stream.write(chunk)
@stream.write(CRLF)
@stream.flush
end
end
|
#write_chunked_body(body) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/http/protocol/http11/connection.rb', line 224
def write_chunked_body(body)
@stream.write("transfer-encoding: chunked\r\n\r\n")
body.each do |chunk|
next if chunk.size == 0
@stream.write("#{chunk.bytesize.to_s(16).upcase}\r\n")
@stream.write(chunk)
@stream.write(CRLF)
@stream.flush
end
@stream.write("0\r\n\r\n")
@stream.flush
end
|
#write_empty_body(body) ⇒ Object
192
193
194
195
196
197
198
199
200
|
# File 'lib/http/protocol/http11/connection.rb', line 192
def write_empty_body(body)
@stream.write("content-length: 0\r\n\r\n")
body.read if body
@stream.flush
end
|
#write_fixed_length_body(body, length) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/http/protocol/http11/connection.rb', line 202
def write_fixed_length_body(body, length)
@stream.write("content-length: #{length}\r\n\r\n")
chunk_length = 0
body.each do |chunk|
chunk_length += chunk.bytesize
if chunk_length > length
raise ArgumentError, "Trying to write #{chunk_length} bytes, but content length was #{length} bytes!"
end
@stream.write(chunk)
end
@stream.flush
if chunk_length != length
raise ArgumentError, "Wrote #{chunk_length} bytes, but content length was #{length} bytes!"
end
end
|
99
100
101
102
103
|
# File 'lib/http/protocol/http11/connection.rb', line 99
def ()
.each do |name, value|
@stream.write("#{name}: #{value}\r\n")
end
end
|
Write the appropriate header for connection persistence.
68
69
70
|
# File 'lib/http/protocol/http11/connection.rb', line 68
def
@stream.write("connection: keep-alive\r\n") if @persistent
end
|
#write_request(authority, method, path, version, headers) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/http/protocol/http11/connection.rb', line 77
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 = nil, head = false) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/http/protocol/http11/connection.rb', line 86
def write_response(version, status, , body = nil, head = false)
@stream.write("#{version} #{status}\r\n")
()
if head
write_body_head(body)
else
write_body(body)
end
@stream.flush
end
|