Commits

Michael Granger  committed 21c8b57

Make explicitly-set 'nil' body also a bodiless response.

  • Participants
  • Parent commits b1ca9f2

Comments (0)

Files changed (13)

File lib/mongrel2/config.rb

 		# The Pathname of the SQL file used to create the config database
 		CONFIG_SQL    = DATA_DIR + 'config.sql'
 
-		# The Pathname of the SQL file used to add default mimetypes mappings to the config 
+		# The Pathname of the SQL file used to add default mimetypes mappings to the config
 		# database
 		MIMETYPES_SQL = DATA_DIR + 'mimetypes.sql'
 

File lib/mongrel2/config/host.rb

 		end
 
 
-		### Create a new Mongrel2::Config::Handler object with the specified +send_spec+, 
+		### Create a new Mongrel2::Config::Handler object with the specified +send_spec+,
 		### +send_ident+, +recv_spec+, +recv_ident+, and +options+ and return it.
 		def handler( send_spec, send_ident, recv_spec=nil, recv_ident='', options={} )
 			# Shift the opts hash over if the other optional args were omitted

File lib/mongrel2/config/route.rb

 	many_to_one :host
 
 
-	### Fetch the route's target, which is either a Mongrel2::Config::Directory, 
+	### Fetch the route's target, which is either a Mongrel2::Config::Directory,
 	### Mongrel2::Config::Proxy, or Mongrel2::Config::Handler object.
 	def target
 		case self.target_type

File lib/mongrel2/connection.rb

 require 'mongrel2' unless defined?( Mongrel2 )
 
 
-# The Mongrel2 connection class. Connection objects serve as a front end for 
-# the ZMQ sockets which talk to the mongrel2 server/s for your handler. It receives 
+# The Mongrel2 connection class. Connection objects serve as a front end for
+# the ZMQ sockets which talk to the mongrel2 server/s for your handler. It receives
 # TNetString requests and wraps Mongrel2::Request objects around them, and
 # then encodes and sends Mongrel2::Response objects back to the server.
 #
 
 
 	### Create a new Connection identified by +app_id+ (a UUID or other unique string) that
-	### will connect to a Mongrel2 server on the +sub_addr+ and +pub_addr+ (e.g., 
+	### will connect to a Mongrel2 server on the +sub_addr+ and +pub_addr+ (e.g.,
 	### 'tcp://127.0.0.1:9998').
 	def initialize( app_id, sub_addr, pub_addr )
 		@app_id       = app_id
 	end
 
 
-	### Tell the server associated with +sender_id+ to close the connections associated 
+	### Tell the server associated with +sender_id+ to close the connections associated
 	### with +conn_ids+.
 	def broadcast_close( sender_id, *conn_ids )
 		self.broadcast( sender_id, conn_ids.flatten, '' )

File lib/mongrel2/control.rb

 	###
 	### Example:
 	###   [
-	###     {:id=>9, :fd=>27, :type=>1, :last_ping=>0, :last_read=>0, :last_write=>0, 
+	###     {:id=>9, :fd=>27, :type=>1, :last_ping=>0, :last_read=>0, :last_write=>0,
 	###      :bytes_read=>319, :bytes_written=>1065}
 	###   ]
 	def conn_status
 	end
 
 
-	### Shuts down the control port permanently in case you want to keep it from 
+	### Shuts down the control port permanently in case you want to keep it from
 	### being accessed for some reason.
 	def control_stop
 		self.request( :control_stop )

File lib/mongrel2/httpresponse.rb

 	### Returns true if the response status means the response
 	### shouldn't have a body.
 	def bodiless?
-		return HTTP::BODILESS_HTTP_RESPONSE_CODES.include?( self.status )
+		return self.body.nil? ||
+			HTTP::BODILESS_HTTP_RESPONSE_CODES.include?( self.status )
 	end
 
 
 	### Get the length of the body IO. If the IO's offset is somewhere other than
 	### the beginning or end, the size of the remainder is used.
 	def get_content_length
+		return 0 if self.bodiless?
+
 		if self.body.pos.nonzero? && !self.body.eof?
 			self.log.info "Calculating content length based on an offset of %d" % [ self.body.pos ]
 			return self.body.size - self.body.pos
 	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? || 
+		if self.body.size.nonzero? ||
 		   (self.header.content_length && self.header.content_length.nonzero?)
 			return HTTP::OK
 

