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

Option for encoding query values #317

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions docs/docs/getting-started/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ the Kulala plugin with the available `opts`:

-- certificates
certificates = {},

-- Specify how to escape query parameters
-- possible values: always, skipencoded = keep %xx as is
urlencode = "always",
},
}
```
Expand Down Expand Up @@ -653,4 +657,26 @@ Hostnames with prefix `*.` will be used as wildcard certificates for the host it
- `sub.api.company.com`
- etc.

### urlencode

Specify how to escape query parameters.

Possible values:

- `always`
- `skipencoded` = keep already encoded `%xx` as is

Default: `always`

Example:

```lua
{
"mistweaverco/kulala.nvim",
opts = {
urlencode = "skipencoded",
},
}
```

[see-env-files]: https://learn.microsoft.com/en-us/aspnet/core/test/http-files?view=aspnetcore-8.0#environment-files
3 changes: 3 additions & 0 deletions lua/kulala/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ M.defaults = {
environment_scope = "b",
-- certificates
certificates = {},
-- Specify how to escape query parameters
-- possible values: always, skipencoded = keep %xx as is
urlencode = "always",
}

M.default_contenttype = {
Expand Down
16 changes: 12 additions & 4 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ local function parse_headers(headers, variables, env, silent)
return h
end

local function url_encode(str)
if CONFIG.get().urlencode == "skipencoded" then
return STRING_UTILS.url_encode_skipencoded(str)
else
return STRING_UTILS.url_encode(str)
end
end

local function encode_url_params(url)
local anchor = ""
local index = url:find("#")
if index then
anchor = "#" .. STRING_UTILS.url_encode(url:sub(index + 1))
anchor = "#" .. url_encode(url:sub(index + 1))
url = url:sub(1, index - 1)
end
index = url:find("?")
Expand All @@ -83,11 +91,11 @@ local function encode_url_params(url)
if index then
query_params = query_params
.. "&"
.. STRING_UTILS.url_encode(query_part:sub(1, index - 1))
.. url_encode(query_part:sub(1, index - 1))
.. "="
.. STRING_UTILS.url_encode(query_part:sub(index + 1))
.. url_encode(query_part:sub(index + 1))
else
query_params = query_params .. "&" .. STRING_UTILS.url_encode(query_part)
query_params = query_params .. "&" .. url_encode(query_part)
end
end
if query_params ~= "" then
Expand Down
15 changes: 15 additions & 0 deletions lua/kulala/utils/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ M.url_encode = function(str)
return str
end

M.url_encode_skipencoded = function(str)
local res = ""
repeat
local startpos, endpos = str:find("%%%x%x")
if startpos and endpos then
res = res .. M.url_encode(str:sub(1, startpos - 1)) .. str:sub(startpos, endpos)
str = str:sub(endpos + 1)
else
res = res .. M.url_encode(str)
str = ""
end
until str == ""
return res
end

M.url_decode = function(str)
if str then
str = string.gsub(str, "%%(%x%x)", function(h)
Expand Down