2009-11-29 15:22:44 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:41:46 -07:00
|
|
|
"http"
|
|
|
|
"io"
|
2009-11-29 15:22:44 -07:00
|
|
|
)
|
|
|
|
|
2010-01-27 21:38:32 -07:00
|
|
|
/*
|
|
|
|
Handler is an interface to a WebSocket.
|
|
|
|
A trivial example server is:
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"http"
|
|
|
|
"io"
|
|
|
|
"websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Echo the data received on the Web Socket.
|
|
|
|
func EchoServer(ws *websocket.Conn) {
|
|
|
|
io.Copy(ws, ws);
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.Handle("/echo", websocket.Handler(EchoServer));
|
|
|
|
err := http.ListenAndServe(":12345", nil);
|
|
|
|
if err != nil {
|
|
|
|
panic("ListenAndServe: ", err.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2009-11-29 15:22:44 -07:00
|
|
|
type Handler func(*Conn)
|
|
|
|
|
2010-01-27 21:38:32 -07:00
|
|
|
// ServeHTTP implements the http.Handler interface for a Web Socket.
|
2009-11-29 15:22:44 -07:00
|
|
|
func (f Handler) ServeHTTP(c *http.Conn, req *http.Request) {
|
2010-02-18 19:32:40 -07:00
|
|
|
if req.Method != "GET" || req.Proto != "HTTP/1.1" {
|
|
|
|
c.WriteHeader(http.StatusBadRequest)
|
|
|
|
io.WriteString(c, "Unexpected request")
|
2009-12-15 16:41:46 -07:00
|
|
|
return
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
2010-02-18 19:32:40 -07:00
|
|
|
if v, present := req.Header["Upgrade"]; !present || v != "WebSocket" {
|
|
|
|
c.WriteHeader(http.StatusBadRequest)
|
|
|
|
io.WriteString(c, "missing Upgrade: WebSocket header")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if v, present := req.Header["Connection"]; !present || v != "Upgrade" {
|
|
|
|
c.WriteHeader(http.StatusBadRequest)
|
|
|
|
io.WriteString(c, "missing Connection: Upgrade header")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
origin, present := req.Header["Origin"]
|
|
|
|
if !present {
|
|
|
|
c.WriteHeader(http.StatusBadRequest)
|
|
|
|
io.WriteString(c, "missing Origin header")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:41:46 -07:00
|
|
|
rwc, buf, err := c.Hijack()
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
2009-12-15 16:41:46 -07:00
|
|
|
panic("Hijack failed: ", err.String())
|
|
|
|
return
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
defer rwc.Close()
|
2010-03-19 15:18:02 -06:00
|
|
|
location := "ws://" + req.Host + req.URL.RawPath
|
2009-11-29 15:22:44 -07:00
|
|
|
|
|
|
|
// TODO(ukai): verify origin,location,protocol.
|
|
|
|
|
2009-12-15 16:41:46 -07:00
|
|
|
buf.WriteString("HTTP/1.1 101 Web Socket Protocol Handshake\r\n")
|
|
|
|
buf.WriteString("Upgrade: WebSocket\r\n")
|
|
|
|
buf.WriteString("Connection: Upgrade\r\n")
|
|
|
|
buf.WriteString("WebSocket-Origin: " + origin + "\r\n")
|
|
|
|
buf.WriteString("WebSocket-Location: " + location + "\r\n")
|
2010-02-18 19:32:40 -07:00
|
|
|
protocol, present := req.Header["Websocket-Protocol"]
|
2009-11-29 15:22:44 -07:00
|
|
|
// canonical header key of WebSocket-Protocol.
|
2010-02-18 19:32:40 -07:00
|
|
|
if present {
|
2009-11-29 15:22:44 -07:00
|
|
|
buf.WriteString("WebSocket-Protocol: " + protocol + "\r\n")
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
buf.WriteString("\r\n")
|
2009-11-29 15:22:44 -07:00
|
|
|
if err := buf.Flush(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
ws := newConn(origin, location, protocol, buf, rwc)
|
|
|
|
f(ws)
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|