Rename CanonicalAddr -> canonicalAddr

Update comment for hasPort

Signed-off-by: Stan Hu <stanhu@gmail.com>
pull/1868/head
Stan Hu 2016-09-19 14:14:07 -07:00
parent 462bb55c3f
commit 6d952c92cf
3 changed files with 7 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import (
"strings" "strings"
) )
// FROM: http://golang.org/src/net/http/client.go // FROM: https://golang.org/src/net/http/http.go
// Given a string of the form "host", "host:port", or "[ipv6::address]:port", // Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port. // return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
@ -16,9 +16,9 @@ var portMap = map[string]string{
"https": "443", "https": "443",
} }
// CanonicalAddr returns url.Host but always with a ":port" suffix // canonicalAddr returns url.Host but always with a ":port" suffix
// FROM: http://golang.org/src/net/http/transport.go // FROM: http://golang.org/src/net/http/transport.go
func CanonicalAddr(url *url.URL) string { func canonicalAddr(url *url.URL) string {
addr := url.Host addr := url.Host
if !hasPort(addr) { if !hasPort(addr) {
return addr + ":" + portMap[url.Scheme] return addr + ":" + portMap[url.Scheme]

View File

@ -56,7 +56,7 @@ type simpleChallengeManager struct {
func normalizeURL(endpoint *url.URL) { func normalizeURL(endpoint *url.URL) {
endpoint.Host = strings.ToLower(endpoint.Host) endpoint.Host = strings.ToLower(endpoint.Host)
endpoint.Host = CanonicalAddr(endpoint) endpoint.Host = canonicalAddr(endpoint)
} }
func (m *simpleChallengeManager) GetChallenges(endpoint url.URL) ([]Challenge, error) { func (m *simpleChallengeManager) GetChallenges(endpoint url.URL) ([]Challenge, error) {
@ -78,9 +78,10 @@ func (m *simpleChallengeManager) AddResponse(resp *http.Response) error {
Host: resp.Request.URL.Host, Host: resp.Request.URL.Host,
Scheme: resp.Request.URL.Scheme, Scheme: resp.Request.URL.Scheme,
} }
normalizeURL(&urlCopy)
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
normalizeURL(&urlCopy)
m.Challanges[urlCopy.String()] = challenges m.Challanges[urlCopy.String()] = challenges
return nil return nil
} }

View File

@ -73,7 +73,7 @@ func testAuthChallengeNormalization(t *testing.T, host string) {
lowered := *url lowered := *url
lowered.Host = strings.ToLower(lowered.Host) lowered.Host = strings.ToLower(lowered.Host)
lowered.Host = CanonicalAddr(&lowered) lowered.Host = canonicalAddr(&lowered)
c, err := scm.GetChallenges(lowered) c, err := scm.GetChallenges(lowered)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)