Commits

Michael Granger committed 7ed6ee3

Fix status line for responses that have an explicit content-length.

This is so HEAD responses, for example, don't get reset to '204
No Content' just because their body is empty.

  • Participants
  • Parent commits 3e87267

Comments (0)

Files changed (2)

File lib/mongrel2/httpresponse.rb

 
 	### Send the response status to the client
 	def status_line
-		st = self.status || (self.body.size.zero? ? HTTP::NO_CONTENT : HTTP::OK)
+		st = self.status || self.derived_status_code
 		return STATUS_LINE_FORMAT % [ st, HTTP::STATUS_NAME[st] ]
 	end
 
 		]
 	end
 
+	### Return the numeric HTTP status code for the response bsaed on what has already been
+	### set
+	def derived_status_code
+		# If there's a non-empty entity body, or the content length has been set explicitly
+		# to something non-zero, assume the response is OK
+		if self.body.size.nonzero? || 
+		   (self.header.content_length && self.header.content_length.nonzero?)
+			return HTTP::OK
+
+		# otherwise set it to 204
+		else
+			return HTTP::NO_CONTENT
+		end
+	end
+
 end # class Mongrel2::Response
 
 # vim: set nosta noet ts=4 sw=4:

File spec/mongrel2/httpresponse_spec.rb

 	end
 
 
+	it "doesn't reset the status to 204 NO CONTENT if there's an explicit content-length header" do
+
+		# Simulate a response to a HEAD request
+		request_factory = Mongrel2::RequestFactory.new( route: '/foo' )
+		@response.request = request_factory.head( '/foo' )
+
+		@response.header.content_length = 2048
+		@response.body = ''
+
+		@response.status_line.should =~ /200 OK/i
+	end
+
+
 	it "can build a valid HTTP status line for its status" do
 		@response.status = HTTP::SEE_OTHER
 		@response.status_line.should == "HTTP/1.1 303 See Other"