Skip to content

Commit

Permalink
feat: add bitwise flags to encode url
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Dec 19, 2024
1 parent 7db64ce commit 6a07ed2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (h *WebHandler) renderPackage(w io.Writer, gnourl *GnoURL) (status int, err

// Render content into the content buffer
var content bytes.Buffer
meta, err := h.webcli.Render(&content, gnourl.Path, gnourl.EncodeArgs())
meta, err := h.webcli.Render(&content, gnourl.Path, gnourl.EncodeArgsQuery())
if err != nil {
if errors.Is(err, vm.InvalidPkgPathError{}) {
return http.StatusNotFound, components.RenderStatusComponent(w, "not found")
Expand Down
69 changes: 40 additions & 29 deletions gno.land/pkg/gnoweb/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,57 @@ type GnoURL struct {
Query url.Values // c=d
}

// EncodeArgs encodes the arguments and query parameters into a string.
func (gnoURL GnoURL) EncodeArgs() string {
type EncodeFlag int

const (
EncodePath EncodeFlag = 1 << iota
EncodeArgs
EncodeWebQuery
EncodeQuery
)

// Encode encodes the URL components based on the provided flags.
func (gnoURL GnoURL) Encode(encodeFlags EncodeFlag) string {
var urlstr strings.Builder
if gnoURL.Args != "" {
urlstr.WriteString(gnoURL.Args)

if encodeFlags&EncodePath != 0 {
urlstr.WriteString(gnoURL.Path)
}
if len(gnoURL.Query) > 0 {
urlstr.WriteString("?" + gnoURL.Query.Encode())

if encodeFlags&EncodeArgs != 0 && gnoURL.Args != "" {
if encodeFlags&EncodePath != 0 {
urlstr.WriteString(":")
}
urlstr.WriteString(gnoURL.Args)
}
return urlstr.String()
}

// EncodePath encodes the path, arguments, and query parameters into a string.
func (gnoURL GnoURL) EncodePath() string {
var urlstr strings.Builder
urlstr.WriteString(gnoURL.Path)
if gnoURL.Args != "" {
urlstr.WriteString(":" + gnoURL.Args)
if encodeFlags&EncodeWebQuery != 0 && len(gnoURL.WebQuery) > 0 {
urlstr.WriteString("$" + gnoURL.WebQuery.Encode())
}
if len(gnoURL.Query) > 0 {

if encodeFlags&EncodeQuery != 0 && len(gnoURL.Query) > 0 {
urlstr.WriteString("?" + gnoURL.Query.Encode())
}

return urlstr.String()
}

// EncodeWebPath encodes the path, arguments, and both web and query parameters into a string.
// EncodeArgsQuery encodes the arguments and query parameters into a string.
// This function is intended to be passed as a realm `Render` argument.
func (gnoURL GnoURL) EncodeArgsQuery() string {
return gnoURL.Encode(EncodeArgs | EncodeQuery)
}

// EncodePathArgsQuery encodes the path, arguments, and query parameters into a string.
// This function provides the full representation of the URL without the web query.
func (gnoURL GnoURL) EncodePathArgsQuery() string {
return gnoURL.Encode(EncodePath | EncodeArgs | EncodeQuery)

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

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/url.go#L78-L79

Added lines #L78 - L79 were not covered by tests
}

// EncodeWebPath encodes the path, package arguments, web query, and query into a string.
// This function provides the full representation of the URL.
func (gnoURL GnoURL) EncodeWebPath() string {
var urlstr strings.Builder
urlstr.WriteString(gnoURL.Path)
if gnoURL.Args != "" {
pathEscape := escapeDollarSign(gnoURL.Args)
urlstr.WriteString(":" + pathEscape)
}
if len(gnoURL.WebQuery) > 0 {
urlstr.WriteString("$" + gnoURL.WebQuery.Encode())
}
if len(gnoURL.Query) > 0 {
urlstr.WriteString("?" + gnoURL.Query.Encode())
}
return urlstr.String()
return gnoURL.Encode(EncodePath | EncodeArgs | EncodeWebQuery | EncodeQuery)
}

// Kind determines the kind of path (invalid, realm, or pure) based on the path structure.
Expand Down
1 change: 0 additions & 1 deletion gno.land/pkg/gnoweb/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ func TestParseGnoURL(t *testing.T) {
result, err := ParseGnoURL(u)
if tc.Err == nil {
require.NoError(t, err)
t.Logf("encoded path: %q", result.EncodePath())
t.Logf("encoded web path: %q", result.EncodeWebPath())
} else {
require.Error(t, err)
Expand Down

0 comments on commit 6a07ed2

Please sign in to comment.