8000 Add `http` adapter. · Hanke/async-websocket@b5f7dfa · GitHub
[go: up one dir, main page]

Skip to content

Commit b5f7dfa

Browse files
committed
Add http adapter.
1 parent c7ca0cc commit b5f7dfa

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

lib/async/websocket/adapters/http.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/async/websocket/adapters/rack.rb

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222

23-
require_relative '../connection'
24-
require_relative '../response'
23+
require_relative 'http'
2524

2625
module Async
2726
module WebSocket
@@ -33,37 +32,21 @@ def self.websocket?(env)
3332
request = env['async.http.request'] and Array(request.protocol).include?(PROTOCOL)
3433
end
3534

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']
3837
env = nil
3938

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
5141

52-
yield connection
42+
if protocol = response.protocol
43+
headers = Protocol::HTTP::Headers::Merged.new(headers, [
44+
['rack.protocol', protocol]
45+
])
46+
end
5347

54-
connection.close unless connection.closed?
48+
return [response.status, headers.to_h, response.body]
5549
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]
6750
end
6851
end
6952
end

0 commit comments

Comments
 (0)
0