Source

paste / paste / util / mimeparse.py

Diff from to

paste/util/mimeparse.py

 
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
 
-Based on mimeparse 0.1.2 by Joe Gregorio:
+Based on mimeparse 0.1.2 (and best_match() from 0.1.3) by Joe Gregorio:
 
     http://code.google.com/p/mimeparse/
 
     """
     if not supported:
         return ''
-    parsed_header = map(parse_media_range, header.split(','))
-    best_type = max([
-            (fitness_and_quality_parsed(mime_type, parsed_header), -n)
-            for n, mime_type in enumerate(supported)])
-    return best_type[0][1] and supported[-best_type[1]] or ''
+
+    split_header = _filter_blank(header.split(','))
+    parsed_header = [parse_media_range(r) for r in split_header]
+    weighted_matches = []
+    pos = 0
+    for mime_type in supported:
+        weighted_matches.append((fitness_and_quality_parsed(mime_type,
+                                 parsed_header), pos, mime_type))
+        pos += 1
+    weighted_matches.sort()
+
+    return weighted_matches[-1][0][1] and weighted_matches[-1][2] or ''
+
+def _filter_blank(i):
+    for s in i:
+        if s.strip():
+            yield s
 
 def desired_matches(desired, header):
     """Takes a list of desired mime-types in the order the server prefers to