mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
parsing and printing to new syntax. Use -oldparser to parse the old syntax, use -oldprinter to print the old syntax. 2) Change default gofmt formatting settings to use tabs for indentation only and to use spaces for alignment. This will make the code alignment insensitive to an editor's tabwidth. Use -spaces=false to use tabs for alignment. 3) Manually changed src/exp/parser/parser_test.go so that it doesn't try to parse the parser's source files using the old syntax (they have new syntax now). 4) gofmt -w src misc test/bench 5th and last set of files. R=rsc CC=golang-dev https://golang.org/cl/180050
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
// 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 (
|
|
"http"
|
|
"io"
|
|
)
|
|
|
|
// Handler is a interface that use a WebSocket.
|
|
//
|
|
// A trivial example server is:
|
|
//
|
|
// package main
|
|
//
|
|
// import (
|
|
// "http"
|
|
// "io"
|
|
// "websocket"
|
|
// )
|
|
//
|
|
// // echo back the websocket.
|
|
// 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())
|
|
// }
|
|
// }
|
|
type Handler func(*Conn)
|
|
|
|
func (f Handler) ServeHTTP(c *http.Conn, req *http.Request) {
|
|
if req.Method != "GET" || req.Proto != "HTTP/1.1" ||
|
|
req.Header["Upgrade"] != "WebSocket" ||
|
|
req.Header["Connection"] != "Upgrade" {
|
|
c.WriteHeader(http.StatusNotFound)
|
|
io.WriteString(c, "must use websocket to connect here")
|
|
return
|
|
}
|
|
rwc, buf, err := c.Hijack()
|
|
if err != nil {
|
|
panic("Hijack failed: ", err.String())
|
|
return
|
|
}
|
|
defer rwc.Close()
|
|
origin := req.Header["Origin"]
|
|
location := "ws://" + req.Host + req.URL.Path
|
|
|
|
// TODO(ukai): verify origin,location,protocol.
|
|
|
|
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")
|
|
protocol := ""
|
|
// canonical header key of WebSocket-Protocol.
|
|
if protocol, found := req.Header["Websocket-Protocol"]; found {
|
|
buf.WriteString("WebSocket-Protocol: " + protocol + "\r\n")
|
|
}
|
|
buf.WriteString("\r\n")
|
|
if err := buf.Flush(); err != nil {
|
|
return
|
|
}
|
|
ws := newConn(origin, location, protocol, buf, rwc)
|
|
f(ws)
|
|
}
|