File: media_type.rb

package info (click to toggle)
ruby-rack 3.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,576 kB
  • sloc: ruby: 15,492; sh: 12; makefile: 7; javascript: 1
file content (52 lines) | stat: -rw-r--r-- 1,885 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

module Rack
  # Rack::MediaType parse media type and parameters out of content_type string

  class MediaType
    SPLIT_PATTERN = /[;,]/

    class << self
      # The media type (type/subtype) portion of the CONTENT_TYPE header
      # without any media type parameters. e.g., when CONTENT_TYPE is
      # "text/plain;charset=utf-8", the media-type is "text/plain".
      #
      # For more information on the use of media types in HTTP, see:
      # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
      def type(content_type)
        return nil unless content_type && !content_type.empty?
        type = content_type.split(SPLIT_PATTERN, 2).first
        type.rstrip!
        type.downcase!
        type
      end

      # The media type parameters provided in CONTENT_TYPE as a Hash, or
      # an empty Hash if no CONTENT_TYPE or media-type parameters were
      # provided.  e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
      # this method responds with the following Hash:
      #   { 'charset' => 'utf-8' }
      #
      # This will pass back parameters with empty strings in the hash if they
      # lack a value (e.g., "text/plain;charset=" will return { 'charset' => '' },
      # and "text/plain;charset" will return { 'charset' => '' }, similarly to 
      # the query params parser (barring the latter case, which returns nil instead)).
      def params(content_type)
        return {} if content_type.nil? || content_type.empty?

        content_type.split(SPLIT_PATTERN)[1..-1].each_with_object({}) do |s, hsh|
          s.strip!
          k, v = s.split('=', 2)
          k.downcase!
          hsh[k] = strip_doublequotes(v)
        end
      end

      private

      def strip_doublequotes(str)
        (str && str.start_with?('"') && str.end_with?('"')) ? str[1..-2] : str || ''
      end
    end
  end
end