aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-16 18:48:35 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-16 18:48:35 +0900
commitde62f5962e9047e867eb39ef933738ea3b9722c1 (patch)
treede71f3b397232151fb59929cd6f347c8d6598505
parentcae4777e2da251aeb9a8377cb19f65fa2b2e757b (diff)
downloadplum-de62f5962e9047e867eb39ef933738ea3b9722c1.tar.gz
server/connection: reorganize subclasses
* HTTPSServerConnection is renamed to SSLSocketServerConnection * HTTPServerConnection now accepts writer Proc (Method) instead of IO
-rw-r--r--examples/non_tls_server.rb2
-rw-r--r--examples/static_server.rb2
-rw-r--r--lib/plum.rb2
-rw-r--r--lib/plum/client.rb2
-rw-r--r--lib/plum/rack/listener.rb6
-rw-r--r--lib/plum/rack/session.rb3
-rw-r--r--lib/plum/server/http_connection.rb15
-rw-r--r--lib/plum/server/ssl_socket_connection.rb (renamed from lib/plum/server/https_connection.rb)2
-rw-r--r--test/plum/client/test_client.rb2
-rw-r--r--test/plum/server/test_connection.rb (renamed from test/plum/server/test_https_connection.rb)10
-rw-r--r--test/plum/server/test_http_connection.rb10
-rw-r--r--test/utils/server.rb8
12 files changed, 27 insertions, 37 deletions
diff --git a/examples/non_tls_server.rb b/examples/non_tls_server.rb
index e7bad41..cc2d233 100644
--- a/examples/non_tls_server.rb
+++ b/examples/non_tls_server.rb
@@ -25,7 +25,7 @@ loop do
next
end
- plum = Plum::HTTPServerConnection.new(sock)
+ plum = Plum::HTTPServerConnection.new(sock.method(:write))
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 4013f28..33f66a9 100644
--- a/examples/static_server.rb
+++ b/examples/static_server.rb
@@ -43,7 +43,7 @@ loop do
next
end
- plum = Plum::HTTPSServerConnection.new(sock)
+ plum = Plum::ServerConnection.new(sock.method(:write))
plum.on(:frame) do |frame|
log(id, frame.stream_id, "recv: #{frame.inspect}")
diff --git a/lib/plum.rb b/lib/plum.rb
index 95e5588..9452d8f 100644
--- a/lib/plum.rb
+++ b/lib/plum.rb
@@ -21,7 +21,7 @@ require "plum/connection"
require "plum/stream_utils"
require "plum/stream"
require "plum/server/connection"
-require "plum/server/https_connection"
+require "plum/server/ssl_socket_connection"
require "plum/server/http_connection"
require "plum/client"
require "plum/client/response"
diff --git a/lib/plum/client.rb b/lib/plum/client.rb
index 8cb9230..103d938 100644
--- a/lib/plum/client.rb
+++ b/lib/plum/client.rb
@@ -175,7 +175,7 @@ module Plum
cert_store.set_default_paths
ctx.cert_store = cert_store
if @config[:http2]
- ctx.ciphers = "ALL:!" + HTTPSServerConnection::CIPHER_BLACKLIST.join(":!")
+ ctx.ciphers = "ALL:!" + SSLSocketServerConnection::CIPHER_BLACKLIST.join(":!")
if ctx.respond_to?(:alpn_protocols)
ctx.alpn_protocols = ["h2", "http/1.1"]
end
diff --git a/lib/plum/rack/listener.rb b/lib/plum/rack/listener.rb
index 079c680..c650ff0 100644
--- a/lib/plum/rack/listener.rb
+++ b/lib/plum/rack/listener.rb
@@ -25,7 +25,7 @@ module Plum
end
def plum(sock)
- ::Plum::HTTPServerConnection.new(sock)
+ ::Plum::HTTPServerConnection.new(sock.method(:write))
end
end
@@ -62,7 +62,7 @@ module Plum
def plum(sock)
raise ::Plum::LegacyHTTPError.new("client doesn't offered h2 with ALPN", nil) unless sock.alpn_protocol == "h2"
- ::Plum::HTTPSServerConnection.new(sock)
+ ::Plum::ServerConnection.new(sock.method(:write))
end
private
@@ -122,7 +122,7 @@ module Plum
end
def plum(sock)
- ::Plum::HTTPSServerConnection.new(sock)
+ ::Plum::ServerConnection.new(sock.method(:write))
end
end
end
diff --git a/lib/plum/rack/session.rb b/lib/plum/rack/session.rb
index f95a10f..84f4869 100644
--- a/lib/plum/rack/session.rb
+++ b/lib/plum/rack/session.rb
@@ -28,9 +28,6 @@ module Plum
while !@sock.closed? && !@sock.eof?
@plum << @sock.readpartial(1024)
end
- rescue Errno::EPIPE, Errno::ECONNRESET => e
- rescue StandardError => e
- @logger.error("#{e.class}: #{e.message}\n#{e.backtrace.map { |b| "\t#{b}" }.join("\n")}")
ensure
@request_thread.each { |stream, thread| thread.kill }
stop
diff --git a/lib/plum/server/http_connection.rb b/lib/plum/server/http_connection.rb
index 1860126..642bcbc 100644
--- a/lib/plum/server/http_connection.rb
+++ b/lib/plum/server/http_connection.rb
@@ -3,20 +3,11 @@ using Plum::BinaryString
module Plum
class HTTPServerConnection < ServerConnection
- attr_reader :sock
-
- def initialize(sock, local_settings = {})
+ def initialize(writer, local_settings = {})
require "http/parser"
- @sock = sock
@negobuf = String.new
@_http_parser = setup_parser
- super(@sock.method(:write), local_settings)
- end
-
- # Closes the socket.
- def close
- super
- @sock.close
+ super(writer, local_settings)
end
private
@@ -68,7 +59,7 @@ module Plum
"Server: plum/#{Plum::VERSION}\r\n" +
"\r\n"
- @sock.write(resp)
+ @writer.call(resp)
end
def process_first_request(parser, headers, body)
diff --git a/lib/plum/server/https_connection.rb b/lib/plum/server/ssl_socket_connection.rb
index bac1b4b..47e823a 100644
--- a/lib/plum/server/https_connection.rb
+++ b/lib/plum/server/ssl_socket_connection.rb
@@ -1,6 +1,6 @@
# -*- frozen-string-literal: true -*-
module Plum
- class HTTPSServerConnection < ServerConnection
+ class SSLSocketServerConnection < ServerConnection
attr_reader :sock
def initialize(sock, local_settings = {})
diff --git a/test/plum/client/test_client.rb b/test/plum/client/test_client.rb
index 214e30f..35c0638 100644
--- a/test/plum/client/test_client.rb
+++ b/test/plum/client/test_client.rb
@@ -112,7 +112,7 @@ class ClientTest < Minitest::Test
begin
Timeout.timeout(1) {
sock = ssl_server.accept
- plum = HTTPSServerConnection.new(sock)
+ plum = ServerConnection.new(sock.method(:write))
plum.on(:stream) { |stream|
headers = data = nil
diff --git a/test/plum/server/test_https_connection.rb b/test/plum/server/test_connection.rb
index d227f73..4a45eaa 100644
--- a/test/plum/server/test_https_connection.rb
+++ b/test/plum/server/test_connection.rb
@@ -4,21 +4,21 @@ using Plum::BinaryString
class HTTPSConnectionNegotiationTest < Minitest::Test
def test_server_must_raise_cprotocol_error_invalid_magic_short
- con = HTTPSServerConnection.new(StringIO.new)
+ con = ServerConnection.new(StringIO.new.method(:write))
assert_connection_error(:protocol_error) {
con << "HELLO"
}
end
def test_server_must_raise_cprotocol_error_invalid_magic_long
- con = HTTPSServerConnection.new(StringIO.new)
+ con = ServerConnection.new(StringIO.new.method(:write))
assert_connection_error(:protocol_error) {
con << ("HELLO" * 100) # over 24
}
end
def test_server_must_raise_cprotocol_error_non_settings_after_magic
- con = HTTPSServerConnection.new(StringIO.new)
+ con = ServerConnection.new(StringIO.new.method(:write))
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 = HTTPSServerConnection.new(StringIO.new)
+ con = ServerConnection.new(StringIO.new.method(:write))
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 = HTTPSServerConnection.new(sock)
+ plum = SSLSocketServerConnection.new(sock)
assert_connection_error(:inadequate_security) {
run = true
while !sock.closed? && !sock.eof?
diff --git a/test/plum/server/test_http_connection.rb b/test/plum/server/test_http_connection.rb
index bd586a5..3093d5b 100644
--- a/test/plum/server/test_http_connection.rb
+++ b/test/plum/server/test_http_connection.rb
@@ -5,7 +5,8 @@ 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 = HTTPServerConnection.new(StringIO.new)
+ io = StringIO.new
+ con = HTTPServerConnection.new(io.method(:write))
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 +15,8 @@ class HTTPConnectionNegotiationTest < Minitest::Test
def test_server_accept_fragmented_magic
magic = Connection::CLIENT_CONNECTION_PREFACE
- con = HTTPServerConnection.new(StringIO.new)
+ io = StringIO.new
+ con = HTTPServerConnection.new(io.method(:write))
assert_no_error {
con << magic[0...5]
con << magic[5..-1]
@@ -25,7 +27,7 @@ class HTTPConnectionNegotiationTest < Minitest::Test
## with HTTP/1.1 Upgrade
def test_server_accept_upgrade
io = StringIO.new
- con = HTTPServerConnection.new(io)
+ con = HTTPServerConnection.new(io.method(:write))
heads = nil
con.on(:headers) {|_, _h| heads = _h.to_h }
req = "GET / HTTP/1.1\r\n" <<
@@ -47,7 +49,7 @@ class HTTPConnectionNegotiationTest < Minitest::Test
def test_server_deny_non_upgrade
io = StringIO.new
- con = HTTPServerConnection.new(io)
+ con = HTTPServerConnection.new(io.method(:write))
req = "GET / HTTP/1.1\r\n" <<
"Host: rhe.jp\r\n" <<
"User-Agent: nya\r\n" <<
diff --git a/test/utils/server.rb b/test/utils/server.rb
index afb9f57..5f1baa7 100644
--- a/test/utils/server.rb
+++ b/test/utils/server.rb
@@ -2,8 +2,8 @@ require "timeout"
module ServerUtils
def open_server_connection(scheme = :https)
- io = StringIO.new
- @_con = (scheme == :https ? HTTPSServerConnection : HTTPServerConnection).new(io)
+ @_io = StringIO.new
+ @_con = (scheme == :https ? ServerConnection : HTTPServerConnection).new(@_io.method(:write))
@_con << Connection::CLIENT_CONNECTION_PREFACE
@_con << Frame.new(type: :settings, stream_id: 0).assemble
if block_given?
@@ -30,8 +30,8 @@ module ServerUtils
end
end
- def sent_frames(con = nil)
- resp = (con || @_con).sock.string.dup.force_encoding(Encoding::BINARY)
+ def sent_frames(io = nil)
+ resp = (io || @_io).string.dup.force_encoding(Encoding::BINARY)
frames = []
while f = Frame.parse!(resp)
frames << f