async-http 0.56.4 → 0.57.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: caff6f0e39afa2ec8e8eaff86fc960121968f136fd23bee8e4c4344e1da55c36
4
- data.tar.gz: 8428ae61665e4bc9275cf1a127006c9c975cc184267a1ac0a35b9b7df80d608f
3
+ metadata.gz: 454a678139fa72fafb6fc06cc34af7f21b44bba2a7ebddf7bfc823c01c5b968a
4
+ data.tar.gz: d91f30582707db6bd9a717d449c7d8295f8110b315defec4477b892e28678c22
5
5
  SHA512:
6
- metadata.gz: 761f315b4fc933cb92d80eb9000e2f5dd2c796bc9e119265ee452f27f0edabc071766e4d48e35dd1866661d315166b7e32fb509348ec5c9cb43c38c5d1b4586f
7
- data.tar.gz: 0b229bf87de21d7cc05de022283901f75114bf37acc240e7992430c5e47602ab04f8b9aa76884b30733f55a1b52b0d3ae51b9dc0e25ea1a653dfda680e77a1ce
6
+ metadata.gz: 0517cac618b852dca972400c943b7fc78ee7693ad1555eeb2699d5fc45a6fee0d4ceeb45121168955da3bdd980ebee788f21c51d42e3de5fba03a8896e3fb3fc
7
+ data.tar.gz: 321e705b70ab0efb1d36cc8614a6193b593c930badd96f239734685594da3456ee505e9dcc9c7fe28fc4ace349c25e4cff3774b5753d1c692769b21466d80de4
checksums.yaml.gz.sig ADDED
Binary file
@@ -21,7 +21,9 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'protocol/http/body/readable'
24
- require_relative 'stream'
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
- if @stream
61
- @stream.empty?
62
- else
63
- false
64
- end
63
+ @output&.empty?
65
64
  end
66
65
 
67
66
  def ready?
68
- if @stream
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 @task
76
- @stream = Stream.new(@input)
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 @stream.output.read
83
+ return @output.read
86
84
  end
87
85
 
88
86
  def inspect
@@ -28,6 +28,8 @@ require 'async/pool/controller'
28
28
  require 'protocol/http/body/completable'
29
29
  require 'protocol/http/methods'
30
30
 
31
+ require 'traces/provider'
32
+
31
33
  require_relative 'protocol'
32
34
 
33
35
  module Async
@@ -136,6 +138,46 @@ module Async
136
138
  @pool.release(connection) if connection
137
139
  end
138
140
  end
141
+
142
+ def inspect
143
+ "#<#{self.class} authority=#{@authority.inspect}>"
144
+ end
145
+
146
+ Traces::Provider(self) do
147
+ def call(request)
148
+ attributes = {
149
+ 'http.method': request.method,
150
+ 'http.authority': request.authority || self.authority,
151
+ 'http.scheme': request.scheme || self.scheme,
152
+ 'http.path': request.path,
153
+ }
154
+
155
+ if protocol = request.protocol
156
+ attributes['http.protocol'] = protocol
157
+ end
158
+
159
+ if length = request.body&.length
160
+ attributes['http.request.length'] = length
161
+ end
162
+
163
+ trace('async.http.client.call', attributes: attributes) do |span|
164
+ if context = trace_context(span)
165
+ request.headers['traceparent'] = context.to_s
166
+ # request.headers['tracestate'] = context.state
167
+ end
168
+
169
+ super.tap do |response|
170
+ if status = response&.status
171
+ span['http.status_code'] = status
172
+ end
173
+
174
+ if length = response.body&.length
175
+ span['http.response.length'] = length
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
139
181
 
140
182
  protected
141
183
 
@@ -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, nil, nil, nil, nil),
46
+ uri_klass.new(scheme, nil, hostname, nil, nil, path, nil, nil, nil).normalize,
47
47
  **options
48
48
  )
49
49
  end
@@ -241,4 +241,4 @@ module Async
241
241
  end
242
242
  end
243
243
  end
244
- end
244
+ end
@@ -34,6 +34,7 @@ module Async
34
34
 
35
35
  Console.logger.debug(self) {"#{request.method} #{request.path} #{request.headers.inspect}"}
36
36
 
37
+ # Mark the start of the trailers:
37
38
  trailer = request.headers.trailer!
38
39
 
39
40
  # We carefully interpret https://tools.ietf.org/html/rfc7230#section-6.3.1 to implement this correctly.
@@ -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
- require_relative '../../body/stream'
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
@@ -34,7 +34,6 @@ module Async
34
34
  super
35
35
 
36
36
  @headers = nil
37
- @trailer = nil
38
37
 
39
38
  # Input buffer, reading request body, or response body (receive_data):
40
39
  @length = nil
@@ -62,11 +61,7 @@ module Async
62
61
 
63
62
  def receive_trailing_headers(headers, end_stream)
64
63
  headers.each do |key, value|
