make relay work under py3
Current twisted.web wants bytes in most places (this will probably change when twisted.web is properly ported to py3).
This commit is contained in:
parent
15cc0a1429
commit
6614783c43
|
@ -26,24 +26,24 @@ class EventsProtocol:
|
||||||
# face of firewall/NAT timeouts. It also helps unit tests, since
|
# face of firewall/NAT timeouts. It also helps unit tests, since
|
||||||
# apparently twisted.web.client.Agent doesn't consider the connection
|
# apparently twisted.web.client.Agent doesn't consider the connection
|
||||||
# to be established until it sees the first byte of the reponse body.
|
# to be established until it sees the first byte of the reponse body.
|
||||||
self.request.write(": %s\n\n" % comment)
|
self.request.write(b": %s\n\n" % comment)
|
||||||
|
|
||||||
def sendEvent(self, data, name=None, id=None, retry=None):
|
def sendEvent(self, data, name=None, id=None, retry=None):
|
||||||
if name:
|
if name:
|
||||||
self.request.write("event: %s\n" % name.encode("utf-8"))
|
self.request.write(b"event: %s\n" % name.encode("utf-8"))
|
||||||
# e.g. if name=foo, then the client web page should do:
|
# e.g. if name=foo, then the client web page should do:
|
||||||
# (new EventSource(url)).addEventListener("foo", handlerfunc)
|
# (new EventSource(url)).addEventListener("foo", handlerfunc)
|
||||||
# Note that this basically defaults to "message".
|
# Note that this basically defaults to "message".
|
||||||
self.request.write("\n")
|
self.request.write(b"\n")
|
||||||
if id:
|
if id:
|
||||||
self.request.write("id: %s\n" % id.encode("utf-8"))
|
self.request.write(b"id: %s\n" % id.encode("utf-8"))
|
||||||
self.request.write("\n")
|
self.request.write(b"\n")
|
||||||
if retry:
|
if retry:
|
||||||
self.request.write("retry: %d\n" % retry) # milliseconds
|
self.request.write(b"retry: %d\n" % retry) # milliseconds
|
||||||
self.request.write("\n")
|
self.request.write(b"\n")
|
||||||
for line in data.splitlines():
|
for line in data.splitlines():
|
||||||
self.request.write("data: %s\n" % line.encode("utf-8"))
|
self.request.write(b"data: %s\n" % line.encode("utf-8"))
|
||||||
self.request.write("\n")
|
self.request.write(b"\n")
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.request.finish()
|
self.request.finish()
|
||||||
|
@ -72,15 +72,15 @@ class Channel(resource.Resource):
|
||||||
|
|
||||||
def render_GET(self, request):
|
def render_GET(self, request):
|
||||||
# rest of URL is: SIDE/poll/MSGNUM
|
# rest of URL is: SIDE/poll/MSGNUM
|
||||||
their_side = request.postpath[0]
|
their_side = request.postpath[0].decode("utf-8")
|
||||||
if request.postpath[1] != "poll":
|
if request.postpath[1] != b"poll":
|
||||||
request.setResponseCode(http.BAD_REQUEST, "GET to wrong URL")
|
request.setResponseCode(http.BAD_REQUEST, b"GET to wrong URL")
|
||||||
return "GET is only for /SIDE/poll/MSGNUM"
|
return b"GET is only for /SIDE/poll/MSGNUM"
|
||||||
their_msgnum = request.postpath[2]
|
their_msgnum = request.postpath[2].decode("utf-8")
|
||||||
if "text/event-stream" not in (request.getHeader("accept") or ""):
|
if b"text/event-stream" not in (request.getHeader(b"accept") or b""):
|
||||||
request.setResponseCode(http.BAD_REQUEST, "Must use EventSource")
|
request.setResponseCode(http.BAD_REQUEST, b"Must use EventSource")
|
||||||
return "Must use EventSource (Content-Type: text/event-stream)"
|
return b"Must use EventSource (Content-Type: text/event-stream)"
|
||||||
request.setHeader("content-type", "text/event-stream; charset=utf-8")
|
request.setHeader(b"content-type", b"text/event-stream; charset=utf-8")
|
||||||
ep = EventsProtocol(request)
|
ep = EventsProtocol(request)
|
||||||
ep.sendEvent(json.dumps(self.welcome), name="welcome")
|
ep.sendEvent(json.dumps(self.welcome), name="welcome")
|
||||||
handle = (their_side, their_msgnum, ep)
|
handle = (their_side, their_msgnum, ep)
|
||||||
|
@ -107,20 +107,20 @@ class Channel(resource.Resource):
|
||||||
|
|
||||||
def render_POST(self, request):
|
def render_POST(self, request):
|
||||||
# rest of URL is: SIDE/(MSGNUM|deallocate)/(post|poll)
|
# rest of URL is: SIDE/(MSGNUM|deallocate)/(post|poll)
|
||||||
side = request.postpath[0]
|
side = request.postpath[0].decode("utf-8")
|
||||||
verb = request.postpath[1]
|
verb = request.postpath[1].decode("utf-8")
|
||||||
|
|
||||||
if verb == "deallocate":
|
if verb == "deallocate":
|
||||||
deleted = self.relay.maybe_free_child(self.channel_id, side)
|
deleted = self.relay.maybe_free_child(self.channel_id, side)
|
||||||
if deleted:
|
if deleted:
|
||||||
return "deleted\n"
|
return b"deleted\n"
|
||||||
return "waiting\n"
|
return b"waiting\n"
|
||||||
|
|
||||||
if verb not in ("post", "poll"):
|
if verb not in ("post", "poll"):
|
||||||
request.setResponseCode(http.BAD_REQUEST)
|
request.setResponseCode(http.BAD_REQUEST)
|
||||||
return "bad verb, want 'post' or 'poll'\n"
|
return b"bad verb, want 'post' or 'poll'\n"
|
||||||
|
|
||||||
msgnum = request.postpath[2]
|
msgnum = request.postpath[2].decode("utf-8")
|
||||||
|
|
||||||
other_messages = []
|
other_messages = []
|
||||||
for row in self.db.execute("SELECT `message` FROM `messages`"
|
for row in self.db.execute("SELECT `message` FROM `messages`"
|
||||||
|
@ -131,7 +131,9 @@ class Channel(resource.Resource):
|
||||||
other_messages.append(row["message"])
|
other_messages.append(row["message"])
|
||||||
|
|
||||||
if verb == "post":
|
if verb == "post":
|
||||||
data = json.load(request.content)
|
#data = json.load(request.content, encoding="utf-8")
|
||||||
|
content = request.content.read()
|
||||||
|
data = json.loads(content.decode("utf-8"))
|
||||||
self.db.execute("INSERT INTO `messages`"
|
self.db.execute("INSERT INTO `messages`"
|
||||||
" (`channel_id`, `side`, `msgnum`, `message`, `when`)"
|
" (`channel_id`, `side`, `msgnum`, `message`, `when`)"
|
||||||
" VALUES (?,?,?,?,?)",
|
" VALUES (?,?,?,?,?)",
|
||||||
|
@ -144,9 +146,10 @@ class Channel(resource.Resource):
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
self.message_added(side, msgnum, data["message"])
|
self.message_added(side, msgnum, data["message"])
|
||||||
|
|
||||||
request.setHeader("content-type", "application/json; charset=utf-8")
|
request.setHeader(b"content-type", b"application/json; charset=utf-8")
|
||||||
return json.dumps({"welcome": self.welcome,
|
data = {"welcome": self.welcome,
|
||||||
"messages": other_messages})+"\n"
|
"messages": other_messages}
|
||||||
|
return (json.dumps(data)+"\n").encode("utf-8")
|
||||||
|
|
||||||
def get_allocated(db):
|
def get_allocated(db):
|
||||||
c = db.execute("SELECT DISTINCT `channel_id` FROM `allocations`")
|
c = db.execute("SELECT DISTINCT `channel_id` FROM `allocations`")
|
||||||
|
@ -183,9 +186,10 @@ class Allocator(resource.Resource):
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
log.msg("allocated #%d, now have %d DB channels" %
|
log.msg("allocated #%d, now have %d DB channels" %
|
||||||
(channel_id, len(get_allocated(self.db))))
|
(channel_id, len(get_allocated(self.db))))
|
||||||
request.setHeader("content-type", "application/json; charset=utf-8")
|
request.setHeader(b"content-type", b"application/json; charset=utf-8")
|
||||||
return json.dumps({"welcome": self.welcome,
|
data = {"welcome": self.welcome,
|
||||||
"channel-id": channel_id})+"\n"
|
"channel-id": channel_id}
|
||||||
|
return (json.dumps(data)+"\n").encode("utf-8")
|
||||||
|
|
||||||
class ChannelList(resource.Resource):
|
class ChannelList(resource.Resource):
|
||||||
def __init__(self, db, welcome):
|
def __init__(self, db, welcome):
|
||||||
|
@ -195,9 +199,10 @@ class ChannelList(resource.Resource):
|
||||||
def render_GET(self, request):
|
def render_GET(self, request):
|
||||||
c = self.db.execute("SELECT DISTINCT `channel_id` FROM `allocations`")
|
c = self.db.execute("SELECT DISTINCT `channel_id` FROM `allocations`")
|
||||||
allocated = sorted(set([row["channel_id"] for row in c.fetchall()]))
|
allocated = sorted(set([row["channel_id"] for row in c.fetchall()]))
|
||||||
request.setHeader("content-type", "application/json; charset=utf-8")
|
request.setHeader(b"content-type", b"application/json; charset=utf-8")
|
||||||
return json.dumps({"welcome": self.welcome,
|
data = {"welcome": self.welcome,
|
||||||
"channel-ids": allocated})+"\n"
|
"channel-ids": allocated}
|
||||||
|
return (json.dumps(data)+"\n").encode("utf-8")
|
||||||
|
|
||||||
class Relay(resource.Resource):
|
class Relay(resource.Resource):
|
||||||
def __init__(self, db, welcome):
|
def __init__(self, db, welcome):
|
||||||
|
@ -207,11 +212,11 @@ class Relay(resource.Resource):
|
||||||
self.channels = {}
|
self.channels = {}
|
||||||
|
|
||||||
def getChild(self, path, request):
|
def getChild(self, path, request):
|
||||||
if path == "allocate":
|
if path == b"allocate":
|
||||||
return Allocator(self.db, self.welcome)
|
return Allocator(self.db, self.welcome)
|
||||||
if path == "list":
|
if path == b"list":
|
||||||
return ChannelList(self.db, self.welcome)
|
return ChannelList(self.db, self.welcome)
|
||||||
if not re.search(r'^\d+$', path):
|
if not re.search(br'^\d+$', path):
|
||||||
return resource.ErrorPage(http.BAD_REQUEST,
|
return resource.ErrorPage(http.BAD_REQUEST,
|
||||||
"invalid channel id",
|
"invalid channel id",
|
||||||
"invalid channel id")
|
"invalid channel id")
|
||||||
|
@ -381,7 +386,7 @@ class Root(resource.Resource):
|
||||||
# child_FOO is a nevow thing, not a twisted.web.resource thing
|
# child_FOO is a nevow thing, not a twisted.web.resource thing
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
resource.Resource.__init__(self)
|
resource.Resource.__init__(self)
|
||||||
self.putChild("", static.Data("Wormhole Relay\n", "text/plain"))
|
self.putChild(b"", static.Data(b"Wormhole Relay\n", "text/plain"))
|
||||||
|
|
||||||
class RelayServer(service.MultiService):
|
class RelayServer(service.MultiService):
|
||||||
def __init__(self, relayport, transitport, advertise_version,
|
def __init__(self, relayport, transitport, advertise_version,
|
||||||
|
@ -405,7 +410,7 @@ class RelayServer(service.MultiService):
|
||||||
self.relayport_service = EndpointServerService(r, site)
|
self.relayport_service = EndpointServerService(r, site)
|
||||||
self.relayport_service.setServiceParent(self)
|
self.relayport_service.setServiceParent(self)
|
||||||
self.relay = Relay(self.db, welcome) # accessible from tests
|
self.relay = Relay(self.db, welcome) # accessible from tests
|
||||||
self.root.putChild("wormhole-relay", self.relay)
|
self.root.putChild(b"wormhole-relay", self.relay)
|
||||||
t = internet.TimerService(EXPIRATION_CHECK_PERIOD,
|
t = internet.TimerService(EXPIRATION_CHECK_PERIOD,
|
||||||
self.relay.prune_old_channels)
|
self.relay.prune_old_channels)
|
||||||
t.setServiceParent(self)
|
t.setServiceParent(self)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user