ó
è¾bc           @   sG  d  Z  d d l m Z d d l m Z m Z m Z d d l m Z d d l	 Z	 d d l
 Z
 d d l Z d d d d	 g Z y e Wn e k
 rœ d
 „  Z n Xd d d d d d d g Z e d d d d d d d d d d d d g Z d „  Z d f  d „  ƒ  YZ d e f d  „  ƒ  YZ d e f d! „  ƒ  YZ d	 e f d" „  ƒ  YZ d S(#   s/   Base classes for server/gateway implementationsiÿÿÿÿ(   t
   StringType(   t   FileWrappert   guess_schemet   is_hop_by_hop(   t   HeadersNt   BaseHandlert   SimpleHandlert   BaseCGIHandlert
   CGIHandlerc         C   s+   i  } x |  D] \ } } | | | <q W| S(   N(    (   t   itemst   dt   kt   v(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   dict   s    t   Mont   Tuet   Wedt   Thut   Frit   Satt   Sunt   Jant   Febt   Mart   Aprt   Mayt   Junt   Jult   Augt   Sept   Octt   Novt   Decc   
   	   C   sO   t  j |  ƒ \	 } } } } } } } } }	 d t | | t | | | | | f S(   Ns#   %s, %02d %3s %4d %02d:%02d:%02d GMT(   t   timet   gmtimet   _weekdaynamet
   _monthname(
   t	   timestampt   yeart   montht   dayt   hht   mmt   sst   wdt   yt   z(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   format_date_time#   s    *c           B   sb  e  Z d  Z d Z e Z e Z e Z e Z	 d Z
 d  Z e e j j ƒ  ƒ Z e Z e Z d  Z d Z d! g Z d Z d  Z Z e Z d  Z d Z d „  Z d	 „  Z  d
 „  Z! d „  Z" d „  Z# d „  Z$ d  d „ Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d „  Z0 d „  Z1 d „  Z2 d „  Z3 d „  Z4 d „  Z5 RS("   s+   Manage the invocation of a WSGI applicationi   i    s   1.0s   500 Internal Server Errors   Content-Types
   text/plains;   A server error occurred.  Please contact the administrator.c         C   sc   y0 |  j  ƒ  | |  j |  j ƒ |  _ |  j ƒ  Wn, y |  j ƒ  Wq_ |  j ƒ  ‚  q_ Xn Xd S(   s   Invoke the applicationN(   t   setup_environt   environt   start_responset   resultt   finish_responset   handle_errort   close(   t   selft   application(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   runL   s    

c         C   sÏ   |  j  j ƒ  } |  _ |  j ƒ  |  j ƒ  | d <|  j ƒ  | d <|  j | d <|  j | d <|  j ƒ  | d <|  j	 | d <|  j
 | d <|  j d
 k	 r£ |  j | d <n  |  j rË |  j rË | j d	 |  j ƒ n  d
 S(   s&   Set up the environment for one requests
   wsgi.inputs   wsgi.errorss   wsgi.versions   wsgi.run_onces   wsgi.url_schemes   wsgi.multithreads   wsgi.multiprocesss   wsgi.file_wrappert   SERVER_SOFTWAREN(   t
   os_environt   copyR1   t   add_cgi_varst	   get_stdint
   get_stderrt   wsgi_versiont   wsgi_run_oncet
   get_schemet   wsgi_multithreadt   wsgi_multiprocesst   wsgi_file_wrappert   Nonet   origin_servert   server_softwaret
   setdefault(   R7   t   env(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR0   `   s    
c         C   s^   zL |  j  ƒ  s |  j ƒ  rK x |  j D] } |  j | ƒ q' W|  j ƒ  n  Wd |  j ƒ  Xd S(   s>  Send any iterable data, then close self and the iterable

        Subclasses intended for use in asynchronous servers will
        want to redefine this method, such that it sets up callbacks
        in the event loop to iterate over the data, and to call
        'self.close()' once the response is finished.
        N(   t   result_is_filet   sendfileR3   t   writet   finish_contentR6   (   R7   t   data(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR4   u   s    c         C   s   t  |  j ƒ S(   s    Return the URL scheme being used(   R   R1   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRB   †   s    c         C   sZ   y t  |  j ƒ } Wn t t t f k
 r/ n' X| d k rV t |  j ƒ |  j d <d Sd S(   s@   Compute Content-Length or switch to chunked encoding if possiblei   s   Content-LengthN(   t   lenR3   t	   TypeErrort   AttributeErrort   NotImplementedErrort   strt
   bytes_sentt   headers(   R7   t   blocks(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   set_content_length‹   s    c         C   s    d |  j  k r |  j ƒ  n  d S(   sq   Make any necessary header changes or defaults

        Subclasses can extend this to add other defaults.
        s   Content-LengthN(   RV   RX   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   cleanup_headers˜   s    c         C   s[  | r; z( |  j  r- | d | d | d ‚ n  Wd d } Xn |  j d k	 rY t d ƒ ‚ n  t | ƒ t k sw t d ƒ ‚ t | ƒ d k s• t d ƒ ‚ t | d	  ƒ s± t d
 ƒ ‚ | d	 d k sÍ t d ƒ ‚ xi | D]a \ } } t | ƒ t k sþ t d ƒ ‚ t | ƒ t k st d ƒ ‚ t | ƒ sÔ t d ƒ ‚ qÔ W| |  _	 |  j
 | ƒ |  _ |  j S(   s3   'start_response()' callable as specified by PEP 333i    i   i   Ns   Headers already set!s   Status must be a stringi   s$   Status must be at least 4 charactersi   s(   Status message must begin w/3-digit codet    s+   Status message must have a space after codes   Header names must be stringss   Header values must be stringss   Hop-by-hop headers not allowed(   t   headers_sentRF   RV   t   AssertionErrort   typeR    RP   t   intR   t   statust   headers_classRM   (   R7   R_   RV   t   exc_infot   namet   val(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR2       s$    	
	c         C   s®   |  j  r– |  j ƒ  rª |  j d |  j |  j f ƒ d |  j k ra |  j d t t j ƒ  ƒ ƒ n  |  j r“ d |  j k r“ |  j d |  j ƒ q“ qª n |  j d |  j ƒ d S(   s6   Transmit version/status/date/server, via self._write()s   HTTP/%s %s
t   Dates
   Date: %s
t   Servers   Server: %s
s   Status: %s
N(	   RG   t   client_is_modernt   _writet   http_versionR_   RV   R/   R!   RH   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   send_preamble»   s    	c         C   s‹   t  | ƒ t k s t d ƒ ‚ |  j s6 t d ƒ ‚ n: |  j s[ t | ƒ |  _ |  j ƒ  n |  j t | ƒ 7_ |  j | ƒ |  j	 ƒ  d S(   s*   'write()' callable as specified by PEP 333s   write() argument must be strings   write() before start_response()N(
   R]   R    R\   R_   R[   RP   RU   t   send_headersRg   t   _flush(   R7   RO   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRM   É   s    		c         C   s   t  S(   s  Platform-specific file transmission

        Override this method in subclasses to support platform-specific
        file transmission.  It is only called if the application's
        return iterable ('self.result') is an instance of
        'self.wsgi_file_wrapper'.

        This method should return a true value if it was able to actually
        transmit the wrapped file-like object using a platform-specific
        approach.  It should return a false value if normal iteration
        should be used instead.  An exception can be raised to indicate
        that transmission was attempted, but failed.

        NOTE: this method should call 'self.send_headers()' if
        'self.headers_sent' is false and it is going to attempt direct
        transmission of the file.
        (   t   False(   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRL   Ý   s    c         C   s-   |  j  s) |  j j d d ƒ |  j ƒ  n  d S(   s.   Ensure headers and content have both been sents   Content-Lengtht   0N(   R[   RV   RI   Rj   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRN   ò   s    	c         C   s^   z& t  |  j d ƒ r% |  j j ƒ  n  Wd d |  _ |  _ |  _ |  _ d |  _ t |  _	 Xd S(   s„   Close the iterable (if needed) and reset all instance vars

        Subclasses may want to also drop the client connection.
        R6   Ni    (
   t   hasattrR3   R6   RF   RV   R_   R1   RU   Rl   R[   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR6   ü   s    	 c         C   sP   |  j  ƒ  t |  _ |  j s) |  j ƒ  rL |  j ƒ  |  j t |  j ƒ ƒ n  d S(   s1   Transmit headers to the client, via self._write()N(	   RY   t   TrueR[   RG   Rf   Ri   Rg   RT   RV   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRj   	  s
    
	
c         C   s%   |  j  } | d k	 o$ t |  j | ƒ S(   s@   True if 'self.result' is an instance of 'self.wsgi_file_wrapper'N(   RE   RF   t
   isinstanceR3   (   R7   t   wrapper(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRK     s    	c         C   s   |  j  d j ƒ  d k S(   s,   True if client can accept status and headerst   SERVER_PROTOCOLs   HTTP/0.9(   R1   t   upper(   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRf     s    c         C   s]   zO d d l  m } |  j ƒ  } | | d | d | d |  j | ƒ | j ƒ  Wd d } Xd S(   s   Log the 'exc_info' tuple in the server log

        Subclasses may override to retarget the output or change its format.
        iÿÿÿÿ(   t   print_exceptioni    i   i   N(   t	   tracebackRt   R?   t   traceback_limitt   flushRF   (   R7   Ra   Rt   t   stderr(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   log_exception  s    c         C   sH   |  j  t j ƒ  ƒ |  j sD |  j |  j |  j ƒ |  _ |  j ƒ  n  d S(   s>   Log current error, and send error output to client if possibleN(	   Ry   t   sysRa   R[   t   error_outputR1   R2   R3   R4   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR5   -  s    	c         C   s'   | |  j  |  j t j ƒ  ƒ |  j g S(   sZ  WSGI mini-app to create error output

        By default, this just uses the 'error_status', 'error_headers',
        and 'error_body' attributes to generate an output page.  It can
        be overridden in a subclass to dynamically generate diagnostics,
        choose an appropriate message for the user's preferred language, etc.

        Note, however, that it's not recommended from a security perspective to
        spit out diagnostics to any old user; ideally, you should have to do
        something special to enable diagnostic output, which is why we don't
        include any here!
        (   t   error_statust   error_headersRz   Ra   t
   error_body(   R7   R1   R2   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR{   5  s    c         C   s
   t  ‚ d S(   s  Override in subclass to buffer data for send to client

        It's okay if this method actually transmits the data; BaseHandler
        just separates write and flush operations for greater efficiency
        when the underlying system actually has such a distinction.
        N(   RS   (   R7   RO   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRg   H  s    c         C   s
   t  ‚ d S(   s¬   Override in subclass to force sending of recent '_write()' calls

        It's okay if this method is a no-op (i.e., if '_write()' actually
        sends the data.
        N(   RS   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRk   Q  s    c         C   s
   t  ‚ d S(   s4   Override in subclass to return suitable 'wsgi.input'N(   RS   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR>   Y  s    c         C   s
   t  ‚ d S(   s5   Override in subclass to return suitable 'wsgi.errors'N(   RS   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR?   ]  s    c         C   s
   t  ‚ d S(   s>   Override in subclass to insert CGI variables in 'self.environ'N(   RS   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR=   a  s    (   i   i    N(   s   Content-Types
   text/plain(6   t   __name__t
   __module__t   __doc__R@   Ro   RC   RD   Rl   RA   RG   Rh   RF   RH   R   t   osR1   R	   R;   R   RE   R   R`   Rv   R|   R}   R~   R_   R3   R[   RV   RU   R9   R0   R4   RB   RX   RY   R2   Ri   RM   RL   RN   R6   Rj   RK   Rf   Ry   R5   R{   Rg   Rk   R>   R?   R=   (    (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR   *   sT   	
										
													c           B   sJ   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sq  Handler that's just initialized with streams, environment, etc.

    This handler subclass is intended for synchronous HTTP/1.0 origin servers,
    and handles sending the entire response output, given the correct inputs.

    Usage::

        handler = SimpleHandler(
            inp,out,err,env, multithread=False, multiprocess=True
        )
        handler.run(app)c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   stdint   stdoutRx   t   base_envRC   RD   (   R7   Rƒ   R„   Rx   R1   t   multithreadt   multiprocess(    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   __init__s  s    					c         C   s   |  j  S(   N(   Rƒ   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR>   }  s    c         C   s   |  j  S(   N(   Rx   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR?   €  s    c         C   s   |  j  j |  j ƒ d  S(   N(   R1   t   updateR…   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR=   ƒ  s    c         C   s#   |  j  j | ƒ |  j  j |  _ d  S(   N(   R„   RM   Rg   (   R7   RO   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRg   †  s    c         C   s    |  j  j ƒ  |  j  j |  _ d  S(   N(   R„   Rw   Rk   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRk   Š  s    (   R   R€   R   Ro   Rl   Rˆ   R>   R?   R=   Rg   Rk   (    (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR   f  s   					c           B   s   e  Z d  Z e Z RS(   sÚ  CGI-like systems using input/output/error streams and environ mapping

    Usage::

        handler = BaseCGIHandler(inp,out,err,env)
        handler.run(app)

    This handler class is useful for gateway protocols like ReadyExec and
    FastCGI, that have usable input/output/error streams and an environment
    mapping.  It's also the base class for CGIHandler, which just uses
    sys.stdin, os.environ, and so on.

    The constructor also takes keyword arguments 'multithread' and
    'multiprocess' (defaulting to 'True' and 'False' respectively) to control
    the configuration sent to the application.  It sets 'origin_server' to
    False (to enable CGI-like output), and assumes that 'wsgi.run_once' is
    False.
    (   R   R€   R   Rl   RG   (    (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR     s   c           B   s#   e  Z d  Z e Z i  Z d „  Z RS(   sé  CGI-based invocation via sys.stdin/stdout/stderr and os.environ

    Usage::

        CGIHandler().run(app)

    The difference between this class and BaseCGIHandler is that it always
    uses 'wsgi.run_once' of 'True', 'wsgi.multithread' of 'False', and
    'wsgi.multiprocess' of 'True'.  It does not take any initialization
    parameters, but always uses 'sys.stdin', 'os.environ', and friends.

    If you need to override any of these parameters, use BaseCGIHandler
    instead.
    c      
   C   sA   t  j |  t j t j t j t t j j	 ƒ  ƒ d t
 d t ƒd  S(   NR†   R‡   (   R   Rˆ   Rz   Rƒ   R„   Rx   R   R‚   R1   R	   Rl   Ro   (   R7   (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyRˆ   ¾  s    *(   R   R€   R   Ro   RA   R;   Rˆ   (    (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyR   §  s   (   R   t   typesR    t   utilR   R   R   RV   R   Rz   R‚   R!   t   __all__R   t	   NameErrorR#   RF   R$   R/   R   R   R   R   (    (    (    s&   /usr/lib/python2.7/wsgiref/handlers.pyt   <module>   s&   $	ÿ =)