rack-content-hash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Gemfile.lock +63 -0
- data/Guardfile +8 -0
- data/LICENSE +21 -0
- data/README.md +16 -0
- data/Rakefile +1 -0
- data/lib/rack/contrib/content_hash.rb +39 -0
- data/rack-content-hash.gemspec +24 -0
- data/spec/rack/contrib/content_hash_spec.rb +80 -0
- data/spec/spec_helper.rb +9 -0
- metadata +113 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rack-content-hash (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.0.9)
|
10
|
+
diff-lcs (1.2.4)
|
11
|
+
ffi (1.9.0)
|
12
|
+
formatador (0.2.4)
|
13
|
+
guard (1.8.1)
|
14
|
+
formatador (>= 0.2.4)
|
15
|
+
listen (>= 1.0.0)
|
16
|
+
lumberjack (>= 1.0.2)
|
17
|
+
pry (>= 0.9.10)
|
18
|
+
thor (>= 0.14.6)
|
19
|
+
guard-rspec (3.0.2)
|
20
|
+
guard (>= 1.8)
|
21
|
+
rspec (~> 2.13)
|
22
|
+
listen (1.2.2)
|
23
|
+
rb-fsevent (>= 0.9.3)
|
24
|
+
rb-inotify (>= 0.9)
|
25
|
+
rb-kqueue (>= 0.2)
|
26
|
+
lumberjack (1.0.4)
|
27
|
+
method_source (0.8.1)
|
28
|
+
multi_json (1.7.7)
|
29
|
+
pry (0.9.12.2)
|
30
|
+
coderay (~> 1.0.5)
|
31
|
+
method_source (~> 0.8)
|
32
|
+
slop (~> 3.4)
|
33
|
+
rb-fsevent (0.9.3)
|
34
|
+
rb-inotify (0.9.0)
|
35
|
+
ffi (>= 0.5.0)
|
36
|
+
rb-kqueue (0.2.0)
|
37
|
+
ffi (>= 0.5.0)
|
38
|
+
rspec (2.14.1)
|
39
|
+
rspec-core (~> 2.14.0)
|
40
|
+
rspec-expectations (~> 2.14.0)
|
41
|
+
rspec-mocks (~> 2.14.0)
|
42
|
+
rspec-core (2.14.3)
|
43
|
+
rspec-expectations (2.14.0)
|
44
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
45
|
+
rspec-mocks (2.14.1)
|
46
|
+
simplecov (0.7.1)
|
47
|
+
multi_json (~> 1.0)
|
48
|
+
simplecov-html (~> 0.7.1)
|
49
|
+
simplecov-html (0.7.1)
|
50
|
+
slop (3.4.5)
|
51
|
+
thor (0.18.1)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
ruby
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
guard
|
58
|
+
guard-rspec
|
59
|
+
rack-content-hash!
|
60
|
+
rb-fsevent (~> 0.9)
|
61
|
+
rb-inotify (~> 0.9)
|
62
|
+
rspec
|
63
|
+
simplecov
|
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 ZippyKid
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Rack::Contrib::ContentHash
|
2
|
+
|
3
|
+
Provide a simple interface for multiple `Content-*` hashes.
|
4
|
+
|
5
|
+
## Supported:
|
6
|
+
|
7
|
+
- `Content-MD5`
|
8
|
+
- `Content-SHA1`
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
# config.ru
|
14
|
+
|
15
|
+
use Rack::Contrib::ContentHash, md5: true, sha1: true
|
16
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
require 'digest/md5'
|
3
|
+
require 'digest/sha1'
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
module Contrib
|
7
|
+
#
|
8
|
+
# Add Content-MD5 and Content-SHA1 headers, containing the hashes of the
|
9
|
+
# body's response.
|
10
|
+
#
|
11
|
+
# Best installed as your very last middleware piece to ensure the contents
|
12
|
+
# doesn't become invalidated by another middleware piece.
|
13
|
+
#
|
14
|
+
# Ex:
|
15
|
+
# use Rack::Contrib::ContentHash, md5: true, sha1: true
|
16
|
+
#
|
17
|
+
class ContentHash
|
18
|
+
VERSION = '0.0.1'
|
19
|
+
|
20
|
+
# Initialize the middleware, pass a Hash of algorithms in the second
|
21
|
+
# parameter. Ex: .new app, md5: true, sha1: false
|
22
|
+
def initialize app, algorithms = {}
|
23
|
+
@app = app
|
24
|
+
@algo= algorithms
|
25
|
+
end
|
26
|
+
|
27
|
+
# Hash the body of your response, and append the headers to the Hash
|
28
|
+
def call env
|
29
|
+
status, headers, body = @app.call env
|
30
|
+
|
31
|
+
headers['Content-MD5'] = Digest::MD5.hexdigest(body) if @algo[:md5]
|
32
|
+
headers['Content-SHA1'] = Digest::SHA1.hexdigest(body) if @algo[:sha1]
|
33
|
+
|
34
|
+
[status, headers, body]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'rack/contrib/content_hash'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'rack-content-hash'
|
8
|
+
gem.version = Rack::Contrib::ContentHash::VERSION
|
9
|
+
gem.summary = 'Return a Content-MD5 or Content-SHA1 header with your' +
|
10
|
+
' content for verification purposes.'
|
11
|
+
gem.homepage = 'https://github.com/zippykid/rack-content-hash'
|
12
|
+
gem.authors = ['Graham Christensen']
|
13
|
+
gem.email = ['info@zippykid.com']
|
14
|
+
gem.licenses = ['MIT']
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_development_dependency('rspec')
|
21
|
+
gem.add_development_dependency('guard')
|
22
|
+
gem.add_development_dependency('guard-rspec')
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,80 @@
|
|
1
|
+
|
2
|
+
describe Rack::Contrib::ContentHash do
|
3
|
+
let (:response) { [200, {'Location' => 'lolwhat'}, 'foobar-body'] }
|
4
|
+
let (:response_clone) { [200, {'Location' => 'lolwhat'}, 'foobar-body'] }
|
5
|
+
describe "#initialize" do
|
6
|
+
it "accepts an app" do
|
7
|
+
expect { ware = Rack::Contrib::ContentHash.new nil }.not_to raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it "requires an app" do
|
11
|
+
expect { ware = Rack::Contrib::ContentHash.new }.to raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "accepts a hash of enabled algorithms" do
|
15
|
+
expect {
|
16
|
+
ware = Rack::Contrib::ContentHash.new nil, md5: true
|
17
|
+
}.not_to raise_error
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#call" do
|
22
|
+
it "requires an env" do
|
23
|
+
ware = Rack::Contrib::ContentHash.new nil
|
24
|
+
expect { ware.call }.to raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it "calls the app's .call method" do
|
28
|
+
app = double(nil, call: response)
|
29
|
+
env = {'foo' => 'bar'}
|
30
|
+
ware = Rack::Contrib::ContentHash.new app
|
31
|
+
ware.call env
|
32
|
+
|
33
|
+
app.should have_received(:call).once.with(env)
|
34
|
+
end
|
35
|
+
|
36
|
+
context "all hashing is disabled" do
|
37
|
+
it "returns the same data from the app" do
|
38
|
+
app = double(nil, call: response)
|
39
|
+
env = {}
|
40
|
+
ware = Rack::Contrib::ContentHash.new app
|
41
|
+
ware.call(env).should eq(response_clone)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "MD5 hashing is enabled" do
|
46
|
+
it "hashes the body, and adds a Content-MD5 header" do
|
47
|
+
app = double(nil, call: response)
|
48
|
+
env = {}
|
49
|
+
ware = Rack::Contrib::ContentHash.new app, md5: true
|
50
|
+
|
51
|
+
response_clone[1]['Content-MD5'] = '58110b6c0b6880db960c1f19e73feb9a'
|
52
|
+
ware.call(env).should eq(response_clone)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "SHA1 hashing is enabled" do
|
57
|
+
it "hashes the body, and adds a Content-SHA1 header" do
|
58
|
+
app = double(nil, call: response)
|
59
|
+
env = {}
|
60
|
+
ware = Rack::Contrib::ContentHash.new app, sha1: true
|
61
|
+
|
62
|
+
response_clone[1]['Content-SHA1'] = 'e04ce9c63d277c7714f71d916461959710b761f2'
|
63
|
+
ware.call(env).should eq(response_clone)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "SHA1 and MD5 hashing are both enabled" do
|
68
|
+
it "hashes the body, and adds a Content-SHA1 and Content-MD5 header" do
|
69
|
+
app = double(nil, call: response)
|
70
|
+
env = {}
|
71
|
+
ware = Rack::Contrib::ContentHash.new app, sha1: true, md5: true
|
72
|
+
|
73
|
+
response_clone[1]['Content-SHA1'] = 'e04ce9c63d277c7714f71d916461959710b761f2'
|
74
|
+
response_clone[1]['Content-MD5'] = '58110b6c0b6880db960c1f19e73feb9a'
|
75
|
+
ware.call(env).should eq(response_clone)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-content-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Graham Christensen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-07-17 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: guard
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard-rspec
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id003
|
59
|
+
description:
|
60
|
+
email:
|
61
|
+
- info@zippykid.com
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- Guardfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/rack/contrib/content_hash.rb
|
76
|
+
- rack-content-hash.gemspec
|
77
|
+
- spec/rack/contrib/content_hash_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
has_rdoc: true
|
80
|
+
homepage: https://github.com/zippykid/rack-content-hash
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.3.7
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Return a Content-MD5 or Content-SHA1 header with your content for verification purposes.
|
111
|
+
test_files:
|
112
|
+
- spec/rack/contrib/content_hash_spec.rb
|
113
|
+
- spec/spec_helper.rb
|