Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Unified Diff: third_party/WebKit/Source/core/xmlhttprequest/XMLHttpRequest.cpp

Issue 2723583005: Support XMLHttpRequest.send(URLSearchParams) (Closed)
Patch Set: rebased upto r454567 Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/xmlhttprequest/XMLHttpRequest.cpp
diff --git a/third_party/WebKit/Source/core/xmlhttprequest/XMLHttpRequest.cpp b/third_party/WebKit/Source/core/xmlhttprequest/XMLHttpRequest.cpp
index eae72e1e03323bca9e761c5068e30b3531e7451d..c74d9e94739cd731464822eaaff8b2fe52b1bfc5 100644
--- a/third_party/WebKit/Source/core/xmlhttprequest/XMLHttpRequest.cpp
+++ b/third_party/WebKit/Source/core/xmlhttprequest/XMLHttpRequest.cpp
@@ -23,7 +23,8 @@
#include "core/xmlhttprequest/XMLHttpRequest.h"
-#include "bindings/core/v8/ArrayBufferOrArrayBufferViewOrBlobOrDocumentOrStringOrFormData.h"
+#include <memory>
+#include "bindings/core/v8/ArrayBufferOrArrayBufferViewOrBlobOrDocumentOrStringOrFormDataOrURLSearchParams.h"
#include "bindings/core/v8/ArrayBufferOrArrayBufferViewOrBlobOrUSVString.h"
#include "bindings/core/v8/DOMWrapperWorld.h"
#include "bindings/core/v8/ExceptionState.h"
@@ -37,6 +38,7 @@
#include "core/dom/DocumentParser.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
+#include "core/dom/URLSearchParams.h"
#include "core/dom/XMLDocument.h"
#include "core/editing/serializers/Serialization.h"
#include "core/events/Event.h"
@@ -81,7 +83,6 @@
#include "wtf/AutoReset.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/CString.h"
-#include <memory>
namespace blink {
@@ -704,7 +705,8 @@ bool XMLHttpRequest::initSend(ExceptionState& exceptionState) {
}
void XMLHttpRequest::send(
- const ArrayBufferOrArrayBufferViewOrBlobOrDocumentOrStringOrFormData& body,
+ const ArrayBufferOrArrayBufferViewOrBlobOrDocumentOrStringOrFormDataOrURLSearchParams&
+ body,
ExceptionState& exceptionState) {
probe::willSendXMLHttpOrFetchNetworkRequest(getExecutionContext(), url());
@@ -738,6 +740,11 @@ void XMLHttpRequest::send(
return;
}
+ if (body.isURLSearchParams()) {
+ send(body.getAsURLSearchParams(), exceptionState);
+ return;
+ }
+
DCHECK(body.isString());
send(body.getAsString(), exceptionState);
}
@@ -784,17 +791,9 @@ void XMLHttpRequest::send(const String& body, ExceptionState& exceptionState) {
RefPtr<EncodedFormData> httpBody;
if (!body.isNull() && areMethodAndURLValidForSend()) {
- String contentType = getRequestHeader(HTTPNames::Content_Type);
- if (contentType.isEmpty()) {
- setRequestHeaderInternal(HTTPNames::Content_Type,
- "text/plain;charset=UTF-8");
- } else {
- replaceCharsetInMediaType(contentType, "UTF-8");
- m_requestHeaders.set(HTTPNames::Content_Type, AtomicString(contentType));
- }
-
httpBody = EncodedFormData::create(
UTF8Encoding().encode(body, WTF::EntitiesForUnencodables));
+ updateContentTypeAndCharset("text/plain;charset=UTF-8", "UTF-8");
}
createRequest(std::move(httpBody), exceptionState);
@@ -846,6 +845,8 @@ void XMLHttpRequest::send(FormData* body, ExceptionState& exceptionState) {
if (areMethodAndURLValidForSend()) {
httpBody = body->encodeMultiPartFormData();
+ // TODO (sof): override any author-provided charset= in the
+ // content type value to UTF-8 ?
if (getRequestHeader(HTTPNames::Content_Type).isEmpty()) {
AtomicString contentType =
AtomicString("multipart/form-data; boundary=") +
@@ -857,6 +858,24 @@ void XMLHttpRequest::send(FormData* body, ExceptionState& exceptionState) {
createRequest(std::move(httpBody), exceptionState);
}
+void XMLHttpRequest::send(URLSearchParams* body,
+ ExceptionState& exceptionState) {
+ NETWORK_DVLOG(1) << this << " send() URLSearchParams " << body;
+
+ if (!initSend(exceptionState))
+ return;
+
+ RefPtr<EncodedFormData> httpBody;
+
+ if (areMethodAndURLValidForSend()) {
+ httpBody = body->toEncodedFormData();
+ updateContentTypeAndCharset(
+ "application/x-www-form-urlencoded;charset=UTF-8", "UTF-8");
+ }
+
+ createRequest(std::move(httpBody), exceptionState);
+}
+
void XMLHttpRequest::send(DOMArrayBuffer* body,
ExceptionState& exceptionState) {
NETWORK_DVLOG(1) << this << " send() ArrayBuffer " << body;
@@ -1443,6 +1462,20 @@ AtomicString XMLHttpRequest::finalResponseMIMETypeWithFallback() const {
return AtomicString("text/xml");
}
+void XMLHttpRequest::updateContentTypeAndCharset(
+ const AtomicString& defaultContentType,
+ const String& charset) {
+ // http://xhr.spec.whatwg.org/#the-send()-method step 4's concilliation of
+ // "charset=" in any author-provided Content-Type: request header.
+ String contentType = getRequestHeader(HTTPNames::Content_Type);
+ if (contentType.isEmpty()) {
+ setRequestHeaderInternal(HTTPNames::Content_Type, defaultContentType);
+ return;
+ }
+ replaceCharsetInMediaType(contentType, charset);
+ m_requestHeaders.set(HTTPNames::Content_Type, AtomicString(contentType));
+}
+
bool XMLHttpRequest::responseIsXML() const {
return DOMImplementation::isXMLMIMEType(finalResponseMIMETypeWithFallback());
}

Powered by Google App Engine
This is Rietveld 408576698