Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http_send_pack.go « stats « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7a2ed1ef002c90209a54a826320841316cec9bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package stats

import (
	"bytes"
	"compress/gzip"
	"context"
	"fmt"
	"io"
	"net/http"
	"time"

	"gitlab.com/gitlab-org/gitaly/internal/git/pktline"
)

// ResponseBody returns how long it took to receive the last bytes of the response body.
func (p *HTTPSendPack) ResponseBody() time.Duration { return p.stats.responseBody.Sub(p.start) }

// BandPayloadSize returns how many bytes were received on a specific sideband.
func (p *HTTPSendPack) BandPayloadSize(b string) int64 { return p.stats.multiband[b].size }

// BandFirstPacket returns how long it took to receive the first packet on a specific sideband.
func (p *HTTPSendPack) BandFirstPacket(b string) time.Duration {
	return p.stats.multiband[b].firstPacket.Sub(p.start)
}

// HTTPSendPack encapsulates statistics about an emulated git-send-pack(1) request.
type HTTPSendPack struct {
	start  time.Time
	header time.Time
	stats  SendPack
}

func buildSendPackRequest(
	ctx context.Context,
	url, user, password string,
	commands []PushCommand,
	packfile io.Reader,
) (*http.Request, error) {
	var requestBuffer bytes.Buffer
	zipper := gzip.NewWriter(&requestBuffer)

	for i, command := range commands {
		c := fmt.Sprintf("%s %s %s", command.OldOID, command.NewOID, command.Reference)
		if i == 0 {
			c += "\x00side-band-64k report-status delete-refs"
		}

		if _, err := pktline.WriteString(zipper, c); err != nil {
			return nil, fmt.Errorf("writing command: %w", err)
		}
	}

	if err := pktline.WriteFlush(zipper); err != nil {
		return nil, fmt.Errorf("terminating command list: %w", err)
	}

	if packfile != nil {
		if _, err := io.Copy(zipper, packfile); err != nil {
			return nil, fmt.Errorf("sending packfile: %w", err)
		}
	}

	if err := zipper.Close(); err != nil {
		return nil, fmt.Errorf("finalizing request body: %w", err)
	}

	request, err := http.NewRequest("POST", url+"/git-receive-pack", &requestBuffer)
	if err != nil {
		return nil, fmt.Errorf("creating request: %w", err)
	}
	request = request.WithContext(ctx)

	if user != "" {
		request.SetBasicAuth(user, password)
	}

	for k, v := range map[string]string{
		"User-Agent":       "gitaly-debug",
		"Content-Type":     "application/x-git-receive-pack-request",
		"Accept":           "application/x-git-receive-pack-result",
		"Content-Encoding": "gzip",
	} {
		request.Header.Set(k, v)
	}

	return request, nil
}

func performHTTPSendPack(
	ctx context.Context,
	url, user, password string,
	commands []PushCommand,
	packfile io.Reader,
	reportProgress func(string, ...interface{}),
) (HTTPSendPack, error) {
	request, err := buildSendPackRequest(ctx, url, user, password, commands, packfile)
	if err != nil {
		return HTTPSendPack{}, err
	}

	reportProgress("---\n")
	reportProgress("--- git-send-pack %v\n", request.URL)
	reportProgress("---\n")

	sendPack := HTTPSendPack{
		start: time.Now(),
		stats: SendPack{
			ReportProgress: func(b []byte) { reportProgress("%s", string(b)) },
		},
	}

	response, err := http.DefaultClient.Do(request)
	if err != nil {
		return HTTPSendPack{}, fmt.Errorf("creating send-pack request: %w", err)
	}
	defer response.Body.Close()

	if code := response.StatusCode; code < 200 || code >= 400 {
		return HTTPSendPack{}, fmt.Errorf("unexpected HTTP status: %d", code)
	}

	sendPack.header = time.Now()
	reportProgress("response code: %d\n", response.StatusCode)
	reportProgress("response header: %v\n", response.Header)

	body := response.Body
	if response.Header.Get("Content-Encoding") == "gzip" {
		body, err = gzip.NewReader(body)
		if err != nil {
			return HTTPSendPack{}, fmt.Errorf("setting up gzip reader: %w", err)
		}
	}

	if err := sendPack.stats.Parse(body); err != nil {
		return HTTPSendPack{}, fmt.Errorf("parsing packfile response: %w", err)
	}

	return sendPack, nil
}