Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow LICENSE rendering via gnoweb #2417

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/gnoweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func MakeApp(logger *slog.Logger, cfg Config) gotuna.App {
}
// realm routes
// NOTE: see rePathPart.
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}/{filename:(?:.*\\.(?:gno|md|txt|mod)$)?}", handlerRealmFile(logger, app, &cfg))
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}/{filename:(?:(?:.*\\.(?:gno|md|txt|mod)$)|(?:LICENSE$))?}", handlerRealmFile(logger, app, &cfg))
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}", handlerRealmMain(logger, app, &cfg))
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}:{querystr:.*}", handlerRealmRender(logger, app, &cfg))
app.Router.Handle("/p/{filepath:.*}", handlerPackageFile(logger, app, &cfg))
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/gnoweb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestRoutes(t *testing.T) {
{"/%ED%85%8C%EC%8A%A4%ED%8A%B8", notFound, "/테스트"},
{"/グノー", notFound, "/グノー"},
{"/⚛️", notFound, "/⚛️"},
{"/p/demo/flow/LICENSE", ok, "BSD 3-Clause"},
}

config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
Expand All @@ -67,7 +68,6 @@ func TestRoutes(t *testing.T) {
response := httptest.NewRecorder()
app.Router.ServeHTTP(response, request)
assert.Equal(t, r.status, response.Code)

assert.Contains(t, response.Body.String(), r.substring)
})
}
Expand Down
15 changes: 10 additions & 5 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,23 @@ func (mempkg *MemPackage) Validate() error {
return nil
}

const licenseName = "LICENSE"
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

// Splits a path into the dirpath and filename.
func SplitFilepath(filepath string) (dirpath string, filename string) {
parts := strings.Split(filepath, "/")
if len(parts) == 1 {
return parts[0], ""
}
last := parts[len(parts)-1]
if strings.Contains(last, ".") {

switch last := parts[len(parts)-1]; {
case strings.Contains(last, "."):
return strings.Join(parts[:len(parts)-1], "/"), last
} else if last == "" {
case last == "":
return strings.Join(parts[:len(parts)-1], "/"), ""
} else {
return strings.Join(parts, "/"), ""
case last == licenseName:
return strings.Join(parts[:len(parts)-1], "/"), licenseName
}

return strings.Join(parts, "/"), ""
}
51 changes: 51 additions & 0 deletions tm2/pkg/std/memfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,54 @@ func TestMemPackage_Validate(t *testing.T) {
})
}
}

func TestSplitFilepath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
filepath string
expDirPath string
expFilename string
}{
{
name: "empty",
},
{
name: "one part",
filepath: "root",
expDirPath: "root",
},
{
name: "file",
filepath: "gno.land/r/demo/avl/avl.gno",
expDirPath: "gno.land/r/demo/avl",
expFilename: "avl.gno",
},
{
name: "trailing slash",
filepath: "gno.land/r/demo/avl/",
expDirPath: "gno.land/r/demo/avl",
},
{
name: "license",
filepath: "gno.land/r/demo/avl/LICENSE",
expDirPath: "gno.land/r/demo/avl",
expFilename: "LICENSE",
},
{
name: "regular path",
filepath: "gno.land/p/demo/avl",
expDirPath: "gno.land/p/demo/avl",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dirPath, filename := SplitFilepath(tt.filepath)
assert.Equal(t, tt.expDirPath, dirPath)
assert.Equal(t, tt.expFilename, filename)
})
}
}
Loading