Finalize this response before sending it over HTTP

Property definitions

nitcorn $ HttpResponse :: finalize
	# Finalize this response before sending it over HTTP
	fun finalize
	do
		# Set the content length if not already set
		if not header.keys.has("Content-Length") then
			# Size of the body
			var len
			var body = self.body
			if body isa Text then
				len = body.byte_length
			else if body isa Bytes then
				len = body.length
			else
				# We need the length, but there is no length in a writable.
				# So just render it as a bytes then measure :/
				body = body.write_to_bytes
				len = body.length
				# Keep the body as bytes since we have it
				self.body = body
			end

			# Size of included files
			for path in files do
				# TODO handle these error cases elsewhere, an error here will result in an invalid response
				if not path.file_exists then
					print_error "File does not exists at '{path}'"
					continue
				end

				var stat = path.file_stat
				if stat == null then
					print_error "Failed to stat file at '{path}'"
					continue
				end

				len += stat.size
			end

			# Set header
			header["Content-Length"] = len.to_s
		end

		# Set server ID
		if not header.keys.has("Server") then header["Server"] = "nitcorn"
	end
lib/nitcorn/http_response.nit:47,2--91,4

nitcorn :: sessions $ HttpResponse :: finalize
	redef fun finalize
	do
		super

		var session = self.session
		if session != null then
			header["Set-Cookie"] = "nitcorn_session={session.id_hash}; HttpOnly"
		else
			# Make sure there are no cookie left client side
			header["Set-Cookie"] = "nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT"
		end
	end
lib/nitcorn/sessions.nit:59,2--70,4