server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "bufio"
  7. "errors"
  8. "net"
  9. "net/http"
  10. )
  11. // HandshakeError describes an error with the handshake from the peer.
  12. type HandshakeError struct {
  13. Err string
  14. }
  15. func (e HandshakeError) Error() string { return e.Err }
  16. // Upgrade upgrades the HTTP server connection to the WebSocket protocol.
  17. //
  18. // Upgrade returns a HandshakeError if the request is not a WebSocket
  19. // handshake. Applications should handle errors of this type by replying to the
  20. // client with an HTTP response.
  21. //
  22. // The application is responsible for checking the request origin before
  23. // calling Upgrade. An example implementation of the same origin policy is:
  24. //
  25. // if req.Header.Get("Origin") != "http://"+req.Host {
  26. // http.Error(w, "Origin not allowed", 403)
  27. // return
  28. // }
  29. //
  30. // Use the responseHeader to specify cookies (Set-Cookie) and the subprotocol
  31. // (Sec-WebSocket-Protocol).
  32. func Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, readBufSize, writeBufSize int) (*Conn, error) {
  33. if values := r.Header["Sec-Websocket-Version"]; len(values) == 0 || values[0] != "13" {
  34. return nil, HandshakeError{"websocket: version != 13"}
  35. }
  36. if !tokenListContainsValue(r.Header, "Connection", "upgrade") {
  37. return nil, HandshakeError{"websocket: connection header != upgrade"}
  38. }
  39. if !tokenListContainsValue(r.Header, "Upgrade", "websocket") {
  40. return nil, HandshakeError{"websocket: upgrade != websocket"}
  41. }
  42. var challengeKey string
  43. values := r.Header["Sec-Websocket-Key"]
  44. if len(values) == 0 || values[0] == "" {
  45. return nil, HandshakeError{"websocket: key missing or blank"}
  46. }
  47. challengeKey = values[0]
  48. var (
  49. netConn net.Conn
  50. br *bufio.Reader
  51. err error
  52. )
  53. h, ok := w.(http.Hijacker)
  54. if !ok {
  55. return nil, errors.New("websocket: response does not implement http.Hijacker")
  56. }
  57. var rw *bufio.ReadWriter
  58. netConn, rw, err = h.Hijack()
  59. br = rw.Reader
  60. if br.Buffered() > 0 {
  61. netConn.Close()
  62. return nil, errors.New("websocket: client sent data before handshake is complete")
  63. }
  64. c := newConn(netConn, true, readBufSize, writeBufSize)
  65. p := c.writeBuf[:0]
  66. p = append(p, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "...)
  67. p = append(p, computeAcceptKey(challengeKey)...)
  68. p = append(p, "\r\n"...)
  69. for k, vs := range responseHeader {
  70. for _, v := range vs {
  71. p = append(p, k...)
  72. p = append(p, ": "...)
  73. for i := 0; i < len(v); i++ {
  74. b := v[i]
  75. if b <= 31 {
  76. // prevent response splitting.
  77. b = ' '
  78. }
  79. p = append(p, b)
  80. }
  81. p = append(p, "\r\n"...)
  82. }
  83. }
  84. p = append(p, "\r\n"...)
  85. if _, err = netConn.Write(p); err != nil {
  86. netConn.Close()
  87. return nil, err
  88. }
  89. return c, nil
  90. }