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


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


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

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

#find(idx) ⇒ Object


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

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
# 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(':')
    next if not val
    self.headers << [ var.to_s.strip, val.to_s.strip ]
    prev = self.headers.length - 1
  end
end

#remove(idx) ⇒ Object


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

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


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

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

#to_sObject


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

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