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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

gfanton
Copy link
Member

@gfanton gfanton commented Dec 18, 2024

This PR simplifies the URL parsing system:

  • Removes the complex regexp that was introduced in the gnoweb revamp.
  • Adds a bunch of new parsing tests for GnoURL.
  • Makes the Encode method more composable to adapt to various use cases.

@github-actions github-actions bot added the 📦 ⛰️ gno.land Issues or PRs gno.land package related label Dec 18, 2024
@gfanton gfanton changed the title fix(gnoweb): simplify url system fix(gnoweb): simplify url parsing system Dec 18, 2024
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Dec 18, 2024

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 The pull request was created from a fork (head branch repo: gfanton/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

Copy link

codecov bot commented Dec 18, 2024

Codecov Report

Attention: Patch coverage is 91.30435% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
gno.land/pkg/gnoweb/url.go 91.13% 5 Missing and 2 partials ⚠️
gno.land/pkg/gnoweb/handler.go 92.30% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@alexiscolin
Copy link
Member

Should we update the breadcrumb (to add the query path) in the same PR?

@Kouteki Kouteki added the in focus Core team is prioritizing this work label Dec 20, 2024
@Kouteki Kouteki requested a review from alexiscolin December 20, 2024 12:04
@gfanton
Copy link
Member Author

gfanton commented Dec 20, 2024

@alexiscolin I will do this in another PR ;)

gno.land/pkg/gnoweb/url.go Outdated Show resolved Hide resolved
default:
return nil, fmt.Errorf("%w: %s", ErrURLMalformedPath, u.Path)
}
if !rePkgOrRealmPath.MatchString(upath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we used gnolang.IsPurePackagePath / gnolang.IsRealmPath instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check the string path directly? Or do you mean removing the check and using IsPurePackagePath and IsRealmPath? I would like to have IsPurePackagePath and IsRealmPath; I think it's a better idea than Kind(), but I'm not sure why you pointed out this specific line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had beef with this regex and this seemed a good place where to make a comment :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, I think we can also substitute this regex to determine if it's a valid path?

Copy link
Member Author

@gfanton gfanton Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should forbid a URL that is neither pure nor a realm. I prefer this package to perform minimal checks; it should be the caller's choice to accept the URL based on the parsed information. That said, we can still establish a minimal base to work on without assuming any urls.

What do you think about:

  • Removing the concept of "kind" within the structure while still providing helpers to check if the method is pure or a realm, as you mentioned.
  • Adding a new File field, trimming any file from the path when parsing and adding it to the structure.
  • Refining the regex to define what a path can be, based on what we have in gnovm/pkg/gnolang/helpers.go, with the only base constraint that it must start with a slash and at least one character. This will makes the check and the assumption of the path structure simple and easy to manage.
var rePkgOrRealmPath = regexp.MustCompile(`^/[a-z][a-z0-9_./]*$`)

Only pure and realm exist, but gnoweb will likely extend this with /u, for example. So it makes sense to me to make GnoURL a bit more flexible in what it can parse. This way, the caller can decide whether to use it as a direct call to the node or to redirect the user to something else.

Copy link
Member Author

@gfanton gfanton Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thehowl suggestion preview: gfanton@d0eb4b0

I've directly pushed the commit 8f93987 with suggested changes. If you think it's appropriate, feel free to merge it. Otherwise, please don't hesitate to give me feedback so I can make the necessary updates.

edit: i've made another commit (62b005a) to handle special file, such as LICSENSE

gno.land/pkg/gnoweb/url.go Outdated Show resolved Hide resolved
gno.land/pkg/gnoweb/url.go Show resolved Hide resolved
- Removing the concept of "kind" within the structure while still providing helpers to check if the method is pure or a realm:
- Adding a new File field, trimming any file from the path when parsing and adding it to the structure.
- Refining the regex to define what a path can be, based on what we have in `gnovm/pkg/gnolang/helpers.go`
"var rePkgOrRealmPath = regexp.MustCompile(`^/[a-z][a-z0-9_/]*$`)"

Signed-off-by: gfanton <[email protected]>
@gfanton gfanton force-pushed the fix/fgnoweb/simplify-url branch from 8d51e3b to 8f93987 Compare December 26, 2024 15:31
@gfanton gfanton requested a review from thehowl January 6, 2025 10:16
@gfanton gfanton force-pushed the fix/fgnoweb/simplify-url branch from c6e36df to 62b005a Compare January 6, 2025 10:36
Copy link
Member

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 💯

Feel free to merge 🚀

@@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

// but this may require changes in some realms.
args := gnoURL.Args
if !encodeFlags.Has(EncodeNoEscape) {
args = escapeDollarSign(url.PathEscape(args))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: you only call this func once

}

func (url GnoURL) EncodePath() string {
// Encode encodes the URL components based on the provided flags.
func (gnoURL GnoURL) Encode(encodeFlags EncodeFlag) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a small section on the gnoweb docs for these URL flags?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in focus Core team is prioritizing this work 📦 ⛰️ gno.land Issues or PRs gno.land package related
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

6 participants