summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-04 01:02:33 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-04 01:02:33 +0900
commit0588a60dd5b73db2dcdbe6fee094f1b1d229cfbe (patch)
tree1e5a9b59377e4e3458d084246bc7e51fac1b8c08
parentb33d179b001119155d4dc0bd8dc43a18a29cfaa9 (diff)
downloadplum-0588a60dd5b73db2dcdbe6fee094f1b1d229cfbe.tar.gz
client: add :failed state
-rw-r--r--lib/plum/client.rb21
-rw-r--r--lib/plum/client/response.rb19
2 files changed, 37 insertions, 3 deletions
diff --git a/lib/plum/client.rb b/lib/plum/client.rb
index b366791..fa5bdd8 100644
--- a/lib/plum/client.rb
+++ b/lib/plum/client.rb
@@ -168,8 +168,16 @@ module Plum
initial_window_size: (1 << 30) - 1,
}
plum = ClientConnection.new(sock.method(:write), local_settings)
- plum.on(:protocol_error) { |ex| raise ex }
- plum.on(:stream_error) { |stream, ex| raise ex }
+ plum.on(:protocol_error) { |ex|
+ _fail
+ raise ex
+ }
+ plum.on(:stream_error) { |stream, ex|
+ if res = @responses.delete(stream)
+ res._fail unless res.finished?
+ end
+ raise ex
+ }
plum.on(:headers) { |stream, headers|
response = @responses[stream]
response._headers(headers)
@@ -189,9 +197,18 @@ module Plum
end
def _succ
+ _fail if @socket.closed? || @socket.eof?
@plum << @socket.readpartial(1024)
end
+ def _fail
+ while res = @responses.pop
+ res._fail unless res.finished?
+ end
+ ensure
+ close
+ end
+
def new_ssl_ctx
ctx = OpenSSL::SSL::SSLContext.new
ctx.ssl_version = :TLSv1_2
diff --git a/lib/plum/client/response.rb b/lib/plum/client/response.rb
index 56bff7e..6afa4f9 100644
--- a/lib/plum/client/response.rb
+++ b/lib/plum/client/response.rb
@@ -9,6 +9,7 @@ module Plum
def initialize
@body = Queue.new
@finished = false
+ @failed = false
@body_read = false
end
@@ -31,13 +32,23 @@ module Plum
@finished
end
+ # Returns whether the request has failed or not.
+ # @return [Boolean]
+ def failed?
+ @failed
+ end
+
# Yields a chunk of the response body until EOF.
# @yield [chunk] A chunk of the response body.
def each_chunk(&block)
raise "Body already read" if @body_read
@body_read = true
while chunk = @body.pop
- yield chunk
+ if chunk == :failed
+ raise EOFError
+ else
+ yield chunk
+ end
end
end
@@ -65,5 +76,11 @@ module Plum
@finished = true
@body << nil # @body.close is not implemented in Ruby 2.2
end
+
+ # @api private
+ def _fail
+ @failed = true
+ @body << :failed
+ end
end
end