class MIME::Content::Application::Http::Parser

Parser for Http Media

Constants

HMD_RGX

Public Class Methods

new(target) click to toggle source
# File lib/safrano/multipart.rb, line 492
def initialize(target)
  @state = :http
  @lines = []
  @body_lines = []
  @target = target
end

Public Instance Methods

addline(line) click to toggle source
# File lib/safrano/multipart.rb, line 499
def addline(line)
  @lines << line
end
parse(level: 0) click to toggle source
# File lib/safrano/multipart.rb, line 530
def parse(level: 0)
  return unless @lines

  @lines.each do |line|
    case @state
    when :http
      parse_http(line)
    when :hd
      parse_head(line)
    when :b
      @body_lines << line
    end
  end

  @target.content.content = @body_lines.join
  @target.content.level = level
  @target.level = level
  @lines = nil
  @body_lines = nil
  @target
end
parse_head(line) click to toggle source
# File lib/safrano/multipart.rb, line 519
def parse_head(line)
  if (hmd = HMD_RGX.match(line))
    @target.content.hd[hmd[1].downcase] = hmd[2].strip
  elsif CRLF == line
    @state = :b
  else
    @body_lines << line
    @state = :b
  end
end
parse_http(line) click to toggle source
# File lib/safrano/multipart.rb, line 503
def parse_http(line)
  if (hmd = HMD_RGX.match(line))
    @target.hd[hmd[1].downcase] = hmd[2].strip
  elsif (mdht = HTTP_R_RGX.match(line))
    @state = :hd
    @target.content = MIME::Content::Application::HttpReq.new
    @target.content.http_method = mdht[1]
    @target.content.uri = mdht[2]
  #  HTTP 1.1 status line  --> HttpResp.new
  elsif (mdht = HTTP_RESP_RGX.match(line))
    @state = :hd
    @target.content = MIME::Content::Application::HttpResp.new
    @target.content.status = mdht[1]
  end
end