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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Handshake struct {

// Extensions is the list of negotiated extensions.
Extensions []httphead.Option

// Header all request headers obtained during handshake
Header http.Header
}

// Errors used by the websocket client.
Expand Down
11 changes: 11 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.


if err == nil {
httpWriteResponseUpgrade(rw.Writer, strToBytes(nonce), hs, header.WriteTo)
err = rw.Writer.Flush()
Expand Down Expand Up @@ -498,6 +502,10 @@ func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error) {

nonce = make([]byte, nonceSize)
)

// init handshake headers
hs.Header = make(http.Header)

for err == nil {
line, e := readLine(br)
if e != nil {
Expand All @@ -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


switch btsToString(k) {
case headerHostCanonical:
headerSeen |= headerSeenHost
Expand Down