2013-10-16 20:41:47 +04:00
|
|
|
// Copyright 2013 Gary Burd. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2013-10-26 17:52:30 +04:00
|
|
|
"strings"
|
2013-10-16 20:41:47 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandshakeError describes an error with the handshake from the peer.
|
|
|
|
type HandshakeError struct {
|
2013-10-27 19:34:33 +04:00
|
|
|
message string
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 19:34:33 +04:00
|
|
|
func (e HandshakeError) Error() string { return e.message }
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2014-04-16 19:25:17 +04:00
|
|
|
// This method is deprecated, use websocket.Upgrader instead.
|
|
|
|
//
|
2013-10-16 20:41:47 +04:00
|
|
|
// Upgrade upgrades the HTTP server connection to the WebSocket protocol.
|
|
|
|
//
|
|
|
|
// The application is responsible for checking the request origin before
|
|
|
|
// calling Upgrade. An example implementation of the same origin policy is:
|
|
|
|
//
|
|
|
|
// if req.Header.Get("Origin") != "http://"+req.Host {
|
|
|
|
// http.Error(w, "Origin not allowed", 403)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//
|
2013-12-21 02:54:49 +04:00
|
|
|
// If the endpoint supports subprotocols, then the application is responsible
|
|
|
|
// for negotiating the protocol used on the connection. Use the Subprotocols()
|
|
|
|
// function to get the subprotocols requested by the client. Use the
|
|
|
|
// Sec-Websocket-Protocol response header to specify the subprotocol selected
|
|
|
|
// by the application.
|
2013-10-27 19:34:33 +04:00
|
|
|
//
|
2013-12-21 02:54:49 +04:00
|
|
|
// The responseHeader is included in the response to the client's upgrade
|
|
|
|
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
|
|
|
|
// negotiated subprotocol (Sec-Websocket-Protocol).
|
|
|
|
//
|
|
|
|
// The connection buffers IO to the underlying network connection. The
|
|
|
|
// readBufSize and writeBufSize parameters specify the size of the buffers to
|
|
|
|
// use. Messages can be larger than the buffers.
|
2013-10-27 19:34:33 +04:00
|
|
|
//
|
|
|
|
// If the request is not a valid WebSocket handshake, then Upgrade returns an
|
|
|
|
// error of type HandshakeError. Applications should handle this error by
|
|
|
|
// replying to the client with an HTTP error response.
|
2013-10-16 20:41:47 +04:00
|
|
|
func Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, readBufSize, writeBufSize int) (*Conn, error) {
|
2014-04-16 19:25:17 +04:00
|
|
|
u := Upgrader{ReadBufferSize: readBufSize, WriteBufferSize: writeBufSize}
|
|
|
|
u.Error = func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
|
|
|
// don't return errors to maintain backwards compatibility
|
2013-12-14 19:48:43 +04:00
|
|
|
}
|
2014-04-16 19:25:17 +04:00
|
|
|
return u.Upgrade(w, r, responseHeader)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2013-10-26 17:52:30 +04:00
|
|
|
|
|
|
|
// Subprotocols returns the subprotocols requested by the client in the
|
|
|
|
// Sec-Websocket-Protocol header.
|
|
|
|
func Subprotocols(r *http.Request) []string {
|
|
|
|
h := strings.TrimSpace(r.Header.Get("Sec-Websocket-Protocol"))
|
|
|
|
if h == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
protocols := strings.Split(h, ",")
|
|
|
|
for i := range protocols {
|
|
|
|
protocols[i] = strings.TrimSpace(protocols[i])
|
|
|
|
}
|
|
|
|
return protocols
|
|
|
|
}
|