Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d099408

Browse files
committed
[server] mime: update DEFAULT_TYPES
- Remove duplicate xhtml - Introduce type aliases for `DEFAULT_TYPES`, e.g. `('text', 'txt'): ['text/plain; charset=utf-8']`. `txt` is an alias of `text`. Reference: #268 See Also: #276
1 parent 13f6c8d commit d099408

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

couchdb/server/mime.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,35 @@ def best_match(supported, header):
7878
# Build list of `MIME types
7979
# <http://www.iana.org/assignments/media-types/>`_ for HTTP responses.
8080
# Ported from `Ruby on Rails
81-
# <https://github.com/rails/rails/blob/v3.1.0/actionpack/lib/action_dispatch/http/mime_types.rb>`_
81+
# <https://github.com/rails/rails/blob/v5.0.0.1/actionpack/lib/action_dispatch/http/mime_types.rb>`_
8282
DEFAULT_TYPES = {
83-
'all': ['*/*'],
84-
'text': ['text/plain; charset=utf-8', 'txt'],
85-
'html': ['text/html; charset=utf-8'],
86-
'xhtml': ['application/xhtml+xml', 'xhtml'],
87-
'xml': ['application/xml', 'text/xml', 'application/x-xml'],
88-
'js': ['text/javascript', 'application/javascript',
89-
'application/x-javascript'],
90-
'css': ['text/css'],
91-
'ics': ['text/calendar'],
92-
'csv': ['text/csv'],
93-
'rss': ['application/rss+xml'],
94-
'atom': ['application/atom+xml'],
95-
'yaml': ['application/x-yaml', 'text/yaml'],
83+
# (type, alias, ...): [content_type, ...]
84+
('all',): ['*/*'],
85+
('text', 'txt'): ['text/plain; charset=utf-8'],
86+
('html',): ['text/html; charset=utf-8'],
87+
('xhtml',): ['application/xhtml+xml'],
88+
('js',): ['text/javascript',
89+
'application/javascript',
90+
'application/x-javascript'],
91+
('css',): ['text/css'],
92+
('ics',): ['text/calendar'],
93+
('csv',): ['text/csv'],
94+
('vcf',): ['text/vcard'],
95+
96+
('xml',): ['application/xml', 'text/xml', 'application/x-xml'],
97+
('rss',): ['application/rss+xml'],
98+
('atom',): ['application/atom+xml'],
99+
('yaml', 'yml'): ['application/x-yaml', 'text/yaml'],
96100
# just like Rails
97-
'multipart_form': ['multipart/form-data'],
98-
'url_encoded_form': ['application/x-www-form-urlencoded'],
101+
('multipart_form',): ['multipart/form-data'],
102+
('url_encoded_form',): ['application/x-www-form-urlencoded'],
99103
# http://www.ietf.org/rfc/rfc4627.txt
100-
'json': ['application/json', 'text/x-json']
104+
# http://www.json.org/JSONRequest.html
105+
('json',): ['application/json', 'text/x-json', 'application/jsonrequest'],
106+
107+
('pdf',): ['application/pdf'],
108+
('zip',): ['application/zip'],
109+
('gzip', 'gz'): ['application/gzip'],
101110
# TODO: https://issues.apache.org/jira/browse/COUCHDB-1261
102111
# 'kml', 'application/vnd.google-earth.kml+xml',
103112
# 'kmz', 'application/vnd.google-earth.kmz'
@@ -113,8 +122,9 @@ def __init__(self):
113122
self.funcs_by_key = OrderedDict()
114123
self._resp_content_type = None
115124

116-
for k, v in DEFAULT_TYPES.items():
117-
self.register_type(k, *v)
125+
for types, v in DEFAULT_TYPES.items():
126+
for type_ in types:
127+
self.register_type(type_, *v)
118128

119129
def is_provides_used(self):
120130
"""Checks if any provides function is registered."""
@@ -135,21 +145,33 @@ def register_type(self, key, *args):
135145
136146
Predefined types:
137147
- all: ``*/*``
138-
- text: ``text/plain; charset=utf-8``, ``txt``
148+
- text: ``text/plain; charset=utf-8``
149+
- txt: alias of ``text``
139150
- html: ``text/html; charset=utf-8``
140-
- xhtml: ``application/xhtml+xml``, ``xhtml``
141-
- xml: ``application/xml``, ``text/xml``, ``application/x-xml``
151+
- xhtml: ``application/xhtml+xml``
142152
- js: ``text/javascript``, ``application/javascript``,
143153
``application/x-javascript``
144154
- css: ``text/css``
145155
- ics: ``text/calendar``
146156
- csv: ``text/csv``
157+
- vcf: ``text/vcard``,
158+
159+
- xml: ``application/xml``, ``text/xml``, ``application/x-xml``
147160
- rss: ``application/rss+xml``
148161
- atom: ``application/atom+xml``
149162
- yaml: ``application/x-yaml``, ``text/yaml``
163+
- yml: alias of ``yaml``
164+
150165
- multipart_form: ``multipart/form-data``
151166
- url_encoded_form: ``application/x-www-form-urlencoded``
152-
- json: ``application/json``, ``text/x-json``
167+
168+
- json: ``application/json``, ``text/x-json``,
169+
``application/jsonrequest``
170+
171+
- pdf: ``application/pdf``
172+
- zip: ``application/zip``
173+
- gzip: ``application/gzip``
174+
- gz: alias of ``gzip``
153175
154176
Example:
155177
>>> register_type('png', 'image/png')

0 commit comments

Comments
 (0)