List Info

Thread: Re: decode tool doesn't decode filename of uploaded files




Re: decode tool doesn't decode filename of uploaded files
user name
2007-05-01 20:37:57
> Right. I'd add a separate decode_multipart_headers
function
> (with its own try/finally and fallback to ISO-8859-1)
so that a
> decoding error there doesn't stop correct decoding of
the
> params.

Why not simply change decode_params this way:

def decode_params(encoding):
    decoded_params = {}
    for key, value in cherrypy.request.params.items():
        if isinstance(value, cgi.FieldStorage):
            value = copy.copy(value)
            value.filename = value.filename.decode(encoding,
'replace')
            decoded_params[key] = value
        elif isinstance(value, list):
            # value is a list: decode each element
            decoded_params[key] = [v.decode(encoding) for v
in value]
        elif isinstance(value, unicode):
            pass
        else:
            # value is a regular string: decode it
            decoded_params[key] = value.decode(encoding)

    # Decode all or nothing, so we can try again on error.
    cherrypy.request.params = decoded_params


That solves the issue by just adding/changing three lines.
The
'replace' parameter guarantees decode won't raise an issue
if the
filename is undecodable.

After some googling, I noticed Paste uses the same technique
in its
UnicodeMultiDict class.  Follow this link and look at line
245:
http://trac.pythonpaste.org/p
ythonpaste/browser/Paste/trunk/paste/util/multidict.py

-- Nicolas

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )