1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
22 def content_type(content_type)
23 @@content_type_mapping ||= {}
24 @@content_type_mapping[content_type] = self
27 def self.match_content_type(content_type)
28 # TODO(bobaman): Do this more efficiently.
29 mime_type_regexp = /^([^\/]+)(?:\/([^+]+\+)?([^;]+))?(?:;.*)?$/
30 if @@content_type_mapping[content_type]
32 return @@content_type_mapping[content_type]
34 media_type, extension, sub_type =
35 content_type.scan(mime_type_regexp)[0]
36 for pattern, parser in @@content_type_mapping
37 # We want to match on subtype first
38 pattern_media_type, pattern_extension, pattern_sub_type =
39 pattern.scan(mime_type_regexp)[0]
40 next if pattern_extension != nil
41 if media_type == pattern_media_type && sub_type == pattern_sub_type
45 for pattern, parser in @@content_type_mapping
46 # We failed to match on the subtype
47 # Try to match only on the media type
48 pattern_media_type, pattern_extension, pattern_sub_type =
49 pattern.scan(mime_type_regexp)[0]
50 next if pattern_extension != nil || pattern_sub_type != nil
51 if media_type == pattern_media_type