� a�Nc@s�dZddlZddlZddlmZddlmZmZmZddlm Z ddl m Z d�Z de fd ��YZd e fd ��YZd �Zd dddd gZdd�Zde fd��YZdS(s Middleware to display error documents for certain status codes The middleware in this module can be used to intercept responses with specified status codes and internally forward the request to an appropriate URL where the content can be displayed to the user as an error document. i����N(turlparse(tForwardRequestExceptiontRecursiveMiddlewaret RecursionLoop(t converters(treplace_headercCs[x6|D].}t|t�stdt|���qqWd�}tt||d|��S(s� Intercepts a response with a particular status code and returns the content from a specified URL instead. The arguments are: ``app`` The WSGI application or middleware chain. ``codes`` A dictionary of integer status codes and the URL to be displayed if the response uses that code. For example, you might want to create a static file to display a "File Not Found" message at the URL ``/error404.html`` and then use ``forward`` middleware to catch all 404 status codes and display the page you created. In this example ``app`` is your exisiting WSGI applicaiton:: from paste.errordocument import forward app = forward(app, codes={404:'/error404.html'}) s4All status codes should be type int. %s is not validcSs|j|�r||SdSdS(N(thas_keytNone(tcodetmessagetenviront global_conftcodes((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyterror_codes_mapper0sR (t isinstancetintt TypeErrortreprRtStatusBasedForward(tappR RR ((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pytforwards  t StatusKeepercBseZd�Zd�ZRS(cCs(||_||_||_||_dS(N(Rtstatusturltheaders(tselfRRRR((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyt__init__@s   cs�d��fd�}�jjd�}|d|d<t|�dkrX|d|d<n d|d<y�j||�SWnLtk r�}|djd |�|d dgtj��d �j gSXdS(NcskxR|D]J\}}|j�dkr>�jj||f�qt�j||�qW��j�j|�S(Ns set-cookie(tlowerRtappendRR(RRtexc_infotheadertvalue(Rtstart_response(sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pytkeep_status_start_responseGs t?it PATH_INFOit QUERY_STRINGts wsgi.errorss'Recursion error getting error page: %s s500 Server Errors Content-types text/plains-Error: %s. (Error page could not be fetched)(s Content-types text/plain( RRtsplittlenRRtwritetsysRR(RR R R!tpartste((RR sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyt__call__Fs (t__name__t __module__RR,(((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyR?s RcBs#eZdZdd�Zd�ZRS(s� Middleware that lets you test a response against a custom mapper object to programatically determine whether to internally forward to another URL and if so, which URL to forward to. If you don't need the full power of this middleware you might choose to use the simpler ``forward`` middleware instead. The arguments are: ``app`` The WSGI application or middleware chain. ``mapper`` A callable that takes a status code as the first parameter, a message as the second, and accepts optional environ, global_conf and named argments afterwards. It should return a URL to forward to or ``None`` if the code is not to be intercepted. ``global_conf`` Optional default configuration from your config file. If ``debug`` is set to ``true`` a message will be written to ``wsgi.errors`` on each internal forward stating the URL forwarded to. ``**params`` Optional, any other configuration and extra arguments you wish to pass which will in turn be passed back to the custom mapper object. Here is an example where a ``404 File Not Found`` status response would be redirected to the URL ``/error?code=404&message=File%20Not%20Found``. This could be useful for passing the status code and message into another application to display an error document: .. code-block:: python from paste.errordocument import StatusBasedForward from paste.recursive import RecursiveMiddleware from urllib import urlencode def error_mapper(code, message, environ, global_conf, kw) if code in [404, 500]: params = urlencode({'message':message, 'code':code}) url = '/error?'%(params) return url else: return None app = RecursiveMiddleware( StatusBasedForward(app, mapper=error_mapper), ) cKsm|dkri}n|r<tj|jdt��|_n t|_||_||_||_||_ dS(Ntdebug( RRtasbooltgettFalseR/t applicationtmapperR tparams(RRR4R R5((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyR�s  !    cs�g�g}d����fd�}�j�|�}�r|t|d�r[|j�n�fd�}td|��n|SdS(Ncs�|jd�}yt|d�}Wn1ttfk rVtdt|d���nXdj|d�}�j||��j�j �}|dkp�t |t �s�td|��n|r��j |||g�gj S�|||�SdS(Nt is@StatusBasedForward middleware received an invalid status code %sisiExpected the url to internally redirect to in the StatusBasedForward mapperto be a string or None, not %r(R&Rt ValueErrorRt ExceptionRtjoinR4R R5RRtstrR(RRRt status_codeRR tnew_url(R RR R(sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pytchange_response�s,  tclosec s4t|d�ddd�ddd�dd�S(NRiiRRi(R(R(R(sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pytfactory�s%R?(RR3thasattrR>R(RR R twriterR=tapp_iterR?((R RR RsD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyR,�s N(R-R.t__doc__RRR,(((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyR^s4 cKsti}xX|j�D]J\}}yt|�}Wn!tk rRtd|��nX|||<qWt||�}|S(s� Paste Deploy entry point to create a error document wrapper. Use like:: [filter-app:main] use = egg:Paste#errordocument next = real-app 500 = /lib/msg/500.html 404 = /lib/msg/404.html sBad status code: %r(titemsRR7R(RR tkwtmapRt redir_loct forwarder((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pytmake_errordocument�s  RRIt empty_errortmake_empty_errorcKs;tjdtd�|dkr(i}nt||||�S(s6 Deprectated; use StatusBasedForward instead. s_errordocuments.custom_forward has been deprecated; please use errordocuments.StatusBasedForwardiN(twarningstwarntDeprecationWarningRt_StatusBasedRedirect(RR4R RE((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pytcustom_forward�s    ROcBs#eZdZdd�Zd�ZRS(s6 Deprectated; use StatusBasedForward instead. cKsYtjdtd�|dkr(i}n||_||_||_||_d|_dS(Nseerrordocuments._StatusBasedRedirect has been deprecated; please use errordocuments.StatusBasedForwardis� <html> <head> <title>Error %(code)s</title> </html> <body> <h1>Error %(code)s</h1> <p>%(message)s</p> <hr> <p> Additionally an error occurred trying to produce an error document. A description of the error was logged to <tt>wsgi.errors</tt>. </p> </body> </html> ( RLRMRNRR3R4R REtfallback_template(RRR4R RE((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyRs       csg�g�y4d�����fd�}�j�|�}Wn�y&ddl}t|j�d�}Wn d}nXy�d\}}Wndd}}nX�djdt|���ji|d6|d 6gSX�r�d�i} xG�j�D]9\} } | d kr1t��d | d <q| | | <qWd t fd ��Y�d��fd�} �d} | j }| | _ y| �| �}Wn[�k r�}�d\}}�djdt�d���ji|d6|d 6gSX|| _ |Sn|SdS(Ncs�d}|jd�}yt|d�}Wn1ttfk r\tdt|d���nXdj|d�}�j||��j �j �}|dkp�t |t �s�tdt|���n|r��j |�n�j ||g��|||�S(NR6isB_StatusBasedRedirect middleware received an invalid status code %sisqExpected the url to internally redirect to in the _StatusBasedRedirect error_mapperto be a string or None, not %s(RR&RR7RR8RR9R4R RERR:R(RRRR<R*RR (t code_messageR RR R(sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyR=&s. i����iR%is wsgi.errorssBError occurred in _StatusBasedRedirect intercepting the response: R RR$itInvalidForwardcBseZRS((R-R.(((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyRS\scs'|d dkr#�d���ndS(s� We don't want start_response to do anything since it has already been called it200slThe URL %s to internally forward to in order to create an error document did not return a '200' status code.N((RRR(RSturl_(sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyteat_start_response^sspaste.recursive.forwards?Error occurred in _StatusBasedRedirect redirecting to new URL: ( RR3R)R:RR(RQRDRR8R (RR R R=RBR)terrorRR t new_environtktvRVRtold_start_responseR+((RSRRR RR RRUsD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyR,"sZ            N(R-R.RCRRR,(((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyRO�s (RCRLR)Rtpaste.recursiveRRRt paste.utilRtpaste.responseRRtobjectRRRIt __pudge_all__RRPRO(((sD/opt/alt/python27/lib/python2.7/site-packages/paste/errordocument.pyt<module> s"   ,r