MCPcopy
hub / github.com/HelloZeroNet/ZeroNet / route

Method route

src/Ui/UiRequest.py:80–172  ·  view source on GitHub ↗
(self, path)

Source from the content-addressed store, hash-verified

78
79 # Call the request handler function base on path
80 def route(self, path):
81 # Restict Ui access by ip
82 if config.ui_restrict and self.env['REMOTE_ADDR'] not in config.ui_restrict:
83 return self.error403(details=False)
84
85 # Check if host allowed to do request
86 if not self.isHostAllowed(self.env.get("HTTP_HOST")):
87 ret_error = next(self.error403("Invalid host: %s" % self.env.get("HTTP_HOST"), details=False))
88
89 http_get = self.env["PATH_INFO"]
90 if self.env["QUERY_STRING"]:
91 http_get += "?{0}".format(self.env["QUERY_STRING"])
92 self_host = self.env["HTTP_HOST"].split(":")[0]
93 self_ip = self.env["HTTP_HOST"].replace(self_host, socket.gethostbyname(self_host))
94 link = "http://{0}{1}".format(self_ip, http_get)
95 ret_link = """<h4>Access via ip: <a href="{0}">{0}</a>""".format(html.escape(link)).encode("utf8")
96 return iter([ret_error, ret_link])
97
98 # Prepend .bit host for transparent proxy
99 if self.server.site_manager.isDomain(self.env.get("HTTP_HOST")):
100 path = re.sub("^/", "/" + self.env.get("HTTP_HOST") + "/", path)
101 path = re.sub("^http://zero[/]+", "/", path) # Remove begining http://zero/ for chrome extension
102 path = re.sub("^http://", "/", path) # Remove begining http for chrome extension .bit access
103
104 # Sanitize request url
105 path = path.replace("\\", "/")
106 if "../" in path or "./" in path:
107 return self.error403("Invalid path: %s" % path)
108
109 if self.env["REQUEST_METHOD"] == "OPTIONS":
110 if "/" not in path.strip("/"):
111 content_type = self.getContentType("index.html")
112 else:
113 content_type = self.getContentType(path)
114
115 extra_headers = {"Access-Control-Allow-Origin": "null"}
116
117 self.sendHeader(content_type=content_type, extra_headers=extra_headers, noscript=True)
118 return ""
119
120 if path == "/":
121 return self.actionIndex()
122 elif path == "/favicon.ico":
123 return self.actionFile("src/Ui/media/img/favicon.ico")
124 # Internal functions
125 elif "/ZeroNet-Internal/" in path:
126 path = re.sub(".*?/ZeroNet-Internal/", "/", path)
127 func = getattr(self, "action" + path.strip("/"), None) # Check if we have action+request_path function
128 if func:
129 return func()
130 else:
131 return self.error404(path)
132 # Media
133 elif path.startswith("/uimedia/"):
134 return self.actionUiMedia(path)
135 elif "/uimedia/" in path:
136 # uimedia within site dir (for chrome extension)
137 path = re.sub(".*?/uimedia/", "/uimedia/", path)

Callers 2

handleRequestMethod · 0.95
all.jsFile · 0.45

Calls 15

error403Method · 0.95
isHostAllowedMethod · 0.95
getContentTypeMethod · 0.95
sendHeaderMethod · 0.95
actionIndexMethod · 0.95
actionFileMethod · 0.95
error404Method · 0.95
actionUiMediaMethod · 0.95
actionWebsocketMethod · 0.95
actionDebugMethod · 0.95
actionConsoleMethod · 0.95
actionSiteMediaMethod · 0.95

Tested by

no test coverage detected