Skip to content

Commit

Permalink
Doc fixes and a couple minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
akdubya committed Apr 1, 2009
1 parent 8b63e0f commit 66f38aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ render requests you can set up <tt>Rack::Thumb</tt> to check for a <tt>SHA-1</tt
signature that is unique to every url. Using this option, only thumbnails requested
by your templates will be valid. Example:

use Rack::Thumb(
use Rack::Thumb, {
:secret => "My secret", # => Don't tell anyone!
:key_length => "16" # => Only use 16 digits of the SHA-1 key
)
:keylength => "16" # => Only use 16 digits of the SHA-1 key
}

You can then use your +secret+ to generate secure links in your templates using
Ruby's built-in <tt>Digest::SHA1</tt> library:
Expand Down
19 changes: 8 additions & 11 deletions lib/rack/thumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ module Rack
# that is unique to every url. Using this option, only thumbnails requested
# by your templates will be valid. Example:
#
# use Rack::Thumb(
# use Rack::Thumb, {
# :secret => "My secret",
# :key_length => "16" # => Only use 16 digits of the SHA-1 key
# )
# :keylength => "16" # => Only use 16 digits of the SHA-1 key
# }
#
# You can then use your +secret+ to generate secure links in your templates:
#
Expand Down Expand Up @@ -132,7 +132,7 @@ def extract_meta(match)
def extract_signed_meta(match)
base, dim, grav, sig, ext = match.captures
digest = Digest::SHA1.hexdigest("#{base}_#{dim}#{grav}#{ext}#{@secret}")[0..@keylen-1]
throw(:halt, forbidden) unless sig && (sig == digest)
throw(:halt, bad_request) unless sig && (sig == digest)
[base + ext, dim, grav]
end

Expand Down Expand Up @@ -228,13 +228,6 @@ def bad_request
[body]]
end

def forbidden
body = "Bad thumbnail signature in #{@path}\n"
[403, {"Content-Type" => "text/plain",
"Content-Length" => body.size.to_s},
[body]]
end

def head?
@env["REQUEST_METHOD"] == "HEAD"
end
Expand All @@ -246,5 +239,9 @@ def each
end
}
end

def to_path
@thumb.path
end
end
end
6 changes: 3 additions & 3 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@
res.body.bytesize.should == 6221
end

it "should return forbidden if the signature is invalid" do
it "should return bad request if the signature is invalid" do
request = Rack::MockRequest.new(Rack::Thumb.new(@app, :keylength => 16,
:secret => "test"))

res = request.get("/media/imagick_50x100-sw-9922d04b14049f85.jpg")
res.should.be.forbidden
res.body.should == "Bad thumbnail signature in /media/imagick_50x100-sw-9922d04b14049f85.jpg\n"
res.should.be.client_error
res.body.should == "Bad thumbnail parameters in /media/imagick_50x100-sw-9922d04b14049f85.jpg\n"
end

it "should return bad request if the dimensions are bad" do
Expand Down

0 comments on commit 66f38aa

Please sign in to comment.