Skip to content

Commit

Permalink
fix url check wrong detection of absolute windows path
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Feb 10, 2024
1 parent ea491b4 commit 2f826bf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/nothub/mrpack-install/server"
"github.com/nothub/mrpack-install/update"
"github.com/nothub/mrpack-install/util"
"github.com/nothub/mrpack-install/web"
"github.com/spf13/cobra"
"log"
"os"
Expand Down Expand Up @@ -106,7 +107,7 @@ var rootCmd = &cobra.Command{
}

archivePath := ""
if util.IsValidUrl(input) {
if web.IsValidHttpUrl(input) {
fmt.Println("Downloading mrpack file from", args)
file, err := requester.DefaultHttpClient.DownloadFile(input, serverDir, "")
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/nothub/mrpack-install/requester"
"github.com/nothub/mrpack-install/update"
"github.com/nothub/mrpack-install/util"
"github.com/nothub/mrpack-install/web"
"github.com/spf13/cobra"
"log"
"os"
Expand Down Expand Up @@ -74,7 +75,7 @@ var updateCmd = &cobra.Command{
if util.PathIsFile(input) {
archivePath = input

} else if util.IsValidUrl(input) {
} else if web.IsValidHttpUrl(input) {
fmt.Println("Downloading mrpack file from", args)
file, err := requester.DefaultHttpClient.DownloadFile(input, serverDir, "")
if err != nil {
Expand Down
14 changes: 0 additions & 14 deletions util/url.go

This file was deleted.

11 changes: 11 additions & 0 deletions web/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package web

import "net/url"

func IsValidHttpUrl(s string) bool {
u, err := url.Parse(s)
if err != nil {
return false
}
return u.Scheme == "http" || u.Scheme == "https"
}
28 changes: 28 additions & 0 deletions web/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package web

import "testing"

func TestIsValidHttpUrl(t *testing.T) {
type testCase struct {
url string
want bool
}

cases := []testCase{
{url: "C:\\Foo\\Bar", want: false},
{url: "C://Foo//Bar", want: false},
{url: "http://example.org", want: true},
{url: "https://example.org", want: true},
{url: "https://example.org\n", want: false},
{url: "https://example.org/foo/bar", want: true},
{url: "https://example.org/foo/bar?x=y", want: true},
}

for _, c := range cases {
t.Run(c.url, func(t *testing.T) {
if got := IsValidHttpUrl(c.url); got != c.want {
t.Errorf("IsValidUrl() = %v, want %v", got, c.want)
}
})
}
}

0 comments on commit 2f826bf

Please sign in to comment.