async-http 0.56.6 → 0.59.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/async/http/body/hijack.rb +10 -12
- data/lib/async/http/client.rb +1 -1
- data/lib/async/http/endpoint.rb +3 -3
- data/lib/async/http/internet.rb +8 -0
- data/lib/async/http/protocol/http1/request.rb +4 -1
- data/lib/async/http/protocol/http1/response.rb +3 -1
- data/lib/async/http/protocol/http1/server.rb +50 -45
- data/lib/async/http/protocol/http2/output.rb +2 -2
- data/lib/async/http/protocol/http2.rb +1 -1
- data/lib/async/http/server.rb +1 -1
- data/lib/async/http/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +32 -32
- metadata.gz.sig +0 -0
- data/lib/async/http/body/stream.rb +0 -172
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 462c9c83407e01dff971e0642b8bb589da83d6ae533ba32d932c1d384e92d26b
|
4
|
+
data.tar.gz: 34b7f23b1d0e37e67e624465f9d444713cc7bb382fa74a4c72114a266a88b409
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81720dc20d8807e6e46c71206191a737e98c09a5df08b6e9cedada59f2333c8498a4e9bfaefe4cef73992d5c350dd0ec0fc6f70d8e4b26e1f013e88b5c8aabb0
|
7
|
+
data.tar.gz: fde249eaa964c5aea2dea86ff147053bb7981248302349c4caed80092c5abeecd27b84ddc4bfdf6625a4db5a328d770addef765a40bb7734b9314656d49dd5e9
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -21,7 +21,9 @@
|
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
23
|
require 'protocol/http/body/readable'
|
24
|
-
|
24
|
+
require 'protocol/http/body/stream'
|
25
|
+
|
26
|
+
require_relative 'writable'
|
25
27
|
|
26
28
|
module Async
|
27
29
|
module HTTP
|
@@ -42,6 +44,7 @@ module Async
|
|
42
44
|
|
43
45
|
@task = nil
|
44
46
|
@stream = nil
|
47
|
+
@output = nil
|
45
48
|
end
|
46
49
|
|
47
50
|
# We prefer streaming directly as it's the lowest overhead.
|
@@ -57,23 +60,18 @@ module Async
|
|
57
60
|
|
58
61
|
# Has the producer called #finish and has the reader consumed the nil token?
|
59
62
|
def empty?
|
60
|
-
|
61
|
-
@stream.empty?
|
62
|
-
else
|
63
|
-
false
|
64
|
-
end
|
63
|
+
@output&.empty?
|
65
64
|
end
|
66
65
|
|
67
66
|
def ready?
|
68
|
-
|
69
|
-
@stream.output.ready?
|
70
|
-
end
|
67
|
+
@output&.ready?
|
71
68
|
end
|
72
69
|
|
73
70
|
# Read the next available chunk.
|
74
71
|
def read
|
75
|
-
unless @
|
76
|
-
@
|
72
|
+
unless @output
|
73
|
+
@output = Writable.new
|
74
|
+
@stream = ::Protocol::HTTP::Body::Stream.new(@input, @output)
|
77
75
|
|
78
76
|
@task = Task.current.async do |task|
|
79
77
|
task.annotate "Streaming hijacked body."
|
@@ -82,7 +80,7 @@ module Async
|
|
82
80
|
end
|
83
81
|
end
|
84
82
|
|
85
|
-
return @
|
83
|
+
return @output.read
|
86
84
|
end
|
87
85
|
|
88
86
|
def inspect
|
data/lib/async/http/client.rb
CHANGED
data/lib/async/http/endpoint.rb
CHANGED
@@ -37,13 +37,13 @@ module Async
|
|
37
37
|
return self.new(url, endpoint, **options)
|
38
38
|
end
|
39
39
|
|
40
|
-
# Construct an endpoint with a specified scheme, hostname, and options.
|
41
|
-
def self.for(scheme, hostname, **options)
|
40
|
+
# Construct an endpoint with a specified scheme, hostname, optional path, and options.
|
41
|
+
def self.for(scheme, hostname, path = "/", **options)
|
42
42
|
# TODO: Consider using URI.for once it becomes available:
|
43
43
|
uri_klass = URI.scheme_list[scheme.upcase] || URI::HTTP
|
44
44
|
|
45
45
|
self.new(
|
46
|
-
uri_klass.new(scheme, nil, hostname, nil, nil,
|
46
|
+
uri_klass.new(scheme, nil, hostname, nil, nil, path, nil, nil, nil).normalize,
|
47
47
|
**options
|
48
48
|
)
|
49
49
|
end
|
data/lib/async/http/internet.rb
CHANGED
@@ -47,6 +47,14 @@ module Async
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
# Make a request to the internet with the given `method` and `url`.
|
51
|
+
#
|
52
|
+
# If you provide non-frozen headers, they may be mutated.
|
53
|
+
#
|
54
|
+
# @parameter method [String] The request method, e.g. `GET`.
|
55
|
+
# @parameter url [String] The URL to request, e.g. `https://www.codeotaku.com`.
|
56
|
+
# @parameter headers [Hash | Protocol::HTTP::Headers] The headers to send with the request.
|
57
|
+
# @parameter body [String | Protocol::HTTP::Body] The body to send with the request.
|
50
58
|
def call(method, url, headers = nil, body = nil)
|
51
59
|
endpoint = Endpoint.parse(url)
|
52
60
|
client = self.client_for(endpoint)
|
@@ -32,11 +32,14 @@ module Async
|
|
32
32
|
self.new(connection, *parts)
|
33
33
|
end
|
34
34
|
end
|
35
|
+
|
36
|
+
UPGRADE = 'upgrade'
|
35
37
|
|
36
38
|
def initialize(connection, authority, method, path, version, headers, body)
|
37
39
|
@connection = connection
|
38
40
|
|
39
|
-
|
41
|
+
# HTTP/1 requests with an upgrade header (which can contain zero or more values) are extracted into the protocol field of the request, and we expect a response to select one of those protocols with a status code of 101 Switching Protocols.
|
42
|
+
protocol = headers.delete('upgrade')
|
40
43
|
|
41
44
|
super(nil, authority, method, path, version, headers, body, protocol)
|
42
45
|
end
|
@@ -33,11 +33,13 @@ module Async
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
UPGRADE = 'upgrade'
|
37
|
+
|
36
38
|
# @param reason [String] HTTP response line reason, ignored.
|
37
39
|
def initialize(connection, version, status, reason, headers, body)
|
38
40
|
@connection = connection
|
39
41
|
|
40
|
-
protocol =
|
42
|
+
protocol = headers.delete(UPGRADE)
|
41
43
|
|
42
44
|
super(version, status, headers, body, protocol)
|
43
45
|
end
|
@@ -60,54 +60,59 @@ module Async
|
|
60
60
|
|
61
61
|
while request = next_request
|
62
62
|
response = yield(request, self)
|
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
|
-
|
63
|
+
body = response&.body
|
64
|
+
|
65
|
+
begin
|
66
|
+
# If a response was generated, send it:
|
67
|
+
if response
|
68
|
+
trailer = response.headers.trailer!
|
69
|
+
|
70
|
+
write_response(@version, response.status, response.headers)
|
71
|
+
|
72
|
+
# Some operations in this method are long running, that is, it's expected that `body.call(stream)` could literally run indefinitely. In order to facilitate garbage collection, we want to nullify as many local variables before calling the streaming body. This ensures that the garbage collection can clean up as much state as possible during the long running operation, so we don't retain objects that are no longer needed.
|
73
|
+
|
74
|
+
if body and protocol = response.protocol
|
75
|
+
stream = write_upgrade_body(protocol)
|
76
|
+
|
77
|
+
# At this point, the request body is hijacked, so we don't want to call #finish below.
|
78
|
+
request = response = nil
|
79
|
+
|
80
|
+
body.call(stream)
|
81
|
+
elsif request.connect? and response.success?
|
82
|
+
stream = write_tunnel_body(request.version)
|
83
|
+
|
84
|
+
# Same as above:
|
85
|
+
request = response = nil
|
86
|
+
|
87
|
+
body.call(stream)
|
88
|
+
else
|
89
|
+
head = request.head?
|
90
|
+
version = request.version
|
91
|
+
|
92
|
+
# Same as above:
|
93
|
+
request = nil unless body
|
94
|
+
response = nil
|
95
|
+
|
96
|
+
write_body(version, body, head, trailer)
|
97
|
+
end
|
98
|
+
|
99
|
+
# We are done with the body, you shouldn't need to call close on it:
|
100
|
+
body = nil
|
91
101
|
else
|
92
|
-
|
93
|
-
version
|
94
|
-
|
95
|
-
request = nil unless body
|
96
|
-
response = nil
|
97
|
-
|
98
|
-
write_body(version, body, head, trailer)
|
102
|
+
# If the request failed to generate a response, it was an internal server error:
|
103
|
+
write_response(@version, 500, {})
|
104
|
+
write_body(request.version, nil)
|
99
105
|
end
|
100
|
-
|
101
|
-
#
|
102
|
-
|
103
|
-
|
106
|
+
|
107
|
+
# Gracefully finish reading the request body if it was not already done so.
|
108
|
+
request&.finish
|
109
|
+
|
110
|
+
# This ensures we yield at least once every iteration of the loop and allow other fibers to execute.
|
111
|
+
task.yield
|
112
|
+
rescue => error
|
113
|
+
ensure
|
114
|
+
body&.close(error)
|
104
115
|
end
|
105
|
-
|
106
|
-
# Gracefully finish reading the request body if it was not already done so.
|
107
|
-
request&.finish
|
108
|
-
|
109
|
-
# This ensures we yield at least once every iteration of the loop and allow other fibers to execute.
|
110
|
-
task.yield
|
111
116
|
end
|
112
117
|
end
|
113
118
|
end
|
@@ -20,7 +20,7 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
|
-
|
23
|
+
require 'protocol/http/body/stream'
|
24
24
|
|
25
25
|
module Async
|
26
26
|
module HTTP
|
@@ -88,7 +88,7 @@ module Async
|
|
88
88
|
|
89
89
|
input = @stream.wait_for_input
|
90
90
|
|
91
|
-
@body.call(Body::Stream.new(input, self))
|
91
|
+
@body.call(::Protocol::HTTP::Body::Stream.new(input, self))
|
92
92
|
rescue Async::Stop
|
93
93
|
# Ignore.
|
94
94
|
end
|
data/lib/async/http/server.rb
CHANGED
@@ -96,7 +96,7 @@ module Async
|
|
96
96
|
attributes['http.protocol'] = protocol
|
97
97
|
end
|
98
98
|
|
99
|
-
trace('async.http.server.call', attributes: attributes) do |span|
|
99
|
+
trace('async.http.server.call', resource: "#{request.method} #{request.path}", attributes: attributes) do |span|
|
100
100
|
super.tap do |response|
|
101
101
|
if status = response&.status
|
102
102
|
span['http.status_code'] = status
|
data/lib/async/http/version.rb
CHANGED
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.59.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -25,33 +25,34 @@ bindir: bin
|
|
25
25
|
cert_chain:
|
26
26
|
- |
|
27
27
|
-----BEGIN CERTIFICATE-----
|
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
|
-
|
28
|
+
MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
|
29
|
+
ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
|
30
|
+
CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
|
31
|
+
MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
|
32
|
+
MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
|
33
|
+
bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
|
34
|
+
igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
|
35
|
+
9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW
|
36
|
+
sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE
|
37
|
+
e5AUKRwUuvaUaumAKgPH+4E4oiLXI4T1Ff5Q7xxv6yXvHuYtlMHhYfgNn8iiW8WN
|
38
|
+
XibYXPNP7NtieSQqwR/xM6IRSoyXKuS+ZNGDPUUGk8RoiV/xvVN4LrVm9upSc0ss
|
39
|
+
RZ6qwOQmXCo/lLcDUxJAgG95cPw//sI00tZan75VgsGzSWAOdjQpFM0l4dxvKwHn
|
40
|
+
tUeT3ZsAgt0JnGqNm2Bkz81kG4A2hSyFZTFA8vZGhp+hz+8Q573tAR89y9YJBdYM
|
41
|
+
zp0FM4zwMNEUwgfRzv1tEVVUEXmoFCyhzonUUw4nE4CFu/sE3ffhjKcXcY//qiSW
|
42
|
+
xm4erY3XAgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
|
43
|
+
BBYEFO9t7XWuFf2SKLmuijgqR4sGDlRsMC4GA1UdEQQnMCWBI3NhbXVlbC53aWxs
|
44
|
+
aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWBI3NhbXVlbC53aWxs
|
45
|
+
aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEBCwUAA4IBgQB5sxkE
|
46
|
+
cBsSYwK6fYpM+hA5B5yZY2+L0Z+27jF1pWGgbhPH8/FjjBLVn+VFok3CDpRqwXCl
|
47
|
+
xCO40JEkKdznNy2avOMra6PFiQyOE74kCtv7P+Fdc+FhgqI5lMon6tt9rNeXmnW/
|
48
|
+
c1NaMRdxy999hmRGzUSFjozcCwxpy/LwabxtdXwXgSay4mQ32EDjqR1TixS1+smp
|
49
|
+
8C/NCWgpIfzpHGJsjvmH2wAfKtTTqB9CVKLCWEnCHyCaRVuKkrKjqhYCdmMBqCws
|
50
|
+
JkxfQWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP
|
51
|
+
eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt
|
52
|
+
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
53
|
+
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
53
54
|
-----END CERTIFICATE-----
|
54
|
-
date: 2022-
|
55
|
+
date: 2022-08-16 00:00:00.000000000 Z
|
55
56
|
dependencies:
|
56
57
|
- !ruby/object:Gem::Dependency
|
57
58
|
name: async
|
@@ -101,14 +102,14 @@ dependencies:
|
|
101
102
|
requirements:
|
102
103
|
- - "~>"
|
103
104
|
- !ruby/object:Gem::Version
|
104
|
-
version: 0.
|
105
|
+
version: 0.23.1
|
105
106
|
type: :runtime
|
106
107
|
prerelease: false
|
107
108
|
version_requirements: !ruby/object:Gem::Requirement
|
108
109
|
requirements:
|
109
110
|
- - "~>"
|
110
111
|
- !ruby/object:Gem::Version
|
111
|
-
version: 0.
|
112
|
+
version: 0.23.1
|
112
113
|
- !ruby/object:Gem::Dependency
|
113
114
|
name: protocol-http1
|
114
115
|
requirement: !ruby/object:Gem::Requirement
|
@@ -141,14 +142,14 @@ dependencies:
|
|
141
142
|
name: traces
|
142
143
|
requirement: !ruby/object:Gem::Requirement
|
143
144
|
requirements:
|
144
|
-
- - "
|
145
|
+
- - ">="
|
145
146
|
- !ruby/object:Gem::Version
|
146
147
|
version: 0.4.0
|
147
148
|
type: :runtime
|
148
149
|
prerelease: false
|
149
150
|
version_requirements: !ruby/object:Gem::Requirement
|
150
151
|
requirements:
|
151
|
-
- - "
|
152
|
+
- - ">="
|
152
153
|
- !ruby/object:Gem::Version
|
153
154
|
version: 0.4.0
|
154
155
|
- !ruby/object:Gem::Dependency
|
@@ -249,7 +250,6 @@ files:
|
|
249
250
|
- lib/async/http/body/hijack.rb
|
250
251
|
- lib/async/http/body/pipe.rb
|
251
252
|
- lib/async/http/body/slowloris.rb
|
252
|
-
- lib/async/http/body/stream.rb
|
253
253
|
- lib/async/http/body/writable.rb
|
254
254
|
- lib/async/http/client.rb
|
255
255
|
- lib/async/http/endpoint.rb
|
metadata.gz.sig
CHANGED
Binary file
|
@@ -1,172 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
#
|
3
|
-
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require_relative 'writable'
|
24
|
-
|
25
|
-
module Async
|
26
|
-
module HTTP
|
27
|
-
module Body
|
28
|
-
# The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.
|
29
|
-
class Stream
|
30
|
-
def initialize(input, output = Writable.new)
|
31
|
-
@input = input
|
32
|
-
@output = output
|
33
|
-
|
34
|
-
raise ArgumentError, "Non-writable output!" unless output.respond_to?(:write)
|
35
|
-
|
36
|
-
# Will hold remaining data in `#read`.
|
37
|
-
@buffer = nil
|
38
|
-
@closed = false
|
39
|
-
end
|
40
|
-
|
41
|
-
attr :input
|
42
|
-
attr :output
|
43
|
-
|
44
|
-
# rack.hijack_io must respond to:
|
45
|
-
# read, write, read_nonblock, write_nonblock, flush, close, close_read, close_write, closed?
|
46
|
-
|
47
|
-
# read behaves like IO#read. Its signature is read([length, [buffer]]). If given, length must be a non-negative Integer (>= 0) or nil, and buffer must be a String and may not be nil. If length is given and not nil, then this method reads at most length bytes from the input stream. If length is not given or nil, then this method reads all data until EOF. When EOF is reached, this method returns nil if length is given and not nil, or “” if length is not given or is nil. If buffer is given, then the read data will be placed into buffer instead of a newly created String object.
|
48
|
-
# @param length [Integer] the amount of data to read
|
49
|
-
# @param buffer [String] the buffer which will receive the data
|
50
|
-
# @return a buffer containing the data
|
51
|
-
def read(size = nil, buffer = nil)
|
52
|
-
return '' if size == 0
|
53
|
-
|
54
|
-
buffer ||= Async::IO::Buffer.new
|
55
|
-
if @buffer
|
56
|
-
buffer.replace(@buffer)
|
57
|
-
@buffer = nil
|
58
|
-
end
|
59
|
-
|
60
|
-
if size
|
61
|
-
while buffer.bytesize < size and chunk = read_next
|
62
|
-
buffer << chunk
|
63
|
-
end
|
64
|
-
|
65
|
-
@buffer = buffer.byteslice(size, buffer.bytesize)
|
66
|
-
buffer = buffer.byteslice(0, size)
|
67
|
-
|
68
|
-
if buffer.empty?
|
69
|
-
return nil
|
70
|
-
else
|
71
|
-
return buffer
|
72
|
-
end
|
73
|
-
else
|
74
|
-
while chunk = read_next
|
75
|
-
buffer << chunk
|
76
|
-
end
|
77
|
-
|
78
|
-
return buffer
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# Read at most `size` bytes from the stream. Will avoid reading from the underlying stream if possible.
|
83
|
-
def read_partial(size = nil)
|
84
|
-
if @buffer
|
85
|
-
buffer = @buffer
|
86
|
-
@buffer = nil
|
87
|
-
else
|
88
|
-
buffer = read_next
|
89
|
-
end
|
90
|
-
|
91
|
-
if buffer and size
|
92
|
-
if buffer.bytesize > size
|
93
|
-
@buffer = buffer.byteslice(size, buffer.bytesize)
|
94
|
-
buffer = buffer.byteslice(0, size)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
return buffer
|
99
|
-
end
|
100
|
-
|
101
|
-
def read_nonblock(length, buffer = nil)
|
102
|
-
@buffer ||= read_next
|
103
|
-
chunk = nil
|
104
|
-
|
105
|
-
return nil if @buffer.nil?
|
106
|
-
|
107
|
-
if @buffer.bytesize > length
|
108
|
-
chunk = @buffer.byteslice(0, length)
|
109
|
-
@buffer = @buffer.byteslice(length, @buffer.bytesize)
|
110
|
-
else
|
111
|
-
chunk = @buffer
|
112
|
-
@buffer = nil
|
113
|
-
end
|
114
|
-
|
115
|
-
if buffer
|
116
|
-
buffer.replace(chunk)
|
117
|
-
else
|
118
|
-
buffer = chunk
|
119
|
-
end
|
120
|
-
|
121
|
-
return buffer
|
122
|
-
end
|
123
|
-
|
124
|
-
def write(buffer)
|
125
|
-
@output.write(buffer)
|
126
|
-
end
|
127
|
-
|
128
|
-
alias write_nonblock write
|
129
|
-
|
130
|
-
def flush
|
131
|
-
end
|
132
|
-
|
133
|
-
def close_read
|
134
|
-
@input&.close
|
135
|
-
end
|
136
|
-
|
137
|
-
def close_write
|
138
|
-
@output&.close
|
139
|
-
end
|
140
|
-
|
141
|
-
# Close the input and output bodies.
|
142
|
-
def close(error = nil)
|
143
|
-
self.close_read
|
144
|
-
self.close_write
|
145
|
-
ensure
|
146
|
-
@closed = true
|
147
|
-
end
|
148
|
-
|
149
|
-
# Whether the stream has been closed.
|
150
|
-
def closed?
|
151
|
-
@closed
|
152
|
-
end
|
153
|
-
|
154
|
-
# Whether there are any output chunks remaining?
|
155
|
-
def empty?
|
156
|
-
@output.empty?
|
157
|
-
end
|
158
|
-
|
159
|
-
private
|
160
|
-
|
161
|
-
def read_next
|
162
|
-
if chunk = @input&.read
|
163
|
-
return chunk
|
164
|
-
else
|
165
|
-
@input = nil
|
166
|
-
return nil
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|