Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Handshake.Header field #203

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

PotatoCloud
Copy link

I think we should add a Header field to Handshake. Some developers may need to process the Header of the corresponding connection. At present, it is difficult for gobwas to implement similar functions.

server.go Outdated
@@ -514,6 +522,9 @@ func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error) {
break
}

// copy and add header
hs.Header.Add(btsToString(bytes.Clone(k)), btsToString(bytes.Clone(v)))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to prevent problems caused by using bytespool, a copy is needed

server.go Outdated
@@ -241,6 +241,10 @@ func (u HTTPUpgrader) Upgrade(r *http.Request, w http.ResponseWriter) (conn net.
if h := u.Header; h != nil {
header[0] = HandshakeHeaderHTTP(h)
}

// set handshake header
hs.Header = r.Header
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we use http.Request Header directly without copying it.

@cristaloleg
Copy link
Collaborator

Hi, thanks for the PR. Can you clarify what you're trying to achieve?

@PotatoCloud
Copy link
Author

Hi, thanks for the PR. Can you clarify what you're trying to achieve?

I copy the headers during the handshake into the Handshake struct (since currently gobwas/ws doesn't have a good way to get the handshake headers/raw headers)

@PotatoCloud
Copy link
Author

Hi, thanks for the PR. Can you clarify what you're trying to achieve?

For example, some developers want to obtain the Authorization field in the header for identity authentication.

@cristaloleg
Copy link
Collaborator

I'm not 100% sure what is the point of passing request headers to handshake result. User can access exactly the same data by looking at request directly.

hs.Header.Get("Authorization")
==
r.Header.Get("Authorization")

What is the benefit of keeping headers in a handshake?

@PotatoCloud
Copy link
Author

PotatoCloud commented Jun 18, 2024

I'm not 100% sure what is the point of passing request headers to handshake result. User can access exactly the same data by looking at request directly.

hs.Header.Get("Authorization")
==
r.Header.Get("Authorization")

What is the benefit of keeping headers in a handshake?

but, func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error) can't do this.

For example, I want to build a pure websocket server, using net + gobwas/ws, in this case I can't do it like you do.

@cristaloleg
Copy link
Collaborator

pure websocket server

Based purely on TCP connections, right?

Ok, I think I'm starting to understand your case. The only thing that looks scary to me is that we will start allocating/copying all the headers for each request, which is completely not acceptable with the current guarantees.

The only thing I see now is to hide it under a flag.

@PotatoCloud
Copy link
Author

we will start allocating/copying all the headers for each request
The only thing I see now is to hide it under a flag.

i got it, i will add build tags support for it later

@cristaloleg
Copy link
Collaborator

No-no, not a build tag. Flag in upgraders.

@cristaloleg
Copy link
Collaborator

Wait a sec, have you looked at OnHeader func(key, value []byte) error ? This is exactly what you need to access all the headers without copying them (see also example in README)

@PotatoCloud
Copy link
Author

OnHeader func(key, value []byte) error

I tried it and found that OnHeader captures all the incoming connection headers. So I added the headers to the Handshake

@PotatoCloud
Copy link
Author

No-no, not a build tag. Flag in upgraders.

got it.

@cristaloleg
Copy link
Collaborator

all the incoming connection headers.

Exactly what you need, isn't it?

@PotatoCloud
Copy link
Author

PotatoCloud commented Jun 18, 2024

Exactly what you need, isn't it?

wrong, there is no way to know which connection the header was passed in from. for example, identity authentication needs to be performed on a specific connection, and this process cannot be completed using OnHeader

@PotatoCloud
Copy link
Author

plz review it. i am not sure if what I did meets your requirements.

@cristaloleg
Copy link
Collaborator

I will take a look tomorrow, thanks.

@PotatoCloud
Copy link
Author

I will take a look tomorrow, thanks.

can u merge now? thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants