Class: Watobo::Gui::ChatDiffViewer

Inherits:
FXDialogBox
  • Object
show all
Includes:
Icons
Defined in:
lib/watobo/gui/chat_diff.rb

Constant Summary

Constants included from Icons

Icons::ICON_ADD_PROJECT, Icons::ICON_BROWSER_MEDIUM, Icons::ICON_BROWSER_SMALL, Icons::ICON_BTN_DOWN, Icons::ICON_BTN_UP, Icons::ICON_CB_CHECKED, Icons::ICON_CB_CHECKED_ORANGE, Icons::ICON_CB_UNCHECKED, Icons::ICON_CONVERSATION, Icons::ICON_DASHBOARD, Icons::ICON_DIFFER, Icons::ICON_FOLDER, Icons::ICON_FOLDER_SMALL, Icons::ICON_FUZZER, Icons::ICON_FUZZER_MEDIUM, Icons::ICON_FUZZER_SMALL, Icons::ICON_FUZZ_FILTER, Icons::ICON_FUZZ_GENERATOR, Icons::ICON_FUZZ_TAG, Icons::ICON_HINTS, Icons::ICON_HINTS_INFO, Icons::ICON_HINTS_INFO_SMALL, Icons::ICON_HINTS_SMALL, Icons::ICON_INFO, Icons::ICON_INFO_INFO, Icons::ICON_INFO_INFO_SMALL, Icons::ICON_INFO_SMALL, Icons::ICON_INFO_USER, Icons::ICON_INFO_USER_SMALL, Icons::ICON_INTERCEPTOR, Icons::ICON_LOGIN_WIZZARD, Icons::ICON_MANUAL_REQUEST, Icons::ICON_MANUAL_REQUEST_MEDIUM, Icons::ICON_MANUAL_REQUEST_SMALL, Icons::ICON_PAUSE, Icons::ICON_PLUGIN, Icons::ICON_PROJECT, Icons::ICON_PROJECT_SMALL, Icons::ICON_REPORT, Icons::ICON_REQUEST, Icons::ICON_REQUEST_SMALL, Icons::ICON_SEND_REQUEST, Icons::ICON_SITE, Icons::ICON_SITE_SMALL, Icons::ICON_START, Icons::ICON_STOP, Icons::ICON_TOKEN, Icons::ICON_TRANSCODER, Icons::ICON_VULN, Icons::ICON_VULN_BP, Icons::ICON_VULN_BP_SMALL, Icons::ICON_VULN_CRITICAL, Icons::ICON_VULN_CRITICAL_SMALL, Icons::ICON_VULN_HIGH, Icons::ICON_VULN_HIGH_SMALL, Icons::ICON_VULN_LOW, Icons::ICON_VULN_LOW_SMALL, Icons::ICON_VULN_MEDIUM, Icons::ICON_VULN_MEDIUM_SMALL, Icons::ICON_VULN_SMALL, Icons::ICON_WATOBO, Icons::SIBERAS_ICON, Icons::TBL_ICON_LOCK, Icons::WATOBO_LOGO

Instance Method Summary collapse

Constructor Details

#initialize(owner, chat_orig, chat_new) ⇒ ChatDiffViewer

Returns a new instance of ChatDiffViewer.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/watobo/gui/chat_diff.rb', line 347

