diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-10-25 18:42:07 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-10-25 18:42:07 +0900 |
commit | 4d5d63bc80e13b0449799f721df768bff2088c44 (patch) | |
tree | 2a4700481745e18a53b1126ee36f155d11b30cd1 /lib/plum | |
parent | 28a3a72007b2592ff5ea6bbd6984340133636065 (diff) | |
download | plum-4d5d63bc80e13b0449799f721df768bff2088c44.tar.gz |
!connection: Connection keep only writer proc
Diffstat (limited to 'lib/plum')
-rw-r--r-- | lib/plum/connection.rb | 20 | ||||
-rw-r--r-- | lib/plum/http_connection.rb | 21 | ||||
-rw-r--r-- | lib/plum/https_connection.rb | 23 |
3 files changed, 43 insertions, 21 deletions
diff --git a/lib/plum/connection.rb b/lib/plum/connection.rb index 56e5607..5c2db93 100644 --- a/lib/plum/connection.rb +++ b/lib/plum/connection.rb @@ -19,10 +19,10 @@ module Plum attr_reader :hpack_encoder, :hpack_decoder attr_reader :local_settings, :remote_settings - attr_reader :state, :streams, :io + attr_reader :state, :streams - def initialize(io, local_settings = {}) - @io = io + def initialize(writer, local_settings = {}) + @writer = writer @local_settings = Hash.new {|hash, key| DEFAULT_SETTINGS[key] }.merge!(local_settings) @remote_settings = Hash.new {|hash, key| DEFAULT_SETTINGS[key] } @buffer = "".force_encoding(Encoding::BINARY) @@ -37,17 +37,10 @@ module Plum end private :initialize - # Starts communication with the peer. It blocks until the io is closed, or reaches EOF. - def run - while !@io.closed? && !@io.eof? - receive @io.readpartial(1024) - end - end - - # Closes the io. + # Emits :close event. Doesn't actually close socket. def close # TODO: server MAY wait streams - @io.close + callback(:close) end # Receives the specified data and process. @@ -84,8 +77,7 @@ module Plum private def send_immediately(frame) callback(:send_frame, frame) - #frame.assemble - @io.write(frame.assemble) + @writer.call(frame.assemble) end def negotiate! diff --git a/lib/plum/http_connection.rb b/lib/plum/http_connection.rb index 61445af..1ec1a6a 100644 --- a/lib/plum/http_connection.rb +++ b/lib/plum/http_connection.rb @@ -2,12 +2,27 @@ using Plum::BinaryString module Plum class HTTPConnection < Connection - def initialize(io, local_settings = {}) + attr_reader :sock + + def initialize(sock, local_settings = {}) require "http/parser" - super @_headers = nil @_body = "" @_http_parser = setup_parser + @sock = sock + super(@sock.method(:write), local_settings) + end + + # Starts communication with the peer. It blocks until the io is closed, or reaches EOF. + def run + while !@sock.closed? && !@sock.eof? + self << @sock.readpartial(1024) + end + end + + def close + super + @sock.close end private @@ -56,7 +71,7 @@ module Plum resp << "Server: plum/#{Plum::VERSION}\r\n" resp << "\r\n" - io.write(resp) + @sock.write(resp) end def process_first_request diff --git a/lib/plum/https_connection.rb b/lib/plum/https_connection.rb index 82f2f5e..5f1cc14 100644 --- a/lib/plum/https_connection.rb +++ b/lib/plum/https_connection.rb @@ -1,15 +1,30 @@ module Plum class HTTPSConnection < Connection - def initialize(io, local_settings = {}) - if io.respond_to?(:cipher) # OpenSSL::SSL::SSLSocket-like - if CIPHER_BLACKLIST.include?(io.cipher.first) # [cipher-suite, ssl-version, keylen, alglen] - self.on(:negotiated) { + attr_reader :sock + + def initialize(sock, local_settings = {}) + @sock = sock + super(@sock.method(:write), local_settings) + end + + # Starts communication with the peer. It blocks until the io is closed, or reaches EOF. + def run + if @sock.respond_to?(:cipher) # OpenSSL::SSL::SSLSocket-like + if CIPHER_BLACKLIST.include?(@sock.cipher.first) # [cipher-suite, ssl-version, keylen, alglen] + on(:negotiated) { raise ConnectionError.new(:inadequate_security) } end end + while !@sock.closed? && !@sock.eof? + self << @sock.readpartial(1024) + end + end + + def close super + @sock.close end CIPHER_BLACKLIST = %w( |