U
    aúdÛ  ã                   @   sv   d Z ddlZddddddgZG d	d„ dƒZd
d„ Zdd„ Zddd„Zdd„ Zdd„ Zddddddddhj	Z
dd„ ZdS )z$Miscellaneous WSGI-related Utilitiesé    NÚFileWrapperÚguess_schemeÚapplication_uriÚrequest_uriÚshift_path_infoÚsetup_testing_defaultsc                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )r   z1Wrapper to convert file-like objects to iterablesé    c                 C   s"   || _ || _t|dƒr|j| _d S )NÚclose)ÚfilelikeÚblksizeÚhasattrr	   )Úselfr
   r   © r   ú"/usr/lib/python3.8/wsgiref/util.pyÚ__init__   s    
zFileWrapper.__init__c                 C   s6   dd l }|jdtdd | j | j¡}|r.|S t‚d S )Nr   zXFileWrapper's __getitem__ method ignores 'key' parameter. Use iterator protocol instead.é   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningr
   Úreadr   Ú
IndexError)r   Úkeyr   Údatar   r   r   Ú__getitem__   s    üzFileWrapper.__getitem__c                 C   s   | S ©Nr   )r   r   r   r   Ú__iter__!   s    zFileWrapper.__iter__c                 C   s   | j  | j¡}|r|S t‚d S r   )r
   r   r   ÚStopIteration)r   r   r   r   r   Ú__next__$   s    zFileWrapper.__next__N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   
c                 C   s   |   d¡dkrdS dS dS )zMReturn a guess for whether 'wsgi.url_scheme' should be 'http' or 'https'
    ZHTTPS)ZyesZonÚ1ÚhttpsÚhttpN)Úget)Úenvironr   r   r   r   *   s    c                 C   s    | d d }ddl m} |  d¡r0|| d 7 }nR|| d 7 }| d dkrf| d d	kr‚|d
| d  7 }n| d dkr‚|d
| d  7 }|||  d¡p’ddd7 }|S )z@Return the application's base URI (no PATH_INFO or QUERY_STRING)úwsgi.url_schemez://r   ©ÚquoteÚ	HTTP_HOSTÚSERVER_NAMEr$   ÚSERVER_PORTÚ443ú:Ú80ÚSCRIPT_NAMEú/Úlatin1)Úencoding)Úurllib.parser*   r&   )r'   Úurlr*   r   r   r   r   2   s    
Tc                 C   sp   t | ƒ}ddlm} ||  dd¡ddd}|  d¡sF||d	d
… 7 }n||7 }|rl|  d¡rl|d| d  7 }|S )zBReturn the full request URI, optionally including the query stringr   r)   Ú	PATH_INFOÚ z/;=,r3   )Zsafer4   r1   é   NZQUERY_STRINGú?)r   r5   r*   r&   )r'   Zinclude_queryr6   r*   Ú	path_infor   r   r   r   F   s    
c                 C   sº   |   dd¡}|sdS | d¡}dd„ |dd… D ƒ|dd…< |d }|d= |   d	d¡}t |d | ¡}| d¡r~|dd… }|s”| d¡s”|d7 }|| d	< d |¡| d< |d
kr¶d}|S )aZ  Shift a name from PATH_INFO to SCRIPT_NAME, returning it

    If there are no remaining path segments in PATH_INFO, return None.
    Note: 'environ' is modified in-place; use a copy if you need to keep
    the original PATH_INFO or SCRIPT_NAME.

    Note: when PATH_INFO is just a '/', this returns '' and appends a trailing
    '/' to SCRIPT_NAME, even though empty path segments are normally ignored,
    and SCRIPT_NAME doesn't normally end in a '/'.  This is intentional
    behavior, to ensure that an application can tell the difference between
    '/x' and '/x/' when traversing to objects.
    r7   r8   Nr2   c                 S   s   g | ]}|r|d kr|‘qS )Ú.r   )Ú.0Úpr   r   r   Ú
<listcomp>e   s       z#shift_path_info.<locals>.<listcomp>r9   éÿÿÿÿr1   r<   )r&   ÚsplitÚ	posixpathÚnormpathÚendswithÚjoin)r'   r;   Ú
path_partsÚnameZscript_namer   r   r   r   S   s$    

c                 C   sþ   |   dd¡ |   dd¡ |   d| d ¡ |   dd¡ d| kr\d	| kr\|   dd
¡ |   d	d¡ |   dd¡ |   dd¡ |   dd¡ |   dd¡ ddlm}m} |   d|ƒ ¡ |   d|ƒ ¡ |   dt| ƒ¡ | d dkrâ|   dd¡ n| d dkrú|   dd¡ dS )a:  Update 'environ' with trivial defaults for testing purposes

    This adds various parameters required for WSGI, including HTTP_HOST,
    SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO,
    and all of the wsgi.* variables.  It only supplies default values,
    and does not replace any existing settings for these variables.

    This routine is intended to make it easier for unit tests of WSGI
    servers and applications to set up dummy environments.  It should *not*
    be used by actual WSGI servers or applications, since the data is fake!
    r,   z	127.0.0.1ZSERVER_PROTOCOLzHTTP/1.0r+   ZREQUEST_METHODZGETr1   r7   r8   r2   zwsgi.version)r9   r   zwsgi.run_oncer   zwsgi.multithreadzwsgi.multiprocess)ÚStringIOÚBytesIOz
wsgi.inputzwsgi.errorsr(   r%   r-   r0   r$   r.   N)Ú
setdefaultÚiorH   rI   r   )r'   rH   rI   r   r   r   r   |   s&    Z
connectionz
keep-alivezproxy-authenticatezproxy-authorizationZteZtrailersztransfer-encodingZupgradec                 C   s   t |  ¡ ƒS )z?Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header)Ú_hoppishÚlower)Zheader_namer   r   r   Úis_hop_by_hopª   s    rN   )T)r"   rB   Ú__all__r   r   r   r   r   r   Ú__contains__rL   rN   r   r   r   r   Ú<module>   s0       þ
))     ý