3
W­E^]W  ã               @   sR  d dl Z 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mZ d dlmZm Z  d dl!m"Z" d dl#m$Z$m%Z% e&ƒ Z'ej(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/G dd„ deƒZ0dd„ Z1dd„ Z2dd„ Z3dS )é    N)ÚBytesIO)Úchain)ÚquoteÚ	urlencodeÚurljoinÚurlsplit)Úsettings)Úsigning)ÚDisallowedHostÚImproperlyConfiguredÚRequestDataTooBig)Úuploadhandler)ÚMultiPartParserÚMultiPartParserError)ÚCaseInsensitiveMappingÚImmutableListÚMultiValueDict)Úescape_uri_pathÚ
iri_to_uri)Úcached_property)Ú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   ú5/tmp/pip-build-78s3knkw/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               @   sJ  e Zd ZdZdZg Zdd„ Zdd„ Zedd„ ƒZ	d	d
„ Z
dd„ Zdd„ Zdd„ ZdCdd„ZdDdd„Zdd„ Zeddfdd„Zdd„ ZdEdd„Ze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d0d/„ ƒZd1d2„ Zed3d4„ ƒZ d5d6„ Z!d7d8„ Z"d9d:„ Z#d;d<„ Z$d=d>„ Z%d?d@„ Z&dAdB„ Z'dS )FÚHttpRequestzA basic HTTP request.Nc             C   sT   t dd�| _t dd�| _i | _i | _tƒ | _d| _d| _d | _	d | _
d | _d | _d S )NT)ÚmutableÚ )Ú	QueryDictÚGETÚPOSTÚCOOKIESÚMETAr   ZFILESÚpathÚ	path_infoÚmethodZresolver_matchÚcontent_typeÚcontent_params)Úselfr   r   r   Ú__init__/   s    zHttpRequest.__init__c             C   s8   | j d ks| jƒ  r d| jj S d| jj| j | jƒ f S )Nz<%s>z<%s: %s %r>)r*   Úget_full_pathÚ	__class__r   )r-   r   r   r   Ú__repr__A   s    zHttpRequest.__repr__c             C   s
   t | jƒS )N)ÚHttpHeadersr'   )r-   r   r   r   ÚheadersF   s    zHttpRequest.headersc             C   s^   t j|jddƒƒ\| _| _d| jkrZytj| jd ƒ W n tk
rL   Y nX | jd | _dS )z/Set content_type, content_params, and encoding.ÚCONTENT_TYPEr"   ÚcharsetN)	ÚcgiÚparse_headerÚgetr+   r,   ÚcodecsÚlookupÚLookupErrorÚencoding)r-   Úmetar   r   r   Ú_set_content_type_paramsJ   s    
z$HttpRequest._set_content_type_paramsc             C   sh   t jrd| jkr| jd }nHd| jkr2| jd }n2| jd }| jƒ }|| jƒ 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_hostU   s    


zHttpRequest._get_raw_hostc             C   sp   | j ƒ }tj}tjr$| r$dddg}t|ƒ\}}|rBt||ƒrB|S d| }|r\|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)rC   r   ZALLOWED_HOSTSÚDEBUGÚsplit_domain_portÚvalidate_hostr
   )r-   rB   Úallowed_hostsÚdomainÚportÚmsgr   r   r   Úget_hosth   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-   rJ   r   r   r   r@   |   s    
zHttpRequest.get_portFc             C   s   | j | j|ƒS )N)Ú_get_full_pathr(   )r-   Úforce_append_slashr   r   r   r/   „   s    zHttpRequest.get_full_pathc             C   s   | j | j|ƒS )N)rN   r)   )r-   rO   r   r   r   Úget_full_path_info‡   s    zHttpRequest.get_full_path_infoc             C   sJ   dt |ƒ|r|jdƒ rdnd| jjddƒrBdt| jjddƒƒ ndf S )Nz%s%s%sú/r"   ÚQUERY_STRINGú?)r   Úendswithr'   r8   r   )r-   r(   rO   r   r   r   rN   Š   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ÚdefaultrU   rV   Zcookie_valueÚvaluer   r   r   Úget_signed_cookie“   s    zHttpRequest.get_signed_cookiec             C   s   dj | j| 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})ÚschemerB   r(   )Úformatr]   rC   r/   )r-   r   r   r   Úget_raw_uriª   s    zHttpRequest.get_raw_uric             C   s”   |dkrd| j ƒ  }t|ƒ}|jo&|jsŒ|jjdƒrz|j rz|j rzd|jkrzd|jkrz|jdƒrn|dd… }| j| }nt| j| j |ƒ}t|ƒS )aÆ  
        Build an absolute URI from the location and the variables available in
        this request. If no ``location`` is specified, build 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//%srQ   z/./z/../z//é   )	r/   r   r]   Únetlocr(   Ú
