Normalize smart HTTP endpoint paths · Entire
Normalize smart HTTP endpoint paths
85113dc→main·
pjbgf·2mo ago·2 files·+56 added/-0 removed
Changes
2
internal/gitproto
Msmarthttp.go+10
Msmarthttp_test.go+46
8 unmodified lines
9
10
11
12
13
14
15
61 unmodified lines
77
78
79
80
81
82
83
2 unmodified lines
86
87
88
89
90
91
92
93
94
95
96
97
98
99
8 unmodified lines
"mime"
"net/http"
"net/url"
"strings"
"github.com/go-git/go-git/v6/plumbing/protocol/packp/capability"
)
61 unmodified lines
if httpClient == nil {
httpClient = &http.Client{Transport: http.DefaultTransport}
}
normalizeEndpointPath(ep)
return &Conn{
Label: label,
Endpoint: ep,
2 unmodified lines
}
}
func normalizeEndpointPath(ep *url.URL) {
if ep == nil {
return
}
ep.Path = strings.TrimRight(ep.Path, "/")
ep.RawPath = strings.TrimRight(ep.RawPath, "/")
}
// NewHTTPTransport creates an http.Transport with optional TLS skip.
func NewHTTPTransport(skipTLS bool) http.RoundTripper {
if !skipTLS {
Minternal/gitproto/smarthttp.go+10
5 unmodified lines
6
7
8
9
10
11
12
24 unmodified lines
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
5 unmodified lines
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
24 unmodified lines
}
}
func TestNewConnStripsTrailingEndpointSlash(t *testing.T) {
ep, err := url.Parse("https://example.com/repo.git///")
if err != nil {
t.Fatalf("parse endpoint: %v", err)
}
var gotURLs []string
conn := NewConn(ep, "source", nil, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
gotURLs = append(gotURLs, req.URL.String())
res := &http.Response{
StatusCode: http.StatusOK,
Request: req,
Body: io.NopCloser(strings.NewReader("").
Header: make(http.Header),
}
if req.Method == http.MethodGet {
res.Header.Set("Content-Type", "application/x-git-upload-pack-advertisement")
res.Body = io.NopCloser(strings.NewReader("0000"))
}
return res, nil
}))
if got, want := conn.Endpoint.Path, "/repo.git"; got != want {
t.Fatalf("Endpoint.Path = %q, want %q", got, want)
}
if _, err := RequestInfoRefs(t.Context(), conn, transport.UploadPackService, ""); err != nil {
t.Fatalf("RequestInfoRefs: %v", err)
}
if _, err := PostRPC(t.Context(), conn, transport.UploadPackService, []byte("0000"), false, "upload-pack test"); err != nil {
t.Fatalf("PostRPC: %v", err)
}
wantURLs := []string{
"https://example.com/repo.git/info/refs?service=git-upload-pack",
"https://example.com/repo.git/git-upload-pack",
}
if len(gotURLs) != len(wantURLs) {
t.Fatalf("got %d request URLs, want %d: %v", len(gotURLs), len(wantURLs), gotURLs)
}
for i := range wantURLs {
if gotURLs[i] != wantURLs[i] {
t.Fatalf("request URL %d = %q, want %q", i, gotURLs[i], wantURLs[i])
}
}
}
func TestNewHTTPTransport(t *testing.T) {
// Without TLS skip should return default transport.
rt := NewHTTPTransport(false)