Skip to content

Commit

Permalink
return correct content-length
Browse files Browse the repository at this point in the history
fixes #6

The content-length should be given in octets. the String#length that was
used only works reliably on ascii/8-bit but not on utf-8.

Thank you @hybridknight for brining this to my attention.
  • Loading branch information
tilsammans committed Sep 14, 2015
1 parent 7e58d3a commit ad213e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rack/maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(app, options={})
def call(env)
if maintenance? && path_in_app(env)
data = File.read(file)
[ 503, { 'Content-Type' => content_type, 'Content-Length' => data.length.to_s }, [data] ]
[ 503, { 'Content-Type' => content_type, 'Content-Length' => data.bytesize.to_s }, [data] ]
else
app.call(env)
end
Expand Down
16 changes: 15 additions & 1 deletion spec/rack-maintenance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
shared_examples "RackMaintenance" do
let(:app) { Class.new { def call(env); end }.new }
let(:rack) { Rack::Maintenance.new(app, :file => file_name) }
let(:data) { data = File.read(file_name) }

context "without a :file option" do
it "raises an error" do
Expand All @@ -14,6 +15,10 @@
end

context "without maintenance file" do
before do
FileUtils.rm(file_name) if File.exists?(file_name)
end

it "calls the app" do
app.should_receive(:call).once
rack.call({})
Expand All @@ -35,7 +40,7 @@
end

it "returns the maintenance response" do
rack.call({}).should eq [503, {"Content-Type"=>content_type, "Content-Length"=>"0"}, [""]]
rack.call({}).should eq [503, {"Content-Type"=>content_type, "Content-Length"=>data.bytesize.to_s}, [data]]
end

context "and :env option MAINTENANCE" do
Expand Down Expand Up @@ -98,3 +103,12 @@
end
end

describe "RackMaintenance with unicode maintenance file" do
before do
FileUtils.cp 'spec/unicode.html', 'spec/maintenance.html'
end
it_behaves_like "RackMaintenance" do
let(:file_name) { "spec/maintenance.html" }
let(:content_type) { "text/html" }
end
end
13 changes: 13 additions & 0 deletions spec/unicode.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Under Maintenance</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
<div class="dialog">
<h1>ขณะนี้กำลังอยู่ระหว่างการปรับปรุงระบบ</h1>
</div>
</body>
</html>

0 comments on commit ad213e3

Please sign in to comment.