Module: Dap::Filter::HTMLGhetto

Included in:
FilterHTMLIframes, FilterHTMLLinks
Defined in:
lib/dap/filter/http.rb

Overview

Dirty element extractor, works around memory issues with Nokogiri

Instance Method Summary collapse

Instance Method Details

#extract_elements(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dap/filter/http.rb', line 12

def extract_elements(data)
  @coder ||= HTMLEntities.new
  res = []
  data.
    to_s.
    encode('UTF-8', invalid: :replace, undef: :replace, replace: '').
    scan(/<([^>]+)>/m).each do |e|

    e = e.first

    # Skip closing tags
    next if e[0,1] == "/"

    # Get the name vs attributes
    name, astr = e.split(/\s+/, 2).map{|x| x.to_s }
    astr ||= ''

    # Skip non-alpha elements
    next unless name =~ /^[a-zA-Z]/

    # Convert newlines to spaces & strip trailing />
    astr = astr.gsub(/\n/, ' ').sub(/\/$/, '')

    o = { name: name }

    begin
     Shellwords.shellwords(astr).each do |attr_str|
        aname, avalue = attr_str.split('=', 2).map{|x| x.to_s.strip }
        avalue = avalue.to_s.gsub(/^\"|"$/, '')
        o[aname.downcase] = @coder.decode(avalue)
      end
    rescue ::Interrupt
      raise $!
    rescue ::Exception
      # If shellwords couldn't parse it, split on space instead
      astr.to_s.split(/\s+/).each do |attr_str|
        aname, avalue = attr_str.split('=', 2).map{|x| x.to_s.strip }
        avalue = avalue.to_s.gsub(/^\"|"$/, '')
        o[aname.downcase] = @coder.decode(avalue)
      end
    end
    res << o
  end

  res
end