summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-06 10:21:26 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-06 10:21:26 +0900
commit4c26fd8e0c8bde6bac5a649629c79a6db82cd06f (patch)
tree3d73725f10d0356d7044c5b18529b8cc0b547f29
parentabfb4a22a7f719b645c9fd97444fbaba7c357c17 (diff)
downloadplum-4c26fd8e0c8bde6bac5a649629c79a6db82cd06f.tar.gz
client: constructor accept IO object
-rw-r--r--lib/plum/client.rb42
-rw-r--r--lib/plum/client/connection.rb1
-rw-r--r--test/plum/client/test_response.rb2
3 files changed, 27 insertions, 18 deletions
diff --git a/lib/plum/client.rb b/lib/plum/client.rb
index 81066d7..35c4001 100644
--- a/lib/plum/client.rb
+++ b/lib/plum/client.rb
@@ -2,8 +2,9 @@
module Plum
class Client
DEFAULT_CONFIG = {
- https: true,
- verify_mode: OpenSSL::SSL::VERIFY_NONE,
+ tls: true,
+ scheme: "https",
+ verify_mode: OpenSSL::SSL::VERIFY_PEER,
}.freeze
attr_reader :host, :port, :config
@@ -11,20 +12,26 @@ module Plum
# Creates a new HTTP client and starts communication.
# A shorthand for `Plum::Client.new(args).start(&block)`
- def self.start(host, port, config = {}, &block)
+ def self.start(host, port = nil, config = {}, &block)
client = self.new(host, port, config)
client.start(&block)
end
- # @param host [String] the host to connect
+ # Creates a new HTTP client.
+ # @param host [String | IO] the host to connect, or IO object.
# @param port [Integer] the port number to connect
# @param config [Hash<Symbol, Object>] the client configuration
- def initialize(host, port, config = {})
- @host = host
- @port = port
+ def initialize(host, port = nil, config = {})
+ if host.is_a?(IO)
+ @socket = host
+ else
+ @host = host
+ @port = port || (config[:tls] ? 443 : 80)
+ end
@config = DEFAULT_CONFIG.merge(config)
@response_handlers = {}
@responses = {}
+ @started = false
end
# Starts communication.
@@ -99,7 +106,7 @@ module Plum
base_headers = { ":method" => nil,
":path" => nil,
":authority" => (@config[:hostname] || @host),
- ":scheme" => @config[:https] ? "https" : "http" }
+ ":scheme" => (@config[:scheme] || "https") }
response = request_async(base_headers.merge(headers), body)
wait(response)
@@ -154,16 +161,19 @@ module Plum
private
def _start
@started = true
- sock = TCPSocket.open(host, port)
- if config[:https]
- ctx = @config[:ssl_context] || new_ssl_ctx
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- sock.connect
+ unless @socket
+ sock = TCPSocket.open(host, port)
+ if config[:tls]
+ ctx = @config[:ssl_context] || new_ssl_ctx
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ sock.connect
+ end
+
+ @socket = sock
end
- @socket = sock
- @plum = setup_plum(sock)
+ @plum = setup_plum(@socket)
end
def setup_plum(sock)
diff --git a/lib/plum/client/connection.rb b/lib/plum/client/connection.rb
index 8721fb5..26e53b3 100644
--- a/lib/plum/client/connection.rb
+++ b/lib/plum/client/connection.rb
@@ -11,7 +11,6 @@ module Plum
end
# Create a new stream for HTTP request.
- # @param args [Hash] the argument for Stream.new
def open_stream
next_id = @max_stream_id + (@max_stream_id.even? ? 1 : 2)
stream(next_id)
diff --git a/test/plum/client/test_response.rb b/test/plum/client/test_response.rb
index 16b9be3..dbb6ba2 100644
--- a/test/plum/client/test_response.rb
+++ b/test/plum/client/test_response.rb
@@ -18,7 +18,7 @@ class ResponseTest < Minitest::Test
run = true
resp.each_chunk { |chunk| ret << chunk } } }
resp._chunk("a")
- resp._fail
+ resp._fail(RuntimeError.new)
timeout(3) {
t.join }
assert(run)