Please help! Can't open websocket, and Error during WebSocket handshake: Unexpected response code: 400

1,271 views
Skip to first unread message

Ashley Wang

unread,
Aug 7, 2015, 10:11:08 AM8/7/15
to Tornado Web Server
Hi all,

I have a small TornadoServer app that opens websockets as soon as the webpage is loaded. This worked perfectly locally, but once I deployed it to production, the websockets returned the error:
WebSocket connection to 'wss://query2logs.default.abattery.appbattery.nss1.tn.akamai.com/socket/' failed: Error during WebSocket handshake: Unexpected response code: 400

On the server side, in the logs, there's this error: "WARNING:tornado.access:400 GET /socket/ (127.0.0.1) 0.29ms


The code that creates the problem is in a js file, which runs on the client side:

function open_websocket(relative_path) {

var new_uri;

if (location.protocol == "https:") {

new_uri = "wss:";

} else { //could this be dangerous?

new_uri = "ws:";

}

new_uri += "//" + location.host;

if (relative_path.startsWith("/")) {

new_uri += relative_path;

} else {

if (pathname.endsWith("/")) { //NOTE: all my URLs will hit this, but we write both kinds for completeness

new_uri += loc.pathname + relative_path; 

} else {

new_uri += loc.pathname + "/" + relative_path;

}

}

return new WebSocket(new_uri);

}


In the server side, the socket open() method never runs.


Any help would be greatly appreciated! I am completely stuck on this.


Ben Darnell

unread,
Aug 7, 2015, 11:10:15 AM8/7/15
to Tornado Mailing List
Searching for "400" in https://github.com/tornadoweb/tornado/blob/master/tornado/websocket.py shows that this response code is used when the "Upgrade: websocket" or "Connection: upgrade" headers are not present. Is akamai passing these headers along? Does the akamai product/configuration you're using support websockets on this url? 

-Ben

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ashley Wang

unread,
Aug 7, 2015, 11:41:06 AM8/7/15
to Tornado Web Server, b...@bendarnell.com
Hi! Thanks so much!

The error happens before I make a request.

When I do make a request with these headers, the error I get is:  

requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))


One difference is that, in production, the url is a https isntead of http. As a result, I'm trying to open a websocket with 'wss' instead of 'ws'. I didn't think this would be an issue though, and either way I can't change the production url to 'http'.


akamai is passing the headers along and it does support websockets. I've tested it before on other akamai machines and there was no problem.

Ben Darnell

unread,
Aug 7, 2015, 12:07:05 PM8/7/15
to Ashley Wang, Tornado Web Server
On Fri, Aug 7, 2015 at 11:41 AM, Ashley Wang <liah....@gmail.com> wrote:
Hi! Thanks so much!

The error happens before I make a request.

When I do make a request with these headers, the error I get is:  

requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))


You can't use an HTTP client to make a request with these headers; the purpose of these headers is to tell the server that you're using a websocket client instead of an HTTP client. 
 


One difference is that, in production, the url is a https isntead of http. As a result, I'm trying to open a websocket with 'wss' instead of 'ws'. I didn't think this would be an issue though, and either way I can't change the production url to 'http'.


I don't think that's the issue; if it were you wouldn't even be getting a 400 back.
 

akamai is passing the headers along and it does support websockets. I've tested it before on other akamai machines and there was no problem.

Unfortunately I don't have much to add. It looks to me like the problem is somewhere in the akamai layer but I have no idea what's going on there. How do you know that akamai is passing the headers along? Do you have packet captures?

-Ben
Reply all
Reply to author
Forward
0 new messages