def initialize(owner, chat_orig, chat_new)
  # Invoke base class initialize function first
  super(owner, "Chat Differ", :opts => DECOR_ALL,:width=>800, :height=>600)
  self.icon = ICON_DIFFER
  @chat_orig = chat_orig
  @chat_new = chat_new
  @max_line_length = 80
  
  @requestBlocks = Hash.new
  @responseBlocks = Hash.new
  
  main = FXHorizontalFrame.new(self, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y, :padding => 0)
  left = FXVerticalFrame.new(main, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH, :width => 200, :padding => 0)
  
  @tabBook = FXTabBook.new(left, nil, 0, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_RIGHT)
  res_tab = FXTabItem.new(@tabBook, "Response", nil)
  res_frame = FXVerticalFrame.new(@tabBook, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_RAISED|FRAME_THICK)
  sunken = FXVerticalFrame.new(res_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, :padding => 0)
  @navResponseTable = FXTable.new(sunken, :opts => TABLE_COL_SIZABLE|TABLE_ROW_SIZABLE|LAYOUT_FILL_X|LAYOUT_FILL_Y|TABLE_READONLY|LAYOUT_SIDE_TOP, :padding => 2)
  @navResponseTable.connect(SEL_COMMAND, method(:onTableClick))
  
  req_tab = FXTabItem.new(@tabBook, "Request", nil)
  req_frame = FXVerticalFrame.new(@tabBook, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y, :padding => 0)
  sunken = FXVerticalFrame.new(req_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, :padding => 0)
  @navRequestTable = FXTable.new(sunken, :opts => FRAME_SUNKEN|TABLE_COL_SIZABLE|TABLE_ROW_SIZABLE|LAYOUT_FILL_X|LAYOUT_FILL_Y|TABLE_READONLY|LAYOUT_SIDE_TOP, :padding => 2)
  
  
  orig_frame = FXVerticalFrame.new(main, :opts => LAYOUT_FILL_Y|LAYOUT_FILL_X, :padding => 0)
  frame = FXVerticalFrame.new(orig_frame, :opts => LAYOUT_FILL_X)
  FXLabel.new(frame, "Original")
  
  @diff_orig = ChatDiffFrame.new(orig_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y, :diffType => DIFF_TYPE_ORIG)
  
  new_frame = FXVerticalFrame.new(main, :opts => LAYOUT_FILL_Y|LAYOUT_FILL_X, :padding => 0)
  frame = FXVerticalFrame.new(new_frame, :opts => LAYOUT_FILL_X)
  FXLabel.new(frame, "New")
  
  
  @diff_new = ChatDiffFrame.new(new_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y, :diffType => DIFF_TYPE_NEW)
  
  
  
  @tabBook.connect(SEL_COMMAND) {
    if @tabBook.current == 0 then
      showResponseDiff()
    else
      showRequestDiff()
    end
  }
  # normalize data before processing
  # * remove empty lines, binary data
  # * wrap lines after @max_line_length
  @normRequestOrig = normalizeData(chat_orig.request)
  @normResponseOrig = normalizeData(chat_orig.response)
  
  @normRequestNew = normalizeData(chat_new.request)
  @normResponseNew = normalizeData(chat_new.response)
  
       
  # diff normalized data
  @requestDiffs = Diff::LCS.diff( @normRequestOrig, @normRequestNew )
  @responseDiffs = Diff::LCS.diff( @normResponseOrig, @normResponseNew )
  
  # set diff blocks
  updateBlocks(@normRequestOrig, @normRequestNew, @requestBlocks, @requestDiffs)
  updateBlocks(@normResponseOrig, @normResponseNew, @responseBlocks, @responseDiffs)
  
  updateNavTables()
  
  showResponseDiff()
  
  
  
end

Instance Method Details

#adjustLine(line) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/watobo/gui/chat_diff.rb', line 141

def adjustLine(line)    
  result = []
  line.strip!
  if line.length > 0 then                    
    pos = 0
    while pos < line.length
      result.push line[pos..pos+@max_line_length-1]
      pos += @max_line_length
    end   
  end
  return result
end

#diffCollections(data_old, data_new, diffs) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/watobo/gui/chat_diff.rb', line 263

def diffCollections(data_old, data_new, diffs)
  file_length_difference = 0
  context_lines = 3
  raw_chunks = []
  collections = []
          
  return collections if diffs.empty?
  oldhunk = hunk = nil
  file_length_difference = 0
  diffs.each do |piece|
    begin
      hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines, file_length_difference)
      file_length_difference = hunk.file_length_difference
      next unless oldhunk
      if (context_lines > 0 ) and hunk.overlaps?(oldhunk)
        hunk.unshift(oldhunk)
      else
        raw_chunks.concat oldhunk.blocks
      end
    ensure
      oldhunk = hunk
    end
  end
  raw_chunks.concat oldhunk.blocks
  
  
  raw_chunks.each do |block|
    begin
      
      last_pos = -1
      last_action = '+'
      count = 0            
      block.insert.each do |b|
        if last_pos < 0 then
          last_pos = b.position
          count = 0
        elsif b.position-1 == (last_pos + count) then
          count +=1                
        else
          collections.push [last_action, last_pos, count+1 ]
          last_pos = b.position
          count = 0
        end
      end
      
      collections.push [ last_action, last_pos, count+1 ] if block.insert.length > 0
      
    rescue => bang
      puts bang            
    end
  end
  
  raw_chunks.each do |block|
    begin
      
      last_pos = -1
      last_action = '-'
      count = 0            
      
      block.remove.each do |b|
        if last_pos < 0 then
          last_pos = b.position
          count = 0
        elsif b.position-1 == (last_pos + count) then
          count +=1                
        else
          collections.push [last_action, last_pos, count+1 ]
          last_pos = b.position
          count = 0
        end
      end
      
      collections.push [ last_action, last_pos, count+1 ] if block.remove.length > 0
      
      
    rescue => bang
      puts bang            
    end
  end
  
  return collections.sort_by{ |e| e[1]}
