diff options
-rw-r--r-- | examples/non_tls_server.rb | 2 | ||||
-rw-r--r-- | examples/static_server.rb | 8 | ||||
-rw-r--r-- | lib/plum.rb | 5 | ||||
-rw-r--r-- | lib/plum/connection.rb | 13 | ||||
-rw-r--r-- | lib/plum/rack/listener.rb | 6 | ||||
-rw-r--r-- | lib/plum/server/connection.rb | 18 | ||||
-rw-r--r-- | lib/plum/server/http_connection.rb (renamed from lib/plum/http_connection.rb) | 2 | ||||
-rw-r--r-- | lib/plum/server/https_connection.rb (renamed from lib/plum/https_connection.rb) | 2 | ||||
-rw-r--r-- | test/plum/test_http_connection.rb | 8 | ||||
-rw-r--r-- | test/plum/test_https_connection.rb | 10 | ||||
-rw-r--r-- | test/utils/server.rb | 4 |
11 files changed, 42 insertions, 36 deletions
diff --git a/examples/non_tls_server.rb b/examples/non_tls_server.rb index 2df12bb..a622725 100644 --- a/examples/non_tls_server.rb +++ b/examples/non_tls_server.rb @@ -25,7 +25,7 @@ loop do next end - plum = Plum::HTTPConnection.new(sock) + plum = Plum::HTTPServerConnection.new(sock) plum.on(:frame) do |frame| log(id, frame.stream_id, "recv: #{frame.inspect}") diff --git a/examples/static_server.rb b/examples/static_server.rb index b065d83..4052163 100644 --- a/examples/static_server.rb +++ b/examples/static_server.rb @@ -43,7 +43,7 @@ loop do next end - plum = Plum::HTTPSConnection.new(sock) + plum = Plum::HTTPSServerConnection.new(sock) plum.on(:frame) do |frame| log(id, frame.stream_id, "recv: #{frame.inspect}") @@ -115,9 +115,9 @@ loop do "content-type": "text/html", "content-length": body.size }, body) - image = ("iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAAACVBMVEX///93o0jG/4mTMy20AAAA" - "bklEQVQ4y2NgoAoIRQJkCoSimIdTgJGBBU1ABE1A1AVdBQuaACu6gCALhhZ0axlZCDgMWYAB6ilU" - "35IoADEMxWyyBDD45AhQCFahM0kXWIVu3sAJrILzyBcgytoFeATABBcXWohhCEC14BCgGAAAX1ZQ" + image = ("iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAAACVBMVEX///93o0jG/4mTMy20AAAA" \ + "bklEQVQ4y2NgoAoIRQJkCoSimIdTgJGBBU1ABE1A1AVdBQuaACu6gCALhhZ0axlZCDgMWYAB6ilU" \ + "35IoADEMxWyyBDD45AhQCFahM0kXWIVu3sAJrILzyBcgytoFeATABBcXWohhCEC14BCgGAAAX1ZQ" \ "ZtJp0zAAAAAASUVORK5CYII=").unpack("m")[0] i_stream.respond({ ":status": "200", diff --git a/lib/plum.rb b/lib/plum.rb index 6dab221..5248073 100644 --- a/lib/plum.rb +++ b/lib/plum.rb @@ -17,7 +17,8 @@ require "plum/frame" require "plum/flow_control" require "plum/connection_utils" require "plum/connection" -require "plum/https_connection" -require "plum/http_connection" require "plum/stream_utils" require "plum/stream" +require "plum/server/connection" +require "plum/server/https_connection" +require "plum/server/http_connection" diff --git a/lib/plum/connection.rb b/lib/plum/connection.rb index a8b2916..df3d7b3 100644 --- a/lib/plum/connection.rb +++ b/lib/plum/connection.rb @@ -36,7 +36,6 @@ module Plum @max_odd_stream_id = 0 @max_even_stream_id = 0 end - private :initialize # Emits :close event. Doesn't actually close socket. def close @@ -79,18 +78,6 @@ module Plum @writer.call(frame.assemble) end - def negotiate! - unless CLIENT_CONNECTION_PREFACE.start_with?(@buffer.byteslice(0, 24)) - raise ConnectionError.new(:protocol_error) # (MAY) send GOAWAY. sending. - end - - if @buffer.bytesize >= 24 - @buffer.byteshift(24) - @state = :waiting_settings - settings(@local_settings) - end - end - def new_stream(stream_id, **args) if stream_id.even? @max_even_stream_id = stream_id diff --git a/lib/plum/rack/listener.rb b/lib/plum/rack/listener.rb index 31bbc8c..b479691 100644 --- a/lib/plum/rack/listener.rb +++ b/lib/plum/rack/listener.rb @@ -25,7 +25,7 @@ module Plum end def plum(sock) - ::Plum::HTTPConnection.new(sock) + ::Plum::HTTPServerConnection.new(sock) end end @@ -56,7 +56,7 @@ module Plum end def plum(sock) - ::Plum::HTTPSConnection.new(sock) + ::Plum::HTTPSServerConnection.new(sock) end private @@ -116,7 +116,7 @@ module Plum end def plum(sock) - ::Plum::HTTPSConnection.new(sock) + ::Plum::HTTPSServerConnection.new(sock) end end end diff --git a/lib/plum/server/connection.rb b/lib/plum/server/connection.rb new file mode 100644 index 0000000..a801d92 --- /dev/null +++ b/lib/plum/server/connection.rb @@ -0,0 +1,18 @@ +# -*- frozen-string-literal: true -*- +using Plum::BinaryString +module Plum + class ServerConnection < Connection + private + def negotiate! + unless CLIENT_CONNECTION_PREFACE.start_with?(@buffer.byteslice(0, 24)) + raise ConnectionError.new(:protocol_error) # (MAY) send GOAWAY. sending. + end + + if @buffer.bytesize >= 24 + @buffer.byteshift(24) + settings(@local_settings) + @state = :waiting_settings + end + end + end +end diff --git a/lib/plum/http_connection.rb b/lib/plum/server/http_connection.rb index 1c30e6e..f864691 100644 --- a/lib/plum/http_connection.rb +++ b/lib/plum/server/http_connection.rb @@ -2,7 +2,7 @@ using Plum::BinaryString module Plum - class HTTPConnection < Connection + class HTTPServerConnection < ServerConnection attr_reader :sock def initialize(sock, local_settings = {}) diff --git a/lib/plum/https_connection.rb b/lib/plum/server/https_connection.rb index c719c2e..1903bc1 100644 --- a/lib/plum/https_connection.rb +++ b/lib/plum/server/https_connection.rb @@ -1,6 +1,6 @@ # -*- frozen-string-literal: true -*- module Plum - class HTTPSConnection < Connection + class HTTPSServerConnection < ServerConnection attr_reader :sock def initialize(sock, local_settings = {}) diff --git a/test/plum/test_http_connection.rb b/test/plum/test_http_connection.rb index ea72a55..bd586a5 100644 --- a/test/plum/test_http_connection.rb +++ b/test/plum/test_http_connection.rb @@ -5,7 +5,7 @@ using Plum::BinaryString class HTTPConnectionNegotiationTest < Minitest::Test ## with Prior Knowledge (same as over TLS) def test_server_must_raise_cprotocol_error_non_settings_after_magic - con = HTTPConnection.new(StringIO.new) + con = HTTPServerConnection.new(StringIO.new) con << Connection::CLIENT_CONNECTION_PREFACE assert_connection_error(:protocol_error) { con << Frame.new(type: :window_update, stream_id: 0, payload: "".push_uint32(1)).assemble @@ -14,7 +14,7 @@ class HTTPConnectionNegotiationTest < Minitest::Test def test_server_accept_fragmented_magic magic = Connection::CLIENT_CONNECTION_PREFACE - con = HTTPConnection.new(StringIO.new) + con = HTTPServerConnection.new(StringIO.new) assert_no_error { con << magic[0...5] con << magic[5..-1] @@ -25,7 +25,7 @@ class HTTPConnectionNegotiationTest < Minitest::Test ## with HTTP/1.1 Upgrade def test_server_accept_upgrade io = StringIO.new - con = HTTPConnection.new(io) + con = HTTPServerConnection.new(io) heads = nil con.on(:headers) {|_, _h| heads = _h.to_h } req = "GET / HTTP/1.1\r\n" << @@ -47,7 +47,7 @@ class HTTPConnectionNegotiationTest < Minitest::Test def test_server_deny_non_upgrade io = StringIO.new - con = HTTPConnection.new(io) + con = HTTPServerConnection.new(io) req = "GET / HTTP/1.1\r\n" << "Host: rhe.jp\r\n" << "User-Agent: nya\r\n" << diff --git a/test/plum/test_https_connection.rb b/test/plum/test_https_connection.rb index b08f7be..52a1cf5 100644 --- a/test/plum/test_https_connection.rb +++ b/test/plum/test_https_connection.rb @@ -4,21 +4,21 @@ using Plum::BinaryString class HTTPSConnectionNegotiationTest < Minitest::Test def test_server_must_raise_cprotocol_error_invalid_magic_short - con = HTTPSConnection.new(StringIO.new) + con = HTTPSServerConnection.new(StringIO.new) assert_connection_error(:protocol_error) { con << "HELLO" } end def test_server_must_raise_cprotocol_error_invalid_magic_long - con = HTTPSConnection.new(StringIO.new) + con = HTTPSServerConnection.new(StringIO.new) assert_connection_error(:protocol_error) { con << ("HELLO" * 100) # over 24 } end def test_server_must_raise_cprotocol_error_non_settings_after_magic - con = HTTPSConnection.new(StringIO.new) + con = HTTPSServerConnection.new(StringIO.new) con << Connection::CLIENT_CONNECTION_PREFACE assert_connection_error(:protocol_error) { con << Frame.new(type: :window_update, stream_id: 0, payload: "".push_uint32(1)).assemble @@ -27,7 +27,7 @@ class HTTPSConnectionNegotiationTest < Minitest::Test def test_server_accept_fragmented_magic magic = Connection::CLIENT_CONNECTION_PREFACE - con = HTTPSConnection.new(StringIO.new) + con = HTTPSServerConnection.new(StringIO.new) assert_no_error { con << magic[0...5] con << magic[5..-1] @@ -49,7 +49,7 @@ class HTTPSConnectionNegotiationTest < Minitest::Test begin Timeout.timeout(3) { sock = ssl_server.accept - plum = HTTPSConnection.new(sock) + plum = HTTPSServerConnection.new(sock) assert_connection_error(:inadequate_security) { run = true plum.run diff --git a/test/utils/server.rb b/test/utils/server.rb index 8d1c81d..82eb00f 100644 --- a/test/utils/server.rb +++ b/test/utils/server.rb @@ -3,7 +3,7 @@ require "timeout" module ServerUtils def open_server_connection(scheme = :https) io = StringIO.new - @_con = (scheme == :https ? HTTPSConnection : HTTPConnection).new(io) + @_con = (scheme == :https ? HTTPSServerConnection : HTTPServerConnection).new(io) @_con << Connection::CLIENT_CONNECTION_PREFACE @_con << Frame.new(type: :settings, stream_id: 0).assemble if block_given? @@ -14,7 +14,7 @@ module ServerUtils end def open_new_stream(arg1 = nil, **kwargs) - if arg1.is_a?(Connection) + if arg1.is_a?(ServerConnection) con = arg1 else con = open_server_connection |