B
    ª0H[hP  ã               @   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m	Z	m
Z
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mZ d d
lmZmZ d dlmZ d dlmZm Z m!Z! d dl"m#Z#m$Z$ e%ƒ Z&e 'd¡Z(G dd„ de)ƒZ*G dd„ de+ƒZ,G dd„ dƒZ-G dd„ deƒZ.dd„ Z/dd„ Z0dd„ Z1dS )é    N)ÚBytesIO)Úchain)ÚquoteÚ	urlencodeÚurljoinÚurlsplit)Úsettings)Úsigning)ÚDisallowedHostÚImproperlyConfiguredÚRequestDataTooBig)Úuploadhandler)ÚMultiPartParserÚMultiPartParserError)ÚImmutableListÚMultiValueDict)ÚRemovedInDjango30Warning)Úescape_uri_pathÚforce_bytesÚ
iri_to_uri)Úis_same_domainÚlimited_parse_qslz1^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9\.:]+\])(:\d+)?$c               @   s   e Zd ZdS )ÚUnreadablePostErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úWC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\http\request.pyr      s   r   c               @   s   e Zd ZdZdS )ÚRawPostDataExceptionzŽ
    You cannot access raw_post_data from a request that has
    multipart/* POST data if it has been accessed via POST,
    FILES, etc..
    N)r   r   r   Ú__doc__r   r   r   r   r      s   r   c               @   s   e Zd ZdZdZg Zdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
d;dd„Zeddfdd„Zdd„ Zd<dd„Zdd„ Zedd„ ƒZdd„ Zdd„ Zedd „ ƒZejd!d „ ƒZd"d#„ Zed$d%„ ƒZejd&d%„ ƒZd'd(„ Zed)d*„ ƒZd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Z d7d8„ Z!d9d:„ Z"dS )=ÚHttpRequestzA basic HTTP request.Nc             C   sZ   t dd�| _t dd�| _i | _i | _tƒ | _d| _d| _d | _	d | _
d| _d | _d | _d S )NT)ÚmutableÚ F)Ú	QueryDictÚGETÚPOSTÚCOOKIESÚMETAr   ZFILESÚpathZ	path_infoÚmethodZresolver_matchÚ_post_parse_errorÚcontent_typeZcontent_params)Úselfr   r   r   Ú__init__,   s    zHttpRequest.__init__c             C   s6   | j d ks|  ¡ sd| jj S d| jj| j |  ¡ f S )Nz<%s>z<%s: %s %r>)r)   Úget_full_pathÚ	__class__r   )r,   r   r   r   Ú__repr__?   s    zHttpRequest.__repr__c             C   sh   t jrd| jkr| jd }nHd| jkr2| jd }n2| jd }|  ¡ }||  ¡ rRdndkrdd||f }|S )z—
        Return the HTTP host using the environment or request headers. Skip
        allowed hosts protection, so may return an insecure host.
        ZHTTP_X_FORWARDED_HOSTZ	HTTP_HOSTZSERVER_NAMEZ443Ú80z%s:%s)r   ZUSE_X_FORWARDED_HOSTr'   Úget_portÚ	is_secure)r,   ÚhostZserver_portr   r   r   Ú_get_raw_hostD   s    


zHttpRequest._get_raw_hostc             C   sn   |   ¡ }tj}tjr"|s"dddg}t|ƒ\}}|r@t||ƒr@|S d| }|rZ|d| 7 }n|d7 }t|ƒ‚dS )z>Return the HTTP host using the environment or request headers.Ú	localhostz	127.0.0.1z[::1]zInvalid HTTP_HOST header: %r.z) You may need to add %r to ALLOWED_HOSTS.zB The domain name provided is not valid according to RFC 1034/1035.N)r5   r   ZALLOWED_HOSTSÚDEBUGÚsplit_domain_portÚvalidate_hostr
   )r,   r4   Úallowed_hostsÚdomainÚportÚmsgr   r   r   Úget_hostW   s    

zHttpRequest.get_hostc             C   s.   t jrd| jkr| jd }n
| jd }t|ƒS )z3Return the port number for the request as a string.ZHTTP_X_FORWARDED_PORTZSERVER_PORT)r   ZUSE_X_FORWARDED_PORTr'   Ústr)r,   r<   r   r   r   r2   k   s    
zHttpRequest.get_portFc          	   C   sL   dt | jƒ|r| j d¡sdnd| j dd¡rDdt| j dd¡ƒ ndf S )Nz%s%s%sú/r"   ÚQUERY_STRINGú?)r   r(   Úendswithr'   Úgetr   )r,   Zforce_append_slashr   r   r   r.   s   s    zHttpRequest.get_full_pathr"   c             C   sz   y| j | }W n" tk
r0   |tk	r*|S ‚ Y nX ytj|| d�j||d�}W n$ tjk
rt   |tk	rn|S ‚ Y nX |S )z×
        Attempt to return a signed cookie. If the signature fails or the
        cookie has expired, raise an exception, unless the `default` argument
        is provided,  in which case return that value.
        )Úsalt)Úmax_age)r&   ÚKeyErrorÚRAISE_ERRORr	   Zget_cookie_signerZunsignZBadSignature)r,   ÚkeyÚdefaultrE   rF   Zcookie_valueÚvaluer   r   r   Úget_signed_cookie|   s    zHttpRequest.get_signed_cookiec             C   s   dj | j|  ¡ |  ¡ d�S )z•
        Return an absolute URI from variables available in this request. Skip
        allowed hosts protection, so may return insecure URI.
        z{scheme}://{host}{path})Úschemer4   r(   )ÚformatrM   r5   r.   )r,   r   r   r   Úget_raw_uri“   s    zHttpRequest.get_raw_uric             C   sR   |dkrd|   ¡  }t|ƒ}|jr(|jsJdj| j|  ¡ | jd�}t||ƒ}t|ƒS )aÆ  
        Build an absolute URI from the location and the variables available in
        this request. If no ``location`` is specified, bulid the absolute URI
        using request.get_full_path(). If the location is absolute, convert it
        to an RFC 3987 compliant URI and return it. If location is relative or
        is scheme-relative (i.e., ``//example.com/``), urljoin() it to a base
        URL constructed from the request variables.
        Nz//%sz{scheme}://{host}{path})rM   r4   r(   )	r.   r   rM   ÚnetlocrN   r>   r(   r   r   )r,   ÚlocationÚbitsZcurrent_urir   r   r   Úbuild_absolute_uriž   s    	

zHttpRequest.build_absolute_uric             C   s   dS )zf
        Hook for subclasses like WSGIRequest to implement. Return 'http' by
        default.
        Úhttpr   )r,   r   r   r   Ú_get_scheme¶   s    zHttpRequest._get_schemec             C   sN   t jrFyt j\}}W n tk
r0   tdƒ‚Y nX | j |¡|krFdS |  ¡ S )NzJThe SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.Úhttps)r   ZSECURE_PROXY_SSL_HEADERÚ
ValueErrorr   r'   rD   rU   )r,   ÚheaderrK   r   r   r   rM   ½   s    zHttpRequest.schemec             C   s
   | j dkS )NrV   )rM   )r,   r   r   r   r3   Ê   s    zHttpRequest.is_securec             C   s   | j  d¡dkS )NZHTTP_X_REQUESTED_WITHZXMLHttpRequest)r'   rD   )r,   r   r   r   Úis_ajaxÍ   s    zHttpRequest.is_ajaxc             C   s   | j S )N)Ú	_encoding)r,   r   r   r   ÚencodingÐ   s    zHttpRequest.encodingc             C   s&   || _ t| dƒr| `t| dƒr"| `dS )zÓ
        Set the encoding used for GET/POST accesses. If the GET or POST
        dictionary has already been created, remove and recreate it on the
        next access (so that it is decoded correctly).
        r$   Ú_postN)rZ   Úhasattrr$   r\   )r,   Úvalr   r   r   r[   Ô   s
    

c                s   ‡ fdd„t jD ƒˆ _d S )Nc                s   g | ]}t  |ˆ ¡‘qS r   )r   Zload_handler)Ú.0Úhandler)r,   r   r   ú
<listcomp>â   s   z4HttpRequest._initialize_handlers.<locals>.<listcomp>)r   ZFILE_UPLOAD_HANDLERSÚ_upload_handlers)r,   r   )r,   r   Ú_initialize_handlersá   s    
z HttpRequest._initialize_handlersc             C   s   | j s|  ¡  | j S )N)rb   rc   )r,   r   r   r   Úupload_handlerså   s    zHttpRequest.upload_handlersc             C   s   t | dƒrtdƒ‚|| _d S )NÚ_fileszGYou cannot set the upload handlers after the upload has been processed.)r]   ÚAttributeErrorrb   )r,   rd   r   r   r   rd   ì   s    
c             C   s*   t | jdd�| _t||| j| jƒ}| ¡ S )z9Return a tuple of (POST QueryDict, FILES MultiValueDict).zEYou cannot alter upload handlers after the upload has been processed.)Úwarning)r   rd   r   r[   Úparse)r,   r'   Z	post_dataÚparserr   r   r   Úparse_file_uploadò   s
    
zHttpRequest.parse_file_uploadc          
   C   s”   t | dƒsŽ| jrtdƒ‚tjd k	rDt| j d¡p2dƒtjkrDtdƒ‚y|  	¡ | _
W n. tk
r€ } zt|jŽ |‚W d d }~X Y nX t| j
ƒ| _| j
S )NÚ_bodyz?You cannot access body after reading from request's data streamÚCONTENT_LENGTHr   z;Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.)r]   Ú_read_startedr   r   ZDATA_UPLOAD_MAX_MEMORY_SIZEÚintr'   rD   r   Úreadrk   ÚIOErrorr   Úargsr   Ú_stream)r,   Úer   r   r   Úbodyû   s    

zHttpRequest.bodyc             C   s   t ƒ | _tƒ | _d| _d S )NT)r#   r\   r   re   r*   )r,   r   r   r   Ú_mark_post_parse_error  s    z"HttpRequest._mark_post_parse_errorc             C   sæ   | j dkr&t| jd�tƒ  | _| _dS | jrBt| dƒsB|  ¡  dS | j	dkr¢t| dƒrbt
| jƒ}n| }y|  | j|¡\| _| _W qâ tk
rž   |  ¡  ‚ Y qâX n@| j	dkrÊt| j| jd�tƒ  | _| _nt| jd�tƒ  | _| _dS )zFPopulate self._post and self._files if the content-type is a form typer%   )r[   Nrk   zmultipart/form-dataz!application/x-www-form-urlencoded)r)   r#   rZ   r   r\   re   rm   r]   ru   r+   r   rk   rj   r'   r   rt   )r,   Údatar   r   r   Ú_load_post_and_files  s$    




z HttpRequest._load_post_and_filesc             C   s:   t | dƒr6x*t dd„ | j ¡ D ƒ¡D ]}| ¡  q&W d S )Nre   c             s   s   | ]}|d  V  qdS )é   Nr   )r_   Úlr   r   r   ú	<genexpr>4  s    z$HttpRequest.close.<locals>.<genexpr>)r]   r   Úfrom_iterablere   ÚlistsÚclose)r,   Úfr   r   r   r}   2  s    
 zHttpRequest.closec          
   O   sH   d| _ y| jj||ŽS  tk
rB } zt|jŽ |‚W d d }~X Y nX d S )NT)rm   rr   ro   rp   r   rq   )r,   rq   Úkwargsrs   r   r   r   ro   ?  s
    zHttpRequest.readc          
   O   sH   d| _ y| jj||ŽS  tk
rB } zt|jŽ |‚W d d }~X Y nX d S )NT)rm   rr   Úreadlinerp   r   rq   )r,   rq   r   rs   r   r   r   r€   F  s
    zHttpRequest.readlinec             c   s   x|   ¡ }|sP |V  qW d S )N)r€   )r,   Úbufr   r   r   Ú__iter__M  s
    zHttpRequest.__iter__c             c   s   t jdtdd� | E d H  d S )NzIHttpRequest.xreadlines() is deprecated in favor of iterating the request.é   )Ú
stacklevel)ÚwarningsÚwarnr   )r,   r   r   r   Ú
xreadlinesT  s    
zHttpRequest.xreadlinesc             C   s   t | ƒS )N)Úlist)r,   r   r   r   Ú	readlines[  s    zHttpRequest.readlines)F)N)#r   r   r   r   rZ   rb   r-   r0   r5   r>   r2   r.   rH   rL   rO   rS   rU   ÚpropertyrM   r3   rY   r[   Úsetterrc   rd   rj   rt   ru   rw   r}   ro   r€   r‚   r‡   r‰   r   r   r   r   r    %   s>   
	
	 r    c                   sì   e Zd ZdZdZdZd)‡ fdd„	Zed*dd	„ƒZe	d
d„ ƒZ
e
jdd„ ƒZ
dd„ Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Z‡ fdd„Zd+‡ fdd„	Z‡ fdd„Z‡ fdd„Z‡ fdd „Z‡ fd!d"„Zd,‡ fd#d$„	Zd%d&„ Zd-d'd(„Z‡  ZS ).r#   aû  
    A specialized MultiValueDict which represents a query string.

    A QueryDict can be used to represent GET or POST data. It subclasses
    MultiValueDict since keys in such data can be repeated, for instance
    in the data from a form with a <select multiple> field.

    By default QueryDicts are immutable, though the copy() method
    will always return a mutable copy.

    Both keys and values set on this class are converted from the given encoding
    (DEFAULT_CHARSET by default) to str.
    TNFc                s˜   t ƒ  ¡  |stj}|| _|p d}dtj|dœ}t|tƒrhy| |¡}W n t	k
rf   | d¡}Y nX x$t
|f|ŽD ]\}}|  ||¡ qvW || _d S )Nr"   T)Úkeep_blank_valuesZfields_limitr[   z
iso-8859-1)Úsuperr-   r   ÚDEFAULT_CHARSETr[   ZDATA_UPLOAD_MAX_NUMBER_FIELDSÚ
isinstanceÚbytesÚdecodeÚUnicodeDecodeErrorr   Ú
appendlistÚ_mutable)r,   Zquery_stringr!   r[   Zparse_qsl_kwargsrI   rK   )r/   r   r   r-   s  s     

zQueryDict.__init__r"   c             C   s6   | dd|d�}x|D ]}|  ||¡ qW |s2d|_|S )zt
        Return a new QueryDict with keys (may be repeated) from an iterable and
        values from value.
        r"   T)r!   r[   F)r“   r”   )ÚclsÚiterablerK   r!   r[   ÚqrI   r   r   r   Úfromkeys‰  s    
zQueryDict.fromkeysc             C   s   | j d krtj| _ | j S )N)rZ   r   rŽ   )r,   r   r   r   r[   –  s    
zQueryDict.encodingc             C   s
   || _ d S )N)rZ   )r,   rK   r   r   r   r[   œ  s    c             C   s   | j stdƒ‚d S )Nz$This QueryDict instance is immutable)r”   rf   )r,   r   r   r   Ú_assert_mutable   s    zQueryDict._assert_mutablec                s2   |   ¡  t|| jƒ}t|| jƒ}tƒ  ||¡ d S )N)r™   Úbytes_to_textr[   r�   Ú__setitem__)r,   rI   rK   )r/   r   r   r›   ¤  s    zQueryDict.__setitem__c                s   |   ¡  tƒ  |¡ d S )N)r™   r�   Ú__delitem__)r,   rI   )r/   r   r   rœ   ª  s    zQueryDict.__delitem__c             C   s8   | j dd| jd�}x |  ¡ D ]\}}| ||¡ qW |S )Nr"   T)r!   r[   )r/   r[   r|   Úsetlist)r,   ÚresultrI   rK   r   r   r   Ú__copy__®  s    zQueryDict.__copy__c             C   sT   | j dd| jd�}||t| ƒ< x0|  ¡ D ]$\}}| t ||¡t ||¡¡ q(W |S )Nr"   T)r!   r[   )r/   r[   Úidr|   r�   ÚcopyÚdeepcopy)r,   Úmemorž   rI   rK   r   r   r   Ú__deepcopy__´  s
     zQueryDict.__deepcopy__c                s8   ˆ   ¡  t|ˆ jƒ}‡ fdd„|D ƒ}tƒ  ||¡ d S )Nc                s   g | ]}t |ˆ jƒ‘qS r   )rš   r[   )r_   Úelt)r,   r   r   ra   ¾  s    z%QueryDict.setlist.<locals>.<listcomp>)r™   rš   r[   r�   r�   )r,   rI   Úlist_)r/   )r,   r   r�   »  s    zQueryDict.setlistc                s   |   ¡  tƒ  ||¡S )N)r™   r�   Úsetlistdefault)r,   rI   Zdefault_list)r/   r   r   r§   Á  s    zQueryDict.setlistdefaultc                s2   |   ¡  t|| jƒ}t|| jƒ}tƒ  ||¡ d S )N)r™   rš   r[   r�   r“   )r,   rI   rK   )r/   r   r   r“   Å  s    zQueryDict.appendlistc                s   |   ¡  tƒ j|f|žŽ S )N)r™   r�   Úpop)r,   rI   rq   )r/   r   r   r¨   Ë  s    zQueryDict.popc                s   |   ¡  tƒ  ¡ S )N)r™   r�   Úpopitem)r,   )r/   r   r   r©   Ï  s    zQueryDict.popitemc                s   |   ¡  tƒ  ¡  d S )N)r™   r�   Úclear)r,   )r/   r   r   rª   Ó  s    zQueryDict.clearc                s.   |   ¡  t|| jƒ}t|| jƒ}tƒ  ||¡S )N)r™   rš   r[   r�   Ú
setdefault)r,   rI   rJ   )r/   r   r   r«   ×  s    zQueryDict.setdefaultc             C   s
   |   i ¡S )z%Return a mutable copy of this object.)r¤   )r,   r   r   r   r¡   Ý  s    zQueryDict.copyc                sr   g }ˆr"t ˆˆjƒ‰‡fdd„‰ ndd„ ‰ x<ˆ ¡ D ]0\‰}t ˆˆjƒ‰| ‡ ‡‡fdd„|D ƒ¡ q4W d |¡S )ah  
        Return an encoded string of all query string arguments.

        `safe` specifies characters which don't require quoting, for example::

            >>> q = QueryDict(mutable=True)
            >>> q['next'] = '/a&b/'
            >>> q.urlencode()
            'next=%2Fa%26b%2F'
            >>> q.urlencode(safe='/')
            'next=/a%26b/'
        c                s   dt | ˆ ƒt |ˆ ƒf S )Nz%s=%s)r   )ÚkÚv)Úsafer   r   Úencodeò  s    z#QueryDict.urlencode.<locals>.encodec             S   s   t | |iƒS )N)r   )r¬   r­   r   r   r   r¯   õ  s    c             3   s    | ]}ˆ ˆt |ˆjƒƒV  qd S )N)r   r[   )r_   r­   )r¯   r¬   r,   r   r   rz   ù  s   z&QueryDict.urlencode.<locals>.<genexpr>ú&)r   r[   r|   ÚextendÚjoin)r,   r®   Úoutputr¦   r   )r¯   r¬   r®   r,   r   r   á  s    zQueryDict.urlencode)NFN)r"   FN)N)N)N)r   r   r   r   r”   rZ   r-   Úclassmethodr˜   rŠ   r[   r‹   r™   r›   rœ   rŸ   r¤   r�   r§   r“   r¨   r©   rª   r«   r¡   r   Ú__classcell__r   r   )r/   r   r#   _  s,   r#   c             C   s   t | tƒrt| |dƒS | S dS )u×   
    Convert bytes objects to strings, using the given encoding. Illegally
    encoded input characters are replaced with Unicode "unknown" codepoint
    (ï¿½).

    Return any non-bytes objects without change.
    ÚreplaceN)r�   r�   r?   )Úsr[   r   r   r   rš     s    
rš   c             C   sx   |   ¡ } t | ¡sdS | d dkr*| dfS |  dd¡}t|ƒdkrF|n
|d df\}}| d	¡rl|d
d… n|}||fS )z•
    Return a (domain, port) tuple from a given host.

    Returned domain is lower-cased. If the host is invalid, the domain will be
    empty.
    )r"   r"   éÿÿÿÿú]r"   ú:rx   rƒ   r   Ú.N)ÚlowerÚhost_validation_reÚmatchÚrsplitÚlenrC   )r4   rR   r;   r<   r   r   r   r8     s    
 r8   c             C   s(   x"|D ]}|dkst | |ƒrdS qW dS )a5  
    Validate the given host for this site.

    Check that the host looks valid and matches a host or host pattern in the
    given list of ``allowed_hosts``. Any pattern beginning with a period
    matches a domain and all its subdomains (e.g. ``.example.com`` matches
    ``example.com`` and any subdomain), ``*`` matches anything, and anything
    else must match exactly.

    Note: This function assumes that the given host is lower-cased and has
    already had the port, if any, stripped off.

    Return ``True`` for a valid host, ``False`` otherwise.
    Ú*TF)r   )r4   r:   Úpatternr   r   r   r9   %  s    
r9   )2r¡   Úrer…   Úior   Ú	itertoolsr   Úurllib.parser   r   r   r   Zdjango.confr   Zdjango.corer	   Zdjango.core.exceptionsr
   r   r   Zdjango.core.filesr   Zdjango.http.multipartparserr   r   Zdjango.utils.datastructuresr   r   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   r   Zdjango.utils.httpr   r   ÚobjectrH   Úcompiler½   rp   r   Ú	Exceptionr   r    r#   rš   r8   r9   r   r   r   r   Ú<module>   s4   
	  < #