class FPM::Fry::StreamParser

Attributes

out[RW]

Public Class Methods

new(out, err) click to toggle source
# File lib/fpm/fry/stream_parser.rb, line 64
def initialize(out, err)
  @out, @err = out, err
  @state = :null
  @left = 0
  @streams = { 1 => out, 2 => err }
end

Public Instance Methods

new(stack) click to toggle source
# File lib/fpm/fry/stream_parser.rb, line 71
def new(stack)
  Instance.new(stack, self)
end
parse(socket) click to toggle source
# File lib/fpm/fry/stream_parser.rb, line 75
def parse(socket)
  loop do
    type = read_exactly(socket,4){|part|
      if part.bytesize == 0
        return
      else
        raise ShortRead
      end
    }.unpack("c".freeze)[0]
    stream = streams.fetch(type){ raise ArgumentError, "Wrong stream type: #{type}"}
    len  = read_exactly(socket,4).unpack('I>')[0]
    while len > 0
      chunk = socket.read([64,len].min)
      raise ShortRead if chunk.nil?
      len -= chunk.bytesize
      stream.write(chunk)
    end
  end
end
parse_chunked(socket) click to toggle source
# File lib/fpm/fry/stream_parser.rb, line 113
def parse_chunked(socket)
  loop do
    line = socket.readline
    chunk_size = line.chomp.to_i(16)
    break if chunk_size.zero?
    chunk = socket.read(chunk_size)
    parse(StringIO.new(chunk))
    line = socket.readline
  end
end
read_exactly(socket, len) { |buf| ... } click to toggle source
# File lib/fpm/fry/stream_parser.rb, line 95
def read_exactly(socket, len)
  buf = ""
  left = len
  while left != 0
    read = socket.read(left)
    if read.nil?
      if block_given?
        yield buf
      else
        raise ShortRead
      end
    end
    buf << read
    left = len - buf.bytesize
  end
  return buf
end