Skip to content

Commit

Permalink
Merge branch 'master' into BTFS-1383
Browse files Browse the repository at this point in the history
  • Loading branch information
Junlin Gao authored Jan 23, 2020
2 parents 06c442b + 35208be commit 211a0fc
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 37 deletions.
23 changes: 0 additions & 23 deletions utils/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -29,11 +28,6 @@ const (
defaultTimeout = 30 * time.Second
)

var (
domainRegexp = regexp.MustCompile(`^(localhost)|([a-zA-Z0-9-]{1,63}\.)+([a-zA-Z]{1,63})$`)
ipv4Regexp = regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`)
)

func (g *ClientBuilder) doWithContext(ctx context.Context, f interface{}) error {
newCtx, cancel := context.WithTimeout(ctx, g.timeout)
if cancel != nil {
Expand Down Expand Up @@ -114,9 +108,6 @@ func parse(rawU string) (*parsedURL, error) {
return nil, err
}
h := u.Hostname()
if err := checkHost(h); err != nil {
return nil, err
}
result := new(parsedURL)
result.schema = u.Scheme
result.host = h
Expand All @@ -142,20 +133,6 @@ func getPort(u *url.URL) (int, error) {
return strconv.Atoi(p)
}

func checkHost(host string) error {
if host == "" {
return errors.New("empty host")
}
host = strings.ToLower(host)
if domainRegexp.MatchString(host) {
return nil
}
if ipv4Regexp.MatchString(host) {
return nil
}
return fmt.Errorf("invalid host: %v", host)
}

type parsedURL struct {
schema string
host string
Expand Down
15 changes: 1 addition & 14 deletions utils/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,8 @@ func TestParse(t *testing.T) {
out *parsedURL
err bool
}{
{in: "", err: true},
{in: "/", err: true},
{in: "//", err: true},
{in: "http:/www.google.com", err: true},
{in: "http:///www.google.com", err: true},
{in: "javascript:void(0)", err: true},
{in: "<script>", err: true},
{in: "127.0.0:8080", err: true},
{in: "127.0.0", err: true},
{in: "127.0.0.0.1", err: true},
{in: "ftp://127.0.0.1", err: true},
{in: "//localhost", out: &parsedURL{schema: "http", host: "localhost", port: 80}},
{in: "localhost", out: &parsedURL{schema: "http", host: "localhost", port: 80}},
{in: "localhost:8080", out: &parsedURL{schema: "http", host: "localhost", port: 8080}},
{in: "btfs.io", out: &parsedURL{schema: "http", host: "btfs.io", port: 80}},
{in: "btfs.io:8080", out: &parsedURL{schema: "http", host: "btfs.io", port: 8080}},
{in: "127.0.0.1", out: &parsedURL{schema: "http", host: "127.0.0.1", port: 80}},
{in: "127.0.0.1:8080", out: &parsedURL{schema: "http", host: "127.0.0.1", port: 8080}},
{in: "https://btfs.io", out: &parsedURL{schema: "https", host: "btfs.io", port: 443}},
Expand All @@ -72,6 +58,7 @@ func TestParse(t *testing.T) {
{in: "http://btfs.io:2333", out: &parsedURL{schema: "http", host: "btfs.io", port: 2333}},
{in: "https://127.0.0.1", out: &parsedURL{schema: "https", host: "127.0.0.1", port: 443}},
{in: "http://127.0.0.1", out: &parsedURL{schema: "http", host: "127.0.0.1", port: 80}},
{in: "hub-service:8080", out: &parsedURL{schema: "http", host: "hub-service", port: 8080}},
}
for _, tt := range tests {
url, err := parse(tt.in)
Expand Down

0 comments on commit 211a0fc

Please sign in to comment.