server.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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) (conn *Conn, err error) {
  12. config := new(Config)
  13. var hs serverHandshaker = &hybiServerHandshaker{Config: config}
  14. code, err := hs.ReadHandshake(buf.Reader, req)
  15. if err == ErrBadWebSocketVersion {
  16. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  17. fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
  18. buf.WriteString("\r\n")
  19. buf.WriteString(err.Error())
  20. buf.Flush()
  21. return
  22. }
  23. if err != nil {
  24. hs = &hixie76ServerHandshaker{Config: config}
  25. code, err = hs.ReadHandshake(buf.Reader, req)
  26. }
  27. if err != nil {
  28. hs = &hixie75ServerHandshaker{Config: config}
  29. code, err = hs.ReadHandshake(buf.Reader, req)
  30. }
  31. if err != nil {
  32. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  33. buf.WriteString("\r\n")
  34. buf.WriteString(err.Error())
  35. buf.Flush()
  36. return
  37. }
  38. config.Protocol = nil
  39. err = hs.AcceptHandshake(buf.Writer)
  40. if err != nil {
  41. code = http.StatusBadRequest
  42. fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
  43. buf.WriteString("\r\n")
  44. buf.Flush()
  45. return
  46. }
  47. conn = hs.NewServerConn(buf, rwc, req)
  48. return
  49. }
  50. /*
  51. Handler is an interface to a WebSocket.
  52. A trivial example server:
  53. package main
  54. import (
  55. "io"
  56. "net/http"
  57. "websocket"
  58. )
  59. // Echo the data received on the WebSocket.
  60. func EchoServer(ws *websocket.Conn) {
  61. io.Copy(ws, ws);
  62. }
  63. func main() {
  64. http.Handle("/echo", websocket.Handler(EchoServer));
  65. err := http.ListenAndServe(":12345", nil);
  66. if err != nil {
  67. panic("ListenAndServe: " + err.Error())
  68. }
  69. }
  70. */
  71. type Handler func(*Conn)
  72. // ServeHTTP implements the http.Handler interface for a Web Socket
  73. func (h Handler) ServeHTTP(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)
  84. if err != nil {
  85. return
  86. }
  87. if conn == nil {
  88. panic("unexpected nil conn")
  89. }
  90. h(conn)
  91. }