mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibWeb: Add URL::url_decode for decoding form url encoded parameters
This commit is contained in:
parent
4629f2e4ad
commit
de19dcf81a
Notes:
sideshowbarker
2024-07-18 04:04:00 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/de19dcf81aa Pull-request: https://github.com/SerenityOS/serenity/pull/10010
2 changed files with 43 additions and 1 deletions
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibWeb/URL/URL.h>
|
||||
|
||||
namespace Web::URL {
|
||||
|
@ -24,4 +23,45 @@ String url_encode(const Vector<QueryParam>& pairs, AK::URL::PercentEncodeSet per
|
|||
return builder.to_string();
|
||||
}
|
||||
|
||||
Vector<QueryParam> url_decode(StringView const& input)
|
||||
{
|
||||
// 1. Let sequences be the result of splitting input on 0x26 (&).
|
||||
auto sequences = input.split_view('&');
|
||||
|
||||
// 2. Let output be an initially empty list of name-value tuples where both name and value hold a string.
|
||||
Vector<QueryParam> output;
|
||||
|
||||
// 3. For each byte sequence bytes in sequences:
|
||||
for (auto bytes : sequences) {
|
||||
// 1. If bytes is the empty byte sequence, then continue.
|
||||
if (bytes.is_empty())
|
||||
continue;
|
||||
|
||||
StringView name;
|
||||
StringView value;
|
||||
|
||||
// 2. If bytes contains a 0x3D (=), then let name be the bytes from the start of bytes up to but excluding its first 0x3D (=), and let value be the bytes, if any, after the first 0x3D (=) up to the end of bytes. If 0x3D (=) is the first byte, then name will be the empty byte sequence. If it is the last, then value will be the empty byte sequence.
|
||||
if (auto index = bytes.find('='); index.has_value()) {
|
||||
name = bytes.substring_view(0, *index);
|
||||
value = bytes.substring_view(*index + 1);
|
||||
}
|
||||
// 3. Otherwise, let name have the value of bytes and let value be the empty byte sequence.
|
||||
else {
|
||||
name = bytes;
|
||||
value = ""sv;
|
||||
}
|
||||
|
||||
// 4. Replace any 0x2B (+) in name and value with 0x20 (SP).
|
||||
auto space_decoded_name = name.replace("+"sv, " "sv, true);
|
||||
|
||||
// 5. Let nameString and valueString be the result of running UTF-8 decode without BOM on the percent-decoding of name and value, respectively.
|
||||
auto name_string = AK::URL::percent_decode(space_decoded_name);
|
||||
auto value_string = AK::URL::percent_decode(value);
|
||||
|
||||
output.empend(move(name_string), move(value_string));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
namespace Web::URL {
|
||||
|
@ -17,5 +18,6 @@ struct QueryParam {
|
|||
String value;
|
||||
};
|
||||
String url_encode(const Vector<QueryParam>&, AK::URL::PercentEncodeSet);
|
||||
Vector<QueryParam> url_decode(StringView const&);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue