server.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. // Command server is a test server for the Autobahn WebSockets Test Suite.
  15. package main
  16. import (
  17. "errors"
  18. "flag"
  19. "github.com/gorilla/websocket"
  20. "io"
  21. "log"
  22. "net/http"
  23. "time"
  24. "unicode/utf8"
  25. )
  26. // echoCopy echoes messages from the client using io.Copy.
  27. func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) {
  28. u := websocket.Upgrader{
  29. ReadBufferSize: 4096,
  30. WriteBufferSize: 4096,
  31. CheckOrigin: func(r *http.Request) bool {
  32. return true
  33. }}
  34. conn, err := u.Upgrade(w, r, nil)
  35. if err != nil {
  36. log.Println("Upgrade:", err)
  37. http.Error(w, "Bad request", 400)
  38. return
  39. }
  40. defer conn.Close()
  41. for {
  42. mt, r, err := conn.NextReader()
  43. if err != nil {
  44. if err != io.EOF {
  45. log.Println("NextReader:", err)
  46. }
  47. return
  48. }
  49. if mt == websocket.TextMessage {
  50. r = &validator{r: r}
  51. }
  52. w, err := conn.NextWriter(mt)
  53. if err != nil {
  54. log.Println("NextWriter:", err)
  55. return
  56. }
  57. if mt == websocket.TextMessage {
  58. r = &validator{r: r}
  59. }
  60. if writerOnly {
  61. _, err = io.Copy(struct{ io.Writer }{w}, r)
  62. } else {
  63. _, err = io.Copy(w, r)
  64. }
  65. if err != nil {
  66. if err == errInvalidUTF8 {
  67. conn.WriteControl(websocket.CloseMessage,
  68. websocket.FormatCloseMessage(websocket.CloseInvalidFramePayloadData, ""),
  69. time.Time{})
  70. }
  71. log.Println("Copy:", err)
  72. return
  73. }
  74. err = w.Close()
  75. if err != nil {
  76. log.Println("Close:", err)
  77. return
  78. }
  79. }
  80. }
  81. func echoCopyWriterOnly(w http.ResponseWriter, r *http.Request) {
  82. echoCopy(w, r, true)
  83. }
  84. func echoCopyFull(w http.ResponseWriter, r *http.Request) {
  85. echoCopy(w, r, false)
  86. }
  87. // echoReadAll echoes messages from the client by reading the entire message
  88. // with ioutil.ReadAll.
  89. func echoReadAll(w http.ResponseWriter, r *http.Request, writeMessage bool) {
  90. u := websocket.Upgrader{
  91. ReadBufferSize: 4096,
  92. WriteBufferSize: 4096,
  93. CheckOrigin: func(r *http.Request) bool {
  94. return true
  95. }}
  96. conn, err := u.Upgrade(w, r, nil)
  97. if err != nil {
  98. log.Println("Upgrade:", err)
  99. return
  100. }
  101. defer conn.Close()
  102. for {
  103. mt, b, err := conn.ReadMessage()
  104. if err != nil {
  105. if err != io.EOF {
  106. log.Println("NextReader:", err)
  107. }
  108. return
  109. }
  110. if mt == websocket.TextMessage {
  111. if !utf8.Valid(b) {
  112. conn.WriteControl(websocket.CloseMessage,
  113. websocket.FormatCloseMessage(websocket.CloseInvalidFramePayloadData, ""),
  114. time.Time{})
  115. log.Println("ReadAll: invalid utf8")
  116. }
  117. }
  118. if writeMessage {
  119. err = conn.WriteMessage(mt, b)
  120. if err != nil {
  121. log.Println("WriteMessage:", err)
  122. }
  123. } else {
  124. w, err := conn.NextWriter(mt)
  125. if err != nil {
  126. log.Println("NextWriter:", err)
  127. return
  128. }
  129. if _, err := w.Write(b); err != nil {
  130. log.Println("Writer:", err)
  131. return
  132. }
  133. if err := w.Close(); err != nil {
  134. log.Println("Close:", err)
  135. return
  136. }
  137. }
  138. }
  139. }
  140. func echoReadAllWriter(w http.ResponseWriter, r *http.Request) {
  141. echoReadAll(w, r, false)
  142. }
  143. func echoReadAllWriteMessage(w http.ResponseWriter, r *http.Request) {
  144. echoReadAll(w, r, true)
  145. }
  146. func serveHome(w http.ResponseWriter, r *http.Request) {
  147. if r.URL.Path != "/" {
  148. http.Error(w, "Not found.", 404)
  149. return
  150. }
  151. if r.Method != "GET" {
  152. http.Error(w, "Method not allowed", 405)
  153. return
  154. }
  155. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  156. io.WriteString(w, "<html><body>Echo Server</body></html")
  157. }
  158. var addr = flag.String("addr", ":9000", "http service address")
  159. func main() {
  160. flag.Parse()
  161. http.HandleFunc("/", serveHome)
  162. http.HandleFunc("/c", echoCopyWriterOnly)
  163. http.HandleFunc("/f", echoCopyFull)
  164. http.HandleFunc("/r", echoReadAllWriter)
  165. http.HandleFunc("/m", echoReadAllWriteMessage)
  166. err := http.ListenAndServe(*addr, nil)
  167. if err != nil {
  168. log.Fatal("ListenAndServe: ", err)
  169. }
  170. }
  171. type validator struct {
  172. state int
  173. x rune
  174. r io.Reader
  175. }
  176. var errInvalidUTF8 = errors.New("invalid utf8")
  177. func (r *validator) Read(p []byte) (int, error) {
  178. n, err := r.r.Read(p)
  179. state := r.state
  180. x := r.x
  181. for _, b := range p[:n] {
  182. state, x = decode(state, x, b)
  183. if state == utf8Reject {
  184. break
  185. }
  186. }
  187. r.state = state
  188. r.x = x
  189. if state == utf8Reject || (err == io.EOF && state != utf8Accept) {
  190. return n, errInvalidUTF8
  191. }
  192. return n, err
  193. }
  194. // UTF-8 decoder from http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  195. //
  196. // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  197. //
  198. // Permission is hereby granted, free of charge, to any person obtaining a copy
  199. // of this software and associated documentation files (the "Software"), to
  200. // deal in the Software without restriction, including without limitation the
  201. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  202. // sell copies of the Software, and to permit persons to whom the Software is
  203. // furnished to do so, subject to the following conditions:
  204. //
  205. // The above copyright notice and this permission notice shall be included in
  206. // all copies or substantial portions of the Software.
  207. //
  208. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  209. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  210. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  211. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  212. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  213. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  214. // IN THE SOFTWARE.
  215. var utf8d = [...]byte{
  216. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1f
  217. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3f
  218. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5f
  219. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7f
  220. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9f
  221. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // a0..bf
  222. 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0..df
  223. 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
  224. 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
  225. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  226. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
  227. 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
  228. 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
  229. 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // s7..s8
  230. }
  231. const (
  232. utf8Accept = 0
  233. utf8Reject = 1
  234. )
  235. func decode(state int, x rune, b byte) (int, rune) {
  236. t := utf8d[b]
  237. if state != utf8Accept {
  238. x = rune(b&0x3f) | (x << 6)
  239. } else {
  240. x = rune((0xff >> t) & b)
  241. }
  242. state = int(utf8d[256+state*16+int(t)])
  243. return state, x
  244. }