B a�NR��@sdZddlZddlZddlZddlmZmZmZddlmZ ddl m Z dddd d gZ Gd d �d e �Zed �Zed �Zed�Zed�Zed�Zed�Zx*e���D]\ZZeee�r�e �e�q�WiZGdd �d e�ZGdd�de�ZGdd�de�ZGdd�de�Z dOdd�Z!dPdd�Z"dQdd�Z#Gdd�de�Z$Gdd �d e�Z%e%d!d"d#�Gd$d%�d%e�Z&e&d&d'd(�Gd)d*�d*e�Z'e'd+d'd,�Gd-d.�d.e�Z(e(d/d'd0�Gd1d2�d2e$�Z)e)d3d4d5�Gd6d7�d7e�Z*e*d8d4d9�Gd:d;�d;e�Z+e+d<d4d=�Gd>d?�d?e�Z,e,d@dAdB�GdCdD�dDe�Z-e-dEd'dF�GdGdH�dHe�Z.e.dId4dJ�x<dKD]4\Z/Z0Z1Z2Z3ee e$edL�e2Z4e3e4e/e0e3e1�_[4�qRWx8e�5�D],Z6e6j/�7dMdN��8�Z9e6e:�e9<e �e9��q�We dd�Z;x:e���D],\ZZeee<��r�e=ee��r�e;�e��q�WdS)Ra� HTTP Message Header Fields (see RFC 4229) This contains general support for HTTP/1.1 message headers [1]_ in a manner that supports WSGI ``environ`` [2]_ and ``response_headers`` [3]_. Specifically, this module defines a ``HTTPHeader`` class whose instances correspond to field-name items. The actual field-content for the message-header is stored in the appropriate WSGI collection (either the ``environ`` for requests, or ``response_headers`` for responses). Each ``HTTPHeader`` instance is a callable (defining ``__call__``) that takes one of the following: - an ``environ`` dictionary, returning the corresponding header value by according to the WSGI's ``HTTP_`` prefix mechanism, e.g., ``USER_AGENT(environ)`` returns ``environ.get('HTTP_USER_AGENT')`` - a ``response_headers`` list, giving a comma-delimited string for each corresponding ``header_value`` tuple entries (see below). - a sequence of string ``*args`` that are comma-delimited into a single string value: ``CONTENT_TYPE("text/html","text/plain")`` returns ``"text/html, text/plain"`` - a set of ``**kwargs`` keyword arguments that are used to create a header value, in a manner dependent upon the particular header in question (to make value construction easier and error-free): ``CONTENT_DISPOSITION(max_age=CONTENT_DISPOSITION.ONEWEEK)`` returns ``"public, max-age=60480"`` Each ``HTTPHeader`` instance also provides several methods to act on a WSGI collection, for removing and setting header values. ``delete(collection)`` This method removes all entries of the corresponding header from the given collection (``environ`` or ``response_headers``), e.g., ``USER_AGENT.delete(environ)`` deletes the 'HTTP_USER_AGENT' entry from the ``environ``. ``update(collection, *args, **kwargs)`` This method does an in-place replacement of the given header entry, for example: ``CONTENT_LENGTH(response_headers,len(body))`` The first argument is a valid ``environ`` dictionary or ``response_headers`` list; remaining arguments are passed on to ``__call__(*args, **kwargs)`` for value construction. ``apply(collection, **kwargs)`` This method is similar to update, only that it may affect other headers. For example, according to recommendations in RFC 2616, certain Cache-Control configurations should also set the ``Expires`` header for HTTP/1.0 clients. By default, ``apply()`` is simply ``update()`` but limited to keyword arguments. This particular approach to managing headers within a WSGI collection has several advantages: 1. Typos in the header name are easily detected since they become a ``NameError`` when executed. The approach of using header strings directly can be problematic; for example, the following should return ``None`` : ``environ.get("HTTP_ACCEPT_LANGUAGES")`` 2. For specific headers with validation, using ``__call__`` will result in an automatic header value check. For example, the _ContentDisposition header will reject a value having ``maxage`` or ``max_age`` (the appropriate parameter is ``max-age`` ). 3. When appending/replacing headers, the field-name has the suggested RFC capitalization (e.g. ``Content-Type`` or ``ETag``) for user-agents that incorrectly use case-sensitive matches. 4. Some headers (such as ``Content-Type``) are 0, that is, only one entry of this type may occur in a given set of ``response_headers``. This module knows about those cases and enforces this cardinality constraint. 5. The exact details of WSGI header management are abstracted so the programmer need not worry about operational differences between ``environ`` dictionary or ``response_headers`` list. 6. Sorting of ``HTTPHeaders`` is done following the RFC suggestion that general-headers come first, followed by request and response headers, and finishing with entity-headers. 7. Special care is given to exceptional cases such as Set-Cookie which violates the RFC's recommendation about combining header content into a single entry using comma separation. A particular difficulty with HTTP message headers is a categorization of sorts as described in section 4.2: Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. This creates three fundamentally different kinds of headers: - Those that do not have a #(values) production, and hence are singular and may only occur once in a set of response fields; this case is handled by the ``_SingleValueHeader`` subclass. - Those which have the #(values) production and follow the combining rule outlined above; our ``_MultiValueHeader`` case. - Those which are multi-valued, but cannot be combined (such as the ``Set-Cookie`` header due to its ``Expires`` parameter); or where combining them into a single header entry would cause common user-agents to fail (``WWW-Authenticate``, ``Warning``) since they fail to handle dates even when properly quoted. This case is handled by ``_MultiEntryHeader``. Since this project does not have time to provide rigorous support and validation for all headers, it does a basic construction of headers listed in RFC 2616 (plus a few others) so that they can be obtained by simply doing ``from paste.httpheaders import *``; the name of the header instance is the "common name" less any dashes to give CamelCase style names. .. [1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 .. [2] http://www.python.org/peps/pep-0333.html#environ-variables .. [3] http://www.python.org/peps/pep-0333.html#the-start-response-callable �N)� formatdate� parsedate_tz� mktime_tz)�time)�HTTPBadRequest� get_header� list_headers�normalize_headers� HTTPHeader�EnvironVariablec@s(eZdZdZdd�Zdd�Zdd�ZdS) r z� a CGI ``environ`` variable as described by WSGI This is a helper object so that standard WSGI ``environ`` variables can be extracted w/o syntax error possibility. cCs |�|d�S)N�)�get)�self�environ�r�B/opt/alt/python37/lib/python3.7/site-packages/paste/httpheaders.py�__call__�szEnvironVariable.__call__cCsd|S)Nz<EnvironVariable %s>r)rrrr�__repr__�szEnvironVariable.__repr__cCs |||<dS)Nr)rr�valuerrr�update�szEnvironVariable.updateN)�__name__� __module__� __qualname__�__doc__rrrrrrrr �s� REMOTE_USER�REMOTE_SESSION� AUTH_TYPE�REQUEST_METHOD� SCRIPT_NAME� PATH_INFOc@s�eZdZdZdZdZdZiZdd�Zdd�Z d d �Z dd d �Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd S)r a an HTTP header HTTPHeader instances represent a particular ``field-name`` of an HTTP message header. They do not hold a field-value, but instead provide operations that work on is corresponding values. Storage of the actual field values is done with WSGI ``environ`` or ``response_headers`` as appropriate. Typically, a sub-classes that represent a specific HTTP header, such as _ContentDisposition, are 0. Once constructed the HTTPHeader instances themselves are immutable and stateless. For purposes of documentation a "container" refers to either a WSGI ``environ`` dictionary, or a ``response_headers`` list. Member variables (and correspondingly constructor arguments). ``name`` the ``field-name`` of the header, in "common form" as presented in RFC 2616; e.g. 'Content-Type' ``category`` one of 'general', 'request', 'response', or 'entity' ``version`` version of HTTP (informational) with which the header should be recognized ``sort_order`` sorting order to be applied before sorting on field-name when ordering headers in a response Special Methods: ``__call__`` The primary method of the HTTPHeader instance is to make it a callable, it takes either a collection, a string value, or keyword arguments and attempts to find/construct a valid field-value ``__lt__`` This method is used so that HTTPHeader objects can be sorted in a manner suggested by RFC 2616. ``__str__`` The string-value for instances of this class is the ``field-name``. Primary Methods: ``delete()`` remove the all occurrences (if any) of the given header in the collection provided ``update()`` replaces (if they exist) all field-value items in the given collection with the value provided ``tuples()`` returns a set of (field-name, field-value) tuples 5 for extending ``response_headers`` Custom Methods (these may not be implemented): ``apply()`` similar to ``update``, but with two differences; first, only keyword arguments can be used, and second, specific sub-classes may introduce side-effects ``parse()`` converts a string value of the header into a more usable form, such as time in seconds for a date header, etc. The collected versions of initialized header instances are immediately registered and accessible through the ``get_header`` function. Do not inherit from this directly, use one of ``_SingleValueHeader``, ``_MultiValueHeader``, or ``_MultiEntryHeader`` as appropriate. z1.1�generalr cKs t��dS)a� build header value from keyword arguments This method is used to build the corresponding header value when keyword arguments (or no arguments) were provided. The result should be a sequence of values. For example, the ``Expires`` header takes a keyword argument ``time`` (e.g. time.time()) from which it returns a the corresponding date. N)�NotImplementedError)r�kwargsrrr�composes zHTTPHeader.composecOs t��dS)a[ convert raw header value into more usable form This method invokes ``values()`` with the arguments provided, parses the header results, and then returns a header-specific data structure corresponding to the header. For example, the ``Expires`` header returns seconds (as returned by time.time()) N)r!)r�argsr"rrr�parses zHTTPHeader.parsecKs|j|f|�dS)a� update the collection /w header value (may have side effects) This method is similar to ``update`` only that usage may result in other headers being changed as recommended by the corresponding specification. The return value is defined by the particular sub-class. For example, the ``_CacheControl.apply()`` sets the ``Expires`` header in addition to its normal behavior. N)r)r� collectionr"rrr�apply(s zHTTPHeader.applyNcCs�t|dd�}|rJ|j|ks"td��|j|ks4td��||jksFtd��|St�|�}||_t|jt�sjt�|pr|j|_|p~|j |_ |p�|j |_ |t |j� �<dddd d �|j|_ t|d d |j���d d��|_t|d|j� ��|_|j dks�t�|S)z� construct a new ``HTTPHeader`` instance We use the ``__new__`` operator to ensure that only one ``HTTPHeader`` instance exists for each field-name, and to register the header so that it can be found/enumerated. F)� raiseErrorz4duplicate registration with different capitalizationz.duplicate registration with different categoryz+duplicate registration with different class����)r �request�response�entity� _environ_nameZHTTP_�-�_� _headers_name)z1.1z1.0z0.9)r�name�AssertionError�category� __class__�object�__new__� isinstance�str�version� reference�_headers�lower� sort_order�getattr�upper�replacer0r3)�clsr4r6r=r<rrrrr97s0        zHTTPHeader.__new__cCs|jS)N)r4)rrrr�__str__[szHTTPHeader.__str__cCs2t|t�r.|j|jkr"|j|jkS|j|jkSdS)a sort header instances as specified by RFC 2616 Re-define sorting so that general headers are first, followed by request/response headers, and then entity headers. The list.sort() methods use the less-than operator for this purpose. F)r:r r@r4)r�otherrrr�__lt__^s     zHTTPHeader.__lt__cCs(|jrd|jpd}d|jj|j|fS)Nz (%s)r z <%s %s%s>)r=r7rr4)r�refrrrrlszHTTPHeader.__repr__cs�|s|jf|�Stt|d�krldt|�ks0t�g}|j���x(�fdd�|dD�D]}|�|�qVW|Stt|d�kr�dt|�kr�d|dks�t�|d� |j �}|s�dS|fSx |D]}t|�ttfks�t�q�W|S)aq find/construct field-value(s) for the given header Resolution is done according to the following arguments: - If only keyword arguments are given, then this is equivalent to ``compose(**kwargs)``. - If the first (and only) argument is a dict, it is assumed to be a WSGI ``environ`` and the result of the corresponding ``HTTP_`` entry is returned. - If the first (and only) argument is a list, it is assumed to be a WSGI ``response_headers`` and the field-value(s) for this header are collected and returned. - In all other cases, the arguments are collected, checked that they are string values, possibly verified by the header's logic, and returned. At this time it is an error to provide keyword arguments if args is present (this might change). It is an error to provide both a WSGI object and also string arguments. If no arguments are provided, then ``compose()`` is called to provide a default value for the header; if there is not default it is an error. rr)cs g|]\}}|���kr|�qSr)r?)�.0�headerr)r4rr� <listcomp>�sz%HTTPHeader.values.<locals>.<listcomp>z wsgi.versionr) r#�list�type�lenr5r4r?�append�dictr r0)rr$r"�resultr�itemr)r4r�valuesps$   zHTTPHeader.valuescOsR|j||�}t|ttf�st�|s&dSt|�dksBtdt|���t|d���S)a� converts ``values()`` into a string value This method converts the results of ``values()`` into a string value for common usage. By default, it is asserted that only one value exists; if you need to access all values then either call ``values()`` directly, or inherit ``_MultiValueHeader`` which overrides this method to return a comma separated list of values as described by section 4.2 of RFC 2616. r r)zmore than one value: %sr) rSr:�tuplerLr5rN�reprr;�strip)rr$r"rSrrrr�s zHTTPHeader.__call__cCsrt|�tkr"|j|kr||j=|Stt|�ks2t�d}x6|t|�krl||d��|jkrb||=q8|d7}q8WdS)zS removes all occurances of the header from the collection provided rr)N)rMrPr0rLr5rNr?r3)rr&�irrr�delete�s  zHTTPHeader.deletecOs�|j||�}|s|�|�dSt|�tkr8|||j<dStt|�ksHt�d}d}xL|t|�kr�||d��|j kr�|r�||=qR|j |f||<d}|d7}qRW|s�|� |j |f�dS)a� updates the collection with the provided header value This method replaces (in-place when possible) all occurrences of the given header with the provided value. If no value is provided, this is the same as ``remove`` (note that this case can only occur if the target is a collection w/o a corresponding header value). The return value is the new header value (which could be a list for ``_MultiEntryHeader`` instances). NrFTr)) rrXrMrPr0rLr5rNr?r3r4rO)rr&r$r"rrW�foundrrrr�s(     zHTTPHeader.updatecOs |j||�}|sdS|j|fgS)Nr)rr4)rr$r"rrrr�tuples�s zHTTPHeader.tuples)NNN)rrrrr<r6r=� extensionsr#r%r'r9rErGrrSrrXrrZrrrrr �s"Z   $/ c@seZdZdZdS)�_SingleValueHeaderz� a ``HTTPHeader`` with exactly a single value This is the default behavior of ``HTTPHeader`` where returning a the string-value of headers via ``__call__`` assumes that only a single value exists. N)rrrrrrrrr\�sr\c@s eZdZdZdd�Zdd�ZdS)�_MultiValueHeaderz� a ``HTTPHeader`` with one or more values The field-value for these header instances is is allowed to be more than one value; whereby the ``__call__`` method returns a comma separated list as described by section 4.2 of RFC 2616. cOs(|j||�}|sdSd�dd�|D��S)Nr z, cSsg|]}t|����qSr)r;rV)rI�vrrrrK�sz._MultiValueHeader.__call__.<locals>.<listcomp>)rS�join)rr$r"�resultsrrrr�s z_MultiValueHeader.__call__cOs$|j||�}|�d�}dd�|D�S)N�,cSsg|]}|��r|���qSr)rV)rIr^rrrrKsz+_MultiValueHeader.parse.<locals>.<listcomp>)r�split)rr$r"rrSrrrr%s  z_MultiValueHeader.parseN)rrrrrr%rrrrr]�sr]c@s eZdZdZdd�Zdd�ZdS)�_MultiEntryHeaderaa a multi-value ``HTTPHeader`` where items cannot be combined with a comma This header is multi-valued, but the values should not be combined with a comma since the header is not in compliance with RFC 2616 (Set-Cookie due to Expires parameter) or which common user-agents do not behave well when the header values are combined. cOs4tt|�kstd��|�|�|�|j||��dS)Nz``environ`` may not be updated)rLrMr5rX�extendrZ)rr&r$r"rrrrs z_MultiEntryHeader.updatecs&�j||�}|sdS�fdd�|D�S)Nrcsg|]}�j|��f�qSr)r4rV)rIr)rrrrKsz,_MultiEntryHeader.tuples.<locals>.<listcomp>)rS)rr$r"rSr)rrrZs z_MultiEntryHeader.tuplesN)rrrrrrZrrrrrcsrcTcCs6t�t|������dd��}|s2|r2td|��|S)z� find the given ``HTTPHeader`` instance This function finds the corresponding ``HTTPHeader`` for the ``name`` provided. So that python-style names can be used, underscores are converted to dashes before the lookup. r2r1z'%s' is an unknown header)r>r r;rVr?rCr5)r4r(�retvalrrrrs csr|s |s |s |s d}}}}g�x6|df|df|df|dffD]\}}|rB��|�qBW�fdd�t��D�S)z' list all headers for a given category Tr r-r.r/csg|]}|j�kr|�qSr)r6)rI�head)�searchrrrK2sz list_headers.<locals>.<listcomp>)rOr>rS)r r-r.r/�boolZstrvalr)rgrr)scs�i�x�tt|��D]t}||\}}t||�}|shd�dd�|�dd��d�D��}||f||<d�|<qt|�|f||<|j�t|�<qW�fdd�}|�|�dS) a sort headers as suggested by RFC 2616 This alters the underlying response_headers to use the common name for each header; as well as sorting them with general headers first, followed by request/response headers, then entity headers, and unknown headers last. r1cSsg|] }|���qSr)� capitalize)rI�xrrrrKBsz%normalize_headers.<locals>.<listcomp>r2r,cs<�|d}�|d}||kr2t|d|d�St||�S)Nr)�cmp)�a�bZacZbc)r6rr�compareIs   z"normalize_headers.<locals>.compareN) �rangerNrr_rCrbr;r@�sort)Zresponse_headers�strict�idx�key�valrfZnewheadrnr)r6rr 4s      c@s"eZdZdZddd�Zdd�ZdS)� _DateHeaderav handle date-based headers This extends the ``_SingleValueHeader`` object with specific treatment of time values: - It overrides ``compose`` to provide a sole keyword argument ``time`` which is an offset in seconds from the current time. - A ``time`` method is provided which parses the given value and returns the current time value. NcCs0|pt�}|r&t|�tkst�||7}t|�fS)N)�nowrM�intr5r)rr�deltarrrr#_s  z_DateHeader.composec OsL|j||�}|rHy tt|��Sttfk rFtd|j|f��YnXdS)z/ return the time value (in seconds since 1970) z-Received an ill-formed timestamp for %s: %s N)rrr� TypeError� OverflowErrorrr4)rr$r"rrrrr%fs  z_DateHeader.parse)NN)rrrrr#r%rrrrruQs  ruc@sNeZdZdZdZedZedZedZedZdd d �Z d d �Z d d�Z dS)� _CacheControla/ Cache-Control, RFC 2616 14.9 (use ``CACHE_CONTROL``) This header can be constructed (using keyword arguments), by first specifying one of the following mechanisms: ``public`` if True, this argument specifies that the response, as a whole, may be cashed. ``private`` if True, this argument specifies that the response, as a whole, may be cashed; this implementation does not support the enumeration of private fields ``no_cache`` if True, this argument specifies that the response, as a whole, may not be cashed; this implementation does not support the enumeration of private fields In general, only one of the above three may be True, the other 2 must then be False or None. If all three are None, then the cache is assumed to be ``public``. Following one of these mechanism specifiers are various modifiers: ``no_store`` indicates if content may be stored on disk; otherwise cache is limited to memory (note: users can still save the data, this applies to intermediate caches) ``max_age`` the maximum duration (in seconds) for which the content should be cached; if ``no-cache`` is specified, this defaults to 0 seconds ``s_maxage`` the maximum duration (in seconds) for which the content should be allowed in a shared cache. ``no_transform`` specifies that an intermediate cache should not convert the content from one type to another (e.g. transform a BMP to a PNG). ``extensions`` gives additional cache-control extensions, such as items like, community="UCI" (14.9.6) The usage of ``apply()`` on this header has side-effects. As recommended by RFC 2616, if ``max_age`` is provided, then then the ``Expires`` header is also calculated for HTTP/1.0 clients and proxies (this is done at the time ``apply()`` is called). For ``no-cache`` and for ``private`` cases, we either do not want the response cached or do not want any response accidently returned to other users; so to prevent this case, we set the ``Expires`` header to the time of the request, signifying to HTTP/1.0 transports that the content isn't to be cached. If you are using SSL, your communication is already "private", so to work with HTTP/1.0 browsers over SSL, consider specifying your cache as ``public`` as the distinction between public and private is moot. i����4NFc KsBt|td�tf�st�t|td�tf�s,t�d} g} |dkrX|sH|sH|rLt�| �d�nR|dkr||sl|sl|rpt�| �d�n.|dks�|dks�t�|s�|r�t�|} | �d�|r�| �d�|r�| �d�|dk r�| �d|�|dk r�| �d |�xF|��D]:\} } | |jk�rtd | ��| �d | �d d �| f�q�W| | fS)NrT�privatezno-cache�publiczno-storez no-transformz max-age=%dz s-maxage=%dzunexpected extension used: '%s'z%s="%s"r2r1)r:rMrwr5rO�itemsr[rC) rr�r�Zno_cacheZno_storeZmax_ageZs_maxageZ no_transformr[�expiresrQ�kr^rrr�_compose�s6        z_CacheControl._composecKs|jf|�\}}|S)N)r�)rr"rQr�rrrr#�sz_CacheControl.composecKs:|jf|�\}}|dk r&tj||d�|j|f|��|S)z* returns the offset expiration in seconds N)rx)r�ZEXPIRESr)rr&r"rQr�rrrr'�s z_CacheControl.apply)NNNFNNF) rrrrZONE_HOURZONE_DAYZONE_WEEKZ ONE_MONTHZONE_YEARr�r#r'rrrrr{wsF r{z Cache-Controlr zRFC 2616, 14.9c@s2eZdZdZdZdZdZdZdZdZ d d d �Z dS) � _ContentTypezg Content-Type, RFC 2616 section 14.17 Unlike other headers, use the CGI variable instead. z1.0� CONTENT_TYPEzapplication/octet-streamz text/plainz text/htmlztext/xmlNcCsP|s&|dkrd}n|s|rt�|jfS|s.d}d||f}|rJ|d|7}|fS)N)�plain�html�xml�text�*z%s/%sz ; charset=%s)r5�UNKNOWN)r�major�minor�charsetrQrrrr#s   z_ContentType.compose)NNN) rrrrr<r0r�Z TEXT_PLAINZ TEXT_HTMLZTEXT_XMLr#rrrrr��sr�z Content-Typer/zRFC 2616, 14.17c@seZdZdZdZdZdS)�_ContentLengthzi Content-Length, RFC 2616 section 14.13 Unlike other headers, use the CGI variable instead. z1.0ZCONTENT_LENGTHN)rrrrr<r0rrrrr�sr�zContent-LengthzRFC 2616, 14.13c@s*eZdZdZd dd�Zdd�Zdd�ZdS) �_ContentDispositiona� Content-Disposition, RFC 2183 (use ``CONTENT_DISPOSITION``) This header can be constructed (using keyword arguments), by first specifying one of the following mechanisms: ``attachment`` if True, this specifies that the content should not be shown in the browser and should be handled externally, even if the browser could render the content ``inline`` exclusive with attachment; indicates that the content should be rendered in the browser if possible, but otherwise it should be handled externally Only one of the above 2 may be True. If both are None, then the disposition is assumed to be an ``attachment``. These are distinct fields since support for field enumeration may be added in the future. ``filename`` the filename parameter, if any, to be reported; if this is None, then the current object's filename attribute is used The usage of ``apply()`` on this header has side-effects. If filename is provided, and Content-Type is not set or is 'application/octet-stream', then the mimetypes.guess is used to upgrade the Content-Type setting. NcCs|g}|dkr |rt�|�d�n|r(t�|�d�|rld|ksBt�|�d�d}|�d�d}|�d|�d �|�f|fS) NT�inline� attachment�"�/����\z filename="%s"z; )r5rOrbr_)rr�r��filenamerQrrrr�Bs   z_ContentDisposition._composecKs|jf|�\}}|S)N)r�)rr"rQ�mimetyperrrr#Qsz_ContentDisposition.composecKsf|jf|�\}}t|�}|rR|r*tj|krRt�|�\}}|rRtj|krRt�||�|j|f|��|S)z/ return the new Content-Type side-effect value )r�r�r�� mimetypesZ guess_typer)rr&r"rQr�r�r2rrrr'Us z_ContentDisposition.apply)NNN)rrrrr�r#r'rrrrr�s" r�zContent-DispositionzRFC 2183c@s$eZdZdZdZdd�Zdd�ZdS)�_IfModifiedSincez3 If-Modified-Since, RFC 2616 section 14.25 z1.0cOstj|f|�|��dd�dS)z� Split the value on ';' incase the header includes extra attributes. E.g. IE 6 is known to send: If-Modified-Since: Sun, 25 Jun 2006 20:36:35 GMT; length=1506 �;r)r)rurrb)rr$r"rrrrhsz_IfModifiedSince.__call__cOs2tj|f|�|�}|r.|t�kr.td|j��|S)NzrPlease check your system clock. According to this server, the time provided in the %s header is in the future. )rur%rvrr4)rr$r"rrrrr%ps  z_IfModifiedSince.parseN)rrrrr<rr%rrrrr�bsr�zIf-Modified-Sincer-zRFC 2616, 14.25c@seZdZdZdd�ZdS)�_Rangeac Range, RFC 2616 14.35 (use ``RANGE``) According to section 14.16, the response to this message should be a 206 Partial Content and that if multiple non-overlapping byte ranges are requested (it is an error to request multiple overlapping ranges) the result should be sent as multipart/byteranges mimetype. The server should respond with '416 Requested Range Not Satisfiable' if the requested ranges are out-of-bounds. The specification also indicates that a syntax error in the Range request should result in the header being ignored rather than a '400 Bad Request'. c Os�|j||�}|sdSg}d}y�|�dd�\}}|����}xl|�d�D]^}|�d�\} } | ��sfd} nt| �} | |kr|t��| ��s�d} nt| �} | }|�| | f�qFWWntk r�dSX||fS)z� Returns a tuple (units, list), where list is a sequence of (begin, end) tuples; and end is None if it was not provided. Nr��=r)rar1r)rrbrVr?rw� ValueErrorrO) rr$r"rZrangesZlast_endZunitsrorRZbegin�endrrrr%�s.  z _Range.parseN)rrrrr%rrrrr�zs r�ZRangezRFC 2616, 14.35c@seZdZdZdd�ZdS)�_AcceptLanguagez0 Accept-Language, RFC 2616 section 14.4 c Os�|j||�}|dkrgSdd�|�d�D�}g}x�|D]�}|�d�}|d����|dd�}}d} xH|D]@} d| krzql| �d�\} } | ����} | ��} | d krlt| �} qlW|�|| f�q6W|�d d ��d d�|D�S) z� Return a list of language tags sorted by their "q" values. For example, "en-us,en;q=0.5" should return ``["en-us", "en"]``. If there is no ``Accept-Language`` header present, default to ``[]``. NcSsg|] }|r|�qSrr)rIr^rrrrK�sz)_AcceptLanguage.parse.<locals>.<listcomp>rar�rr)r��qcSst|d|d� S)Nr))rk)rlrmrrr�<lambda>��z'_AcceptLanguage.parse.<locals>.<lambda>cSsg|] \}}|�qSrr)rI�langr�rrrrK�s)rrbrVr?�floatrOrp) rr$r"rJZlangs�qsr��pieces�paramsr��paramZlvalueZrvaluerrrr%�s(      z_AcceptLanguage.parseN)rrrrr%rrrrr��sr�zAccept-LanguagezRFC 2616, 14.4c@seZdZdZddd�ZdS)� _AcceptRangesz. Accept-Ranges, RFC 2616 section 14.5 NcCs |rdSdS)N)�bytes)�noner)rr�r�rrrr#�sz_AcceptRanges.compose)NN)rrrrr#rrrrr��sr�z Accept-Rangesr.zRFC 2616, 14.5c@seZdZdZddd�ZdS)� _ContentRangez. Content-Range, RFC 2616 section 14.6 NcCs4d|||f}|dks"||ks"t�||ks.t�|fS)Nzbytes %d-%d/%dr�)r5)rZ first_byteZ last_byteZ total_lengthrerrrr#�s z_ContentRange.compose)NNN)rrrrr#rrrrr��sr�z Content-RangezRFC 2616, 14.6c@seZdZdZddd�ZdS)�_Authorizationz2 Authorization, RFC 2617 (RFC 2616, 14.8) Ncs�|r|s t�|s|sB|rt�d|��|��f}d|�d���S|rJ|rNt��pTd�|�d�\} } | �dd�\} } t��} | �| �||�|�dd�\} }t�t�|��} G��fd d �d t �}d | � |�| �}|fS) Nz%s:%szBasic %s�base64r�zrealm="r�r)� cs0eZdZ�fdd�Zdd�Z�fdd�ZeZdS)z+_Authorization.compose.<locals>.FakeRequestcs�S)Nr)r)�pathrr� get_full_url�sz8_Authorization.compose.<locals>.FakeRequest.get_full_urlcSsdS)NFr)rrrr�has_data�sz4_Authorization.compose.<locals>.FakeRequest.has_datacs�pdS)NZGETr)r)�methodrr� get_method�sz6_Authorization.compose.<locals>.FakeRequest.get_methodN)rrrr�r�r�Z get_selectorr)r�r�rr� FakeRequest�s  r�z Digest %s) r5rV�encoderb�urllib2�AbstractDigestAuthHandlerZ add_passwordZparse_keqv_listZparse_http_listr8Zget_authorization)r�digestZbasic�username�passwordZ challenger�r�Zuserpassr2�realmZauth�tokenZchalr�rer)r�r�rr#�s   z_Authorization.compose)NNNNNNN)rrrrr#rrrrr��sr�Z AuthorizationzRFC 2617)()ZAcceptr-z1.1z multi-valuezRFC 2616, 14.1)zAccept-Charsetr-z1.1z multi-valuezRFC 2616, 14.2)zAccept-Encodingr-z1.1z multi-valuezRFC 2616, 14.3)ZAger.z1.1�singularzRFC 2616, 14.6)ZAllowr/z1.0z multi-valuezRFC 2616, 14.7)ZCookier-z1.0z multi-valuezRFC 2109/Netscape)Z Connectionr z1.1z multi-valuezRFC 2616, 14.10)zContent-Encodingr/z1.0z multi-valuezRFC 2616, 14.11)zContent-Languager/z1.1z multi-valuezRFC 2616, 14.12)zContent-Locationr/z1.1r�zRFC 2616, 14.14)z Content-MD5r/z1.1r�zRFC 2616, 14.15)ZDater z1.0z date-headerzRFC 2616, 14.18)ZETagr.z1.1r�zRFC 2616, 14.19)ZExpectr-z1.1z multi-valuezRFC 2616, 14.20)ZExpiresr/z1.0z date-headerzRFC 2616, 14.21)ZFromr-z1.0r�zRFC 2616, 14.22)ZHostr-z1.1r�zRFC 2616, 14.23)zIf-Matchr-z1.1z multi-valuezRFC 2616, 14.24)z If-None-Matchr-z1.1z multi-valuezRFC 2616, 14.26)zIf-Ranger-z1.1r�zRFC 2616, 14.27)zIf-Unmodified-Sincer-z1.1z date-headerzRFC 2616, 14.28)z Last-Modifiedr/z1.0z date-headerzRFC 2616, 14.29)ZLocationr.z1.0r�zRFC 2616, 14.30)z Max-Forwardsr-z1.1r�zRFC 2616, 14.31)ZPragmar z1.0z multi-valuezRFC 2616, 14.32)zProxy-Authenticater.z1.1z multi-valuezRFC 2616, 14.33)zProxy-Authorizationr-z1.1r�zRFC 2616, 14.34)ZRefererr-z1.0r�zRFC 2616, 14.36)z Retry-Afterr.z1.1r�zRFC 2616, 14.37)ZServerr.z1.0r�zRFC 2616, 14.38)z Set-Cookier.z1.0z multi-entryzRFC 2109/Netscape)ZTEr-z1.1z multi-valuezRFC 2616, 14.39)ZTrailerr z1.1z multi-valuezRFC 2616, 14.40)zTransfer-Encodingr z1.1z multi-valuezRFC 2616, 14.41)ZUpgrader z1.1z multi-valuezRFC 2616, 14.42)z User-Agentr-z1.0r�zRFC 2616, 14.43)ZVaryr.z1.1z multi-valuezRFC 2616, 14.44)ZViar z1.1z multi-valuezRFC 2616, 14.45)�Warningr z1.1z multi-entryzRFC 2616, 14.46)zWWW-Authenticater.z1.0z multi-entryzRFC 2616, 14.47)z multi-valuez multi-entryz date-headerr�r1r2)T)NNNN)T)>rr�r��reZrfc822rrrrrvZhttpexceptionsr�__all__r;r rrrrrr�globalsr��_name�_objr:rOr>r8r r\r]rcrrr rur{r�r�r�r�r�r�r�r�r�r4r6r<Zstyle�comment�klassrSrfrCrBZheadname�localsZ __pudge_all__rM� issubclassrrrr�<module>�s�    <  &{   B  0     2