2015-03-19 19:30:55 +00:00
|
|
|
package lfs
|
2015-02-13 23:51:01 +00:00
|
|
|
|
|
|
|
import (
|
2015-06-03 21:33:00 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2015-02-13 23:51:01 +00:00
|
|
|
"crypto/tls"
|
2015-03-26 17:10:17 +00:00
|
|
|
"errors"
|
2015-03-05 19:49:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-04-10 18:43:58 +00:00
|
|
|
"net"
|
2015-02-13 23:51:01 +00:00
|
|
|
"net/http"
|
2015-06-03 21:33:00 +00:00
|
|
|
"net/http/httputil"
|
2015-02-13 23:51:01 +00:00
|
|
|
"os"
|
2015-06-05 15:13:57 +00:00
|
|
|
"path/filepath"
|
2015-03-05 19:49:15 +00:00
|
|
|
"strings"
|
2015-06-04 18:36:08 +00:00
|
|
|
"sync"
|
2015-04-10 18:43:58 +00:00
|
|
|
"time"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2015-05-25 18:20:50 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
|
2015-02-13 23:51:01 +00:00
|
|
|
)
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
type transferStats struct {
|
|
|
|
HeaderSize int
|
|
|
|
BodySize int
|
|
|
|
Start time.Time
|
|
|
|
Stop time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type transfer struct {
|
|
|
|
requestStats *transferStats
|
|
|
|
responseStats *transferStats
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// TODO should use some locks
|
2015-06-04 18:36:08 +00:00
|
|
|
transfers = make(map[*http.Response]*transfer)
|
|
|
|
transferBuckets = make(map[string][]*http.Response)
|
|
|
|
transfersLock sync.Mutex
|
|
|
|
transferBucketsLock sync.Mutex
|
2015-06-03 21:33:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func LogTransfer(key string, res *http.Response) {
|
2015-06-04 18:30:47 +00:00
|
|
|
if Config.isLoggingStats {
|
2015-06-04 18:36:08 +00:00
|
|
|
transferBucketsLock.Lock()
|
2015-06-04 18:30:47 +00:00
|
|
|
transferBuckets[key] = append(transferBuckets[key], res)
|
2015-06-04 18:36:08 +00:00
|
|
|
transferBucketsLock.Unlock()
|
2015-06-04 18:30:47 +00:00
|
|
|
}
|
2015-06-03 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HttpClient struct {
|
|
|
|
*http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HttpClient) Do(req *http.Request) (*http.Response, error) {
|
|
|
|
traceHttpRequest(req)
|
|
|
|
|
|
|
|
crc := countingRequest(req)
|
|
|
|
if req.Body != nil {
|
|
|
|
// Only set the body if we have a body, but create the countingRequest
|
|
|
|
// anyway to make using zeroed stats easier.
|
|
|
|
req.Body = crc
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
res, err := c.Client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
traceHttpResponse(res)
|
|
|
|
|
|
|
|
cresp := countingResponse(res)
|
|
|
|
res.Body = cresp
|
|
|
|
|
2015-06-04 18:30:47 +00:00
|
|
|
if Config.isLoggingStats {
|
|
|
|
reqHeaderSize := 0
|
2015-06-05 15:13:57 +00:00
|
|
|
resHeaderSize := 0
|
|
|
|
|
|
|
|
if dump, err := httputil.DumpRequest(req, false); err == nil {
|
2015-06-04 18:30:47 +00:00
|
|
|
reqHeaderSize = len(dump)
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:13:57 +00:00
|
|
|
if dump, err := httputil.DumpResponse(res, false); err == nil {
|
|
|
|
resHeaderSize = len(dump)
|
|
|
|
}
|
|
|
|
|
2015-06-04 18:30:47 +00:00
|
|
|
reqstats := &transferStats{HeaderSize: reqHeaderSize, BodySize: crc.Count}
|
2015-06-05 15:13:57 +00:00
|
|
|
|
|
|
|
// Response body size cannot be figured until it is read. Do not rely on a Content-Length
|
|
|
|
// header because it may not exist or be -1 in the case of chunked responses.
|
|
|
|
resstats := &transferStats{HeaderSize: resHeaderSize, Start: start}
|
2015-06-16 22:34:41 +00:00
|
|
|
t := &transfer{requestStats: reqstats, responseStats: resstats}
|
2015-06-04 18:36:08 +00:00
|
|
|
transfersLock.Lock()
|
2015-06-16 22:34:41 +00:00
|
|
|
transfers[res] = t
|
2015-06-04 18:36:08 +00:00
|
|
|
transfersLock.Unlock()
|
2015-06-04 18:30:47 +00:00
|
|
|
}
|
2015-06-03 21:33:00 +00:00
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DoHTTP(req *http.Request) (*http.Response, error) {
|
|
|
|
res, err := Config.HttpClient().Do(req)
|
2015-03-26 18:46:33 +00:00
|
|
|
if res == nil {
|
|
|
|
res = &http.Response{StatusCode: 0, Header: make(http.Header), Request: req}
|
|
|
|
}
|
2015-03-05 18:39:37 +00:00
|
|
|
return res, err
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
func (c *Configuration) HttpClient() *HttpClient {
|
2015-05-12 08:47:34 +00:00
|
|
|
if c.httpClient != nil {
|
|
|
|
return c.httpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
2015-05-12 08:47:34 +00:00
|
|
|
|
|
|
|
sslVerify, _ := c.GitConfig("http.sslverify")
|
2015-06-16 23:30:16 +00:00
|
|
|
if sslVerify == "false" || Config.GetenvBool("GIT_SSL_NO_VERIFY", false) {
|
2015-05-12 08:47:34 +00:00
|
|
|
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
c.httpClient = &HttpClient{
|
|
|
|
&http.Client{Transport: tr, CheckRedirect: checkRedirect},
|
2015-05-12 08:47:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 16:56:25 +00:00
|
|
|
return c.httpClient
|
2015-02-13 23:51:01 +00:00
|
|
|
}
|
2015-03-05 19:49:15 +00:00
|
|
|
|
2015-03-26 17:10:17 +00:00
|
|
|
func checkRedirect(req *http.Request, via []*http.Request) error {
|
|
|
|
if len(via) >= 3 {
|
|
|
|
return errors.New("stopped after 3 redirects")
|
|
|
|
}
|
|
|
|
|
|
|
|
oldest := via[0]
|
|
|
|
for key, _ := range oldest.Header {
|
2015-03-26 17:19:59 +00:00
|
|
|
if key == "Authorization" {
|
|
|
|
if req.URL.Scheme != oldest.URL.Scheme || req.URL.Host != oldest.URL.Host {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 17:10:17 +00:00
|
|
|
req.Header.Set(key, oldest.Header.Get(key))
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:46:33 +00:00
|
|
|
tracerx.Printf("api: redirect %s %s to %s", oldest.Method, oldest.URL, req.URL)
|
|
|
|
|
2015-03-26 17:10:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:49:15 +00:00
|
|
|
var tracedTypes = []string{"json", "text", "xml", "html"}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
func traceHttpRequest(req *http.Request) {
|
2015-03-05 19:49:15 +00:00
|
|
|
tracerx.Printf("HTTP: %s %s", req.Method, req.URL.String())
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
if Config.isTracingHttp == false {
|
2015-03-05 19:49:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
dump, err := httputil.DumpRequest(req, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2015-03-22 20:13:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(dump))
|
|
|
|
for scanner.Scan() {
|
|
|
|
fmt.Fprintf(os.Stderr, "> %s\n", scanner.Text())
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
func traceHttpResponse(res *http.Response) {
|
2015-03-26 18:46:33 +00:00
|
|
|
if res == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:49:15 +00:00
|
|
|
tracerx.Printf("HTTP: %d", res.StatusCode)
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
if Config.isTracingHttp == false {
|
2015-03-05 19:49:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
dump, err := httputil.DumpResponse(res, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(dump))
|
|
|
|
for scanner.Scan() {
|
|
|
|
fmt.Fprintf(os.Stderr, "< %s\n", scanner.Text())
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
func countingRequest(req *http.Request) *countingReadCloser {
|
|
|
|
return &countingReadCloser{request: req, ReadCloser: req.Body}
|
|
|
|
}
|
2015-03-22 18:30:54 +00:00
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
func countingResponse(res *http.Response) *countingReadCloser {
|
|
|
|
return &countingReadCloser{response: res, ReadCloser: res.Body}
|
2015-03-05 21:18:02 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
type countingReadCloser struct {
|
|
|
|
Count int
|
|
|
|
request *http.Request
|
|
|
|
response *http.Response
|
|
|
|
io.ReadCloser
|
2015-03-05 21:18:02 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
func (c *countingReadCloser) Read(b []byte) (int, error) {
|
|
|
|
n, err := c.ReadCloser.Read(b)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return n, err
|
2015-03-22 18:30:54 +00:00
|
|
|
}
|
2015-03-05 21:18:02 +00:00
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
c.Count += n
|
2015-03-22 18:30:54 +00:00
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
if Config.isTracingHttp {
|
|
|
|
contentType := ""
|
|
|
|
if c.response != nil { // Response, only print certain kinds of data
|
|
|
|
contentType = strings.ToLower(strings.SplitN(c.response.Header.Get("Content-Type"), ";", 2)[0])
|
|
|
|
} else {
|
|
|
|
contentType = strings.ToLower(strings.SplitN(c.request.Header.Get("Content-Type"), ";", 2)[0])
|
|
|
|
}
|
2015-03-05 21:18:02 +00:00
|
|
|
|
2015-06-03 21:33:00 +00:00
|
|
|
for _, tracedType := range tracedTypes {
|
|
|
|
if strings.Contains(contentType, tracedType) {
|
|
|
|
fmt.Fprint(os.Stderr, string(b[0:n]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 19:49:15 +00:00
|
|
|
|
2015-06-04 18:30:47 +00:00
|
|
|
if err == io.EOF && Config.isLoggingStats {
|
2015-06-03 21:33:00 +00:00
|
|
|
// This transfer is done, we're checking it this way so we can also
|
|
|
|
// catch transfers where the caller forgets to Close() the Body.
|
|
|
|
if c.response != nil {
|
2015-06-04 18:36:08 +00:00
|
|
|
transfersLock.Lock()
|
2015-06-03 21:33:00 +00:00
|
|
|
if transfer, ok := transfers[c.response]; ok {
|
|
|
|
transfer.responseStats.BodySize = c.Count
|
|
|
|
transfer.responseStats.Stop = time.Now()
|
|
|
|
}
|
2015-06-04 18:36:08 +00:00
|
|
|
transfersLock.Unlock()
|
2015-06-03 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-05 19:49:15 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:13:57 +00:00
|
|
|
// LogHttpStats is intended to be called after all HTTP operations for the
|
|
|
|
// commmand have finished. It dumps k/v logs, one line per transfer into
|
|
|
|
// a log file with the current timestamp.
|
2015-06-04 19:18:14 +00:00
|
|
|
func LogHttpStats() {
|
|
|
|
if !Config.isLoggingStats {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:13:57 +00:00
|
|
|
file, err := statsLogFile()
|
2015-06-04 19:18:14 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error logging http stats: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:22:12 +00:00
|
|
|
fmt.Fprintf(file, "concurrent=%d batch=%v time=%d version=%s\n", Config.ConcurrentTransfers(), Config.BatchTransfer(), time.Now().Unix(), Version)
|
|
|
|
|
2015-06-04 19:18:14 +00:00
|
|
|
for key, responses := range transferBuckets {
|
|
|
|
for _, response := range responses {
|
|
|
|
stats := transfers[response]
|
2015-06-05 16:32:20 +00:00
|
|
|
fmt.Fprintf(file, "key=%s reqheader=%d reqbody=%d resheader=%d resbody=%d restime=%d status=%d url=%s\n",
|
2015-06-04 19:18:14 +00:00
|
|
|
key,
|
|
|
|
stats.requestStats.HeaderSize,
|
|
|
|
stats.requestStats.BodySize,
|
|
|
|
stats.responseStats.HeaderSize,
|
|
|
|
stats.responseStats.BodySize,
|
|
|
|
stats.responseStats.Stop.Sub(stats.responseStats.Start).Nanoseconds(),
|
2015-06-05 16:32:20 +00:00
|
|
|
response.StatusCode,
|
2015-06-16 22:34:41 +00:00
|
|
|
response.Request.URL)
|
2015-06-04 19:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "HTTP Stats logged to file %s\n", file.Name())
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:13:57 +00:00
|
|
|
func statsLogFile() (*os.File, error) {
|
2015-06-05 16:37:00 +00:00
|
|
|
logBase := filepath.Join(LocalLogDir, "http")
|
|
|
|
if err := os.MkdirAll(logBase, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:13:57 +00:00
|
|
|
logFile := fmt.Sprintf("http-%d.log", time.Now().Unix())
|
2015-06-05 16:37:00 +00:00
|
|
|
return os.Create(filepath.Join(logBase, logFile))
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|