protocol-http1 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of protocol-http1 might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62f627bcb79ea69ffb6e8c4aef117cb467a248e30de49fc71ecbde19a9ea7efc
4
- data.tar.gz: 0f989b969a3242abe794e8ade640d65cdc1c08730e8f04e40bdf073cda02139f
3
+ metadata.gz: 2dfc82795dbd903f87734cbb81c3606a77c0805798d12c1175d7ead652df3fca
4
+ data.tar.gz: 6ac2c382cdf141214fc27b07c3c7e19b47759dc373a4d08a81895d2a027a57ef
5
5
  SHA512:
6
- metadata.gz: 19ed83781ae2acf812f07182a16581067e929f5faa9bccaa5265a265919c02c30efcaecd97ece717b11b4a370fd948720ea20ef82fd27dae4d57683266507b8e
7
- data.tar.gz: 5ef114c31372b9058f4daae3962286d4afd60188736c0fa2726fb73d420c54cb54992ea1d3a8637d05c939f8da8229371a44a8068549f2cf37d02aad2cdfe746
6
+ metadata.gz: 2d9eb50902a3e8865a00fcb2658fa2d639ac41ce1e91e57868099822a8b4205fe177de0cae438d933e7bfbd38fda2fa4aa4de4427ae8bd21601151a98cb61519
7
+ data.tar.gz: 477a532d2b52e92c94022733d02d8c77bab86de01f7eda6bb65f447a3414c8f8f8a6253dc70b6adfcb4771b611bc2f826a42248d6e83bb6c89f8a6b466578350
@@ -24,23 +24,29 @@ module Protocol
24
24
  module HTTP1
25
25
  module Body
26
26
  class Remainder < HTTP::Body::Readable
27
+ BLOCK_SIZE = 1024 * 64
28
+
29
+ # block_size may be removed in the future. It is better managed by stream.
27
30
  def initialize(stream)
28
31
  @stream = stream
29
32
  end
30
33
 
31
34
  def empty?
32
- @stream.closed?
35
+ @stream.eof? or @stream.closed?
33
36
  end
34
37
 
35
38
  def close(error = nil)
36
39
  # We can't really do anything in this case except close the connection.
37
40
  @stream.close
38
-
41
+
39
42
  super
40
43
  end
41
44
 
45
+ # TODO this is a bit less efficient in order to maintain compatibility with `IO`.
42
46
  def read
43
- @stream.read_partial
47
+ @stream.readpartial(BLOCK_SIZE)
48
+ rescue EOFError
49
+ return nil
44
50
  end
45
51
 
46
52
  def call(stream)
@@ -52,7 +58,7 @@ module Protocol
52
58
  end
53
59
 
54
60
  def join
55
- read
61
+ @stream.read
56
62
  end
57
63
 
58
64
  def inspect
@@ -74,7 +74,11 @@ module Protocol
74
74
  end
75
75
  end
76
76
 
77
- def persistent?(version, headers)
77
+ def persistent?(version, method, headers)
78
+ if method == HTTP::Methods::CONNECT
79
+ return false
80
+ end
81
+
78
82
  if version == HTTP10
79
83
  if connection = headers[CONNECTION]
80
84
  return connection.include?(KEEP_ALIVE)
@@ -159,9 +163,9 @@ module Protocol
159
163
 
160
164
  headers = read_headers
161
165
 
162
- @persistent = persistent?(version, headers)
166
+ @persistent = persistent?(version, method, headers)
163
167
 
164
- body = read_request_body(headers)
168
+ body = read_request_body(method, headers)
165
169
 
166
170
  @count += 1
167
171
 
@@ -175,7 +179,7 @@ module Protocol
175
179
 
176
180
  headers = read_headers
177
181
 
178
- @persistent = persistent?(version, headers)
182
+ @persistent = persistent?(version, method, headers)
179
183
 
180
184
  body = read_response_body(method, status, headers)
181
185
 
@@ -208,9 +212,33 @@ module Protocol
208
212
  @stream.write("\r\n")
209
213
  @stream.flush # Don't remove me!
210
214
 
211
- body&.each do |chunk|
212
- @stream.write(chunk)
213
- @stream.flush
215
+ if body
216
+ body.each do |chunk|
217
+ @stream.write(chunk)
218
+ @stream.flush
219
+ end
220
+
221
+ @stream.close_write
222
+ end
223
+
224
+ return @stream
225
+ end
226
+
227
+ def write_tunnel_body(version, body = nil)
228
+ @persistent = false
229
+
230
+ write_connection_header(version)
231
+
232
+ @stream.write("\r\n")
233
+ @stream.flush # Don't remove me!
234
+
235
+ if body
236
+ body.each do |chunk|
237
+ @stream.write(chunk)
238
+ @stream.flush
239
+ end
240
+
241
+ @stream.close_write
214
242
  end
215
243
 
216
244
  return @stream
@@ -341,7 +369,7 @@ module Protocol
341
369
  # code is always terminated by the first empty line after the
342
370
  # header fields, regardless of the header fields present in the
343
371
  # message, and thus cannot contain a message body.
344
- if method == "HEAD" or (status >= 100 and status < 200) or status == 204 or status == 304
372
+ if method == HTTP::Methods::HEAD or (status >= 100 and status < 200) or status == 204 or status == 304
345
373
  return nil
346
374
  end
347
375
 
@@ -350,14 +378,23 @@ module Protocol
350
378
  # line that concludes the header fields. A client MUST ignore any
351
379
  # Content-Length or Transfer-Encoding header fields received in
352
380
  # such a message.
353
- if method == "CONNECT" and status == 200
381
+ if method == HTTP::Methods::CONNECT and status == 200
354
382
  return read_tunnel_body
355
383
  end
356
384
 
357
385
  return read_body(headers, true)
358
386
  end
359
387
 
360
- def read_request_body(headers)
388
+ def read_request_body(method, headers)
389
+ # 2. Any 2xx (Successful) response to a CONNECT request implies that
390
+ # the connection will become a tunnel immediately after the empty
391
+ # line that concludes the header fields. A client MUST ignore any
392
+ # Content-Length or Transfer-Encoding header fields received in
393
+ # such a message.
394
+ if method == HTTP::Methods::CONNECT
395
+ return read_tunnel_body
396
+ end
397
+
361
398
  # 6. If this is a request message and none of the above are true, then
362
399
  # the message body length is zero (no message body is present).
363
400
  return read_body(headers)
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Protocol
22
22
  module HTTP1
23
- VERSION = "0.8.3"
23
+ VERSION = "0.9.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http1
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-10 00:00:00.000000000 Z
11
+ date: 2019-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: protocol-http