Skip to content

Commit

Permalink
Fix undefined method each error occurring on static assets. [Fix #9]
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Dutil committed Jan 15, 2013
1 parent d0ce3ef commit 1f45844
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
38 changes: 20 additions & 18 deletions lib/rack/honeypot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,31 @@ def call(env)
@logger.warn("[Rack::Honeypot] Spam bot detected; responded with null") unless @logger.nil?
null_response
else
status, headers, body = @app.call(env)

if @always_enabled || honeypot_header_present?(headers)
body = insert_honeypot(body)
headers = response_headers(headers, body)
@status, @headers, @response = @app.call(env)
if @headers["Content-Type"] and @headers["Content-Type"].include?("text/html") and (@always_enabled || honeypot_header_present?(@headers))
body = insert_honeypot(response_body(@response))
@headers.merge("Content-Length" => body.length.to_s)
[@status, @headers, [body]]
else
[@status, @headers, @response]
end

[status, headers, body]
end
end

private

def response_body(response)
if response.respond_to?(:body)
response.body
elsif response.is_a?(String)
response
elsif response.respond_to?(:each)
body = ""
response.each { |part| body << part }
body
end
end

def spambot_submission?(form_hash)
form_hash && form_hash[@input_name] && form_hash[@input_name] != @input_value
end
Expand All @@ -43,26 +55,16 @@ def honeypot_header_present?(headers)
header = headers.delete(HONEYPOT_HEADER)
header && header.index("enabled")
end

def null_response
[200, {'Content-Type' => 'text/html', "Content-Length" => "0"}, []]
end

def response_body(response)
body = ""

# The body may not be an array, so we need to call #each here.
response.each {|part| body << part }

body
end

def response_headers(headers, body)
headers.merge("Content-Length" => body.length.to_s)
end

def insert_honeypot(body)
body = response_body(body)
body.gsub!(/<\/head>/, css + "\n</head>")
body.gsub!(/<form(.*)>/, '<form\1>' + "\n" + div)
body
Expand Down
1 change: 1 addition & 0 deletions rack-honeypot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Gem::Specification.new do |s|

s.add_dependency(%q<unindentable>, ["= 0.0.4"])
s.add_dependency(%q<rack>, [">= 0"])
s.add_development_dependency('rack-test')
end

4 changes: 2 additions & 2 deletions test/test_honeypot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rack/test'
require 'test/unit'
require 'mocha'
require 'mocha/setup'
require 'unindentable'

require File.expand_path(File.dirname(__FILE__) + '/../lib/rack/honeypot')
Expand Down Expand Up @@ -30,7 +30,7 @@ def app
BLOCK

headers = {
'Content-Type' => 'text/plain',
'Content-Type' => 'text/html',
'Content-Length' => content.length.to_s,
}

Expand Down

0 comments on commit 1f45844

Please sign in to comment.