server.go 2.5 KB

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