Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httplib.HTTPConnection._send_request should not blindly assume dicts for headers #47526

Closed
ludvigericson mannequin opened this issue Jul 4, 2008 · 3 comments
Closed
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ludvigericson
Copy link
Mannequin

ludvigericson mannequin commented Jul 4, 2008

BPO 3276
Nosy @terryjreedy, @iritkatriel
Files
  • httplib.py.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-06-20.23:05:01.602>
    created_at = <Date 2008-07-04.00:19:28.012>
    labels = ['invalid', 'type-feature', 'library']
    title = 'httplib.HTTPConnection._send_request should not blindly assume dicts for headers'
    updated_at = <Date 2021-06-20.23:05:01.601>
    user = 'https://bugs.python.org/ludvigericson'

    bugs.python.org fields:

    activity = <Date 2021-06-20.23:05:01.601>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-06-20.23:05:01.602>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2008-07-04.00:19:28.012>
    creator = 'ludvig.ericson'
    dependencies = []
    files = ['10807']
    hgrepos = []
    issue_num = 3276
    keywords = ['patch']
    message_count = 3.0
    messages = ['69234', '107439', '396203']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'ludvig.ericson', 'piotr.dobrogost', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue3276'
    versions = ['Python 2.5']

    @ludvigericson
    Copy link
    Mannequin Author

    ludvigericson mannequin commented Jul 4, 2008

    Presently it's impossible to use httplib.HTTPConnection.request and send
    the several headers with the same name. This is because _send_request
    assumes a dict is passed in, or a dict-like interface. Obviously one could
    make a list subclass or some such and give it an iteritems that returns
    itself, but IMHO the solution is to fix httplib.

    Attached patch changes the current behavior to using iteritems only if it
    exists, otherwise iterate directly (which would fit with sequences of two-
    tuples).

    @ludvigericson ludvigericson mannequin added the stdlib Python modules in the Lib dir label Jul 4, 2008
    @terryjreedy
    Copy link
    Member

    This is a feature request for now old versions. It would have to be reformulated as a feature request for a 3.2 module. I do not see the dict (mapping now?) requirement being changed.

    @terryjreedy terryjreedy added the type-feature A feature request or enhancement label Jun 9, 2010
    @iritkatriel
    Copy link
    Member

    Indeed, as Terry wrote the assumption is that header is a mapping (not necessarily a dict). It is not hard to implement a Multimap that has this API:

    import collections.abc
    
    class Multimap(collections.abc.Mapping):
        def __init__(self):
            self.data = collections.defaultdict(list)
    
        def __getitem__(self, key):
            return self.data[key]
    
        def __setitem__(self, key, value):
            self.data[key].append(value)
    
        def __iter__(self):
            yield from self.data
    
        def items(self):
            for k in list(self.data.keys()):
                for v in list(self.data[k]):
                    yield (k,v)
    
        def __len__(self):
            return sum([len(v) for v in self.data.values()])
    
    mm = Multimap()
    mm['1'] = 'a'
    mm['1'] = 'aa'
    mm['1'] = 'aaa'
    mm['2'] = 'b'
    mm['3'] = 'c'
    mm['3'] = 'cc'
    print(f'len = {len(mm)}')
    print(f'mm.items() = {list(mm.items())}')

    Output:
    len = 6
    mm.items() = [('1', 'a'), ('1', 'aa'), ('1', 'aaa'), ('2', 'b'), ('3', 'c'), ('3', 'cc')]

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants