Refactor to allow dropping to websockets on any anchor point

This commit is contained in:
Aldo Cortesi 2015-06-04 20:23:05 +12:00
parent fb1b9113a1
commit 182b79ab9e

View File

@ -243,29 +243,40 @@ class PathodHandler(tcp.BaseHandler):
lg(s)
return None, dict(type="error", msg=s)
for i in self.server.anchors:
if i[0].match(path):
lg("crafting anchor: %s" % path)
nexthandler, retlog["response"] = self.serve_crafted(i[1])
return nexthandler, retlog
m = utils.MemBool()
if m(self.server.craftanchor.match(path)):
spec = urllib.unquote(path)[len(m.v.group()):]
websocket_key = websockets.check_client_handshake(headers)
self.settings.websocket_key = websocket_key
if websocket_key and not spec:
spec = "ws"
lg("crafting spec: %s" % spec)
# If this is a websocket initiation, we respond with a proper
# server response, unless over-ridden.
if websocket_key:
anchor_spec = language.parse_pathod("ws")
else:
anchor_spec = None
for i in self.server.anchors:
if i[0].match(path):
anchor_spec = i[1]
break
else:
if m(self.server.craftanchor.match(path)):
spec = urllib.unquote(path)[len(m.v.group()):]
if spec:
try:
crafted = language.parse_pathod(spec)
anchor_spec = language.parse_pathod(spec)
except language.ParseException as v:
lg("Parse error: %s" % v.msg)
crafted = language.http.make_error_response(
anchor_spec = language.http.make_error_response(
"Parse Error",
"Error parsing response spec: %s\n" % v.msg + v.marked()
"Error parsing response spec: %s\n" % (
v.msg + v.marked()
)
)
if anchor_spec:
lg("crafting spec: %s" % anchor_spec)
nexthandler, retlog["response"] = self.serve_crafted(
anchor_spec
)
nexthandler, retlog["response"] = self.serve_crafted(crafted)
if nexthandler and websocket_key:
return self.handle_websocket, retlog
else: