rawConnWrapper.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 2018 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package test
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io"
  21. "net"
  22. "strings"
  23. "sync"
  24. "time"
  25. "golang.org/x/net/http2"
  26. "golang.org/x/net/http2/hpack"
  27. )
  28. type listenerWrapper struct {
  29. net.Listener
  30. mu sync.Mutex
  31. rcw *rawConnWrapper
  32. }
  33. func listenWithConnControl(network, address string) (net.Listener, error) {
  34. l, err := net.Listen(network, address)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return &listenerWrapper{Listener: l}, nil
  39. }
  40. // Accept blocks until Dial is called, then returns a net.Conn for the server
  41. // half of the connection.
  42. func (l *listenerWrapper) Accept() (net.Conn, error) {
  43. c, err := l.Listener.Accept()
  44. if err != nil {
  45. return nil, err
  46. }
  47. l.mu.Lock()
  48. l.rcw = newRawConnWrapperFromConn(c)
  49. l.mu.Unlock()
  50. return c, nil
  51. }
  52. func (l *listenerWrapper) getLastConn() *rawConnWrapper {
  53. l.mu.Lock()
  54. defer l.mu.Unlock()
  55. return l.rcw
  56. }
  57. type dialerWrapper struct {
  58. c net.Conn
  59. rcw *rawConnWrapper
  60. }
  61. func (d *dialerWrapper) dialer(target string, t time.Duration) (net.Conn, error) {
  62. c, err := net.DialTimeout("tcp", target, t)
  63. d.c = c
  64. d.rcw = newRawConnWrapperFromConn(c)
  65. return c, err
  66. }
  67. func (d *dialerWrapper) getRawConnWrapper() *rawConnWrapper {
  68. return d.rcw
  69. }
  70. type rawConnWrapper struct {
  71. cc io.ReadWriteCloser
  72. fr *http2.Framer
  73. // writing headers:
  74. headerBuf bytes.Buffer
  75. hpackEnc *hpack.Encoder
  76. // reading frames:
  77. frc chan http2.Frame
  78. frErrc chan error
  79. readTimer *time.Timer
  80. }
  81. func newRawConnWrapperFromConn(cc io.ReadWriteCloser) *rawConnWrapper {
  82. rcw := &rawConnWrapper{
  83. cc: cc,
  84. frc: make(chan http2.Frame, 1),
  85. frErrc: make(chan error, 1),
  86. }
  87. rcw.hpackEnc = hpack.NewEncoder(&rcw.headerBuf)
  88. rcw.fr = http2.NewFramer(cc, cc)
  89. rcw.fr.ReadMetaHeaders = hpack.NewDecoder(4096 /*initialHeaderTableSize*/, nil)
  90. return rcw
  91. }
  92. func (rcw *rawConnWrapper) Close() error {
  93. return rcw.cc.Close()
  94. }
  95. func (rcw *rawConnWrapper) readFrame() (http2.Frame, error) {
  96. go func() {
  97. fr, err := rcw.fr.ReadFrame()
  98. if err != nil {
  99. rcw.frErrc <- err
  100. } else {
  101. rcw.frc <- fr
  102. }
  103. }()
  104. t := time.NewTimer(2 * time.Second)
  105. defer t.Stop()
  106. select {
  107. case f := <-rcw.frc:
  108. return f, nil
  109. case err := <-rcw.frErrc:
  110. return nil, err
  111. case <-t.C:
  112. return nil, fmt.Errorf("timeout waiting for frame")
  113. }
  114. }
  115. // greet initiates the client's HTTP/2 connection into a state where
  116. // frames may be sent.
  117. func (rcw *rawConnWrapper) greet() error {
  118. rcw.writePreface()
  119. rcw.writeInitialSettings()
  120. rcw.wantSettings()
  121. rcw.writeSettingsAck()
  122. for {
  123. f, err := rcw.readFrame()
  124. if err != nil {
  125. return err
  126. }
  127. switch f := f.(type) {
  128. case *http2.WindowUpdateFrame:
  129. // grpc's transport/http2_server sends this
  130. // before the settings ack. The Go http2
  131. // server uses a setting instead.
  132. case *http2.SettingsFrame:
  133. if f.IsAck() {
  134. return nil
  135. }
  136. return fmt.Errorf("during greet, got non-ACK settings frame")
  137. default:
  138. return fmt.Errorf("during greet, unexpected frame type %T", f)
  139. }
  140. }
  141. }
  142. func (rcw *rawConnWrapper) writePreface() error {
  143. n, err := rcw.cc.Write([]byte(http2.ClientPreface))
  144. if err != nil {
  145. return fmt.Errorf("error writing client preface: %v", err)
  146. }
  147. if n != len(http2.ClientPreface) {
  148. return fmt.Errorf("writing client preface, wrote %d bytes; want %d", n, len(http2.ClientPreface))
  149. }
  150. return nil
  151. }
  152. func (rcw *rawConnWrapper) writeInitialSettings() error {
  153. if err := rcw.fr.WriteSettings(); err != nil {
  154. return fmt.Errorf("error writing initial SETTINGS frame from client to server: %v", err)
  155. }
  156. return nil
  157. }
  158. func (rcw *rawConnWrapper) writeSettingsAck() error {
  159. if err := rcw.fr.WriteSettingsAck(); err != nil {
  160. return fmt.Errorf("error writing ACK of server's SETTINGS: %v", err)
  161. }
  162. return nil
  163. }
  164. func (rcw *rawConnWrapper) wantSettings() (*http2.SettingsFrame, error) {
  165. f, err := rcw.readFrame()
  166. if err != nil {
  167. return nil, fmt.Errorf("error while expecting a SETTINGS frame: %v", err)
  168. }
  169. sf, ok := f.(*http2.SettingsFrame)
  170. if !ok {
  171. return nil, fmt.Errorf("got a %T; want *SettingsFrame", f)
  172. }
  173. return sf, nil
  174. }
  175. func (rcw *rawConnWrapper) wantSettingsAck() error {
  176. f, err := rcw.readFrame()
  177. if err != nil {
  178. return err
  179. }
  180. sf, ok := f.(*http2.SettingsFrame)
  181. if !ok {
  182. return fmt.Errorf("wanting a settings ACK, received a %T", f)
  183. }
  184. if !sf.IsAck() {
  185. return fmt.Errorf("settings Frame didn't have ACK set")
  186. }
  187. return nil
  188. }
  189. // wait for any activity from the server
  190. func (rcw *rawConnWrapper) wantAnyFrame() (http2.Frame, error) {
  191. f, err := rcw.fr.ReadFrame()
  192. if err != nil {
  193. return nil, err
  194. }
  195. return f, nil
  196. }
  197. func (rcw *rawConnWrapper) encodeHeaderField(k, v string) error {
  198. err := rcw.hpackEnc.WriteField(hpack.HeaderField{Name: k, Value: v})
  199. if err != nil {
  200. return fmt.Errorf("HPACK encoding error for %q/%q: %v", k, v, err)
  201. }
  202. return nil
  203. }
  204. // encodeHeader encodes headers and returns their HPACK bytes. headers
  205. // must contain an even number of key/value pairs. There may be
  206. // multiple pairs for keys (e.g. "cookie"). The :method, :path, and
  207. // :scheme headers default to GET, / and https.
  208. func (rcw *rawConnWrapper) encodeHeader(headers ...string) []byte {
  209. if len(headers)%2 == 1 {
  210. panic("odd number of kv args")
  211. }
  212. rcw.headerBuf.Reset()
  213. if len(headers) == 0 {
  214. // Fast path, mostly for benchmarks, so test code doesn't pollute
  215. // profiles when we're looking to improve server allocations.
  216. rcw.encodeHeaderField(":method", "GET")
  217. rcw.encodeHeaderField(":path", "/")
  218. rcw.encodeHeaderField(":scheme", "https")
  219. return rcw.headerBuf.Bytes()
  220. }
  221. if len(headers) == 2 && headers[0] == ":method" {
  222. // Another fast path for benchmarks.
  223. rcw.encodeHeaderField(":method", headers[1])
  224. rcw.encodeHeaderField(":path", "/")
  225. rcw.encodeHeaderField(":scheme", "https")
  226. return rcw.headerBuf.Bytes()
  227. }
  228. pseudoCount := map[string]int{}
  229. keys := []string{":method", ":path", ":scheme"}
  230. vals := map[string][]string{
  231. ":method": {"GET"},
  232. ":path": {"/"},
  233. ":scheme": {"https"},
  234. }
  235. for len(headers) > 0 {
  236. k, v := headers[0], headers[1]
  237. headers = headers[2:]
  238. if _, ok := vals[k]; !ok {
  239. keys = append(keys, k)
  240. }
  241. if strings.HasPrefix(k, ":") {
  242. pseudoCount[k]++
  243. if pseudoCount[k] == 1 {
  244. vals[k] = []string{v}
  245. } else {
  246. // Allows testing of invalid headers w/ dup pseudo fields.
  247. vals[k] = append(vals[k], v)
  248. }
  249. } else {
  250. vals[k] = append(vals[k], v)
  251. }
  252. }
  253. for _, k := range keys {
  254. for _, v := range vals[k] {
  255. rcw.encodeHeaderField(k, v)
  256. }
  257. }
  258. return rcw.headerBuf.Bytes()
  259. }
  260. func (rcw *rawConnWrapper) writeHeadersGRPC(streamID uint32, path string) {
  261. rcw.writeHeaders(http2.HeadersFrameParam{
  262. StreamID: streamID,
  263. BlockFragment: rcw.encodeHeader(
  264. ":method", "POST",
  265. ":path", path,
  266. "content-type", "application/grpc",
  267. "te", "trailers",
  268. ),
  269. EndStream: false,
  270. EndHeaders: true,
  271. })
  272. }
  273. func (rcw *rawConnWrapper) writeHeaders(p http2.HeadersFrameParam) error {
  274. if err := rcw.fr.WriteHeaders(p); err != nil {
  275. return fmt.Errorf("error writing HEADERS: %v", err)
  276. }
  277. return nil
  278. }
  279. func (rcw *rawConnWrapper) writeData(streamID uint32, endStream bool, data []byte) error {
  280. if err := rcw.fr.WriteData(streamID, endStream, data); err != nil {
  281. return fmt.Errorf("error writing DATA: %v", err)
  282. }
  283. return nil
  284. }
  285. func (rcw *rawConnWrapper) writeRSTStream(streamID uint32, code http2.ErrCode) error {
  286. if err := rcw.fr.WriteRSTStream(streamID, code); err != nil {
  287. return fmt.Errorf("error writing RST_STREAM: %v", err)
  288. }
  289. return nil
  290. }
  291. func (rcw *rawConnWrapper) writeDataPadded(streamID uint32, endStream bool, data, padding []byte) error {
  292. if err := rcw.fr.WriteDataPadded(streamID, endStream, data, padding); err != nil {
  293. return fmt.Errorf("error writing DATA with padding: %v", err)
  294. }
  295. return nil
  296. }
  297. func (rcw *rawConnWrapper) writeGoAway(maxStreamID uint32, code http2.ErrCode, debugData []byte) error {
  298. if err := rcw.fr.WriteGoAway(maxStreamID, code, debugData); err != nil {
  299. return fmt.Errorf("error writing GoAway: %v", err)
  300. }
  301. return nil
  302. }
  303. func (rcw *rawConnWrapper) writeRawFrame(t http2.FrameType, flags http2.Flags, streamID uint32, payload []byte) error {
  304. if err := rcw.fr.WriteRawFrame(t, flags, streamID, payload); err != nil {
  305. return fmt.Errorf("error writing Raw Frame: %v", err)
  306. }
  307. return nil
  308. }