3
j6bHc                 @   s  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m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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& d dl'm(Z( d dl)m*Z* d dl+m,Z, ddl-m.Z. e*e	d s(e*e	d r4d dl'm	Z	 e/ Z0e,dZ1G dd de2Z3G dd de4Z5G dd dZ6G dd deZ7G dd  d eZ8G d!d" d"Z9d#d$ Z:d%d& Z;d'd( Z<d)d* Z=dS )+    N)BytesIO)chain)	parse_qslquote	urlencodeurljoinurlsplit)settings)signing)DisallowedHostImproperlyConfiguredRequestDataTooBigTooManyFieldsSent)uploadhandler)MultiPartParserMultiPartParserError)CaseInsensitiveMappingImmutableListMultiValueDict)RemovedInDjango40Warning)escape_uri_path
iri_to_uri)cached_property)is_same_domain)func_supports_parameter)_lazy_re_compile   )parse_headermax_num_fields	separator)r   z1^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9\.:]+\])(:\d+)?$c               @   s   e Zd ZdS )UnreadablePostErrorN)__name__
__module____qualname__ r$   r$   R/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/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edd Z	ed	d
 Z
dd Zdd Zdd Zdd Zdd ZdGddZdHddZdd ZeddfddZdd  ZdId!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d0d1 Zed2d3 Z e jd4d3 Z d5d6 Z!ed7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&dAdB Z'dCdD Z(dEdF Z)dS )J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>)r2   get_full_path	__class__r!   )r5   r$   r$   r%   __repr__Q   s    zHttpRequest.__repr__c             C   s
   t | jS )N)HttpHeadersr/   )r5   r$   r$   r%   headersV   s    zHttpRequest.headersc             C   s   t | jjddS )z%Return a list of MediaType instances.Acceptz*/*)parse_accept_headerr;   get)r5   r$   r$   r%   accepted_typesZ   s    zHttpRequest.accepted_typesc                s   t  fdd| jD S )Nc             3   s   | ]}|j  V  qd S )N)match).0Zaccepted_type)
media_typer$   r%   	<genexpr>a   s   z&HttpRequest.accepts.<locals>.<genexpr>)anyr?   )r5   rB   r$   )rB   r%   accepts_   s    
zHttpRequest.acceptsc             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)	cgir   r>   r3   r4   codecslookupLookupErrorencoding)r5   metar$   r$   r%   _set_content_type_paramse   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_NAME44380z%s:%s)r	   ZUSE_X_FORWARDED_HOSTr/   get_port	is_secure)r5   hostZserver_portr$   r$   r%   _get_raw_hostp   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.z
.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)rT   r	   ZALLOWED_HOSTSDEBUGsplit_domain_portvalidate_hostr   )r5   rS   allowed_hostsdomainportmsgr$   r$   r%   get_host   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)r5   rZ   r$   r$   r%   rQ      s    
zHttpRequest.get_portFc             C   s   | j | j|S )N)_get_full_pathr0   )r5   force_append_slashr$   r$   r%   r7      s    zHttpRequest.get_full_pathc             C   s   | j | j|S )N)r^   r1   )r5   r_   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/   r>   r   )r5   r0   r_   r$   r$   r%   r^      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)r5   keydefaultre   rf   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})schemerS   r0   )formatrm   rT   r7   )r5   r$   r$   r%   get_raw_uri   s    zHttpRequest.get_raw_uric             C   s   |dkrd| j   }nt|}t|}|jo0|js|jjdr|j r|j rd|jkrd|jkr|jdrx|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//%sra   z/./z/../z//   )
r7   r]   r   rm   netlocr0   
startswith_current_scheme_hostr   r   )r5   locationbitsr$   r$   r%   build_absolute_uri   s    	
zHttpRequest.build_absolute_uric             C   s   dj | j| j S )Nz{}://{})rn   rm   r\   )r5   r$   r$   r%   rs      s    z HttpRequest._current_scheme_hostc             C   s   dS )zf
        Hook for subclasses like WSGIRequest to implement. Return 'http' by
        default.
        httpr$   )r5   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.httpsrw   )r	   ZSECURE_PROXY_SSL_HEADER
ValueErrorr   r/   r>   rx   )r5   headerZsecure_valueheader_valuer$   r$   r%   rm      s    zHttpRequest.schemec             C   s
   | j dkS )Nry   )rm   )r5   r$   r$   r%   rR     s    zHttpRequest.is_securec             C   s    t jdtdd | jjddkS )Nzfrequest.is_ajax() is deprecated. See Django 3.1 release notes for more details about this deprecation.rp   )
stacklevelZHTTP_X_REQUESTED_WITHZXMLHttpRequest)warningswarnr   r/   r>   )r5   r$   r$   r%   is_ajax  s
    zHttpRequest.is_ajaxc             C   s   | j S )N)	_encoding)r5   r$   r$   r%   rL     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)r   hasattrr,   r   )r5   valr$   r$   r%   rL     s
    

c                s    fddt jD  _d S )Nc                s   g | ]}t j| qS r$   )r   Zload_handler)rA   handler)r5   r$   r%   
<listcomp>+  s   z4HttpRequest._initialize_handlers.<locals>.<listcomp>)r	   ZFILE_UPLOAD_HANDLERS_upload_handlers)r5   r$   )r5   r%   _initialize_handlers*  s    
z HttpRequest._initialize_handlersc             C   s   | j s| j  | j S )N)r   r   )r5   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   AttributeErrorr   )r5   r   r$   r$   r%   r   5  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   r   r   rL   parse)r5   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.)r   _read_startedr&   r	   ZDATA_UPLOAD_MAX_MEMORY_SIZEintr/   r>   r   readr   OSErrorr    argsr   _stream)r5   er$   r$   r%   bodyD  s    

zHttpRequest.bodyc             C   s   t  | _t | _d S )N)r+   r   r   r   )r5   r$   r$   r%   _mark_post_parse_errorV  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rt| drdt
| j}n| }y| j| j|\| _| _W q tk
r   | j   Y qX n@| j	dkrt| 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-   )rL   Nr   zmultipart/form-dataz!application/x-www-form-urlencoded)r2   r+   r   r   r   r   r   r   r   r3   r   r   r   r/   r   r   )r5   datar$   r$   r%   _load_post_and_filesZ  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 )Nr   c             s   s   | ]}|d  V  qdS )r   Nr$   )rA   list_r$   r$   r%   rC   y  s    z$HttpRequest.close.<locals>.<genexpr>)r   r   from_iterabler   listsclose)r5   fr$   r$   r%   r   w  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   )r5   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   )r5   r   r   r   r$   r$   r%   r     s
    zHttpRequest.readlinec             C   s   t | jdS )N    )iterr   )r5   r$   r$   r%   __iter__  s    zHttpRequest.__iter__c             C   s   t | S )N)list)r5   r$   r$   r%   	readlines  s    zHttpRequest.readlines)F)F)N)*r!   r"   r#   r'   r   r   r6   r9   r   r;   r?   rE   rN   rT   r\   rQ   r7   r`   r^   rh   rl   ro   rv   rs   rx   propertyrm   rR   r   rL   setterr   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   r(   8   sJ   

	
$		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 )
r:   ZHTTP_rF   r   c                s@   i }x*|j  D ]\}}| j|}|r|||< qW t j| d S )N)itemsparse_header_namesuperr6   )r5   environr;   r{   rk   name)r8   r$   r%   r6     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)r5   ri   )r8   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   )rr   HTTP_PREFIXlenUNPREFIXED_HEADERSr   title)clsr{   r$   r$   r%   r     s
    
zHttpHeaders.parse_header_name)
r!   r"   r#   r   r   r6   r   classmethodr   __classcell__r$   r$   )r8   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  j  |ptj| _|pd}d| jtjd}t|trhy|j| j}W n t	k
rf   |jd}Y nX y*x$t
|f|D ]\}}| j|| qxW W n, tk
r } ztd|W Y d d }~X nX || _d S )Nr*   T)keep_blank_valuesrL   r   z
iso-8859-1zRThe number of GET/POST parameters exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS.)r   r6   r	   DEFAULT_CHARSETrL   ZDATA_UPLOAD_MAX_NUMBER_FIELDS
isinstancebytesdecodeUnicodeDecodeErrorr   
appendlistrz   r   _mutable)r5   Zquery_stringr)   rL   Zparse_qsl_kwargsri   rk   r   )r8   r$   r%   r6     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)   rL   F)r   r   )r   iterablerk   r)   rL   qri   r$   r$   r%   fromkeys  s    
zQueryDict.fromkeysc             C   s   | j d krtj| _ | j S )N)r   r	   r   )r5   r$   r$   r%   rL     s    
zQueryDict.encodingc             C   s
   || _ d S )N)r   )r5   rk   r$   r$   r%   rL     s    c             C   s   | j stdd S )Nz$This QueryDict instance is immutable)r   r   )r5   r$   r$   r%   _assert_mutable  s    zQueryDict._assert_mutablec                s2   | j   t|| j}t|| j}t j|| d S )N)r   bytes_to_textrL   r   __setitem__)r5   ri   rk   )r8   r$   r%   r      s    zQueryDict.__setitem__c                s   | j   t j| d S )N)r   r   __delitem__)r5   ri   )r8   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)   rL   )r8   rL   r   setlist)r5   resultri   rk   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)   rL   )r8   rL   idr   r   copydeepcopy)r5   memor   ri   rk   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   rL   )rA   elt)r5   r$   r%   r     s    z%QueryDict.setlist.<locals>.<listcomp>)r   r   rL   r   r   )r5   ri   r   )r8   )r5   r%   r     s    zQueryDict.setlistc                s   | j   t j||S )N)r   r   setlistdefault)r5   ri   Zdefault_list)r8   r$   r%   r     s    zQueryDict.setlistdefaultc                s2   | j   t|| j}t|| j}t j|| d S )N)r   r   rL   r   r   )r5   ri   rk   )r8   r$   r%   r   !  s    zQueryDict.appendlistc                s   | j   t j|f| S )N)r   r   pop)r5   ri   r   )r8   r$   r%   r   '  s    zQueryDict.popc                s   | j   t j S )N)r   r   popitem)r5   )r8   r$   r%   r   +  s    zQueryDict.popitemc                s   | j   t j  d S )N)r   r   clear)r5   )r8   r$   r%   r   /  s    zQueryDict.clearc                s.   | j   t|| j}t|| j}t j||S )N)r   r   rL   r   
setdefault)r5   ri   rj   )r8   r$   r%   r   3  s    zQueryDict.setdefaultc             C   s
   | j i S )z%Return a mutable copy of this object.)r   )r5   r$   r$   r%   r   9  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%   encodeN  s    z#QueryDict.urlencode.<locals>.encodec             S   s   t | |iS )N)r   )r   r   r$   r$   r%   r   Q  s    c             3   s,   | ]$} j jt|j jV  qd S )N)r   rL   r]   )rA   r   )r   r   r5   r$   r%   rC   U  s   z&QueryDict.urlencode.<locals>.<genexpr>&)r   rL   r   extendjoin)r5   r   outputr   r$   )r   r   r   r5   r%   r   =  s    zQueryDict.urlencode)NFN)r*   FN)N)N)N)r!   r"   r#   r'   r   r   r6   r   r   r   rL   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r$   )r8   r%   r+     s,   r+   c               @   s8   e Zd Zdd Zdd Zdd Zedd Zd	d
 ZdS )	MediaTypec             C   s4   t |r|jdnd\}| _|jd\| _}| _d S )Nasciir   ra   )r   r   params	partition	main_typesub_type)r5   Zmedia_type_raw_lineZ	full_typer   r$   r$   r%   r6   \  s    zMediaType.__init__c             C   s:   dj dd | jj D }d| j| jr0d| j nd|f S )Nr*   c             s   s$   | ]\}}d ||j df V  qdS )z; %s=%sr   N)r   )rA   r   r   r$   r$   r%   rC   d  s   z$MediaType.__str__.<locals>.<genexpr>z%s%s%sz/%s)r   r   r   r   r   )r5   Z
params_strr$   r$   r%   __str__b  s    zMediaType.__str__c             C   s   d| j j| f S )Nz<%s: %s>)r8   r#   )r5   r$   r$   r%   r9   m  s    zMediaType.__repr__c             C   s   | j dko| jdkS )N*)r   r   )r5   r$   r$   r%   is_all_typesp  s    zMediaType.is_all_typesc             C   s6   | j r
dS t|}| j|jkr2| jd|jhkr2dS dS )NTr   F)r   r   r   r   )r5   otherr$   r$   r%   r@   t  s    zMediaType.matchN)	r!   r"   r#   r6   r   r9   r   r   r@   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.
    r   N)r   r   r]   )srL   r$   r$   r%   r     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   ]:rp   r   .N)r*   r*   r   )lowerhost_validation_rer@   rsplitr   rd   )rS   ru   rY   rZ   r$   r$   r%   rV     s    
 rV   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 )r   N)r   )rA   pattern)rS   r$   r%   rC     s    z validate_host.<locals>.<genexpr>)rD   )rS   rX   r$   )rS   r%   rW     s    rW   c             C   s   dd | j dD S )Nc             S   s   g | ]}|j  rt|qS r$   )stripr   )rA   tokenr$   r$   r%   r     s    z'parse_accept_header.<locals>.<listcomp>,)split)r{   r$   r$   r%   r=     s    r=   )>rH   rI   r   r~   ior   	itertoolsr   urllib.parser   r   r   r   r   Zdjango.confr	   Zdjango.corer
   Zdjango.core.exceptionsr   r   r   r   Zdjango.core.filesr   Zdjango.http.multipartparserr   r   Zdjango.utils.datastructuresr   r   r   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   Zdjango.utils.httpr   Zdjango.utils.inspectr   Zdjango.utils.regex_helperr   Zmultipartparserr   objectrh   r   r   r    	Exceptionr&   r(   r:   r+   r   r   rV   rW   r=   r$   r$   r$   r%   <module>   sJ   	  c )%