Skip to content

Commit 414b027

Browse files
authored
Merge pull request go-git#1001 from hairyhenderson/fix-redirect-on-clone
plumbing: transport/http, Add missing host/port on redirect. Fixes go-git#820
2 parents 4203e78 + 02a92b7 commit 414b027

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Diff for: plumbing/transport/http/common.go

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package http
44
import (
55
"bytes"
66
"fmt"
7+
"net"
78
"net/http"
89
"strconv"
910
"strings"
@@ -151,6 +152,18 @@ func (s *session) ModifyEndpointIfRedirect(res *http.Response) {
151152
return
152153
}
153154

155+
h, p, err := net.SplitHostPort(r.URL.Host)
156+
if err != nil {
157+
h = r.URL.Host
158+
}
159+
if p != "" {
160+
port, err := strconv.Atoi(p)
161+
if err == nil {
162+
s.endpoint.Port = port
163+
}
164+
}
165+
s.endpoint.Host = h
166+
154167
s.endpoint.Protocol = r.URL.Scheme
155168
s.endpoint.Path = r.URL.Path[:len(r.URL.Path)-len(infoRefsPath)]
156169
}

Diff for: plumbing/transport/http/common_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/http"
1010
"net/http/cgi"
11+
"net/url"
1112
"os"
1213
"os/exec"
1314
"path/filepath"
@@ -119,6 +120,42 @@ func (s *ClientSuite) TestSetAuthWrongType(c *C) {
119120
c.Assert(err, Equals, transport.ErrInvalidAuthMethod)
120121
}
121122

123+
func (s *ClientSuite) TestModifyEndpointIfRedirect(c *C) {
124+
sess := &session{endpoint: nil}
125+
u, _ := url.Parse("https://example.com/info/refs")
126+
res := &http.Response{Request: &http.Request{URL: u}}
127+
c.Assert(func() {
128+
sess.ModifyEndpointIfRedirect(res)
129+
}, PanicMatches, ".*nil pointer dereference.*")
130+
131+
sess = &session{endpoint: nil}
132+
// no-op - should return and not panic
133+
sess.ModifyEndpointIfRedirect(&http.Response{})
134+
135+
data := []struct {
136+
url string
137+
endpoint *transport.Endpoint
138+
expected *transport.Endpoint
139+
}{
140+
{"https://example.com/foo/bar", nil, nil},
141+
{"https://example.com/foo.git/info/refs",
142+
&transport.Endpoint{},
143+
&transport.Endpoint{Protocol: "https", Host: "example.com", Path: "/foo.git"}},
144+
{"https://example.com:8080/foo.git/info/refs",
145+
&transport.Endpoint{},
146+
&transport.Endpoint{Protocol: "https", Host: "example.com", Port: 8080, Path: "/foo.git"}},
147+
}
148+
149+
for _, d := range data {
150+
u, _ := url.Parse(d.url)
151+
sess := &session{endpoint: d.endpoint}
152+
sess.ModifyEndpointIfRedirect(&http.Response{
153+
Request: &http.Request{URL: u},
154+
})
155+
c.Assert(d.endpoint, DeepEquals, d.expected)
156+
}
157+
}
158+
122159
type BaseSuite struct {
123160
fixtures.Suite
124161

0 commit comments

Comments
 (0)