2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2007-04-02 03:58:11 +00:00
|
|
|
module Rex
|
|
|
|
module MIME
|
|
|
|
class Header
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
require 'rex/text'
|
2007-04-02 03:58:11 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
attr_accessor :headers
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def initialize(data='')
|
|
|
|
self.headers = []
|
|
|
|
parse(data)
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def parse(data)
|
|
|
|
prev = nil
|
|
|
|
data.gsub("\r", '').split("\n").each do |line|
|
2007-04-02 03:58:11 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Handle header folding
|
|
|
|
if (line =~ /^\s+/)
|
|
|
|
# Ignore if there is no previous header
|
|
|
|
next if not prev
|
|
|
|
next if not self.headers[prev]
|
|
|
|
self.headers[prev][1] << line.strip
|
|
|
|
next
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
var,val = line.split(':')
|
|
|
|
next if not val
|
|
|
|
self.headers << [ var.to_s.strip, val.to_s.strip ]
|
|
|
|
prev = self.headers.length - 1
|
|
|
|
end
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def to_s
|
|
|
|
self.headers.map{ |pair| "#{pair[0]}: #{pair[1]}\r\n" }.join
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def find(idx)
|
|
|
|
if (idx.class == ::Fixnum)
|
|
|
|
return self.headers[idx]
|
|
|
|
else
|
|
|
|
self.headers.each do |pair|
|
|
|
|
if (pair[0] == idx.to_s)
|
|
|
|
return pair
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def set(var, val)
|
|
|
|
hdr = self.find(var) || self.add(var, '')
|
|
|
|
hdr[1] = val
|
|
|
|
end
|
2007-04-02 03:58:11 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def add(var, val)
|
|
|
|
self.headers << [var, val]
|
|
|
|
self.headers[-1]
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def remove(idx)
|
|
|
|
if (idx.class == ::Fixnum)
|
|
|
|
self.headers.delete_at(idx)
|
|
|
|
else
|
|
|
|
self.headers.each_index do |i|
|
|
|
|
pair = self.headers[i]
|
|
|
|
if (pair[0] == idx.to_s)
|
|
|
|
self.headers.delete_at(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-04-02 03:58:11 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2010-05-09 16:53:16 +00:00
|
|
|
end
|
|
|
|
|