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) lg(s)
return None, dict(type="error", msg=s) return None, dict(type="error", msg=s)
m = utils.MemBool()
websocket_key = websockets.check_client_handshake(headers)
self.settings.websocket_key = websocket_key
# 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: for i in self.server.anchors:
if i[0].match(path): if i[0].match(path):
lg("crafting anchor: %s" % path) anchor_spec = i[1]
nexthandler, retlog["response"] = self.serve_crafted(i[1]) break
return nexthandler, retlog else:
if m(self.server.craftanchor.match(path)):
spec = urllib.unquote(path)[len(m.v.group()):]
if spec:
try:
anchor_spec = language.parse_pathod(spec)
except language.ParseException as v:
lg("Parse error: %s" % v.msg)
anchor_spec = language.http.make_error_response(
"Parse Error",
"Error parsing response spec: %s\n" % (
v.msg + v.marked()
)
)
m = utils.MemBool() if anchor_spec:
if m(self.server.craftanchor.match(path)): lg("crafting spec: %s" % anchor_spec)
spec = urllib.unquote(path)[len(m.v.group()):] nexthandler, retlog["response"] = self.serve_crafted(
websocket_key = websockets.check_client_handshake(headers) anchor_spec
self.settings.websocket_key = websocket_key )
if websocket_key and not spec:
spec = "ws"
lg("crafting spec: %s" % spec)
try:
crafted = language.parse_pathod(spec)
except language.ParseException as v:
lg("Parse error: %s" % v.msg)
crafted = language.http.make_error_response(
"Parse Error",
"Error parsing response spec: %s\n" % v.msg + v.marked()
)
nexthandler, retlog["response"] = self.serve_crafted(crafted)
if nexthandler and websocket_key: if nexthandler and websocket_key:
return self.handle_websocket, retlog return self.handle_websocket, retlog
else: else: