| 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. |