async-websocket 0.18.0 → 0.19.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: 54e97832ebcebab09dfd5a042aad8532a7a5277cf6609fecae9f73a1826cac7d
4
- data.tar.gz: 671da6e463ced51447b8cb49aabbb8b51d644a19adc655e9f955fe4b6772afd4
3
+ metadata.gz: 0fd37d5eb2b762f6f5d7bb8027d3d51c1a9300f63d6d38b5bd7aec5a373d1a0d
4
+ data.tar.gz: 0c8cf165cf58f920ac80534b08fef3146e08caa3676542bea72b6ae2b2fdbe9c
5
5
  SHA512:
6
- metadata.gz: c33efe4ce32ddf75596defe2b3c3f56bb05973d1c13e4b9c39e9fbaed604c9774271b6dd88db11691ba1a498b3c3ee807e6aa3a54447a30ea3ba791d8818388b
7
- data.tar.gz: 50b6419551ee9643771cb80dbd7ad48460e265f3e8a168e60f4afa9557802eb4bc2543b5cb678e55b81ccf006679bfa865047e590f5f7b99526f0dd628bc3005
6
+ metadata.gz: e05aaeb2c9f4ce782bf99c57789d0e21d89a6fc262eae1141451c40c2d17776920117deae3f7c2864218a90ec5a5f7feb5ff4604460cb1cb5ee2ab783fac230d
7
+ data.tar.gz: 919c2aa1bafd05fe0e2283d201553fd546f9e734f7f4134c9adfd62ea9fb6ac714eace090d487b22fd3613dcd52d64eed6a225143a56687f9cecddea973e84db
@@ -0,0 +1,64 @@
1
+ # frozen_string_literals: 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 '../connection'
24
+ require_relative '../response'
25
+
26
+ module Async
27
+ module WebSocket
28
+ module Adapters
29
+ module HTTP
30
+ include ::Protocol::WebSocket::Headers
31
+
32
+ def self.websocket?(request)
33
+ Array(request.protocol).include?(PROTOCOL)
34
+ end
35
+
36
+ def self.open(request, headers: [], protocols: [], handler: Connection, **options, &block)
37
+ if Array(request.protocol).include?(PROTOCOL)
38
+ # Select websocket sub-protocol:
39
+ if requested_protocol = request.headers[SEC_WEBSOCKET_PROTOCOL]
40
+ protocol = (requested_protocol & protocols).first
41
+ end
42
+
43
+ response = Response.for(request, headers, protocol: protocol, **options) do |stream|
44
+ # Once we get to this point, we no longer need to hold on to the response:
45
+ response = nil
46
+
47
+ framer = Protocol::WebSocket::Framer.new(stream)
48
+ connection = handler.call(framer, protocol)
49
+
50
+ yield connection
51
+
52
+ connection.close unless connection.closed?
53
+ end
54
+
55
+ # Once we get to this point, we no longer need to hold on to the request:
56
+ request = nil
57
+
58
+ return response
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -20,8 +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 '../connection'
24
- require_relative '../response'
23
+ require_relative 'http'
25
24
 
26
25
  module Async
27
26
  module WebSocket
@@ -33,37 +32,21 @@ module Async
33
32
  request = env['async.http.request'] and Array(request.protocol).include?(PROTOCOL)
34
33
  end
35
34
 
36
- def self.open(env, headers: [], protocols: [], handler: Connection, **options, &block)
37
- if request = env['async.http.request'] and Array(request.protocol).include?(PROTOCOL)
35
+ def self.open(env, **options, &block)
36
+ if request = env['async.http.request']
38
37
  env = nil
39
38
 
40
- # Select websocket sub-protocol:
41
- if requested_protocol = request.headers[SEC_WEBSOCKET_PROTOCOL]
42
- protocol = (requested_protocol & protocols).first
43
- end
44
-
45
- response = Response.for(request, headers, protocol: protocol, **options) do |stream|
46
- response = nil
47
-
48
- framer = Protocol::WebSocket::Framer.new(stream)
49
-
50
- connection = handler.call(framer, protocol)
39
+ if response = HTTP.open(request, **options, &block)
40
+ headers = response.headers
51
41
 
52
- yield connection
42
+ if protocol = response.protocol
43
+ headers = Protocol::HTTP::Headers::Merged.new(headers, [
44
+ ['rack.protocol', protocol]
45
+ ])
46
+ end
53
47
 
54
- connection.close unless connection.closed?
48
+ return [response.status, headers.to_h, response.body]
55
49
  end
56
-
57
- request = nil
58
- headers = response.headers
59
-
60
- if protocol = response.protocol
61
- headers = Protocol::HTTP::Headers::Merged.new(headers, [
62
- ['rack.protocol', protocol]
63
- ])
64
- end
65
-
66
- return [response.status, headers.to_h, response.body]
67
50
  end
68
51
  end
69
52
  end
@@ -49,16 +49,16 @@ module Async
49
49
 
50
50
  # @return [Connection] an open websocket connection to the given endpoint.
51
51
  def self.connect(endpoint, *args, **options, &block)
52
- self.open(endpoint, *args) do |client|
53
- connection = client.connect(endpoint.authority, endpoint.path, **options)
52
+ client = self.open(endpoint, *args)
53
+ connection = client.connect(endpoint.authority, endpoint.path, **options)
54
54
 
55
- return connection unless block_given?
55
+ return connection unless block_given?
56
56
 
57
- begin
58
- yield connection
59
- ensure
60
- connection.close
61
- end
57
+ begin
58
+ yield connection
59
+ ensure
60
+ connection.close
61
+ client.close
62
62
  end
63
63
  end
64
64
 
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module WebSocket
23
- VERSION = "0.18.0"
23
+ VERSION = "0.19.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-websocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.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: 2021-04-21 00:00:00.000000000 Z
11
+ date: 2021-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-http
@@ -143,6 +143,7 @@ extensions: []
143
143
  extra_rdoc_files: []
144
144
  files:
145
145
  - lib/async/websocket.rb
146
+ - lib/async/websocket/adapters/http.rb
146
147
  - lib/async/websocket/adapters/rack.rb
147
148
  - lib/async/websocket/adapters/rails.rb
148
149
  - lib/async/websocket/client.rb
@@ -176,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
177
  - !ruby/object:Gem::Version
177
178
  version: '0'
178
179
  requirements: []
179
- rubygems_version: 3.1.2
180
+ rubygems_version: 3.3.0.dev
180
181
  signing_key:
181
182
  specification_version: 4
182
183
  summary: An async websocket library on top of websocket-driver.