65
- if @trailer.include?(key)
66
- add_header(key, value)
67
- else
68
- raise ::Protocol::HTTP2::HeaderError, "Cannot add trailer #{key} as it was not specified as a trailer!"
69
- end
64
+ add_header(key, value)
70
65
  end
71
66
  end
72
67
 
@@ -74,9 +69,7 @@ module Async
74
69
  if @headers.nil?
75
70
  @headers = ::Protocol::HTTP::Headers.new
76
71
  self.receive_initial_headers(super, frame.end_stream?)
77
-
78
- @trailer = @headers[TRAILER]
79
- elsif @trailer and frame.end_stream?
72
+ elsif frame.end_stream?
80
73
  self.receive_trailing_headers(super, frame.end_stream?)
81
74
  else
82
75
  raise ::Protocol::HTTP2::HeaderError, "Unable to process headers!"
@@ -152,7 +145,7 @@ module Async
152
145
  send_reset_stream(::Protocol::HTTP2::Error::INTERNAL_ERROR)
153
146
  else
154
147
  # Write trailer?
155
- if trailer
148
+ if trailer&.any?
156
149
  send_headers(nil, trailer, ::Protocol::HTTP2::END_STREAM)
157
150
  else
158
151
  send_data(nil, ::Protocol::HTTP2::END_STREAM)
@@ -28,11 +28,11 @@ module Async
28
28
  module HTTP
29
29
  class TooManyRedirects < StandardError
30
30
  end
31
-
31
+
32
32
  # A client wrapper which transparently handles both relative and absolute redirects to a given maximum number of hops.
33
33
  class RelativeLocation < ::Protocol::HTTP::Middleware
34
34
  DEFAULT_METHOD = GET
35
-
35
+
36
36
  # maximum_hops is the max number of redirects. Set to 0 to allow 1 request with no redirects.
37
37
  def initialize(app, maximum_hops = 3)
38
38
  super(app)
@@ -51,7 +51,7 @@ module Async
51
51
 
52
52
  while hops <= @maximum_hops
53
53
  response = super(request)
54
-
54
+
55
55
  if response.redirection?
56
56
  hops += 1
57
57
  response.finish
@@ -23,9 +23,12 @@
23
23
  require 'async/io/endpoint'
24
24
  require 'async/io/stream'
25
25
 
26
- require_relative 'protocol'
27
26
  require 'protocol/http/middleware'
28
27
 
28
+ require 'traces/provider'
29
+
30
+ require_relative 'protocol'
31
+
29
32
  module Async
30
33
  module HTTP
31
34
  class Server < ::Protocol::HTTP::Middleware
@@ -70,6 +73,42 @@ module Async
70
73
  def run
71
74
  @endpoint.accept(&self.method(:accept))
72
75
  end
76
+
77
+ Traces::Provider(self) do
78
+ def call(request)
79
+ if trace_parent = request.headers['traceparent']
80
+ self.trace_context = Traces::Context.parse(trace_parent.join, request.headers['tracestate'], remote: true)
81
+ end
82
+
83
+ attributes = {
84
+ 'http.method': request.method,
85
+ 'http.authority': request.authority,
86
+ 'http.scheme': request.scheme,
87
+ 'http.path': request.path,
88
+ 'http.user_agent': request.headers['user-agent'],
89
+ }
90
+
91
+ if length = request.body&.length
92
+ attributes['http.request.length'] = length
93
+ end
94
+
95
+ if protocol = request.protocol
96
+ attributes['http.protocol'] = protocol
97
+ end
98
+
99
+ trace('async.http.server.call', attributes: attributes) do |span|
100
+ super.tap do |response|
101
+ if status = response&.status
102
+ span['http.status_code'] = status
103
+ end
104
+
105
+ if length = response&.body&.length
106
+ span['http.response.length'] = length
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
73
112
  end
74
113
  end
75
114
  end
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Async
24
24
  module HTTP
25
- VERSION = "0.56.4"
25
+ VERSION = "0.57.0"
26
26
  end
27
27
  end
data.tar.gz.sig ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.56.4
4
+ version: 0.57.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
+ - Brian Morearty
9
+ - Bruno Sutic
10
+ - Janko Marohnić
11
+ - Adam Daniels
12
+ - Cyril Roelandt
13
+ - Denis Talakevich
14
+ - Ian Ker-Seymer
15
+ - Igor Sidorov
16
+ - Marco Concetto Rudilosso
17
+ - Olle Jonsson
18
+ - Orgad Shaneh
19
+ - Stefan Wrobel
20
+ - TheAthlete
21
+ - Trevor Turk
22
+ - samshadwell
8
23
  autorequire:
9
24
  bindir: bin
10
- cert_chain: []
11
- date: 2021-07-16 00:00:00.000000000 Z
25
+ cert_chain:
26
+ - |
27
+ -----BEGIN CERTIFICATE-----
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=
54
+ -----END CERTIFICATE-----
55
+ date: 2022-08-07 00:00:00.000000000 Z
12
56
  dependencies:
13
57
  - !ruby/object:Gem::Dependency
14
58
  name: async
