mercurial/hgweb/wsgiheaders.py
author Gregory Szorc <gregory.szorc@gmail.com>
Mon, 21 Feb 2022 13:08:28 -0700
changeset 48946 642e31cb55f0
parent 48875 6000f5b25c9b
permissions -rw-r--r--
py3: use class X: instead of class X(object): The inheritance from object is implied in Python 3. So this should be equivalent. This change was generated via an automated search and replace. So there may have been some accidental changes. Differential Revision: https://phab.mercurial-scm.org/D12352
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
"""This was forked from cpython's wsgiref.headers module to work on bytes.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
Header from old file showing copyright is below.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
Much of this module is red-handedly pilfered from email.message in the stdlib,
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
so portions are Copyright (C) 2001,2002 Python Software Foundation, and were
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
written by Barry Warsaw.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
"""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
# Regular expression that matches `special' characters in parameters, the
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
# existence of which force quoting of the parameter value.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
import re
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    14
44021
6d3b67a837a6 cleanup: drop redundant character escapes from `[]` character sets
Matt Harbison <matt_harbison@yahoo.com>
parents: 43503
diff changeset
    15
tspecials = re.compile(br'[ ()<>@,;:\\"/\[\]?=]')
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    17
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
def _formatparam(param, value=None, quote=1):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
    """Convenience function to format and return a key=value pair.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
    This will quote the value if needed or if quote is true.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
    """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
    if value is not None and len(value) > 0:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
        if quote or tspecials.search(value):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    24
            value = value.replace(b'\\', b'\\\\').replace(b'"', r'\"')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    25
            return b'%s="%s"' % (param, value)
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    27
            return b'%s=%s' % (param, value)
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
    else:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
        return param
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    32
class Headers:
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
    """Manage a collection of HTTP response headers"""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
    def __init__(self, headers=None):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
        headers = headers if headers is not None else []
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
        if type(headers) is not list:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    38
            raise TypeError(b"Headers must be a list of name/value tuples")
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
        self._headers = headers
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
        if __debug__:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
            for k, v in headers:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
                self._convert_string_type(k)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
                self._convert_string_type(v)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
    def _convert_string_type(self, value):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
        """Convert/check value type."""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
        if type(value) is bytes:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
            return value
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    49
        raise AssertionError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    50
            u"Header names/values must be"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    51
            u" of type bytes (got %s)" % repr(value)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    52
        )
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
    def __len__(self):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
        """Return the total number of headers, including duplicates."""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
        return len(self._headers)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
    def __setitem__(self, name, val):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
        """Set the value of a header."""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
        del self[name]
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
        self._headers.append(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    62
            (self._convert_string_type(name), self._convert_string_type(val))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    63
        )
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
    def __delitem__(self, name):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
        """Delete all occurrences of a header, if present.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
        Does *not* raise an exception if the header is missing.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
        name = self._convert_string_type(name.lower())
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
        self._headers[:] = [kv for kv in self._headers if kv[0].lower() != name]
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
    def __getitem__(self, name):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
        """Get the first header value for 'name'
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
        Return None if the header is missing instead of raising an exception.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
        Note that if the header appeared multiple times, the first exactly which
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
        occurrence gets returned is undefined.  Use getall() to get all
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
        the values matching a header field name.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
        return self.get(name)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    81
    def __contains__(self, name):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
        """Return true if the message contains the header."""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    83
        return self.get(name) is not None
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    84
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    85
    def get_all(self, name):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    86
        """Return a list of all the values for the named field.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    87
        These will be sorted in the order they appeared in the original header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
        list or were added to this instance, and may contain duplicates.  Any
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    89
        fields deleted and re-inserted are always appended to the header list.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    90
        If no fields exist with the given name, returns an empty list.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    92
        name = self._convert_string_type(name.lower())
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    93
        return [kv[1] for kv in self._headers if kv[0].lower() == name]
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    94
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    95
    def get(self, name, default=None):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    96
        """Get the first header value for 'name', or return 'default'"""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    97
        name = self._convert_string_type(name.lower())
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
    98
        for k, v in self._headers:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
    99
            if k.lower() == name:
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   100
                return v
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   101
        return default
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   102
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   103
    def keys(self):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   104
        """Return a list of all the header field names.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   105
        These will be sorted in the order they appeared in the original header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   106
        list, or were added to this instance, and may contain duplicates.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   107
        Any fields deleted and re-inserted are always appended to the header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
        list.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
        return [k for k, v in self._headers]
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   112
    def values(self):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
        """Return a list of all header values.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
        These will be sorted in the order they appeared in the original header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
        list, or were added to this instance, and may contain duplicates.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
        Any fields deleted and re-inserted are always appended to the header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
        list.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
        return [v for k, v in self._headers]
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
    def items(self):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
        """Get all the header fields and values.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
        These will be sorted in the order they were in the original header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
        list, or were added to this instance, and may contain duplicates.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
        Any fields deleted and re-inserted are always appended to the header
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
        list.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
        return self._headers[:]
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   129
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   130
    def __repr__(self):
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   131
        return "%s(%r)" % (self.__class__.__name__, self._headers)
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
    def __str__(self):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   134
        """str() returns the formatted headers, complete with end line,
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   135
        suitable for direct HTTP transmission."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   136
        return b'\r\n'.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   137
            [b"%s: %s" % kv for kv in self._headers] + [b'', b'']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   138
        )
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   139
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   140
    def __bytes__(self):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   141
        return str(self).encode('iso-8859-1')
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   142
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   143
    def setdefault(self, name, value):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   144
        """Return first matching header value for 'name', or 'value'
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   145
        If there is no header named 'name', add a new header with name 'name'
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   146
        and value 'value'."""
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   147
        result = self.get(name)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   148
        if result is None:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   149
            self._headers.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   150
                (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   151
                    self._convert_string_type(name),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   152
                    self._convert_string_type(value),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   153
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   154
            )
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   155
            return value
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   156
        else:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   157
            return result
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   158
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   159
    def add_header(self, _name, _value, **_params):
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   160
        """Extended header setting.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   161
        _name is the header field to add.  keyword arguments can be used to set
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   162
        additional parameters for the header field, with underscores converted
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   163
        to dashes.  Normally the parameter will be added as key="value" unless
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   164
        value is None, in which case only the key will be added.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   165
        Example:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   166
        h.add_header('content-disposition', 'attachment', filename='bud.gif')
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   167
        Note that unlike the corresponding 'email.message' method, this does
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   168
        *not* handle '(charset, language, value)' tuples: all values must be
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   169
        strings or None.
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   170
        """
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   171
        parts = []
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   172
        if _value is not None:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   173
            _value = self._convert_string_type(_value)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   174
            parts.append(_value)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   175
        for k, v in _params.items():
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   176
            k = self._convert_string_type(k)
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   177
            if v is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   178
                parts.append(k.replace(b'_', b'-'))
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   179
            else:
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   180
                v = self._convert_string_type(v)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   181
                parts.append(_formatparam(k.replace(b'_', b'-'), v))
37605
74e1362585c0 wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff changeset
   182
        self._headers.append(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   183
            (self._convert_string_type(_name), b"; ".join(parts))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41588
diff changeset
   184
        )