Skip to content

Commit

Permalink
Merge branch 'conditional-get'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 13, 2008
2 parents 992fda1 + 08b0cf0 commit 45b79d9
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 303 deletions.
10 changes: 8 additions & 2 deletions actionpack/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
* Update Prototype to 1.6.0.2 #599 [Patrick Joyce]

* Conditional GET utility methods. [Jeremy Kemper]
* etag!([:admin, post, current_user]) sets the ETag response header and returns head(:not_modified) if it matches the If-None-Match request header.
* last_modified!(post.updated_at) sets Last-Modified and returns head(:not_modified) if it's no later than If-Modified-Since.
response.last_modified = @post.updated_at
response.etag = [:admin, @post, current_user]

if request.fresh?(response)
head :not_modified
else
# render ...
end

* All 2xx requests are considered successful [Josh Peek]

Expand Down
45 changes: 3 additions & 42 deletions actionpack/lib/action_controller/cgi_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SessionFixationAttempt < StandardError #:nodoc:
:session_path => "/", # available to all paths in app
:session_key => "_session_id",
:cookie_only => true
} unless const_defined?(:DEFAULT_SESSION_OPTIONS)
}

def initialize(cgi, session_options = {})
@cgi = cgi
Expand All @@ -61,53 +61,14 @@ def query_string
end
end

# The request body is an IO input stream. If the RAW_POST_DATA environment
# variable is already set, wrap it in a StringIO.
def body
if raw_post = env['RAW_POST_DATA']
raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
StringIO.new(raw_post)
else
@cgi.stdinput
end
end

def query_parameters
@query_parameters ||= self.class.parse_query_parameters(query_string)
end

def request_parameters
@request_parameters ||= parse_formatted_request_parameters
def body_stream #:nodoc:
@cgi.stdinput
end

def cookies
@cgi.cookies.freeze
end

def host_with_port_without_standard_port_handling
if forwarded = env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
elsif http_host = env['HTTP_HOST']
http_host
elsif server_name = env['SERVER_NAME']
server_name
else
"#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
end
end

def host
host_with_port_without_standard_port_handling.sub(/:\d+$/, '')
end

def port
if host_with_port_without_standard_port_handling =~ /:(\d+)$/
$1.to_i
else
standard_port
end
end

def session
unless defined?(@session)
if @session_options == false
Expand Down
30 changes: 16 additions & 14 deletions actionpack/lib/action_controller/headers.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
require 'active_support/memoizable'

module ActionController
module Http
class Headers < ::Hash

def initialize(constructor = {})
if constructor.is_a?(Hash)
extend ActiveSupport::Memoizable

def initialize(*args)
if args.size == 1 && args[0].is_a?(Hash)
super()
update(constructor)
update(args[0])
else
super(constructor)
super
end
end

def [](header_name)
if include?(header_name)
super
super
else
super(normalize_header(header_name))
super(env_name(header_name))
end
end



private
# Takes an HTTP header name and returns it in the
# format
def normalize_header(header_name)
# Converts a HTTP header name to an environment variable name.
def env_name(header_name)
"HTTP_#{header_name.upcase.gsub(/-/, '_')}"
end
memoize :env_name
end
end
end
end
52 changes: 5 additions & 47 deletions actionpack/lib/action_controller/rack_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module ActionController #:nodoc:
class RackRequest < AbstractRequest #:nodoc:
attr_accessor :env, :session_options
attr_accessor :session_options
attr_reader :cgi

class SessionFixationAttempt < StandardError #:nodoc:
Expand All @@ -15,7 +15,7 @@ class SessionFixationAttempt < StandardError #:nodoc:
:session_path => "/", # available to all paths in app
:session_key => "_session_id",
:cookie_only => true
} unless const_defined?(:DEFAULT_SESSION_OPTIONS)
}

def initialize(env, session_options = DEFAULT_SESSION_OPTIONS)
@session_options = session_options
Expand All @@ -30,35 +30,21 @@ def initialize(env, session_options = DEFAULT_SESSION_OPTIONS)
SERVER_NAME SERVER_PROTOCOL

HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM
HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env|
define_method(env.sub(/^HTTP_/n, '').downcase) do
@env[env]
end
end

# The request body is an IO input stream. If the RAW_POST_DATA environment
# variable is already set, wrap it in a StringIO.
def body
if raw_post = env['RAW_POST_DATA']
StringIO.new(raw_post)
else
@env['rack.input']
end
def body_stream #:nodoc:
@env['rack.input']
end

def key?(key)
@env.key?(key)
end

def query_parameters
@query_parameters ||= self.class.parse_query_parameters(query_string)
end

def request_parameters
@request_parameters ||= parse_formatted_request_parameters
end

def cookies
return {} unless @env["HTTP_COOKIE"]

Expand All @@ -70,34 +56,6 @@ def cookies
@env["rack.request.cookie_hash"]
end

def host_with_port_without_standard_port_handling
if forwarded = @env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
elsif http_host = @env['HTTP_HOST']
http_host
elsif server_name = @env['SERVER_NAME']
server_name
else
"#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
end
end

def host
host_with_port_without_standard_port_handling.sub(/:\d+$/, '')
end

def port
if host_with_port_without_standard_port_handling =~ /:(\d+)$/
$1.to_i
else
standard_port
end
end

def remote_addr
@env['REMOTE_ADDR']
end

def server_port
@env['SERVER_PORT'].to_i
end
Expand Down

0 comments on commit 45b79d9

Please sign in to comment.