Class: Rex::MIME::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/mime/header.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = '') ⇒ Header

Returns a new instance of Header.



10
11
12
13
# File 'lib/rex/mime/header.rb', line 10

def initialize(data='')
  self.headers = []
  parse(data)
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/rex/mime/header.rb', line 8

def headers
  @headers
end

Instance Method Details

#add(var, val) ⇒ Object



58
59
60
61
# File 'lib/rex/mime/header.rb', line 58

def add(var, val)
  self.headers << [var, val]
  self.headers[-1]
end

#find(idx) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rex/mime/header.rb', line 40

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

#parse(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rex/mime/header.rb', line 15

def parse(data)
  prev = nil
  data.gsub("\r", '').split("\n").each do |line|

    # 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

    var, val = line.split(':', 2)
    next if val.nil?

    self.headers << [ var.to_s.strip, val.to_s.strip ]
    prev = self.headers.length - 1
  end
end

#remove(idx) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rex/mime/header.rb', line 63

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

#set(var, val) ⇒ Object



53
54
55
56
# File 'lib/rex/mime/header.rb', line 53

def set(var, val)
  hdr = self.find(var) || self.add(var, '')
  hdr[1] = val
end

#to_sObject



36
37
38
# File 'lib/rex/mime/header.rb', line 36

def to_s
  self.headers.map{ |pair| "#{pair[0]}: #{pair[1]}\r\n" }.join
end