MCPcopy Index your code
hub / github.com/imroc/req / writeBody

Method writeBody

transfer.go:298–384  ·  view source on GitHub ↗

always closes t.BodyCloser

(w io.Writer, dumps []*dump.Dumper)

Source from the content-addressed store, hash-verified

296
297// always closes t.BodyCloser
298func (t *transferWriter) writeBody(w io.Writer, dumps []*dump.Dumper) (err error) {
299 var ncopy int64
300 closed := false
301 defer func() {
302 if closed || t.BodyCloser == nil {
303 return
304 }
305 if closeErr := t.BodyCloser.Close(); closeErr != nil && err == nil {
306 err = closeErr
307 }
308 }()
309
310 rw := w // raw writer
311 for _, dump := range dumps {
312 if dump.RequestBody() {
313 w = dump.WrapRequestBodyWriter(w)
314 }
315 }
316
317 // Write body. We "unwrap" the body first if it was wrapped in a
318 // nopCloser or readTrackingBody. This is to ensure that we can take advantage of
319 // OS-level optimizations in the event that the body is an
320 // *os.File.
321 if t.Body != nil {
322 body := t.unwrapBody()
323 if chunked(t.TransferEncoding) {
324 if bw, ok := rw.(*bufio.Writer); ok {
325 rw = &internal.FlushAfterChunkWriter{Writer: bw}
326 }
327 cw := internal.NewChunkedWriter(rw)
328 for _, dump := range dumps {
329 if dump.RequestBody() {
330 cw = dump.WrapRequestBodyWriteCloser(cw)
331 }
332 }
333 _, err = t.doBodyCopy(cw, body)
334 if err == nil {
335 err = cw.Close()
336 }
337 } else if t.ContentLength == -1 {
338 dst := w
339 if t.Method == "CONNECT" {
340 dst = bufioFlushWriter{dst}
341 }
342 ncopy, err = t.doBodyCopy(dst, body)
343 } else {
344 ncopy, err = t.doBodyCopy(w, io.LimitReader(body, t.ContentLength))
345 if err != nil {
346 return err
347 }
348 var nextra int64
349 nextra, err = t.doBodyCopy(io.Discard, body)
350 ncopy += nextra
351 }
352 if err != nil {
353 return err
354 }
355 for _, dump := range dumps {

Callers 1

writeRequestMethod · 0.80

Calls 12

unwrapBodyMethod · 0.95
doBodyCopyMethod · 0.95
NewChunkedWriterFunction · 0.92
chunkedFunction · 0.85
WrapRequestBodyWriterMethod · 0.80
DumpDefaultMethod · 0.80
WriteStringMethod · 0.80
RequestBodyMethod · 0.65
ErrorfMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected