From e986e7123643b91ece134ea47db9935670108ca2 Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Mon, 18 Aug 2003 22:49:48 +0000 Subject: * ext/openssl/ossl_ssl.c: sync_close is moved to SSLSocket as a builtin. * ext/openssl/lib/openssl/buffering.rb (Buffering#close): ditto. * ext/openssl/lib/openssl/buffering.rb (Buffering#puts): should add a return to the tails of each line. * ext/openssl/lib/openssl/ssl.rb: new class OpenSSL::SSL::SSLServer. * ext/openssl/lib/net/protocols.rb (SSLIO#ssl_connect): use sync_close. * ext/openssl/sample/echo_svr.rb: use SSLServer. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@4407 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/lib/net/protocols.rb | 11 ++------ ext/openssl/lib/openssl/buffering.rb | 13 +++++---- ext/openssl/lib/openssl/ssl.rb | 53 ++++++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 22 deletions(-) (limited to 'ext/openssl/lib') diff --git a/ext/openssl/lib/net/protocols.rb b/ext/openssl/lib/net/protocols.rb index 6f646b578..25e940c54 100644 --- a/ext/openssl/lib/net/protocols.rb +++ b/ext/openssl/lib/net/protocols.rb @@ -40,17 +40,12 @@ module Net end def ssl_connect() - @raw_socket = @socket - @socket = OpenSSL::SSL::SSLSocket.new(@raw_socket, @ssl_context) - @scoket.sync = true + @socket = OpenSSL::SSL::SSLSocket.new(@socket, @ssl_context) + @socket.sync = true + @socket.sync_close = true @socket.connect end - def close - super - @raw_socket.close if @raw_socket - end - def peer_cert @socket.peer_cert end diff --git a/ext/openssl/lib/openssl/buffering.rb b/ext/openssl/lib/openssl/buffering.rb index 6ddec099b..031af4baa 100644 --- a/ext/openssl/lib/openssl/buffering.rb +++ b/ext/openssl/lib/openssl/buffering.rb @@ -16,7 +16,7 @@ module Buffering include Enumerable - attr_accessor :sync, :sync_close + attr_accessor :sync BLOCK_SIZE = 1024*16 # @@ -158,7 +158,12 @@ module Buffering def puts(*args) s = "" - args.each{ |arg| s << arg.to_s + $/ } + args.each{|arg| + s << arg.to_s + unless /#{$/}\Z/o =~ s + s << $/ + end + } do_write(s) nil end @@ -183,9 +188,7 @@ module Buffering end def close - flush + flush rescue nil sysclose - @sync_close ||= false - @io.close if @sync_close end end diff --git a/ext/openssl/lib/openssl/ssl.rb b/ext/openssl/lib/openssl/ssl.rb index 39d975b1c..6e6bdfe94 100644 --- a/ext/openssl/lib/openssl/ssl.rb +++ b/ext/openssl/lib/openssl/ssl.rb @@ -18,31 +18,66 @@ require 'openssl/buffering' module OpenSSL module SSL - class SSLSocket - include Buffering - + module SocketForwarder def addr - @io.addr + to_io.addr end def peeraddr - @io.peeraddr + to_io.peeraddr end def getsockopt(level, optname, optval) - @io.setsockopt(level, optname, optval) + to_io.setsockopt(level, optname, optval) end def setsockopt(level, optname) - @io.setsockopt(level, optname) + to_io.setsockopt(level, optname) end def fcntl(*args) - @io.fcntl(*args) + to_io.fcntl(*args) end def closed? - @io.closed? + to_io.closed? + end + end + + class SSLSocket + include Buffering + include SocketForwarder + end + + class SSLServer + include SocketForwarder + attr_accessor :start_immediately + + def initialize(svr, ctx) + @svr = svr + @ctx = ctx + @start_immediately = true + end + + def to_io + @svr + end + + def listen(basklog=5) + @svr.listen(backlog) + end + + def accept + sock = @svr.accept + ssl = OpenSSL::SSL::SSLSocket.new(sock, @ctx) + ssl.sync = true + ssl.sync_close = true + ssl.accept if @start_immediately + ssl + end + + def close + @svr.close end end end -- cgit