end

#getInserts(data_old, data_new) ⇒ Object



203
204
205
206
207
208
# File 'lib/watobo/gui/chat_diff.rb', line 203

def getInserts(data_old, data_new)
  
  
  return nil if diffs.empty?
  
end

#initNavTable(table) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/watobo/gui/chat_diff.rb', line 221

def initNavTable(table)
  table.clearItems()
  table.setTableSize(0, 3)
  
  table.setColumnText( 0, "Type" ) 
  table.setColumnText( 1, "Pos" )
  table.setColumnText( 2, "Count" )
  
  table.rowHeader.width = 0
  table.setColumnWidth(0, 50)
  table.setColumnWidth(1, 50)
  table.setColumnWidth(2, 50)
end

#normalizeData(data) ⇒ Object

Raises:

  • (ArgumentError)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/watobo/gui/chat_diff.rb', line 172

def normalizeData(data)
  raise ArgumentError, "Bad data type. Need Request/Response." unless data.respond_to? :headers 
  dummy = []
  begin
    unless data.headers.nil?
      data.headers.each do |h|
        dummy.concat adjustLine(h)    
      end
      
      dummy.push ""
    end
    
    
    unless data.body.nil?
    #  puts "> clean up body #{data.body.length}"
      body =  data.body_encoded
      body.split("\n").each do |l|
       # puts "[#{i}] #{l}"
        dummy.concat adjustLine(l)
      end
    end
  rescue => bang
    puts bang
    dummy = data
  end
  #  puts dummy.join("\n")
 # return dummy.join("\n")
  return dummy
end

#onTableClick(sender, sel, item) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/watobo/gui/chat_diff.rb', line 128

def onTableClick(sender,sel,item)    
  row = item.row
  action = sender.getItem(row, 0).text.to_s
  pos = sender.getItem(row, 1).text.to_i
  
  sender.killSelection()
  if action == "+" then
    @diff_new.makeRowVisible(pos)
  else
    @diff_orig.makeRowVisible(pos)
  end
end

#showRequestDiffObject



210
211
212
213
214
# File 'lib/watobo/gui/chat_diff.rb', line 210

def showRequestDiff()
  @diff_orig.showDiff(@normRequestOrig, @requestBlocks[:remove])
  @diff_new.showDiff(@normRequestNew, @requestBlocks[:insert])
  
end

#showResponseDiffObject



216
217
218
219
# File 'lib/watobo/gui/chat_diff.rb', line 216

def showResponseDiff()
  @diff_orig.showDiff(@normResponseOrig, @responseBlocks[:remove])
  @diff_new.showDiff(@normResponseNew, @responseBlocks[:insert])
end

#updateBlocks(data_old, data_new, blocks, diffs) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/watobo/gui/chat_diff.rb', line 154

def updateBlocks(data_old, data_new, blocks, diffs)
  ib = []
  rb = []
  context_lines = 3
  file_length_difference = 0
  
  diffs.each do |piece|
    hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines, file_length_difference)
    hunk.blocks.each do |b|
      ib.concat b.insert
      rb.concat b.remove
    end
  end
  blocks[:remove] = rb
  blocks[:insert] = ib
end

#updateNavTablesObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/watobo/gui/chat_diff.rb', line 235

def updateNavTables()
  req_collection = diffCollections(@normRequestOrig, @normRequestNew, @requestDiffs)
  res_collection = diffCollections(@normResponseOrig, @normResponseNew, @responseDiffs)
  initNavTable(@navRequestTable)
  initNavTable(@navResponseTable)
  
  req_collection.each do |action, pos, count|
    lastRowIndex = @navRequestTable.getNumRows
    @navRequestTable.appendRows(1)
    
    @navRequestTable.setItemText(lastRowIndex, 0, action)
    @navRequestTable.setItemText(lastRowIndex, 1, pos.to_s)
    @navRequestTable.setItemText(lastRowIndex, 2, count.to_s)
    
  end
  
  res_collection.each do |action, pos, count|
    lastRowIndex = @navResponseTable.getNumRows
    @navResponseTable.appendRows(1)
    
    @navResponseTable.setItemText(lastRowIndex, 0, action)
    @navResponseTable.setItemText(lastRowIndex, 1, pos.to_s)
    @navResponseTable.setItemText(lastRowIndex, 2, count.to_s)
    #@navRequestTable.getItem(lastRowIndex, index).justify = FXTableItem::LEFT
    
  end
end