Skip to content
This repository
Browse code

Remove legacy processing and content_length

  * convert_content_type! is handled by assign_default_content_type_and_charset!
  * set_content_length! should be handled by the endpoint server. Otherwise
    each middleware that modifies the body has to do the expensive work of
    recalculating content_length.
  * convert_language! appears to be legacy. There are no tests for this
  * convert_cookies! should be handled by the new HeaderHash in Rack
  * Use an integer for .status's internal representation to avoid needing to
    do String manipulation just to find out the status
  • Loading branch information...
commit b53f00690173797a39ff46e55dd25c20581c3d00 1 parent 503ce1d
Yehuda Katz authored July 30, 2009
50  actionpack/lib/action_dispatch/http/response.rb
@@ -46,18 +46,22 @@ def initialize
46 46
       @header = Rack::Utils::HeaderHash.new
47 47
     end
48 48
 
  49
+    def status=(status)
  50
+      @status = status.to_i
  51
+    end
  52
+
49 53
     # The response code of the request
50 54
     def response_code
51  
-      status.to_s[0,3].to_i rescue 0
  55
+      @status
52 56
     end
53 57
 
54 58
     # Returns a String to ensure compatibility with Net::HTTPResponse
55 59
     def code
56  
-      status.to_s.split(' ')[0]
  60
+      @status.to_s
57 61
     end
58 62
 
59 63
     def message
60  
-      status.to_s.split(' ',2)[1] || StatusCodes::STATUS_CODES[response_code]
  64
+      StatusCodes::STATUS_CODES[@status]
61 65
     end
62 66
     alias_method :status_message, :message
63 67
 
@@ -143,10 +147,7 @@ def assign_default_content_type_and_charset!
143 147
     def prepare!
144 148
       assign_default_content_type_and_charset!
145 149
       handle_conditional_get!
146  
-      set_content_length!
147  
-      convert_content_type!
148  
-      convert_language!
149  
-      convert_cookies!
  150
+      self["Set-Cookie"] ||= ""
150 151
     end
151 152
 
152 153
     def each(&callback)
@@ -203,7 +204,7 @@ def handle_conditional_get!
203 204
           self.etag = body
204 205
 
205 206
           if request && request.etag_matches?(etag)
206  
-            self.status = '304 Not Modified'
  207
+            self.status = 304
207 208
             self.body = []
208 209
           end
209 210
 
@@ -214,7 +215,7 @@ def handle_conditional_get!
214 215
       end
215 216
 
216 217
       def nonempty_ok_response?
217  
-        ok = !status || status.to_s[0..2] == '200'
  218
+        ok = !@status || @status == 200
218 219
         ok && string_body?
219 220
       end
220 221
 
@@ -238,36 +239,5 @@ def set_conditional_cache_control!
238 239
 
239 240
         headers["Cache-Control"] = options.join(", ")
240 241
       end
241  
-
242  
-      def convert_content_type!
243  
-        headers['Content-Type'] ||= "text/html"
244  
-        headers['Content-Type'] += "; charset=" + headers.delete('charset') if headers['charset']
245  
-      end
246  
-
247  
-      # Don't set the Content-Length for block-based bodies as that would mean
248  
-      # reading it all into memory. Not nice for, say, a 2GB streaming file.
249  
-      def set_content_length!
250  
-        if status && status.to_s[0..2] == '204'
251  
-          headers.delete('Content-Length')
252  
-        elsif length = headers['Content-Length']
253  
-          headers['Content-Length'] = length.to_s
254  
-        elsif string_body? && (!status || status.to_s[0..2] != '304')
255  
-          headers["Content-Length"] = Rack::Utils.bytesize(body).to_s
256  
-        end
257  
-      end
258  
-
259  
-      def convert_language!
260  
-        headers["Content-Language"] = headers.delete("language") if headers["language"]
261  
-      end
262  
-
263  
-      def convert_cookies!
264  
-        headers['Set-Cookie'] =
265  
-          if header = headers['Set-Cookie']
266  
-            header = header.split("\n") if header.respond_to?(:to_str)
267  
-            header.compact
268  
-          else
269  
-            []
270  
-          end
271  
-      end
272 242
   end
273 243
 end
2  actionpack/test/dispatch/request/multipart_params_parsing_test.rb
@@ -120,8 +120,6 @@ def teardown
120 120
       fixture = FIXTURE_PATH + "/mona_lisa.jpg"
121 121
       params = { :uploaded_data => fixture_file_upload(fixture, "image/jpg") }
122 122
       post '/read', params
123  
-      expected_length = 'File: '.length + File.size(fixture)
124  
-      assert_equal expected_length, response.content_length
125 123
     end
126 124
   end
127 125
 
6  actionpack/test/dispatch/response_test.rb
@@ -15,8 +15,7 @@ def setup
15 15
       "Content-Type" => "text/html; charset=utf-8",
16 16
       "Cache-Control" => "max-age=0, private, must-revalidate",
17 17
       "ETag" => '"65a8e27d8879283831b664bd8b7f0ad4"',
18  
-      "Set-Cookie" => "",
19  
-      "Content-Length" => "13"
  18
+      "Set-Cookie" => ""
20 19
     }, headers)
21 20
 
22 21
     parts = []
@@ -34,8 +33,7 @@ def setup
34 33
       "Content-Type" => "text/html; charset=utf-8",
35 34
       "Cache-Control" => "max-age=0, private, must-revalidate",
36 35
       "ETag" => '"ebb5e89e8a94e9dd22abf5d915d112b2"',
37  
-      "Set-Cookie" => "",
38  
-      "Content-Length" => "8"
  36
+      "Set-Cookie" => ""
39 37
     }, headers)
40 38
   end
41 39
 
3  actionpack/test/new_base/base_test.rb
@@ -34,7 +34,6 @@ class BaseTest < SimpleRouteCase
34 34
       assert_body "success"
35 35
       assert_status 200
36 36
       assert_content_type "text/html; charset=utf-8"
37  
-      assert_header "Content-Length", "7"
38 37
     end
39 38
 
40 39
     # :api: plugin
@@ -42,7 +41,6 @@ class BaseTest < SimpleRouteCase
42 41
       get "/dispatching/simple/modify_response_body"
43 42
 
44 43
       assert_body "success"
45  
-      assert_header "Content-Length", "7" # setting the body manually sets the content length
46 44
     end
47 45
 
48 46
     # :api: plugin
@@ -50,7 +48,6 @@ class BaseTest < SimpleRouteCase
50 48
       get "/dispatching/simple/modify_response_body_twice"
51 49
 
52 50
       assert_body "success!"
53  
-      assert_header "Content-Length", "8"
54 51
     end
55 52
 
56 53
     test "controller path" do

0 notes on commit b53f006

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