def do_GET(self): tsn = self.headers.getheader('TiVo_TCD_ID', self.headers.getheader('tsn', '')) ip = self.address_string() self.tivos[tsn] = ip basepath = unquote_plus(self.path).split('/')[1] ## Get File for name, container in self.server.containers.items(): if basepath == name: plugin = GetPlugin(container['type']) plugin.send_file(self, container, name) return ## Not a file not a TiVo command f**k them if not self.path.startswith('/TiVoConnect'): self.infopage() return o = urlparse("http://fake.host" + self.path) query = parse_qs(o[4]) mname = False if query.has_key('Command') and len(query['Command']) >= 1: command = query['Command'][0] # If we are looking at the root container if command == "QueryContainer" and \ (not query.has_key('Container') or query['Container'][0] == '/'): self.root_container() return if query.has_key('Container'): # Dispatch to the container plugin basepath = unquote(query['Container'][0].split('/')[0]) for name, container in self.server.containers.items(): if basepath == name: plugin = GetPlugin(container['type']) if hasattr(plugin, command): method = getattr(plugin, command) method(self, query) return else: break # If we made it here it means we couldn't match the request to # anything. self.unsupported(query)
def handle_file(self, query, splitpath): if '..' not in splitpath: # Protect against path exploits ## Pass it off to a plugin? for name, container in self.server.containers.items(): if splitpath[0] == name: self.cname = name self.container = container base = os.path.normpath(container['path']) path = os.path.join(base, *splitpath[1:]) plugin = GetPlugin(container['type']) plugin.send_file(self, path, query) return ## Serve it from a "content" directory? base = os.path.join(SCRIPTDIR, *splitpath[:-1]) path = os.path.join(base, 'content', splitpath[-1]) if os.path.isfile(path): try: handle = open(path, 'rb') except: self.send_error(404) return # Send the header mime = mimetypes.guess_type(path)[0] self.send_response(200) if mime: self.send_header('Content-type', mime) self.send_header('Content-length', os.path.getsize(path)) self.end_headers() # Send the body of the file try: shutil.copyfileobj(handle, self.wfile) except: pass handle.close() return ## Give up self.send_error(404)
def handle_file(self, query, splitpath): if '..' not in splitpath: # Protect against path exploits ## Pass it off to a plugin? for name, container in self.server.containers.items(): if splitpath[0] == name: self.cname = name self.container = container base = os.path.normpath(container['path']) path = os.path.join(base, *splitpath[1:]) plugin = GetPlugin(container['type']) plugin.send_file(self, path, query) return ## Serve it from a "content" directory? base = os.path.join(SCRIPTDIR, *splitpath[:-1]) path = os.path.join(base, 'content', splitpath[-1]) if os.path.isfile(path): self.send_content_file(path) return ## Give up self.send_error(404)