async-http 0.57.0 → 0.58.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: 454a678139fa72fafb6fc06cc34af7f21b44bba2a7ebddf7bfc823c01c5b968a
4
- data.tar.gz: d91f30582707db6bd9a717d449c7d8295f8110b315defec4477b892e28678c22
3
+ metadata.gz: 238edc4ed5e4244f2060e9c788aa93588f9b3dd920646c8ec9161512af381e96
4
+ data.tar.gz: c77506488fedd3fec5a20d1172f1a2e6e6285f2c0dccb6a447a217a2e3423bc0
5
5
  SHA512:
6
- metadata.gz: 0517cac618b852dca972400c943b7fc78ee7693ad1555eeb2699d5fc45a6fee0d4ceeb45121168955da3bdd980ebee788f21c51d42e3de5fba03a8896e3fb3fc
7
- data.tar.gz: 321e705b70ab0efb1d36cc8614a6193b593c930badd96f239734685594da3456ee505e9dcc9c7fe28fc4ace349c25e4cff3774b5753d1c692769b21466d80de4
6
+ metadata.gz: 87247dfbc7b31ead28e1fef6f6b532a6db22778eb3c0bde7932cdf6655027309ef1f0f1453b8a72d4ff38aabe3aeb9d8c7c75af28f8d35285bf6644f23655e99
7
+ data.tar.gz: bc199930224287caaea2c2b2c9b7219d81d24872932efa0a85b7bcada7d7c56b8f1da827ce34742db7bfc7193a72148a233cef09a65892d36949c5200f288e0a
checksums.yaml.gz.sig CHANGED
Binary file
@@ -138,7 +138,7 @@ module Async
138
138
  @pool.release(connection) if connection
139
139
  end
140
140
  end
141
-
141
+
142
142
  def inspect
143
143
  "#<#{self.class} authority=#{@authority.inspect}>"
144
144
  end
@@ -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
- protocol = connection.upgrade?(headers)
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 = connection.upgrade?(headers)
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
- return if @stream.nil? or @stream.closed?
65
-
66
- if response
67
- trailer = response.headers.trailer!
68
-
69
- write_response(@version, response.status, response.headers)
70
-
71
- body = response.body
72
-
73
- if body and protocol = response.protocol
74
- stream = write_upgrade_body(protocol)
75
-
76
- # At this point, the request body is hijacked, so we don't want to call #finish below.
77
- request = nil
78
-
79
- # We also don't want to hold on to the response object:
80
- response = nil
81
-
82
- body.call(stream)
83
- elsif request.connect? and response.success?
84
- stream = write_tunnel_body(request.version)
85
-
86
- # Same as above:
87
- request = nil
88
- response = nil
89
-
90
- body.call(stream)
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
- head = request.head?
93
- version = request.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
- else
101
- # If the request failed to generate a response, it was an internal server error:
102
- write_response(@version, 500, {})
103
- write_body(request.version, nil)
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
@@ -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
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Async
24
24
  module HTTP
25
- VERSION = "0.57.0"
25
+ VERSION = "0.58.0"
26
26
  end
27
27
  end
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.57.0
4
+ version: 0.58.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -52,7 +52,7 @@ cert_chain:
52
52
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
53
53
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
54
54
  -----END CERTIFICATE-----
55
- date: 2022-08-07 00:00:00.000000000 Z
55
+ date: 2022-08-13 00:00:00.000000000 Z
56
56
  dependencies:
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: async
@@ -142,14 +142,14 @@ dependencies:
142
142
  name: traces
143
143
  requirement: !ruby/object:Gem::Requirement
144
144
  requirements:
145
- - - "~>"
145
+ - - ">="
146
146
  - !ruby/object:Gem::Version
147
147
  version: 0.4.0
148
148
  type: :runtime
149
149
  prerelease: false
150
150
  version_requirements: !ruby/object:Gem::Requirement
151
151
  requirements:
152
- - - "~>"
152
+ - - ">="
153
153
  - !ruby/object:Gem::Version
154
154
  version: 0.4.0
155
155
  - !ruby/object:Gem::Dependency
metadata.gz.sig CHANGED
Binary file