a
    Qh                     @   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e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 )    )IOBase)settings)signals)base)HttpRequest	QueryDictparse_cookie)set_script_prefix)repercent_broken_unicode)cached_property)_lazy_re_compiles   /+c                   @   s,   e Zd ZdZdd Zd
ddZdddZd	S )LimitedStreamz
    Wrap another stream to disallow reading it past a number of bytes.

    Based on the implementation from werkzeug.wsgi.LimitedStream
    See https://github.com/pallets/werkzeug/blob/dbf78f67/src/werkzeug/wsgi.py#L828
    c                 C   s    |j | _|j| _d| _|| _d S )Nr   )read_readreadline	_readline_poslimit)selfstreamr    r   R/var/www/sistema_ama/venv/lib/python3.9/site-packages/django/core/handlers/wsgi.py__init__   s    zLimitedStream.__init__c                C   s`   | j }| j}||krdS |dks(|d u r2|| }nt||| }| |}|  j t|7  _ |S N    r   )r   r   minr   len)r   sizer   r   datar   r   r   r      s    

zLimitedStream.readc                C   s`   | j }| j}||krdS |dks(|d u r2|| }nt||| }| |}|  j t|7  _ |S r   )r   r   r   r   r   )r   r   r   r   liner   r   r   r   *   s    

zLimitedStream.readlineN)r   )r   )__name__
__module____qualname____doc__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y   d	}Y n0 t| jd
 || _d| _d | _d S )N/z%s/%s    	PATH_INFOSCRIPT_NAMEZREQUEST_METHODZCONTENT_LENGTHr   z
wsgi.inputF)get_script_nameget_path_infoenviron	path_inforstripreplacepathZMETAuppermethodZ_set_content_type_paramsintget
ValueError	TypeErrorr   _streamZ_read_startedZresolver_match)r   r-   script_namer.   Zcontent_lengthr   r   r   r   9   s"    



zWSGIRequest.__init__c                 C   s   | j dS )Nzwsgi.url_scheme)r-   r5   r   r   r   r   _get_schemeR   s    zWSGIRequest._get_schemec                 C   s   t | jdd}t|| jdS )NZQUERY_STRINGr'   )encoding)get_bytes_from_wsgir-   r   Z	_encoding)r   Zraw_query_stringr   r   r   GETU   s    zWSGIRequest.GETc                 C   s   t | ds|   | jS )N_post)hasattr_load_post_and_filesr?   r:   r   r   r   	_get_post[   s    
zWSGIRequest._get_postc                 C   s
   || _ d S N)r?   )r   Zpostr   r   r   	_set_post`   s    zWSGIRequest._set_postc                 C   s   t | jdd}t|S )NZHTTP_COOKIEr'   )get_str_from_wsgir-   r   )r   Z
raw_cookier   r   r   COOKIESc   s    zWSGIRequest.COOKIESc                 C   s   t | ds|   | jS )N_files)r@   rA   rG   r:   r   r   r   FILESh   s    
zWSGIRequest.FILESN)r!   r"   r#   r   r;   r   r>   rB   rD   rF   propertyrH   ZPOSTr   r   r   r   r%   8   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 rC   )superr   Zload_middleware)r   argskwargs	__class__r   r   r   t   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r|dr|j|j_|d |j|j}|S )N)Zsenderr-   z%d %sc                 s   s   | ]}d |j ddfV  qdS )z
Set-Cookier'   )headerN)output).0cr   r   r   	<genexpr>   r   z'WSGIHandler.__call__.<locals>.<genexpr>file_to_streamzwsgi.file_wrapper)r	   r+   r   Zrequest_startedsendrO   request_classZget_responseZ_handler_classZstatus_codeZreason_phraseitemsZcookiesvaluesgetattrr5   closerU   Z
block_size)r   r-   start_responseZrequestZresponsestatusZresponse_headersr   r   r   __call__x   s(    



zWSGIHandler.__call__)r!   r"   r#   r%   rW   r   r^   __classcell__r   r   rN   r   rJ   q   s   rJ   c                 C   s   t | dd}t| S )z0Return the HTTP request's PATH_INFO as a string.r)   r&   )r=   r
   decode)r-   r.   r   r   r   r,      s    r,   c                 C   s|   t jdurt jS t| ddp&t| dd}|rhd|v r@td|}t| dd}|rb|dt|  n|}n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).
    NZ
SCRIPT_URLr'   ZREDIRECT_URLs   //   /r)   r*   )r   FORCE_SCRIPT_NAMEr=   _slashes_resubr   r`   )r-   Z
script_urlr.   r9   r   r   r   r+      s    
r+   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)r5   encoder-   keydefaultvaluer   r   r   r=      s    r=   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.
    r0   )errors)r=   r`   rf   r   r   r   rE      s    rE   N)ior   django.confr   Zdjango.corer   Zdjango.core.handlersr   Zdjango.httpr   r   r   django.urlsr	   Zdjango.utils.encodingr
   django.utils.functionalr   django.utils.regex_helperr   rc   r   r%   ZBaseHandlerrJ   r,   r+   r=   rE   r   r   r   r   <module>   s    )9"!