mime_type.rb 5.4 KB
Newer Older
1 2
require 'set'

3
module Mime
4
  SET              = []
5 6
  EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
  LOOKUP           = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
7

D
David Heinemeier Hansson 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21
  # Encapsulates the notion of a mime type. Can be used at render time, for example, with:
  #
  #   class PostsController < ActionController::Base
  #     def show
  #       @post = Post.find(params[:id])
  #
  #       respond_to do |format|
  #         format.html
  #         format.ics { render :text => post.to_ics, :mime_type => Mime::Type["text/calendar"]  }
  #         format.xml { render :xml => @people.to_xml }
  #       end
  #     end
  #   end
  class Type
22 23 24 25
    @@html_types = Set.new [:html, :all]
    @@unverifiable_types = Set.new [:text, :json, :csv, :xml, :rss, :atom, :yaml]
    cattr_reader :html_types, :unverifiable_types

26 27 28 29 30 31 32
    # A simple helper class used in parsing the accept header
    class AcceptItem #:nodoc:
      attr_accessor :order, :name, :q

      def initialize(order, name, q=nil)
        @order = order
        @name = name.strip
33
        q ||= 0.0 if @name == Mime::ALL # default wilcard match to end of list
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        @q = ((q || 1.0).to_f * 100).to_i
      end

      def to_s
        @name
      end

      def <=>(item)
        result = item.q <=> q
        result = order <=> item.order if result == 0
        result
      end

      def ==(item)
        name == (item.respond_to?(:name) ? item.name : item)
      end
    end

52 53 54 55
    class << self
      def lookup(string)
        LOOKUP[string]
      end
56

57 58 59 60
      def lookup_by_extension(extension)
        EXTENSION_LOOKUP[extension]
      end

61
      # Registers an alias that's not used on mime type lookup, but can be referenced directly. Especially useful for
62 63 64 65 66 67
      # rendering different HTML versions depending on the user agent, like an iPhone.
      def register_alias(string, symbol, extension_synonyms = [])
        register(string, symbol, [], extension_synonyms, true)
      end

      def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
68
        Mime.instance_eval { const_set symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) }
69

70
        SET << Mime.const_get(symbol.to_s.upcase)
71

72
        ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
73
        ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
74 75
      end

76
      def parse(accept_header)
77 78 79 80 81 82 83 84 85 86 87
        if accept_header !~ /,/
          [Mime::Type.lookup(accept_header)]
        else
          # keep track of creation order to keep the subsequent sort stable
          list = []
          accept_header.split(/,/).each_with_index do |header, index| 
            params, q = header.split(/;\s*q=/)       
            if params
              params.strip!          
              list << AcceptItem.new(index, params, q) unless params.empty?
            end
88
          end
89
          list.sort!
90

91 92 93
          # Take care of the broken text/xml entry by renaming or deleting it
          text_xml = list.index("text/xml")
          app_xml = list.index(Mime::XML.to_s)
94

95 96 97
          if text_xml && app_xml
            # set the q value to the max of the two
            list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
98

99 100 101 102 103
            # make sure app_xml is ahead of text_xml in the list
            if app_xml > text_xml
              list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
              app_xml, text_xml = text_xml, app_xml
            end
104

105 106
            # delete text_xml from the list
            list.delete_at(text_xml)
107

108 109 110
          elsif text_xml
            list[text_xml].name = Mime::XML.to_s
          end
111

112
          # Look for more specific XML-based types and sort them ahead of app/xml
113

114 115 116
          if app_xml
            idx = app_xml
            app_xml_type = list[app_xml]
117

118 119 120 121 122 123 124 125
            while(idx < list.length)
              type = list[idx]
              break if type.q < app_xml_type.q
              if type.name =~ /\+xml$/
                list[app_xml], list[idx] = list[idx], list[app_xml]
                app_xml = idx
              end
              idx += 1
126 127
            end
          end
128

129 130 131
          list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
          list
        end
132
      end
133
    end
134 135 136
    
    def initialize(string, symbol = nil, synonyms = [])
      @symbol, @synonyms = symbol, synonyms
137 138 139 140 141
      @string = string
    end
    
    def to_s
      @string
142 143
    end
    
144 145 146 147
    def to_str
      to_s
    end
    
148
    def to_sym
149
      @symbol || @string.to_sym
150 151 152 153
    end

    def ===(list)
      if list.is_a?(Array)
154
        (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
155 156 157 158
      else
        super
      end
    end
159 160
    
    def ==(mime_type)
161
      return false if mime_type.blank?
162 163 164
      (@synonyms + [ self ]).any? do |synonym| 
        synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym 
      end
165
    end
166

P
Pratik Naik 已提交
167
    # Returns true if Action Pack should check requests using this Mime Type for possible request forgery.  See
168 169 170 171 172 173 174 175 176
    # ActionController::RequestForgerProtection.
    def verify_request?
      !@@unverifiable_types.include?(to_sym)
    end

    def html?
      @@html_types.include?(to_sym) || @string =~ /html/
    end

177 178 179
    private
      def method_missing(method, *args)
        if method.to_s =~ /(\w+)\?$/
180
          $1.downcase.to_sym == to_sym
181 182 183 184
        else
          super
        end
      end
185
  end
186
end
187

188
require 'action_controller/mime_types'