server.go 7.0 KB

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