From 51f680271d9ec6ed03198748d64f22891b723ada Mon Sep 17 00:00:00 2001 From: Alexey Diyan Date: Mon, 8 Sep 2014 19:37:58 +0300 Subject: [PATCH 1/6] Flush bad request and disconnect responses --- socketio/handler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/socketio/handler.py b/socketio/handler.py index e4b972f..493b089 100644 --- a/socketio/handler.py +++ b/socketio/handler.py @@ -214,6 +214,7 @@ def handle_bad_request(self): ('Connection', 'close'), ('Content-Length', 0) ]) + self.write(None) def handle_disconnect_request(self): @@ -223,3 +224,4 @@ def handle_disconnect_request(self): ('Connection', 'close'), ('Content-Length', 0) ]) + self.write(None) From b7aec21b7930b4faf32bfcaa662eab6f04209f7b Mon Sep 17 00:00:00 2001 From: Alexey Diyan Date: Mon, 8 Sep 2014 19:45:22 +0300 Subject: [PATCH 2/6] JSONPolling. Do not add JSON padding for responses to HTTP POST --- socketio/transports.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/socketio/transports.py b/socketio/transports.py index d4b7ab2..8831f72 100644 --- a/socketio/transports.py +++ b/socketio/transports.py @@ -173,15 +173,18 @@ def _request_body(self): data = data.decode("unicode_escape").encode("utf-8") return data - def write(self, data): - """Just quote out stuff before sending it out""" - args = urlparse.parse_qs(self.handler.environ.get("QUERY_STRING")) - if "i" in args: - i = args["i"] + def write(self, data=""): + """Do not add JSON padding for responses to HTTP POST""" + if self.handler.environ.get('REQUEST_METHOD') == 'POST': + super(JSONPolling, self).write(data) else: - i = "0" - # TODO: don't we need to quote this data in here ? - super(JSONPolling, self).write("io.j[%s]('%s');" % (i, data)) + args = urlparse.parse_qs(self.handler.environ.get("QUERY_STRING")) + if "i" in args: + i = args["i"] + else: + i = "0" + # TODO: don't we need to quote this data in here ? + super(JSONPolling, self).write("io.j[%s]('%s');" % (i, data)) class XHRMultipartTransport(XHRPollingTransport): From 72310cd52923bf297fbc990f62b5f65bba15dcc8 Mon Sep 17 00:00:00 2001 From: Alexey Diyan Date: Mon, 8 Sep 2014 20:12:12 +0300 Subject: [PATCH 3/6] JSONPolling. Write Content-Type application/javascript instead of text/plain --- socketio/handler.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/socketio/handler.py b/socketio/handler.py index 493b089..5b23edd 100644 --- a/socketio/handler.py +++ b/socketio/handler.py @@ -63,11 +63,14 @@ def _do_handshake(self, tokens): ",".join(self.transports)) self.write_smart(data) - def write_jsonp_result(self, data, wrapper="0"): + def write_jsonp_result(self, data, wrapper=None): self.start_response("200 OK", [ ("Content-Type", "application/javascript"), ]) - self.result = ['io.j[%s]("%s");' % (wrapper, data)] + if wrapper: + self.result = ['io.j[%s]("%s");' % (wrapper, data)] + else: + self.result = [data] def write_plain_result(self, data): self.start_response("200 OK", [ @@ -84,6 +87,8 @@ def write_smart(self, data): if "jsonp" in args: self.write_jsonp_result(data, args["jsonp"][0]) + elif data.startswith('io.j['): + self.write_jsonp_result(data) else: self.write_plain_result(data) From 8df9a50ffc7c8da7cb519e759598e9a0d10a7a1f Mon Sep 17 00:00:00 2001 From: Alexey Diyan Date: Wed, 10 Sep 2014 19:40:54 +0300 Subject: [PATCH 4/6] JSONPolling. Encode packet string for JSONP response --- socketio/transports.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/socketio/transports.py b/socketio/transports.py index 8831f72..90903f8 100644 --- a/socketio/transports.py +++ b/socketio/transports.py @@ -1,3 +1,4 @@ +import json import gevent import urllib import urlparse @@ -183,8 +184,7 @@ def write(self, data=""): i = args["i"] else: i = "0" - # TODO: don't we need to quote this data in here ? - super(JSONPolling, self).write("io.j[%s]('%s');" % (i, data)) + super(JSONPolling, self).write("io.j[%s](%s);" % (i, json.dumps(data))) class XHRMultipartTransport(XHRPollingTransport): From f89e7b07e64bf9788b6d0d29613724d9771504f1 Mon Sep 17 00:00:00 2001 From: Alexey Diyan Date: Wed, 10 Sep 2014 21:32:35 +0300 Subject: [PATCH 5/6] Travis. Fix nose package name --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 18c6aa7..b583c3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ python: before_install: "sudo apt-get install python-dev libevent-dev" # command to install dependencies -install: pip install nosetests; python setup.py install +install: pip install nose; python setup.py install # command to run tests script: nosetests From 8e09e2957e19042b2361827df90c93fd9499c5df Mon Sep 17 00:00:00 2001 From: Alexey Diyan Date: Wed, 10 Sep 2014 22:15:59 +0300 Subject: [PATCH 6/6] Travis. Workaround issue in gevent-websocket's setup.py --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b583c3d..23d1d9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,9 @@ python: before_install: "sudo apt-get install python-dev libevent-dev" # command to install dependencies -install: pip install nose; python setup.py install +# NOTE gevent-websocket's install_requires equals to a string instead of tuple, +# fortunately "pip install" can handle this while "setup.py install" can not +install: pip install nose gevent-websocket; python setup.py install # command to run tests script: nosetests