File lib/mongrel2/jsonrequest.rb

 	attr_reader :data
 
 
-	### Returns +true+ if the request is a special Mongrel2 'disconnect' 
+	### Returns +true+ if the request is a special Mongrel2 'disconnect'
 	### notification.
 	def is_disconnect?
 		return true if self.data['type'] == 'disconnect'

File lib/mongrel2/request.rb

 
 	### Create a Mongrel2::Response that will respond to the same server/connection as
 	### the receiver. If you wish your specialized Request class to have a corresponding
-	### response type, you can override the Mongrel2::Request.response_class method 
+	### response type, you can override the Mongrel2::Request.response_class method
 	### to achieve that.
 	def response
 		return @response ||= self.class.response_class.from_request( self )

File spec/lib/constants.rb

 		# month        = "Jan" | "Feb" | "Mar" | "Apr"
 		#              | "May" | "Jun" | "Jul" | "Aug"
 		#              | "Sep" | "Oct" | "Nov" | "Dec"
-		MONTH = Regexp.union( %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] ) 
+		MONTH = Regexp.union( %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] )
 
 		# Match an RFC1123 "HTTP date"
 		# rfc1123-date = wkday "," SP date1 SP time SP "GMT"

File spec/lib/helpers.rb

 	end
 
 
-	### Make a new-style (TNetstring headers) raw Mongrel2 request from the specified +opts+ 
+	### Make a new-style (TNetstring headers) raw Mongrel2 request from the specified +opts+
 	### and return it as a String.
 	def make_tn_request( opts={} )
 		opts = TEST_REQUEST_OPTS.merge( opts )

File spec/mongrel2/config/dsl_spec.rb

 		it "can set the expert tweakable settings en masse" do
 			result = settings(
 				"zeromq.threads"         => 8,
-				"upload.temp_store"      => "/home/zedshaw/projects/mongrel2/tmp/upload.XXXXXX", 
-				"upload.temp_store_mode" => "0666" 
+				"upload.temp_store"      => "/home/zedshaw/projects/mongrel2/tmp/upload.XXXXXX",
+				"upload.temp_store_mode" => "0666"
 			)
 
 			result.should be_an( Array )

File spec/mongrel2/httpresponse_spec.rb

 	end
 
 
+	it "knows that a response with a body explicitly set to nil is bodiless" do
+		@response.status = HTTP::CREATED
+		@response.body = nil
+		@response.should be_bodiless()
+	end
+
+
 	it "knows what the response content type is" do
 		@response.headers['Content-Type'] = 'text/erotica'
 		@response.content_type.should == 'text/erotica'
 	end
 
 
-	it "returns a body length of 0 if it's a bodiless reponse" do
+	it "returns a body length of 0 if it's a bodiless status code" do
+		@response.puts "Some stuff"
 		@response.status = HTTP::NO_CONTENT
 		@response.get_content_length.should == 0
 	end
 
 
+	it "returns a body length of 0 if it has a nil body" do
+		@response.body = nil
+		@response.status = HTTP::CREATED
+		@response.get_content_length.should == 0
+	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

File spec/mongrel2/websocket_spec.rb

 			@factory.continuation( '/websock' ).opcode.should == :continuation
 		end
 
-		it "knows that is opcode is 'text' if its opcode is 0x1" do 
+		it "knows that is opcode is 'text' if its opcode is 0x1" do
 			@factory.text( '/websock', 'Hello!' ).opcode.should == :text
 		end
 
-		it "knows that is opcode is 'binary' if its opcode is 0x2" do 
+		it "knows that is opcode is 'binary' if its opcode is 0x2" do
 			@factory.binary( '/websock', 'Hello!' ).opcode.should == :binary
 		end
 
-		it "knows that is opcode is 'close' if its opcode is 0x8" do 
+		it "knows that is opcode is 'close' if its opcode is 0x8" do
 			@factory.close( '/websock' ).opcode.should == :close
 		end
 
-		it "knows that is opcode is 'ping' if its opcode is 0x9" do 
+		it "knows that is opcode is 'ping' if its opcode is 0x9" do
 			@factory.ping( '/websock' ).opcode.should == :ping
 		end
 
-		it "knows that is opcode is 'pong' if its opcode is 0xA" do 
+		it "knows that is opcode is 'pong' if its opcode is 0xA" do
 			@factory.pong( '/websock' ).opcode.should == :pong
 		end