JuliaWeb / WebSockets.jl

A WebSockets library for Julia
MIT License
157 stars 58 forks source link

Add user-set additional headers for handshake #155

Open Grobycn opened 4 years ago

Grobycn commented 4 years ago

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.

hustf commented 3 years ago

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?

Grobycn commented 3 years ago

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

hustf commented 3 years ago

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