diff --git a/Libraries/LibWeb/DOM/HTMLFormElement.cpp b/Libraries/LibWeb/DOM/HTMLFormElement.cpp index 8e310983f1..cabb282d3e 100644 --- a/Libraries/LibWeb/DOM/HTMLFormElement.cpp +++ b/Libraries/LibWeb/DOM/HTMLFormElement.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace Web { @@ -59,12 +60,7 @@ void HTMLFormElement::submit(RefPtr submitter) URL url(document().complete_url(action())); - struct NameAndValue { - String name; - String value; - }; - - Vector parameters; + Vector parameters; for_each_in_subtree_of_type([&](auto& node) { auto& input = to(node); @@ -73,15 +69,7 @@ void HTMLFormElement::submit(RefPtr submitter) return IterationDecision::Continue; }); - StringBuilder builder; - 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()); + url.set_query(url_encode(parameters)); // FIXME: We shouldn't let the form just do this willy-nilly. document().frame()->html_view()->load(url); diff --git a/Libraries/LibWeb/Makefile b/Libraries/LibWeb/Makefile index 961648393f..2016f72898 100644 --- a/Libraries/LibWeb/Makefile +++ b/Libraries/LibWeb/Makefile @@ -89,7 +89,8 @@ LIBWEB_OBJS = \ Layout/LineBoxFragment.o \ Parser/CSSParser.o \ Parser/HTMLParser.o \ - ResourceLoader.o + ResourceLoader.o \ + URLEncoder.o EXTRA_SOURCES = \ CSS/DefaultStyleSheetSource.cpp \ diff --git a/Libraries/LibWeb/URLEncoder.cpp b/Libraries/LibWeb/URLEncoder.cpp new file mode 100644 index 0000000000..a3bd5fdd00 --- /dev/null +++ b/Libraries/LibWeb/URLEncoder.cpp @@ -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 +#include + +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 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(); +} + +} diff --git a/Libraries/LibWeb/URLEncoder.h b/Libraries/LibWeb/URLEncoder.h new file mode 100644 index 0000000000..5b8cfa01be --- /dev/null +++ b/Libraries/LibWeb/URLEncoder.h @@ -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 +#include + +namespace Web { + +struct URLQueryParam { + String name; + String value; +}; + +String url_encode(StringView); +String url_encode(Vector); + +} +