Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush bad request and disconnect responses #201

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions socketio/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [
Expand All @@ -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)

Expand Down Expand Up @@ -214,6 +219,7 @@ def handle_bad_request(self):
('Connection', 'close'),
('Content-Length', 0)
])
self.write(None)


def handle_disconnect_request(self):
Expand All @@ -223,3 +229,4 @@ def handle_disconnect_request(self):
('Connection', 'close'),
('Content-Length', 0)
])
self.write(None)
19 changes: 11 additions & 8 deletions socketio/transports.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import gevent
import urllib
import urlparse
Expand Down Expand Up @@ -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):
Expand Down