From f633fcd4aed632f8b5660dd3de19d4a3337bc322 Mon Sep 17 00:00:00 2001 From: Travis Date: Wed, 6 Jan 2021 16:19:14 -0600 Subject: [PATCH] remove pilosa.URI --- gossip/gossip.go | 4 +- server/server.go | 2 +- uri.go | 226 ------------------------------------------- uri_internal_test.go | 176 --------------------------------- 4 files changed, 3 insertions(+), 405 deletions(-) delete mode 100644 uri.go delete mode 100644 uri_internal_test.go diff --git a/gossip/gossip.go b/gossip/gossip.go index e37e8e8ca..e4f9110ef 100644 --- a/gossip/gossip.go +++ b/gossip/gossip.go @@ -472,7 +472,7 @@ func (g *eventReceiver) listen() { type Transport struct { //memberlist.Transport net *memberlist.NetTransport - URI *pilosa.URI + URI *pnet.URI } // NewTransport returns a NetTransport based on the given host and port. @@ -492,7 +492,7 @@ func NewTransport(host string, port int, logger *log.Logger) (*Transport, error) return nil, fmt.Errorf("new transport: %s", err) } - uri, err := pilosa.NewURIFromHostPort(host, uint16(net.GetAutoBindPort())) + uri, err := pnet.NewURIFromHostPort(host, uint16(net.GetAutoBindPort())) if err != nil { return nil, fmt.Errorf("new uri from host port: %s", err) } diff --git a/server/server.go b/server/server.go index b27f7741f..5fab8ae3c 100644 --- a/server/server.go +++ b/server/server.go @@ -310,7 +310,7 @@ func (m *Command) SetupServer() error { return errors.Wrap(err, "processing bind address") } - grpcURI, err := pilosa.NewURIFromAddress(m.Config.BindGRPC) + grpcURI, err := pnet.NewURIFromAddress(m.Config.BindGRPC) if err != nil { return errors.Wrap(err, "processing bind grpc address") } diff --git a/uri.go b/uri.go deleted file mode 100644 index b1030f8ce..000000000 --- a/uri.go +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright 2017 Pilosa Corp. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pilosa - -import ( - "encoding/json" - "fmt" - "net" - "net/url" - "regexp" - "strconv" - "strings" - - "github.com/pkg/errors" -) - -var schemeRegexp = regexp.MustCompile("^[+a-z]+$") -var hostRegexp = regexp.MustCompile(`^[0-9a-z.-]+$|^\[[:0-9a-fA-F]+\]$`) -var addressRegexp = regexp.MustCompile(`^(([+a-z]+):\/\/)?([0-9a-z.-]+|\[[:0-9a-fA-F]+\])?(:([0-9]+))?$`) - -// URI represents a Pilosa URI. -// A Pilosa URI consists of three parts: -// 1) Scheme: Protocol of the URI. Default: http. -// 2) Host: Hostname or IP URI. Default: localhost. IPv6 addresses should be written in brackets, e.g., `[fd42:4201:f86b:7e09:216:3eff:fefa:ed80]`. -// 3) Port: Port of the URI. Default: 10101. -// -// All parts of the URI are optional. The following are equivalent: -// http://localhost:10101 -// http://localhost -// http://:10101 -// localhost:10101 -// localhost -// :10101 -type URI struct { - Scheme string `json:"scheme"` - Host string `json:"host"` - Port uint16 `json:"port"` -} - -// URL returns a url.URL representation of the URI. -func (u *URI) URL() url.URL { - return url.URL{Scheme: u.Scheme, Host: net.JoinHostPort(u.Host, strconv.Itoa(int(u.Port)))} -} - -// defaultURI creates and returns the default URI. -func defaultURI() *URI { - return &URI{ - Scheme: "http", - Host: "localhost", - Port: 10101, - } -} - -// URIs is a convenience type representing a slice of URI. -type URIs []URI - -// HostPortStrings returns a slice of host:port strings -// based on the slice of URI. -func (u URIs) HostPortStrings() []string { - s := make([]string, len(u)) - for i, a := range u { - s[i] = a.HostPort() - } - return s -} - -// NewURIFromHostPort returns a URI with specified host and port. -func NewURIFromHostPort(host string, port uint16) (*URI, error) { - uri := defaultURI() - err := uri.setHost(host) - if err != nil { - return nil, errors.Wrap(err, "setting uri host") - } - uri.SetPort(port) - return uri, nil -} - -// NewURIFromAddress parses the passed address and returns a URI. -func NewURIFromAddress(address string) (*URI, error) { - return parseAddress(address) -} - -// setScheme sets the scheme of this URI. -func (u *URI) setScheme(scheme string) error { - m := schemeRegexp.FindStringSubmatch(scheme) - if m == nil { - return errors.New("invalid scheme") - } - u.Scheme = scheme - return nil -} - -// setHost sets the host of this URI. -func (u *URI) setHost(host string) error { - m := hostRegexp.FindStringSubmatch(host) - if m == nil { - return errors.New("invalid host") - } - u.Host = host - return nil -} - -// SetPort sets the port of this URI. -func (u *URI) SetPort(port uint16) { - u.Port = port -} - -// HostPort returns `Host:Port` -func (u *URI) HostPort() string { - // XXX: The following is just to make TestHandler_Status; remove it - if u == nil { - return "" - } - s := fmt.Sprintf("%s:%d", u.Host, u.Port) - return s -} - -// normalize returns the address in a form usable by a HTTP client. -func (u *URI) normalize() string { - scheme := u.Scheme - index := strings.Index(scheme, "+") - if index >= 0 { - scheme = scheme[:index] - } - return fmt.Sprintf("%s://%s:%d", scheme, u.Host, u.Port) -} - -// String returns the address as a string. -func (u URI) String() string { - return fmt.Sprintf("%s://%s:%d", u.Scheme, u.Host, u.Port) -} - -// Path returns URI with path -func (u *URI) Path(path string) string { - return fmt.Sprintf("%s%s", u.normalize(), path) -} - -// The following methods are required to implement pflag Value interface. - -// Set sets the uri value. -func (u *URI) Set(value string) error { - uri, err := NewURIFromAddress(value) - if err != nil { - return err - } - *u = *uri - return nil -} - -// Type returns the type of a uri. -func (u URI) Type() string { - return "URI" -} - -func parseAddress(address string) (uri *URI, err error) { - m := addressRegexp.FindStringSubmatch(address) - if m == nil { - return nil, errors.New("invalid address") - } - scheme := "http" - if m[2] != "" { - scheme = m[2] - } - host := "localhost" - if m[3] != "" { - host = m[3] - } - var port = 10101 - if m[5] != "" { - port, err = strconv.Atoi(m[5]) - if err != nil { - return nil, errors.New("converting port string to int") - } - if port > 65535 { - return nil, errors.New("port must be in range 0 - 65535") - } - } - uri = &URI{ - Scheme: scheme, - Host: host, - Port: uint16(port), - } - return uri, nil -} - -// MarshalJSON marshals URI into a JSON-encoded byte slice. -func (u *URI) MarshalJSON() ([]byte, error) { - var output struct { - Scheme string `json:"scheme,omitempty"` - Host string `json:"host,omitempty"` - Port uint16 `json:"port,omitempty"` - } - output.Scheme = u.Scheme - output.Host = u.Host - output.Port = u.Port - - return json.Marshal(output) -} - -// UnmarshalJSON unmarshals a byte slice to a URI. -func (u *URI) UnmarshalJSON(b []byte) error { - var input struct { - Scheme string `json:"scheme,omitempty"` - Host string `json:"host,omitempty"` - Port uint16 `json:"port,omitempty"` - } - if err := json.Unmarshal(b, &input); err != nil { - return err - } - u.Scheme = input.Scheme - u.Host = input.Host - u.Port = input.Port - return nil -} diff --git a/uri_internal_test.go b/uri_internal_test.go deleted file mode 100644 index cb59c75c6..000000000 --- a/uri_internal_test.go +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright 2017 Pilosa Corp. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pilosa - -import "testing" - -func TestDefaultURI(t *testing.T) { - uri := defaultURI() - compare(t, uri, "http", "localhost", 10101) -} - -func TestURIWithHostPort(t *testing.T) { - uri, err := NewURIFromHostPort("index1.pilosa.com", 3333) - if err != nil { - t.Fatal(err) - } - compare(t, uri, "http", "index1.pilosa.com", 3333) -} - -func TestURIWithInvalidHostPort(t *testing.T) { - _, err := NewURIFromHostPort("index?.pilosa.com", 3333) - if err == nil { - t.Fatalf("should have failed") - } -} - -func TestNewURIFromAddress(t *testing.T) { - for _, item := range validFixture() { - uri, err := NewURIFromAddress(item.address) - if err != nil { - t.Fatalf("Can't parse address: %s, %s", item.address, err) - } - compare(t, uri, item.scheme, item.host, item.port) - } -} - -func TestNewURIFromAddressInvalidAddress(t *testing.T) { - for _, addr := range invalidFixture() { - _, err := NewURIFromAddress(addr) - if err == nil { - t.Fatalf("Invalid address should return an error: %s", addr) - } - } -} - -func TestNormalizedAddress(t *testing.T) { - uri, err := NewURIFromAddress("http+protobuf://big-data.pilosa.com:6888") - if err != nil { - t.Fatalf("Can't parse address") - } - if uri.normalize() != "http://big-data.pilosa.com:6888" { - t.Fatalf("Normalized address is not normal") - } -} - -func TestURIPath(t *testing.T) { - uri, err := NewURIFromAddress("http+protobuf://big-data.pilosa.com:6888") - if err != nil { - t.Fatal(err) - } - target := "http://big-data.pilosa.com:6888/index/foo" - if uri.Path("/index/foo") != target { - t.Fatalf("%s != %s", uri.Path("/index/foo"), target) - } -} - -func TestSetScheme(t *testing.T) { - uri := defaultURI() - target := "fun" - err := uri.setScheme(target) - if err != nil { - t.Fatal(err) - } - if uri.Scheme != target { - t.Fatalf("%s != %s", uri.Scheme, target) - } -} - -func TestSetHost(t *testing.T) { - uri := defaultURI() - target := "10.20.30.40" - err := uri.setHost(target) - if err != nil { - t.Fatal(err) - } - if uri.Host != target { - t.Fatalf("%s != %s", uri.Host, target) - } -} - -func TestSetPort(t *testing.T) { - uri := defaultURI() - target := uint16(9999) - uri.SetPort(target) - if uri.Port != target { - t.Fatalf("%d != %d", uri.Port, target) - } -} - -func TestSetInvalidScheme(t *testing.T) { - uri := defaultURI() - err := uri.setScheme("?invalid") - if err == nil { - t.Fatalf("Should have failed") - } -} - -func TestSetInvalidHost(t *testing.T) { - uri := defaultURI() - err := uri.setHost("index?.pilosa.com") - if err == nil { - t.Fatalf("Should have failed") - } -} - -func TestHostPort(t *testing.T) { - uri, err := NewURIFromHostPort("i.pilosa.com", 15001) - if err != nil { - t.Fatal(err) - } - target := "i.pilosa.com:15001" - if uri.HostPort() != target { - t.Fatalf("%s != %s", uri.HostPort(), target) - } -} - -func compare(t *testing.T, uri *URI, scheme string, host string, port uint16) { - if uri.Scheme != scheme { - t.Fatalf("Scheme does not match: %s != %s", uri.Scheme, scheme) - } - if uri.Host != host { - t.Fatalf("Host does not match: %s != %s", uri.Host, host) - } - if uri.Port != port { - t.Fatalf("Port does not match: %d != %d", uri.Port, port) - } -} - -type uriItem struct { - address string - scheme string - host string - port uint16 -} - -func validFixture() []uriItem { - var test = []uriItem{ - {"http+protobuf://index1.pilosa.com:3333", "http+protobuf", "index1.pilosa.com", 3333}, - {"index1.pilosa.com:3333", "http", "index1.pilosa.com", 3333}, - {"https://index1.pilosa.com", "https", "index1.pilosa.com", 10101}, - {"index1.pilosa.com", "http", "index1.pilosa.com", 10101}, - {"https://:3333", "https", "localhost", 3333}, - {":3333", "http", "localhost", 3333}, - {"[::1]", "http", "[::1]", 10101}, - {"[::1]:3333", "http", "[::1]", 3333}, - {"[fd42:4201:f86b:7e09:216:3eff:fefa:ed80]:3333", "http", "[fd42:4201:f86b:7e09:216:3eff:fefa:ed80]", 3333}, - {"https://[fd42:4201:f86b:7e09:216:3eff:fefa:ed80]:3333", "https", "[fd42:4201:f86b:7e09:216:3eff:fefa:ed80]", 3333}, - } - return test -} - -func invalidFixture() []string { - return []string{"foo:bar", "http://foo:", "foo:", ":bar", "http://pilosa.com:129999999999999999999999993", "fd42:4201:f86b:7e09:216:3eff:fefa:ed80", ":65536"} -}