rack-audit 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/rack-audit.rb +37 -37
- data/lib/rack-audit/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
data/lib/rack-audit.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
require 'thread'
|
3
3
|
require 'net/http'
|
4
4
|
require 'uri'
|
5
|
+
require 'logger'
|
5
6
|
|
6
7
|
# gems
|
7
8
|
require 'rack'
|
@@ -9,81 +10,80 @@ require 'uuidtools'
|
|
9
10
|
require 'multi_json'
|
10
11
|
|
11
12
|
class Rack::Audit
|
12
|
-
attr_reader :queue, :host, :port, :name
|
13
|
-
|
14
|
-
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@port = @uri.port
|
19
|
-
@name = name
|
13
|
+
attr_reader :queue, :host, :port, :name, :url
|
14
|
+
|
15
|
+
def initialize(app, name, url, options={})
|
16
|
+
@app, @name, @url = app, name, url
|
17
|
+
@queue = Queue.new
|
18
|
+
@logger = options[:logger] || Logger.new(STDOUT)
|
20
19
|
|
21
20
|
@consumer = Thread.new do
|
22
|
-
|
23
|
-
body =
|
21
|
+
loop do
|
22
|
+
body = self.queue.pop
|
24
23
|
begin
|
25
|
-
post(
|
24
|
+
post(body)
|
26
25
|
rescue => e
|
27
|
-
|
26
|
+
self.logger.error("#{e.inspect}\n#{e.backtrace.join("\n\t")}")
|
28
27
|
end
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
32
|
def call(env)
|
34
|
-
|
35
|
-
log_request(
|
33
|
+
id = UUIDTools::UUID.random_create.to_s
|
34
|
+
log_request(id, env)
|
36
35
|
response = @app.call(env)
|
37
|
-
log_response(
|
36
|
+
log_response(id, response)
|
38
37
|
response
|
39
38
|
end
|
40
39
|
|
41
40
|
private
|
42
41
|
|
43
|
-
def log_request(
|
42
|
+
def log_request(id, env)
|
44
43
|
request = Rack::Request.new(env)
|
45
44
|
loggable_request = {
|
46
45
|
:request_method => request.request_method,
|
47
46
|
:url => request.url,
|
48
47
|
:params => request.params,
|
49
|
-
:
|
48
|
+
:body => request.body.read,
|
49
|
+
:headers => env.inject({}) do |r,(k,v)|
|
50
|
+
k.match(/^HTTP_/) ? r.merge(k.gsub("HTTP_") => v) : r
|
51
|
+
end,
|
50
52
|
}
|
51
53
|
|
52
|
-
# FIXME: necessary?
|
53
54
|
request.body.rewind
|
54
55
|
|
55
56
|
queue << MultiJson.encode(
|
56
|
-
:
|
57
|
+
:id => id,
|
57
58
|
:request => loggable_request,
|
58
59
|
:time => Time.new.to_i,
|
59
|
-
:from => name
|
60
|
+
:from => self.name,
|
60
61
|
)
|
61
|
-
rescue => e
|
62
|
-
STDERR.puts e.inspect
|
63
|
-
ensure
|
64
|
-
true
|
65
62
|
end
|
66
63
|
|
67
|
-
def log_response(
|
64
|
+
def log_response(id, response)
|
68
65
|
status, headers, body = response
|
69
66
|
full_body = ''
|
70
67
|
body.each { |b| full_body += b.to_s; b.rewind if b.respond_to?(:rewind) }
|
71
|
-
queue << MultiJson.encode(
|
72
|
-
:
|
68
|
+
self.queue << MultiJson.encode(
|
69
|
+
:id => id,
|
73
70
|
:response => [status, headers, full_body],
|
74
71
|
:time => Time.new.to_i,
|
75
|
-
:from => name
|
72
|
+
:from => self.name,
|
76
73
|
)
|
77
|
-
rescue => e
|
78
|
-
STDERR.puts e.inspect
|
79
|
-
ensure
|
80
|
-
true
|
81
74
|
end
|
82
75
|
|
83
|
-
def post(
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
Net::HTTP.
|
76
|
+
def post(body)
|
77
|
+
uri = URI.parse(self.url)
|
78
|
+
host, port = uri.host, uri.port
|
79
|
+
|
80
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
81
|
+
http.use_ssl= (uri.port == 443)
|
82
|
+
|
83
|
+
request = Net::HTTP::Post.new(uri.to_s)
|
84
|
+
request["Content-Type"]= "application/json"
|
85
|
+
request.body= body
|
86
|
+
|
87
|
+
http.request(request)
|
88
88
|
end
|
89
89
|
end
|
data/lib/rack-audit/version.rb
CHANGED