mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.0 KiB
83 lines
2.0 KiB
9 years ago
|
/*
|
||
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||
|
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package client
|
||
9 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"net/http/httputil"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
type debugTransport struct {
|
||
|
// Writer is the logging destination
|
||
|
Writer io.Writer
|
||
|
|
||
|
http.RoundTripper
|
||
|
}
|
||
|
|
||
9 years ago
|
// NewDebugTransport returns a debugging implementation of a RoundTripper.
|
||
9 years ago
|
func NewDebugTransport(rt http.RoundTripper) http.RoundTripper {
|
||
|
return debugTransport{
|
||
|
RoundTripper: rt,
|
||
|
Writer: os.Stderr,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (tr debugTransport) CancelRequest(req *http.Request) {
|
||
|
type canceler interface {
|
||
|
CancelRequest(*http.Request)
|
||
|
}
|
||
|
if cr, ok := tr.transport().(canceler); ok {
|
||
|
cr.CancelRequest(req)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (tr debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||
|
tr.logRequest(req)
|
||
|
resp, err := tr.transport().RoundTrip(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
tr.logResponse(resp)
|
||
|
return resp, err
|
||
|
}
|
||
|
|
||
|
func (tr debugTransport) transport() http.RoundTripper {
|
||
|
if tr.RoundTripper != nil {
|
||
|
return tr.RoundTripper
|
||
|
}
|
||
|
return http.DefaultTransport
|
||
|
}
|
||
|
|
||
|
func (tr debugTransport) logRequest(req *http.Request) {
|
||
|
dump, err := httputil.DumpRequestOut(req, true)
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(tr.Writer, "%s: %s\n", "could not dump request", err)
|
||
|
}
|
||
|
fmt.Fprint(tr.Writer, string(dump))
|
||
|
}
|
||
|
|
||
|
func (tr debugTransport) logResponse(resp *http.Response) {
|
||
|
dump, err := httputil.DumpResponse(resp, true)
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(tr.Writer, "%s: %s\n", "could not dump response", err)
|
||
|
}
|
||
|
fmt.Fprint(tr.Writer, string(dump))
|
||
|
}
|