Skip to content
This repository
Browse code

Ported ConditionalGet to new Base

  • Loading branch information...
commit 94ee9d245289a36207847a684b51dbd1c7a6a4eb 1 parent c1d120a
authored May 11, 2009
1  actionpack/lib/action_controller/new_base/base.rb
@@ -14,6 +14,7 @@ class Base < Http
14 14
     
15 15
     # Legacy modules
16 16
     include SessionManagement
  17
+    include ActionDispatch::StatusCodes
17 18
     
18 19
     # Rails 2.x compatibility
19 20
     use ActionController::Rails2Compatibility
3  actionpack/lib/action_controller/new_base/compatibility.rb
@@ -26,6 +26,9 @@ def render_to_body(options)
26 26
       if options.is_a?(Hash) && options.key?(:template)
27 27
         options[:template].sub!(/^\//, '')
28 28
       end
  29
+      
  30
+      options[:text] = nil if options[:nothing] == true
  31
+
29 32
       super
30 33
     end
31 34
    
91  actionpack/lib/action_controller/new_base/conditional_get.rb
@@ -36,7 +36,96 @@ def fresh_when(options)
36 36
       if request.fresh?(response)
37 37
         head :not_modified
38 38
       end
39  
-    end    
  39
+    end 
  40
+    
  41
+    # Return a response that has no content (merely headers). The options
  42
+    # argument is interpreted to be a hash of header names and values.
  43
+    # This allows you to easily return a response that consists only of
  44
+    # significant headers:
  45
+    #
  46
+    #   head :created, :location => person_path(@person)
  47
+    #
  48
+    # It can also be used to return exceptional conditions:
  49
+    #
  50
+    #   return head(:method_not_allowed) unless request.post?
  51
+    #   return head(:bad_request) unless valid_request?
  52
+    #   render
  53
+    def head(*args)
  54
+      if args.length > 2
  55
+        raise ArgumentError, "too many arguments to head"
  56
+      elsif args.empty?
  57
+        raise ArgumentError, "too few arguments to head"
  58
+      end
  59
+      options = args.extract_options!
  60
+      status = interpret_status(args.shift || options.delete(:status) || :ok)
  61
+
  62
+      options.each do |key, value|
  63
+        headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s
  64
+      end
  65
+
  66
+      render :nothing => true, :status => status
  67
+    end   
  68
+    
  69
+    # Sets the etag and/or last_modified on the response and checks it against
  70
+    # the client request. If the request doesn't match the options provided, the
  71
+    # request is considered stale and should be generated from scratch. Otherwise,
  72
+    # it's fresh and we don't need to generate anything and a reply of "304 Not Modified" is sent.
  73
+    #
  74
+    # Parameters:
  75
+    # * <tt>:etag</tt>
  76
+    # * <tt>:last_modified</tt> 
  77
+    # * <tt>:public</tt> By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches).
  78
+    #
  79
+    # Example:
  80
+    #
  81
+    #   def show
  82
+    #     @article = Article.find(params[:id])
  83
+    #
  84
+    #     if stale?(:etag => @article, :last_modified => @article.created_at.utc)
  85
+    #       @statistics = @article.really_expensive_call
  86
+    #       respond_to do |format|
  87
+    #         # all the supported formats
  88
+    #       end
  89
+    #     end
  90
+    #   end
  91
+    def stale?(options)
  92
+      fresh_when(options)
  93
+      !request.fresh?(response)
  94
+    end
  95
+    
  96
+    # Sets a HTTP 1.1 Cache-Control header. Defaults to issuing a "private" instruction, so that
  97
+    # intermediate caches shouldn't cache the response.
  98
+    #
  99
+    # Examples:
  100
+    #   expires_in 20.minutes
  101
+    #   expires_in 3.hours, :public => true
  102
+    #   expires in 3.hours, 'max-stale' => 5.hours, :public => true
  103
+    #
  104
+    # This method will overwrite an existing Cache-Control header.
  105
+    # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities.
  106
+    def expires_in(seconds, options = {}) #:doc:
  107
+      cache_control = response.headers["Cache-Control"].split(",").map {|k| k.strip }
  108
+
  109
+      cache_control << "max-age=#{seconds}"
  110
+      cache_control.delete("no-cache")
  111
+      if options[:public]
  112
+        cache_control.delete("private")
  113
+        cache_control << "public"
  114
+      else
  115
+        cache_control << "private"
  116
+      end
  117
+      
  118
+      # This allows for additional headers to be passed through like 'max-stale' => 5.hours
  119
+      cache_control += options.symbolize_keys.reject{|k,v| k == :public || k == :private }.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
  120
+      
  121
+      response.headers["Cache-Control"] = cache_control.join(', ')
  122
+    end
  123
+
  124
+    # Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or
  125
+    # intermediate caches (like caching proxy servers).
  126
+    def expires_now #:doc:
  127
+      response.headers["Cache-Control"] = "no-cache"
  128
+    end
40 129
     
41 130
   end
42 131
 end
1  actionpack/lib/action_controller/new_base/http.rb
@@ -42,6 +42,7 @@ def self.call(env)
42 42
     def call(name, env)
43 43
       @_request = ActionDispatch::Request.new(env)
44 44
       @_response = ActionDispatch::Response.new
  45
+      @_response.request = request
45 46
       process(name)
46 47
       @_response.body = response_body
47 48
       @_response.prepare!
1  actionpack/lib/action_controller/new_base/testing.rb
@@ -5,6 +5,7 @@ module Testing
5 5
     def process_with_test(request, response)
6 6
       @_request = request
7 7
       @_response = response
  8
+      @_response.request = request
8 9
       ret = process(request.parameters[:action])
9 10
       @_response.body = self.response_body
10 11
       @_response.prepare!
2  actionpack/lib/action_view/template/text.rb
@@ -6,5 +6,7 @@ def identifier() self end
6 6
     def render(*) self end
7 7
     
8 8
     def mime_type() Mime::HTML end
  9
+      
  10
+    def partial?() false end
9 11
   end
10 12
 end
8  actionpack/test/controller/render_test.rb
@@ -1660,13 +1660,13 @@ def test_render_against_etag_request_should_have_no_content_length_when_match
1660 1660
   def test_render_against_etag_request_should_200_when_no_match
1661 1661
     @request.if_none_match = etag_for("hello somewhere else")
1662 1662
     get :render_hello_world_from_variable
1663  
-    assert_equal "200 OK", @response.status
  1663
+    assert_equal 200, @response.status.to_i
1664 1664
     assert !@response.body.empty?
1665 1665
   end
1666 1666
 
1667 1667
   def test_render_should_not_set_etag_when_last_modified_has_been_specified
1668 1668
     get :render_hello_world_with_last_modified_set
1669  
-    assert_equal "200 OK", @response.status
  1669
+    assert_equal 200, @response.status.to_i
1670 1670
     assert_not_nil @response.last_modified
1671 1671
     assert_nil @response.etag
1672 1672
     assert @response.body.present?
@@ -1752,7 +1752,7 @@ def test_responds_with_last_modified
1752 1752
   def test_request_not_modified
1753 1753
     @request.if_modified_since = @last_modified
1754 1754
     get :conditional_hello
1755  
-    assert_equal "304 Not Modified", @response.status
  1755
+    assert_equal 304, @response.status.to_i
1756 1756
     assert @response.body.blank?, @response.body
1757 1757
     assert_equal @last_modified, @response.headers['Last-Modified']
1758 1758
   end
@@ -1767,7 +1767,7 @@ def test_request_not_modified_but_etag_differs
1767 1767
   def test_request_modified
1768 1768
     @request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
1769 1769
     get :conditional_hello
1770  
-    assert_equal "200 OK", @response.status
  1770
+    assert_equal 200, @response.status.to_i
1771 1771
     assert !@response.body.blank?
1772 1772
     assert_equal @last_modified, @response.headers['Last-Modified']
1773 1773
   end

0 notes on commit 94ee9d2

Please sign in to comment.
Something went wrong with that request. Please try again.