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 all commits
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
25 changes: 25 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func Upgrade(conn io.ReadWriter) (Handshake, error) {
// HTTPUpgrader contains options for upgrading connection to websocket from
// net/http Handler arguments.
type HTTPUpgrader struct {
// CopyHeadersToHandshake setting specifies whether headers should be preserved during the handshake process.
// If enabled, the headers will be copied to Handshake.Header.
CopyHeadersToHandshake bool

// Timeout is the maximum amount of time an Upgrade() will spent while
// writing handshake response.
//
Expand Down Expand Up @@ -241,6 +245,12 @@ func (u HTTPUpgrader) Upgrade(r *http.Request, w http.ResponseWriter) (conn net.
if h := u.Header; h != nil {
header[0] = HandshakeHeaderHTTP(h)
}

if u.CopyHeadersToHandshake {
// set handshake header
hs.Header = r.Header
}

if err == nil {
httpWriteResponseUpgrade(rw.Writer, strToBytes(nonce), hs, header.WriteTo)
err = rw.Writer.Flush()
Expand All @@ -262,6 +272,10 @@ func (u HTTPUpgrader) Upgrade(r *http.Request, w http.ResponseWriter) (conn net.

// Upgrader contains options for upgrading connection to websocket.
type Upgrader struct {
// CopyHeadersToHandshake setting specifies whether headers should be preserved during the handshake process.
// If enabled, the headers will be copied to Handshake.Header.
CopyHeadersToHandshake bool

// ReadBufferSize and WriteBufferSize is an I/O buffer sizes.
// They used to read and write http data while upgrading to WebSocket.
// Allocated buffers are pooled with sync.Pool to avoid extra allocations.
Expand Down Expand Up @@ -498,6 +512,12 @@ func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error) {

nonce = make([]byte, nonceSize)
)

if u.CopyHeadersToHandshake {
// init handshake headers
hs.Header = make(http.Header)
}

for err == nil {
line, e := readLine(br)
if e != nil {
Expand All @@ -514,6 +534,11 @@ func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error) {
break
}

if u.CopyHeadersToHandshake {
// copy and add header
hs.Header.Add(btsToString(bytes.Clone(k)), btsToString(bytes.Clone(v)))
}

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