o ]Lb@sDdZddlmZmZddlZedZd ddZGdd d eZ dS) a0This was forked from cpython's wsgiref.headers module to work on bytes. Header from old file showing copyright is below. Much of this module is red-handedly pilfered from email.message in the stdlib, so portions are Copyright (C) 2001,2002 Python Software Foundation, and were written by Barry Warsaw. )absolute_importprint_functionNs[ ()<>@,;:\\"/\[\]?=]cCsR|dur't|dkr'|st|r!|dddd}d||fSd||fS|S) z}Convenience function to format and return a key=value pair. This will quote the value if needed or if quote is true. Nr\s\\"z\"s%s="%s"s%s=%s)len tspecialssearchreplace)paramvaluequoter=/usr/lib/python3/dist-packages/mercurial/hgweb/wsgiheaders.py _formatparams   rc@seZdZdZd%ddZddZddZd d Zd d Zd dZ ddZ ddZ d%ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$ZdS)&Headersz,Manage a collection of HTTP response headersNcCsR|dur|ng}t|turtd||_ |D]\}}||||qdS)Ns+Headers must be a list of name/value tuples)typelist TypeError_headers_convert_string_type)selfheaderskvrrr__init__$s    zHeaders.__init__cCs t|tur|Stdt|)zConvert/check value type.z2Header names/values must be of type bytes (got %s))rbytesAssertionErrorrepr)rr rrrr.s zHeaders._convert_string_typecCs t|jS)z9Return the total number of headers, including duplicates.)rrrrrr__len__7s zHeaders.__len__cCs&||=|j||||fdS)zSet the value of a header.N)rappendr)rnamevalrrr __setitem__;szHeaders.__setitem__cs0|fdd|jD|jdd<dS)zxDelete all occurrences of a header, if present. Does *not* raise an exception if the header is missing. cs g|] }|dkr|qS)rlower.0kvr"rr Gs z'Headers.__delitem__..Nrr&rrr"rr*r __delitem__Bs"zHeaders.__delitem__cCs ||S)aFGet the first header value for 'name' Return None if the header is missing instead of raising an exception. Note that if the header appeared multiple times, the first exactly which occurrence gets returned is undefined. Use getall() to get all the values matching a header field name. getr-rrr __getitem__Is zHeaders.__getitem__cCs||duS)z/Return true if the message contains the header.Nr/r-rrr __contains__RszHeaders.__contains__cs"|fdd|jDS)apReturn a list of all the values for the named field. These will be sorted in the order they appeared in the original header list or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no fields exist with the given name, returns an empty list. cs$g|]}|dkr|dqS)rrr%r'r*rrr+^s$z#Headers.get_all..r,r-rr*rget_allVszHeaders.get_allcCs6||}|jD]\}}||kr|Sq |S)z:Get the first header value for 'name', or return 'default'r,)rr"defaultrrrrrr0`s  z Headers.getcCdd|jDS)a)Return a list of all the header field names. These will be sorted in the order they appeared in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. cSsg|]\}}|qSrrr(rrrrrr+oz Headers.keys..rrrrrkeyshz Headers.keyscCr5)a Return a list of all header values. These will be sorted in the order they appeared in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. cSsg|]\}}|qSrrr6rrrr+xr7z"Headers.values..r8rrrrvaluesqr:zHeaders.valuescCs|jddS)aGet all the header fields and values. These will be sorted in the order they were in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. Nr8rrrritemszsz Headers.itemscCsd|jj|jfS)Nz%s(%r)) __class____name__rrrrr__repr__szHeaders.__repr__cCsddd|jDddgS)zkstr() returns the formatted headers, complete with end line, suitable for direct HTTP transmission.s cSsg|]}d|qS)s%s: %srr'rrrr+r7z#Headers.__str__..)joinrrrrr__str__szHeaders.__str__cCst|dS)Nz iso-8859-1)strencoderrrr __bytes__szHeaders.__bytes__cCs6||}|dur|j||||f|S|S)zReturn first matching header value for 'name', or 'value' If there is no header named 'name', add a new header with name 'name' and value 'value'.N)r0rr!r)rr"r resultrrr setdefaults zHeaders.setdefaultcKsg}|dur||}|||D](\}}||}|dur+||ddq||}|t|dd|q|j||d|fdS)abExtended header setting. _name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key="value" unless value is None, in which case only the key will be added. Example: h.add_header('content-disposition', 'attachment', filename='bud.gif') Note that unlike the corresponding 'email.message' method, this does *not* handle '(charset, language, value)' tuples: all values must be strings or None. N_-s; )rr!r<r rrrA)r_name_value_paramspartsrrrrr add_headers     zHeaders.add_header)N)r> __module__ __qualname____doc__rrr r$r.r1r2r3r0r9r;r<r?rBrErGrNrrrrr!s&         r)Nr) rQ __future__rrrecompilerrobjectrrrrrs