@@ -28,28 +72,28 @@ dependencies:
28
72
  name: async-io
29
73
  requirement: !ruby/object:Gem::Requirement
30
74
  requirements:
31
- - - "~>"
75
+ - - ">="
32
76
  - !ruby/object:Gem::Version
33
77
  version: '1.28'
34
78
  type: :runtime
35
79
  prerelease: false
36
80
  version_requirements: !ruby/object:Gem::Requirement
37
81
  requirements:
38
- - - "~>"
82
+ - - ">="
39
83
  - !ruby/object:Gem::Version
40
84
  version: '1.28'
41
85
  - !ruby/object:Gem::Dependency
42
86
  name: async-pool
43
87
  requirement: !ruby/object:Gem::Requirement
44
88
  requirements:
45
- - - "~>"
89
+ - - ">="
46
90
  - !ruby/object:Gem::Version
47
91
  version: '0.2'
48
92
  type: :runtime
49
93
  prerelease: false
50
94
  version_requirements: !ruby/object:Gem::Requirement
51
95
  requirements:
52
- - - "~>"
96
+ - - ">="
53
97
  - !ruby/object:Gem::Version
54
98
  version: '0.2'
55
99
  - !ruby/object:Gem::Dependency
@@ -58,14 +102,14 @@ dependencies:
58
102
  requirements:
59
103
  - - "~>"
60
104
  - !ruby/object:Gem::Version
61
- version: 0.22.0
105
+ version: 0.23.1
62
106
  type: :runtime
63
107
  prerelease: false
64
108
  version_requirements: !ruby/object:Gem::Requirement
65
109
  requirements:
66
110
  - - "~>"
67
111
  - !ruby/object:Gem::Version
68
- version: 0.22.0
112
+ version: 0.23.1
69
113
  - !ruby/object:Gem::Dependency
70
114
  name: protocol-http1
71
115
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +138,20 @@ dependencies:
94
138
  - - "~>"
95
139
  - !ruby/object:Gem::Version
96
140
  version: 0.14.0
141
+ - !ruby/object:Gem::Dependency
142
+ name: traces
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: 0.4.0
148
+ type: :runtime
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - "~>"
153
+ - !ruby/object:Gem::Version
154
+ version: 0.4.0
97
155
  - !ruby/object:Gem::Dependency
98
156
  name: async-container
99
157
  requirement: !ruby/object:Gem::Requirement
@@ -137,7 +195,7 @@ dependencies:
137
195
  - !ruby/object:Gem::Version
138
196
  version: '0'
139
197
  - !ruby/object:Gem::Dependency
140
- name: rack-test
198
+ name: localhost
141
199
  requirement: !ruby/object:Gem::Requirement
142
200
  requirements:
143
201
  - - ">="
@@ -151,33 +209,33 @@ dependencies:
151
209
  - !ruby/object:Gem::Version
152
210
  version: '0'
153
211
  - !ruby/object:Gem::Dependency
154
- name: rspec
212
+ name: rack-test
155
213
  requirement: !ruby/object:Gem::Requirement
156
214
  requirements:
157
- - - "~>"
215
+ - - ">="
158
216
  - !ruby/object:Gem::Version
159
- version: '3.6'
217
+ version: '0'
160
218
  type: :development
161
219
  prerelease: false
162
220
  version_requirements: !ruby/object:Gem::Requirement
163
221
  requirements:
164
- - - "~>"
222
+ - - ">="
165
223
  - !ruby/object:Gem::Version
166
- version: '3.6'
224
+ version: '0'
167
225
  - !ruby/object:Gem::Dependency
168
- name: localhost
226
+ name: rspec
169
227
  requirement: !ruby/object:Gem::Requirement
170
228
  requirements:
171
- - - ">="
229
+ - - "~>"
172
230
  - !ruby/object:Gem::Version
173
- version: '0'
231
+ version: '3.6'
174
232
  type: :development
175
233
  prerelease: false
176
234
  version_requirements: !ruby/object:Gem::Requirement
177
235
  requirements:
178
- - - ">="
236
+ - - "~>"
179
237
  - !ruby/object:Gem::Version
180
- version: '0'
238
+ version: '3.6'
181
239
  description:
182
240
  email:
183
241
  executables: []
@@ -192,7 +250,6 @@ files:
192
250
  - lib/async/http/body/hijack.rb
193
251
  - lib/async/http/body/pipe.rb
194
252
  - lib/async/http/body/slowloris.rb
195
- - lib/async/http/body/stream.rb
196
253
  - lib/async/http/body/writable.rb
197
254
  - lib/async/http/client.rb
198
255
  - lib/async/http/endpoint.rb
@@ -244,7 +301,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
301
  - !ruby/object:Gem::Version
245
302
  version: '0'
246
303
  requirements: []
247
- rubygems_version: 3.2.22
304
+ rubygems_version: 3.3.7
248
305
  signing_key:
249
306
  specification_version: 4
250
307
  summary: A HTTP client and server library.
metadata.gz.sig ADDED
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