home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.3)
-
- """
- This handler is conceputally similar to Zope's ZPublisher, except
- that it:
-
- 1. Is written specifically for mod_python and is therefore much faster
- 2. Does not require objects to have a documentation string
- 3. Passes all arguments as simply string
- 4. Does not try to match Python errors to HTTP errors
- 5. Does not give special meaning to '.' and '..'.
- """
- import apache
- import util
- import sys
- import os
- import imp
- import re
- import base64
- import new
- from types import *
- imp_suffixes = []([ x[0][1:] for x in imp.get_suffixes() ])
-
- def handler(req):
- req.allow_methods([
- 'GET',
- 'POST'])
- if req.method not in [
- 'GET',
- 'POST']:
- raise apache.SERVER_RETURN, apache.HTTP_METHOD_NOT_ALLOWED
-
- func_path = ''
- if req.path_info:
- func_path = req.path_info[1:]
- func_path = func_path.replace('/', '.')
- if func_path[-1:] == '.':
- func_path = func_path[:-1]
-
-
- if not func_path:
- func_path = 'index'
-
- if func_path[0] == '_' or func_path.count('._'):
- raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
-
- (path, module_name) = os.path.split(req.filename)
- if not module_name:
- module_name = 'index'
-
- exts = req.get_addhandler_exts()
- if not exts:
- exts = imp_suffixes
-
- if req.extension:
- exts += req.extension[1:]
-
- if exts:
- suffixes = exts.strip().split()
- exp = '\\.' + '$|\\.'.join(suffixes)
- suff_matcher = re.compile(exp)
- module_name = suff_matcher.sub('', module_name)
-
- config = req.get_config()
- autoreload = int(config.get('PythonAutoReload', 1))
- log = int(config.get('PythonDebug', 0))
-
- try:
- module = apache.import_module(module_name, autoreload = autoreload, log = log, path = [
- path])
- except ImportError:
- (et, ev, etb) = sys.exc_info()
- func_path = module_name
- module_name = 'index'
-
- try:
- module = apache.import_module(module_name, autoreload = autoreload, log = log, path = [
- path])
- except ImportError:
- raise et, ev, etb
- except:
- None<EXCEPTION MATCH>ImportError
-
-
- None<EXCEPTION MATCH>ImportError
-
- (realm, user, passwd) = process_auth(req, module)
-
- try:
- object = resolve_object(req, module, func_path, realm, user, passwd)
- except AttributeError:
- raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
-
- if not callable(object) and type(object) is ClassType and hasattr(object, 'im_self') and not (object.im_self):
- result = str(object)
- else:
- req.form = util.FieldStorage(req, keep_blank_values = 1)
- result = util.apply_fs_data(object, req.form, req = req)
- if result and req.bytes_sent > 0 or req.next:
- if result is None:
- result = ''
- else:
- result = str(result)
- if not (req._content_type_set):
- if result[:100].strip()[:6].lower() == '<html>' or result.find('</') > 0:
- req.content_type = 'text/html'
- else:
- req.content_type = 'text/plain'
-
- if req.method != 'HEAD':
- req.write(result)
- else:
- req.write('')
- return apache.OK
- else:
- req.log_error('mod_python.publisher: %s returned nothing.' % `object`)
- return apache.HTTP_INTERNAL_SERVER_ERROR
-
-
- def process_auth(req, object, realm = 'unknown', user = None, passwd = None):
- (found_auth, found_access) = (0, 0)
- if not user and req.headers_in.has_key('Authorization'):
-
- try:
- s = req.headers_in['Authorization'][6:]
- s = base64.decodestring(s)
- (user, passwd) = s.split(':', 1)
- raise apache.SERVER_RETURN, apache.HTTP_BAD_REQUEST
-
-
- if hasattr(object, '__auth_realm__'):
- realm = object.__auth_realm__
-
- if type(object) is FunctionType:
- if hasattr(object, 'func_code'):
- func_code = object.func_code
- if '__auth__' in func_code.co_names:
- i = list(func_code.co_names).index('__auth__')
- __auth__ = func_code.co_consts[i + 1]
- if hasattr(__auth__, 'co_name'):
- __auth__ = new.function(__auth__, globals())
-
- found_auth = 1
-
- if '__access__' in func_code.co_names:
- i = list(func_code.co_names).index('__access__')
- __access__ = func_code.co_consts[i + 1]
- if hasattr(__access__, 'co_name'):
- __access__ = new.function(__access__, globals())
-
- found_access = 1
-
- if '__auth_realm__' in func_code.co_names:
- i = list(func_code.co_names).index('__auth_realm__')
- realm = func_code.co_consts[i + 1]
-
-
- elif hasattr(object, '__auth__'):
- __auth__ = object.__auth__
- found_auth = 1
-
- if hasattr(object, '__access__'):
- __access__ = object.__access__
- found_access = 1
-
- if found_auth:
- if not user:
- s = 'Basic realm="%s"' % realm
- req.err_headers_out['WWW-Authenticate'] = s
- raise apache.SERVER_RETURN, apache.HTTP_UNAUTHORIZED
-
- if callable(__auth__):
- rc = __auth__(req, user, passwd)
- elif type(__auth__) is DictionaryType:
- if __auth__.has_key(user):
- pass
- rc = __auth__[user] == passwd
- else:
- rc = __auth__
- if not rc:
- s = 'Basic realm = "%s"' % realm
- req.err_headers_out['WWW-Authenticate'] = s
- raise apache.SERVER_RETURN, apache.HTTP_UNAUTHORIZED
-
-
- if found_access:
- if callable(__access__):
- rc = __access__(req, user)
- elif type(__access__) in (ListType, TupleType):
- rc = user in __access__
- else:
- rc = __access__
- if not rc:
- raise apache.SERVER_RETURN, apache.HTTP_FORBIDDEN
-
-
- return (realm, user, passwd)
-
-
- def resolve_object(req, obj, object_str, realm = None, user = None, passwd = None):
- """
- This function traverses the objects separated by .
- (period) to find the last one we're looking for.
- """
- for obj_str in object_str.split('.'):
- obj = getattr(obj, obj_str)
- if type(obj) == ModuleType:
- raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
-
- (realm, user, passwd) = process_auth(req, obj, realm, user, passwd)
-
- return obj
-
-