-
Notifications
You must be signed in to change notification settings - Fork 21
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 upgrade request/response callbacks #132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,12 @@ type WebsocketStream struct { | |
// Optional callback invoked when a control frame is received. | ||
ccb ControlCallback | ||
|
||
// Optional callback invoked when an upgrade request is sent. | ||
upReqCb UpgradeRequestCallback | ||
|
||
// Optional callback invoked when an upgrade response is received. | ||
upResCb UpgradeResponseCallback | ||
|
||
// Used to establish a TCP connection to the peer with a timeout. | ||
dialer *net.Dialer | ||
|
||
|
@@ -788,6 +794,10 @@ func (s *WebsocketStream) upgrade( | |
} | ||
} | ||
|
||
if s.upReqCb != nil { | ||
s.upReqCb(req) | ||
} | ||
|
||
err = req.Write(stream) | ||
if err != nil { | ||
return err | ||
|
@@ -820,6 +830,10 @@ func (s *WebsocketStream) upgrade( | |
} | ||
s.hb = s.hb[:0] | ||
|
||
if s.upResCb != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this be called after we assert that the handshake response is valid below? Or is there a use-case to snoop into a potentially invalid response? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The latter. The main motivation for this is to be able to log the request/response so we can debug failed handshakes. |
||
s.upResCb(res) | ||
} | ||
|
||
if !IsUpgradeRes(res) { | ||
return ErrCannotUpgrade | ||
} | ||
|
@@ -867,6 +881,22 @@ func (s *WebsocketStream) ControlCallback() ControlCallback { | |
return s.ccb | ||
} | ||
|
||
func (s *WebsocketStream) SetUpgradeRequestCallback(upReqCb UpgradeRequestCallback) { | ||
s.upReqCb = upReqCb | ||
} | ||
|
||
func (s *WebsocketStream) UpgradeRequestCallback() UpgradeRequestCallback { | ||
return s.upReqCb | ||
} | ||
|
||
func (s *WebsocketStream) SetUpgradeResponseCallback(upResCb UpgradeResponseCallback) { | ||
s.upResCb = upResCb | ||
} | ||
|
||
func (s *WebsocketStream) UpgradeResponseCallback() UpgradeResponseCallback { | ||
return s.upResCb | ||
} | ||
|
||
func (s *WebsocketStream) SetMaxMessageSize(bytes int) { | ||
// This is just for checking against the length returned in the frame | ||
// header. The sizes of the buffers in which we read or write the messages | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this is here in order to provide the
http.Request
to the caller for potential modification... which for the handshake means adding more headers. If so, we already have this mechanism of adding extra headers to the handshake, see here (this is used in bullish iirc)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for logging the request (at least for my use case).