diff --git a/docs/docs/getting-started/configuration-options.md b/docs/docs/getting-started/configuration-options.md index 48e32dc..fa6b1c5 100644 --- a/docs/docs/getting-started/configuration-options.md +++ b/docs/docs/getting-started/configuration-options.md @@ -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", }, } ``` @@ -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 diff --git a/lua/kulala/config/init.lua b/lua/kulala/config/init.lua index aa381c0..f7dbe7d 100644 --- a/lua/kulala/config/init.lua +++ b/lua/kulala/config/init.lua @@ -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 = { diff --git a/lua/kulala/parser/init.lua b/lua/kulala/parser/init.lua index af8580e..cdf2c4c 100644 --- a/lua/kulala/parser/init.lua +++ b/lua/kulala/parser/init.lua @@ -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("?") @@ -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 diff --git a/lua/kulala/utils/string.lua b/lua/kulala/utils/string.lua index ccb332a..0c5c41f 100644 --- a/lua/kulala/utils/string.lua +++ b/lua/kulala/utils/string.lua @@ -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)