vertx3: Writing WebSocket Response Headers

503 views
Skip to first unread message

Christopher Paul Burke

unread,
Aug 19, 2016, 7:26:31 PM8/19/16
to vert.x
Hello Vertx3 Team!

Thank you for the great work. I have a perhaps small question; I've noticed others have brought up similar topics in 2013 but I couldn't see a resolution.

Please refer to subprotocols (behind this link). I am currently implementing a server where the client is sending the Sec-WebSocket-Protocol header to the server, and in order to not have the client immediately disconnect I must acknowledge the protocol requested of the client is correct (by responding with the appropriate protocol). After review of the ServerWebSocket interface I can't see any method where I can write a response back to the client; rather I can only reject a connection based on it's path. 

Am I missing something obvious? 

Thanks,

Chris

Julien Viet

unread,
Aug 21, 2016, 12:40:27 PM8/21/16
to ve...@googlegroups.com
Hi,

I haven’t read all the details yet, but you need to know you can upgrade an HttpServerRequest to a Websocket with the upgrade() method.

Julien

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/7f00f0bd-4c60-4d6c-a348-7a079977069e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christopher Paul Burke

unread,
Aug 22, 2016, 2:25:12 PM8/22/16
to ve...@googlegroups.com
Hi Julien, 

Thanks for the response. I believe that this is certainly a step in the right direction; I can now write a Sec-WebSocket-Protocol response header back, however when I upgrade the HttpRequest to a ServerWebSocket it (appears to) re-write the headers due to the Connection/Sec-Websocket-Upgrade/Upgrade headers being written in netty (by the Handshaker). 

I’ll look into it a little more as perhaps I am following the wrong pattern to handle subprotocols elegantly. 

Cheers,

Chris

You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/ZEeahuIvYCA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
signature.asc

Christopher Paul Burke

unread,
Aug 22, 2016, 3:11:47 PM8/22/16
to ve...@googlegroups.com
Just to elaborate; currently the request headers look like this: 
  1. Accept-Encoding:
    gzip, deflate, sdch
  2. Accept-Language:
    en-US,en;q=0.8
  3. Cache-Control:
    no-cache
  4. Connection:
    Upgrade
  5. Host:
    localhost:8080
  6. Origin:
  7. Pragma:
    no-cache
  8. Sec-WebSocket-Extensions:
    permessage-deflate; client_max_window_bits
  9. Sec-WebSocket-Key:
    QK3yjBJ4omOXz9jsGTNbfA==
  10. Sec-WebSocket-Protocol:
    someprotocol.v1
  11. Sec-WebSocket-Version:
    13
  12. Upgrade:
    websocket
  13. User-Agent:
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
On receipt of the request, if I write headers to the response object (either from HttpServerRequest#response, or from RoutingContext#response) the headers aren’t written to the response due to the upgrade call (seeming) to write the headers and not check what I’ve added. This means the client rejects as I’ve not specified a subprotocol. 
  1. connection:
    upgrade
  2. sec-websocket-accept:
    xq8A1Je9GtPqG5F3XzU6cdaazLg=
  3. upgrade:
    websocket
However if I write the headers and end the request I’ll of course see the headers (but as the connection effectively closes, I cannot upgrade). My client then will complain that the upgrade header is missing. It appears at least as if the existing response headers aren’t read during the upgrade (which seems right as https://github.com/eclipse/vert.x/blob/master/src/main/java/io/vertx/core/http/impl/ServerConnection.java#L187 does not reference the response).

Cheers,

Chris
signature.asc

Julien Viet

unread,
Aug 23, 2016, 3:05:30 AM8/23/16
to ve...@googlegroups.com
Hi,

you can specify the list of sub protocols in the HttpServerOptions:


this will automatically do it for you .

Julien

Christopher Paul Burke

unread,
Aug 23, 2016, 4:19:49 PM8/23/16
to ve...@googlegroups.com
Thank you! 

signature.asc
Message has been deleted

Christopher Paul Burke

unread,
Sep 9, 2016, 4:20:07 PM9/9/16
to ve...@googlegroups.com
Hi Srikanth,

FYI that's a header specified on the client so that the server knows the data sent over the wire is compressed, according to https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-28 aka a "PMCE" or per message client extension. 

If vertx is your WebSocket client of choice, I believe you can use http://vertx.io/docs/apidocs/io/vertx/core/http/HttpServerOptions.html#setCompressionSupported-boolean- however I have not tested as my test suite for the project is not covering this part of the implementation. 

Let me know if you're still stuck however and I can put together a small unit test to confirm. 

Kind Regards,

Christopher

On 6 Sep 2016, at 22:49, Srikanth Rajasekaran <sseka...@gmail.com> wrote:

Hi Chris,

Please could help me in setting the below header in the web socket.

Sec-WebSocket-Extensions:
permessage-deflate;

Regards,
Srikanth

Guillaume Lamirand

unread,
Sep 18, 2024, 10:51:15 AMSep 18
to vert.x
Hi,
I know it is an old issue but I have the same issue with Websocket-Sub-Protocols headers but also others headers I'd like to add to the websocket response.

Thanks 

Thomas SEGISMONT

unread,
Sep 19, 2024, 4:29:33 AMSep 19
to ve...@googlegroups.com
Hi,
Could you file an issue for enhancement and send a PR ?
Regards,
Thomas

Guillaume Lamirand

unread,
Sep 19, 2024, 6:14:27 AMSep 19
to ve...@googlegroups.com
Hi,

The code on master has changed from 4.x and the fix will be a bit different: do you want me to open 2 PRs ?

Thanks

Thomas SEGISMONT

unread,
Sep 19, 2024, 6:27:31 AMSep 19
to ve...@googlegroups.com

Julien Viet

unread,
Oct 11, 2024, 3:23:05 AMOct 11
to ve...@googlegroups.com
Hi,

I think that is something that should be possible.

Julien
> To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/dbb26b7c-ec37-4814-83e4-1726f1edc6e8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages