MCPcopy
hub / github.com/caddyserver/caddy / bufferedBody

Method bufferedBody

modules/caddyhttp/reverseproxy/reverseproxy.go:1471–1496  ·  view source on GitHub ↗

bufferedBody reads originalBody into a buffer with maximum size of limit (-1 for unlimited), then returns a reader for the buffer along with how many bytes were buffered. Always close the return value when done with it, just like if it was the original body! If limit is 0 (which it shouldn't be), th

(originalBody io.ReadCloser, limit int64)

Source from the content-addressed store, hash-verified

1469// TODO: the error during reading is discarded if the limit is negative, should the error be propagated
1470// to upstream/downstream?
1471func (h Handler) bufferedBody(originalBody io.ReadCloser, limit int64) (io.ReadCloser, int64) {
1472 if limit == 0 {
1473 return originalBody, 0
1474 }
1475 var written int64
1476 buf := bufPool.Get().(*bytes.Buffer)
1477 buf.Reset()
1478 if limit > 0 {
1479 var err error
1480 written, err = io.CopyN(buf, originalBody, limit)
1481 if (err != nil && err != io.EOF) || written == limit {
1482 return bodyReadCloser{
1483 Reader: io.MultiReader(buf, originalBody),
1484 buf: buf,
1485 body: originalBody,
1486 }, written
1487 }
1488 } else {
1489 written, _ = io.Copy(buf, originalBody)
1490 }
1491 originalBody.Close() // no point in keeping it open
1492 return bodyReadCloser{
1493 Reader: buf,
1494 buf: buf,
1495 }, written
1496}
1497
1498// cloneRequest makes a semi-deep clone of origReq.
1499//

Callers 3

prepareRequestMethod · 0.95
reverseProxyMethod · 0.95
TestBufferingFunction · 0.80

Calls 3

GetMethod · 0.65
ResetMethod · 0.65
CloseMethod · 0.45

Tested by 1

TestBufferingFunction · 0.64