HTMLFormElement.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibWeb/HTML/HTMLFormElement.h>
  28. #include <LibWeb/HTML/HTMLInputElement.h>
  29. #include <LibWeb/InProcessWebView.h>
  30. #include <LibWeb/Page/Frame.h>
  31. #include <LibWeb/URLEncoder.h>
  32. namespace Web::HTML {
  33. HTMLFormElement::HTMLFormElement(DOM::Document& document, const QualifiedName& qualified_name)
  34. : HTMLElement(document, qualified_name)
  35. {
  36. }
  37. HTMLFormElement::~HTMLFormElement()
  38. {
  39. }
  40. void HTMLFormElement::submit(RefPtr<HTMLInputElement> submitter)
  41. {
  42. if (action().is_null()) {
  43. dbg() << "Unsupported form action ''";
  44. return;
  45. }
  46. auto effective_method = method().to_lowercase();
  47. if (effective_method == "dialog") {
  48. dbg() << "Failed to submit form: Unsupported form method '" << method() << "'";
  49. return;
  50. }
  51. if (effective_method != "get" && effective_method != "post") {
  52. effective_method = "get";
  53. }
  54. URL url(document().complete_url(action()));
  55. if (!url.is_valid()) {
  56. dbg() << "Failed to submit form: Invalid URL: " << action();
  57. return;
  58. }
  59. if (url.protocol() == "file") {
  60. if (document().url().protocol() != "file") {
  61. dbg() << "Failed to submit form: Security violation: " << document().url() << " may not submit to " << url;
  62. return;
  63. }
  64. if (effective_method != "get") {
  65. dbg() << "Failed to submit form: Unsupported form method '" << method() << "' for URL: " << url;
  66. return;
  67. }
  68. } else if (url.protocol() != "http" && url.protocol() != "https") {
  69. dbg() << "Failed to submit form: Unsupported protocol for URL: " << url;
  70. return;
  71. }
  72. Vector<URLQueryParam> parameters;
  73. for_each_in_subtree_of_type<HTMLInputElement>([&](auto& node) {
  74. auto& input = downcast<HTMLInputElement>(node);
  75. if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
  76. parameters.append({ input.name(), input.value() });
  77. return IterationDecision::Continue;
  78. });
  79. if (effective_method == "get") {
  80. url.set_query(urlencode(parameters));
  81. }
  82. LoadRequest request;
  83. request.set_url(url);
  84. if (effective_method == "post") {
  85. auto body = urlencode(parameters).to_byte_buffer();
  86. request.set_method("POST");
  87. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  88. request.set_header("Content-Length", String::number(body.size()));
  89. request.set_body(body);
  90. }
  91. if (auto* page = document().page())
  92. page->load(request);
  93. }
  94. }