home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-python-addon-1.4.9-installer.exe / publisher.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-10-01  |  6.4 KB  |  215 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. """
  5.   This handler is conceputally similar to Zope's ZPublisher, except
  6.   that it:
  7.  
  8.   1. Is written specifically for mod_python and is therefore much faster
  9.   2. Does not require objects to have a documentation string
  10.   3. Passes all arguments as simply string
  11.   4. Does not try to match Python errors to HTTP errors
  12.   5. Does not give special meaning to '.' and '..'.
  13. """
  14. import apache
  15. import util
  16. import sys
  17. import os
  18. import imp
  19. import re
  20. import base64
  21. import new
  22. from types import *
  23. imp_suffixes = []([ x[0][1:] for x in imp.get_suffixes() ])
  24.  
  25. def handler(req):
  26.     req.allow_methods([
  27.         'GET',
  28.         'POST'])
  29.     if req.method not in [
  30.         'GET',
  31.         'POST']:
  32.         raise apache.SERVER_RETURN, apache.HTTP_METHOD_NOT_ALLOWED
  33.     
  34.     func_path = ''
  35.     if req.path_info:
  36.         func_path = req.path_info[1:]
  37.         func_path = func_path.replace('/', '.')
  38.         if func_path[-1:] == '.':
  39.             func_path = func_path[:-1]
  40.         
  41.     
  42.     if not func_path:
  43.         func_path = 'index'
  44.     
  45.     if func_path[0] == '_' or func_path.count('._'):
  46.         raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
  47.     
  48.     (path, module_name) = os.path.split(req.filename)
  49.     if not module_name:
  50.         module_name = 'index'
  51.     
  52.     exts = req.get_addhandler_exts()
  53.     if not exts:
  54.         exts = imp_suffixes
  55.     
  56.     if req.extension:
  57.         exts += req.extension[1:]
  58.     
  59.     if exts:
  60.         suffixes = exts.strip().split()
  61.         exp = '\\.' + '$|\\.'.join(suffixes)
  62.         suff_matcher = re.compile(exp)
  63.         module_name = suff_matcher.sub('', module_name)
  64.     
  65.     config = req.get_config()
  66.     autoreload = int(config.get('PythonAutoReload', 1))
  67.     log = int(config.get('PythonDebug', 0))
  68.     
  69.     try:
  70.         module = apache.import_module(module_name, autoreload = autoreload, log = log, path = [
  71.             path])
  72.     except ImportError:
  73.         (et, ev, etb) = sys.exc_info()
  74.         func_path = module_name
  75.         module_name = 'index'
  76.         
  77.         try:
  78.             module = apache.import_module(module_name, autoreload = autoreload, log = log, path = [
  79.                 path])
  80.         except ImportError:
  81.             raise et, ev, etb
  82.         except:
  83.             None<EXCEPTION MATCH>ImportError
  84.         
  85.  
  86.         None<EXCEPTION MATCH>ImportError
  87.  
  88.     (realm, user, passwd) = process_auth(req, module)
  89.     
  90.     try:
  91.         object = resolve_object(req, module, func_path, realm, user, passwd)
  92.     except AttributeError:
  93.         raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
  94.  
  95.     if not callable(object) and type(object) is ClassType and hasattr(object, 'im_self') and not (object.im_self):
  96.         result = str(object)
  97.     else:
  98.         req.form = util.FieldStorage(req, keep_blank_values = 1)
  99.         result = util.apply_fs_data(object, req.form, req = req)
  100.     if result and req.bytes_sent > 0 or req.next:
  101.         if result is None:
  102.             result = ''
  103.         else:
  104.             result = str(result)
  105.         if not (req._content_type_set):
  106.             if result[:100].strip()[:6].lower() == '<html>' or result.find('</') > 0:
  107.                 req.content_type = 'text/html'
  108.             else:
  109.                 req.content_type = 'text/plain'
  110.         
  111.         if req.method != 'HEAD':
  112.             req.write(result)
  113.         else:
  114.             req.write('')
  115.         return apache.OK
  116.     else:
  117.         req.log_error('mod_python.publisher: %s returned nothing.' % `object`)
  118.         return apache.HTTP_INTERNAL_SERVER_ERROR
  119.  
  120.  
  121. def process_auth(req, object, realm = 'unknown', user = None, passwd = None):
  122.     (found_auth, found_access) = (0, 0)
  123.     if not user and req.headers_in.has_key('Authorization'):
  124.         
  125.         try:
  126.             s = req.headers_in['Authorization'][6:]
  127.             s = base64.decodestring(s)
  128.             (user, passwd) = s.split(':', 1)
  129.         raise apache.SERVER_RETURN, apache.HTTP_BAD_REQUEST
  130.  
  131.     
  132.     if hasattr(object, '__auth_realm__'):
  133.         realm = object.__auth_realm__
  134.     
  135.     if type(object) is FunctionType:
  136.         if hasattr(object, 'func_code'):
  137.             func_code = object.func_code
  138.             if '__auth__' in func_code.co_names:
  139.                 i = list(func_code.co_names).index('__auth__')
  140.                 __auth__ = func_code.co_consts[i + 1]
  141.                 if hasattr(__auth__, 'co_name'):
  142.                     __auth__ = new.function(__auth__, globals())
  143.                 
  144.                 found_auth = 1
  145.             
  146.             if '__access__' in func_code.co_names:
  147.                 i = list(func_code.co_names).index('__access__')
  148.                 __access__ = func_code.co_consts[i + 1]
  149.                 if hasattr(__access__, 'co_name'):
  150.                     __access__ = new.function(__access__, globals())
  151.                 
  152.                 found_access = 1
  153.             
  154.             if '__auth_realm__' in func_code.co_names:
  155.                 i = list(func_code.co_names).index('__auth_realm__')
  156.                 realm = func_code.co_consts[i + 1]
  157.             
  158.         
  159.     elif hasattr(object, '__auth__'):
  160.         __auth__ = object.__auth__
  161.         found_auth = 1
  162.     
  163.     if hasattr(object, '__access__'):
  164.         __access__ = object.__access__
  165.         found_access = 1
  166.     
  167.     if found_auth:
  168.         if not user:
  169.             s = 'Basic realm="%s"' % realm
  170.             req.err_headers_out['WWW-Authenticate'] = s
  171.             raise apache.SERVER_RETURN, apache.HTTP_UNAUTHORIZED
  172.         
  173.         if callable(__auth__):
  174.             rc = __auth__(req, user, passwd)
  175.         elif type(__auth__) is DictionaryType:
  176.             if __auth__.has_key(user):
  177.                 pass
  178.             rc = __auth__[user] == passwd
  179.         else:
  180.             rc = __auth__
  181.         if not rc:
  182.             s = 'Basic realm = "%s"' % realm
  183.             req.err_headers_out['WWW-Authenticate'] = s
  184.             raise apache.SERVER_RETURN, apache.HTTP_UNAUTHORIZED
  185.         
  186.     
  187.     if found_access:
  188.         if callable(__access__):
  189.             rc = __access__(req, user)
  190.         elif type(__access__) in (ListType, TupleType):
  191.             rc = user in __access__
  192.         else:
  193.             rc = __access__
  194.         if not rc:
  195.             raise apache.SERVER_RETURN, apache.HTTP_FORBIDDEN
  196.         
  197.     
  198.     return (realm, user, passwd)
  199.  
  200.  
  201. def resolve_object(req, obj, object_str, realm = None, user = None, passwd = None):
  202.     """
  203.     This function traverses the objects separated by .
  204.     (period) to find the last one we're looking for.
  205.     """
  206.     for obj_str in object_str.split('.'):
  207.         obj = getattr(obj, obj_str)
  208.         if type(obj) == ModuleType:
  209.             raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
  210.         
  211.         (realm, user, passwd) = process_auth(req, obj, realm, user, passwd)
  212.     
  213.     return obj
  214.  
  215.