2015-02-13 23:51:01 +00:00
|
|
|
package hawser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2015-03-05 19:49:15 +00:00
|
|
|
"fmt"
|
2015-03-05 18:39:37 +00:00
|
|
|
"github.com/rubyist/tracerx"
|
2015-03-05 19:49:15 +00:00
|
|
|
"io"
|
2015-02-13 23:51:01 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-03-05 19:49:15 +00:00
|
|
|
"strings"
|
2015-02-13 23:51:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func DoHTTP(c *Configuration, req *http.Request) (*http.Response, error) {
|
2015-03-05 18:39:37 +00:00
|
|
|
var res *http.Response
|
|
|
|
var err error
|
|
|
|
|
2015-03-05 19:49:15 +00:00
|
|
|
traceHttpRequest(c, req)
|
2015-03-05 18:39:37 +00:00
|
|
|
|
2015-02-13 23:51:01 +00:00
|
|
|
switch req.Method {
|
|
|
|
case "GET", "HEAD":
|
2015-03-05 18:39:37 +00:00
|
|
|
res, err = c.RedirectingHttpClient().Do(req)
|
2015-02-13 23:51:01 +00:00
|
|
|
default:
|
2015-03-05 18:39:37 +00:00
|
|
|
res, err = c.HttpClient().Do(req)
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
2015-03-05 18:39:37 +00:00
|
|
|
|
2015-03-05 19:49:15 +00:00
|
|
|
traceHttpResponse(c, res)
|
2015-03-05 18:39:37 +00:00
|
|
|
|
|
|
|
return res, err
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) HttpClient() *http.Client {
|
|
|
|
if c.httpClient == nil {
|
|
|
|
c.httpClient = &http.Client{
|
|
|
|
Transport: c.RedirectingHttpClient().Transport,
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return RedirectError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.httpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) RedirectingHttpClient() *http.Client {
|
|
|
|
if c.redirectingHttpClient == nil {
|
2015-03-05 18:49:21 +00:00
|
|
|
tr := &http.Transport{}
|
|
|
|
sslVerify, _ := c.GitConfig("http.sslverify")
|
|
|
|
if sslVerify == "false" || len(os.Getenv("GIT_SSL_NO_VERIFY")) > 0 {
|
|
|
|
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
2015-03-05 18:49:21 +00:00
|
|
|
c.redirectingHttpClient = &http.Client{Transport: tr}
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
|
|
|
return c.redirectingHttpClient
|
|
|
|
}
|
2015-03-05 19:49:15 +00:00
|
|
|
|
|
|
|
var tracedTypes = []string{"json", "text", "xml", "html"}
|
|
|
|
|
|
|
|
func traceHttpRequest(c *Configuration, req *http.Request) {
|
|
|
|
tracerx.Printf("HTTP: %s %s", req.Method, req.URL.String())
|
|
|
|
|
|
|
|
if c.isTracingHttp == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-05 20:15:04 +00:00
|
|
|
fmt.Fprintf(c.OutputWriter, "> %s %s %s\n", req.Method, req.URL.RequestURI(), req.Proto)
|
2015-03-05 19:49:15 +00:00
|
|
|
for key, _ := range req.Header {
|
2015-03-05 20:15:04 +00:00
|
|
|
fmt.Fprintf(c.OutputWriter, "> %s: %s\n", key, req.Header.Get(key))
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func traceHttpResponse(c *Configuration, res *http.Response) {
|
|
|
|
tracerx.Printf("HTTP: %d", res.StatusCode)
|
|
|
|
|
|
|
|
if c.isTracingHttp == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-05 20:04:00 +00:00
|
|
|
fmt.Fprintf(c.OutputWriter, "< %s %s\n", res.Proto, res.Status)
|
2015-03-05 19:49:15 +00:00
|
|
|
for key, _ := range res.Header {
|
2015-03-05 20:04:00 +00:00
|
|
|
fmt.Fprintf(c.OutputWriter, "< %s: %s\n", key, res.Header.Get(key))
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
traceBody := false
|
|
|
|
ctype := strings.ToLower(strings.SplitN(res.Header.Get("Content-Type"), ";", 2)[0])
|
|
|
|
for _, tracedType := range tracedTypes {
|
|
|
|
if strings.Contains(ctype, tracedType) {
|
|
|
|
traceBody = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if traceBody {
|
2015-03-05 20:04:00 +00:00
|
|
|
fmt.Fprintf(c.OutputWriter, "\n")
|
|
|
|
res.Body = newTracedBody(res.Body, c.OutputWriter)
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tracedBody struct {
|
2015-03-05 20:04:00 +00:00
|
|
|
body io.ReadCloser
|
|
|
|
output io.Writer
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tracedBody) Read(p []byte) (int, error) {
|
|
|
|
n, err := r.body.Read(p)
|
2015-03-05 20:04:00 +00:00
|
|
|
fmt.Fprintf(r.output, "%s\n", string(p[0:n]))
|
2015-03-05 19:49:15 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tracedBody) Close() error {
|
|
|
|
return r.body.Close()
|
|
|
|
}
|
|
|
|
|
2015-03-05 20:04:00 +00:00
|
|
|
func newTracedBody(body io.ReadCloser, output io.Writer) *tracedBody {
|
|
|
|
return &tracedBody{body, output}
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|