Class: Watobo::Interceptor::CarverRule

Inherits:
Object
  • Object
show all
Defined in:
lib/watobo/core/intercept_carver.rb

Instance Method Summary collapse

Constructor Details

#initialize(parms) ⇒ CarverRule



116
117
118
119
120
121
122
# File 'lib/watobo/core/intercept_carver.rb', line 116

def initialize(parms)
  @settings = Hash.new
  [:action, :location, :pattern, :content, :filter].each do |k|
    @settings[k] = parms[k]
  end

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



126
127
128
129
130
# File 'lib/watobo/core/intercept_carver.rb', line 126

def method_missing(name, *args, &block)
  # puts "* instance method missing (#{name})"
  @settings.has_key? name.to_sym || super
  @settings[name.to_sym]
end

Instance Method Details

#action_nameObject



5
6
7
# File 'lib/watobo/core/intercept_carver.rb', line 5

def action_name
  action.to_s
end

#apply(item, flags) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/watobo/core/intercept_carver.rb', line 88

def apply(item, flags)
  begin
    unless filter.nil?
    return false unless filter.match?(item, flags)
    end
    res = case action
    when :flag
      puts "set flag >> #{content} (#{content.class})"
      flags << :request
      true
    when :inject
      inject_content(item, location, pattern, content)
    when :rewrite
      puts "REWRITE"
      puts "Location: #{location}"
      puts "Pattern: #{pattern}"
     # puts "Content: #{content}"
      rewrite(item, location, pattern, content)
    else
      true
    end
    return res
  rescue => bang
    puts bang
    puts bang.backtrace
  end
end

#content_nameObject



33
34
35
# File 'lib/watobo/core/intercept_carver.rb', line 33

def content_name
  content
end

#filter_nameObject



17
18
19
20
# File 'lib/watobo/core/intercept_carver.rb', line 17

def filter_name
  # return "NA" if filter.nil?
  return filter.class.to_s
end

#filtersObject



28
29
30
31
# File 'lib/watobo/core/intercept_carver.rb', line 28

def filters
  return [] unless filter.respond_to? :list
  filter.list
end

#location_nameObject



9
10
11
# File 'lib/watobo/core/intercept_carver.rb', line 9

def location_name
  location.to_s
end

#pattern_nameObject



13
14
15
# File 'lib/watobo/core/intercept_carver.rb', line 13

def pattern_name
  Regexp.quote pattern
end

#rewrite(item, l, p, c) ⇒ Object

rewrite options item location pattern content



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/watobo/core/intercept_carver.rb', line 43

def rewrite(item, l, p, c)
  res = false
  case l
  when :replace_all
    if File.exist? c
      begin
       item.replace Watobo::Utils.string2response(File.open(c,"rb").read)              
      rescue => bang
        puts bang
        puts bang.backtrace
      end
    else
      puts "Could not find file > #{c}"
    end
    
  when :body
    if item.respond_to? :body
      if p.upcase == :ALL
      res = item.replace_body(c)
      else
        puts "* rewrite body ..."
      res = item.rewrite_body(p,c)
      end
    end
  when :http_parm
    1
  when :cookie
    1
  when :url
    if item.respond_to? :url
      item.first.gsub!(/#{p}/, c)
    end
  when :header
    puts "REPLACE HEADER"
    item.each_with_index do |line, index|
      if line =~ /#{p}/
        item[index] = "#{c.strip}\r\n"
      end
      break if line.strip.empty?
    end
    res = item  
  end
  res
end

#set_filter(filter_chain) ⇒ Object



22
23
24
25
26
# File 'lib/watobo/core/intercept_carver.rb', line 22

def set_filter(filter_chain)
  puts "* set filter_chain"
  puts filter_chain.class
  @settings[:filter] = filter_chain
end