Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/net
File: protocol.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# = net/protocol.rb
[2] Fix | Delete
#
[3] Fix | Delete
#--
[4] Fix | Delete
# Copyright (c) 1999-2004 Yukihiro Matsumoto
[5] Fix | Delete
# Copyright (c) 1999-2004 Minero Aoki
[6] Fix | Delete
#
[7] Fix | Delete
# written and maintained by Minero Aoki <aamine@loveruby.net>
[8] Fix | Delete
#
[9] Fix | Delete
# This program is free software. You can re-distribute and/or
[10] Fix | Delete
# modify this program under the same terms as Ruby itself,
[11] Fix | Delete
# Ruby Distribute License or GNU General Public License.
[12] Fix | Delete
#
[13] Fix | Delete
# $Id$
[14] Fix | Delete
#++
[15] Fix | Delete
#
[16] Fix | Delete
# WARNING: This file is going to remove.
[17] Fix | Delete
# Do not rely on the implementation written in this file.
[18] Fix | Delete
#
[19] Fix | Delete
[20] Fix | Delete
require 'socket'
[21] Fix | Delete
require 'timeout'
[22] Fix | Delete
require 'io/wait'
[23] Fix | Delete
[24] Fix | Delete
module Net # :nodoc:
[25] Fix | Delete
[26] Fix | Delete
class Protocol #:nodoc: internal use only
[27] Fix | Delete
private
[28] Fix | Delete
def Protocol.protocol_param(name, val)
[29] Fix | Delete
module_eval(<<-End, __FILE__, __LINE__ + 1)
[30] Fix | Delete
def #{name}
[31] Fix | Delete
#{val}
[32] Fix | Delete
end
[33] Fix | Delete
End
[34] Fix | Delete
end
[35] Fix | Delete
[36] Fix | Delete
def ssl_socket_connect(s, timeout)
[37] Fix | Delete
if timeout
[38] Fix | Delete
while true
[39] Fix | Delete
raise Net::OpenTimeout if timeout <= 0
[40] Fix | Delete
start = Process.clock_gettime Process::CLOCK_MONOTONIC
[41] Fix | Delete
# to_io is required because SSLSocket doesn't have wait_readable yet
[42] Fix | Delete
case s.connect_nonblock(exception: false)
[43] Fix | Delete
when :wait_readable; s.to_io.wait_readable(timeout)
[44] Fix | Delete
when :wait_writable; s.to_io.wait_writable(timeout)
[45] Fix | Delete
else; break
[46] Fix | Delete
end
[47] Fix | Delete
timeout -= Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
[48] Fix | Delete
end
[49] Fix | Delete
else
[50] Fix | Delete
s.connect
[51] Fix | Delete
end
[52] Fix | Delete
end
[53] Fix | Delete
end
[54] Fix | Delete
[55] Fix | Delete
[56] Fix | Delete
class ProtocolError < StandardError; end
[57] Fix | Delete
class ProtoSyntaxError < ProtocolError; end
[58] Fix | Delete
class ProtoFatalError < ProtocolError; end
[59] Fix | Delete
class ProtoUnknownError < ProtocolError; end
[60] Fix | Delete
class ProtoServerError < ProtocolError; end
[61] Fix | Delete
class ProtoAuthError < ProtocolError; end
[62] Fix | Delete
class ProtoCommandError < ProtocolError; end
[63] Fix | Delete
class ProtoRetriableError < ProtocolError; end
[64] Fix | Delete
ProtocRetryError = ProtoRetriableError
[65] Fix | Delete
[66] Fix | Delete
##
[67] Fix | Delete
# OpenTimeout, a subclass of Timeout::Error, is raised if a connection cannot
[68] Fix | Delete
# be created within the open_timeout.
[69] Fix | Delete
[70] Fix | Delete
class OpenTimeout < Timeout::Error; end
[71] Fix | Delete
[72] Fix | Delete
##
[73] Fix | Delete
# ReadTimeout, a subclass of Timeout::Error, is raised if a chunk of the
[74] Fix | Delete
# response cannot be read within the read_timeout.
[75] Fix | Delete
[76] Fix | Delete
class ReadTimeout < Timeout::Error
[77] Fix | Delete
def initialize(io = nil)
[78] Fix | Delete
@io = io
[79] Fix | Delete
end
[80] Fix | Delete
attr_reader :io
[81] Fix | Delete
[82] Fix | Delete
def message
[83] Fix | Delete
msg = super
[84] Fix | Delete
if @io
[85] Fix | Delete
msg = "#{msg} with #{@io.inspect}"
[86] Fix | Delete
end
[87] Fix | Delete
msg
[88] Fix | Delete
end
[89] Fix | Delete
end
[90] Fix | Delete
[91] Fix | Delete
##
[92] Fix | Delete
# WriteTimeout, a subclass of Timeout::Error, is raised if a chunk of the
[93] Fix | Delete
# response cannot be written within the write_timeout. Not raised on Windows.
[94] Fix | Delete
[95] Fix | Delete
class WriteTimeout < Timeout::Error
[96] Fix | Delete
def initialize(io = nil)
[97] Fix | Delete
@io = io
[98] Fix | Delete
end
[99] Fix | Delete
attr_reader :io
[100] Fix | Delete
[101] Fix | Delete
def message
[102] Fix | Delete
msg = super
[103] Fix | Delete
if @io
[104] Fix | Delete
msg = "#{msg} with #{@io.inspect}"
[105] Fix | Delete
end
[106] Fix | Delete
msg
[107] Fix | Delete
end
[108] Fix | Delete
end
[109] Fix | Delete
[110] Fix | Delete
[111] Fix | Delete
class BufferedIO #:nodoc: internal use only
[112] Fix | Delete
def initialize(io, read_timeout: 60, write_timeout: 60, continue_timeout: nil, debug_output: nil)
[113] Fix | Delete
@io = io
[114] Fix | Delete
@read_timeout = read_timeout
[115] Fix | Delete
@write_timeout = write_timeout
[116] Fix | Delete
@continue_timeout = continue_timeout
[117] Fix | Delete
@debug_output = debug_output
[118] Fix | Delete
@rbuf = ''.b
[119] Fix | Delete
end
[120] Fix | Delete
[121] Fix | Delete
attr_reader :io
[122] Fix | Delete
attr_accessor :read_timeout
[123] Fix | Delete
attr_accessor :write_timeout
[124] Fix | Delete
attr_accessor :continue_timeout
[125] Fix | Delete
attr_accessor :debug_output
[126] Fix | Delete
[127] Fix | Delete
def inspect
[128] Fix | Delete
"#<#{self.class} io=#{@io}>"
[129] Fix | Delete
end
[130] Fix | Delete
[131] Fix | Delete
def eof?
[132] Fix | Delete
@io.eof?
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
def closed?
[136] Fix | Delete
@io.closed?
[137] Fix | Delete
end
[138] Fix | Delete
[139] Fix | Delete
def close
[140] Fix | Delete
@io.close
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
#
[144] Fix | Delete
# Read
[145] Fix | Delete
#
[146] Fix | Delete
[147] Fix | Delete
public
[148] Fix | Delete
[149] Fix | Delete
def read(len, dest = ''.b, ignore_eof = false)
[150] Fix | Delete
LOG "reading #{len} bytes..."
[151] Fix | Delete
read_bytes = 0
[152] Fix | Delete
begin
[153] Fix | Delete
while read_bytes + @rbuf.size < len
[154] Fix | Delete
s = rbuf_consume(@rbuf.size)
[155] Fix | Delete
read_bytes += s.size
[156] Fix | Delete
dest << s
[157] Fix | Delete
rbuf_fill
[158] Fix | Delete
end
[159] Fix | Delete
s = rbuf_consume(len - read_bytes)
[160] Fix | Delete
read_bytes += s.size
[161] Fix | Delete
dest << s
[162] Fix | Delete
rescue EOFError
[163] Fix | Delete
raise unless ignore_eof
[164] Fix | Delete
end
[165] Fix | Delete
LOG "read #{read_bytes} bytes"
[166] Fix | Delete
dest
[167] Fix | Delete
end
[168] Fix | Delete
[169] Fix | Delete
def read_all(dest = ''.b)
[170] Fix | Delete
LOG 'reading all...'
[171] Fix | Delete
read_bytes = 0
[172] Fix | Delete
begin
[173] Fix | Delete
while true
[174] Fix | Delete
s = rbuf_consume(@rbuf.size)
[175] Fix | Delete
read_bytes += s.size
[176] Fix | Delete
dest << s
[177] Fix | Delete
rbuf_fill
[178] Fix | Delete
end
[179] Fix | Delete
rescue EOFError
[180] Fix | Delete
;
[181] Fix | Delete
end
[182] Fix | Delete
LOG "read #{read_bytes} bytes"
[183] Fix | Delete
dest
[184] Fix | Delete
end
[185] Fix | Delete
[186] Fix | Delete
def readuntil(terminator, ignore_eof = false)
[187] Fix | Delete
begin
[188] Fix | Delete
until idx = @rbuf.index(terminator)
[189] Fix | Delete
rbuf_fill
[190] Fix | Delete
end
[191] Fix | Delete
return rbuf_consume(idx + terminator.size)
[192] Fix | Delete
rescue EOFError
[193] Fix | Delete
raise unless ignore_eof
[194] Fix | Delete
return rbuf_consume(@rbuf.size)
[195] Fix | Delete
end
[196] Fix | Delete
end
[197] Fix | Delete
[198] Fix | Delete
def readline
[199] Fix | Delete
readuntil("\n").chop
[200] Fix | Delete
end
[201] Fix | Delete
[202] Fix | Delete
private
[203] Fix | Delete
[204] Fix | Delete
BUFSIZE = 1024 * 16
[205] Fix | Delete
[206] Fix | Delete
def rbuf_fill
[207] Fix | Delete
tmp = @rbuf.empty? ? @rbuf : nil
[208] Fix | Delete
case rv = @io.read_nonblock(BUFSIZE, tmp, exception: false)
[209] Fix | Delete
when String
[210] Fix | Delete
return if rv.equal?(tmp)
[211] Fix | Delete
@rbuf << rv
[212] Fix | Delete
rv.clear
[213] Fix | Delete
return
[214] Fix | Delete
when :wait_readable
[215] Fix | Delete
(io = @io.to_io).wait_readable(@read_timeout) or raise Net::ReadTimeout.new(io)
[216] Fix | Delete
# continue looping
[217] Fix | Delete
when :wait_writable
[218] Fix | Delete
# OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
[219] Fix | Delete
# http://www.openssl.org/support/faq.html#PROG10
[220] Fix | Delete
(io = @io.to_io).wait_writable(@read_timeout) or raise Net::ReadTimeout.new(io)
[221] Fix | Delete
# continue looping
[222] Fix | Delete
when nil
[223] Fix | Delete
raise EOFError, 'end of file reached'
[224] Fix | Delete
end while true
[225] Fix | Delete
end
[226] Fix | Delete
[227] Fix | Delete
def rbuf_consume(len)
[228] Fix | Delete
if len == @rbuf.size
[229] Fix | Delete
s = @rbuf
[230] Fix | Delete
@rbuf = ''.b
[231] Fix | Delete
else
[232] Fix | Delete
s = @rbuf.slice!(0, len)
[233] Fix | Delete
end
[234] Fix | Delete
@debug_output << %Q[-> #{s.dump}\n] if @debug_output
[235] Fix | Delete
s
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
#
[239] Fix | Delete
# Write
[240] Fix | Delete
#
[241] Fix | Delete
[242] Fix | Delete
public
[243] Fix | Delete
[244] Fix | Delete
def write(*strs)
[245] Fix | Delete
writing {
[246] Fix | Delete
write0(*strs)
[247] Fix | Delete
}
[248] Fix | Delete
end
[249] Fix | Delete
[250] Fix | Delete
alias << write
[251] Fix | Delete
[252] Fix | Delete
def writeline(str)
[253] Fix | Delete
writing {
[254] Fix | Delete
write0 str + "\r\n"
[255] Fix | Delete
}
[256] Fix | Delete
end
[257] Fix | Delete
[258] Fix | Delete
private
[259] Fix | Delete
[260] Fix | Delete
def writing
[261] Fix | Delete
@written_bytes = 0
[262] Fix | Delete
@debug_output << '<- ' if @debug_output
[263] Fix | Delete
yield
[264] Fix | Delete
@debug_output << "\n" if @debug_output
[265] Fix | Delete
bytes = @written_bytes
[266] Fix | Delete
@written_bytes = nil
[267] Fix | Delete
bytes
[268] Fix | Delete
end
[269] Fix | Delete
[270] Fix | Delete
def write0(*strs)
[271] Fix | Delete
@debug_output << strs.map(&:dump).join if @debug_output
[272] Fix | Delete
orig_written_bytes = @written_bytes
[273] Fix | Delete
strs.each_with_index do |str, i|
[274] Fix | Delete
need_retry = true
[275] Fix | Delete
case len = @io.write_nonblock(str, exception: false)
[276] Fix | Delete
when Integer
[277] Fix | Delete
@written_bytes += len
[278] Fix | Delete
len -= str.bytesize
[279] Fix | Delete
if len == 0
[280] Fix | Delete
if strs.size == i+1
[281] Fix | Delete
return @written_bytes - orig_written_bytes
[282] Fix | Delete
else
[283] Fix | Delete
need_retry = false
[284] Fix | Delete
# next string
[285] Fix | Delete
end
[286] Fix | Delete
elsif len < 0
[287] Fix | Delete
str = str.byteslice(len, -len)
[288] Fix | Delete
else # len > 0
[289] Fix | Delete
need_retry = false
[290] Fix | Delete
# next string
[291] Fix | Delete
end
[292] Fix | Delete
# continue looping
[293] Fix | Delete
when :wait_writable
[294] Fix | Delete
(io = @io.to_io).wait_writable(@write_timeout) or raise Net::WriteTimeout.new(io)
[295] Fix | Delete
# continue looping
[296] Fix | Delete
end while need_retry
[297] Fix | Delete
end
[298] Fix | Delete
end
[299] Fix | Delete
[300] Fix | Delete
#
[301] Fix | Delete
# Logging
[302] Fix | Delete
#
[303] Fix | Delete
[304] Fix | Delete
private
[305] Fix | Delete
[306] Fix | Delete
def LOG_off
[307] Fix | Delete
@save_debug_out = @debug_output
[308] Fix | Delete
@debug_output = nil
[309] Fix | Delete
end
[310] Fix | Delete
[311] Fix | Delete
def LOG_on
[312] Fix | Delete
@debug_output = @save_debug_out
[313] Fix | Delete
end
[314] Fix | Delete
[315] Fix | Delete
def LOG(msg)
[316] Fix | Delete
return unless @debug_output
[317] Fix | Delete
@debug_output << msg + "\n"
[318] Fix | Delete
end
[319] Fix | Delete
end
[320] Fix | Delete
[321] Fix | Delete
[322] Fix | Delete
class InternetMessageIO < BufferedIO #:nodoc: internal use only
[323] Fix | Delete
def initialize(*, **)
[324] Fix | Delete
super
[325] Fix | Delete
@wbuf = nil
[326] Fix | Delete
end
[327] Fix | Delete
[328] Fix | Delete
#
[329] Fix | Delete
# Read
[330] Fix | Delete
#
[331] Fix | Delete
[332] Fix | Delete
def each_message_chunk
[333] Fix | Delete
LOG 'reading message...'
[334] Fix | Delete
LOG_off()
[335] Fix | Delete
read_bytes = 0
[336] Fix | Delete
while (line = readuntil("\r\n")) != ".\r\n"
[337] Fix | Delete
read_bytes += line.size
[338] Fix | Delete
yield line.delete_prefix('.')
[339] Fix | Delete
end
[340] Fix | Delete
LOG_on()
[341] Fix | Delete
LOG "read message (#{read_bytes} bytes)"
[342] Fix | Delete
end
[343] Fix | Delete
[344] Fix | Delete
# *library private* (cannot handle 'break')
[345] Fix | Delete
def each_list_item
[346] Fix | Delete
while (str = readuntil("\r\n")) != ".\r\n"
[347] Fix | Delete
yield str.chop
[348] Fix | Delete
end
[349] Fix | Delete
end
[350] Fix | Delete
[351] Fix | Delete
def write_message_0(src)
[352] Fix | Delete
prev = @written_bytes
[353] Fix | Delete
each_crlf_line(src) do |line|
[354] Fix | Delete
write0 dot_stuff(line)
[355] Fix | Delete
end
[356] Fix | Delete
@written_bytes - prev
[357] Fix | Delete
end
[358] Fix | Delete
[359] Fix | Delete
#
[360] Fix | Delete
# Write
[361] Fix | Delete
#
[362] Fix | Delete
[363] Fix | Delete
def write_message(src)
[364] Fix | Delete
LOG "writing message from #{src.class}"
[365] Fix | Delete
LOG_off()
[366] Fix | Delete
len = writing {
[367] Fix | Delete
using_each_crlf_line {
[368] Fix | Delete
write_message_0 src
[369] Fix | Delete
}
[370] Fix | Delete
}
[371] Fix | Delete
LOG_on()
[372] Fix | Delete
LOG "wrote #{len} bytes"
[373] Fix | Delete
len
[374] Fix | Delete
end
[375] Fix | Delete
[376] Fix | Delete
def write_message_by_block(&block)
[377] Fix | Delete
LOG 'writing message from block'
[378] Fix | Delete
LOG_off()
[379] Fix | Delete
len = writing {
[380] Fix | Delete
using_each_crlf_line {
[381] Fix | Delete
begin
[382] Fix | Delete
block.call(WriteAdapter.new(self, :write_message_0))
[383] Fix | Delete
rescue LocalJumpError
[384] Fix | Delete
# allow `break' from writer block
[385] Fix | Delete
end
[386] Fix | Delete
}
[387] Fix | Delete
}
[388] Fix | Delete
LOG_on()
[389] Fix | Delete
LOG "wrote #{len} bytes"
[390] Fix | Delete
len
[391] Fix | Delete
end
[392] Fix | Delete
[393] Fix | Delete
private
[394] Fix | Delete
[395] Fix | Delete
def dot_stuff(s)
[396] Fix | Delete
s.sub(/\A\./, '..')
[397] Fix | Delete
end
[398] Fix | Delete
[399] Fix | Delete
def using_each_crlf_line
[400] Fix | Delete
@wbuf = ''.b
[401] Fix | Delete
yield
[402] Fix | Delete
if not @wbuf.empty? # unterminated last line
[403] Fix | Delete
write0 dot_stuff(@wbuf.chomp) + "\r\n"
[404] Fix | Delete
elsif @written_bytes == 0 # empty src
[405] Fix | Delete
write0 "\r\n"
[406] Fix | Delete
end
[407] Fix | Delete
write0 ".\r\n"
[408] Fix | Delete
@wbuf = nil
[409] Fix | Delete
end
[410] Fix | Delete
[411] Fix | Delete
def each_crlf_line(src)
[412] Fix | Delete
buffer_filling(@wbuf, src) do
[413] Fix | Delete
while line = @wbuf.slice!(/\A[^\r\n]*(?:\n|\r(?:\n|(?!\z)))/)
[414] Fix | Delete
yield line.chomp("\n") + "\r\n"
[415] Fix | Delete
end
[416] Fix | Delete
end
[417] Fix | Delete
end
[418] Fix | Delete
[419] Fix | Delete
def buffer_filling(buf, src)
[420] Fix | Delete
case src
[421] Fix | Delete
when String # for speeding up.
[422] Fix | Delete
0.step(src.size - 1, 1024) do |i|
[423] Fix | Delete
buf << src[i, 1024]
[424] Fix | Delete
yield
[425] Fix | Delete
end
[426] Fix | Delete
when File # for speeding up.
[427] Fix | Delete
while s = src.read(1024)
[428] Fix | Delete
buf << s
[429] Fix | Delete
yield
[430] Fix | Delete
end
[431] Fix | Delete
else # generic reader
[432] Fix | Delete
src.each do |str|
[433] Fix | Delete
buf << str
[434] Fix | Delete
yield if buf.size > 1024
[435] Fix | Delete
end
[436] Fix | Delete
yield unless buf.empty?
[437] Fix | Delete
end
[438] Fix | Delete
end
[439] Fix | Delete
end
[440] Fix | Delete
[441] Fix | Delete
[442] Fix | Delete
#
[443] Fix | Delete
# The writer adapter class
[444] Fix | Delete
#
[445] Fix | Delete
class WriteAdapter
[446] Fix | Delete
def initialize(socket, method)
[447] Fix | Delete
@socket = socket
[448] Fix | Delete
@method_id = method
[449] Fix | Delete
end
[450] Fix | Delete
[451] Fix | Delete
def inspect
[452] Fix | Delete
"#<#{self.class} socket=#{@socket.inspect}>"
[453] Fix | Delete
end
[454] Fix | Delete
[455] Fix | Delete
def write(str)
[456] Fix | Delete
@socket.__send__(@method_id, str)
[457] Fix | Delete
end
[458] Fix | Delete
[459] Fix | Delete
alias print write
[460] Fix | Delete
[461] Fix | Delete
def <<(str)
[462] Fix | Delete
write str
[463] Fix | Delete
self
[464] Fix | Delete
end
[465] Fix | Delete
[466] Fix | Delete
def puts(str = '')
[467] Fix | Delete
write str.chomp("\n") + "\n"
[468] Fix | Delete
end
[469] Fix | Delete
[470] Fix | Delete
def printf(*args)
[471] Fix | Delete
write sprintf(*args)
[472] Fix | Delete
end
[473] Fix | Delete
end
[474] Fix | Delete
[475] Fix | Delete
[476] Fix | Delete
class ReadAdapter #:nodoc: internal use only
[477] Fix | Delete
def initialize(block)
[478] Fix | Delete
@block = block
[479] Fix | Delete
end
[480] Fix | Delete
[481] Fix | Delete
def inspect
[482] Fix | Delete
"#<#{self.class}>"
[483] Fix | Delete
end
[484] Fix | Delete
[485] Fix | Delete
def <<(str)
[486] Fix | Delete
call_block(str, &@block) if @block
[487] Fix | Delete
end
[488] Fix | Delete
[489] Fix | Delete
private
[490] Fix | Delete
[491] Fix | Delete
# This method is needed because @block must be called by yield,
[492] Fix | Delete
# not Proc#call. You can see difference when using `break' in
[493] Fix | Delete
# the block.
[494] Fix | Delete
def call_block(str)
[495] Fix | Delete
yield str
[496] Fix | Delete
end
[497] Fix | Delete
end
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function