This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jrjsmrtn
Recipients jrjsmrtn
Date 2011-02-25.10:37:51
SpamBayes Score 4.94464e-05
Marked as misclassified No
Message-id <1298630272.85.0.58708707749.issue11316@psf.upfronthosting.co.za>
In-reply-to
Content
Both httplib.HTTPMessage and email.message.Message classes[1] implements methods for RFC822 headers parsing. Unfortunately, they have different implementations and they do not provide the same level of functionality.

One example that is bugging me is that:

* httplib.HTTPMessage is missing the get_filename method present in email.message.Message, that allows you to easily retrieve the filename from a 'Content-disposition: attachment; filename="fghi.xyz"' header;

* httplib.HTTPMessage has getparam, getplist and parseplist methods but AFAIK, they are not and cannot be used outside of the 'content-type' header parsing;

* email.message.Message has a generic get_param method to parse any RFC822 header with parameters, such as 'content-disposition' or 'content-type'.

The workaround I'm using is to decorate an httplib.HTTPMessage with the missing methods from email.message.Message:

    def monkeypatch_http_message(obj):
        """ Decorate an httplib.HTTPMessage instance's class 
            with the RFC822 header parameters parsing methods 
            from email.message.Message. (thanks to ncoghlan)
        """
        import httplib
        assert isinstance(obj, httplib.HTTPMessage)
        cls = obj.__class__
    
        from email import utils
        from email.message import (
            _parseparam, 
            _unquotevalue, 
            Message
        )
        funcnames = (
            '_get_params_preserve', 
            'get_params', 
            'get_param',
            'get_filename'
        )
        for funcname in funcnames:
            cls.__dict__[funcname] = Message.__dict__[funcname]

So I can do:

    import mechanize
    from some.module import monkeypatch_http_message
    browser = mechanize.Browser()
    
    # in that form, browser.retrieve returns a temporary filename 
    # and an httplib.HTTPMessage instance
    (tmp_filename, headers) = browser.retrieve(someurl) 

    # monkeypatch the httplib.HTTPMessage instance
    monkeypatch_http_message(headers)

    # yeah... my original filename, finally
    filename = headers.get_filename()
History
Date User Action Args
2011-02-25 10:37:52jrjsmrtnsetrecipients: + jrjsmrtn
2011-02-25 10:37:52jrjsmrtnsetmessageid: <1298630272.85.0.58708707749.issue11316@psf.upfronthosting.co.za>
2011-02-25 10:37:52jrjsmrtnlinkissue11316 messages
2011-02-25 10:37:51jrjsmrtncreate