B
    ©0H[]  ã               @   sÞ   d dl Z d dlZd dlZ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 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 )é    N)ÚBytesIO)Úsettings)Úsignals)Úbase)ÚHttpRequestÚ	QueryDictÚparse_cookie)Úset_script_prefix)Úrepercent_broken_unicode)Úcached_propertys   /+c               @   s8   e Zd ZdZd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Úbuf_size)Úselfr   Úlimitr   © r   ú]C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\handlers\wsgi.pyÚ__init__   s    zLimitedStream.__init__Nc             C   sF   |d k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   sp   |d kr| j |  ¡  }d| _ nN|t| j ƒk rL| j d |… }| j |d … | _ n | j |  |t| j ƒ ¡ }d| _ |S )Nr   )r   r   r   )r   r   r   r   r   r   r   "   s    zLimitedStream.readc             C   sŒ   xZd| j krZ|d ks"t| j ƒ|k rZ|r<|  |t| j ƒ ¡}n|  ¡ }|sJP |  j |7  _ qW t| j ƒ}|rv| |¡}n| ¡ }| ¡ | _ |S )Nó   
)r   r   r   r   Úreadliner   )r   r   ÚchunkÚsioÚliner   r   r   r   .   s    

zLimitedStream.readline)r   )N)N)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|ƒ}|sd}|| _|| _d| d¡| ddd¡f | _|| _|| jd< || jd< |d  ¡ | _	t
 | dd¡¡\| _| _d	| jkrÄyt | jd	 ¡ W n tk
r¶   Y nX | jd	 | _d
| _yt| d¡ƒ}W n ttfk
rø   d}Y nX t| jd |ƒ| _d
| _d | _d S )Nú/z%s/%sÚ é   Ú	PATH_INFOÚSCRIPT_NAMEÚREQUEST_METHODÚCONTENT_TYPEÚcharsetFÚCONTENT_LENGTHr   z
wsgi.input)Úget_script_nameÚget_path_infoÚenvironÚ	path_infoÚrstripÚreplaceÚpathZMETAÚupperÚmethodÚcgiÚparse_headerÚgetÚcontent_typeZcontent_paramsÚcodecsÚlookupÚLookupErrorÚencodingZ_post_parse_errorÚintÚ
ValueErrorÚ	TypeErrorr   Ú_streamZ_read_startedZresolver_match)r   r2   Úscript_namer3   Úcontent_lengthr   r   r   r   C   s6    




zWSGIRequest.__init__c             C   s   | j  d¡S )Nzwsgi.url_scheme)r2   r;   )r   r   r   r   Ú_get_schemeh   s    zWSGIRequest._get_schemec             C   s   t | jddƒ}t|| jd�S )NÚQUERY_STRINGr(   )r@   )Úget_bytes_from_wsgir2   r   Ú	_encoding)r   Zraw_query_stringr   r   r   ÚGETk   s    zWSGIRequest.GETc             C   s   t | dƒs|  ¡  | jS )NÚ_post)ÚhasattrÚ_load_post_and_filesrL   )r   r   r   r   Ú	_get_postq   s    
zWSGIRequest._get_postc             C   s
   || _ d S )N)rL   )r   Úpostr   r   r   Ú	_set_postv   s    zWSGIRequest._set_postc             C   s   t | jddƒ}t|ƒS )NZHTTP_COOKIEr(   )Úget_str_from_wsgir2   r   )r   Z
raw_cookier   r   r   ÚCOOKIESy   s    zWSGIRequest.COOKIESc             C   s   t | dƒs|  ¡  | jS )NÚ_files)rM   rN   rT   )r   r   r   r   ÚFILES~   s    
zWSGIRequest.FILESN)r"   r#   r$   r   rG   r   rK   rO   rQ   rS   ÚpropertyrU   ÚPOSTr   r   r   r   r&   B   s   %r&   c                   s(   e Zd ZeZ‡ fdd„Zdd„ Z‡  ZS )ÚWSGIHandlerc                s   t ƒ j||Ž |  ¡  d S )N)Úsuperr   Z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 }t| ¡ ƒ}x(|j ¡ D ]}| d|jdd�f¡ qbW |||ƒ t|dd ƒd k	r²| d¡r²|d |jƒ}|S )N)Zsenderr2   z%d %sz
Set-Cookier(   )ÚheaderÚfile_to_streamzwsgi.file_wrapper)r	   r0   r   Zrequest_startedÚsendr\   Úrequest_classZget_responseZ_handler_classÚstatus_codeZreason_phraseÚlistÚitemsÚcookiesÚvaluesÚappendÚoutputÚgetattrr;   r^   )r   r2   Zstart_responseÚrequestÚresponseÚstatusÚresponse_headersÚcr   r   r   Ú__call__Ž   s    


zWSGIHandler.__call__)r"   r#   r$   r&   r`   r   rn   Ú__classcell__r   r   )r\   r   rX   ‡   s   rX   c             C   s   t | ddƒ}t|ƒ ¡ S )z0Return the HTTP request's PATH_INFO as a string.r*   r'   )rI   r
   Údecode)r2   r3   r   r   r   r1       s    r1   c             C   s€   t jdk	rt jS t| ddƒ}|s,t| ddƒ}|rld|krDt d|¡}t| ddƒ}|rf|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   ZFORCE_SCRIPT_NAMErI   Ú_slashes_reÚsubr   rp   )r2   Z
script_urlr3   rE   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   rI   È   s    rI   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)rI   rp   )r2   ru   rv   rw   r   r   r   rR   Õ   s    rR   )r9   r=   ÚreÚior   Zdjango.confr   Zdjango.corer   Zdjango.core.handlersr   Zdjango.httpr   r   r   Zdjango.urlsr	   Zdjango.utils.encodingr
   Zdjango.utils.functionalr   Úcompilerr   r   r&   ÚBaseHandlerrX   r1   r0   rI   rR   r   r   r   r   Ú<module>   s$   
1E!