Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 638e0d2

Browse files
authored
Merge pull request #751 from mcuadros/redirect
transport: http, fix services redirecting only info/refs
2 parents 01c428c + 4cc9a5e commit 638e0d2

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

plumbing/transport/http/common.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"strconv"
9+
"strings"
910

1011
"gopkg.in/src-d/go-git.v4/plumbing"
1112
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
@@ -28,24 +29,27 @@ func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string
2829
req.Header.Add("Content-Length", strconv.Itoa(content.Len()))
2930
}
3031

32+
const infoRefsPath = "/info/refs"
33+
3134
func advertisedReferences(s *session, serviceName string) (*packp.AdvRefs, error) {
3235
url := fmt.Sprintf(
33-
"%s/info/refs?service=%s",
34-
s.endpoint.String(), serviceName,
36+
"%s%s?service=%s",
37+
s.endpoint.String(), infoRefsPath, serviceName,
3538
)
3639

3740
req, err := http.NewRequest(http.MethodGet, url, nil)
3841
if err != nil {
3942
return nil, err
4043
}
4144

42-
s.applyAuthToRequest(req)
45+
s.ApplyAuthToRequest(req)
4346
applyHeadersToRequest(req, nil, s.endpoint.Host, serviceName)
4447
res, err := s.client.Do(req)
4548
if err != nil {
4649
return nil, err
4750
}
4851

52+
s.ModifyEndpointIfRedirect(res)
4953
defer ioutil.CheckClose(res.Body, &err)
5054

5155
if err := NewErr(res); err != nil {
@@ -129,18 +133,32 @@ func newSession(c *http.Client, ep *transport.Endpoint, auth transport.AuthMetho
129133
return s, nil
130134
}
131135

132-
func (*session) Close() error {
133-
return nil
134-
}
135-
136-
func (s *session) applyAuthToRequest(req *http.Request) {
136+
func (s *session) ApplyAuthToRequest(req *http.Request) {
137137
if s.auth == nil {
138138
return
139139
}
140140

141141
s.auth.setAuth(req)
142142
}
143143

144+
func (s *session) ModifyEndpointIfRedirect(res *http.Response) {
145+
if res.Request == nil {
146+
return
147+
}
148+
149+
r := res.Request
150+
if !strings.HasSuffix(r.URL.Path, infoRefsPath) {
151+
return
152+
}
153+
154+
s.endpoint.Protocol = r.URL.Scheme
155+
s.endpoint.Path = r.URL.Path[:len(r.URL.Path)-len(infoRefsPath)]
156+
}
157+
158+
func (*session) Close() error {
159+
return nil
160+
}
161+
144162
// AuthMethod is concrete implementation of common.AuthMethod for HTTP services
145163
type AuthMethod interface {
146164
transport.AuthMethod

plumbing/transport/http/receive_pack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *rpSession) doRequest(
9090
}
9191

9292
applyHeadersToRequest(req, content, s.endpoint.Host, transport.ReceivePackServiceName)
93-
s.applyAuthToRequest(req)
93+
s.ApplyAuthToRequest(req)
9494

9595
res, err := s.client.Do(req.WithContext(ctx))
9696
if err != nil {

plumbing/transport/http/upload_pack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *upSession) doRequest(
8888
}
8989

9090
applyHeadersToRequest(req, content, s.endpoint.Host, transport.UploadPackServiceName)
91-
s.applyAuthToRequest(req)
91+
s.ApplyAuthToRequest(req)
9292

9393
res, err := s.client.Do(req.WithContext(ctx))
9494
if err != nil {

plumbing/transport/http/upload_pack_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,31 @@ func (s *UploadPackSuite) newEndpoint(c *C, name string) *transport.Endpoint {
7575

7676
return ep
7777
}
78+
79+
func (s *UploadPackSuite) TestAdvertisedReferencesRedirectPath(c *C) {
80+
endpoint, _ := transport.NewEndpoint("https://gitlab.com/gitlab-org/gitter/webapp")
81+
82+
session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth)
83+
c.Assert(err, IsNil)
84+
85+
info, err := session.AdvertisedReferences()
86+
c.Assert(err, IsNil)
87+
c.Assert(info, NotNil)
88+
89+
url := session.(*upSession).endpoint.String()
90+
c.Assert(url, Equals, "https://gitlab.com/gitlab-org/gitter/webapp.git")
91+
}
92+
93+
func (s *UploadPackSuite) TestAdvertisedReferencesRedirectSchema(c *C) {
94+
endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic")
95+
96+
session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth)
97+
c.Assert(err, IsNil)
98+
99+
info, err := session.AdvertisedReferences()
100+
c.Assert(err, IsNil)
101+
c.Assert(info, NotNil)
102+
103+
url := session.(*upSession).endpoint.String()
104+
c.Assert(url, Equals, "https://github.com/git-fixtures/basic")
105+
}

0 commit comments

Comments
 (0)