In Files

  • openssl/lib/openssl/buffering.rb

Class/Module Index [+]

Quicksearch

OpenSSL::Buffering

Constants

BLOCK_SIZE

Attributes

sync[RW]

Public Class Methods

new(*args) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 23
def initialize(*args)
  @eof = false
  @rbuffer = ""
  @sync = @io.sync
end
            

Public Instance Methods

<<(s) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 295
def << (s)
  do_write(s)
  self
end
            
close() click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 336
def close
  flush rescue nil
  sysclose
end
            
each(eol=$/) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 174
def each(eol=$/)
  while line = self.gets(eol)
    yield line
  end
end
            
Also aliased as: each_line
each_byte() click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 199
def each_byte
  while c = getc
    yield(c)
  end
end
            
each_line(eol=$/) click to toggle source
Alias for: each
eof() click to toggle source
Alias for: eof?
eof?() click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 214
def eof?
  fill_rbuff if !@eof && @rbuffer.empty?
  @eof && @rbuffer.empty?
end
            
Also aliased as: eof
flush() click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 327
def flush
  osync = @sync
  @sync = true
  do_write ""
  return self
ensure
  @sync = osync
end
            
getc() click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 194
def getc
  c = read(1)
  c ? c[0] : nil
end
            
gets(eol=$/, limit=nil) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 156
def gets(eol=$/, limit=nil)
  idx = @rbuffer.index(eol)
  until @eof
    break if idx
    fill_rbuff
    idx = @rbuffer.index(eol)
  end
  if eol.is_a?(Regexp)
    size = idx ? idx+$&.size : nil
  else
    size = idx ? idx+eol.size : nil
  end
  if limit and limit >= 0
    size = [size, limit].min
  end
  consume_rbuff(size)
end
            
printf(s, *args) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 322
def printf(s, *args)
  do_write(s % args)
  nil
end
            
puts(*args) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 300
def puts(*args)
  s = ""
  if args.empty?
    s << "\n"
  end
  args.each{|arg|
    s << arg.to_s
    if $/ && /\n\z/ !~ s
      s << "\n"
    end
  }
  do_write(s)
  nil
end
            
read(size=nil, buf=nil) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 57
def read(size=nil, buf=nil)
  if size == 0
    if buf
      buf.clear
      return buf
    else
      return ""
    end
  end
  until @eof
    break if size && size <= @rbuffer.size
    fill_rbuff
  end
  ret = consume_rbuff(size) || ""
  if buf
    buf.replace(ret)
    ret = buf
  end
  (size && ret.empty?) ? nil : ret
end
            
read_nonblock(maxlen, buf=nil) click to toggle source

Reads at most maxlen bytes in the non-blocking manner.

When no data can be read without blocking, It raises OpenSSL::SSL::SSLError extended by IO::WaitReadable or IO::WaitWritable.

IO::WaitReadable means SSL needs to read internally. So #read_nonblock should be called again after underlying IO is readable.

IO::WaitWritable means SSL needs to write internally. So #read_nonblock should be called again after underlying IO is writable.

So #read_nonblock needs two rescue clause as follows.

# emulates blocking read (readpartial).
begin
  result = ssl.read_nonblock(maxlen)
rescue IO::WaitReadable
  IO.select([io])
  retry
rescue IO::WaitWritable
  IO.select(nil, [io])
  retry
end

Note that one reason that #read_nonblock write to a underlying IO is the peer requests a new TLS/SSL handshake. See openssl FAQ for more details. www.openssl.org/support/faq.html

 
               # File openssl/lib/openssl/buffering.rb, line 135
def read_nonblock(maxlen, buf=nil)
  if maxlen == 0
    if buf
      buf.clear
      return buf
    else
      return ""
    end
  end
  if @rbuffer.empty?
    return sysread_nonblock(maxlen, buf)
  end
  ret = consume_rbuff(maxlen)
  if buf
    buf.replace(ret)
    ret = buf
  end
  raise EOFError if ret.empty?
  ret
end
            
readchar() click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 205
def readchar
  raise EOFError if eof?
  getc
end
            
readline(eol=$/) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 189
def readline(eol=$/)
  raise EOFError if eof?
  gets(eol)
end
            
readlines(eol=$/) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 181
def readlines(eol=$/)
  ary = []
  while line = self.gets(eol)
    ary << line
  end
  ary
end
            
readpartial(maxlen, buf=nil) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 78
def readpartial(maxlen, buf=nil)
  if maxlen == 0
    if buf
      buf.clear
      return buf
    else
      return ""
    end
  end
  if @rbuffer.empty?
    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
            
ungetc(c) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 210
def ungetc(c)
  @rbuffer[0,0] = c.chr
end
            
write(s) click to toggle source
 
               # File openssl/lib/openssl/buffering.rb, line 248
def write(s)
  do_write(s)
  s.length
end
            
write_nonblock(s) click to toggle source

Writes str in the non-blocking manner.

If there are buffered data, it is flushed at first. This may block.

#write_nonblock returns number of bytes written to the SSL connection.

When no data can be written without blocking, It raises OpenSSL::SSL::SSLError extended by IO::WaitReadable or IO::WaitWritable.

IO::WaitReadable means SSL needs to read internally. So #write_nonblock should be called again after underlying IO is readable.

IO::WaitWritable means SSL needs to write internally. So #write_nonblock should be called again after underlying IO is writable.

So #write_nonblock needs two rescue clause as follows.

# emulates blocking write.
begin
  result = ssl.write_nonblock(str)
rescue IO::WaitReadable
  IO.select([io])
  retry
rescue IO::WaitWritable
  IO.select(nil, [io])
  retry
end

Note that one reason that #write_nonblock read from a underlying IO is the peer requests a new TLS/SSL handshake. See openssl FAQ for more details. www.openssl.org/support/faq.html

 
               # File openssl/lib/openssl/buffering.rb, line 290
def write_nonblock(s)
  flush
  syswrite_nonblock(s)
end