
 XX#                 @   s  d  d l  m Z d  d l Z 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 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 d  d l m Z e j d  Z e d  e d  Z Z e j d  Z  Gd d   d e!  Z" Gd d   d e
 j#  Z$ Gd d   d e j%  Z& d d   Z' d d   Z( d d   Z) d d   Z* d S)    )unicode_literalsN)BytesIO)http)settings)signals)base)set_script_prefix)six)	force_str
force_text)cached_propertyzdjango.requestz
iso-8859-1zutf-8s   /+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)LimitedStreamzv
    LimitedStream wraps another stream in order to not allow reading from it
    past specified amount of bytes.
    @   i   c             C   s(   | |  _  | |  _ d |  _ | |  _ d  S)N    )stream	remainingbufferbuf_size)selfr   limitr    r   G/home/ubuntu/projects/ifolica/build/django/django/core/handlers/wsgi.py__init__    s    			zLimitedStream.__init__Nc             C   sb   | d  k s | |  j  k r' |  j  } n  | d k r7 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   } | sn Pn  |  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	 d d   Z
 e e e  Z e e
  Z d S)WSGIRequestc             C   s  t  |  } t |  } | s' d } n  | |  _ | |  _ d | j d  | j d d d  f |  _ | |  _ | |  j d <| |  j d <| d j   |  _	 t
 j | j d d   \ |  _ |  _ d	 |  j k ry t j |  j d	  Wn t k
 r YqX|  j d	 |  _ n  d
 |  _ y t | j d   } Wn t t f k
 rRd } Yn Xt |  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   Zcontent_lengthr   r   r   r   P   s6    				'		zWSGIRequest.__init__c             C   s   |  j  j d  S)Nzwsgi.url_scheme)r2   r;   )r   r   r   r   _get_schemeu   s    zWSGIRequest._get_schemec             C   s+   t  |  j d d  } t j | d |  j S)NQUERY_STRINGr(   r@   )get_bytes_from_wsgir2   r   Z	QueryDict	_encoding)r   Zraw_query_stringr   r   r   GETx   s    zWSGIRequest.GETc             C   s#   t  |  d  s |  j   n  |  j S)N_post)hasattr_load_post_and_filesrK   )r   r   r   r   	_get_post~   s    zWSGIRequest._get_postc             C   s   | |  _  d  S)N)rK   )r   postr   r   r   	_set_post   s    zWSGIRequest._set_postc             C   s"   t  |  j d d  } t j |  S)NZHTTP_COOKIEr(   )get_str_from_wsgir2   r   Zparse_cookie)r   Z
raw_cookier   r   r   COOKIES   s    zWSGIRequest.COOKIESc             C   s#   t  |  d  s |  j   n  |  j S)N_files)rL   rM   rS   )r   r   r   r   
_get_files   s    zWSGIRequest._get_filesN)r"   r#   r$   r   rF   r   rJ   rN   rP   rR   rT   propertyPOSTZFILESr   r   r   r   r&   O   s   %r&   c                   s4   e  Z d  Z e Z   f d d   Z d d   Z   S)WSGIHandlerc                s'   t  t |   j | |   |  j   d  S)N)superrW   r   Zload_middleware)r   argskwargs)	__class__r   r   r      s    zWSGIHandler.__init__c             C   si  t  t |   t j j d |  j d |  y |  j |  } WnD t k
 r t j	 d d t
 j   d i d d 6t j   } Yn X|  j |  } |  j | _ d | j | j f } d	 d
   | j   D } xB | j j   D]1 } | j t d  t | j d d   f  q W| t |  |  t | d d   d  k	 re| j d  re| d | j  } n  | S)NZsenderr2   z Bad Request (UnicodeDecodeError)exc_infoextrai  status_codez%d %sc             S   s.   g  |  ]$ \ } } t  |  t  |  f  q Sr   )str).0kvr   r   r   
<listcomp>   s   	 z(WSGIHandler.__call__.<locals>.<listcomp>z
Set-Cookieheaderr(   file_to_streamzwsgi.file_wrapper)r   r0   r   Zrequest_startedsendr[   request_classUnicodeDecodeErrorloggerwarningsysr\   r   ZHttpResponseBadRequestZget_responseZ_handler_classr^   Zreason_phraseitemscookiesvaluesappendr_   outputr
   getattrr;   re   )r   r2   Zstart_responserequestresponsestatusZresponse_headerscr   r   r   __call__   s*    /'zWSGIHandler.__call__)r"   r#   r$   r&   rg   r   rv   r   r   )r[   r   rW      s   rW   c             C   s   t  |  d d  } | j t  S)zC
    Returns the HTTP request's PATH_INFO as a unicode string.
    r*   r'   )rH   decodeUTF_8)r2   r3   r   r   r   r1      s    r1   c             C   s   t  j d k	 r t t  j  St |  d d  } | sI t |  d d  } n  | r d | k rp t j d |  } n  t |  d d  } | r | d t |   n | } n t |  d d  } | j t  S)	aM  
    Returns the equivalent of the HTTP request's SCRIPT_NAME environment
    variable. If Apache mod_rewrite has been used, returns 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   rH   _slashes_resubr   rw   rx   )r2   Z
script_urlr3   rE   r   r   r   r0      s    &r0   c             C   s8   |  j  t |  t |   } t j r4 | j t  S| S)z
    Get a value from the WSGI environ dictionary as bytes.

    key and default should be str objects. Under Python 2 they may also be
    unicode objects provided they only contain ASCII characters.
    )r;   r_   r	   PY3encode
ISO_8859_1)r2   keydefaultvaluer   r   r   rH      s    rH   c             C   s2   t  |  | |  } t j r. | j t d d S| S)z
    Get a value from the WSGI environ dictionary as str.

    key and default should be str objects. Under Python 2 they may also be
    unicode objects provided they only contain ASCII characters.
    errorsr5   )rH   r	   r|   rw   rx   )r2   r   r   r   r   r   r   rQ      s    rQ   )+
__future__r   r9   r=   loggingrerk   ior   Zdjangor   Zdjango.confr   Zdjango.corer   Zdjango.core.handlersr   Zdjango.urlsr   Zdjango.utilsr	   Zdjango.utils.encodingr
   r   Zdjango.utils.functionalr   	getLoggerri   r_   r~   rx   compilerz   objectr   ZHttpRequestr&   BaseHandlerrW   r1   r0   rH   rQ   r   r   r   r   <module>   s0   4E$	!