提交 0a958d84 编写于 作者: K kennyj

Use freezed string constant. Some string literals is used repeatedly.

上级 0df2ef3e
......@@ -4,14 +4,18 @@ module ActionDispatch
module Http
module Cache
module Request
HTTP_IF_MODIFIED_SINCE = 'HTTP_IF_MODIFIED_SINCE'.freeze
HTTP_IF_NONE_MATCH = 'HTTP_IF_NONE_MATCH'.freeze
def if_modified_since
if since = env['HTTP_IF_MODIFIED_SINCE']
if since = env[HTTP_IF_MODIFIED_SINCE]
Time.rfc2822(since) rescue nil
end
end
def if_none_match
env['HTTP_IF_NONE_MATCH']
env[HTTP_IF_NONE_MATCH]
end
def not_modified?(modified_at)
......@@ -43,31 +47,35 @@ module Response
alias :etag? :etag
def last_modified
if last = headers['Last-Modified']
if last = headers[LAST_MODIFIED]
Time.httpdate(last)
end
end
def last_modified?
headers.include?('Last-Modified')
headers.include?(LAST_MODIFIED)
end
def last_modified=(utc_time)
headers['Last-Modified'] = utc_time.httpdate
headers[LAST_MODIFIED] = utc_time.httpdate
end
def etag=(etag)
key = ActiveSupport::Cache.expand_cache_key(etag)
@etag = self["ETag"] = %("#{Digest::MD5.hexdigest(key)}")
@etag = self[ETAG] = %("#{Digest::MD5.hexdigest(key)}")
end
private
LAST_MODIFIED = "Last-Modified".freeze
ETAG = "ETag".freeze
CACHE_CONTROL = "Cache-Control".freeze
def prepare_cache_control!
@cache_control = {}
@etag = self["ETag"]
@etag = self[ETAG]
if cache_control = self["Cache-Control"]
if cache_control = self[CACHE_CONTROL]
cache_control.split(/,\s*/).each do |segment|
first, last = segment.split("=")
@cache_control[first.to_sym] = last || true
......@@ -81,28 +89,32 @@ def handle_conditional_get!
end
end
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate".freeze
NO_CACHE = "no-cache".freeze
PUBLIC = "public".freeze
PRIVATE = "private".freeze
MUST_REVALIDATE = "must-revalidate".freeze
def set_conditional_cache_control!
return if self["Cache-Control"].present?
return if self[CACHE_CONTROL].present?
control = @cache_control
if control.empty?
headers["Cache-Control"] = DEFAULT_CACHE_CONTROL
headers[CACHE_CONTROL] = DEFAULT_CACHE_CONTROL
elsif control[:no_cache]
headers["Cache-Control"] = "no-cache"
headers[CACHE_CONTROL] = NO_CACHE
else
extras = control[:extras]
max_age = control[:max_age]
options = []
options << "max-age=#{max_age.to_i}" if max_age
options << (control[:public] ? "public" : "private")
options << "must-revalidate" if control[:must_revalidate]
options << (control[:public] ? PUBLIC : PRIVATE)
options << MUST_REVALIDATE if control[:must_revalidate]
options.concat(extras) if extras
headers["Cache-Control"] = options.join(", ")
headers[CACHE_CONTROL] = options.join(", ")
end
end
end
......
......@@ -20,6 +20,8 @@ def enabled?
@filters.present?
end
FILTERED = '[FILTERED]'.freeze
def compiled_filter
@compiled_filter ||= begin
regexps, blocks = compile_filter
......@@ -29,7 +31,7 @@ def compiled_filter
original_params.each do |key, value|
if regexps.find { |r| key =~ r }
value = '[FILTERED]'
value = FILTERED
elsif value.is_a?(Hash)
value = filter(value)
elsif value.is_a?(Array)
......
......@@ -53,8 +53,10 @@ class Response
# information.
attr_accessor :charset, :content_type
CONTENT_TYPE = "Content-Type"
CONTENT_TYPE = "Content-Type".freeze
SET_COOKIE = "Set-Cookie".freeze
LOCATION = "Location".freeze
cattr_accessor(:default_charset) { "utf-8" }
include Rack::Response::Helpers
......@@ -66,7 +68,7 @@ def initialize(status = 200, header = {}, body = [])
@sending_file = false
@blank = false
if content_type = self["Content-Type"]
if content_type = self[CONTENT_TYPE]
type, charset = content_type.split(/;\s*charset=/)
@content_type = Mime::Type.lookup(type)
@charset = charset || self.class.default_charset
......@@ -142,12 +144,12 @@ def delete_cookie(key, value={})
end
def location
headers['Location']
headers[LOCATION]
end
alias_method :redirect_url, :location
def location=(url)
headers['Location'] = url
headers[LOCATION] = url
end
def close
......@@ -158,10 +160,10 @@ def to_a
assign_default_content_type_and_charset!
handle_conditional_get!
@header["Set-Cookie"] = @header["Set-Cookie"].join("\n") if @header["Set-Cookie"].respond_to?(:join)
@header[SET_COOKIE] = @header[SET_COOKIE].join("\n") if @header[SET_COOKIE].respond_to?(:join)
if [204, 304].include?(@status)
@header.delete "Content-Type"
@header.delete CONTENT_TYPE
[@status, @header, []]
else
[@status, @header, self]
......@@ -175,7 +177,7 @@ def to_a
# assert_equal 'AuthorOfNewPage', r.cookies['author']
def cookies
cookies = {}
if header = self["Set-Cookie"]
if header = self[SET_COOKIE]
header = header.split("\n") if header.respond_to?(:to_str)
header.each do |cookie|
if pair = cookie.split(';').first
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册