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

Returns a new instance of CarverRule.



137
138
139
140
141
142
143
# File 'lib/watobo/core/intercept_carver.rb', line 137

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)



147
148
149
150
151
# File 'lib/watobo/core/intercept_carver.rb', line 147

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



26
27
28
# File 'lib/watobo/core/intercept_carver.rb', line 26

def action_name
  action.to_s
end

#apply(item, flags) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/watobo/core/intercept_carver.rb', line 109

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



54
55
56
# File 'lib/watobo/core/intercept_carver.rb', line 54

def content_name
  content
end

#filter_nameObject



38
39
40
41
# File 'lib/watobo/core/intercept_carver.rb', line 38

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

#filtersObject



49
50
51
52
# File 'lib/watobo/core/intercept_carver.rb', line 49

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

#location_nameObject



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

def location_name
  location.to_s
end

#pattern_nameObject



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

def pattern_name
  Regexp.quote pattern
end

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

rewrite options item location pattern content



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/watobo/core/intercept_carver.rb', line 64

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



43
44
45
46
47
# File 'lib/watobo/core/intercept_carver.rb', line 43

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