2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-02-25 08:58:18 +03:00
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
#include "FormData.h"
|
2010-02-25 08:58:18 +03:00
|
|
|
#include "nsIInputStream.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2016-10-07 00:23:08 +03:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2013-06-19 18:24:37 +04:00
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2015-02-21 22:54:44 +03:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
#include "MultipartBlobImpl.h"
|
Bug 1127150 - Use File's name instead of explicit file name in form submission related classes. r=baku
Our nsFormSubmission and subclasses accepted a third filename argument to
explicitly specify the filename. Since switching from nsIDOMBlob to File in Bug
1085283, we can read out the filename directly from the File. This simplifies
the code, but introduces a change in the way Firefox submits form data to
servers.
Consider the code:
var fd = new FormData();
fd.append("blob1", new Blob(["hi"]), ""); // explicit empty filename as third arg
fd.append("file1", new File(["hi"], "")); // File's name is empty, no third arg.
xhr.send(fd);
Previously, the request body had filename="" in the first case, and filename="blob" in the second.
This patch will change it to both cases result in filename=""
This behaviour isn't exactly specced anywhere, nor in the HTML spec [1], nor in
RFC 2388. In addition Blink (at least Chromium v40) has the same behaviour
introduced by this patch. So shipping it seems ok to me.
[1]: http://www.w3.org/html/wg/drafts/html/master/semantics.html#multipart/form-data-encoding-algorithm
--HG--
extra : rebase_source : 6631e6900fe1a9b991c397b76e5be6b913715c5a
2015-02-21 22:54:44 +03:00
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::FormData(nsISupports* aOwner)
|
2018-07-22 12:11:27 +03:00
|
|
|
: HTMLFormSubmission(nullptr, EmptyString(), UTF_8_ENCODING, nullptr)
|
2012-12-11 22:09:56 +04:00
|
|
|
, mOwner(aOwner)
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Bug 1127150 - Use File's name instead of explicit file name in form submission related classes. r=baku
Our nsFormSubmission and subclasses accepted a third filename argument to
explicitly specify the filename. Since switching from nsIDOMBlob to File in Bug
1085283, we can read out the filename directly from the File. This simplifies
the code, but introduces a change in the way Firefox submits form data to
servers.
Consider the code:
var fd = new FormData();
fd.append("blob1", new Blob(["hi"]), ""); // explicit empty filename as third arg
fd.append("file1", new File(["hi"], "")); // File's name is empty, no third arg.
xhr.send(fd);
Previously, the request body had filename="" in the first case, and filename="blob" in the second.
This patch will change it to both cases result in filename=""
This behaviour isn't exactly specced anywhere, nor in the HTML spec [1], nor in
RFC 2388. In addition Blink (at least Chromium v40) has the same behaviour
introduced by this patch. So shipping it seems ok to me.
[1]: http://www.w3.org/html/wg/drafts/html/master/semantics.html#multipart/form-data-encoding-algorithm
--HG--
extra : rebase_source : 6631e6900fe1a9b991c397b76e5be6b913715c5a
2015-02-21 22:54:44 +03:00
|
|
|
namespace {
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
already_AddRefed<File>
|
|
|
|
GetOrCreateFileCalledBlob(Blob& aBlob, ErrorResult& aRv)
|
Bug 1127150 - Use File's name instead of explicit file name in form submission related classes. r=baku
Our nsFormSubmission and subclasses accepted a third filename argument to
explicitly specify the filename. Since switching from nsIDOMBlob to File in Bug
1085283, we can read out the filename directly from the File. This simplifies
the code, but introduces a change in the way Firefox submits form data to
servers.
Consider the code:
var fd = new FormData();
fd.append("blob1", new Blob(["hi"]), ""); // explicit empty filename as third arg
fd.append("file1", new File(["hi"], "")); // File's name is empty, no third arg.
xhr.send(fd);
Previously, the request body had filename="" in the first case, and filename="blob" in the second.
This patch will change it to both cases result in filename=""
This behaviour isn't exactly specced anywhere, nor in the HTML spec [1], nor in
RFC 2388. In addition Blink (at least Chromium v40) has the same behaviour
introduced by this patch. So shipping it seems ok to me.
[1]: http://www.w3.org/html/wg/drafts/html/master/semantics.html#multipart/form-data-encoding-algorithm
--HG--
extra : rebase_source : 6631e6900fe1a9b991c397b76e5be6b913715c5a
2015-02-21 22:54:44 +03:00
|
|
|
{
|
2016-02-17 17:56:19 +03:00
|
|
|
// If this is file, we can just use it
|
|
|
|
RefPtr<File> file = aBlob.ToFile();
|
|
|
|
if (file) {
|
|
|
|
return file.forget();
|
Bug 1127150 - Use File's name instead of explicit file name in form submission related classes. r=baku
Our nsFormSubmission and subclasses accepted a third filename argument to
explicitly specify the filename. Since switching from nsIDOMBlob to File in Bug
1085283, we can read out the filename directly from the File. This simplifies
the code, but introduces a change in the way Firefox submits form data to
servers.
Consider the code:
var fd = new FormData();
fd.append("blob1", new Blob(["hi"]), ""); // explicit empty filename as third arg
fd.append("file1", new File(["hi"], "")); // File's name is empty, no third arg.
xhr.send(fd);
Previously, the request body had filename="" in the first case, and filename="blob" in the second.
This patch will change it to both cases result in filename=""
This behaviour isn't exactly specced anywhere, nor in the HTML spec [1], nor in
RFC 2388. In addition Blink (at least Chromium v40) has the same behaviour
introduced by this patch. So shipping it seems ok to me.
[1]: http://www.w3.org/html/wg/drafts/html/master/semantics.html#multipart/form-data-encoding-algorithm
--HG--
extra : rebase_source : 6631e6900fe1a9b991c397b76e5be6b913715c5a
2015-02-21 22:54:44 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
// Forcing 'blob' as filename
|
|
|
|
file = aBlob.ToFile(NS_LITERAL_STRING("blob"), aRv);
|
2015-12-05 00:15:46 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.forget();
|
2015-05-12 00:54:02 +03:00
|
|
|
}
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
already_AddRefed<File>
|
|
|
|
GetBlobForFormDataStorage(Blob& aBlob, const Optional<nsAString>& aFilename,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Forcing a filename
|
|
|
|
if (aFilename.WasPassed()) {
|
|
|
|
RefPtr<File> file = aBlob.ToFile(aFilename.Value(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetOrCreateFileCalledBlob(aBlob, aRv);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
Bug 1127150 - Use File's name instead of explicit file name in form submission related classes. r=baku
Our nsFormSubmission and subclasses accepted a third filename argument to
explicitly specify the filename. Since switching from nsIDOMBlob to File in Bug
1085283, we can read out the filename directly from the File. This simplifies
the code, but introduces a change in the way Firefox submits form data to
servers.
Consider the code:
var fd = new FormData();
fd.append("blob1", new Blob(["hi"]), ""); // explicit empty filename as third arg
fd.append("file1", new File(["hi"], "")); // File's name is empty, no third arg.
xhr.send(fd);
Previously, the request body had filename="" in the first case, and filename="blob" in the second.
This patch will change it to both cases result in filename=""
This behaviour isn't exactly specced anywhere, nor in the HTML spec [1], nor in
RFC 2388. In addition Blink (at least Chromium v40) has the same behaviour
introduced by this patch. So shipping it seems ok to me.
[1]: http://www.w3.org/html/wg/drafts/html/master/semantics.html#multipart/form-data-encoding-algorithm
--HG--
extra : rebase_source : 6631e6900fe1a9b991c397b76e5be6b913715c5a
2015-02-21 22:54:44 +03:00
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// nsISupports
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(FormData)
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(FormData)
|
2014-10-08 20:15:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
|
|
|
|
|
|
|
|
for (uint32_t i = 0, len = tmp->mFormData.Length(); i < len; ++i) {
|
2015-10-20 05:06:00 +03:00
|
|
|
ImplCycleCollectionUnlink(tmp->mFormData[i].value);
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(FormData)
|
2014-10-08 20:15:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
|
|
|
|
|
|
|
for (uint32_t i = 0, len = tmp->mFormData.Length(); i < len; ++i) {
|
2015-10-20 05:06:00 +03:00
|
|
|
ImplCycleCollectionTraverse(cb, tmp->mFormData[i].value,
|
2016-01-20 20:25:03 +03:00
|
|
|
"mFormData[i].GetAsBlob()", 0);
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(FormData)
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(FormData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(FormData)
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FormData)
|
2012-12-11 22:09:56 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2018-02-22 22:42:12 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2010-02-25 08:58:18 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
2016-06-16 10:24:16 +03:00
|
|
|
// HTMLFormSubmission
|
2010-02-25 08:58:18 +03:00
|
|
|
nsresult
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::GetEncodedSubmission(nsIURI* aURI,
|
2017-09-22 09:12:03 +03:00
|
|
|
nsIInputStream** aPostDataStream,
|
2018-02-26 22:43:45 +03:00
|
|
|
nsCOMPtr<nsIURI>& aOutURI)
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Shouldn't call FormData::GetEncodedSubmission");
|
2010-02-25 08:58:18 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Append(const nsAString& aName, const nsAString& aValue,
|
|
|
|
ErrorResult& aRv)
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
2013-03-03 22:30:13 +04:00
|
|
|
AddNameValuePair(aName, aValue);
|
2010-02-25 08:58:18 +03:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Append(const nsAString& aName, Blob& aBlob,
|
|
|
|
const Optional<nsAString>& aFilename,
|
2016-01-20 20:25:03 +03:00
|
|
|
ErrorResult& aRv)
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
2016-02-17 17:56:19 +03:00
|
|
|
RefPtr<File> file = GetBlobForFormDataStorage(aBlob, aFilename, aRv);
|
2015-12-05 00:15:46 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
AddNameBlobOrNullPair(aName, file);
|
2015-01-29 04:04:28 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
void
|
|
|
|
FormData::Append(const nsAString& aName, Directory* aDirectory)
|
|
|
|
{
|
|
|
|
AddNameDirectoryPair(aName, aDirectory);
|
|
|
|
}
|
|
|
|
|
2015-01-29 04:04:28 +03:00
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Delete(const nsAString& aName)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
// We have to use this slightly awkward for loop since uint32_t >= 0 is an
|
|
|
|
// error for being always true.
|
|
|
|
for (uint32_t i = mFormData.Length(); i-- > 0; ) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
mFormData.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Get(const nsAString& aName,
|
2016-07-14 10:01:31 +03:00
|
|
|
Nullable<OwningBlobOrDirectoryOrUSVString>& aOutValue)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
2015-10-20 05:06:00 +03:00
|
|
|
aOutValue.SetValue() = mFormData[i].value;
|
2015-01-29 04:04:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aOutValue.SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::GetAll(const nsAString& aName,
|
2016-07-14 10:01:31 +03:00
|
|
|
nsTArray<OwningBlobOrDirectoryOrUSVString>& aValues)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
2016-07-14 10:01:31 +03:00
|
|
|
OwningBlobOrDirectoryOrUSVString* element = aValues.AppendElement();
|
2015-10-20 05:06:00 +03:00
|
|
|
*element = mFormData[i].value;
|
2015-01-29 04:04:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Has(const nsAString& aName)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
nsresult
|
2016-02-24 02:01:01 +03:00
|
|
|
FormData::AddNameBlobOrNullPair(const nsAString& aName, Blob* aBlob)
|
2015-05-12 15:09:51 +03:00
|
|
|
{
|
2016-02-24 02:01:01 +03:00
|
|
|
RefPtr<File> file;
|
|
|
|
|
|
|
|
if (!aBlob) {
|
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
|
|
|
SetNameValuePair(data, aName, EmptyString(), true /* aWasNullBlob */);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-08 11:35:30 +03:00
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
ErrorResult rv;
|
2016-02-24 02:01:01 +03:00
|
|
|
file = GetOrCreateFileCalledBlob(*aBlob, rv);
|
2016-02-17 17:56:19 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
2016-02-17 17:56:19 +03:00
|
|
|
SetNameFilePair(data, aName, file);
|
2015-05-12 15:09:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
nsresult
|
|
|
|
FormData::AddNameDirectoryPair(const nsAString& aName, Directory* aDirectory)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDirectory);
|
|
|
|
|
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
|
|
|
SetNameDirectoryPair(data, aName, aDirectory);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::FormDataTuple*
|
|
|
|
FormData::RemoveAllOthersAndGetFirstFormDataTuple(const nsAString& aName)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
FormDataTuple* lastFoundTuple = nullptr;
|
|
|
|
uint32_t lastFoundIndex = mFormData.Length();
|
|
|
|
// We have to use this slightly awkward for loop since uint32_t >= 0 is an
|
|
|
|
// error for being always true.
|
|
|
|
for (uint32_t i = mFormData.Length(); i-- > 0; ) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
if (lastFoundTuple) {
|
|
|
|
// The one we found earlier was not the first one, we can remove it.
|
|
|
|
mFormData.RemoveElementAt(lastFoundIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastFoundTuple = &mFormData[i];
|
|
|
|
lastFoundIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastFoundTuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Set(const nsAString& aName, Blob& aBlob,
|
|
|
|
const Optional<nsAString>& aFilename,
|
|
|
|
ErrorResult& aRv)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
FormDataTuple* tuple = RemoveAllOthersAndGetFirstFormDataTuple(aName);
|
|
|
|
if (tuple) {
|
2016-02-17 17:56:19 +03:00
|
|
|
RefPtr<File> file = GetBlobForFormDataStorage(aBlob, aFilename, aRv);
|
2015-12-05 00:15:46 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
SetNameFilePair(tuple, aName, file);
|
2015-01-29 04:04:28 +03:00
|
|
|
} else {
|
2015-12-05 00:15:46 +03:00
|
|
|
Append(aName, aBlob, aFilename, aRv);
|
2015-01-29 04:04:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::Set(const nsAString& aName, const nsAString& aValue,
|
|
|
|
ErrorResult& aRv)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
FormDataTuple* tuple = RemoveAllOthersAndGetFirstFormDataTuple(aName);
|
|
|
|
if (tuple) {
|
|
|
|
SetNameValuePair(tuple, aName, aValue);
|
|
|
|
} else {
|
2015-12-05 00:15:46 +03:00
|
|
|
Append(aName, aValue, aRv);
|
2015-01-29 04:04:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 05:06:00 +03:00
|
|
|
uint32_t
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::GetIterableLength() const
|
2015-10-20 05:06:00 +03:00
|
|
|
{
|
|
|
|
return mFormData.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAString&
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::GetKeyAtIndex(uint32_t aIndex) const
|
2015-10-20 05:06:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIndex < mFormData.Length());
|
|
|
|
return mFormData[aIndex].name;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
const OwningBlobOrDirectoryOrUSVString&
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::GetValueAtIndex(uint32_t aIndex) const
|
2015-10-20 05:06:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIndex < mFormData.Length());
|
|
|
|
return mFormData[aIndex].value;
|
|
|
|
}
|
|
|
|
|
2016-01-07 12:54:00 +03:00
|
|
|
void
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::SetNameValuePair(FormDataTuple* aData,
|
|
|
|
const nsAString& aName,
|
2016-02-24 02:01:01 +03:00
|
|
|
const nsAString& aValue,
|
|
|
|
bool aWasNullBlob)
|
2016-01-07 12:54:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
aData->name = aName;
|
2016-02-24 02:01:01 +03:00
|
|
|
aData->wasNullBlob = aWasNullBlob;
|
2016-01-07 12:54:00 +03:00
|
|
|
aData->value.SetAsUSVString() = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-17 17:56:19 +03:00
|
|
|
FormData::SetNameFilePair(FormDataTuple* aData,
|
2016-01-07 22:30:36 +03:00
|
|
|
const nsAString& aName,
|
2016-02-17 17:56:19 +03:00
|
|
|
File* aFile)
|
2016-01-07 12:54:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
2016-02-17 17:56:19 +03:00
|
|
|
MOZ_ASSERT(aFile);
|
2016-01-08 11:35:30 +03:00
|
|
|
|
2016-01-07 12:54:00 +03:00
|
|
|
aData->name = aName;
|
2016-02-24 02:01:01 +03:00
|
|
|
aData->wasNullBlob = false;
|
2016-02-17 17:56:19 +03:00
|
|
|
aData->value.SetAsBlob() = aFile;
|
2016-01-07 12:54:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
void
|
|
|
|
FormData::SetNameDirectoryPair(FormDataTuple* aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
Directory* aDirectory)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
MOZ_ASSERT(aDirectory);
|
|
|
|
|
|
|
|
aData->name = aName;
|
|
|
|
aData->wasNullBlob = false;
|
|
|
|
aData->value.SetAsDirectory() = aDirectory;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
/* virtual */ JSObject*
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2012-12-11 22:09:56 +04:00
|
|
|
{
|
2018-06-26 00:20:54 +03:00
|
|
|
return FormData_Binding::Wrap(aCx, this, aGivenProto);
|
2012-12-11 22:09:56 +04:00
|
|
|
}
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
/* static */ already_AddRefed<FormData>
|
|
|
|
FormData::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Optional<NonNull<HTMLFormElement> >& aFormElement,
|
|
|
|
ErrorResult& aRv)
|
2012-12-11 22:09:56 +04:00
|
|
|
{
|
2016-01-07 22:30:36 +03:00
|
|
|
RefPtr<FormData> formData = new FormData(aGlobal.GetAsSupports());
|
2012-12-11 22:09:56 +04:00
|
|
|
if (aFormElement.WasPassed()) {
|
2013-06-19 22:48:43 +04:00
|
|
|
aRv = aFormElement.Value().WalkFormElements(formData);
|
2012-12-11 22:09:56 +04:00
|
|
|
}
|
|
|
|
return formData.forget();
|
2010-02-25 08:58:18 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 04:32:12 +03:00
|
|
|
// contentTypeWithCharset can be set to the contentType or
|
|
|
|
// contentType+charset based on what the spec says.
|
|
|
|
// See: https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
|
|
|
nsresult
|
2016-01-07 22:30:36 +03:00
|
|
|
FormData::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
|
2017-09-20 04:32:12 +03:00
|
|
|
nsACString& aContentTypeWithCharset, nsACString& aCharset) const
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
2018-07-22 12:11:27 +03:00
|
|
|
FSMultipartFormData fs(nullptr, EmptyString(), UTF_8_ENCODING, nullptr);
|
2013-03-03 22:30:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
2016-02-24 02:01:01 +03:00
|
|
|
if (mFormData[i].wasNullBlob) {
|
|
|
|
MOZ_ASSERT(mFormData[i].value.IsUSVString());
|
|
|
|
fs.AddNameBlobOrNullPair(mFormData[i].name, nullptr);
|
2015-10-20 05:06:00 +03:00
|
|
|
} else if (mFormData[i].value.IsUSVString()) {
|
|
|
|
fs.AddNameValuePair(mFormData[i].name,
|
|
|
|
mFormData[i].value.GetAsUSVString());
|
2016-07-14 10:01:31 +03:00
|
|
|
} else if (mFormData[i].value.IsBlob()) {
|
2016-02-24 02:01:01 +03:00
|
|
|
fs.AddNameBlobOrNullPair(mFormData[i].name,
|
|
|
|
mFormData[i].value.GetAsBlob());
|
2016-07-14 10:01:31 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mFormData[i].value.IsDirectory());
|
|
|
|
fs.AddNameDirectoryPair(mFormData[i].name,
|
|
|
|
mFormData[i].value.GetAsDirectory());
|
2010-02-25 08:58:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:13:31 +03:00
|
|
|
fs.GetContentType(aContentTypeWithCharset);
|
2010-02-25 08:58:18 +03:00
|
|
|
aCharset.Truncate();
|
2012-09-20 02:15:32 +04:00
|
|
|
*aContentLength = 0;
|
|
|
|
NS_ADDREF(*aBody = fs.GetSubmissionBody(aContentLength));
|
2010-02-25 08:58:18 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|