startswithÚ_current_scheme_hostr   r   )r-   ÚlocationÚbitsr   r   r   Úbuild_absolute_uriµ   s    	
zHttpRequest.build_absolute_uric             C   s   dj | j| jƒ ƒS )Nz{}://{})r^   r]   rL   )r-   r   r   r   rc   Ö   s    z HttpRequest._current_scheme_hostc             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   s^   t jrVyt j\}}W n tk
r0   tdƒ‚Y nX | jj|ƒ}|d k	rV||krRdS dS | jƒ S )NzJThe SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.Úhttpsrg   )r   ZSECURE_PROXY_SSL_HEADERÚ
ValueErrorr   r'   r8   rh   )r-   ÚheaderZsecure_valueÚheader_valuer   r   r   r]   á   s    zHttpRequest.schemec             C   s
   | j dkS )Nri   )r]   )r-   r   r   r   rA   ï   s    zHttpRequest.is_securec             C   s   | j jdƒdkS )NZHTTP_X_REQUESTED_WITHZXMLHttpRequest)r'   r8   )r-   r   r   r   Úis_ajaxò   s    zHttpRequest.is_ajaxc             C   s   | j S )N)Ú	_encoding)r-   r   r   r   r<   õ   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)rn   Úhasattrr$   ro   )r-   Úvalr   r   r   r<   ù   s
    

c                s   ‡ fdd„t jD ƒˆ _d S )Nc                s   g | ]}t j|ˆ ƒ‘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ƒ  | j S )N)ru   rv   )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.)rp   ÚAttributeErrorru   )r-   rw   r   r   r   rw     s    
c             C   s*   t | jdd�| _t||| j| jƒ}|jƒ S )z9Return a tuple of (POST QueryDict, FILES MultiValueDict).zEYou cannot alter upload handlers after the upload has been processed.)Úwarning)r   rw   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jdƒp2dƒtjkrDtdƒ‚y| j	ƒ | _
W n. tk
r€ } zt|jŽ |‚W Y d d }~X 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.)rp   Ú_read_startedr   r   ZDATA_UPLOAD_MAX_MEMORY_SIZEÚintr'   r8   r   Úreadr~   ÚOSErrorr   Úargsr   Ú_stream)r-   Úer   r   r   Úbody   s    

zHttpRequest.bodyc             C   s   t ƒ | _tƒ | _d S )N)r#   ro   r   rx   )r-   r   r   r   Ú_mark_post_parse_error2  s    z"HttpRequest._mark_post_parse_errorc             C   sè   | j dkr&t| jd�tƒ  | _| _dS | jrDt| dƒ rD| jƒ  dS | j	dkr¤t| dƒrdt
| jƒ}n| }y| j| j|ƒ\| _| _W qä tk
r    | jƒ  ‚ 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<   Nr~   zmultipart/form-dataz!application/x-www-form-urlencoded)r*   r#   rn   r   ro   rx   r€   rp   rˆ   r+   r   r~   r}   r'   r   r‡   )r-   Údatar   r   r   Ú_load_post_and_files6  s$    




z HttpRequest._load_post_and_filesc             C   s:   t | dƒr6x*tjdd„ | jjƒ D ƒƒD ]}|jƒ  q&W d S )Nrx   c             s   s   | ]}|d  V  qdS )é   Nr   )rr   Úlr   r   r   ú	<genexpr>U  s    z$HttpRequest.close.<locals>.<genexpr>)rp   r   Úfrom_iterablerx   ÚlistsÚclose)r-   Úfr   r   r   r�   S  s    
 zHttpRequest.closec             O   sH   d| _ y| jj||ŽS  tk
