
v^b                 @   s  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  j d
  Z Gd d   d  Z Gd d   d e
  Z Gd 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               @   sR   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.@   i   c             C   s(   | |  _  | |  _ d |  _ | |  _ d  S)N    )stream	remainingbufferbuf_size)selfr   limitr    r   ;/tmp/pip-build-8lau8j11/django/django/core/handlers/wsgi.py__init__   s    			zLimitedStream.__init__Nc             C   s_   | d  k s | |  j  k r$ |  j  } | d k r4 d S|  j j |  } |  j  t |  8_  | S)Nr   r   )r   r   readlen)r   sizeresultr   r   r   _read_limited   s    	zLimitedStream._read_limitedc             C   s   | d  k r+ |  j  |  j   } d |  _  nm | t |  j   k  rl |  j  d  |  } |  j  | d   |  _  n, |  j  |  j | t |  j    } d |  _  | S)Nr   )r   r   r   )r   r   r   r   r   r   r       s    #	zLimitedStream.readc             C   s   x{ d |  j  k r} | d  k s3 t |  j   | k  r} | rX |  j | t |  j    } n |  j   } | sk P|  j  | 7_  q Wt |  j   } | r | j |  } n | j   } | j   |  _  | S)Ns   
)r   r   r   r   readliner   )r   r   chunksioliner   r   r   r   ,   s    !zLimitedStream.readlinei   i   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s
   	r   c               @   s   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 | j d  | j d d d  f |  _ | |  _ | |  j d <| |  j d <| d j   |  _	 |  j
 |  y t | j d   } Wn t t f k
 r d	 } Yn Xt |  j d
 |  |  _ d |  _ d  |  _ d  S)N/z%s/%s    	PATH_INFOSCRIPT_NAMEREQUEST_METHOD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_namer0   Zcontent_lengthr   r   r   r   A   s$    				zWSGIRequest.__init__c             C   s   |  j  j d  S)Nzwsgi.url_scheme)r/   r7   )r   r   r   r   _get_scheme[   s    zWSGIRequest._get_schemec             C   s(   t  |  j d d  } t | d |  j S)NQUERY_STRINGr'   encoding)get_bytes_from_wsgir/   r   	_encoding)r   Zraw_query_stringr   r   r   GET^   s    zWSGIRequest.GETc             C   s    t  |  d  s |  j   |  j S)N_post)hasattr_load_post_and_filesrB   )r   r   r   r   	_get_postd   s    
zWSGIRequest._get_postc             C   s   | |  _  d  S)N)rB   )r   postr   r   r   	_set_posti   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   COOKIESl   s    zWSGIRequest.COOKIESc             C   s    t  |  d  s |  j   |  j S)N_files)rC   rD   rJ   )r   r   r   r   FILESq   s    
zWSGIRequest.FILESN)r!   r"   r#   r   r<   r   rA   rE   rG   rI   propertyrK   POSTr   r   r   r   r%   @   s   r%   c                   s4   e  Z d  Z e Z   f d d   Z d d   Z   S)WSGIHandlerc                s!   t    j | |   |  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 d |  j d |  |  j |  } |  j |  } |  j | _ d | j	 | j
 f } | j   d d   | j j   D  } | | |  t | d d   d  k	 r | j d  r | j | j _ | d | j | j  } | S)NZsenderr/   z%d %sc             s   s'   |  ] } d  | j  d d  f Vq d S)z
Set-Cookieheaderr'   N)output).0cr   r   r   	<genexpr>   s    z'WSGIHandler.__call__.<locals>.<genexpr>file_to_streamzwsgi.file_wrapper)r	   r-   r   Zrequest_startedsendrR   request_classZget_responseZ_handler_classstatus_codeZreason_phraseitemscookiesvaluesgetattrr7   closerX   
block_size)r   r/   Zstart_responserequestresponsestatusresponse_headersr   r   r   __call__   s    	'zWSGIHandler.__call__)r!   r"   r#   r%   rZ   r   rf   r   r   )rR   r   rN   z   s   rN   c             C   s"   t  |  d d  } t |  j   S)z0Return the HTTP request's PATH_INFO as a string.r)   r&   )r?   r
   decode)r/   r0   r   r   r   r.      s    r.   c             C   s   t  j d k	 r t  j St |  d d  p7 t |  d d  } | r d | k r^ t j d |  } t |  d d  } | r | d t |   n | } n t |  d d  } | j   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_NAMEr?   _slashes_resubr   rg   )r/   Z
script_urlr0   r;   r   r   r   r-      s    $&r-   c             C   s   |  j  | |  } | j d  S)zh
    Get a value from the WSGI environ dictionary as bytes.

    key and default should be strings.
    z
iso-8859-1)r7   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.
    errorsr2   )r?   rg   )r/   rl   rm   rn   r   r   r   rH      s    rH   )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   compileri   r   r%   BaseHandlerrN   r.   r-   r?   rH   r   r   r   r   <module>   s    1: