@@ -27,12 +27,13 @@ type RequestMutatorFunc func(incoming *http.Request, outgoing *http.Request) *ht
27
27
28
28
// Proxy provides websocket transport upgrade to compatible endpoints.
29
29
type Proxy struct {
30
- h http.Handler
31
- logger Logger
32
- methodOverrideParam string
33
- tokenCookieName string
34
- requestMutator RequestMutatorFunc
35
- headerForwarder func (header string ) bool
30
+ h http.Handler
31
+ logger Logger
32
+ maxRespBodyBufferBytes int
33
+ methodOverrideParam string
34
+ tokenCookieName string
35
+ requestMutator RequestMutatorFunc
36
+ headerForwarder func (header string ) bool
36
37
}
37
38
38
39
// Logger collects log messages.
@@ -52,6 +53,15 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
52
53
// Option allows customization of the proxy.
53
54
type Option func (* Proxy )
54
55
56
+ // WithMaxRespBodyBufferSize allows specification of a custom size for the
57
+ // buffer used while reading the response body. By default, the bufio.Scanner
58
+ // used to read the response body sets the maximum token size to MaxScanTokenSize.
59
+ func WithMaxRespBodyBufferSize (nBytes int ) Option {
60
+ return func (p * Proxy ) {
61
+ p .maxRespBodyBufferBytes = nBytes
62
+ }
63
+ }
64
+
55
65
// WithMethodParamOverride allows specification of the special http parameter that is used in the proxied streaming request.
56
66
func WithMethodParamOverride (param string ) Option {
57
67
return func (p * Proxy ) {
@@ -234,6 +244,14 @@ func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) {
234
244
}()
235
245
// write loop -- take messages from response and write to websocket
236
246
scanner := bufio .NewScanner (responseBodyR )
247
+
248
+ // if maxRespBodyBufferSize has been specified, use custom buffer for scanner
249
+ var scannerBuf []byte
250
+ if p .maxRespBodyBufferBytes > 0 {
251
+ scannerBuf = make ([]byte , 0 , 64 * 1024 )
252
+ scanner .Buffer (scannerBuf , p .maxRespBodyBufferBytes )
253
+ }
254
+
237
255
for scanner .Scan () {
238
256
if len (scanner .Bytes ()) == 0 {
239
257
p .logger .Warnln ("[write] empty scan" , scanner .Err ())
0 commit comments