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 Handler is an interface to a WebSocket.
16 A trivial example server:
26 // Echo the data received on the Web Socket.
27 func EchoServer(ws *websocket.Conn) {
32 http.Handle("/echo", websocket.Handler(EchoServer));
33 err := http.ListenAndServe(":12345", nil);
35 panic("ListenAndServe: " + err.String())
39 type Handler func(*Conn)
42 Gets key number from Sec-WebSocket-Key<n>: field as described
43 in 5.2 Sending the server's opening handshake, 4.
45 func getKeyNumber(s string) (r uint32) {
46 // 4. Let /key-number_n/ be the digits (characters in the range
47 // U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9)) in /key_1/,
48 // interpreted as a base ten integer, ignoring all other characters
51 for i := 0; i < len(s); i++ {
52 if s[i] >= '0' && s[i] <= '9' {
53 r = r*10 + uint32(s[i]) - '0'
59 // ServeHTTP implements the http.Handler interface for a Web Socket
60 func (f Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
61 rwc, buf, err := w.(http.Hijacker).Hijack()
63 panic("Hijack failed: " + err.String())
66 // The server should abort the WebSocket connection if it finds
67 // the client did not send a handshake that matches with protocol
71 if req.Method != "GET" {
74 // HTTP version can be safely ignored.
76 if strings.ToLower(req.Header.Get("Upgrade")) != "websocket" ||
77 strings.ToLower(req.Header.Get("Connection")) != "upgrade" {
81 // TODO(ukai): check Host
82 origin := req.Header.Get("Origin")
87 key1 := req.Header.Get("Sec-Websocket-Key1")
91 key2 := req.Header.Get("Sec-Websocket-Key2")
95 key3 := make([]byte, 8)
96 if _, err := io.ReadFull(buf, key3); err != nil {
102 location = "wss://" + req.Host + req.URL.RawPath
104 location = "ws://" + req.Host + req.URL.RawPath
107 // Step 4. get key number in Sec-WebSocket-Key<n> fields.
108 keyNumber1 := getKeyNumber(key1)
109 keyNumber2 := getKeyNumber(key2)
111 // Step 5. get number of spaces in Sec-WebSocket-Key<n> fields.
112 space1 := uint32(strings.Count(key1, " "))
113 space2 := uint32(strings.Count(key2, " "))
114 if space1 == 0 || space2 == 0 {
118 // Step 6. key number must be an integral multiple of spaces.
119 if keyNumber1%space1 != 0 || keyNumber2%space2 != 0 {
123 // Step 7. let part be key number divided by spaces.
124 part1 := keyNumber1 / space1
125 part2 := keyNumber2 / space2
127 // Step 8. let challenge to be concatination of part1, part2 and key3.
128 // Step 9. get MD5 fingerprint of challenge.
129 response, err := getChallengeResponse(part1, part2, key3)
134 // Step 10. send response status line.
135 buf.WriteString("HTTP/1.1 101 WebSocket Protocol Handshake\r\n")
136 // Step 11. send response headers.
137 buf.WriteString("Upgrade: WebSocket\r\n")
138 buf.WriteString("Connection: Upgrade\r\n")
139 buf.WriteString("Sec-WebSocket-Location: " + location + "\r\n")
140 buf.WriteString("Sec-WebSocket-Origin: " + origin + "\r\n")
141 protocol := strings.TrimSpace(req.Header.Get("Sec-Websocket-Protocol"))
143 buf.WriteString("Sec-WebSocket-Protocol: " + protocol + "\r\n")
145 // Step 12. send CRLF.
146 buf.WriteString("\r\n")
147 // Step 13. send response data.
149 if err := buf.Flush(); err != nil {
152 ws := newConn(origin, location, protocol, buf, rwc)
159 Draft75Handler is an interface to a WebSocket based on the
160 (soon obsolete) draft-hixie-thewebsocketprotocol-75.
162 type Draft75Handler func(*Conn)
164 // ServeHTTP implements the http.Handler interface for a Web Socket.
165 func (f Draft75Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
166 if req.Method != "GET" || req.Proto != "HTTP/1.1" {
167 w.WriteHeader(http.StatusBadRequest)
168 io.WriteString(w, "Unexpected request")
171 if req.Header.Get("Upgrade") != "WebSocket" {
172 w.WriteHeader(http.StatusBadRequest)
173 io.WriteString(w, "missing Upgrade: WebSocket header")
176 if req.Header.Get("Connection") != "Upgrade" {
177 w.WriteHeader(http.StatusBadRequest)
178 io.WriteString(w, "missing Connection: Upgrade header")
181 origin := strings.TrimSpace(req.Header.Get("Origin"))
183 w.WriteHeader(http.StatusBadRequest)
184 io.WriteString(w, "missing Origin header")
188 rwc, buf, err := w.(http.Hijacker).Hijack()
190 panic("Hijack failed: " + err.String())
197 location = "wss://" + req.Host + req.URL.RawPath
199 location = "ws://" + req.Host + req.URL.RawPath
202 // TODO(ukai): verify origin,location,protocol.
204 buf.WriteString("HTTP/1.1 101 Web Socket Protocol Handshake\r\n")
205 buf.WriteString("Upgrade: WebSocket\r\n")
206 buf.WriteString("Connection: Upgrade\r\n")
207 buf.WriteString("WebSocket-Origin: " + origin + "\r\n")
208 buf.WriteString("WebSocket-Location: " + location + "\r\n")
209 protocol := strings.TrimSpace(req.Header.Get("Websocket-Protocol"))
210 // canonical header key of WebSocket-Protocol.
212 buf.WriteString("WebSocket-Protocol: " + protocol + "\r\n")
214 buf.WriteString("\r\n")
215 if err := buf.Flush(); err != nil {
218 ws := newConn(origin, location, protocol, buf, rwc)