Okhttp: WebSocket sends Connection: "close" instead of "upgrade" on HTTPS

Created on 5 Feb 2018  Â·  5Comments  Â·  Source: square/okhttp

What kind of issue is this?

  • [x] Question. This issue tracker is not the place for questions. If you want to ask how to do
    something, or to understand why something isn't working the way you expect it to, use Stack
    Overflow. https://stackoverflow.com/questions/tagged/okhttp

  • [ ] Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests
    get fixed. Here’s an example: https://gist.github.com/swankjesse/981fcae102f513eb13ed

  • [ ] Feature Request. Start by telling us what problem you’re trying to solve. Often a solution
    already exists! Don’t send pull requests to implement new features without first getting our
    support. Sometimes we leave features out on purpose to keep the project small.

I'm using OKHTTP 3.9.1 and WebSocket always fails on https. So I printed the packet on server, and seems that for https, connection is close instead of upgrade. Same code runs well on http server (actually my https is under nginx it's the same service on different ports). I noticed that there's some "https upgrade problem" mentioned in okhttp-3.9.1 release notes, but not sure about the details. Any configuration needs to be done or anything please?

Most helpful comment

It looks like misconfigured Ngnix. Make sure your location entry
explicitly sets Upgrade and Connection headers. Both values can be
either static or dynamic depending on requirements.

See this article https://www.nginx.com/blog/websocket-nginx/ and
documentation https://nginx.org/en/docs/http/websocket.html for more info.

I'm using OKHTTP 3.9.1

According to pasted headers you're using OkHttp 3.8.0

On Mon, Feb 5, 2018, 9:37 PM Leo notifications@github.com wrote:

I've printed headers from server.

http:

[authorization: "Bearer 3gMQkpHJ02iS/bgfMDwTtw==", Sec-WebSocket-Version: "13", Connection: "Upgrade", User-Agent: "okhttp/3.8.0", Upgrade: "websocket", Sec-WebSocket-Key: "+0jpaxSfa1/nnBZG5/VKmw==", Accept-Encoding: "gzip", Host: "10.0.2.2:8080"]

https:

[X-Forwarded-For: "96.246.149.227", Sec-WebSocket-Version: "13", authorization: "Bearer 3gMQkpHJ02iS/bgfMDwTtw==", Connection: "close", Sec-WebSocket-Key: "J5q07bjo43UGwhxxxx+SHw==", User-Agent: "okhttp/3.8.0", X-Real-IP: "96.246.149.227", Accept-Encoding: "gzip", Host: "api.bxxo.live"]

Again, I'm not familiar with WebSocket so maybe I'm missing some basic
things?

My Android code:

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(Const.socketCast).build()
            .newBuilder().addHeader("authorization", UserRepository.mapToken.get("Authorization").toString()).build();
    socket = client.newWebSocket(request, listener);

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/okhttp/issues/3828#issuecomment-363316331, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA7B3IeWtIrpAcVoZGiRz1aJJZll6_Qaks5tR-UFgaJpZM4R6Eqc
.

All 5 comments

I don't think the Connection: close header is coming from OkHttp.

I've printed headers from server.

http:

[authorization: "Bearer 3gMQkpHJ02iS/bgfMDwTtw==", Sec-WebSocket-Version: "13", Connection: "Upgrade", User-Agent: "okhttp/3.8.0", Upgrade: "websocket", Sec-WebSocket-Key: "+0jpaxSfa1/nnBZG5/VKmw==", Accept-Encoding: "gzip", Host: "10.0.2.2:8080"]

https:

[X-Forwarded-For: "96.246.149.227", Sec-WebSocket-Version: "13", authorization: "Bearer 3gMQkpHJ02iS/bgfMDwTtw==", Connection: "close", Sec-WebSocket-Key: "J5q07bjo43UGwhxxxx+SHw==", User-Agent: "okhttp/3.8.0", X-Real-IP: "96.246.149.227", Accept-Encoding: "gzip", Host: "api.bxxo.live"]

Again, I'm not familiar with WebSocket so maybe I'm missing some basic things?

My Android code:

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(Const.socketCast).build()
                .newBuilder().addHeader("authorization", UserRepository.mapToken.get("Authorization").toString()).build();
        socket = client.newWebSocket(request, listener);

It looks like misconfigured Ngnix. Make sure your location entry
explicitly sets Upgrade and Connection headers. Both values can be
either static or dynamic depending on requirements.

See this article https://www.nginx.com/blog/websocket-nginx/ and
documentation https://nginx.org/en/docs/http/websocket.html for more info.

I'm using OKHTTP 3.9.1

According to pasted headers you're using OkHttp 3.8.0

On Mon, Feb 5, 2018, 9:37 PM Leo notifications@github.com wrote:

I've printed headers from server.

http:

[authorization: "Bearer 3gMQkpHJ02iS/bgfMDwTtw==", Sec-WebSocket-Version: "13", Connection: "Upgrade", User-Agent: "okhttp/3.8.0", Upgrade: "websocket", Sec-WebSocket-Key: "+0jpaxSfa1/nnBZG5/VKmw==", Accept-Encoding: "gzip", Host: "10.0.2.2:8080"]

https:

[X-Forwarded-For: "96.246.149.227", Sec-WebSocket-Version: "13", authorization: "Bearer 3gMQkpHJ02iS/bgfMDwTtw==", Connection: "close", Sec-WebSocket-Key: "J5q07bjo43UGwhxxxx+SHw==", User-Agent: "okhttp/3.8.0", X-Real-IP: "96.246.149.227", Accept-Encoding: "gzip", Host: "api.bxxo.live"]

Again, I'm not familiar with WebSocket so maybe I'm missing some basic
things?

My Android code:

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(Const.socketCast).build()
            .newBuilder().addHeader("authorization", UserRepository.mapToken.get("Authorization").toString()).build();
    socket = client.newWebSocket(request, listener);

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/okhttp/issues/3828#issuecomment-363316331, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA7B3IeWtIrpAcVoZGiRz1aJJZll6_Qaks5tR-UFgaJpZM4R6Eqc
.

Thanks so much! Yes it's an Ngnix issue. Sorry for this noob question...

Great! Don't forget to update OkHttp :)

Was this page helpful?
0 / 5 - 0 ratings