server.go 7.4 KB

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