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(gnoweb): simplify url parsing system #3366

Merged
merged 23 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f82ec43
fix: simplify url system
gfanton Dec 17, 2024
e28401a
wip: more tests
gfanton Dec 18, 2024
4c6535a
fix: improve url
gfanton Dec 18, 2024
5d855d7
feat: improve url tests
gfanton Dec 18, 2024
039b9ed
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Dec 18, 2024
9f7b111
fix: simplify more and add comments
gfanton Dec 18, 2024
d44ea6b
chore: lint
gfanton Dec 18, 2024
d809304
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Dec 18, 2024
7db64ce
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Dec 19, 2024
6a07ed2
feat: add bitwise flags to encode url
gfanton Dec 19, 2024
ed3709c
fix: improve url encoding + tests
gfanton Dec 20, 2024
3a5a71c
feat(url): add IsValid method
gfanton Dec 20, 2024
6ffc310
chore: lint
gfanton Dec 20, 2024
0c41f48
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Dec 20, 2024
c88c186
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Dec 20, 2024
bff4a0b
Update gno.land/pkg/gnoweb/url.go - typo
gfanton Dec 20, 2024
90318c7
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Dec 23, 2024
8f93987
feat: improve url
gfanton Dec 23, 2024
c51ec01
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Jan 6, 2025
62b005a
fix: check for upcase string as file
gfanton Jan 6, 2025
b3c9463
fix: add some documentation on Encode function
gfanton Jan 7, 2025
05d7810
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Jan 7, 2025
1e0366a
Merge branch 'master' into fix/fgnoweb/simplify-url
gfanton Jan 8, 2025
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
4 changes: 3 additions & 1 deletion gno.land/pkg/gnoweb/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestRoutes(t *testing.T) {

for _, r := range routes {
t.Run(fmt.Sprintf("test route %s", r.route), func(t *testing.T) {
t.Logf("input: %q", r.route)
request := httptest.NewRequest(http.MethodGet, r.route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestAnalytics(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
fmt.Println("HELLO:", response.Body.String())

assert.Contains(t, response.Body.String(), "sa.gno.services")
})
}
Expand All @@ -143,6 +144,7 @@ func TestAnalytics(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)

assert.NotContains(t, response.Body.String(), "sa.gno.services")
})
}
Expand Down
48 changes: 14 additions & 34 deletions gno.land/pkg/gnoweb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@
indexData.HeaderData.WebQuery = gnourl.WebQuery

// Render
switch gnourl.Kind() {
case KindRealm, KindPure:
switch {
case gnourl.IsRealm(), gnourl.IsPure():
status, err = h.renderPackage(&body, gnourl)
default:
h.logger.Debug("invalid page kind", "kind", gnourl.Kind)
h.logger.Debug("invalid path: path is neither a pure package or a realm")
status, err = http.StatusNotFound, components.RenderStatusComponent(&body, "page not found")
}
}
Expand All @@ -129,37 +129,20 @@
func (h *WebHandler) renderPackage(w io.Writer, gnourl *GnoURL) (status int, err error) {
h.logger.Info("component render", "path", gnourl.Path, "args", gnourl.Args)

kind := gnourl.Kind()

// Display realm help page?
if kind == KindRealm && gnourl.WebQuery.Has("help") {
if gnourl.WebQuery.Has("help") {
return h.renderRealmHelp(w, gnourl)
}

// Display package source page?
switch {
case gnourl.WebQuery.Has("source"):
return h.renderRealmSource(w, gnourl)
case kind == KindPure,
strings.HasSuffix(gnourl.Path, "/"),
isFile(gnourl.Path):
i := strings.LastIndexByte(gnourl.Path, '/')
if i < 0 {
return http.StatusInternalServerError, fmt.Errorf("unable to get ending slash for %q", gnourl.Path)
}

case gnourl.IsFile():
// Fill webquery with file infos
gnourl.WebQuery.Set("source", "") // set source

file := gnourl.Path[i+1:]
if file == "" {
return h.renderRealmDirectory(w, gnourl)
}

gnourl.WebQuery.Set("file", file)
gnourl.Path = gnourl.Path[:i]

return h.renderRealmSource(w, gnourl)
case gnourl.IsDir(), gnourl.IsPure():
return h.renderRealmDirectory(w, gnourl)
}

// Render content into the content buffer
Expand Down Expand Up @@ -250,12 +233,16 @@
return http.StatusOK, components.RenderStatusComponent(w, "no files available")
}

file := gnourl.WebQuery.Get("file") // webquery override file
if file == "" {
file = gnourl.File
}

var fileName string
file := gnourl.WebQuery.Get("file")
if file == "" {
fileName = files[0]
fileName = files[0] // Default to the first file if none specified

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

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/handler.go#L243

Added line #L243 was not covered by tests
} else if slices.Contains(files, file) {
fileName = file
fileName = file // Use specified file if it exists
} else {
h.logger.Error("unable to render source", "file", file, "err", "file does not exist")
return http.StatusInternalServerError, components.RenderStatusComponent(w, "internal error")
Expand Down Expand Up @@ -370,10 +357,3 @@

return parts
}

// IsFile checks if the last element of the path is a file (has an extension)
func isFile(path string) bool {
base := filepath.Base(path)
ext := filepath.Ext(base)
return ext != ""
}
Loading
Loading