Move path parsing to separate function

This commit is contained in:
HelloZeroNet 2016-03-16 00:33:05 +01:00
parent 28b96179a3
commit 145d4dbd69

View file

@ -298,13 +298,21 @@ class UiRequest(object):
referer_path = re.sub("http[s]{0,1}://.*?/", "/", referer).replace("/media", "") # Remove site address referer_path = re.sub("http[s]{0,1}://.*?/", "/", referer).replace("/media", "") # Remove site address
return referer_path.startswith("/" + site_address) return referer_path.startswith("/" + site_address)
# Serve a media for site def parsePath(self, path):
def actionSiteMedia(self, path):
path = path.replace("/index.html/", "/") # Base Backward compatibility fix path = path.replace("/index.html/", "/") # Base Backward compatibility fix
if path.endswith("/"): if path.endswith("/"):
path = path + "index.html" path = path + "index.html"
match = re.match("/media/(?P<address>[A-Za-z0-9\._-]+)/(?P<inner_path>.*)", path) match = re.match("/media/(?P<address>[A-Za-z0-9\._-]+)/(?P<inner_path>.*)", path)
if match:
return match.groupdict()
else:
return None
# Serve a media for site
def actionSiteMedia(self, path):
path_parts = self.parsePath(path)
# Check wrapper nonce # Check wrapper nonce
content_type = self.getContentType(path) content_type = self.getContentType(path)
@ -315,13 +323,13 @@ class UiRequest(object):
self.server.wrapper_nonces.remove(self.get["wrapper_nonce"]) self.server.wrapper_nonces.remove(self.get["wrapper_nonce"])
referer = self.env.get("HTTP_REFERER") referer = self.env.get("HTTP_REFERER")
if referer and match: # Only allow same site to receive media if referer and path_parts: # Only allow same site to receive media
if not self.isMediaRequestAllowed(match.group("address"), referer): if not self.isMediaRequestAllowed(path_parts["address"], referer):
return self.error403("Media referrer error") # Referrer not starts same address as requested path return self.error403("Media referrer error") # Referrer not starts same address as requested path
if match: # Looks like a valid path if path_parts: # Looks like a valid path
address = match.group("address") address = path_parts["address"]
file_path = "%s/%s/%s" % (config.data_dir, address, match.group("inner_path")) file_path = "%s/%s/%s" % (config.data_dir, address, path_parts["inner_path"])
allowed_dir = os.path.abspath("%s/%s" % (config.data_dir, address)) # Only files within data/sitehash allowed allowed_dir = os.path.abspath("%s/%s" % (config.data_dir, address)) # Only files within data/sitehash allowed
data_dir = os.path.abspath("data") # No files from data/ allowed data_dir = os.path.abspath("data") # No files from data/ allowed
if ( if (
@ -341,15 +349,15 @@ class UiRequest(object):
return self.actionFile(file_path) return self.actionFile(file_path)
else: # File not exits, try to download else: # File not exits, try to download
site = SiteManager.site_manager.need(address, all_file=False) site = SiteManager.site_manager.need(address, all_file=False)
result = site.needFile(match.group("inner_path"), priority=5) # Wait until file downloads result = site.needFile(path_parts["inner_path"], priority=5) # Wait until file downloads
if result: if result:
return self.actionFile(file_path) return self.actionFile(file_path)
else: else:
self.log.debug("File not found: %s" % match.group("inner_path")) self.log.debug("File not found: %s" % path_parts["inner_path"])
# Site larger than allowed, re-add wrapper nonce to allow reload # Site larger than allowed, re-add wrapper nonce to allow reload
if site.settings.get("size", 0) > site.getSizeLimit() * 1024 * 1024: if site.settings.get("size", 0) > site.getSizeLimit() * 1024 * 1024:
self.server.wrapper_nonces.append(self.get.get("wrapper_nonce")) self.server.wrapper_nonces.append(self.get.get("wrapper_nonce"))
return self.error404(match.group("inner_path")) return self.error404(path_parts["inner_path"])
else: # Bad url else: # Bad url
return self.error404(path) return self.error404(path)