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

plumbing: transport/http, Add missing host/port on redirect. Fixes #820 #1001

Merged
merged 1 commit into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions plumbing/transport/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package http
import (
"bytes"
"fmt"
"net"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -151,6 +152,18 @@ func (s *session) ModifyEndpointIfRedirect(res *http.Response) {
return
}

h, p, err := net.SplitHostPort(r.URL.Host)
if err != nil {
h = r.URL.Host
}
if p != "" {
port, err := strconv.Atoi(p)
if err == nil {
s.endpoint.Port = port
}
}
s.endpoint.Host = h

s.endpoint.Protocol = r.URL.Scheme
s.endpoint.Path = r.URL.Path[:len(r.URL.Path)-len(infoRefsPath)]
}
Expand Down
37 changes: 37 additions & 0 deletions plumbing/transport/http/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/http/cgi"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -119,6 +120,42 @@ func (s *ClientSuite) TestSetAuthWrongType(c *C) {
c.Assert(err, Equals, transport.ErrInvalidAuthMethod)
}

func (s *ClientSuite) TestModifyEndpointIfRedirect(c *C) {
sess := &session{endpoint: nil}
u, _ := url.Parse("https://example.com/info/refs")
res := &http.Response{Request: &http.Request{URL: u}}
c.Assert(func() {
sess.ModifyEndpointIfRedirect(res)
}, PanicMatches, ".*nil pointer dereference.*")

sess = &session{endpoint: nil}
// no-op - should return and not panic
sess.ModifyEndpointIfRedirect(&http.Response{})

data := []struct {
url string
endpoint *transport.Endpoint
expected *transport.Endpoint
}{
{"https://example.com/foo/bar", nil, nil},
{"https://example.com/foo.git/info/refs",
&transport.Endpoint{},
&transport.Endpoint{Protocol: "https", Host: "example.com", Path: "/foo.git"}},
{"https://example.com:8080/foo.git/info/refs",
&transport.Endpoint{},
&transport.Endpoint{Protocol: "https", Host: "example.com", Port: 8080, Path: "/foo.git"}},
}

for _, d := range data {
u, _ := url.Parse(d.url)
sess := &session{endpoint: d.endpoint}
sess.ModifyEndpointIfRedirect(&http.Response{
Request: &http.Request{URL: u},
})
c.Assert(d.endpoint, DeepEquals, d.expected)
}
}

type BaseSuite struct {
fixtures.Suite

Expand Down