From 32462f0e0cfdfe73b00ab2fab42a6addcf1fc146 Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow Date: Mon, 17 Nov 2008 19:04:38 +0800 Subject: [PATCH] Add constants for strings that are commonly used in HTTP headers in ActionController::Http::Headers and use them. --- actionpack/lib/action_controller/base.rb | 10 ++-- .../lib/action_controller/caching/actions.rb | 2 +- actionpack/lib/action_controller/caching/pages.rb | 2 +- actionpack/lib/action_controller/components.rb | 2 +- actionpack/lib/action_controller/headers.rb | 10 +++++ actionpack/lib/action_controller/rack_process.rb | 10 ++-- actionpack/lib/action_controller/response.rb | 42 ++++++++++---------- actionpack/lib/action_controller/streaming.rb | 10 ++-- 8 files changed, 49 insertions(+), 39 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index f35c42f..0bcffc0 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -994,8 +994,8 @@ module ActionController #:nodoc: @performed_redirect = false response.redirected_to = nil response.redirected_to_method_params = nil - response.headers['Status'] = DEFAULT_RENDER_STATUS_CODE - response.headers.delete('Location') + response.headers[Http::Headers::STATUS] = DEFAULT_RENDER_STATUS_CODE + response.headers.delete(Http::Headers::LOCATION) end # Erase both render and redirect results @@ -1151,13 +1151,13 @@ module ActionController #:nodoc: cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys) cache_options.delete_if { |k,v| v.nil? or v == false } cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"} - response.headers["Cache-Control"] = cache_control.join(', ') + response.headers[Http::Headers::CACHE_CONTROL] = cache_control.join(', ') end # Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or # intermediate caches (like caching proxy servers). def expires_now #:doc: - response.headers["Cache-Control"] = "no-cache" + response.headers[Http::Headers::CACHE_CONTROL] = "no-cache" end # Resets the session by clearing out all the objects stored within and initializing a new session object. @@ -1177,7 +1177,7 @@ module ActionController #:nodoc: def render_for_text(text = nil, status = nil, append_response = false) #:nodoc: @performed_render = true - response.headers['Status'] = interpret_status(status || DEFAULT_RENDER_STATUS_CODE) + response.headers[Http::Headers::STATUS] = interpret_status(status || DEFAULT_RENDER_STATUS_CODE) if append_response response.body ||= '' diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index 7c803a9..4b75e4c 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -113,7 +113,7 @@ module ActionController #:nodoc: end def caching_allowed(controller) - controller.request.get? && controller.response.headers['Status'].to_i == 200 + controller.request.get? && controller.response.headers[Http::Headers::STATUS].to_i == 200 end def cache_layout? diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index a70ed72..6c6a3b7 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -147,7 +147,7 @@ module ActionController #:nodoc: private def caching_allowed - request.get? && response.headers['Status'].to_i == 200 + request.get? && response.headers[Http::Headers::STATUS].to_i == 200 end end end diff --git a/actionpack/lib/action_controller/components.rb b/actionpack/lib/action_controller/components.rb index f446b91..63da24f 100644 --- a/actionpack/lib/action_controller/components.rb +++ b/actionpack/lib/action_controller/components.rb @@ -80,7 +80,7 @@ module ActionController #:nodoc: # Renders the component specified as the response for the current method def render_component(options) #:doc: component_logging(options) do - render_for_text(component_response(options, true).body, response.headers["Status"]) + render_for_text(component_response(options, true).body, response.headers[Http::Headers::STATUS]) end end deprecate :render_component => "Please install render_component plugin from http://github.com/rails/render_component/tree/master" diff --git a/actionpack/lib/action_controller/headers.rb b/actionpack/lib/action_controller/headers.rb index 139669c..2fd3e2d 100644 --- a/actionpack/lib/action_controller/headers.rb +++ b/actionpack/lib/action_controller/headers.rb @@ -5,6 +5,16 @@ module ActionController class Headers < ::Hash extend ActiveSupport::Memoizable + CACHE_CONTROL = 'Cache-Control'.freeze + CONTENT_DISPOSITION = 'Content-Disposition'.freeze + CONTENT_TRANSFER_ENCODING = 'Content-Transfer-Encoding'.freeze + CONTENT_TYPE = 'Content-Type'.freeze + CONTENT_LENGTH = 'Content-Length'.freeze + ETAG = 'ETag'.freeze + LAST_MODIFIED = 'Last-Modified'.freeze + LOCATION = 'Location'.freeze + STATUS = 'Status'.freeze + def initialize(*args) if args.size == 1 && args[0].is_a?(Hash) super() diff --git a/actionpack/lib/action_controller/rack_process.rb b/actionpack/lib/action_controller/rack_process.rb index e8ea370..8d13e60 100644 --- a/actionpack/lib/action_controller/rack_process.rb +++ b/actionpack/lib/action_controller/rack_process.rb @@ -171,9 +171,9 @@ end_msg set_cookies! @block = block - @status = headers.delete("Status") + @status = headers.delete(Http::Headers::STATUS) if [204, 304].include?(status.to_i) - headers.delete("Content-Type") + headers.delete(Http::Headers::CONTENT_TYPE) [status, headers.to_hash, []] else [status, headers.to_hash, self] @@ -228,13 +228,13 @@ end_msg def convert_content_type! super - headers['Content-Type'] = headers.delete('type') || "text/html" - headers['Content-Type'] += "; charset=" + headers.delete('charset') if headers['charset'] + headers[Http::Headers::CONTENT_TYPE] = headers.delete('type') || "text/html" + headers[Http::Headers::CONTENT_TYPE] += "; charset=" + headers.delete('charset') if headers['charset'] end def set_content_length! super - headers["Content-Length"] = headers["Content-Length"].to_s if headers["Content-Length"] + headers[Http::Headers::CONTENT_LENGTH] = headers[Http::Headers::CONTENT_LENGTH].to_s if headers[Http::Headers::CONTENT_LENGTH] end def set_status! diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb index 559c38e..cb35c95 100644 --- a/actionpack/lib/action_controller/response.rb +++ b/actionpack/lib/action_controller/response.rb @@ -46,11 +46,11 @@ module ActionController # :nodoc: @body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], [] end - def status; headers['Status'] end - def status=(status) headers['Status'] = status end + def status; headers[Http::Headers::STATUS] end + def status=(status) headers[Http::Headers::STATUS] = status end - def location; headers['Location'] end - def location=(url) headers['Location'] = url end + def location; headers[Http::Headers::LOCATION] end + def location=(url) headers[Http::Headers::LOCATION] = url end # Sets the HTTP response's content MIME type. For example, in the controller @@ -62,7 +62,7 @@ module ActionController # :nodoc: # the character set information will also be included in the content type # information. def content_type=(mime_type) - self.headers["Content-Type"] = + self.headers[Http::Headers::CONTENT_TYPE] = if mime_type =~ /charset/ || (c = charset).nil? mime_type.to_s else @@ -72,14 +72,14 @@ module ActionController # :nodoc: # Returns the response's content MIME type, or nil if content type has been set. def content_type - content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0] + content_type = String(headers[Http::Headers::CONTENT_TYPE] || headers["type"]).split(";")[0] content_type.blank? ? nil : content_type end # Set the charset of the Content-Type header. Set to nil to remove it. # If no content type is set, it defaults to HTML. def charset=(charset) - headers["Content-Type"] = + headers[Http::Headers::CONTENT_TYPE] = if charset "#{content_type || Mime::HTML}; charset=#{charset}" else @@ -88,34 +88,34 @@ module ActionController # :nodoc: end def charset - charset = String(headers["Content-Type"] || headers["type"]).split(";")[1] + charset = String(headers[Http::Headers::CONTENT_TYPE] || headers["type"]).split(";")[1] charset.blank? ? nil : charset.strip.split("=")[1] end def last_modified - if last = headers['Last-Modified'] + if last = headers[Http::Headers::LAST_MODIFIED] Time.httpdate(last) end end def last_modified? - headers.include?('Last-Modified') + headers.include?(Http::Headers::LAST_MODIFIED) end def last_modified=(utc_time) - headers['Last-Modified'] = utc_time.httpdate + headers[Http::Headers::LAST_MODIFIED] = utc_time.httpdate end def etag - headers['ETag'] + headers[Http::Headers::ETAG] end def etag? - headers.include?('ETag') + headers.include?(Http::Headers::ETAG) end def etag=(etag) - headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}") + headers[Http::Headers::ETAG] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}") end def redirect(url, status) @@ -125,7 +125,7 @@ module ActionController # :nodoc: end def sending_file? - headers["Content-Transfer-Encoding"] == "binary" + headers[Http::Headers::CONTENT_TRANSFER_ENCODING] == "binary" end def assign_default_content_type_and_charset! @@ -162,19 +162,19 @@ module ActionController # :nodoc: end def set_conditional_cache_control! - if headers['Cache-Control'] == DEFAULT_HEADERS['Cache-Control'] - headers['Cache-Control'] = 'private, max-age=0, must-revalidate' + if headers[Http::Headers::CACHE_CONTROL] == DEFAULT_HEADERS[Http::Headers::CACHE_CONTROL] + headers[Http::Headers::CACHE_CONTROL] = 'private, max-age=0, must-revalidate' end end def convert_content_type! - if content_type = headers.delete("Content-Type") + if content_type = headers.delete(Http::Headers::CONTENT_TYPE) self.headers["type"] = content_type end - if content_type = headers.delete("Content-type") + if content_type = headers.delete(Http::Headers::CONTENT_TYPE) self.headers["type"] = content_type end - if content_type = headers.delete("content-type") + if content_type = headers.delete(Http::Headers::CONTENT_TYPE) self.headers["type"] = content_type end end @@ -183,7 +183,7 @@ module ActionController # :nodoc: # for, say, a 2GB streaming file. def set_content_length! unless body.respond_to?(:call) || (status && status[0..2] == '304') - self.headers["Content-Length"] ||= body.size + self.headers[Http::Headers::CONTENT_LENGTH] ||= body.size end end end diff --git a/actionpack/lib/action_controller/streaming.rb b/actionpack/lib/action_controller/streaming.rb index 333fb61..eb2cf25 100644 --- a/actionpack/lib/action_controller/streaming.rb +++ b/actionpack/lib/action_controller/streaming.rb @@ -144,10 +144,10 @@ module ActionController #:nodoc: disposition <<= %(; filename="#{options[:filename]}") if options[:filename] headers.update( - 'Content-Length' => options[:length], - 'Content-Type' => options[:type].to_s.strip, # fixes a problem with extra '\r' with some browsers - 'Content-Disposition' => disposition, - 'Content-Transfer-Encoding' => 'binary' + Http::Headers::CONTENT_LENGTH => options[:length], + Http::Headers::CONTENT_TYPE => options[:type].to_s.strip, # fixes a problem with extra '\r' with some browsers + Http::Headers::CONTENT_DISPOSITION => disposition, + Http::Headers::CONTENT_TRANSFER_ENCODING => 'binary' ) # Fix a problem with IE 6.0 on opening downloaded files: @@ -156,7 +156,7 @@ module ActionController #:nodoc: # after it displays the "open/save" dialog, which means that if you # hit "open" the file isn't there anymore when the application that # is called for handling the download is run, so let's workaround that - headers['Cache-Control'] = 'private' if headers['Cache-Control'] == 'no-cache' + headers[Http::Headers::CACHE_CONTROL] = 'private' if headers[Http::Headers::CACHE_CONTROL] == 'no-cache' end end end -- 1.6.0.2