class NchanTools::Subscriber::MultiparMixedClient::MultipartMixedParser

Attributes

bound[RW]
buf[RW]
finished[RW]

Public Class Methods

new(multipart_header) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1303
def initialize(multipart_header)
  matches=/^multipart\/mixed; boundary=(?<boundary>.*)/.match multipart_header
  raise SubscriberError, "malformed Content-Type multipart/mixed header" unless matches[:boundary]
  @bound = matches[:boundary]
  @buf = ""
  @preambled = false
  @headered = nil
  @headers = {}
  @ninished = nil
end

Public Instance Methods

<<(chunk) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1321
def <<(chunk)
  @buf << chunk
  #puts @buf
  repeat = true
  while repeat do
    if !@preambled && @buf.slice!(/^--#{Regexp.escape @bound}/)
      @finished = nil
      @preambled = true
      @headered = nil
    end
    if @preambled && @buf.slice!(/^(\r\n(.*?))?\r\n\r\n/m)
      @headered = true
      ($~[2]).each_line do |l|
        if l.match(/(?<name>[^:]+):\s(?<val>[^\r\n]*)/)
          @headers[$~[:name]]=$~[:val]
        end
      end
    else
      repeat = false
    end
    
    if @headered && @buf.slice!(/^(.*?)\r\n--#{Regexp.escape @bound}/m)
      @on_part.call @headers, $~[1]
      @headered = nil
      @headers.clear
      repeat = true
    else
      repeat = false
    end
    
    if (@preambled && !@headered && @buf.slice!(/^--\r\n/)) ||
      (!@preambled && @buf.slice!(/^--#{Regexp.escape @bound}--\r\n/))
      @on_finish.call
      repeat = false
    end
  end
end
on_finish(&block) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1317
def on_finish(&block)
  @on_finish = block
end
on_part(&block) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1314
def on_part(&block)
  @on_part = block
end