def handler(req): conf = req.get_config() # get module name to be published dir, file = os.path.split(req.filename) module_name, ext = os.path.splitext(file) # if autoreload is on, we will check dates # and reload the module if the source is newer apache.import_module(module_name, req) # setup CGI environment env, si, so = apache.setup_cgi(req) try: ZPublisher.publish_module(module_name, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, environ=os.environ) finally: apache.restore_nocgi(env, si, so) return apache.OK
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 = req.path_info[1:] # skip first / func_path = func_path.replace("/", ".") if func_path[-1:] == ".": func_path = func_path[:-1] # default to 'index' if no path_info was given if not func_path: func_path = "index" # if any part of the path begins with "_", abort if func_path[0] == '_' or func_path.count("._"): raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND # process input, if any fs = util.FieldStorage(req, keep_blank_values=1) req.form = fs args = {} # step through fields for field in fs.list: # if it is a file, make File() if field.filename: val = File(field) else: val = field.value if args.has_key(field.name): args[field.name].append(val) else: args[field.name] = [val] # at the end, we replace lists with single values for arg in args.keys(): if len(args[arg]) == 1: args[arg] = args[arg][0] # add req args["req"] = req ## import the script path, module_name = os.path.split(req.filename) # get rid of the suffix # explanation: Suffixes that will get stripped off # are those that were specified as an argument to the # AddHandler directive. Everything else will be considered # a package.module rather than module.suffix exts = req.get_addhandler_exts() if exts: suffixes = exts.strip().split() exp = "\\." + "$|\\.".join(suffixes) suff_matcher = re.compile(exp) # python caches these, so its fast module_name = suff_matcher.sub("", module_name) # import module (or reload if needed) # the [path] argument tells import_module not to allow modules whose # full path is not in [path] or below. module = apache.import_module(module_name, req.get_config(), [path]) # does it have an __auth__? realm, user, passwd = process_auth(req, module) # resolve the object ('traverse') try: object = resolve_object(req, module, func_path, realm, user, passwd) except AttributeError: raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND # not callable, a class or an aunbound method if not callable(object) or \ str(type(object)) == "<type 'class'>" \ or (hasattr(object, 'im_self') and not object.im_self): result = str(object) else: # callable, (but not a class or unbound method) # we need to weed out unexpected keyword arguments # and for that we need to get a list of them. There # are a few options for callable objects here: if str(type(object)) == "<type 'instance'>": # instances are callable when they have __call__() object = object.__call__ if hasattr(object, "func_code"): # function fc = object.func_code expected = fc.co_varnames[0:fc.co_argcount] elif hasattr(object, 'im_func'): # method fc = object.im_func.func_code expected = fc.co_varnames[1:fc.co_argcount] # remove unexpected args unless co_flags & 0x08, # meaning function accepts **kw syntax if not (fc.co_flags & 0x08): for name in args.keys(): if name not in expected: del args[name] result = apply(object, (), args) if result: result = str(result) # unless content_type was manually set, we will attempt # to guess it if not req._content_type_set: # make an attempt to guess content-type 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: return apache.HTTP_INTERNAL_SERVER_ERROR
def handler(req): # use direct access to _req for speed _req = req._req args = {} # get the path PATH_INFO (everthing after script) if not _req.subprocess_env.has_key("PATH_INFO"): raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND func_path = _req.subprocess_env["PATH_INFO"][1:] # skip fist / func_path = string.replace(func_path, "/", ".") if func_path[-1] == ".": func_path = func_path[:-1] # if any part of the path begins with "_", abort if func_path[0] == "_" or string.count(func_path, "._"): raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND # process input, if any fs = util.FieldStorage(req, keep_blank_values=1) req.form = fs # step through fields for field in fs.list: # if it is a file, make File() if field.filename: val = File(field) else: val = field.value if args.has_key(field.name): args[field.name].append(val) else: args[field.name] = [val] # at the end, we replace lists with single values for arg in args.keys(): if len(args[arg]) == 1: args[arg] = args[arg][0] # add req args["req"] = req # import the script path, module_name = os.path.split(_req.filename) # get rid of the suffix module_name = suff_matcher.sub("", module_name) # import module (or reload if needed) module = apache.import_module(module_name, _req, [path]) # does it have an __auth__? realm, user, passwd = process_auth(req, module) # resolve the object ('traverse') try: object = resolve_object(req, module, func_path, realm, user, passwd) except AttributeError: raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND # not callable, a class or an aunbound method if ( not callable(object) or str(type(object)) == "<type 'class'>" or (hasattr(object, "im_self") and not object.im_self) ): result = str(object) else: # callable, (but not a class or unbound method) # we need to weed out unexpected keyword arguments # and for that we need to get a list of them. There # are a few options for callable objects here: if str(type(object)) == "<type 'instance'>": # instances are callable when they have __call__() object = object.__call__ if hasattr(object, "func_code"): # function fc = object.func_code expected = fc.co_varnames[0 : fc.co_argcount] elif hasattr(object, "im_func"): # method fc = object.im_func.func_code expected = fc.co_varnames[1 : fc.co_argcount] # remove unexpected args for name in args.keys(): if name not in expected: del args[name] result = apply(object, (), args) if result: result = str(result) # unless content_type was manually set, we will attempt # to guess it if not req._content_type_set: # make an attempt to guess content-type if string.lower(string.strip(result[:100])[:6]) == "<html>" or string.find(result, "</") > 0: req.content_type = "text/html" else: req.content_type = "text/plain" req.send_http_header() req.write(result) return apache.OK else: return apache.HTTP_INTERNAL_SERVER_ERROR
def handler(req, auth=None): """ """ # be pessimistic result = apache.DECLINED try: opt = req.get_options() # get filename filename = req.filename # module names do not have to end with .py # they can have any extension or no extention at all # the extention will be discarded # find the module name by getting the string between the # last slash and the last dot, if any. slash = string.rfind(filename, "/") dot = string.rfind(filename, ".") if dot > slash: module_name = filename[slash + 1:dot] else: # this file has no extension module_name = filename[slash + 1:] # if we're using packages if opt.has_key("rootpkg"): module_name = opt["rootpkg"] + "." + module_name if opt.has_key("debug"): debug = opt["debug"] else: debug = 0 if auth: module_name = opt["authhandler"] else: if opt.has_key("handler"): module_name = opt["handler"] # add the uri directory to pythonpath if os.path.isdir(filename): dir = filename else: dir = filename[:slash] if dir not in sys.path: sys.path[:0] = [dir] # import the module module = apache.import_module(module_name, req) # instantiate the handler class if auth: Class = module.AuthHandler else: Class = module.RequestHandler # backward compatibility objects - pb, sn, rq pb = NSAPI_ParameterBlock(req) rq = NSAPI_Request(req) sn = NSAPI_Session(req) # construct and return an instance of the handler class handler = Class(pb, sn, rq) handler.__req__ = req # do it if auth: result = handler.Handle() else: result = handler.Handle(debug=debug) except apache.SERVER_RETURN, value: # SERVER_RETURN indicates a non-local abort from below # with value as (result, status) or (result, None) try: (result, status) = value if status: req.status = status except: pass