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.
14
func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request) (conn *Conn, err error) {
16
var hs serverHandshaker = &hybiServerHandshaker{Config: config}
17
code, err := hs.ReadHandshake(buf.Reader, req)
18
if err == ErrBadWebSocketVersion {
19
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
20
fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
21
buf.WriteString("\r\n")
22
buf.WriteString(err.Error())
27
hs = &hixie76ServerHandshaker{Config: config}
28
code, err = hs.ReadHandshake(buf.Reader, req)
31
hs = &hixie75ServerHandshaker{Config: config}
32
code, err = hs.ReadHandshake(buf.Reader, req)
35
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
36
buf.WriteString("\r\n")
37
buf.WriteString(err.Error())
43
err = hs.AcceptHandshake(buf.Writer)
45
code = http.StatusBadRequest
46
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
47
buf.WriteString("\r\n")
51
conn = hs.NewServerConn(buf, rwc, req)
55
// Handler is an interface to a WebSocket.
56
type Handler func(*Conn)
58
// ServeHTTP implements the http.Handler interface for a Web Socket
59
func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
60
rwc, buf, err := w.(http.Hijacker).Hijack()
62
panic("Hijack failed: " + err.Error())
65
// The server should abort the WebSocket connection if it finds
66
// the client did not send a handshake that matches with protocol
69
conn, err := newServerConn(rwc, buf, req)
74
panic("unexpected nil conn")