|
| 1 | +// Copyright © 2022 Meroxa, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain 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, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package grpcutil |
| 16 | + |
| 17 | +import ( |
| 18 | + "bufio" |
| 19 | + "context" |
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/conduitio/conduit/pkg/foundation/log" |
| 24 | + "github.com/gorilla/websocket" |
| 25 | +) |
| 26 | + |
| 27 | +type inMemoryResponseWriter struct { |
| 28 | + io.Writer |
| 29 | + header http.Header |
| 30 | + code int |
| 31 | + closed chan bool |
| 32 | +} |
| 33 | + |
| 34 | +func newInMemoryResponseWriter(writer io.Writer) *inMemoryResponseWriter { |
| 35 | + return &inMemoryResponseWriter{ |
| 36 | + Writer: writer, |
| 37 | + header: http.Header{}, |
| 38 | + closed: make(chan bool, 1), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (w *inMemoryResponseWriter) Write(b []byte) (int, error) { |
| 43 | + return w.Writer.Write(b) |
| 44 | +} |
| 45 | +func (w *inMemoryResponseWriter) Header() http.Header { |
| 46 | + return w.header |
| 47 | +} |
| 48 | +func (w *inMemoryResponseWriter) WriteHeader(code int) { |
| 49 | + w.code = code |
| 50 | +} |
| 51 | +func (w *inMemoryResponseWriter) CloseNotify() <-chan bool { |
| 52 | + return w.closed |
| 53 | +} |
| 54 | +func (w *inMemoryResponseWriter) Flush() {} |
| 55 | + |
| 56 | +// wsProxy is a proxy around a http.Handler which |
| 57 | +// redirects the response data from the http.Handler |
| 58 | +// to a WebSocket connection. |
| 59 | +type wsProxy struct { |
| 60 | + handler http.Handler |
| 61 | + logger log.CtxLogger |
| 62 | + upgrader websocket.Upgrader |
| 63 | +} |
| 64 | + |
| 65 | +func newWebSocketProxy(handler http.Handler, logger log.CtxLogger) *wsProxy { |
| 66 | + return &wsProxy{ |
| 67 | + handler: handler, |
| 68 | + logger: logger.WithComponent("grpcutil.websocket"), |
| 69 | + upgrader: websocket.Upgrader{ |
| 70 | + ReadBufferSize: 1024, |
| 71 | + WriteBufferSize: 1024, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (p *wsProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 77 | + if !websocket.IsWebSocketUpgrade(r) { |
| 78 | + p.handler.ServeHTTP(w, r) |
| 79 | + return |
| 80 | + } |
| 81 | + p.proxy(w, r) |
| 82 | +} |
| 83 | + |
| 84 | +func (p *wsProxy) proxy(w http.ResponseWriter, r *http.Request) { |
| 85 | + ctx, cancelFn := context.WithCancel(r.Context()) |
| 86 | + defer cancelFn() |
| 87 | + |
| 88 | + // upgrade connection to WebSocket |
| 89 | + conn, err := p.upgrader.Upgrade(w, r, http.Header{}) |
| 90 | + if err != nil { |
| 91 | + p.logger.Err(ctx, err).Msg("error upgrading websocket") |
| 92 | + return |
| 93 | + } |
| 94 | + defer conn.Close() |
| 95 | + |
| 96 | + // We use a pipe to read the data |
| 97 | + // being written to the underlying http.Handler |
| 98 | + responseR, responseW := io.Pipe() |
| 99 | + response := newInMemoryResponseWriter(responseW) |
| 100 | + go func() { |
| 101 | + <-ctx.Done() |
| 102 | + p.logger.Debug(ctx).Err(ctx.Err()).Msg("closing pipes") |
| 103 | + responseW.CloseWithError(io.EOF) |
| 104 | + response.closed <- true |
| 105 | + }() |
| 106 | + |
| 107 | + go func() { |
| 108 | + defer cancelFn() |
| 109 | + p.handler.ServeHTTP(response, r) |
| 110 | + }() |
| 111 | + |
| 112 | + scanner := bufio.NewScanner(responseR) |
| 113 | + |
| 114 | + for scanner.Scan() { |
| 115 | + if len(scanner.Bytes()) == 0 { |
| 116 | + p.logger.Warn(ctx).Err(scanner.Err()).Msg("[write] empty scan") |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + p.logger.Trace(ctx).Msgf("[write] scanned %v", scanner.Text()) |
| 121 | + if err := conn.WriteMessage(websocket.TextMessage, scanner.Bytes()); err != nil { |
| 122 | + p.logger.Warn(ctx).Err(err).Msg("[write] error writing websocket message") |
| 123 | + return |
| 124 | + } |
| 125 | + } |
| 126 | + // todo properly communicate the error to the client |
| 127 | + // and close the connection |
| 128 | + if sErr := scanner.Err(); sErr != nil { |
| 129 | + p.logger.Err(ctx, sErr).Msg("scanner err") |
| 130 | + } |
| 131 | +} |
0 commit comments