rB } zt|jŽ |‚W Y d d }~X nX d S )NT)r€   r…   r‚   rƒ   r   r„   )r-   r„   Úkwargsr†   r   r   r   r‚   `  s
    zHttpRequest.readc             O   sH   d| _ y| jj||ŽS  tk
rB } zt|jŽ |‚W Y d d }~X nX d S )NT)r€   r…   Úreadlinerƒ   r   r„   )r-   r„   r’   r†   r   r   r   r“   g  s
    zHttpRequest.readlinec             C   s   t | jdƒS )Nó    )Úiterr“   )r-   r   r   r   Ú__iter__n  s    zHttpRequest.__iter__c             C   s   t | ƒS )N)Úlist)r-   r   r   r   Ú	readlinesq  s    zHttpRequest.readlines)F)F)N)(r   r   r   r   rn   ru   r.   r1   r   r3   r>   rC   rL   r@   r/   rP   rN   rX   r\   r_   rf   rc   rh   Úpropertyr]   rA   rm   r<   Úsetterrv   rw   r}   r‡   rˆ   rŠ   r�   r‚   r“   r–   r˜   r   r   r   r   r    (   sF   

	
!	r    c                   s@   e Zd ZdZddhZ‡ fdd„Z‡ fdd„Zedd	„ ƒZ‡  Z	S )
r2   ZHTTP_r4   r   c                s@   i }x*|j ƒ D ]\}}| j|ƒ}|r|||< qW tƒ j|ƒ d S )N)ÚitemsÚparse_header_nameÚsuperr.   )r-   Úenvironr3   rk   r[   Úname)r0   r   r   r.   z  s    
zHttpHeaders.__init__c                s   t ƒ j|jddƒƒS )z:Allow header lookup using underscores in place of hyphens.Ú_ú-)r�   Ú__getitem__Úreplace)r-   rY   )r0   r   r   r¢   ‚  s    zHttpHeaders.__getitem__c             C   s>   |j | jƒr |t| jƒd … }n|| jkr.d S |jddƒjƒ S )Nr    r¡   )rb   ÚHTTP_PREFIXÚlenÚUNPREFIXED_HEADERSr£   Útitle)Úclsrk   r   r   r   rœ   †  s
    
zHttpHeaders.parse_header_name)
r   r   r   r¤   r¦   r.   r¢   Úclassmethodrœ   Ú__classcell__r   r   )r0   r   r2   u  s
   r2   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 ƒ jƒ  |ptj| _|pd}dtj| jdœ}t|tƒrhy|j| jƒ}W n t	k
rf   |jdƒ}Y nX x$t
|f|ŽD ]\}}| j||ƒ qvW || _d S )Nr"   T)Úkeep_blank_valuesZfields_limitr<   z
iso-8859-1)r�   r.   r   ÚDEFAULT_CHARSETr<   ZDATA_UPLOAD_MAX_NUMBER_FIELDSÚ
isinstanceÚbytesÚdecodeÚUnicodeDecodeErrorr   Ú
appendlistÚ_mutable)r-   Zquery_stringr!   r<   Zparse_qsl_kwargsrY   r[   )r0   r   r   r.   £  s    


zQueryDict.__init__r"   c             C   s6   | dd|d�}x|D ]}|j ||ƒ 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²   )r¨   Úiterabler[   r!   r<   ÚqrY   r   r   r   Úfromkeys·  s    
zQueryDict.fromkeysc             C   s   | j d krtj| _ | j S )N)rn   r   r¬   )r-   r   r   r   r<   Ä  s    
zQueryDict.encodingc             C   s
   || _ d S )N)rn   )r-   r[   r   r   r   r<   Ê  s    c             C   s   | j stdƒ‚d S )Nz$This QueryDict instance is immutable)r²   ry   )r-   r   r   r   Ú_assert_mutableÎ  s    zQueryDict._assert_mutablec                s2   | j ƒ  t|| jƒ}t|| jƒ}tƒ j||ƒ d S )N)r¶   Úbytes_to_textr<   r�   Ú__setitem__)r-   rY   r[   )r0   r   r   r¸   Ò  s    zQueryDict.__setitem__c                s   | j ƒ  tƒ j|ƒ d S )N)r¶   r�   Ú__delitem__)r-   rY   )r0   r   r   r¹   Ø  s    zQueryDict.__delitem__c             C   s8   | j dd| jd�}x | jƒ D ]\}}|j||ƒ qW |S )Nr"   T)r!   r<   )r0   r<   r�   Úsetlist)r-   ÚresultrY   r[   r   r   r   Ú__copy__Ü  s    zQueryDict.__copy__c             C   sT   | j dd| jd�}||t| ƒ< x0| jƒ D ]$\}}|jtj||ƒtj||ƒƒ q(W |S )Nr"   T)r!   r<   )r0   r<   Úidr�   rº   ÚcopyÚdeepcopy)r-   Úmemor»   rY   r[   r   r   r   Ú__deepcopy__â  s
     zQueryDict.__deepcopy__c                s8   ˆ j ƒ  t|ˆ jƒ}‡ fdd„|D ƒ}tƒ j||ƒ d S )Nc                s   g | ]}t |ˆ jƒ‘qS r   )r·   r<   )rr   Úelt)r-   r   r   rt   ì  s    z%QueryDict.setlist.<locals>.<listcomp>)r¶   r·   r<   r�   rº   )r-   rY   Úlist_)r0   )r-   r   rº   é  s    zQueryDict.setlistc                s   | j ƒ  tƒ j||ƒS )N)r¶   r�   Úsetlistdefault)r-   rY   Zdefault_list)r0   r   r   rÄ   ï  s    zQueryDict.setlistdefaultc                s2   | j ƒ  t|| jƒ}t|| jƒ}tƒ j||ƒ d S )N)r¶   r·   r<   r�   r±   )r-   rY   r[   )r0   r   r   r±   ó  s    zQueryDict.appendlistc                s   | j ƒ  tƒ j|f|žŽ S )N)r¶   r�   Úpop)r-   rY   r„   )r0   r   r   rÅ   ù  s    zQueryDict.popc                s   | j ƒ  tƒ jƒ S )N)r¶   r�   Úpopitem)r-   )r0   r   r   rÆ   ý  s    zQueryDict.popitemc                s   | j ƒ  tƒ jƒ  d S )N)r¶   r�   Úclear)r-   )r0   r   r   rÇ     s    zQueryDict.clearc                s.   | j ƒ  t|| jƒ}t|| jƒ}tƒ j||ƒS )N)r¶   r·   r<   r�   Ú
setdefault)r-   rY   rZ   )r0   r   r   rÈ     s    zQueryDict.setdefaultc             C   s
   | j i ƒS )z%Return a mutable copy of this object.)rÁ   )r-   r   r   r   r¾     s    zQueryDict.copyc                sf   g }ˆr"ˆj ˆjƒ‰‡fdd„‰ ndd„ ‰ x0ˆjƒ D ]$\‰}|j‡ ‡‡fdd„|D ƒƒ q4W dj|ƒ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,   | ]$}ˆ ˆj ˆjƒt|ƒj ˆjƒƒV  qd S )N)rÌ   r<   rM   )rr   rÊ   )rÌ   rÉ   r-   r   r   r�   '  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²   rn   r.   r©   rµ   r™   r<   rš   r¶   r¸   r¹   r¼   rÁ   rº   rÄ   r±   rÅ   rÆ   rÇ   rÈ   r¾   r   rª   r   r   )r0   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.
    r£   N)r­   r®   rM   )Úsr<   r   r   r   r·   0  s    
r·   c             C   sx   | j ƒ } tj| ƒsd	S | d
 dkr*| dfS | jddƒ}t|ƒdkrF|n
|d df\}}|jdƒrl|dd… n|}||fS )z”
    Return a (domain, port) tuple from a given host.

    Returned domain is lowercased. If the host is invalid, the domain will be
    empty.
    r"   r‹   ú]ú:r`   r   Ú.N)r"   r"   éÿÿÿÿrÕ   )ÚlowerÚhost_validation_reÚmatchÚrsplitr¥   rT   )rB   re   rI   rJ   r   r   r   rF   >  s    
 rF   c                s   t ‡ fdd„|D ƒƒS )a4  
    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 lowercased and has
    already had the port, if any, stripped off.

    Return ``True`` for a valid host, ``False`` otherwise.
    c             3   s    | ]}|d kpt ˆ |ƒV  qdS )Ú*N)r   )rr   Úpattern)rB   r   r   r�   c  s    z validate_host.<locals>.<genexpr>)Úany)rB   rH   r   )rB   r   rG   T  s    rG   )4r6   r9   r¾   ÚreÚ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   r   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   Zdjango.utils.httpr   r   ÚobjectrX   Úcompiler×   rƒ   r   Ú	Exceptionr   r    r2   r#   r·   rF   rG   r   r   r   r   Ú<module>   s8   
	  O "