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

feat(gnoweb): Handle unicode characters in 404 page URL #1878

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 15 additions & 8 deletions gno.land/pkg/gnoweb/gnoweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"io/fs"
"log/slog"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -122,7 +123,7 @@

app.Router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.RequestURI
handleNotFound(app, &cfg, path, w, r)
handleNotFound(logger, app, &cfg, path, w, r)
})
return app
}
Expand Down Expand Up @@ -444,12 +445,12 @@
fpath := filepath.Clean(vars["path"])
f, err := fs.Open(fpath)
if os.IsNotExist(err) {
handleNotFound(app, cfg, fpath, w, r)
handleNotFound(logger, app, cfg, fpath, w, r)

Check warning on line 448 in gno.land/pkg/gnoweb/gnoweb.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L448

Added line #L448 was not covered by tests
return
}
stat, err := f.Stat()
if err != nil || stat.IsDir() {
handleNotFound(app, cfg, fpath, w, r)
handleNotFound(logger, app, cfg, fpath, w, r)

Check warning on line 453 in gno.land/pkg/gnoweb/gnoweb.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L453

Added line #L453 was not covered by tests
return
}

Expand All @@ -467,7 +468,7 @@
fpath := "img/favicon.ico"
f, err := fs.Open(fpath)
if os.IsNotExist(err) {
handleNotFound(app, cfg, fpath, w, r)
handleNotFound(logger, app, cfg, fpath, w, r)

Check warning on line 471 in gno.land/pkg/gnoweb/gnoweb.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L471

Added line #L471 was not covered by tests
return
}
w.Header().Set("Content-Type", "image/x-icon")
Expand All @@ -476,11 +477,17 @@
})
}

func handleNotFound(app gotuna.App, cfg *Config, path string, w http.ResponseWriter, r *http.Request) {
func handleNotFound(logger *slog.Logger, app gotuna.App, cfg *Config, path string, w http.ResponseWriter, r *http.Request) {
// decode path for non-ascii characters
decodedPath, err := url.PathUnescape(path)
if err != nil {
logger.Error("failed to decode path", err)
decodedPath = path

Check warning on line 485 in gno.land/pkg/gnoweb/gnoweb.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L484-L485

Added lines #L484 - L485 were not covered by tests
}
w.WriteHeader(http.StatusNotFound)
app.NewTemplatingEngine().
Set("title", "Not found").
Set("path", path).
Set("path", decodedPath).
Set("Config", cfg).
Render(w, r, "404.html", "funcs.html")
}
Expand All @@ -501,7 +508,7 @@
parts := strings.Split(diruri, "/")
if parts[0] == "gno.land" {
return "/" + strings.Join(parts[1:], "/")
} else {
panic(fmt.Sprintf("invalid dir-URI %q", diruri))
}

panic(fmt.Sprintf("invalid dir-URI %q", diruri))

Check warning on line 513 in gno.land/pkg/gnoweb/gnoweb.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L513

Added line #L513 was not covered by tests
}
4 changes: 3 additions & 1 deletion gno.land/pkg/gnoweb/gnoweb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func TestRoutes(t *testing.T) {
{"/gor", found, "/game-of-realms"},
{"/blog", found, "/r/gnoland/blog"},
{"/404-not-found", notFound, "/404-not-found"},
{"/아스키문자가아닌경로", notFound, "/아스키문자가아닌경로"},
{"/%ED%85%8C%EC%8A%A4%ED%8A%B8", notFound, "테스트"},
{"/グノー", notFound, "グノー"},
notJoon marked this conversation as resolved.
Show resolved Hide resolved
}

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

assert.Contains(t, response.Body.String(), r.substring)
// println(response.Body.String())
})
}
}
Expand Down
Loading