server.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2009 The Go Authors. 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. "fmt"
  8. "io"
  9. "net/http"
  10. )
  11. func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request, config *Config, handshake func(*Config, *http.Request) error) (conn *Conn, err error) {
  12. var hs serverHandshaker = &hybiServerHandshaker{Config: config}
  13. code, err := hs.ReadHandshake(buf.Reader, req)
  14. if err == ErrBadWebSocketVersion {
  15. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  16. fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
  17. buf.WriteString("\r\n")
  18. buf.WriteString(err.Error())
  19. buf.Flush()
  20. return
  21. }
  22. if err != nil {
  23. hs = &hixie76ServerHandshaker{Config: config}
  24. code, err = hs.ReadHandshake(buf.Reader, req)
  25. }
  26. if err != nil {
  27. hs = &hixie75ServerHandshaker{Config: config}
  28. code, err = hs.ReadHandshake(buf.Reader, req)
  29. }
  30. if err != nil {
  31. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  32. buf.WriteString("\r\n")
  33. buf.WriteString(err.Error())
  34. buf.Flush()
  35. return
  36. }
  37. if handshake != nil {
  38. err = handshake(config, req)
  39. if err != nil {
  40. code = http.StatusForbidden
  41. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  42. buf.WriteString("\r\n")
  43. buf.Flush()
  44. return
  45. }
  46. }
  47. err = hs.AcceptHandshake(buf.Writer)
  48. if err != nil {
  49. code = http.StatusBadRequest
  50. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  51. buf.WriteString("\r\n")
  52. buf.Flush()
  53. return
  54. }
  55. conn = hs.NewServerConn(buf, rwc, req)
  56. return
  57. }
  58. // Server represents a server of a WebSocket.
  59. type Server struct {
  60. // Config is a WebSocket configuration for new WebSocket connection.
  61. Config
  62. // Handshake is an optional function in WebSocket handshake.
  63. // For example, you can check, or don't check Origin header.
  64. // Another example, you can select config.Protocol.
  65. Handshake func(*Config, *http.Request) error
  66. // Handler handles a WebSocket connection.
  67. Handler
  68. }
  69. // ServeHTTP implements the http.Handler interface for a WebSocket
  70. func (s Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  71. s.serveWebSocket(w, req)
  72. }
  73. func (s Server) serveWebSocket(w http.ResponseWriter, req *http.Request) {
  74. rwc, buf, err := w.(http.Hijacker).Hijack()
  75. if err != nil {
  76. panic("Hijack failed: " + err.Error())
  77. return
  78. }
  79. // The server should abort the WebSocket connection if it finds
  80. // the client did not send a handshake that matches with protocol
  81. // specification.
  82. defer rwc.Close()
  83. conn, err := newServerConn(rwc, buf, req, &s.Config, s.Handshake)
  84. if err != nil {
  85. return
  86. }
  87. if conn == nil {
  88. panic("unexpected nil conn")
  89. }
  90. s.Handler(conn)
  91. }
  92. // Handler is a simple interface to a WebSocket browser client.
  93. // It checks if Origin header is valid URL by default.
  94. // You might want to verify websocket.Conn.Config().Origin in the func.
  95. // If you use Server instead of Handler, you could call websocket.Origin and
  96. // check the origin in your Handshake func. So, if you want to accept
  97. // non-browser client, which doesn't send Origin header, you could use Server
  98. //. that doesn't check origin in its Handshake.
  99. type Handler func(*Conn)
  100. func checkOrigin(config *Config, req *http.Request) (err error) {
  101. config.Origin, err = Origin(config, req)
  102. if err == nil && config.Origin == nil {
  103. return fmt.Errorf("null origin")
  104. }
  105. return err
  106. }
  107. // ServeHTTP implements the http.Handler interface for a WebSocket
  108. func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  109. s := Server{Handler: h, Handshake: checkOrigin}
  110. s.serveWebSocket(w, req)
  111. }