aboutsummaryrefslogtreecommitdiffstats
path: root/lib/net/protocol.rb
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-03-06 17:08:21 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-03-06 17:08:21 +0000
commit3eedf9156cf01751423a99ef2939ec81964155d2 (patch)
tree9cc29f6a4e68821dcdaa57058bf44871bcd4fd8c /lib/net/protocol.rb
parentdd53813e38e2839b08cc540df6296c392dbf3c25 (diff)
downloadruby-3eedf9156cf01751423a99ef2939ec81964155d2.tar.gz
* lib/net/http.rb: spin off https code again.
* lib/net/https.rb: new file. * ext/openssl/lib/net/https.rb: removed. moved to net/https with modifications. * ext/openssl/lib/net/protocol.rb: removed. merged with net/http. * lib/net/protocol.rb: new class BufferedIO. * lib/net/protocol.rb: InternetMessageIO < BufferedIO. * lib/net/protocol.rb: BufferedIO.new takes an IO. * lib/net/smtp.rb: follow InternetMessageIO's change. * lib/net/pop.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5908 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/net/protocol.rb')
-rw-r--r--lib/net/protocol.rb227
1 files changed, 103 insertions, 124 deletions
diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb
index 78fd8dc3d8..aaed060a51 100644
--- a/lib/net/protocol.rb
+++ b/lib/net/protocol.rb
@@ -46,61 +46,33 @@ module Net # :nodoc:
ProtocRetryError = ProtoRetriableError
- class InternetMessageIO #:nodoc: internal use only
- class << self
- alias open new
- end
-
- def initialize(addr, port, open_timeout = nil, read_timeout = nil, debug_output = nil)
- @address = addr
- @port = port
- @read_timeout = read_timeout
- @debug_output = debug_output
- @socket = nil
- @rbuf = nil # read buffer
- @wbuf = nil # write buffer
- connect open_timeout
- LOG 'opened'
- end
-
- def connect(open_timeout)
- LOG "opening connection to #{@address}..."
- timeout(open_timeout) {
- @socket = TCPsocket.new(@address, @port)
- }
+ class BufferedIO #:nodoc: internal use only
+ def initialize(io)
+ @io = io
+ @read_timeout = 60
+ @debug_output = nil
@rbuf = ''
end
- private :connect
- def close
- if @socket
- @socket.close
- LOG 'closed'
- else
- LOG 'close call for already closed socket'
- end
- @socket = nil
- @rbuf = ''
- end
+ attr_reader :io
+ attr_accessor :read_timeout
+ attr_accessor :debug_output
- def reopen(open_timeout = nil)
- LOG 'reopening...'
- close
- connect open_timeout
- LOG 'reopened'
+ def inspect
+ "#<#{self.class} io=#{@io}>"
end
def closed?
- not @socket
+ @io.closed?
end
- def inspect
- "#<#{self.class} #{closed?() ? 'closed' : 'opened'}>"
+ def close
+ @io.close
end
- ###
- ### READ
- ###
+ #
+ # Read
+ #
public
@@ -154,32 +126,13 @@ module Net # :nodoc:
readuntil("\n").chop
end
- def each_message_chunk
- LOG 'reading message...'
- LOG_off()
- read_bytes = 0
- while (line = readuntil("\r\n")) != ".\r\n"
- read_bytes += line.size
- yield line.sub(/\A\./, '')
- end
- LOG_on()
- LOG "read message (#{read_bytes} bytes)"
- end
-
- # *library private* (cannot handle 'break')
- def each_list_item
- while (str = readuntil("\r\n")) != ".\r\n"
- yield str.chop
- end
- end
-
private
def rbuf_fill
- until IO.select([@socket], nil, nil, @read_timeout)
+ until IO.select([@io], nil, nil, @read_timeout)
raise TimeoutError, "socket read timeout (#{@read_timeout} sec)"
end
- @rbuf << @socket.sysread(1024)
+ @rbuf << @io.sysread(1024)
end
def rbuf_consume(len)
@@ -188,9 +141,9 @@ module Net # :nodoc:
s
end
- ###
- ### WRITE
- ###
+ #
+ # Write
+ #
public
@@ -206,6 +159,88 @@ module Net # :nodoc:
}
end
+ private
+
+ def writing
+ @written_bytes = 0
+ @debug_output << '<- ' if @debug_output
+ yield
+ @debug_output << "\n" if @debug_output
+ bytes = @written_bytes
+ @written_bytes = nil
+ bytes
+ end
+
+ def write0(str)
+ @debug_output << str.dump if @debug_output
+ len = @io.write(str)
+ @written_bytes += len
+ len
+ end
+
+ #
+ # Logging
+ #
+
+ private
+
+ def LOG_off
+ @save_debug_out = @debug_output
+ @debug_output = nil
+ end
+
+ def LOG_on
+ @debug_output = @save_debug_out
+ end
+
+ def LOG(msg)
+ return unless @debug_output
+ @debug_output << msg + "\n"
+ end
+ end
+
+
+ class InternetMessageIO < BufferedIO #:nodoc: internal use only
+ def initialize(io)
+ super
+ @wbuf = nil
+ end
+
+ #
+ # Read
+ #
+
+ def each_message_chunk
+ LOG 'reading message...'
+ LOG_off()
+ read_bytes = 0
+ while (line = readuntil("\r\n")) != ".\r\n"
+ read_bytes += line.size
+ yield line.sub(/\A\./, '')
+ end
+ LOG_on()
+ LOG "read message (#{read_bytes} bytes)"
+ end
+
+ # *library private* (cannot handle 'break')
+ def each_list_item
+ while (str = readuntil("\r\n")) != ".\r\n"
+ yield str.chop
+ end
+ end
+
+ def write_message_0(src)
+ prev = @written_bytes
+ each_crlf_line(src) do |line|
+ write0 line.sub(/\A\./, '..')
+ end
+ @written_bytes - prev
+ end
+
+ #
+ # Write
+ #
+
def write_message(src)
LOG "writing message from #{src.class}"
LOG_off()
@@ -238,42 +273,6 @@ module Net # :nodoc:
private
- def writing
- @written_bytes = 0
- @debug_output << '<- ' if @debug_output
- yield
- @socket.flush
- @debug_output << "\n" if @debug_output
- bytes = @written_bytes
- @written_bytes = nil
- bytes
- end
-
- def write0(str)
- @debug_output << str.dump if @debug_output
- len = @socket.write(str)
- @written_bytes += len
- len
- end
-
- #
- # Reads string from src calling :each, and write to @socket.
- # Escapes '.' on the each line head.
- #
- def write_message_0(src)
- prev = @written_bytes
- each_crlf_line(src) do |line|
- if line[0] == ?.
- then write0 '.' + line
- else write0 line
- end
- end
- @written_bytes - prev
- end
-
- #
- # setup @wbuf for each_crlf_line.
- #
def using_each_crlf_line
@wbuf = ''
yield
@@ -315,26 +314,6 @@ module Net # :nodoc:
yield unless buf.empty?
end
end
-
- ###
- ### DEBUG
- ###
-
- private
-
- def LOG_off
- @save_debug_out = @debug_output
- @debug_output = nil
- end
-
- def LOG_on
- @debug_output = @save_debug_out
- end
-
- def LOG(msg)
- return unless @debug_output
- @debug_output << msg + "\n"
- end
end