Permalink
Browse files

rescuing some stuff and splitting on each on_data rather than on_connect

  • Loading branch information...
1 parent f5a5139 commit a086121d22f331646afac3f805c2653e809e6ab8 @schacon committed Apr 27, 2010
Showing with 38 additions and 22 deletions.
  1. +16 −6 browser/browser.rb
  2. +1 −1 browser/views/index.erb
  3. +17 −11 server.rb
  4. +4 −4 snifter.rb
View
@@ -35,13 +35,21 @@ def process_http(req)
http = headers.shift
harr = headers.map { |h| h.split(': ') }
- req = REXML::Document.new(xml)
- r = ""
- req.write(r, 3)
- div = CodeRay.scan(r, :xml).div
+ begin
+ req = REXML::Document.new(xml)
+ r = ""
+ req.write(r, 3)
+ div = CodeRay.scan(r, :xml).div
+ rescue
+ div = CodeRay.scan(xml, :xml).div
+ end
- n = Nokogiri.XML(xml)
- values = get_values([n.root.name], n.root, [])
+ begin
+ n = Nokogiri.XML(xml)
+ values = get_values([n.root.name], n.root, [])
+ rescue
+ values = []
+ end
{ :headers => harr, :body => div,
:header_raw => header, :body_raw => xml,
@@ -51,6 +59,8 @@ def process_http(req)
def get_line(data)
data.split("\n").first.gsub("HTTP/1.1", '')
+rescue
+ 'fu'
end
get '/' do
@@ -4,7 +4,7 @@
<table>
<% last_time = 0 %>
<% @sessions.each do |sess, req, res, time| %>
- <% if (time - last_time) > 2 %>
+ <% if (time - last_time) > 3 %>
<tr><td colspan="2"><hr/></td></tr>
<% end %>
<% last_time = time %>
View
@@ -2,30 +2,36 @@
require 'em-proxy'
require 'snifter'
+require 'pp'
@snifter = Snifter.new
-Proxy.start(:host => "0.0.0.0", :port => 5201, :debug => false) do |conn|
- conn.server :srv, :host => "127.0.0.1", :port => 5200
+listen_port = 5202
+host_to = ['127.0.0.1', 80]
- conn.on_connect do
- @snifter.log_connect(conn)
- end
+@@sessions = {}
+
+Proxy.start(:host => "0.0.0.0", :port => listen_port, :debug => false) do |conn|
+ conn.server :srv, :host => host_to[0], :port => host_to[1]
# modify / process request stream
conn.on_data do |data|
- @snifter.log_data(conn, data)
+ if id = @@sessions[conn.object_id]
+ id = conn.object_id.to_s + rand(1000).to_s
+ @@sessions[conn.object_id] = id
+ else
+ id = @@sessions[conn.object_id] = conn.object_id
+ end
+ @snifter.log_connect(id)
+ @snifter.log_data(id, data)
data
end
# modify / process response stream
conn.on_response do |backend, resp|
- @snifter.log_response(conn, backend, resp)
+ id = @@sessions[conn.object_id]
+ @snifter.log_response(id, backend, resp)
resp
end
- # termination logic
- conn.on_finish do |backend, name|
- @snifter.log_finish(conn, backend, name)
- end
end
View
@@ -9,12 +9,12 @@ def initialize
LIST_ID = 'snifter-conn-list'
def conn_id(conn)
- 'snifter-conn-' + conn.object_id.to_s
+ 'snifter-conn-' + conn.to_s
end
def update_list(id)
@redis.rpush LIST_ID, id
- @redis.ltrim LIST_ID, -50, -1
+ @redis.ltrim LIST_ID, -30, -1
end
def log_connect(conn)
@@ -25,7 +25,7 @@ def log_connect(conn)
def add_data(conn, type, data)
cid = conn_id(conn) + type
if predata = @redis.get(cid)
- data = predata + data
+ data = predata.to_s + data.to_s
end
@redis.set cid, data
end
@@ -39,7 +39,7 @@ def log_response(conn, backend, resp)
end
def log_finish(conn, backend, name)
- p [:on_finish, conn.object_id]
+ p [:on_finish, conn]
end
def current

0 comments on commit a086121

Please sign in to comment.