def is_route(self, uri): """Check if uri have any registered record.""" uri = uni(uri) if re_filter.search(uri): r_uri = re_filter.sub(self.__regex, uri) + '$' return self.is_rroute(r_uri) return uri in self.__handlers
def pop_route(self, uri, method): """Pop handler for uri and method from handers table. Method must be define unique, so METHOD_GET_POST could not be use. If you want to remove handler for both methods, you must call pop route for each method state. """ uri = uni(uri) if re_filter.search(uri): r_uri = re_filter.sub(self.__regex, uri) + '$' return self.pop_rroute(r_uri, method) else: handlers = self.__handlers.get(uri, {}) rv = handlers.pop(method) if not handlers: # is empty self.__handlers.pop(uri, None) return rv
def set_route(self, uri, fn, method=METHOD_HEAD | METHOD_GET): """Set handler for uri and method. Another way to add fn as handler for uri. See Application.route documentation for details. app.set_route('/use/post', user_create, METHOD_POST) """ uri = uni(uri) if re_filter.search(uri): r_uri = re_filter.sub(self.__regex, uri) + '$' convertors = tuple((g[0], self.__convertor(g[1])) for g in (m.groups() for m in re_filter.finditer(uri))) self.set_rroute(r_uri, fn, method, convertors) else: if uri not in self.__handlers: self.__handlers[uri] = {} for m in methods.values(): if method & m: self.__handlers[uri][m] = fn
def uri(self): """The path portion of the URI.""" return uni(self.__environ.get('PATH_INFO'))
def handler_from_table(self, req): """Call right handler from handlers table (fill with route function). If no handler is fined, try to find directory or file if Document Root, resp. Document Index is set. Then try to call default handler for right method or call handler for status code 404 - not found. """ # static routes if req.uri in self.__handlers: if req.method_number in self.__handlers[req.uri]: handler = self.__handlers[req.uri][req.method_number] req.uri_rule = req.uri # nice variable for pre handlers req.uri_handler = handler self.handler_from_pre(req) # call pre handlers now retval = handler(req) # call right handler now # return text is allowed if isinstance(retval, str) \ or (_unicode_exist and isinstance(retval, unicode)): req.write(retval, 1) # write data and flush retval = DONE if retval != DECLINED: raise SERVER_RETURN(retval or DONE) # could be state.DONE else: raise SERVER_RETURN(HTTP_METHOD_NOT_ALLOWED) # endif # endif # regular expression for ruri in self.__rhandlers.keys(): match = ruri.match(req.uri) if match and req.method_number in self.__rhandlers[ruri]: handler, convertors = self.__rhandlers[ruri][req.method_number] req.uri_rule = ruri.pattern # nice variable for pre handlers req.uri_handler = handler self.handler_from_pre(req) # call pre handlers now if len(convertors): # create OrderedDict from match insead of dict for # convertors applying req.groups = OrderedDict( (g, c(v))for ((g, c), v) in zip(convertors, match.groups())) retval = handler(req, *req.groups.values()) else: req.groups = match.groupdict() retval = handler(req, *match.groups()) # return text is allowed if isinstance(retval, str) \ or (_unicode_exist and isinstance(retval, unicode)): req.write(retval, 1) # write data and flush retval = DONE if retval != DECLINED: raise SERVER_RETURN(retval or DONE) # could be state.DONE # endif - no METHOD_NOT_ALLOWED here # endfor # try file or index if req.document_root(): rfile = "%s%s" % (uni(req.document_root()), path.normpath("%s" % uni(req.uri))) if not path.exists(rfile): if req.debug and req.uri == '/debug-info': # work if debug req.uri_rule = '_debug_info_' req.uri_handler = debug_info self.handler_from_pre(req) # call pre handlers now raise SERVER_RETURN(debug_info(req, self)) self.handler_from_default(req) # try default raise SERVER_RETURN(HTTP_NOT_FOUND) # not found # return file if path.isfile(rfile) and access(rfile, R_OK): req.uri_rule = '_send_file_' req.uri_handler = send_file self.handler_from_pre(req) # call pre handlers now req.log_error("Return file: %s" % req.uri, LOG_INFO) raise SERVER_RETURN(send_file(req, rfile)) # return directory index if req.document_index and path.isdir(rfile) \ and access(rfile, R_OK): req.log_error("Return directory: %s" % req.uri, LOG_INFO) req.uri_rule = '_directory_index_' req.uri_handler = directory_index self.handler_from_pre(req) # call pre handlers now raise SERVER_RETURN(directory_index(req, rfile)) raise SERVER_RETURN(HTTP_FORBIDDEN) # endif if req.debug and req.uri == '/debug-info': req.uri_rule = '_debug_info_' req.uri_handler = debug_info self.handler_from_pre(req) # call pre handlers now raise SERVER_RETURN(debug_info(req, self)) self.handler_from_default(req) req.log_error("404 Not Found: %s" % req.uri, LOG_ERR) raise SERVER_RETURN(HTTP_NOT_FOUND)