4

I'm trying to send a custom header during the handshake. I can intercept the handshake with the ServerEndpointConfig.Configurator and override modifyHandshake.

public class WebsocketConfigurator extends ServerEndpointConfig.Configurator {

    public static final String HEADER_SOCKET_ID = "X-Socket-Id";

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        String id = UUID.randomUUID().toString();

        List<String> list = new ArrayList<String>();
            list.add(id);
            response.getHeaders().put(HEADER_SOCKET_ID, list);

        sec.getUserProperties().put(HEADER_SOCKET_ID, id);  }



}

The method gets called and the id gets added to the response headers, though it doesn't send the additional headers.

I sniffed it with wireshark to make sure it's not the client framework:

GET /websocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8080
Origin: http://localhost:8080
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: QgZwJwVv8+i/vaKFHDqPZg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36

HTTP/1.1 101 Switching Protocols
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: Kim6Qjj7sOBLfG+6I++gS/OVx4A=
Sec-WebSocket-Extensions: permessage-deflate

.u.....0....y.R...K'6...9M
.....PU.......}..).B..g.P....D..4."!..R|._.@..Fp..G....c..0...].D...F.H...{E-.k{.pP...S{n...

I want to send a generated id to the client and don't want to do it in an extra call. Is there a reason why it's not sent or is there a different approach?

I found an example on the dev blog which also adds a header in modifyHandshake so I guess theoretically it should be possible.

Dominic
  • 3,353
  • 36
  • 47
  • Can you please share which version of Tyrus do you use? Also, if its something older than 1.6, can you please re-try with Tyrus 1.6? – Pavel Bucek May 19 '14 at 13:36
  • Sorry, I mistakenly mixed a few things. The [tag:tyrus] tag is inappropriate on this question. I'm using Jetty and their implementation. However your blog post helped me to understand that, if correctly implemented such functionality should be possible. – Dominic May 20 '14 at 09:46
  • 1
    ok. Yes, it should be possible. Following testcase (or at least major part of it) should work on all JSR 356 implementations: https://github.com/tyrus-project/tyrus/blob/master/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/ModifyRequestResponseHeadersTest.java ... if it doesn't, you should file a bug against jetty – Pavel Bucek May 20 '14 at 10:03

1 Answers1

4

I assume it was a bug in Jetty's implementation of Websockets. I was using

Jetty: 9.1.1.v20140108

I briefly looked into the code and it seemed to me that they called modifyHandshake after the response was sent. Though could be totally wrong too.

However upgrading to

 Jetty: 9.2.0.RC0

fixes this issue.

GET /websocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8080
Origin: http://localhost:8080
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: P93FwyXNrD9ecVHCDywzTg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.8 Safari/537.36

HTTP/1.1 101 Switching Protocols
Date: Tue, 20 May 2014 10:58:31 GMT
Connection: Upgrade
Sec-WebSocket-Accept: U+tHV5zYWWSX8vDsVdiDo7yUN9s=
Upgrade: WebSocket
X-Socket-Id: f16bc17b-9a5c-45a4-a851-9d66ffb1d2f1
Dominic
  • 3,353
  • 36
  • 47