Browse Source

fix bug in file:// url parsing

new-options
Nicolas Massé 5 years ago
parent
commit
91bd68cdc5
  1. 24
      url_checker.go
  2. 5
      url_checker_test.go

24
url_checker.go

@ -9,6 +9,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"sync" "sync"
"time" "time"
@ -93,13 +94,32 @@ func (c urlChecker) resolveURL(u string, f string) (string, bool, error) {
return u, false, nil return u, false, nil
} }
// The file URLs are a mess. According to the RFC, they should be like
// file://host/absolute/path/to/file or file:///absolute/path/to/file
//
// But in real life, the following form is accepted:
// file://relative/path/to/file
//
// To handle this special case, we have to parse the URL by ourself
p := uu.Path
if uu.Scheme == "file" {
if !strings.HasPrefix(u, "file://") {
return "", false, fmt.Errorf("wrong file URL syntax")
}
p, err = url.PathUnescape(u[7:])
if err != nil {
return "", false, err
}
}
if !path.IsAbs(uu.Path) { if !path.IsAbs(uu.Path) {
return path.Join(filepath.Dir(f), uu.Path), true, nil return path.Join(filepath.Dir(f), p), true, nil
} }
if c.documentRoot == "" { if c.documentRoot == "" {
return "", false, fmt.Errorf("document root directory is not specified") return "", false, fmt.Errorf("document root directory is not specified")
} }
return path.Join(c.documentRoot, uu.Path), true, nil return path.Join(c.documentRoot, p), true, nil
} }

5
url_checker_test.go

@ -41,9 +41,12 @@ func TestURLCheckerCheckLocal(t *testing.T) {
assert.Equal(t, errSkipped, c.Check(u, "README.md")) assert.Equal(t, errSkipped, c.Check(u, "README.md"))
} }
for _, u := range []string{"README.md"} { for _, u := range []string{"README.md", "file://README.md"} {
assert.Equal(t, nil, c.Check(u, "README.md")) assert.Equal(t, nil, c.Check(u, "README.md"))
} }
for _, u := range []string{"file://foo-bar-missing-file-azertyuiop"} {
assert.NotEqual(t, nil, c.Check(u, "README.md"))
}
} }
func TestURLCheckerCheckWithTimeout(t *testing.T) { func TestURLCheckerCheckWithTimeout(t *testing.T) {

Loading…
Cancel
Save