Class: Rexlite::MIME::Header

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = '') ⇒ Header

Returns a new instance of Header.



8
9
10
11
# File 'lib/nexpose/rexlite/mime/header.rb', line 8

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

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/nexpose/rexlite/mime/header.rb', line 6

def headers
  @headers
end

Instance Method Details

#add(var, val) ⇒ Object



56
57
58
59
# File 'lib/nexpose/rexlite/mime/header.rb', line 56

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

#find(idx) ⇒ Object



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

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



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

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



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

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



51
52
53
54
# File 'lib/nexpose/rexlite/mime/header.rb', line 51

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

#to_sObject



34
35
36
# File 'lib/nexpose/rexlite/mime/header.rb', line 34

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