Back to index

plone3  3.1.7
RequestTypeSniffer.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """ Classes: RequestTypeSniffer
00016 
00017 $Id: RequestTypeSniffer.py 70851 2006-10-20 19:25:56Z jens $
00018 """
00019 
00020 from Acquisition import aq_parent
00021 from AccessControl import ClassSecurityInfo
00022 from Globals import InitializeClass
00023 from ZServer.FTPRequest import FTPRequest
00024 from ZPublisher import xmlrpc
00025 
00026 from zope.interface import Interface
00027 
00028 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00029 
00030 from Products.PluggableAuthService.interfaces.plugins \
00031     import IRequestTypeSniffer
00032 from Products.PluggableAuthService.interfaces.request \
00033     import IBrowserRequest
00034 from Products.PluggableAuthService.interfaces.request \
00035     import IWebDAVRequest
00036 from Products.PluggableAuthService.interfaces.request \
00037     import IFTPRequest
00038 from Products.PluggableAuthService.interfaces.request \
00039     import IXMLRPCRequest
00040 from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
00041 from Products.PluggableAuthService.utils import classImplements
00042 
00043 class IRequestTypeSnifferPlugin(Interface):
00044     """ Marker interface.
00045     """
00046 
00047 _sniffers = ()
00048 
00049 def registerSniffer(iface, func):
00050     global _sniffers
00051     registry = list(_sniffers)
00052     registry.append((iface, func))
00053     _sniffers = tuple(registry)
00054 
00055 manage_addRequestTypeSnifferForm = PageTemplateFile(
00056     'www/rtsAdd', globals(), __name__='manage_addRequestTypeSnifferForm' )
00057 
00058 def addRequestTypeSnifferPlugin( dispatcher, id, title=None, REQUEST=None ):
00059     """ Add a RequestTypeSnifferPlugin to a Pluggable Auth Service. """
00060 
00061     rts = RequestTypeSniffer(id, title)
00062     dispatcher._setObject(rts.getId(), rts)
00063 
00064     if REQUEST is not None:
00065         REQUEST['RESPONSE'].redirect(
00066                                 '%s/manage_workspace'
00067                                 '?manage_tabs_message='
00068                                 'RequestTypeSniffer+added.'
00069                             % dispatcher.absolute_url())
00070 
00071 
00072 class RequestTypeSniffer( BasePlugin ):
00073 
00074     """ PAS plugin for detecting a Request's type
00075     """
00076     meta_type = 'Request Type Sniffer Plugin'
00077 
00078     security = ClassSecurityInfo()
00079 
00080     def __init__(self, id, title=None):
00081 
00082         self._id = self.id = id
00083         self.title = title
00084 
00085     security.declarePrivate('sniffRequestType')
00086     def sniffRequestType(self, request):
00087         found = None
00088         for iface, func in _sniffers:
00089             if func( request ):
00090                 found = iface
00091 
00092         if found is not None:
00093             return found
00094 
00095 classImplements(RequestTypeSniffer,
00096                 IRequestTypeSnifferPlugin,
00097                 IRequestTypeSniffer,
00098                )
00099 
00100 InitializeClass(RequestTypeSniffer)
00101 
00102 # Most of the sniffing code below has been inspired by
00103 # similar tests found in BaseRequest, HTTPRequest and ZServer
00104 def webdavSniffer(request):
00105     dav_src = request.get('WEBDAV_SOURCE_PORT', None)
00106     method = request.get('REQUEST_METHOD', 'GET').upper()
00107     path_info = request.get('PATH_INFO', '')
00108 
00109     if dav_src:
00110         return True
00111 
00112     if method not in ('GET', 'POST'):
00113         return True
00114 
00115     if method in ('GET',) and path_info.endswith('manage_DAVget'):
00116         return True
00117 
00118 registerSniffer(IWebDAVRequest, webdavSniffer)
00119 
00120 def xmlrpcSniffer(request):
00121     response = request['RESPONSE']
00122     method = request.get('REQUEST_METHOD', 'GET').upper()
00123 
00124     if method in ('GET', 'POST') and isinstance(response, xmlrpc.Response):
00125         return True
00126 
00127 registerSniffer(IXMLRPCRequest, xmlrpcSniffer)
00128 
00129 def ftpSniffer(request):
00130     if isinstance(request, FTPRequest):
00131         return True
00132 
00133 registerSniffer(IFTPRequest, ftpSniffer)
00134 
00135 def browserSniffer(request):
00136     # If it's none of the above, it's very likely a browser request.
00137     for sniffer in (webdavSniffer, ftpSniffer, xmlrpcSniffer):
00138         if sniffer(request):
00139             return False
00140     return True
00141 
00142 registerSniffer(IBrowserRequest, browserSniffer)