o
    ˜¨Êhn  ã                   @   sÐ   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d d	lmZ ed
ƒZG dd„ dƒZG dd„ de	ƒZG dd„ dejƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )é    )ÚBytesIO)Úsettings)Úsignals)Úbase)ÚHttpRequestÚ	QueryDictÚparse_cookie)Úset_script_prefix)Úrepercent_broken_unicode)Úcached_property)Ú_lazy_re_compiles   /+c                   @   s6   e Zd ZdZdd„ Zddd„Zddd„Zdd	d
„ZdS )ÚLimitedStreamzBWrap another stream to disallow reading it past a number of bytes.c                 C   s   || _ || _d| _d S ©Nó    )ÚstreamÚ	remainingÚbuffer)Úselfr   Úlimit© r   úK/var/www/html/env/lib/python3.10/site-packages/django/core/handlers/wsgi.pyÚ__init__   s   
zLimitedStream.__init__Nc                 C   sF   |d u s	|| j kr| j }|dkrdS | j |¡}|  j t|ƒ8  _ |S )Nr   r   )r   r   ÚreadÚlen©r   ÚsizeÚresultr   r   r   Ú_read_limited   s   zLimitedStream._read_limitedc                 C   st   |d u r| j |  ¡  }d| _ |S |t| j ƒk r(| j d |… }| j |d … | _ |S | j |  |t| j ƒ ¡ }d| _ |S r   )r   r   r   r   r   r   r   r       s   úþzLimitedStream.readc                 C   s¦   d| j vr;|d u st| j ƒ|k r;|r|  |t| j ƒ ¡}n|  ¡ }|s$n|  j |7  _ d| j vr;|d u st| j ƒ|k st| j ƒ}|rH| |¡}n| ¡ }| ¡ | _ |S )Nó   
)r   r   r   r   Úreadliner   )r   r   ÚchunkÚsioÚliner   r   r   r   ,   s     ø
	
zLimitedStream.readline©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s    

	r   c                   @   sZ   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zd	d
„ Zedd„ ƒZ	e
dd„ ƒZe
eeƒZdS )ÚWSGIRequestc              	   C   sÀ   t |ƒ}t|ƒp	d}|| _|| _d| d¡| ddd¡f | _|| _|| jd< || jd< |d  ¡ | _	|  
|¡ z	t| d¡ƒ}W n ttfyN   d	}Y nw t| jd
 |ƒ| _d| _d | _d S )Nú/z%s/%sÚ é   Ú	PATH_INFOÚSCRIPT_NAMEÚREQUEST_METHODÚCONTENT_LENGTHr   z
wsgi.inputF)Úget_script_nameÚget_path_infoÚenvironÚ	path_infoÚrstripÚreplaceÚpathÚMETAÚupperÚmethodÚ_set_content_type_paramsÚintÚgetÚ
ValueErrorÚ	TypeErrorr   Ú_streamÚ_read_startedÚresolver_match)r   r2   Úscript_namer3   Úcontent_lengthr   r   r   r   @   s$   


ÿ
zWSGIRequest.__init__c                 C   s   | j  d¡S )Nzwsgi.url_scheme)r2   r<   ©r   r   r   r   Ú_get_schemeY   s   zWSGIRequest._get_schemec                 C   s   t | jddƒ}t|| jd�S )NÚQUERY_STRINGr*   )Úencoding)Úget_bytes_from_wsgir2   r   Ú	_encoding)r   Úraw_query_stringr   r   r   ÚGET\   s   zWSGIRequest.GETc                 C   ó   t | dƒs	|  ¡  | jS )NÚ_post)ÚhasattrÚ_load_post_and_filesrM   rD   r   r   r   Ú	_get_postb   s   
zWSGIRequest._get_postc                 C   s
   || _ d S r#   )rM   )r   Úpostr   r   r   Ú	_set_postg   s   
