Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(19)

Unified Diff: src/pkg/websocket/websocket.go

Issue 5303096: code review 5303096: websocket: manage subprotocol negotiation in the websoc...
Patch Set: diff -r 6ed228888a72 https://go.googlecode.com/hg/ Created 14 years ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/pkg/websocket/websocket.go
===================================================================
--- a/src/pkg/websocket/websocket.go
+++ b/src/pkg/websocket/websocket.go
@@ -89,11 +89,39 @@
handshakeData map[string]string
}
+// A ProtocolChooser returns the preferred subprotocol from the list supplied
+// by the client.
+// If none of the subprotocols are suitable to the server,
+// the string returned should be "".
+// If there is some fatal error (including the lack of a suitable subprotocol,
+// though this is not required), the error value should be
+// websocket.ErrUnsupportedSubProtocol.
+// If the error value is non-nil, an HTTP 400 Bad Request will be
+// generated with the string value of the error. It is an error to
+// return a string other than "" or one of the provided protocols.
+type ProtocolChooser func([]string) (string, error)
+
+// Returnss the first protocol in the argument list that matches
+// a protocol requested by the client. If there is no match, returns ""
+func SimpleChooser(preferences []string) ProtocolChooser {
+ return func(clientPrefs []string) (string, error) {
+ for _, pref := range preferences {
+ for _, clientPref := range clientPrefs {
+ if pref == clientPref {
+ return pref, nil
+ }
+ }
+ }
+ return "", nil
+ }
+}
+
// serverHandshaker is an interface to handle WebSocket server side handshake.
type serverHandshaker interface {
// ReadHandshake reads handshake request message from client.
// Returns http response code and error if any.
- ReadHandshake(buf *bufio.Reader, req *http.Request) (code int, err error)
+ ReadHandshake(buf *bufio.Reader, req *http.Request,
+ protocolChooser ProtocolChooser) (code int, err error)
// AcceptHandshake accepts the client handshake request and sends
// handshake response back to client.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b