Starscream: Error: Invalid HTTP upgrade

Created on 28 Sep 2015  路  21Comments  路  Source: daltoniam/Starscream

Hi,

I have random connection problem which display these logs:

websocket is connected
websocket is disconnected: "Invalid HTTP upgrade"

code:

    let useSSL = true
    let websocketScheme = "wss"
    let host = "x.x.x.x"
    let port: NSNumber = 8080

    public init() {
        let components = NSURLComponents()
        components.scheme = websocketScheme
        components.host = host
        components.port = port
        components.path = "/"

        webSocket = WebSocket(url: components.URL!)
        webSocket?.selfSignedSSL = useSSL
        webSocket?.delegate = self
        webSocket?.connect()
    }

It happens on iOS 9.0.1 (iPads).
With swift 1.x and Xcode 6.x everything worked well, but now it randomly failed.
Please give me some suggestion.
Thank you.

Most helpful comment

For what it's worth, I get this http upgrade error constantly when using Starscream in an iOS Simulator in Mac, but my brief testing on an actual iOS device connected to the mac (not simulator) has not seen the error.

All 21 comments

Invalid Upgrade is just the response the server gives back when it couldn't switch the http connection over to WebSocket protocol. Do you have any way I can reproduce this? What server are you using? How often does it "randomly" happen? This doesn't give me much to go off or how to troubleshoot it, so I'm not sure what suggestions I can provide.

We reran the the autobahn suite to ensure compliance. Good news is everything passed. We also tested a Ruby WebSocket server and can't reproduce this issue.

I use this library in my socket.io-client for swift and I've had users complain that it's happening. What's odd is I've gotten complaints that it's happened on a Java based socket.io server and the default node.js server. My first thought was that something behind-the-scenes changed with NSInputStream/NSOutputStream that's causing these random failures. Personally I haven't been able to reproduce on OS X and everyone who's complained is using it on iOS.

Also, I think this is where the actual message is coming from https://github.com/daltoniam/Starscream/blob/master/WebSocket.swift#L324 with the last things changing there being https://github.com/daltoniam/Starscream/blame/master/WebSocket.swift#L322-L324

Thanks @nuclearace I read through the issue on your library. We tried the autobahn (Python) and Ruby based servers on iOS and couldn't reproduce the error either. Agreed. We are reviewing that code now.

I am taking a shot in the dark with the latest checkin. My guess is maybe some people are seeing that:

https://github.com/daltoniam/Starscream/commit/fe2c26b7600d7dc594d8c8823025554d45957fe7#diff-39698dc57a08651b1351d1b254bf1530L322

connected is still set to NO when the next processInput call happens. This would cause the upgrade logic to be run on an already connected socket and disconnect with that error.

@daltoniam Thank you!
It seems to work well!! :100:

Glad to hear it. I created a new version 1.0.1.

I'm getting Invalid HTTP upgrade with this version and I debugged the problem. The server is sending zero content and Starscream can't find the HTTP Packet in the TCP stream.

I checked the TCP frame with Wireshark:

HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Sec-Websocket-Accept: qmefkI5tNjzyqBtCzM7ZeKNTEy4=
Upgrade: web socket
Content-Length: 0

Everything seems fine but somehow the client is cutting the Content-Length: 0.
I converted each buffer byte to char and I notice the difference:
https://gist.github.com/ricardopereira/ac946ea2d8d7f6aa6d71

i am using with gorilla web socket for GoLang. I having this issue of "Invalid HTTP upgrade". Have you guys tried this combination.

@arunsirrpi Yes. We generally use Gorilla WebSocket for all our testing. Are you running it behind a proxy? It sounds like there might be a network config issue.

For what it's worth, I get this http upgrade error constantly when using Starscream in an iOS Simulator in Mac, but my brief testing on an actual iOS device connected to the mac (not simulator) has not seen the error.

Just as an after thought,
Using ws://192.168.1.104:8080 instaed of ws://localhost:8080 solved the issue for me.

If you're testing locally just ifconfig | grep 192. or ifconfig | grep -i 10. (whatever your gateway is) and use the actual local IP address.

@astoeckley pushed me and the right line of thought.

Nice catch @astoeckley 馃檶

I disable AppTransportSecurity from my Info.plist and I can connect WebSocket from simulator. Hope it help.

_[Warning] It just a workaround and not a completely solution. Besides disable ATS in release app is not a good idea too. SO remember to remove before releasing to AppStore._

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Thanks @astoeckley, you just saved me a ton of headache. Glad I found this issue before I started debugging. 馃憤

@astoeckley i am getting the same issue on iOS Device. can you please help me?

@sandipgill Can you check whether you have SockJS enabled server-side? I had to disable it to get Starscream to communicate/upgrade properly.

@craigotis Thanks for your revert but i got my problem its solved. That was header problem.

I had the same problem, and as @craigotis mentioned, I disabled SockJS on the server-side and Starscream was able to connect without any issues. I think Starscream is not compatible with SockJS

I had the same issue, the reason was in bad DNS, when our DNS server lays down we see the error. Fix server - fix error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adrian-niculescu picture adrian-niculescu  路  4Comments

blajivskiy picture blajivskiy  路  6Comments

milanstevanovic picture milanstevanovic  路  5Comments

xjimi picture xjimi  路  4Comments

Dezabro picture Dezabro  路  3Comments