2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "FormSubmission.h"
34 #include "DOMFormData.h"
38 #include "FormDataBuilder.h"
39 #include "FormState.h"
41 #include "FrameLoadRequest.h"
42 #include "FrameLoader.h"
43 #include "HTMLFormControlElement.h"
44 #include "HTMLFormElement.h"
45 #include "HTMLInputElement.h"
46 #include "HTMLNames.h"
47 #include "HTMLParserIdioms.h"
48 #include "TextEncoding.h"
49 #include <wtf/CurrentTime.h>
50 #include <wtf/RandomNumber.h>
54 using namespace HTMLNames;
56 static int64_t generateFormDataIdentifier()
58 // Initialize to the current time to reduce the likelihood of generating
59 // identifiers that overlap with those from past/future browser sessions.
60 static int64_t nextIdentifier = static_cast<int64_t>(currentTime() * 1000000.0);
61 return ++nextIdentifier;
64 static void appendMailtoPostFormDataToURL(KURL& url, const FormData& data, const String& encodingType)
66 String body = data.flattenToString();
68 if (equalIgnoringCase(encodingType, "text/plain")) {
69 // Convention seems to be to decode, and s/&/\r\n/. Also, spaces are encoded as %20.
70 body = decodeURLEscapeSequences(body.replace('&', "\r\n").replace('+', ' ') + "\r\n");
73 Vector<char> bodyData;
74 bodyData.append("body=", 5);
75 FormDataBuilder::encodeStringAsFormData(bodyData, body.utf8());
76 body = String(bodyData.data(), bodyData.size()).replace('+', "%20");
78 String query = url.query();
85 void FormSubmission::Attributes::parseAction(const String& action)
87 // FIXME: Can we parse into a KURL?
88 m_action = stripLeadingAndTrailingHTMLSpaces(action);
91 String FormSubmission::Attributes::parseEncodingType(const String& type)
93 if (type.contains("multipart", false) || type.contains("form-data", false))
94 return "multipart/form-data";
95 if (type.contains("text", false) || type.contains("plain", false))
97 return "application/x-www-form-urlencoded";
100 void FormSubmission::Attributes::updateEncodingType(const String& type)
102 m_encodingType = parseEncodingType(type);
103 m_isMultiPartForm = (m_encodingType == "multipart/form-data");
106 FormSubmission::Method FormSubmission::Attributes::parseMethodType(const String& type)
108 return equalIgnoringCase(type, "post") ? FormSubmission::PostMethod : FormSubmission::GetMethod;
111 void FormSubmission::Attributes::updateMethodType(const String& type)
113 m_method = parseMethodType(type);
116 void FormSubmission::Attributes::copyFrom(const Attributes& other)
118 m_method = other.m_method;
119 m_isMultiPartForm = other.m_isMultiPartForm;
121 m_action = other.m_action;
122 m_target = other.m_target;
123 m_encodingType = other.m_encodingType;
124 m_acceptCharset = other.m_acceptCharset;
127 inline FormSubmission::FormSubmission(Method method, const KURL& action, const String& target, const String& contentType, PassRefPtr<FormState> state, PassRefPtr<FormData> data, const String& boundary, bool lockHistory, PassRefPtr<Event> event)
131 , m_contentType(contentType)
134 , m_boundary(boundary)
135 , m_lockHistory(lockHistory)
140 PassRefPtr<FormSubmission> FormSubmission::create(HTMLFormElement* form, const Attributes& attributes, PassRefPtr<Event> event, bool lockHistory, FormSubmissionTrigger trigger)
144 HTMLFormControlElement* submitButton = 0;
145 if (event && event->target() && event->target()->toNode())
146 submitButton = static_cast<HTMLFormControlElement*>(event->target()->toNode());
148 FormSubmission::Attributes copiedAttributes;
149 copiedAttributes.copyFrom(attributes);
151 String attributeValue;
152 if (!(attributeValue = submitButton->getAttribute(formactionAttr)).isNull())
153 copiedAttributes.parseAction(attributeValue);
154 if (!(attributeValue = submitButton->getAttribute(formenctypeAttr)).isNull())
155 copiedAttributes.updateEncodingType(attributeValue);
156 if (!(attributeValue = submitButton->getAttribute(formmethodAttr)).isNull())
157 copiedAttributes.updateMethodType(attributeValue);
158 if (!(attributeValue = submitButton->getAttribute(formtargetAttr)).isNull())
159 copiedAttributes.setTarget(attributeValue);
162 Document* document = form->document();
163 KURL actionURL = document->completeURL(copiedAttributes.action().isEmpty() ? document->url().string() : copiedAttributes.action());
164 bool isMailtoForm = actionURL.protocolIs("mailto");
165 bool isMultiPartForm = false;
166 String encodingType = copiedAttributes.encodingType();
168 if (copiedAttributes.method() == PostMethod) {
169 isMultiPartForm = copiedAttributes.isMultiPartForm();
170 if (isMultiPartForm && isMailtoForm) {
171 encodingType = "application/x-www-form-urlencoded";
172 isMultiPartForm = false;
176 TextEncoding dataEncoding = isMailtoForm ? UTF8Encoding() : FormDataBuilder::encodingFromAcceptCharset(copiedAttributes.acceptCharset(), document);
177 RefPtr<DOMFormData> domFormData = DOMFormData::create(dataEncoding.encodingForFormSubmission());
178 Vector<pair<String, String> > formValues;
180 for (unsigned i = 0; i < form->associatedElements().size(); ++i) {
181 FormAssociatedElement* control = form->associatedElements()[i];
182 HTMLElement* element = toHTMLElement(control);
183 if (!element->disabled())
184 control->appendFormData(*domFormData, isMultiPartForm);
185 if (element->hasLocalName(inputTag)) {
186 HTMLInputElement* input = static_cast<HTMLInputElement*>(control);
187 if (input->isTextField()) {
188 formValues.append(pair<String, String>(input->name().string(), input->value()));
189 if (input->isSearchField())
190 input->addSearchResult();
195 RefPtr<FormData> formData;
198 if (isMultiPartForm) {
199 formData = FormData::createMultiPart(*(static_cast<FormDataList*>(domFormData.get())), domFormData->encoding(), document);
200 boundary = formData->boundary().data();
202 formData = FormData::create(*(static_cast<FormDataList*>(domFormData.get())), domFormData->encoding());
203 if (copiedAttributes.method() == PostMethod && isMailtoForm) {
204 // Convert the form data into a string that we put into the URL.
205 appendMailtoPostFormDataToURL(actionURL, *formData, encodingType);
206 formData = FormData::create();
210 formData->setIdentifier(generateFormDataIdentifier());
211 String targetOrBaseTarget = copiedAttributes.target().isEmpty() ? document->baseTarget() : copiedAttributes.target();
212 RefPtr<FormState> formState = FormState::create(form, formValues, document->frame(), trigger);
213 return adoptRef(new FormSubmission(copiedAttributes.method(), actionURL, targetOrBaseTarget, encodingType, formState.release(), formData.release(), boundary, lockHistory, event));
216 KURL FormSubmission::requestURL() const
218 if (m_method == FormSubmission::PostMethod)
221 KURL requestURL(m_action);
222 requestURL.setQuery(m_formData->flattenToString());
226 void FormSubmission::populateFrameLoadRequest(FrameLoadRequest& frameRequest)
228 if (!m_target.isEmpty())
229 frameRequest.setFrameName(m_target);
231 if (!m_referrer.isEmpty())
232 frameRequest.resourceRequest().setHTTPReferrer(m_referrer);
234 if (m_method == FormSubmission::PostMethod) {
235 frameRequest.resourceRequest().setHTTPMethod("POST");
236 frameRequest.resourceRequest().setHTTPBody(m_formData);
238 // construct some user headers if necessary
239 if (m_contentType.isNull() || m_contentType == "application/x-www-form-urlencoded")
240 frameRequest.resourceRequest().setHTTPContentType(m_contentType);
241 else // contentType must be "multipart/form-data"
242 frameRequest.resourceRequest().setHTTPContentType(m_contentType + "; boundary=" + m_boundary);
245 frameRequest.resourceRequest().setURL(requestURL());
246 FrameLoader::addHTTPOriginIfNeeded(frameRequest.resourceRequest(), m_origin);