(request *http.Request)
| 34 | } |
| 35 | |
| 36 | func (i InMemWebsocketRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { |
| 37 | i.Logger.Debug(context.Background(), "round trip start") |
| 38 | defer i.Logger.Debug(context.Background(), "round trip end") |
| 39 | newCtx := i.CtxMutator(request.Context()) |
| 40 | request = request.WithContext(newCtx) |
| 41 | serverP, clientP := net.Pipe() |
| 42 | var _ io.ReadWriteCloser = clientP // compile time check that response body is OK for websocket |
| 43 | response := &http.Response{ |
| 44 | Header: make(http.Header), |
| 45 | Body: clientP, |
| 46 | } |
| 47 | rw := newInMemWebsocketResponseWriter(response, serverP) |
| 48 | go func() { |
| 49 | i.Handler.ServeHTTP(rw, request) |
| 50 | if !rw.hijacked { |
| 51 | i.Logger.Debug(context.Background(), "closing connection after handler did not hijack") |
| 52 | // If the handler didn't hijack the connection, we should close it when the handler finishes. |
| 53 | // This prevents a 3s delay in websocket.Dial() reading the non-upgraded response. |
| 54 | _ = serverP.Close() |
| 55 | } |
| 56 | }() |
| 57 | select { |
| 58 | case <-newCtx.Done(): |
| 59 | return nil, newCtx.Err() |
| 60 | case <-rw.gotHeaders: |
| 61 | return response, nil |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func newInMemWebsocketResponseWriter(resp *http.Response, conn net.Conn) *inMemWebsocketResponseWriter { |
| 66 | r := bufio.NewReader(conn) |
nothing calls this directly
no test coverage detected