Starscream: "Invalid HTTP upgrade" error after updating to 3 version of Starscream

Created on 28 Sep 2017  ·  11Comments  ·  Source: daltoniam/Starscream

After updating to Starscream 3 version I cannot connect to web socket and receiving this error:

Error Domain=WebSocket Code=301 "Invalid HTTP upgrade" UserInfo={NSLocalizedDescription=Invalid HTTP upgrade}

Most helpful comment

Version 3.0.2 has been released with the fix for this.

All 11 comments

Invalid upgrade comes from the server when it fails to upgrade the HTTP/TCP connection to a WebSocket connection. I would double check your url, your headers as the API did change a little bit for the 3.0.0 release (https://github.com/daltoniam/Starscream#custom-headers). If that looks correct, I would suggest a packet capture to see what the differences are. Also be sure to check your Origin header, as most WebSocket servers will reject any connection that doesn't match. Your error code is a 301, which is an HTTP redirect and could be an issue with setting up your connection.

I printed all http request and there is a difference:
Starscream version 2.0.4:

"GET /ws/ HTTP/1.1
Host: a*.net:443
Sec-WebSocket-Version: 13
Upgrade: websocket
Connection: Upgrade
Origin: wss://a
*.net
Sec-WebSocket-Key: aWd1eHFqYnJwdmJ4Z3BsZw==

"
Starscream version 3.0.1:
"GET /ws HTTP/1.1
Connection: Upgrade
Host: a*.net:443
Origin: wss://a
*.net
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits; server_max_window_bits=15
Sec-WebSocket-Key: b2NvdGNwZ2RwaXdieXlwag==

"

If I put "wss://a*.net/ws//" instead of "wss://ap*.net/ws/" it will work

My server rejects requests request-Line "GET /ws HTTP/1.1" instead of "GET /ws/ HTTP/1.1"

Is this a bug on your side or my server should answer to /ws?

This sounds similar to #394

@nuclearace I agree, probably the same issue.
@ChristRm what server are you using?

I might just add code to append the / if it doesn't exist since that is how it use to work (even though the RFC doesn't require it, some servers must?).

@daltoniam The server has nginx in front of it, it takes care of ssl termination & routing
https://github.com/uNetworking/uWebSockets - and this one is using for webSockets

Fix commit works for me. Would you make a new version of lib?

Version 3.0.2 has been released with the fix for this.

Thanks!

hi @daltoniam after I upgraded my version of starscream to 3.1.0
Im experiencing this error
"Invalid HTTP upgrade”, code 503.

Using xcode 10.2, swift 5, apollo, apollo/websocket , please help, thanks

When do you have this issue? Can you show an example? Thanks.

Hi Kristaps

First of all thank you for your response,
here how I get the error,

scenarios

  1. First I call the subscribeNewMessageAdded function to initiate subscription to websocket server
  2. inside the subscribeNewMessageAdded function,

    • I instantiate my apollo websocket client

    • intantiate subscription graph values

    • call subcribe function of websocket and catch the completion result and error

Code:
func subscribeNewMessageAdded(userId:String) {
// instantiate websocket
let websocket = instantiateApolloWebSocketClient()

//intantiate subscription graph values 
    let newAddedMessageGraph = NewMessageAddedSubscription(userId: userId)

//call subcribe function to websocket server and the the completion result and error 
    websocket.subscribe(subscription: newAddedMessageGraph) { (result, error) in

        if let error  = error{
                // handle error here
       //Here I received this error :  “WSError(type: Starscream.ErrorType.upgradeError, message: “Invalid HTTP upgrade”, code: 503)”
        }else{

           //handle success result here
        }
    }
}

// instantiate apollo websocket client
func instantiateApolloWebSocketClient() -> ApolloClient{
let apolloAccount: ApolloClient = {

     if let rUserId = UserDefaults.standard.object(forKey: "userId") as? String {
            userId = rUserId
        }
            let map:GraphQLMap = [
                "treat": "\(kTreat)",
                "spoil": "\(kSpoil)",
                "slave": "\(slave)",
                "userId":"\(userId)"]

            let wsEndpointURL = URL(string: kEndPointWebSocket)!
            var request =  URLRequest(url: wsEndpointURL)
            request.timeoutInterval = 3000
            let webSocket = WebSocketTransport(request: request, connectingPayload: map)
            return ApolloClient(networkTransport: webSocket)
    }()
    return apolloAccount
}

This code is working before i upgraded my version to latest, I might need to add some code but I have hard time finding references in the internet for this issue, hope you can help, thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NickNeedsAName picture NickNeedsAName  ·  5Comments

dessmith picture dessmith  ·  5Comments

adrian-niculescu picture adrian-niculescu  ·  4Comments

felipeflorencio picture felipeflorencio  ·  4Comments

nerzh picture nerzh  ·  6Comments