WebSockets.jl icon indicating copy to clipboard operation
WebSockets.jl copied to clipboard

Add user-set additional headers for handshake

Open Grobycn opened this issue 5 years ago • 3 comments
trafficstars

Some websocket server may check the headers, such as the example of golang.org/x/net/websocket. Get 403 when there is no "Origin: xxx" header.

For compatibility, it shoud have the ability of adding additional headers to open websocket.

Grobycn avatar Nov 30 '19 13:11 Grobycn

I don't really understand what you mean here. The way I'm thinking about this, the headers are read prior to upgrading a connection to a websocket connection. While the websocket is open, any further communication on that connection will be interpreted as websocket communication.

So I suppose you actually mean 'have the ability of adding additional headers in the opening handshake'. I think you have that option. I might be wrong. Do you have a halfway-working example?

hustf avatar Dec 18 '20 21:12 hustf

Sorry for my poor english. But yes, that's what I mean: have the ability of adding additional headers in the opening handshake.

Grobycn avatar Dec 19 '20 15:12 Grobycn

Yes, adding this header could 'easily' be achieved by adding additional headers through the function call. But it complicates the API. For the user to be aware of which headers is missing requires detail knowledge, or at least a decent ability to study error messages. How did error messages look in your case? I hope they were informative.

Personally I think writing a custom 'open' function has more appeal than studying the nitty gritty of keyword arguments. It could be something like this:

module Grobycn
using WebSockets
import WebSockets.HTTP._openstream
import WebSockets.HTTP.open
import WebSockets.HTTP.IOExtras.IOError
function open_golang(f::Function, url; verbose=false, subprotocol = "", kw...)
    key = base64encode(rand(UInt8, 16))
    headers = [
        "Upgrade" => "websocket",
        "Connection" => "Upgrade",
        "Sec-WebSocket-Key" => key,
        "Sec-WebSocket-Version" => "13",
        "Origin" => "xxxx"
    ]
    ....the rest a copy of WebSockets' 'open' function.
end

hustf avatar Dec 19 '20 17:12 hustf