Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
Too many redirects when unknown path #147 (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jan 30, 2019
1 parent edfdec2 commit d5dafc5
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 48 deletions.
49 changes: 33 additions & 16 deletions v2/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"runtime"
"sort"
"strings"
"sync"

"github.com/gobuffalo/envy"
"github.com/gobuffalo/packd"
Expand All @@ -23,7 +22,7 @@ import (
)

var _ packd.Box = &Box{}
var _ packd.HTTPBox = Box{}
var _ packd.HTTPBox = &Box{}
var _ packd.Addable = &Box{}
var _ packd.Walkable = &Box{}
var _ packd.Finder = Box{}
Expand Down Expand Up @@ -93,8 +92,8 @@ func construct(name string, path string) *Box {
Path: path,
Name: name,
ResolutionDir: resolutionDir(path),
resolvers: map[string]resolver.Resolver{},
moot: &sync.RWMutex{},
resolvers: resolversMap{},
dirs: dirsMap{},
}
}

Expand Down Expand Up @@ -122,15 +121,16 @@ type Box struct {
Name string `json:"name"`
ResolutionDir string `json:"resolution_dir"`
DefaultResolver resolver.Resolver `json:"default_resolver"`
resolvers map[string]resolver.Resolver
moot *sync.RWMutex
resolvers resolversMap
dirs dirsMap
}

func (b *Box) SetResolver(file string, res resolver.Resolver) {
b.moot.Lock()
d := filepath.Dir(file)
b.dirs.Store(d, true)
b.dirs.Store(strings.TrimPrefix(d, "/"), true)
plog.Debug(b, "SetResolver", "file", file, "resolver", fmt.Sprintf("%T", res))
b.resolvers[resolver.Key(file)] = res
b.moot.Unlock()
b.resolvers.Store(resolver.Key(file), res)
}

// AddString converts t to a byteslice and delegates to AddBytes to add to b.data
Expand Down Expand Up @@ -179,10 +179,28 @@ func (b Box) Has(name string) bool {
return true
}

func (b *Box) HasDir(name string) bool {
oncer.Do("packr2/box/HasDir", func() {
for _, f := range b.List() {
d := filepath.Dir(f)
b.dirs.Store(d, true)
b.dirs.Store(strings.TrimPrefix(d, "/"), true)
}
})
if name == "/" {
return b.Has("index.html")
}
_, ok := b.dirs.Load(name)
return ok
}

