@@ -236,7 +236,7 @@ func (p proxyHandler) tunnel(name string, rw http.ResponseWriter, req *http.Requ
236
236
237
237
cc = []copier {
238
238
{"outbound " + name , crw , req .Body },
239
- {"inbound " + name , writeFlusher { rw , rc } , crw },
239
+ {"inbound " + name , makeH2Writer ( rw , rc , req ) , crw },
240
240
}
241
241
default :
242
242
err := fmt .Errorf ("unsupported protocol version: %d" , req .ProtoMajor )
@@ -337,27 +337,40 @@ func (p proxyHandler) handleRequest(rw http.ResponseWriter, req *http.Request) {
337
337
}
338
338
}
339
339
340
- type writeFlusher struct {
341
- rw io.Writer
342
- rc * http.ResponseController
340
+ type h2Writer struct {
341
+ w io.Writer
342
+ flush func () error
343
+ close func () error
343
344
}
344
345
345
- func (w writeFlusher ) Write (p []byte ) (n int , err error ) {
346
- n , err = w .rw .Write (p )
346
+ func makeH2Writer (rw http.ResponseWriter , rc * http.ResponseController , req * http.Request ) h2Writer {
347
+ return h2Writer {
348
+ w : rw ,
349
+ flush : rc .Flush ,
350
+ close : req .Body .Close ,
351
+ }
352
+ }
353
+
354
+ func (w h2Writer ) Write (p []byte ) (n int , err error ) {
355
+ n , err = w .w .Write (p )
347
356
348
357
if n > 0 {
349
- if err := w .rc . Flush (); err != nil {
358
+ if err := w .flush (); err != nil {
350
359
log .Errorf (context .TODO (), "got error while flushing response back to client: %v" , err )
351
360
}
352
361
}
353
362
354
363
return
355
364
}
356
365
357
- func (w writeFlusher ) CloseWrite () error {
358
- // This is a nop implementation of closeWriter.
359
- // It avoids printing the error log "cannot close write side of inbound CONNECT tunnel".
360
- return nil
366
+ func (w h2Writer ) CloseWrite () error {
367
+ // Send any DATA frames buffered in the transport.
368
+ if err := w .flush (); err != nil {
369
+ log .Errorf (context .TODO (), "got error while flushing response back to client: %v" , err )
370
+ }
371
+ // Close request body to signal the end of the request.
372
+ // This results RST_STREAM frame with error code NO_ERROR to be sent to the other side.
373
+ return w .close ()
361
374
}
362
375
363
376
func (p proxyHandler ) writeErrorResponse (rw http.ResponseWriter , req * http.Request , err error ) {
0 commit comments