1
0
Fork 0

Merge pull request #97 from otoolep/leader_redirection2

Move URL normalization to http package
master
Philip O'Toole 9 years ago
commit 69a0e05c4d

@ -21,7 +21,6 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime/pprof" "runtime/pprof"
"strings"
"github.com/otoolep/rqlite/auth" "github.com/otoolep/rqlite/auth"
sql "github.com/otoolep/rqlite/db" sql "github.com/otoolep/rqlite/db"
@ -206,10 +205,7 @@ func join(joinAddr string, skipVerify bool, raftAddr string) error {
} }
// Check for protocol scheme, and insert default if necessary. // Check for protocol scheme, and insert default if necessary.
fullAddr := fmt.Sprintf("%s/join", joinAddr) fullAddr := httpd.NormalizeAddr(fmt.Sprintf("%s/join", joinAddr))
if !strings.HasPrefix(joinAddr, "http://") && !strings.HasPrefix(joinAddr, "https://") {
fullAddr = fmt.Sprintf("http://%s", joinAddr)
}
// Enable skipVerify as requested. // Enable skipVerify as requested.
tr := &http.Transport{ tr := &http.Transport{

@ -591,3 +591,12 @@ func prettyEnabled(e bool) string {
} }
return "disabled" return "disabled"
} }
// NormalizeAddr ensures that the given URL has a HTTP protocol prefix.
// If none is supplied, it prefixes the URL with "http://".
func NormalizeAddr(addr string) string {
if !strings.HasPrefix(addr, "http://") && !strings.HasPrefix(addr, "https://") {
return fmt.Sprintf("http://%s", addr)
}
return addr
}

@ -9,6 +9,44 @@ import (
"github.com/otoolep/rqlite/store" "github.com/otoolep/rqlite/store"
) )
func Test_NormalizeAddr(t *testing.T) {
tests := []struct {
orig string
norm string
}{
{
orig: "http://localhost:4001",
norm: "http://localhost:4001",
},
{
orig: "https://localhost:4001",
norm: "https://localhost:4001",
},
{
orig: "https://localhost:4001/foo",
norm: "https://localhost:4001/foo",
},
{
orig: "localhost:4001",
norm: "http://localhost:4001",
},
{
orig: "localhost",
norm: "http://localhost",
},
{
orig: ":4001",
norm: "http://:4001",
},
}
for _, tt := range tests {
if NormalizeAddr(tt.orig) != tt.norm {
t.Fatalf("%s not normalized correctly, got: %s", tt.orig, tt.norm)
}
}
}
func Test_NewService(t *testing.T) { func Test_NewService(t *testing.T) {
m := &MockStore{} m := &MockStore{}
s := New("127.0.0.1:0", m, nil) s := New("127.0.0.1:0", m, nil)

Loading…
Cancel
Save