From 6da922ac6589f06abe2c4aa7bb68bccd532f70f0 Mon Sep 17 00:00:00 2001 From: Isan Rivkin Date: Wed, 13 Sep 2023 17:35:01 +0300 Subject: [PATCH 1/5] add url parsing to lua --- pkg/actions/lua/net/url/url.go | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pkg/actions/lua/net/url/url.go diff --git a/pkg/actions/lua/net/url/url.go b/pkg/actions/lua/net/url/url.go new file mode 100644 index 00000000000..20861a0b109 --- /dev/null +++ b/pkg/actions/lua/net/url/url.go @@ -0,0 +1,36 @@ +package url + +import ( + neturl "net/url" + + "github.com/Shopify/go-lua" + "github.com/treeverse/lakefs/pkg/actions/lua/util" +) + +func Open(l *lua.State) { + open := func(l *lua.State) int { + lua.NewLibrary(l, library) + return 1 + } + lua.Require(l, "net/url", open, false) + l.Pop(1) +} + +var library = []lua.RegistryFunction{ + {Name: "parse", Function: Parse}, +} + +func Parse(l *lua.State) int { + rawURL := lua.CheckString(l, 1) + u, err := neturl.Parse(rawURL) + if err != nil { + lua.Errorf(l, err.Error()) + panic("unreachable") + } + return util.DeepPush(l, map[string]string{ + "host": u.Host, + "path": u.Path, + "scheme": u.Scheme, + "query": u.RawQuery, + }) +} From 0f12b59fe54cea33014db0db2f9241901d81546c Mon Sep 17 00:00:00 2001 From: Isan Rivkin Date: Wed, 13 Sep 2023 17:36:26 +0300 Subject: [PATCH 2/5] update name --- pkg/actions/lua/net/url/url.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/actions/lua/net/url/url.go b/pkg/actions/lua/net/url/url.go index 20861a0b109..f287b164554 100644 --- a/pkg/actions/lua/net/url/url.go +++ b/pkg/actions/lua/net/url/url.go @@ -17,10 +17,10 @@ func Open(l *lua.State) { } var library = []lua.RegistryFunction{ - {Name: "parse", Function: Parse}, + {Name: "parse", Function: parse}, } -func Parse(l *lua.State) int { +func parse(l *lua.State) int { rawURL := lua.CheckString(l, 1) u, err := neturl.Parse(rawURL) if err != nil { From 75c154281d595fbd04c3ba85d2b51f5a8f8dad44 Mon Sep 17 00:00:00 2001 From: Isan Rivkin Date: Wed, 13 Sep 2023 17:52:25 +0300 Subject: [PATCH 3/5] update docs --- docs/howto/hooks/lua.md | 19 +++++++++++++++++++ pkg/actions/lua/net/url/url.go | 9 +++++---- pkg/actions/lua/open.go | 2 ++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/docs/howto/hooks/lua.md b/docs/howto/hooks/lua.md index 1f73de8a775..79abbab7704 100644 --- a/docs/howto/hooks/lua.md +++ b/docs/howto/hooks/lua.md @@ -420,6 +420,25 @@ The `value` string should be in [ISO8601](https://en.wikipedia.org/wiki/ISO_8601 Returns a new 128-bit [RFC 4122 UUID](https://www.rfc-editor.org/rfc/rfc4122){: target="_blank" } in string representation. +### `net/url` + +Provides a `parse` function that parses a raw url into a URL structure. +The url may be relative (a path, without a host) or absolute (starting with a scheme). Trying to parse a hostname and path without a scheme is invalid but may not necessarily return an error, due to parsing ambiguities. + + +```lua +> local url = require("net/url") +> url.parse("https://example.com/path?p1=a#section") +{ + ["host"] = "example.com" + ["path"] = "/path" + ["scheme"] = "https" + ["query"] = "p1=a" + ["fragment"] = "section" +} +``` + + ### `net/http` (optional) Provides a `request` function that performs an HTTP request. diff --git a/pkg/actions/lua/net/url/url.go b/pkg/actions/lua/net/url/url.go index f287b164554..ad8c4612fbd 100644 --- a/pkg/actions/lua/net/url/url.go +++ b/pkg/actions/lua/net/url/url.go @@ -28,9 +28,10 @@ func parse(l *lua.State) int { panic("unreachable") } return util.DeepPush(l, map[string]string{ - "host": u.Host, - "path": u.Path, - "scheme": u.Scheme, - "query": u.RawQuery, + "host": u.Host, + "path": u.Path, + "scheme": u.Scheme, + "query": u.RawQuery, + "fragment": u.Fragment, }) } diff --git a/pkg/actions/lua/open.go b/pkg/actions/lua/open.go index 2a40bd4ec1d..a573941209e 100644 --- a/pkg/actions/lua/open.go +++ b/pkg/actions/lua/open.go @@ -13,6 +13,7 @@ import ( "github.com/treeverse/lakefs/pkg/actions/lua/encoding/parquet" "github.com/treeverse/lakefs/pkg/actions/lua/encoding/yaml" "github.com/treeverse/lakefs/pkg/actions/lua/net/http" + "github.com/treeverse/lakefs/pkg/actions/lua/net/url" "github.com/treeverse/lakefs/pkg/actions/lua/path" "github.com/treeverse/lakefs/pkg/actions/lua/regexp" "github.com/treeverse/lakefs/pkg/actions/lua/storage/aws" @@ -41,6 +42,7 @@ func Open(l *lua.State, ctx context.Context, cfg OpenSafeConfig) { path.Open(l) aws.Open(l, ctx) gcloud.Open(l, ctx) + url.Open(l) if cfg.NetHTTPEnabled { http.Open(l) } From 2c2df6ce0437146e6784cb3fa25bbbb6801cf925 Mon Sep 17 00:00:00 2001 From: Isan Rivkin Date: Wed, 13 Sep 2023 18:17:50 +0300 Subject: [PATCH 4/5] docs --- docs/howto/hooks/lua.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/howto/hooks/lua.md b/docs/howto/hooks/lua.md index 79abbab7704..a457f085b62 100644 --- a/docs/howto/hooks/lua.md +++ b/docs/howto/hooks/lua.md @@ -422,9 +422,7 @@ Returns a new 128-bit [RFC 4122 UUID](https://www.rfc-editor.org/rfc/rfc4122){: ### `net/url` -Provides a `parse` function that parses a raw url into a URL structure. -The url may be relative (a path, without a host) or absolute (starting with a scheme). Trying to parse a hostname and path without a scheme is invalid but may not necessarily return an error, due to parsing ambiguities. - +Provides a `parse` function parse a URL string into parts, returns a table with the URL's host, path, scheme, query and fragment. ```lua > local url = require("net/url") From 170c827233ba8dfd4dffbda529d55bf503c3de9b Mon Sep 17 00:00:00 2001 From: Isan Rivkin Date: Wed, 13 Sep 2023 18:18:39 +0300 Subject: [PATCH 5/5] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41d89a748cb..fd8a8bf3537 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## UNRELEASED +:new: What's new: +- Restrict lakectl local to common prefixes only (#6510) +- Added URL parser to lua runtime (#6597) + # v0.109.0 :new: What's new: