rack_upstream_identification 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +34 -21
- data/VERSION +1 -1
- data/lib/rack/upstream_identification.rb +24 -9
- data/test/test_rack_upstream_identification.rb +18 -3
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,20 +1,11 @@
|
|
1
1
|
= rack_upstream_identification
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
* filter_for_digits: leaves only numeric values
|
8
|
-
* filter_for_uppercase: uppercase all alpha characters
|
9
|
-
* filter_for_lowercase: lowercase all alpha characters
|
10
|
-
* filter_for_whitespace: strips and non-essential whitespace out of a string (leaving only single whitespace characters).
|
11
|
-
|
12
|
-
Features I'd like to add in the future:
|
13
|
-
* User-defined custom filters configured via DSL.
|
14
|
-
* Additional macros that filter decimal values, etc.
|
15
|
-
* Allow the developer to opt-in models for filtering and not include
|
16
|
-
the plugin in ActiveRecord::Base
|
3
|
+
Upstream Identification is a Rack middleware that is useful in debugging requests in mult-server environments.
|
4
|
+
It is often difficult to debug client-side (or server-side ones) that might be occuring on only a handful of machines.
|
5
|
+
This was created to provide originating server indentity that is easily accessable in the browser (or command-line tools like curl).
|
17
6
|
|
7
|
+
Read below for usage.
|
8
|
+
|
18
9
|
= Runtime Dependencies
|
19
10
|
|
20
11
|
* Rack 1.0.0+
|
@@ -24,22 +15,44 @@ Features I'd like to add in the future:
|
|
24
15
|
|
25
16
|
== Rails
|
26
17
|
|
27
|
-
config.gem "
|
18
|
+
config.gem "rack_upstream_identification", :lib => "rack/upstream_identification", :version => "0.0.1", :source => "http://gemcutter.org"
|
19
|
+
|
20
|
+
OR
|
21
|
+
|
22
|
+
script/plugin install git://github.com/rares/rack_upstream_identification.git
|
28
23
|
|
29
24
|
== Bundler
|
30
25
|
|
31
26
|
source "http://gemcutter.org"
|
32
|
-
gem "
|
27
|
+
gem "rack_upstream_identification", "0.0.1"
|
33
28
|
|
34
29
|
== RubyGems
|
35
30
|
|
36
|
-
gem install
|
31
|
+
gem install rack_upstream_identification --source http://gemcutter.org
|
37
32
|
|
38
33
|
= Usage
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
|
35
|
+
By default this middleware injects a header called "X-Upsteam-Identification" that defaults to using the machine's hostname (given a hostname 'foo'):
|
36
|
+
|
37
|
+
use Rack::UpstreamIdentification
|
38
|
+
|
39
|
+
yields the header: "X-Upstream-Identification: foo"
|
40
|
+
|
41
|
+
There are a few available options that allow for mutation of the default output:
|
42
|
+
|
43
|
+
* header - The following configuration will change the <b>name</b> of the header:
|
44
|
+
|
45
|
+
use Rack::UpstreamIdentification, "your_header_name"
|
46
|
+
|
47
|
+
* blk - Passing a block yields the machine's hostname and should return a string that will be sent to the client as the value:
|
48
|
+
|
49
|
+
use Rack::UpstreamIdentification do |hostname|
|
50
|
+
Zlib.crc32(hostname).to_s
|
51
|
+
end
|
52
|
+
|
53
|
+
If you plan on obfuscating this value (as above) there is no facility to track mappings from checksums to servernames so this will have to be managed in your code.
|
54
|
+
|
55
|
+
Depending on what stack you are using, these statements will live in various places. Please consult documentation for the proper location.
|
43
56
|
|
44
57
|
= Contributing
|
45
58
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -5,14 +5,14 @@ module Rack
|
|
5
5
|
# A middleware for injecting upstream host info into a response header.
|
6
6
|
class UpstreamIdentification
|
7
7
|
|
8
|
-
attr_reader :transform, :header_name
|
8
|
+
attr_reader :app, :transform, :header_name
|
9
9
|
|
10
10
|
# Creates the middleware with the following configuration options:
|
11
11
|
#
|
12
|
-
# [
|
12
|
+
# [+header+] The name of the header that is sent to the client.
|
13
13
|
# Defaults to "X-Upstream-Identification"
|
14
14
|
#
|
15
|
-
# [
|
15
|
+
# [+blk+] The optional block used to transform a header. If
|
16
16
|
# a block is passed, the following applies:
|
17
17
|
# * The machine's hostname is passed to the block.
|
18
18
|
# This gives the block the opportunity to change
|
@@ -31,19 +31,34 @@ module Rack
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def call(env)
|
34
|
-
|
34
|
+
status, headers, response = app.call(env)
|
35
|
+
inject_identity! headers
|
36
|
+
|
37
|
+
[status, headers, response]
|
35
38
|
end
|
36
39
|
|
37
|
-
|
40
|
+
protected
|
38
41
|
|
39
42
|
# Push the configured header down to the client
|
40
|
-
def
|
41
|
-
|
42
|
-
transform
|
43
|
+
def inject_identity!(headers)
|
44
|
+
headers[header_name] = if transform
|
45
|
+
invoke_transform host_name, &transform
|
43
46
|
else
|
44
47
|
host_name
|
45
48
|
end
|
46
|
-
|
49
|
+
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
# Invoke the transform block.
|
54
|
+
# If the block does not return a non-nil value, raise and exception
|
55
|
+
def invoke_transform(host_name, &transform)
|
56
|
+
header_value = transform.call(host_name)
|
57
|
+
unless header_value
|
58
|
+
raise ArgumentError, "transform block must return a non-nil value"
|
59
|
+
end
|
60
|
+
|
61
|
+
header_value.to_s
|
47
62
|
end
|
48
63
|
|
49
64
|
# Get the hostname for the current machine
|
@@ -31,6 +31,13 @@ class TestRackUpstreamIdentification < Test::Unit::TestCase
|
|
31
31
|
end
|
32
32
|
run handler
|
33
33
|
end
|
34
|
+
|
35
|
+
map "/empty_block" do
|
36
|
+
use Rack::UpstreamIdentification do |host|
|
37
|
+
end
|
38
|
+
run handler
|
39
|
+
end
|
40
|
+
|
34
41
|
end.to_app
|
35
42
|
end
|
36
43
|
|
@@ -89,7 +96,7 @@ class TestRackUpstreamIdentification < Test::Unit::TestCase
|
|
89
96
|
end
|
90
97
|
|
91
98
|
should "push the header down using the transformed value" do
|
92
|
-
assert_header_value "X-Upstream-Identification",
|
99
|
+
assert_header_value "X-Upstream-Identification", Socket.gethostname.upcase
|
93
100
|
end
|
94
101
|
|
95
102
|
end
|
@@ -103,13 +110,21 @@ class TestRackUpstreamIdentification < Test::Unit::TestCase
|
|
103
110
|
assert_header_present "crypted_header"
|
104
111
|
end
|
105
112
|
|
106
|
-
should "push the header down using the
|
113
|
+
should "push the header down using the mutated header value" do
|
107
114
|
assert_header_value "crypted_header", Zlib.crc32(Socket.gethostname).to_s
|
108
115
|
end
|
109
116
|
|
117
|
+
end
|
118
|
+
|
119
|
+
context "with an empty transform block" do
|
120
|
+
should "raise an ArgumentError indicating that the transform block should return a non-nil value" do
|
121
|
+
assert_raise ArgumentError do
|
122
|
+
get "/empty_block"
|
123
|
+
end
|
124
|
+
end
|
110
125
|
|
111
126
|
end
|
112
|
-
|
127
|
+
|
113
128
|
end
|
114
129
|
|
115
130
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_upstream_identification
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Ares
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-29 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|