1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:28:12 +00:00

LibWeb: Add basic URL encoder for individual values and param lists

This commit is contained in:
Shadowfacts 2020-05-04 23:43:05 -04:00 committed by Andreas Kling
parent 7f538ea7eb
commit fcd922f7b1
4 changed files with 110 additions and 16 deletions

View file

@ -29,6 +29,7 @@
#include <LibWeb/DOM/HTMLInputElement.h> #include <LibWeb/DOM/HTMLInputElement.h>
#include <LibWeb/Frame.h> #include <LibWeb/Frame.h>
#include <LibWeb/HtmlView.h> #include <LibWeb/HtmlView.h>
#include <LibWeb/URLEncoder.h>
namespace Web { namespace Web {
@ -59,12 +60,7 @@ void HTMLFormElement::submit(RefPtr<HTMLInputElement> submitter)
URL url(document().complete_url(action())); URL url(document().complete_url(action()));
struct NameAndValue { Vector<URLQueryParam> parameters;
String name;
String value;
};
Vector<NameAndValue> parameters;
for_each_in_subtree_of_type<HTMLInputElement>([&](auto& node) { for_each_in_subtree_of_type<HTMLInputElement>([&](auto& node) {
auto& input = to<HTMLInputElement>(node); auto& input = to<HTMLInputElement>(node);
@ -73,15 +69,7 @@ void HTMLFormElement::submit(RefPtr<HTMLInputElement> submitter)
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
StringBuilder builder; url.set_query(url_encode(parameters));
for (size_t i = 0; i < parameters.size(); ++i) {
builder.append(parameters[i].name);
builder.append('=');
builder.append(parameters[i].value);
if (i != parameters.size() - 1)
builder.append('&');
}
url.set_query(builder.to_string());
// FIXME: We shouldn't let the form just do this willy-nilly. // FIXME: We shouldn't let the form just do this willy-nilly.
document().frame()->html_view()->load(url); document().frame()->html_view()->load(url);

View file

@ -89,7 +89,8 @@ LIBWEB_OBJS = \
Layout/LineBoxFragment.o \ Layout/LineBoxFragment.o \
Parser/CSSParser.o \ Parser/CSSParser.o \
Parser/HTMLParser.o \ Parser/HTMLParser.o \
ResourceLoader.o ResourceLoader.o \
URLEncoder.o
EXTRA_SOURCES = \ EXTRA_SOURCES = \
CSS/DefaultStyleSheetSource.cpp \ CSS/DefaultStyleSheetSource.cpp \

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringBuilder.h>
#include <LibWeb/URLEncoder.h>
namespace Web {
String url_encode(StringView view)
{
StringBuilder builder;
for (char c : view) {
if (c == ' ') {
builder.append('+');
} else if (c == '*' || c == '-' || c == '.' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z')) {
builder.append(c);
} else {
builder.appendf("%%%02X", c);
}
}
return builder.to_string();
}
String url_encode(Vector<URLQueryParam> pairs)
{
StringBuilder builder;
for (size_t i = 0; i < pairs.size(); ++i) {
builder.append(url_encode(pairs[i].name));
builder.append('=');
builder.append(url_encode(pairs[i].value));
if (i != pairs.size() - 1)
builder.append('&');
}
return builder.to_string();
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/String.h>
#include <AK/Vector.h>
namespace Web {
struct URLQueryParam {
String name;
String value;
};
String url_encode(StringView);
String url_encode(Vector<URLQueryParam>);
}