lib/roda/response.rb in roda-3.53.0 vs lib/roda/response.rb in roda-3.54.0

- old
+ new

@@ -35,15 +35,26 @@ # The status code to use for the response. If none is given, will use 200 # code for non-empty responses and a 404 code for empty responses. attr_accessor :status - # Set the default headers when creating a response. - def initialize - @headers = {} - @body = [] - @length = 0 + # :nocov: + if defined?(Rack::Headers) && Rack::Headers.is_a?(Class) + # Set the default headers when creating a response. + def initialize + @headers = Rack::Headers.new + @body = [] + @length = 0 + end + else + # :nocov: + # Set the default headers when creating a response. + def initialize + @headers = {} + @body = [] + @length = 0 + end end # Return the response header with the given key. Example: # # response['Content-Type'] # => 'text/html' @@ -94,12 +105,11 @@ if b.empty? s = @status || 404 if (s == 304 || s == 204 || (s >= 100 && s <= 199)) h.delete("Content-Type") elsif s == 205 - h.delete("Content-Type") - h["Content-Length"] = '0' + empty_205_headers(h) else h["Content-Length"] ||= '0' end else s = @status || default_status @@ -155,9 +165,26 @@ @body << s nil end private + + # :nocov: + if Rack.release < '2.0.2' + # Don't use a content length for empty 205 responses on + # rack 1, as it violates Rack::Lint in that version. + def empty_205_headers(headers) + headers.delete("Content-Type") + headers.delete("Content-Length") + end + # :nocov: + else + # Set the content length for empty 205 responses to 0 + def empty_205_headers(headers) + headers.delete("Content-Type") + headers["Content-Length"] = '0' + end + end # For each default header, if a header has not already been set for the # response, set the header in the response. def set_default_headers h = @headers