def mime_type(self): if self.is_dir: raise Exception("The path a directory: " + self.path) from mime import get_mime_type return get_mime_type(self.path)
def send_file(conn, path): mime_type = mime.get_mime_type(path) send_headers(conn, 200, 'OK', mime_type) with open(path, 'rb') as f: data = f.read(BYTES) while data: conn.send(data) data = f.read(BYTES)
def handle_request(conn): data = conn.recv(BYTES) # Start of HTTP header format: # METHOD PATH PROTOCOL\r\n... if len(data) == 0: return STATUS_NODATA request = data.decode('ascii').split('\r\n')[0] try: (method, path, protocol) = request.split() except: return STATUS_INVALID_REQUEST abs_fname = WWW_DIR + path if method != 'GET': log(request, 501) send_error(conn, 501, 'Not supported', None, 'Method is not supported.') elif os.path.isdir(abs_fname): if path == '/': log(request, 302) log(request, 200) if os.path.isfile(abs_fname + 'index.html'): send_file(conn, abs_fname + 'index.html') else: send_default_page(conn) else: if os.path.isfile(abs_fname + 'index.html'): log(request, 200) send_file(conn, abs_fname + 'index.html') else: log(request, 404) send_error(conn, 404, 'Not Found', None, 'The requested URL %s was not found on this server.' \ % path) elif os.path.isfile(abs_fname): if not mime.get_mime_type(abs_fname): log(request, 404) send_error(conn, 404, 'Unsupported File', None, 'The format of the requested file %s' \ ' is not supported by this server.' % path) else: log(request, 200) send_file(conn, abs_fname) else: log(request, 404) send_error(conn, 404, 'Not Found', None, 'The requested URL %s was not found on this server.' % path) return STATUS_SUCCESS
def get_icon_path_for_file(file): icon_path = None if os.path.isdir(file.path): icon_path = get_icon_path('folder') elif is_thumbnailable(file): thumbnail_path = thumbnail.get_thumbnail_path(file) if thumbnail_path != None: icon_path = file.path if icon_path == None: mime_type = mime.get_mime_type(file.path) icon_path = get_icon_path_for_mime_type('gnome-mime-' + mime_type) if icon_path == None: icon_path = get_icon_path_for_mime_type(mime_type) # have a default if icon_path == None: icon_path = get_icon_path('unknown') return icon_path
def onHTTP(self, header, method, path, query, post): # log.log(1,self,header,method,path,query,post) if method == "POST": postJson = json.loads(post) opt = postJson["opt"] respons = {} if(opt == "status"): respons = self.server.dumpConnects() elif(opt == "serverList"): respons["pac"] = "http://" + self.httpMessageParse.getHeader("host") + "/pac" respons["list"] = settingConfig.setting(settingConfig.remoteServerList) respons["version"] = version.version elif opt == "setServerList": settingConfig.setting(settingConfig.remoteServerList, postJson["data"]) respons["status"] = "ok" # elif opt == "testRemoteProxy": # respons["status"] = "" elif opt == "domainList": if "action" in postJson: action = postJson["action"] domain = postJson["domain"] respons={"status":"ok"} if action == "delete": domainConfig.config.removeDomain(domain) elif action == "open": domainConfig.config.openDomain(domain) elif action == "close": domainConfig.config.closeDomain(domain) else: respons={"status":"no found action"} else: respons["domainList"] = domainConfig.config.getDomainListWithAnalysis() elif opt == "analysisData": respons["analysisData"] = analysis.getAnalysisData( selectDomain=postJson["domain"], startTime=postJson["startTime"], todayStartTime=postJson["todayStartTime"] ) elif opt == "addDomain": url = postJson["url"] host = parserUrlAddrPort(url)[0] if host: host = getDomainName(host) else: host = url if getDomainName(url) else "" respons["status"] = "ok" if domainConfig.config.addDomain(host) else "error" self.reseponse(respons,connection=self.httpMessageParse.connection()) elif path == "/pac": content = self.getFileContent(dirname(__file__) + "/template/pac.js") content = content.replace("{{domainWhiteListJson}}", json.dumps(domainConfig.config.getDomainList(0))) content = content.replace("{{domainListJson}}", json.dumps(domainConfig.config.getDomainList(1))) content = content.replace("{{proxy_ddr}}", self.httpMessageParse.getHeader("host")) self.reseponse(content,connection=self.httpMessageParse.connection()) else: if path == "/": path = "/index.html" content = self.getFileContent(dirname(__file__) + "/template" +path) if content: self.reseponse(content,ContentType=get_mime_type(path),connection=self.httpMessageParse.connection()) else: self.reseponse("\"" + path + "\" not found", code=404,connection=self.httpMessageParse.connection())
def onHTTP(self, header, method, path, query, post): # log.log(1,self,header,method,path,query,post) if method == "POST": postJson = json.loads(post) opt = postJson["opt"] respons = {} if (opt == "status"): respons = self.server.dumpConnects() elif (opt == "serverList"): respons["pac"] = "http://" + self.httpMessageParse.getHeader( "host") + "/pac" respons["proxyAddr"] = parserUrlAddrPort(respons["pac"]) respons["list"] = settingConfig.setting( settingConfig.remoteServerList) respons["version"] = version.version elif opt == "setServerList": settingConfig.setting(settingConfig.remoteServerList, postJson["data"]) respons["status"] = "ok" # elif opt == "testRemoteProxy": # respons["status"] = "" elif opt == "domainList": if "action" in postJson: action = postJson["action"] domain = postJson["domain"] respons = {"status": "ok"} if action == "delete": domainConfig.config.removeDomain(domain) elif action == "open": domainConfig.config.openDomain(domain) elif action == "close": domainConfig.config.closeDomain(domain) else: respons = {"status": "no found action"} else: respons[ "domainList"] = domainConfig.config.getDomainListWithAnalysis( ) elif opt == "analysisData": respons["analysisData"] = analysis.getAnalysisData( selectDomain=postJson["domain"], startTime=postJson["startTime"], todayStartTime=postJson["todayStartTime"]) elif opt == "restore": if postJson["clearAll"]: domainConfig.config.setting = {} domainList = postJson["domainList"] for domain in domainList: domainConfig.config.addDomain( domain[0], Open=domain[1], updateTime=domain[2] if len(domain) > 2 else 0) respons["status"] = "ok" elif opt == "addDomain": url = postJson["url"] host = parserUrlAddrPort(url)[0] if host: host = getDomainName(host) else: host = url if getDomainName(url) else "" respons["status"] = "ok" if domainConfig.config.addDomain( host) else "error" self.reseponse(respons, connection=self.httpMessageParse.connection()) elif path == "/pac": content = self.getFileContent( dirname(__file__) + "/template/pac.js") content = content.replace( "{{domainWhiteListJson}}", json.dumps(domainConfig.config.getDomainList(0))) content = content.replace( "{{domainListJson}}", json.dumps(domainConfig.config.getDomainList(1))) content = content.replace("{{proxy_ddr}}", self.httpMessageParse.getHeader("host")) self.reseponse(content, connection=self.httpMessageParse.connection()) else: if path == "/": path = "/index.html" content = self.getFileContent( dirname(__file__) + "/template" + path) if content and path.find("..") == -1: self.reseponse(content, ContentType=get_mime_type(path), connection=self.httpMessageParse.connection()) else: self.reseponse("\"" + path + "\" not found", code=404, connection=self.httpMessageParse.connection())