Module: GetsSafe

Defined in:
lib/smtpserver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#maxlengthObject

Returns the value of attribute maxlength.



31
32
33
# File 'lib/smtpserver.rb', line 31

def maxlength
  @maxlength
end

#timeoutObject

Returns the value of attribute timeout.



31
32
33
# File 'lib/smtpserver.rb', line 31

def timeout
  @timeout
end

Instance Method Details

#gets_safe(rs = nil, timeout = @timeout, maxlength = @maxlength) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smtpserver.rb', line 8

def gets_safe(rs = nil, timeout = @timeout, maxlength = @maxlength)
  rs = $/ unless rs
  f = self.kind_of?(IO) ? self : STDIN
  @gets_safe_buf = '' unless @gets_safe_buf
  until @gets_safe_buf.include? rs do
    if maxlength and @gets_safe_buf.length > maxlength then
      raise Errno::E2BIG, 'too long'
    end
    if IO.select([f], nil, nil, timeout) == nil then
      raise Errno::ETIMEDOUT, 'timeout exceeded'
    end
    begin
      @gets_safe_buf << f.sysread(4096)
    rescue EOFError, Errno::ECONNRESET
      return @gets_safe_buf.empty? ? nil : @gets_safe_buf.slice!(0..-1)
    end
  end
  p = @gets_safe_buf.index rs
  if maxlength and p > maxlength then
    raise Errno::E2BIG, 'too long'
  end
  return @gets_safe_buf.slice!(0, p+rs.length)
end