File: content_type.rb

package info (click to toggle)
ruby-rack 1.5.2-3%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 1,340 kB
  • ctags: 968
  • sloc: ruby: 12,617; sh: 12; makefile: 5
file content (29 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'rack/utils'

module Rack

  # Sets the Content-Type header on responses which don't have one.
  #
  # Builder Usage:
  #   use Rack::ContentType, "text/plain"
  #
  # When no content type argument is provided, "text/html" is assumed.
  class ContentType
    include Rack::Utils

    def initialize(app, content_type = "text/html")
      @app, @content_type = app, content_type
    end

    def call(env)
      status, headers, body = @app.call(env)
      headers = Utils::HeaderHash.new(headers)

      unless STATUS_WITH_NO_ENTITY_BODY.include?(status)
        headers['Content-Type'] ||= @content_type
      end

      [status, headers, body]
    end
  end
end