soveran / cuba

Rum based microframework for web development.
http://cuba.is
MIT License
1.44k stars 249 forks source link

HTTP 204, 205 and 304 responses can't have a `Content-Type` header #50

Closed foca closed 10 years ago

foca commented 10 years ago

…and Cuba::Response is setting it regardless of the status code.

Rack::Lint complains rather loudly about it.

I suppose the simplest solution is to do something similar to what was done for the status_code being 404 by default, by initially not setting the Content-Type, and only doing so when finishing the response if the status code is anything but 204/205/304.

Thoughts, @soveran @frodsan @djanowski @cyx?

tynd-zz commented 10 years ago

Something like this perhaps?

diff --git a/lib/cuba.rb b/lib/cuba.rb
index e669114..32258bb 100644
--- a/lib/cuba.rb
+++ b/lib/cuba.rb
@@ -8,9 +8,7 @@ class Cuba
     attr :body
     attr :headers

-    def initialize(status = nil,
-                   headers = { "Content-Type" => "text/html; charset=utf-8" })
-
+    def initialize(status = nil, headers = {})
       @status  = status
       @headers = headers
       @body    = []
@@ -185,6 +183,7 @@ class Cuba
         if res.body.empty?
           res.status = 404
         else
+          res.headers["Content-Type"] ||= "text/html; charset=utf-8"
           res.status = 200
         end
       end
soveran commented 10 years ago

Fixed in 3.3.0.