// Open returns a File using the http.File interface
func (b Box) Open(name string) (http.File, error) {
func (b *Box) Open(name string) (http.File, error) {
plog.Debug(b, "Open", "name", name)
if len(filepath.Ext(name)) == 0 {
if !b.HasDir(name) {
return nil, os.ErrNotExist
}
d, err := file.NewDir(name)
plog.Debug(b, "Open", "name", name, "dir", d)
return d, err
Expand All @@ -197,7 +215,7 @@ func (b Box) Open(name string) (http.File, error) {
}

// List shows "What's in the box?"
func (b Box) List() []string {
func (b *Box) List() []string {
var keys []string

b.Walk(func(path string, info File) error {
Expand All @@ -216,19 +234,18 @@ func (b Box) List() []string {

func (b *Box) Resolve(key string) (file.File, error) {
key = strings.TrimPrefix(key, "/")
b.moot.RLock()

var r resolver.Resolver

for k, vr := range b.resolvers {
b.resolvers.Range(func(k string, vr resolver.Resolver) bool {
lk := strings.ToLower(resolver.Key(k))
lkey := strings.ToLower(resolver.Key(key))
if lk == lkey {
r = vr
break
return false
}
}
b.moot.RUnlock()
return true
})

if r == nil {
r = b.DefaultResolver
Expand Down
41 changes: 26 additions & 15 deletions v2/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
"github.com/stretchr/testify/require"
)

func Test_NewBox(t *testing.T) {
func Test_New(t *testing.T) {
r := require.New(t)

box := NewBox(filepath.Join("_fixtures", "list_test"))
box := New("Test_NewBox", filepath.Join("_fixtures", "list_test"))
r.Len(box.List(), 4)

}
func Test_Box_AddString(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
box := New("Test_Box_AddString", "./templates")
s, err := box.FindString("foo.txt")
r.Error(err)
r.Equal("", s)
Expand All @@ -35,7 +35,7 @@ func Test_Box_AddString(t *testing.T) {
func Test_Box_AddBytes(t *testing.T) {
r := require.New(t)

box := NewBox("Test_Box_AddBytes")
box := New("Test_Box_AddBytes", "")
s, err := box.FindString("foo.txt")
r.Error(err)
r.Equal("", s)
Expand All @@ -49,7 +49,7 @@ func Test_Box_AddBytes(t *testing.T) {
func Test_Box_String(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
box := New("Test_Box_String", "./templates")
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
})
Expand All @@ -65,7 +65,7 @@ func Test_Box_String(t *testing.T) {
func Test_Box_String_Miss(t *testing.T) {
r := require.New(t)

box := NewBox(filepath.Join("_fixtures", "templates"))
box := New("Test_Box_String_Miss", filepath.Join("_fixtures", "templates"))

s := box.String("foo.txt")
r.Equal("FOO!!!", strings.TrimSpace(s))
Expand All @@ -77,7 +77,7 @@ func Test_Box_String_Miss(t *testing.T) {
func Test_Box_FindString(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
box := New("Test_Box_FindString", "./templates")
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
})
Expand All @@ -95,7 +95,7 @@ func Test_Box_FindString(t *testing.T) {
func Test_Box_FindString_Miss(t *testing.T) {
r := require.New(t)

box := NewBox(filepath.Join("_fixtures", "templates"))
box := New("Test_Box_FindString_Miss", filepath.Join("_fixtures", "templates"))

s, err := box.FindString("foo.txt")
r.NoError(err)
Expand All @@ -109,7 +109,7 @@ func Test_Box_FindString_Miss(t *testing.T) {
func Test_Box_Bytes(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
box := New("Test_Box_Bytes", "./templates")
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
})
Expand All @@ -125,7 +125,7 @@ func Test_Box_Bytes(t *testing.T) {
func Test_Box_Bytes_Miss(t *testing.T) {
r := require.New(t)

box := NewBox(filepath.Join("_fixtures", "templates"))
box := New("Test_Box_Bytes_Miss", filepath.Join("_fixtures", "templates"))

s := box.Bytes("foo.txt")
r.Equal([]byte("FOO!!!"), bytes.TrimSpace(s))
Expand All @@ -137,7 +137,7 @@ func Test_Box_Bytes_Miss(t *testing.T) {
func Test_Box_Find(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
box := New("Test_Box_Find", "./templates")
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
})
Expand All @@ -155,7 +155,7 @@ func Test_Box_Find(t *testing.T) {
func Test_Box_Find_Miss(t *testing.T) {
r := require.New(t)

box := NewBox("./_fixtures/templates")
box := New("Test_Box_Find_Miss", "./_fixtures/templates")
s, err := box.Find("foo.txt")
r.NoError(err)
r.Equal("FOO!!!", strings.TrimSpace(string(s)))
Expand All @@ -168,7 +168,7 @@ func Test_Box_Find_Miss(t *testing.T) {
func Test_Box_Has(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
box := New("Test_Box_Has", "./templates")
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
})
Expand All @@ -184,7 +184,7 @@ func Test_Box_Open(t *testing.T) {
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": qfile("foo.txt", "foo!"),
})
box := NewBox("./templates")
box := New("Test_Box_Open", "./templates")

box.SetResolver("foo.txt", d)

Expand All @@ -200,10 +200,21 @@ func Test_Box_Open(t *testing.T) {
func Test_Box_List(t *testing.T) {
r := require.New(t)

box := NewBox(filepath.Join("_fixtures", "list_test"))
box := New("Test_Box_List", filepath.Join("_fixtures", "list_test"))
r.NoError(box.AddString(filepath.Join("d", "d.txt"), "D"))

act := box.List()
exp := []string{"a.txt", filepath.Join("b", "b.txt"), filepath.Join("b", "b2.txt"), filepath.Join("c", "c.txt"), filepath.Join("d", "d.txt")}
r.Equal(exp, act)
}

func Test_Box_HasDir(t *testing.T) {
r := require.New(t)

box := New("Test_Box_HasDir", filepath.Join("_fixtures", "list_test"))
r.NoError(box.AddString("d/e/f.txt", "D"))

r.True(box.HasDir("d/e"))
r.True(box.HasDir("c"))
r.False(box.HasDir("a"))
}
80 changes: 80 additions & 0 deletions v2/dirs_map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module github.com/gobuffalo/packr/v2

require (
github.com/gobuffalo/buffalo-plugins v1.11.0 // indirect
github.com/gobuffalo/envy v1.6.12
github.com/gobuffalo/genny v0.0.0-20190112155932-f31a84fcacf5
github.com/gobuffalo/genny v0.0.0-20190124191459-3310289fa4b4
github.com/gobuffalo/logger v0.0.0-20181127160119-5b956e21995c
github.com/gobuffalo/meta v0.0.0-20190120163247-50bbb1fa260d // indirect
github.com/gobuffalo/packd v0.0.0-20181212173646-eca3b8fd6687
github.com/karrick/godirwalk v1.7.8
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2
Expand All @@ -16,6 +14,5 @@ require (
github.com/spf13/cobra v0.0.3
github.com/stretchr/testify v1.3.0
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 // indirect
golang.org/x/tools v0.0.0-20190118193359-16909d206f00
golang.org/x/tools v0.0.0-20190130015043-a06a922acc1b
)

0 comments on commit d5dafc5

Please sign in to comment.