Sha256: fd98fd522504943ef384477f65072fbee1aec79bfbfcab3c06460ce087ee1821

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The drop_body plugin automatically drops the body and
    # Content-Type/Content-Length headers from the response if
    # the response status indicates that the response should
    # not include a body (response statuses 100, 101, 102, 204, 205,
    # and 304).
    module DropBody
      module ResponseMethods
        DROP_BODY_STATUSES = [100, 101, 102, 204, 205, 304].freeze
        EMPTY_BODY = [].freeze
        CONTENT_LENGTH = "Content-Length".freeze
        CONTENT_TYPE = "Content-Type".freeze

        # If the response status indicates a body should not be
        # returned, use an empty body and remove the Content-Length
        # and Content-Type headers.
        def finish
          r = super
          if DROP_BODY_STATUSES.include?(r[0])
            r[2] = EMPTY_BODY
            h = r[1]
            h.delete(CONTENT_LENGTH)
            h.delete(CONTENT_TYPE)
          end
          r
        end
      end
    end

    register_plugin(:drop_body, DropBody)
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
roda-2.27.0 lib/roda/plugins/drop_body.rb
roda-2.26.0 lib/roda/plugins/drop_body.rb
roda-2.25.0 lib/roda/plugins/drop_body.rb
roda-2.24.0 lib/roda/plugins/drop_body.rb
roda-2.23.0 lib/roda/plugins/drop_body.rb
roda-2.22.0 lib/roda/plugins/drop_body.rb
roda-2.21.0 lib/roda/plugins/drop_body.rb
roda-2.20.0 lib/roda/plugins/drop_body.rb
roda-2.19.0 lib/roda/plugins/drop_body.rb
roda-2.18.0 lib/roda/plugins/drop_body.rb
roda-2.17.0 lib/roda/plugins/drop_body.rb
roda-2.16.0 lib/roda/plugins/drop_body.rb
roda-2.15.0 lib/roda/plugins/drop_body.rb
roda-2.14.0 lib/roda/plugins/drop_body.rb
roda-2.13.0 lib/roda/plugins/drop_body.rb
roda-2.12.0 lib/roda/plugins/drop_body.rb
roda-2.11.0 lib/roda/plugins/drop_body.rb