Skip to content

Commit

Permalink
Handle unicode characters in 404 page URL
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Apr 3, 2024
1 parent 6760265 commit 38f7fea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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 @@ import (
"io/fs"
"log/slog"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -122,7 +123,7 @@ func MakeApp(logger *slog.Logger, cfg Config) gotuna.App {

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 @@ func handlerStaticFile(logger *slog.Logger, app gotuna.App, cfg *Config) http.Ha
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 @@ func handlerFavicon(logger *slog.Logger, app gotuna.App, cfg *Config) http.Handl
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 handlerFavicon(logger *slog.Logger, app gotuna.App, cfg *Config) http.Handl
})
}

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 @@ func pathOf(diruri string) string {
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, "グノー"},
}

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

0 comments on commit 38f7fea

Please sign in to comment.