Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined method each error occurring on static assets. [Fix #9] #10

Merged
merged 2 commits into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions lib/rack/honeypot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@ 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)
body = response.respond_to?(:body) ? response.body : response
body = body.inject("") { |i, a| i << a } if body.respond_to?(:each)
body.to_s
end

def spambot_submission?(form_hash)
form_hash && form_hash[@input_name] && form_hash[@input_name] != @input_value
end
Expand All @@ -43,26 +49,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