Issue #54 resolved

(type, subtype) = parts[0].split("/") ValueError: too many values to unpack

ramusus avatarramusus created an issue

Real example of HEADERS:

...
 'DOCUMENT_URI': '/',
 'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
 'HTTP_ACCEPT_ENCODING': 'gzip,defalte',
 'HTTP_ACCEPT_LANGUAGE': 'ru,en-us;q=0.7,en;q=0.3',
...
Traceback (most recent call last):

 File "/var/www/movister.ru/web_site/django/core/handlers/base.py", line 178, in get_response
   response = middleware_method(request, response)

 File "/var/www/movister.ru/web_site/django_authopenid/middleware.py", line 44, in process_response
   request.META['HTTP_ACCEPT']) == 'application/xrds+xml':

 File "/var/www/movister.ru/web_site/django_authopenid/utils/mimeparse.py", line 111, in best_match
   parsed_header = [parse_media_range(r) for r in header.split(",")]

 File "/var/www/movister.ru/web_site/django_authopenid/utils/mimeparse.py", line 54, in parse_media_range
   (type, subtype, params) = parse_mime_type(range)

 File "/var/www/movister.ru/web_site/django_authopenid/utils/mimeparse.py", line 38, in parse_mime_type
   (type, subtype) = parts[0].split("/")

ValueError: need more than 1 value to unpack
In [4]: from django_authopenid.utils.mimeparse import parse_mime_type
In [5]: parse_mime_type("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/ram/workspace/movister/web_site/<ipython console> in <module>()

/home/ram/workspace/movister/web_site/django_authopenid/utils/mimeparse.pyc in parse_mime_type(mime_type)
     36     params = dict([tuple([s.strip() for s in param.split("=")])\
     37             for param in parts[1:] ])
---> 38     (type, subtype) = parts[0].split("/")
     39     return (type.strip(), subtype.strip(), params)
     40 

ValueError: too many values to unpack

Comments (5)

  1. ramusus
    @@ -32,9 +32,12 @@
     
            ('application', 'xhtml', {'q', '0.5'})
            """
    -    parts = mime_type.split(";")
    -    params = dict([tuple([s.strip() for s in param.split("=")])\
    -            for param in parts[1:] ])
    +
    +    parts = []
    +    for part in mime_type.split(";"):
    +        parts += part.split(",") if part.count(",") else [part]
    +    params = dict([tuple([s.strip() for s in param.split("=")]) \
    +            for param in parts[1:] if param.count("=")])
         (type, subtype) = parts[0].split("/")
         return (type.strip(), subtype.strip(), params)
    
  2. ramusus

    Patch-solution

    @@ -32,9 +32,12 @@
     
            ('application', 'xhtml', {'q', '0.5'})
            """
    -    parts = mime_type.split(";")
    -    params = dict([tuple([s.strip() for s in param.split("=")])\
    -            for param in parts[1:] ])
    +
    +    parts = []
    +    for part in mime_type.split(";"):
    +        parts += part.split(",") if part.count(",") else [part]
    +    params = dict([tuple([s.strip() for s in param.split("=")]) \
    +            for param in parts[1:] if param.count("=")])
         (type, subtype) = parts[0].split("/")
         return (type.strip(), subtype.strip(), params)
    
  3. ramusus
    @@ -23,6 +23,8 @@
     __email__ = "joe@bitworking.org"
     __credits__ = ""
     
    +import re
    +
     def parse_mime_type(mime_type):
         """Carves up a mime_type and returns a tuple of the
            (type, subtype, params) where 'params' is a dictionary
    @@ -32,10 +34,10 @@
     
            ('application', 'xhtml', {'q', '0.5'})
            """
    -    parts = mime_type.split(";")
    -    params = dict([tuple([s.strip() for s in param.split("=")])\
    -            for param in parts[1:] ])
    -    (type, subtype) = parts[0].split("/")
    +
    +    parts = re.split(r'[,|;]', mime_type)
    +    params = dict([tuple([s.strip() for s in param.split("=")]) for param in parts[1:] if param.count("=")])
    +    (type, subtype) = parts[0].split("/") if parts[0].count("/") else (parts[0], parts[0])
         return (type.strip(), subtype.strip(), params)
    @@ -126,6 +128,7 @@
                 self.assertEqual(('application', 'xml', {'q': '1'}), parse_media_range('application/xml ; q='))
                 self.assertEqual(('application', 'xml', {'q': '1', 'b': 'other'}), parse_media_range('application/xml ; q=1;b=other'))
                 self.assertEqual(('application', 'xml', {'q': '1', 'b': 'other'}), parse_media_range('application/xml ; q=2;b=other'))
    +            self.assertEqual(('text', 'html', {'q': '.2'}), parse_media_range('text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'))
    
  4. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.