Permalink
Browse files

Always set the Content-Length inside Webmachine instead of relying on…

… the adapter. Fixes #58.
  • Loading branch information...
1 parent c65879d commit 64c124a6086ce92106781705fb4fed9ac6ff9f05 @seancribbs seancribbs committed Jul 7, 2012
Showing with 61 additions and 1 deletion.
  1. +1 −0 lib/webmachine/decision/fsm.rb
  2. +25 −1 lib/webmachine/decision/helpers.rb
  3. +35 −0 spec/webmachine/decision/helpers_spec.rb
@@ -58,6 +58,7 @@ def respond(code, headers={})
end
response.code = code
resource.finish_request
+ ensure_content_length
trace_response(response)
end
@@ -41,7 +41,7 @@ def encode_body
end
end
if String === response.body
- response.headers['Content-Length'] = response.body.respond_to?(:bytesize) ? response.body.bytesize.to_s : response.body.length.to_s
+ set_content_length
else
response.headers.delete 'Content-Length'
response.headers['Transfer-Encoding'] = 'chunked'
@@ -99,6 +99,30 @@ def add_caching_headers
response.headers['Last-Modified'] = modified.httpdate
end
end
+
+ # Ensures that responses have an appropriate Content-Length
+ # header
+ def ensure_content_length
+ case
+ when response.headers['Transfer-Encoding']
+ return
+ when [204, 304].include?(response.code)
@lgierth
lgierth Jul 7, 2012

205 is also a candidate for this.

@seancribbs
seancribbs Jul 7, 2012 Webmachine member

Thanks for catching that. There's nothing specifically in Webmachine that produces 205, but one could definitely halt with that status code.

+ response.headers.delete 'Content-Length'
+ when has_response_body?
+ set_content_length
+ else
+ response.headers['Content-Length'] = '0'
+ end
+ end
+
+ # Sets the Content-Length header on the response
+ def set_content_length
+ if response.body.respond_to?(:bytesize)
+ response.headers['Content-Length'] = response.body.bytesize.to_s
+ else
+ response.headers['Content-Length'] = response.body.length.to_s
+ end
+ end
end # module Helpers
end # module Decision
end # module Webmachine
@@ -46,6 +46,41 @@ def accept_doc; result; end
end
end
+ context "setting the Content-Length header when responding" do
+ [204, 304].each do |code|
+ it "removes the header for entity-less response code #{code}" do
+ response.headers['Content-Length'] = '0'
+ response.body = nil
+ subject.send :respond, code
+ response.headers.should_not include 'Content-Length'
+ end
+ end
+
+ (200..599).each do |code|
+ # 204 and 304 have no bodies, 404 is set to a default non-zero
+ # response by Webmachine
+ next if code == 204 || code == 304 || code == 404
+
+ it "adds the header for response code #{code} that should include an entity but has an empty body" do
+ response.code = code
+ response.body = nil
+ subject.send :respond, code
+ response.headers['Content-Length'].should == '0'
+ end
+ end
+
+ (200..599).each do |code|
+ next if code == 204 || code == 304
+
+ it "does not add the header when Transfer-Encoding is set on code #{code}" do
+ response.headers['Transfer-Encoding'] = 'chunked'
+ response.body = []
+ subject.send :respond, code
+ response.headers.should_not include 'Content-Length'
+ end
+ end
+ end
+
describe "#encode_body" do
before { subject.run }

0 comments on commit 64c124a

Please sign in to comment.