socketIO-client icon indicating copy to clipboard operation
socketIO-client copied to clipboard

Unreliable checking of HTTP 200 response string

Open kdizzl opened this issue 11 years ago • 4 comments

In transports.py, there is an assert statement that checks responses for the string "ok":

class XHR_PollingTransport(AbstractTransport):
    ...
    def send_packet(self, engineIO_packet_type, engineIO_packet_data=''):
    ...
            response = get_response(
                self.http_session.post,
                self._http_url,
                params=params,
                data=memoryview(data),
                **self._kw_post)
            assert response.content == b'ok'

This code fails when a 200 status response is returned with the string "OK" instead of "ok". To make the code work with the Flask-SocketIO python package, I had to make the following change:

            assert response.content in [b'ok', b'OK']

However, I believe both versions of the assert statement are redundant because it appears the response status code is already being checked in get_response():

def get_response(request, *args, **kw):
    ...
    status_code = response.status_code
    if 200 != status_code:
        raise ConnectionError('unexpected status code (%s %s)' % (
            status_code, response.text))
    return response

An HTTP 200 "OK" response will pass the check in get_response() but fail the check in send_packet(), but an HTTP 200 "ok" response will pass both checks.

kdizzl avatar Nov 16 '15 23:11 kdizzl

I've just come across this issue as well. The client was complaining the status code wasn't 'ok' yet I could only see 200 response codes from my flask server. I did wonder if it was something as simple as different case...

bodgit avatar Jan 06 '16 12:01 bodgit

Would be great if this fix could be included!

cansik avatar Feb 15 '16 15:02 cansik

I'm also running into this issue using python-socketio as the server. I haven't tested with other servers.

robin-norwood avatar Jun 13 '16 20:06 robin-norwood

@kcgh thank you for reporting this issue in great detail! @bodgit thank you for submitting the fix!

pip install -U socketIO-client

Version 0.6.6 is on PyPI.

invisibleroads avatar Jun 14 '16 16:06 invisibleroads