Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../usr/share/ruby/openssl
File: buffering.rb
# coding: binary
[0] Fix | Delete
# frozen_string_literal: false
[1] Fix | Delete
#--
[2] Fix | Delete
#= Info
[3] Fix | Delete
# 'OpenSSL for Ruby 2' project
[4] Fix | Delete
# Copyright (C) 2001 GOTOU YUUZOU <gotoyuzo@notwork.org>
[5] Fix | Delete
# All rights reserved.
[6] Fix | Delete
#
[7] Fix | Delete
#= Licence
[8] Fix | Delete
# This program is licensed under the same licence as Ruby.
[9] Fix | Delete
# (See the file 'LICENCE'.)
[10] Fix | Delete
#++
[11] Fix | Delete
[12] Fix | Delete
##
[13] Fix | Delete
# OpenSSL IO buffering mix-in module.
[14] Fix | Delete
#
[15] Fix | Delete
# This module allows an OpenSSL::SSL::SSLSocket to behave like an IO.
[16] Fix | Delete
#
[17] Fix | Delete
# You typically won't use this module directly, you can see it implemented in
[18] Fix | Delete
# OpenSSL::SSL::SSLSocket.
[19] Fix | Delete
[20] Fix | Delete
module OpenSSL::Buffering
[21] Fix | Delete
include Enumerable
[22] Fix | Delete
[23] Fix | Delete
##
[24] Fix | Delete
# The "sync mode" of the SSLSocket.
[25] Fix | Delete
#
[26] Fix | Delete
# See IO#sync for full details.
[27] Fix | Delete
[28] Fix | Delete
attr_accessor :sync
[29] Fix | Delete
[30] Fix | Delete
##
[31] Fix | Delete
# Default size to read from or write to the SSLSocket for buffer operations.
[32] Fix | Delete
[33] Fix | Delete
BLOCK_SIZE = 1024*16
[34] Fix | Delete
[35] Fix | Delete
##
[36] Fix | Delete
# Creates an instance of OpenSSL's buffering IO module.
[37] Fix | Delete
[38] Fix | Delete
def initialize(*)
[39] Fix | Delete
super
[40] Fix | Delete
@eof = false
[41] Fix | Delete
@rbuffer = ""
[42] Fix | Delete
@sync = @io.sync
[43] Fix | Delete
end
[44] Fix | Delete
[45] Fix | Delete
#
[46] Fix | Delete
# for reading.
[47] Fix | Delete
#
[48] Fix | Delete
private
[49] Fix | Delete
[50] Fix | Delete
##
[51] Fix | Delete
# Fills the buffer from the underlying SSLSocket
[52] Fix | Delete
[53] Fix | Delete
def fill_rbuff
[54] Fix | Delete
begin
[55] Fix | Delete
@rbuffer << self.sysread(BLOCK_SIZE)
[56] Fix | Delete
rescue Errno::EAGAIN
[57] Fix | Delete
retry
[58] Fix | Delete
rescue EOFError
[59] Fix | Delete
@eof = true
[60] Fix | Delete
end
[61] Fix | Delete
end
[62] Fix | Delete
[63] Fix | Delete
##
[64] Fix | Delete
# Consumes _size_ bytes from the buffer
[65] Fix | Delete
[66] Fix | Delete
def consume_rbuff(size=nil)
[67] Fix | Delete
if @rbuffer.empty?
[68] Fix | Delete
nil
[69] Fix | Delete
else
[70] Fix | Delete
size = @rbuffer.size unless size
[71] Fix | Delete
ret = @rbuffer[0, size]
[72] Fix | Delete
@rbuffer[0, size] = ""
[73] Fix | Delete
ret
[74] Fix | Delete
end
[75] Fix | Delete
end
[76] Fix | Delete
[77] Fix | Delete
public
[78] Fix | Delete
[79] Fix | Delete
##
[80] Fix | Delete
# Reads _size_ bytes from the stream. If _buf_ is provided it must
[81] Fix | Delete
# reference a string which will receive the data.
[82] Fix | Delete
#
[83] Fix | Delete
# See IO#read for full details.
[84] Fix | Delete
[85] Fix | Delete
def read(size=nil, buf=nil)
[86] Fix | Delete
if size == 0
[87] Fix | Delete
if buf
[88] Fix | Delete
buf.clear
[89] Fix | Delete
return buf
[90] Fix | Delete
else
[91] Fix | Delete
return ""
[92] Fix | Delete
end
[93] Fix | Delete
end
[94] Fix | Delete
until @eof
[95] Fix | Delete
break if size && size <= @rbuffer.size
[96] Fix | Delete
fill_rbuff
[97] Fix | Delete
end
[98] Fix | Delete
ret = consume_rbuff(size) || ""
[99] Fix | Delete
if buf
[100] Fix | Delete
buf.replace(ret)
[101] Fix | Delete
ret = buf
[102] Fix | Delete
end
[103] Fix | Delete
(size && ret.empty?) ? nil : ret
[104] Fix | Delete
end
[105] Fix | Delete
[106] Fix | Delete
##
[107] Fix | Delete
# Reads at most _maxlen_ bytes from the stream. If _buf_ is provided it
[108] Fix | Delete
# must reference a string which will receive the data.
[109] Fix | Delete
#
[110] Fix | Delete
# See IO#readpartial for full details.
[111] Fix | Delete
[112] Fix | Delete
def readpartial(maxlen, buf=nil)
[113] Fix | Delete
if maxlen == 0
[114] Fix | Delete
if buf
[115] Fix | Delete
buf.clear
[116] Fix | Delete
return buf
[117] Fix | Delete
else
[118] Fix | Delete
return ""
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
if @rbuffer.empty?
[122] Fix | Delete
begin
[123] Fix | Delete
return sysread(maxlen, buf)
[124] Fix | Delete
rescue Errno::EAGAIN
[125] Fix | Delete
retry
[126] Fix | Delete
end
[127] Fix | Delete
end
[128] Fix | Delete
ret = consume_rbuff(maxlen)
[129] Fix | Delete
if buf
[130] Fix | Delete
buf.replace(ret)
[131] Fix | Delete
ret = buf
[132] Fix | Delete
end
[133] Fix | Delete
ret
[134] Fix | Delete
end
[135] Fix | Delete
[136] Fix | Delete
##
[137] Fix | Delete
# Reads at most _maxlen_ bytes in the non-blocking manner.
[138] Fix | Delete
#
[139] Fix | Delete
# When no data can be read without blocking it raises
[140] Fix | Delete
# OpenSSL::SSL::SSLError extended by IO::WaitReadable or IO::WaitWritable.
[141] Fix | Delete
#
[142] Fix | Delete
# IO::WaitReadable means SSL needs to read internally so read_nonblock
[143] Fix | Delete
# should be called again when the underlying IO is readable.
[144] Fix | Delete
#
[145] Fix | Delete
# IO::WaitWritable means SSL needs to write internally so read_nonblock
[146] Fix | Delete
# should be called again after the underlying IO is writable.
[147] Fix | Delete
#
[148] Fix | Delete
# OpenSSL::Buffering#read_nonblock needs two rescue clause as follows:
[149] Fix | Delete
#
[150] Fix | Delete
# # emulates blocking read (readpartial).
[151] Fix | Delete
# begin
[152] Fix | Delete
# result = ssl.read_nonblock(maxlen)
[153] Fix | Delete
# rescue IO::WaitReadable
[154] Fix | Delete
# IO.select([io])
[155] Fix | Delete
# retry
[156] Fix | Delete
# rescue IO::WaitWritable
[157] Fix | Delete
# IO.select(nil, [io])
[158] Fix | Delete
# retry
[159] Fix | Delete
# end
[160] Fix | Delete
#
[161] Fix | Delete
# Note that one reason that read_nonblock writes to the underlying IO is
[162] Fix | Delete
# when the peer requests a new TLS/SSL handshake. See openssl the FAQ for
[163] Fix | Delete
# more details. http://www.openssl.org/support/faq.html
[164] Fix | Delete
#
[165] Fix | Delete
# By specifying a keyword argument _exception_ to +false+, you can indicate
[166] Fix | Delete
# that read_nonblock should not raise an IO::Wait*able exception, but
[167] Fix | Delete
# return the symbol +:wait_writable+ or +:wait_readable+ instead. At EOF,
[168] Fix | Delete
# it will return +nil+ instead of raising EOFError.
[169] Fix | Delete
[170] Fix | Delete
def read_nonblock(maxlen, buf=nil, exception: true)
[171] Fix | Delete
if maxlen == 0
[172] Fix | Delete
if buf
[173] Fix | Delete
buf.clear
[174] Fix | Delete
return buf
[175] Fix | Delete
else
[176] Fix | Delete
return ""
[177] Fix | Delete
end
[178] Fix | Delete
end
[179] Fix | Delete
if @rbuffer.empty?
[180] Fix | Delete
return sysread_nonblock(maxlen, buf, exception: exception)
[181] Fix | Delete
end
[182] Fix | Delete
ret = consume_rbuff(maxlen)
[183] Fix | Delete
if buf
[184] Fix | Delete
buf.replace(ret)
[185] Fix | Delete
ret = buf
[186] Fix | Delete
end
[187] Fix | Delete
ret
[188] Fix | Delete
end
[189] Fix | Delete
[190] Fix | Delete
##
[191] Fix | Delete
# Reads the next "line" from the stream. Lines are separated by _eol_. If
[192] Fix | Delete
# _limit_ is provided the result will not be longer than the given number of
[193] Fix | Delete
# bytes.
[194] Fix | Delete
#
[195] Fix | Delete
# _eol_ may be a String or Regexp.
[196] Fix | Delete
#
[197] Fix | Delete
# Unlike IO#gets the line read will not be assigned to +$_+.
[198] Fix | Delete
#
[199] Fix | Delete
# Unlike IO#gets the separator must be provided if a limit is provided.
[200] Fix | Delete
[201] Fix | Delete
def gets(eol=$/, limit=nil)
[202] Fix | Delete
idx = @rbuffer.index(eol)
[203] Fix | Delete
until @eof
[204] Fix | Delete
break if idx
[205] Fix | Delete
fill_rbuff
[206] Fix | Delete
idx = @rbuffer.index(eol)
[207] Fix | Delete
end
[208] Fix | Delete
if eol.is_a?(Regexp)
[209] Fix | Delete
size = idx ? idx+$&.size : nil
[210] Fix | Delete
else
[211] Fix | Delete
size = idx ? idx+eol.size : nil
[212] Fix | Delete
end
[213] Fix | Delete
if size && limit && limit >= 0
[214] Fix | Delete
size = [size, limit].min
[215] Fix | Delete
end
[216] Fix | Delete
consume_rbuff(size)
[217] Fix | Delete
end
[218] Fix | Delete
[219] Fix | Delete
##
[220] Fix | Delete
# Executes the block for every line in the stream where lines are separated
[221] Fix | Delete
# by _eol_.
[222] Fix | Delete
#
[223] Fix | Delete
# See also #gets
[224] Fix | Delete
[225] Fix | Delete
def each(eol=$/)
[226] Fix | Delete
while line = self.gets(eol)
[227] Fix | Delete
yield line
[228] Fix | Delete
end
[229] Fix | Delete
end
[230] Fix | Delete
alias each_line each
[231] Fix | Delete
[232] Fix | Delete
##
[233] Fix | Delete
# Reads lines from the stream which are separated by _eol_.
[234] Fix | Delete
#
[235] Fix | Delete
# See also #gets
[236] Fix | Delete
[237] Fix | Delete
def readlines(eol=$/)
[238] Fix | Delete
ary = []
[239] Fix | Delete
while line = self.gets(eol)
[240] Fix | Delete
ary << line
[241] Fix | Delete
end
[242] Fix | Delete
ary
[243] Fix | Delete
end
[244] Fix | Delete
[245] Fix | Delete
##
[246] Fix | Delete
# Reads a line from the stream which is separated by _eol_.
[247] Fix | Delete
#
[248] Fix | Delete
# Raises EOFError if at end of file.
[249] Fix | Delete
[250] Fix | Delete
def readline(eol=$/)
[251] Fix | Delete
raise EOFError if eof?
[252] Fix | Delete
gets(eol)
[253] Fix | Delete
end
[254] Fix | Delete
[255] Fix | Delete
##
[256] Fix | Delete
# Reads one character from the stream. Returns nil if called at end of
[257] Fix | Delete
# file.
[258] Fix | Delete
[259] Fix | Delete
def getc
[260] Fix | Delete
read(1)
[261] Fix | Delete
end
[262] Fix | Delete
[263] Fix | Delete
##
[264] Fix | Delete
# Calls the given block once for each byte in the stream.
[265] Fix | Delete
[266] Fix | Delete
def each_byte # :yields: byte
[267] Fix | Delete
while c = getc
[268] Fix | Delete
yield(c.ord)
[269] Fix | Delete
end
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
##
[273] Fix | Delete
# Reads a one-character string from the stream. Raises an EOFError at end
[274] Fix | Delete
# of file.
[275] Fix | Delete
[276] Fix | Delete
def readchar
[277] Fix | Delete
raise EOFError if eof?
[278] Fix | Delete
getc
[279] Fix | Delete
end
[280] Fix | Delete
[281] Fix | Delete
##
[282] Fix | Delete
# Pushes character _c_ back onto the stream such that a subsequent buffered
[283] Fix | Delete
# character read will return it.
[284] Fix | Delete
#
[285] Fix | Delete
# Unlike IO#getc multiple bytes may be pushed back onto the stream.
[286] Fix | Delete
#
[287] Fix | Delete
# Has no effect on unbuffered reads (such as #sysread).
[288] Fix | Delete
[289] Fix | Delete
def ungetc(c)
[290] Fix | Delete
@rbuffer[0,0] = c.chr
[291] Fix | Delete
end
[292] Fix | Delete
[293] Fix | Delete
##
[294] Fix | Delete
# Returns true if the stream is at file which means there is no more data to
[295] Fix | Delete
# be read.
[296] Fix | Delete
[297] Fix | Delete
def eof?
[298] Fix | Delete
fill_rbuff if !@eof && @rbuffer.empty?
[299] Fix | Delete
@eof && @rbuffer.empty?
[300] Fix | Delete
end
[301] Fix | Delete
alias eof eof?
[302] Fix | Delete
[303] Fix | Delete
#
[304] Fix | Delete
# for writing.
[305] Fix | Delete
#
[306] Fix | Delete
private
[307] Fix | Delete
[308] Fix | Delete
##
[309] Fix | Delete
# Writes _s_ to the buffer. When the buffer is full or #sync is true the
[310] Fix | Delete
# buffer is flushed to the underlying socket.
[311] Fix | Delete
[312] Fix | Delete
def do_write(s)
[313] Fix | Delete
@wbuffer = "" unless defined? @wbuffer
[314] Fix | Delete
@wbuffer << s
[315] Fix | Delete
@wbuffer.force_encoding(Encoding::BINARY)
[316] Fix | Delete
@sync ||= false
[317] Fix | Delete
if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/)
[318] Fix | Delete
remain = idx ? idx + $/.size : @wbuffer.length
[319] Fix | Delete
nwritten = 0
[320] Fix | Delete
while remain > 0
[321] Fix | Delete
str = @wbuffer[nwritten,remain]
[322] Fix | Delete
begin
[323] Fix | Delete
nwrote = syswrite(str)
[324] Fix | Delete
rescue Errno::EAGAIN
[325] Fix | Delete
retry
[326] Fix | Delete
end
[327] Fix | Delete
remain -= nwrote
[328] Fix | Delete
nwritten += nwrote
[329] Fix | Delete
end
[330] Fix | Delete
@wbuffer[0,nwritten] = ""
[331] Fix | Delete
end
[332] Fix | Delete
end
[333] Fix | Delete
[334] Fix | Delete
public
[335] Fix | Delete
[336] Fix | Delete
##
[337] Fix | Delete
# Writes _s_ to the stream. If the argument is not a String it will be
[338] Fix | Delete
# converted using +.to_s+ method. Returns the number of bytes written.
[339] Fix | Delete
[340] Fix | Delete
def write(*s)
[341] Fix | Delete
s.inject(0) do |written, str|
[342] Fix | Delete
do_write(str)
[343] Fix | Delete
written + str.bytesize
[344] Fix | Delete
end
[345] Fix | Delete
end
[346] Fix | Delete
[347] Fix | Delete
##
[348] Fix | Delete
# Writes _s_ in the non-blocking manner.
[349] Fix | Delete
#
[350] Fix | Delete
# If there is buffered data, it is flushed first. This may block.
[351] Fix | Delete
#
[352] Fix | Delete
# write_nonblock returns number of bytes written to the SSL connection.
[353] Fix | Delete
#
[354] Fix | Delete
# When no data can be written without blocking it raises
[355] Fix | Delete
# OpenSSL::SSL::SSLError extended by IO::WaitReadable or IO::WaitWritable.
[356] Fix | Delete
#
[357] Fix | Delete
# IO::WaitReadable means SSL needs to read internally so write_nonblock
[358] Fix | Delete
# should be called again after the underlying IO is readable.
[359] Fix | Delete
#
[360] Fix | Delete
# IO::WaitWritable means SSL needs to write internally so write_nonblock
[361] Fix | Delete
# should be called again after underlying IO is writable.
[362] Fix | Delete
#
[363] Fix | Delete
# So OpenSSL::Buffering#write_nonblock needs two rescue clause as follows.
[364] Fix | Delete
#
[365] Fix | Delete
# # emulates blocking write.
[366] Fix | Delete
# begin
[367] Fix | Delete
# result = ssl.write_nonblock(str)
[368] Fix | Delete
# rescue IO::WaitReadable
[369] Fix | Delete
# IO.select([io])
[370] Fix | Delete
# retry
[371] Fix | Delete
# rescue IO::WaitWritable
[372] Fix | Delete
# IO.select(nil, [io])
[373] Fix | Delete
# retry
[374] Fix | Delete
# end
[375] Fix | Delete
#
[376] Fix | Delete
# Note that one reason that write_nonblock reads from the underlying IO
[377] Fix | Delete
# is when the peer requests a new TLS/SSL handshake. See the openssl FAQ
[378] Fix | Delete
# for more details. http://www.openssl.org/support/faq.html
[379] Fix | Delete
#
[380] Fix | Delete
# By specifying a keyword argument _exception_ to +false+, you can indicate
[381] Fix | Delete
# that write_nonblock should not raise an IO::Wait*able exception, but
[382] Fix | Delete
# return the symbol +:wait_writable+ or +:wait_readable+ instead.
[383] Fix | Delete
[384] Fix | Delete
def write_nonblock(s, exception: true)
[385] Fix | Delete
flush
[386] Fix | Delete
syswrite_nonblock(s, exception: exception)
[387] Fix | Delete
end
[388] Fix | Delete
[389] Fix | Delete
##
[390] Fix | Delete
# Writes _s_ to the stream. _s_ will be converted to a String using
[391] Fix | Delete
# +.to_s+ method.
[392] Fix | Delete
[393] Fix | Delete
def <<(s)
[394] Fix | Delete
do_write(s)
[395] Fix | Delete
self
[396] Fix | Delete
end
[397] Fix | Delete
[398] Fix | Delete
##
[399] Fix | Delete
# Writes _args_ to the stream along with a record separator.
[400] Fix | Delete
#
[401] Fix | Delete
# See IO#puts for full details.
[402] Fix | Delete
[403] Fix | Delete
def puts(*args)
[404] Fix | Delete
s = ""
[405] Fix | Delete
if args.empty?
[406] Fix | Delete
s << "\n"
[407] Fix | Delete
end
[408] Fix | Delete
args.each{|arg|
[409] Fix | Delete
s << arg.to_s
[410] Fix | Delete
if $/ && /\n\z/ !~ s
[411] Fix | Delete
s << "\n"
[412] Fix | Delete
end
[413] Fix | Delete
}
[414] Fix | Delete
do_write(s)
[415] Fix | Delete
nil
[416] Fix | Delete
end
[417] Fix | Delete
[418] Fix | Delete
##
[419] Fix | Delete
# Writes _args_ to the stream.
[420] Fix | Delete
#
[421] Fix | Delete
# See IO#print for full details.
[422] Fix | Delete
[423] Fix | Delete
def print(*args)
[424] Fix | Delete
s = ""
[425] Fix | Delete
args.each{ |arg| s << arg.to_s }
[426] Fix | Delete
do_write(s)
[427] Fix | Delete
nil
[428] Fix | Delete
end
[429] Fix | Delete
[430] Fix | Delete
##
[431] Fix | Delete
# Formats and writes to the stream converting parameters under control of
[432] Fix | Delete
# the format string.
[433] Fix | Delete
#
[434] Fix | Delete
# See Kernel#sprintf for format string details.
[435] Fix | Delete
[436] Fix | Delete
def printf(s, *args)
[437] Fix | Delete
do_write(s % args)
[438] Fix | Delete
nil
[439] Fix | Delete
end
[440] Fix | Delete
[441] Fix | Delete
##
[442] Fix | Delete
# Flushes buffered data to the SSLSocket.
[443] Fix | Delete
[444] Fix | Delete
def flush
[445] Fix | Delete
osync = @sync
[446] Fix | Delete
@sync = true
[447] Fix | Delete
do_write ""
[448] Fix | Delete
return self
[449] Fix | Delete
ensure
[450] Fix | Delete
@sync = osync
[451] Fix | Delete
end
[452] Fix | Delete
[453] Fix | Delete
##
[454] Fix | Delete
# Closes the SSLSocket and flushes any unwritten data.
[455] Fix | Delete
[456] Fix | Delete
def close
[457] Fix | Delete
flush rescue nil
[458] Fix | Delete
sysclose
[459] Fix | Delete
end
[460] Fix | Delete
end
[461] Fix | Delete
[462] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function