Revert "trace: Avoid httputil for less memory consumption (#2102)" (#2104)

This reverts commit eafa942a30.
This commit is contained in:
Anis Elleuch
2017-04-02 14:02:35 -07:00
committed by Harshavardhana
parent 1c038760ce
commit a009299433
2 changed files with 54 additions and 64 deletions
+27 -32
View File
@@ -18,8 +18,8 @@ package cmd
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
"github.com/minio/mc/pkg/console"
@@ -45,18 +45,12 @@ func (t traceV2) Request(req *http.Request) (err error) {
// Set a temporary redacted auth
req.Header.Set("Authorization", "AWS **REDACTED**:**REDACTED**")
// Store headers in buf
var buf bytes.Buffer
if err = req.Header.Write(&buf); err != nil {
return err
var reqTrace []byte
reqTrace, err = httputil.DumpRequestOut(req, false) // Only display header
if err == nil {
console.Debug(string(reqTrace))
}
// Add headers to the debug text
reqTrace := append(buf.Bytes(), []byte("\r\n")...)
// Print debug information
console.Debug(string(reqTrace))
// Undo
req.Header.Set("Authorization", origAuth)
}
@@ -65,34 +59,35 @@ func (t traceV2) Request(req *http.Request) (err error) {
// Response - Trace HTTP Response
func (t traceV2) Response(resp *http.Response) (err error) {
// Store headers in buf
var buf bytes.Buffer
if err = resp.Header.Write(&buf); err != nil {
return err
}
// Build debug message and add headers information
respTrace := append(buf.Bytes(), []byte("\r\n")...)
var respTrace []byte
// For errors we make sure to dump response body as well.
if resp.StatusCode != http.StatusOK &&
resp.StatusCode != http.StatusPartialContent &&
resp.StatusCode != http.StatusNoContent {
// Load response body
respBody, rErr := ioutil.ReadAll(resp.Body)
if rErr != nil {
return rErr
respTrace, err = httputil.DumpResponse(resp, true)
} else {
// WORKAROUND for https://github.com/golang/go/issues/13942.
// httputil.DumpResponse does not print response headers for
// all successful calls which have response ContentLength set
// to zero. Keep this workaround until the above bug is fixed.
if resp.ContentLength == 0 {
var buffer bytes.Buffer
if err = resp.Header.Write(&buffer); err != nil {
return err
}
respTrace = buffer.Bytes()
respTrace = append(respTrace, []byte("\r\n")...)
} else {
respTrace, err = httputil.DumpResponse(resp, false)
if err != nil {
return err
}
}
// Append response body to the debug text
respTrace = append(respTrace, respBody...)
respTrace = append(respTrace, []byte("\r\n")...)
}
if err == nil {
console.Debug(string(respTrace))
}
// Print debug
console.Debug(string(respTrace))
// Print TLS certificates if applicable
if globalInsecure && resp.TLS != nil {
dumpTLSCertificates(resp.TLS)
}
+27 -32
View File
@@ -18,8 +18,8 @@ package cmd
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httputil"
"regexp"
"strings"
@@ -54,18 +54,12 @@ func (t traceV4) Request(req *http.Request) (err error) {
// Set a temporary redacted auth
req.Header.Set("Authorization", newAuth)
// Store requests headers in buf
var buf bytes.Buffer
if err = req.Header.Write(&buf); err != nil {
return err
var reqTrace []byte
reqTrace, err = httputil.DumpRequestOut(req, false) // Only display header
if err == nil {
console.Debug(string(reqTrace))
}
// Build debug and add headers
reqTrace := append(buf.Bytes(), []byte("\r\n")...)
// Print debug text
console.Debug(string(reqTrace))
// Undo
req.Header.Set("Authorization", origAuth)
}
@@ -74,34 +68,35 @@ func (t traceV4) Request(req *http.Request) (err error) {
// Response - Trace HTTP Response
func (t traceV4) Response(resp *http.Response) (err error) {
// Store headers in buf
var buf bytes.Buffer
if err = resp.Header.Write(&buf); err != nil {
return err
}
// Build debug text and add headers
respTrace := append(buf.Bytes(), []byte("\r\n")...)
var respTrace []byte
// For errors we make sure to dump response body as well.
if resp.StatusCode != http.StatusOK &&
resp.StatusCode != http.StatusPartialContent &&
resp.StatusCode != http.StatusNoContent {
// Load response body
respBody, rErr := ioutil.ReadAll(resp.Body)
if rErr != nil {
return rErr
respTrace, err = httputil.DumpResponse(resp, true)
} else {
// WORKAROUND for https://github.com/golang/go/issues/13942.
// httputil.DumpResponse does not print response headers for
// all successful calls which have response ContentLength set
// to zero. Keep this workaround until the above bug is fixed.
if resp.ContentLength == 0 {
var buffer bytes.Buffer
if err = resp.Header.Write(&buffer); err != nil {
return err
}
respTrace = buffer.Bytes()
respTrace = append(respTrace, []byte("\r\n")...)
} else {
respTrace, err = httputil.DumpResponse(resp, false)
if err != nil {
return err
}
}
// Add response body to debug text
respTrace = append(respTrace, respBody...)
respTrace = append(respTrace, []byte("\r\n")...)
}
if err == nil {
console.Debug(string(respTrace))
}
// Print debug text
console.Debug(string(respTrace))
// Print TLS certificates when applicable
if globalInsecure && resp.TLS != nil {
dumpTLSCertificates(resp.TLS)
}