feat(sterni/nix/url): implement urldecoding

We use builtins.split directly as it should be a bit more efficient as
lib.splitStrings. Also its returning of a list for every regex match is
useful to update the state while parsing the tokens:

* The tokens are obtained by splitting the string at every '%'
* Everytime we see a boundary (that is a list in the returned
  list of builtins.split), we know that the first two chars of
  the next string are a percent encoded character.

One implementation flaw is that it will currently crash if it encounters
mal-formed URLs (since int.fromHex chrashes if it encounters any non
hex digit characters) and accepts some malformed urlencoding like
"foo %A".

Change-Id: I90d08d7a71b16b4f4a4879214abd7aeff46c20c8
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2744
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
sterni 2021-04-01 11:15:57 +02:00
parent 28b2c79c14
commit a5f2b446aa
2 changed files with 46 additions and 0 deletions

View file

@ -16,6 +16,10 @@ let
assertEq "encode ${builtins.toJSON left} == ${builtins.toJSON right}"
(url.encode args left) right;
checkDecoding = { left, right }:
assertEq "${builtins.toJSON left} == decode ${builtins.toJSON right}"
(url.decode left) right;
unreserved = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_~";
encodeExpected = [
@ -31,6 +35,9 @@ let
testEncode = it "checks url.encode"
(builtins.map (checkEncoding {}) encodeExpected);
testDecode = it "checks url.decode"
(builtins.map checkDecoding encodeExpected);
testLeaveReserved = it "checks that leaveReserved is like id for valid URLs"
(builtins.map (x: checkEncoding { leaveReserved = true; } { left = x; right = x; }) [
"ftp://ftp.is.co.za/rfc/rfc1808.txt"