zWSGIRequest._set_postc                 C   s   t | jddƒ}t|ƒS )NÚHTTP_COOKIEr*   )Úget_str_from_wsgir2   r   )r   Ú
raw_cookier   r   r   ÚCOOKIESj   s   zWSGIRequest.COOKIESc                 C   rL   )NÚ_files)rN   rO   rW   rD   r   r   r   ÚFILESo   s   
zWSGIRequest.FILESN)r$   r%   r&   r   rE   r   rK   rP   rR   rV   ÚpropertyrX   ÚPOSTr   r   r   r   r(   ?   s    


r(   c                       s(   e Zd ZeZ‡ fdd„Zdd„ Z‡  ZS )ÚWSGIHandlerc                    s   t ƒ j|i |¤Ž |  ¡  d S r#   )Úsuperr   Úload_middleware)r   ÚargsÚkwargs©Ú	__class__r   r   r   {   s   zWSGIHandler.__init__c                 C   s®   t t|ƒƒ tjj| j|d� |  |¡}|  |¡}| j|_d|j	|j
f }g | ¡ ¢dd„ |j ¡ D ƒ¢}|||ƒ t|dd ƒd urU| d¡rU|j|j_|d |j|jƒ}|S )N)Úsenderr2   z%d %sc                 s   s    � | ]}d |j dd�fV  qdS )z
Set-Cookier*   )ÚheaderN)Úoutput)Ú.0Úcr   r   r   Ú	<genexpr>Š   s   € z'WSGIHandler.__call__.<locals>.<genexpr>Úfile_to_streamzwsgi.file_wrapper)r	   r0   r   Úrequest_startedÚsendra   Úrequest_classÚget_responseÚ_handler_classÚstatus_codeÚreason_phraseÚitemsÚcookiesÚvaluesÚgetattrr<   Úcloserh   Ú
block_size)r   r2   Ústart_responseÚrequestÚresponseÚstatusÚresponse_headersr   r   r   Ú__call__   s(   

ÿþ
ÿ
ÿzWSGIHandler.__call__)r$   r%   r&   r(   rk   r   r{   Ú__classcell__r   r   r`   r   r[   x   s    r[   c                 C   s   t | ddƒ}t|ƒ ¡ S )z0Return the HTTP request's PATH_INFO as a string.r,   r)   )rH   r
   Údecode)r2   r3   r   r   r   r1   š   s   r1   c                 C   s‚   t jdurt jS t| ddƒpt| ddƒ}|r7d|v r t d|¡}t| ddƒ}|r1|dt|ƒ … n|}| ¡ S t| ddƒ}| ¡ S )	aE  
    Return the equivalent of the HTTP request's SCRIPT_NAME environment
    variable. If Apache mod_rewrite is used, return what would have been
    the script name prior to any rewriting (so it's the script name as seen
    from the client's perspective), unless the FORCE_SCRIPT_NAME setting is
    set (to anything).
    NÚ
SCRIPT_URLr*   ÚREDIRECT_URLs   //ó   /r,   r-   )r   ÚFORCE_SCRIPT_NAMErH   Ú_slashes_reÚsubr   r}   )r2   Ú
script_urlr3   rB   r   r   r   r0   ¡   s   
ÿþr0   c                 C   s   |   ||¡}| d¡S )zh
    Get a value from the WSGI environ dictionary as bytes.

    key and default should be strings.
    z
iso-8859-1)r<   Úencode©r2   ÚkeyÚdefaultÚvaluer   r   r   rH   Â   s   
rH   c                 C   s   t | ||ƒ}|jdd�S )zj
    Get a value from the WSGI environ dictionary as str.

    key and default should be str objects.
    r5   )Úerrors)rH   r}   r†   r   r   r   rT   Ï   s   rT   N)Úior   Údjango.confr   Údjango.corer   Údjango.core.handlersr   Údjango.httpr   r   r   Údjango.urlsr	   Údjango.utils.encodingr
   Údjango.utils.functionalr   Údjango.utils.regex_helperr   r‚   r   r(   ÚBaseHandlerr[   r1   r0   rH   rT   r   r   r   r   Ú<module>   s"    09"!