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.

classification
Title: RFC822 header parsing API inconsistencies between httplib.HTTPMessage and email.message.Message
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: jrjsmrtn, r.david.murray
Priority: normal Keywords:

Created on 2011-02-25 10:37 by jrjsmrtn, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg129348 - (view) Author: Georges Martin (jrjsmrtn) Date: 2011-02-25 10:37
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()
msg129364 - (view) Author: Georges Martin (jrjsmrtn) Date: 2011-02-25 13:34
Hello, David.

According to issue4773, httplib.HTTPMessage in Python 3.x is using email.message.Message underneath, so my issue is only with 2.6 (I've not checked 2.7).

Georges
msg129369 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-02-25 14:06
Ah, yes, you are correct.   We must reject this, then, since 2.7 is now feature frozen and this is a feature request.
msg129376 - (view) Author: Georges Martin (jrjsmrtn) Date: 2011-02-25 15:03
No problem. I thought important that the issue and a workaround were
documented somehow... :-)
msg129382 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-02-25 15:31
Yes, thanks for that.
History
Date User Action Args
2022-04-11 14:57:13adminsetgithub: 55525
2014-04-15 15:56:56r.david.murraysetstatus: open -> closed
2011-02-25 15:31:37r.david.murraysetnosy: r.david.murray, jrjsmrtn
messages: + msg129382
2011-02-25 15:03:03jrjsmrtnsetnosy: r.david.murray, jrjsmrtn
messages: + msg129376
2011-02-25 14:06:40r.david.murraysetnosy: r.david.murray, jrjsmrtn
messages: + msg129369
resolution: out of date
stage: resolved
2011-02-25 13:34:13jrjsmrtnsetnosy: r.david.murray, jrjsmrtn
messages: + msg129364
2011-02-25 13:06:22r.david.murraysetnosy: r.david.murray, jrjsmrtn
type: behavior -> enhancement
versions: + Python 3.3, - Python 2.6
2011-02-25 13:05:56r.david.murraysetnosy: + r.david.murray
2011-02-25 10:37:52jrjsmrtncreate