server.go 7.5 KB

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