aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl
diff options
context:
space:
mode:
authorgotoyuzo <gotoyuzo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-03-05 21:36:11 +0000
committergotoyuzo <gotoyuzo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-03-05 21:36:11 +0000
commita9fb0817a15469f0572cd3b8637243daecfe168e (patch)
tree146a5c28bcd348a1ea3333e9b0ae59d58ff078d9 /ext/openssl
parent4b0f7cecc41ec1ae015eed42bd0b82538e8b9408 (diff)
downloadruby-a9fb0817a15469f0572cd3b8637243daecfe168e.tar.gz
* ext/openssl/ossl_ssl.c (ossl_start_ssl): should wait for that
the underlying IO become readable or writable if the error was SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE. [ruby-dev:25795] * ext/openssl/ossl_ssl.c (ossl_ssl_read, ossl_ssl_write): ditto. * ext/openssl/lib/openssl/buffering.rb (Buffering#consume_rbuf): pointless eof flag resetting is deleted. (Buffering#read): should return an empty string if the specified size is zero. (Buffering#readpartial): new method. (Buffering#readline): fix typo. (Buffering#getc): return the first character of string correctly. (Buffering#readchar): fix typo. (Buffering#eof?): should read again it the input buffer is empty. (Buffering#do_write): should rescue Errno::EAGAIN. (Buffering#puts): use "\n" as the output field separator. * ext/openssl/extconf.rb: get rid of GNUmakefile generation. * text/openssl/test_pair.rb: test for IO like methods. * test/ruby/ut_eof.rb: test about empty file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8081 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/openssl')
-rw-r--r--ext/openssl/extconf.rb28
-rw-r--r--ext/openssl/lib/openssl/buffering.rb55
-rw-r--r--ext/openssl/ossl_ssl.c55
3 files changed, 77 insertions, 61 deletions
diff --git a/ext/openssl/extconf.rb b/ext/openssl/extconf.rb
index d587116c82..4517ec82fb 100644
--- a/ext/openssl/extconf.rb
+++ b/ext/openssl/extconf.rb
@@ -113,33 +113,5 @@ have_struct_member("EVP_CIPHER_CTX", "engine", "openssl/evp.h")
have_struct_member("X509_ATTRIBUTE", "single", "openssl/x509.h")
message "=== Checking done. ===\n"
-$distcleanfiles << "GNUmakefile" << "dep"
create_makefile("openssl")
-if /gcc/ =~ CONFIG["CC"]
- File.open("GNUmakefile", "w") {|f|
- f.print <<EOD
-include Makefile
-
-SRCS = $(OBJS:.o=.c)
-
-test-link: $(OBJS)
- $(CC) $(DLDFLAGS) #{OUTFLAG}.testlink $(OBJS) $(LIBPATH) $(LIBS) $(LOCAL_LIBS)
- @$(RM) .testlink
- @echo "Done."
-
-dep:
- $(CC) $(CFLAGS) $(CPPFLAGS) -c $(SRCS) -MM | \\
- $(RUBY) -p -e 'BEGIN{S = []' \\
- -e 'while !ARGV.empty? and /^(\\w+)=(.*)/ =~ ARGV[0]' \\
- -e 'S << [/\#{Regexp.quote($$2)}\\//, "$$(\#{$$1})/"]' \\
- -e 'ARGV.shift' \\
- -e 'end' \\
- -e '}' -e 'S.each(&method(:gsub!))' -- \\
- 'topdir=$(topdir)' 'srcdir=$(srcdir)' 'hdrdir=$(hdrdir)' \\
- > dep
-
-include dep
-EOD
- }
-end
message "Done.\n"
diff --git a/ext/openssl/lib/openssl/buffering.rb b/ext/openssl/lib/openssl/buffering.rb
index 7ad12d7203..39c442bfd1 100644
--- a/ext/openssl/lib/openssl/buffering.rb
+++ b/ext/openssl/lib/openssl/buffering.rb
@@ -41,7 +41,6 @@ module Buffering
def consume_rbuff(size=nil)
if @rbuffer.size == 0
- @eof = nil
nil
else
size = @rbuffer.size unless size
@@ -54,6 +53,14 @@ module Buffering
public
def read(size=nil, buf=nil)
+ if size == 0
+ if buf
+ buf.clear
+ return buf
+ else
+ return ""
+ end
+ end
fill_rbuff unless defined? @rbuffer
@eof ||= nil
until @eof
@@ -68,6 +75,31 @@ module Buffering
(size && ret.empty?) ? nil : ret
end
+ def readpartial(maxlen, buf=nil)
+ if maxlen == 0
+ if buf
+ buf.clear
+ return buf
+ else
+ return ""
+ end
+ end
+ if !defined?(@rbuffer) || @rbuffer.size == 0
+ begin
+ return sysread(maxlen, buf)
+ rescue Errno::EAGAIN
+ retry
+ end
+ end
+ ret = consume_rbuff(maxlen)
+ if buf
+ buf.replace(ret)
+ ret = buf
+ end
+ raise EOFError if ret.empty?
+ ret
+ end
+
def gets(eol=$/)
fill_rbuff unless defined? @rbuffer
idx = @rbuffer.index(eol)
@@ -101,13 +133,13 @@ module Buffering
end
def readline(eol=$/)
- raise EOFErorr if eof?
+ raise EOFError if eof?
gets(eol)
end
def getc
c = read(1)
- c ? c.to_i : nil
+ c ? c[0] : nil
end
def each_byte
@@ -117,7 +149,7 @@ module Buffering
end
def readchar
- raise EOFErorr if eof?
+ raise EOFError if eof?
getc
end
@@ -127,6 +159,7 @@ module Buffering
def eof?
@eof ||= nil
+ fill_rbuff if !@eof && (!defined?(@rbuffer) || @rbuffer.size == 0)
@eof && @rbuffer.size == 0
end
alias eof eof?
@@ -144,7 +177,12 @@ module Buffering
remain = idx ? idx + $/.size : @wbuffer.length
nwritten = 0
while remain > 0
- nwrote = syswrite(@wbuffer[nwritten,remain])
+ str = @wbuffer[nwritten,remain]
+ begin
+ nwrote = syswrite(str)
+ rescue Errno::EAGAIN
+ retry
+ end
remain -= nwrote
nwritten += nwrote
end
@@ -166,10 +204,13 @@ module Buffering
def puts(*args)
s = ""
+ if args.empty?
+ s << "\n"
+ end
args.each{|arg|
s << arg.to_s
- unless /#{$/}\z/o =~ s
- s << $/
+ if $/ && /\n\z/ !~ s
+ s << "\n"
end
}
do_write(s)
diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
index e85430e859..323dc97f83 100644
--- a/ext/openssl/ossl_ssl.c
+++ b/ext/openssl/ossl_ssl.c
@@ -433,52 +433,49 @@ ossl_ssl_setup(VALUE self)
return Qtrue;
}
-static void
-ossl_start_ssl(SSL *ssl, int (*func)())
+static VALUE
+ossl_start_ssl(VALUE self, int (*func)())
{
+ SSL *ssl;
+ OpenFile *fptr;
+ VALUE cb;
int ret;
+ Data_Get_Struct(self, SSL, ssl);
+ GetOpenFile(ossl_ssl_get_io(self), fptr);
+ cb = ossl_sslctx_get_verify_cb(ossl_ssl_get_ctx(self));
+ SSL_set_ex_data(ssl, ossl_ssl_ex_vcb_idx, (void *)cb);
for(;;){
if((ret = func(ssl)) > 0) break;
switch(SSL_get_error(ssl, ret)){
case SSL_ERROR_WANT_WRITE:
+ rb_io_wait_writable(fptr->fd);
+ continue;
case SSL_ERROR_WANT_READ:
- rb_thread_schedule();
- continue;
+ rb_io_wait_readable(fptr->fd);
+ continue;
+ case SSL_ERROR_SYSCALL:
+ rb_sys_fail(0);
default:
ossl_raise(eSSLError, NULL);
}
}
+
+ return self;
}
static VALUE
ossl_ssl_connect(VALUE self)
{
- SSL *ssl;
- VALUE cb;
-
ossl_ssl_setup(self);
- Data_Get_Struct(self, SSL, ssl);
- cb = ossl_sslctx_get_verify_cb(ossl_ssl_get_ctx(self));
- SSL_set_ex_data(ssl, ossl_ssl_ex_vcb_idx, (void *)cb);
- ossl_start_ssl(ssl, SSL_connect);
-
- return self;
+ return ossl_start_ssl(self, SSL_connect);
}
static VALUE
ossl_ssl_accept(VALUE self)
{
- SSL *ssl;
- VALUE cb;
-
ossl_ssl_setup(self);
- Data_Get_Struct(self, SSL, ssl);
- cb = ossl_sslctx_get_verify_cb(ossl_ssl_get_ctx(self));
- SSL_set_ex_data(ssl, ossl_ssl_ex_vcb_idx, (void *)cb);
- ossl_start_ssl(ssl, SSL_accept);
-
- return self;
+ return ossl_start_ssl(self, SSL_accept);
}
static VALUE
@@ -512,8 +509,10 @@ ossl_ssl_read(int argc, VALUE *argv, VALUE self)
case SSL_ERROR_ZERO_RETURN:
rb_eof_error();
case SSL_ERROR_WANT_WRITE:
+ rb_io_wait_writable(fptr->fd);
+ continue;
case SSL_ERROR_WANT_READ:
- rb_thread_schedule();
+ rb_io_wait_readable(fptr->fd);
continue;
case SSL_ERROR_SYSCALL:
if(ERR_peek_error() == 0 && nread == 0) rb_eof_error();
@@ -542,9 +541,11 @@ ossl_ssl_write(VALUE self, VALUE str)
{
SSL *ssl;
int nwrite = 0;
+ OpenFile *fptr;
StringValue(str);
Data_Get_Struct(self, SSL, ssl);
+ GetOpenFile(ossl_ssl_get_io(self), fptr);
if (ssl) {
for (;;){
@@ -553,11 +554,13 @@ ossl_ssl_write(VALUE self, VALUE str)
case SSL_ERROR_NONE:
goto end;
case SSL_ERROR_WANT_WRITE:
+ rb_io_wait_writable(fptr->fd);
+ continue;
case SSL_ERROR_WANT_READ:
- rb_thread_schedule();
- continue;
+ rb_io_wait_readable(fptr->fd);
+ continue;
case SSL_ERROR_SYSCALL:
- rb_eof_error();
+ rb_sys_fail(0);
default:
ossl_raise(eSSLError, "SSL_write:");
}