soveran / cuba

Rum based microframework for web development.

Home Page:http://cuba.is

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

foca opened this issue · comments

…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?

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

Fixed in 3.3.0.