gobwas / ws

Tiny WebSocket library for Go.
MIT License
6.1k stars 373 forks source link

Dialer's Header parameter doesn't work as intended #170

Closed Gekko0114 closed 8 months ago

Gekko0114 commented 1 year ago

Hello Gobwas,

Thanks for the great package!

I found that httpWriteUpgradeRequest doesn't work as intended. When I try to add dial function like this:

        dialer := &ws.Dialer{
            Header:  ws.HandshakeHeaderHTTP(h),
        }

I want to assign request's header to h, but it doesn't work as expected due to this code httpWriteHeader(bw, headerHost, u.Host). This causes both h and u.Host to be assigned to the request's header.

I hope this problem can be resolved soon. Thanks,

FYI We discussed this problem in this PR.

cristaloleg commented 1 year ago

Thanks for the info, will take a look shortly. Do you have a fix (even a theoretical) for that btw ?

Gekko0114 commented 1 year ago

You can fix it by replacing httpWriteHeader(bw, headerHost, u.Host) with the following code:

    host := u.Host
    if headerHTTP, ok := header.(HandshakeHeaderHTTP); ok {
        httpHeaderHTTP := http.Header(headerHTTP)
        if v := httpHeaderHTTP.Get(headerHost); v != "" {
            host = v
        }
    }

    httpWriteHeader(bw, headerHost, host)

And you need to comment out https://github.com/gobwas/ws/blob/master/http.go#L323-L325.

However, since handshakeheader is an interface, so I don't think my code is a perfect solution. My solution is specific to HandshakeHeaderHTTP type.

I think you should decide how to fix it because this solution is related to this library's design itself.

matthyx commented 8 months ago

@cristaloleg have you looked at this?

cristaloleg commented 8 months ago

Yes, but AFAIR we didn't found a solution. Feel free to make a PR.

matthyx commented 8 months ago

Yes, but AFAIR we didn't found a solution. Feel free to make a PR.

Please have a look at: https://github.com/gobwas/ws/pull/194 Merry Christmas!

cristaloleg commented 8 months ago

Fixed in https://github.com/gobwas/ws/releases/tag/v1.3.2