MCPcopy
hub / github.com/davecheney/httpstat / readResponseBody

Function readResponseBody

main.go:449–486  ·  view source on GitHub ↗

readResponseBody consumes the body of the response. readResponseBody returns an informational message about the disposition of the response body's contents.

(req *http.Request, resp *http.Response)

Source from the content-addressed store, hash-verified

447// readResponseBody returns an informational message about the
448// disposition of the response body's contents.
449func readResponseBody(req *http.Request, resp *http.Response) string {
450 if isRedirect(resp) || req.Method == http.MethodHead {
451 return ""
452 }
453
454 w := io.Discard
455 msg := color.CyanString("Body discarded")
456
457 if saveOutput || outputFile != "" {
458 filename := outputFile
459
460 if saveOutput {
461 // try to get the filename from the Content-Disposition header
462 // otherwise fall back to the RequestURI
463 if filename = getFilenameFromHeaders(resp.Header); filename == "" {
464 filename = path.Base(req.URL.RequestURI())
465 }
466
467 if filename == "/" {
468 log.Fatalf("No remote filename; specify output filename with -o to save response body")
469 }
470 }
471
472 f, err := os.Create(filename)
473 if err != nil {
474 log.Fatalf("unable to create file %s: %v", filename, err)
475 }
476 defer f.Close()
477 w = f
478 msg = color.CyanString("Body read")
479 }
480
481 if _, err := io.Copy(w, resp.Body); err != nil && w != io.Discard {
482 log.Fatalf("failed to read response body: %v", err)
483 }
484
485 return msg
486}
487
488type headers []string
489

Callers 1

visitFunction · 0.85

Calls 2

isRedirectFunction · 0.85
getFilenameFromHeadersFunction · 0.85

Tested by

no test coverage detected