ó
ú2ec           @   sr   d  Z  d d l Z d d l Z d d l Z d d l m Z d „  Z d e j f d „  ƒ  YZ d d d d „ Z
 d S(   sv  
SCGI-->WSGI application proxy, "SWAP".

(Originally written by Titus Brown.)

This lets an SCGI front-end like mod_scgi be used to execute WSGI
application objects.  To use it, subclass the SWAP class like so::

   class TestAppHandler(swap.SWAP):
       def __init__(self, *args, **kwargs):
           self.prefix = '/canal'
           self.app_obj = TestAppClass
           swap.SWAP.__init__(self, *args, **kwargs)

where 'TestAppClass' is the application object from WSGI and '/canal'
is the prefix for what is served by the SCGI Web-server-side process.

Then execute the SCGI handler "as usual" by doing something like this::

   scgi_server.SCGIServer(TestAppHandler, port=4000).serve()

and point mod_scgi (or whatever your SCGI front end is) at port 4000.

Kudos to the WSGI folk for writing a nice PEP & the Quixote folk for
writing a nice extensible SCGI server for Python!
iÿÿÿÿN(   t   scgi_serverc         C   s?   t  j d t  j t  j  ƒ  ƒ ƒ } t j j d | |  f ƒ d  S(   Ns   %Y-%m-%d %H:%M:%Ss   [%s] %s
(   t   timet   strftimet	   localtimet   syst   stderrt   write(   t   msgt	   timestamp(    (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyt   debug!   s    	t   SWAPc           B   s,   e  Z d  Z d Z d Z d „  Z d „  Z RS(   s`   
    SCGI->WSGI application proxy: let an SCGI server execute WSGI
    application objects.
    c         O   sT   |  j  s t d ƒ ‚ |  j d  k	 s0 t d ƒ ‚ |  f | } t j j | | Ž  d  S(   Ns   must set app_objs   must set prefix(   t   app_objt   AssertionErrort   prefixt   NoneR    t   SCGIHandlert   __init__(   t   selft   argst   kwargs(    (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyR   .   s    c            sC  | j  d ƒ } | j  d ƒ } |  j | ƒ } | | d <t j | d <d" | d <t | d <t | d	 <t | d
 <| j d d ƒ d# k r‘ d | d <n
 d | d <|  j } | d t | ƒ j	 d d ƒ d } | | d <| | d <g  ‰ g  ‰ g  ‰  ‡  f d †  ‰ d ‡ ‡ ‡ f d † } |  j | | ƒ } z² x | D] }	 ˆ  j |	 ƒ q1Wˆ sid }
 d$ g } d g ‰  n ˆ \ }
 } ˆ (| j d |
 ƒ x | D] } | j d | ƒ q’W| j d ƒ x ˆ  D] }	 | j |	 ƒ qÁWWd t | d  ƒ rø| j ƒ  n  Xy" | j ƒ  | j ƒ  | j ƒ  Wn! t k
 r>} t d! | ƒ n Xd S(%   s2   
        Handle an individual connection.
        t   rt   ws
   wsgi.inputs   wsgi.errorsi   i    s   wsgi.versions   wsgi.multithreads   wsgi.multiprocesss   wsgi.run_oncet   HTTPSt   offt   ont   1t   httpss   wsgi.url_schemet   httpt   REQUEST_URIt   ?t   SCRIPT_NAMEt	   PATH_INFOc            s   ˆ  j  |  ƒ d  S(   N(   t   append(   t   data(   t   chunks(    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyR   V   s    c            se   | r? z, ˆ  r1 t  j | d | d | d ƒ n  Wd  d  } Xn ˆ rT t d ƒ ‚ n  |  | g ˆ (ˆ S(   Ni    i   i   s   Headers already set!(   t   sixt   reraiseR   R   (   t   statust   response_headerst   exc_info(   t   headers_sentt   headers_setR   (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyt   start_responseY   s    &
s   500 Server Errors   Content-types	   text/htmls   XXX start_response never calleds   Status: %s
s   %s: %s
s   
Nt   closes,   IOError while closing connection ignored: %s(   i   i    (   R   R   (   s   Content-types	   text/html(   t   makefilet   read_envR   R   t   Falset   Truet   getR   t   lent   splitR   R   R    R   t   hasattrR+   t   IOErrorR	   (   R   t   connt   inputt   outputt   environR   t   pathR*   t   resultR!   R%   R&   t   headert   err(    (   R"   R(   R)   R   s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyt   handle_connection4   sX    





	$

	

N(   t   __name__t
   __module__t   __doc__R   R   R   R   R=   (    (    (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyR
   &   s
   	c            s}   d t  f ‡  ‡ f d †  ƒ  Y} t d | ƒ } x5 d D]- } t ƒ  | d k	 r5 t ƒ  | | | <q5 q5 Wt j |   j ƒ  d S(	   sV  
    Serve the specified WSGI application via SCGI proxy.

    ``application``
        The WSGI application to serve.

    ``prefix``
        The prefix for what is served by the SCGI Web-server-side process.

    ``port``
        Optional port to bind the SCGI proxy to. Defaults to SCGIServer's
        default port value.

    ``host``
        Optional host to bind the SCGI proxy to. Defaults to SCGIServer's
        default host value.

    ``host``
        Optional maximum number of child processes the SCGIServer will
        spawn. Defaults to SCGIServer's default max_children value.
    t   SCGIAppHandlerc              s   e  Z ‡  ‡ f d  †  Z RS(   c            s)   ˆ |  _  ˆ  |  _ t j |  | | Ž d  S(   N(   R   R   R
   R   (   R   R   R   (   t   applicationR   (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyR   ¢   s    		(   R>   R?   R   (    (   RB   R   (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyRA   ¡   s   t   handler_classt   hostt   portt   max_childrenN(   RD   RE   RF   (   R
   t   dictt   localsR   R    t
   SCGIServert   serve(   RB   R   RE   RD   RF   RA   R   t   kwarg(    (   RB   R   s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyt   serve_application‹   s    (   R@   R#   R   R   t   scgiR    R	   R   R
   R   RL   (    (    (    s?   /usr/local/lib/python2.7/dist-packages/paste/util/scgiserver.pyt   <module>   s   	e