1
0

server.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, writePrepared 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. if !writePrepared {
  105. err = conn.WriteMessage(mt, b)
  106. if err != nil {
  107. log.Println("WriteMessage:", err)
  108. }
  109. } else {
  110. pm, err := websocket.NewPreparedMessage(mt, b)
  111. if err != nil {
  112. log.Println("NewPreparedMessage:", err)
  113. return
  114. }
  115. err = conn.WritePreparedMessage(pm)
  116. if err != nil {
  117. log.Println("WritePreparedMessage:", err)
  118. }
  119. }
  120. } else {
  121. w, err := conn.NextWriter(mt)
  122. if err != nil {
  123. log.Println("NextWriter:", err)
  124. return
  125. }
  126. if _, err := w.Write(b); err != nil {
  127. log.Println("Writer:", err)
  128. return
  129. }
  130. if err := w.Close(); err != nil {
  131. log.Println("Close:", err)
  132. return
  133. }
  134. }
  135. }
  136. }
  137. func echoReadAllWriter(w http.ResponseWriter, r *http.Request) {
  138. echoReadAll(w, r, false, false)
  139. }
  140. func echoReadAllWriteMessage(w http.ResponseWriter, r *http.Request) {
  141. echoReadAll(w, r, true, false)
  142. }
  143. func echoReadAllWritePreparedMessage(w http.ResponseWriter, r *http.Request) {
  144. echoReadAll(w, r, true, 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. http.HandleFunc("/p", echoReadAllWritePreparedMessage)
  167. err := http.ListenAndServe(*addr, nil)
  168. if err != nil {
  169. log.Fatal("ListenAndServe: ", err)
  170. }
  171. }
  172. type validator struct {
  173. state int
  174. x rune
  175. r io.Reader
  176. }
  177. var errInvalidUTF8 = errors.New("invalid utf8")
  178. func (r *validator) Read(p []byte) (int, error) {
  179. n, err := r.r.Read(p)
  180. state := r.state
  181. x := r.x
  182. for _, b := range p[:n] {
  183. state, x = decode(state, x, b)
  184. if state == utf8Reject {
  185. break
  186. }
  187. }
  188. r.state = state
  189. r.x = x
  190. if state == utf8Reject || (err == io.EOF && state != utf8Accept) {
  191. return n, errInvalidUTF8
  192. }
  193. return n, err
  194. }
  195. // UTF-8 decoder from http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  196. //
  197. // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  198. //
  199. // Permission is hereby granted, free of charge, to any person obtaining a copy
  200. // of this software and associated documentation files (the "Software"), to
  201. // deal in the Software without restriction, including without limitation the
  202. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  203. // sell copies of the Software, and to permit persons to whom the Software is
  204. // furnished to do so, subject to the following conditions:
  205. //
  206. // The above copyright notice and this permission notice shall be included in
  207. // all copies or substantial portions of the Software.
  208. //
  209. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  210. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  211. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  212. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  213. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  214. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  215. // IN THE SOFTWARE.
  216. var utf8d = [...]byte{
  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, // 00..1f
  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, // 20..3f
  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, // 40..5f
  220. 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
  221. 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
  222. 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
  223. 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
  224. 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
  225. 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
  226. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  227. 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
  228. 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
  229. 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
  230. 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
  231. }
  232. const (
  233. utf8Accept = 0
  234. utf8Reject = 1
  235. )
  236. func decode(state int, x rune, b byte) (int, rune) {
  237. t := utf8d[b]
  238. if state != utf8Accept {
  239. x = rune(b&0x3f) | (x << 6)
  240. } else {
  241. x = rune((0xff >> t) & b)
  242. }
  243. state = int(utf8d[256+state*16+int(t)])
  244. return state, x
  245. }