diff --git a/.travis.yml b/.travis.yml index 18c6aa7..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 nosetests; 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 diff --git a/socketio/handler.py b/socketio/handler.py index e4b972f..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) @@ -214,6 +219,7 @@ def handle_bad_request(self): ('Connection', 'close'), ('Content-Length', 0) ]) + self.write(None) def handle_disconnect_request(self): @@ -223,3 +229,4 @@ def handle_disconnect_request(self): ('Connection', 'close'), ('Content-Length', 0) ]) + self.write(None) diff --git a/socketio/transports.py b/socketio/transports.py index d4b7ab2..90903f8 100644 --- a/socketio/transports.py +++ b/socketio/transports.py @@ -1,3 +1,4 @@ +import json import gevent import urllib import urlparse @@ -173,15 +174,17 @@ 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" + super(JSONPolling, self).write("io.j[%s](%s);" % (i, json.dumps(data))) class XHRMultipartTransport(XHRPollingTransport):