def open(self): key = self.get_query_argument('key', '') if key != app_key: access_log.warn('Access denied on %s.' % self.request.remote_ip) self.write_message("Access denied") self.close() else: self.write_message("Access granted") access_log.info('Access allowed on %s.' % self.request.remote_ip)
def open(self): # returns the value of the argument with the given name from the request # query string (the url) # it's the key that will be compared with the one saved key = self.get_query_argument('key', '') if key != app_key: access_log.warn('Access denied on %s.' % self.request.remote_ip) self.write_message("Access denied") self.close() else: self.write_message("Access granted") access_log.info('Access allowed on %s.' % self.request.remote_ip)
def validate_absolute_path(self, root_folder, absolute_path): """overload this method to handle default rule""" root_folder = os.path.abspath(root_folder) if not absolute_path.startswith(root_folder): errmsg = "%s is not in root_folder static directory" % self.path raise tornado.web.HTTPError(403, errmsg) if (os.path.isdir(absolute_path) and self.index_file is not None): if not self.request.path.endswith("/"): # if the path is folder, the path should end with '/'. self.redirect(self.request.path + "/", permanent=True) return absolute_path = os.path.join(absolute_path, self.index_file) if os.path.exists(absolute_path): if os.path.isfile(absolute_path): return absolute_path else: msg = "it is not a file: '%s' '%s' " % (absolute_path, self.path) raise tornado.web.HTTPError(403, msg) if self.request.path == self.default_path: # default_path does exists errmsg = 'NOT FOUND: default_path[%s]: %s ' errmsg %= (self.default_path, absolute_path) access_log.warn(errmsg) raise tornado.web.HTTPError(404) if self.default_path is not None: guess_mimetype = lambda path : mimetypes.guess_type(path)[0] abs_default_path = os.path.join(root_folder, self.default_path) abs_default_path = os.path.abspath(abs_default_path) if guess_mimetype(abs_default_path) == guess_mimetype(absolute_path) : # redirect if the mime type of path are same with default_path self.redirect(self.default_path) else: raise tornado.web.HTTPError(404)