LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // The websocket package implements a client and server for the Web Socket proto
col. | 5 // The websocket package implements a client and server for the Web Socket proto
col. |
6 // The protocol is defined at http://tools.ietf.org/html/draft-hixie-thewebsocke
tprotocol | 6 // The protocol is defined at http://tools.ietf.org/html/draft-hixie-thewebsocke
tprotocol |
7 package websocket | 7 package websocket |
8 | 8 |
9 // TODO(ukai): | 9 // TODO(ukai): |
10 // better logging. | 10 // better logging. |
11 | 11 |
12 import ( | 12 import ( |
13 "bufio" | 13 "bufio" |
| 14 "crypto/md5" |
| 15 "encoding/binary" |
14 "io" | 16 "io" |
15 "net" | 17 "net" |
16 "os" | 18 "os" |
17 ) | 19 ) |
18 | 20 |
19 // WebSocketAddr is an implementation of net.Addr for Web Sockets. | 21 // WebSocketAddr is an implementation of net.Addr for Web Sockets. |
20 type WebSocketAddr string | 22 type WebSocketAddr string |
21 | 23 |
22 // Network returns the network type for a Web Socket, "websocket". | 24 // Network returns the network type for a Web Socket, "websocket". |
23 func (addr WebSocketAddr) Network() string { return "websocket" } | 25 func (addr WebSocketAddr) Network() string { return "websocket" } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 } | 131 } |
130 | 132 |
131 // SetReadTimeout sets the connection's network read timeout in nanoseconds. | 133 // SetReadTimeout sets the connection's network read timeout in nanoseconds. |
132 func (ws *Conn) SetReadTimeout(nsec int64) os.Error { | 134 func (ws *Conn) SetReadTimeout(nsec int64) os.Error { |
133 if conn, ok := ws.rwc.(net.Conn); ok { | 135 if conn, ok := ws.rwc.(net.Conn); ok { |
134 return conn.SetReadTimeout(nsec) | 136 return conn.SetReadTimeout(nsec) |
135 } | 137 } |
136 return os.EINVAL | 138 return os.EINVAL |
137 } | 139 } |
138 | 140 |
139 // SeWritetTimeout sets the connection's network write timeout in nanoseconds. | 141 // SetWritetTimeout sets the connection's network write timeout in nanoseconds. |
140 func (ws *Conn) SetWriteTimeout(nsec int64) os.Error { | 142 func (ws *Conn) SetWriteTimeout(nsec int64) os.Error { |
141 if conn, ok := ws.rwc.(net.Conn); ok { | 143 if conn, ok := ws.rwc.(net.Conn); ok { |
142 return conn.SetWriteTimeout(nsec) | 144 return conn.SetWriteTimeout(nsec) |
143 } | 145 } |
144 return os.EINVAL | 146 return os.EINVAL |
145 } | 147 } |
146 | 148 |
| 149 // getChallengeResponse computes the expected response from the |
| 150 // challenge as described in section 5.1 Opening Handshake steps 42 to |
| 151 // 43 of http://www.whatwg.org/specs/web-socket-protocol/ |
| 152 func getChallengeResponse(number1, number2 uint32, key3 []byte) (expected []byte
, err os.Error) { |
| 153 // 41. Let /challenge/ be the concatenation of /number_1/, expressed |
| 154 // a big-endian 32 bit integer, /number_2/, expressed in a big- |
| 155 // endian 32 bit integer, and the eight bytes of /key_3/ in the |
| 156 // order they were sent to the wire. |
| 157 challenge := make([]byte, 16) |
| 158 binary.BigEndian.PutUint32(challenge[0:], number1) |
| 159 binary.BigEndian.PutUint32(challenge[4:], number2) |
| 160 copy(challenge[8:], key3) |
| 161 |
| 162 // 42. Let /expected/ be the MD5 fingerprint of /challenge/ as a big- |
| 163 // endian 128 bit string. |
| 164 h := md5.New() |
| 165 if _, err = h.Write(challenge); err != nil { |
| 166 return |
| 167 } |
| 168 expected = h.Sum() |
| 169 return |
| 170 } |
| 171 |
147 var _ net.Conn = (*Conn)(nil) // compile-time check that *Conn implements net.Co
nn. | 172 var _ net.Conn = (*Conn)(nil) // compile-time check that *Conn implements net.Co
nn. |
LEFT | RIGHT |