2002-02-16 04:19:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
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/. */
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-05-26 16:49:38 +04:00
|
|
|
#include "nsFormSubmission.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsILinkHandler.h"
|
|
|
|
#include "nsIDocument.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsIFormControl.h"
|
|
|
|
#include "nsIDOMHTMLFormElement.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2005-01-12 22:45:38 +03:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2012-09-30 20:40:24 +04:00
|
|
|
#include "nsAttrValueInlines.h"
|
2002-02-27 18:49:54 +03:00
|
|
|
#include "nsISaveAsCharset.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsIFile.h"
|
2010-09-05 22:00:05 +04:00
|
|
|
#include "nsIDOMFile.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsStringStream.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsIURI.h"
|
2007-09-06 08:29:17 +04:00
|
|
|
#include "nsIURL.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsLinebreakConverter.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsIMultiplexInputStream.h"
|
|
|
|
#include "nsIMIMEInputStream.h"
|
2010-02-25 08:58:16 +03:00
|
|
|
#include "nsIMIMEService.h"
|
2002-08-06 08:26:35 +04:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIStringBundle.h"
|
2010-02-25 08:58:16 +03:00
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIFileStreams.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2012-11-08 03:04:22 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2015-01-29 04:04:28 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2012-11-08 03:04:22 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2012-11-08 03:04:22 +04:00
|
|
|
using mozilla::dom::EncodingUtils;
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
static void
|
|
|
|
SendJSWarning(nsIDocument* aDocument,
|
2004-12-17 23:40:48 +03:00
|
|
|
const char* aWarningName,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t** aWarningArgs, uint32_t aWarningArgsLen)
|
2010-02-25 08:58:17 +03:00
|
|
|
{
|
2011-12-15 18:47:03 +04:00
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
2013-08-21 23:28:26 +04:00
|
|
|
NS_LITERAL_CSTRING("HTML"), aDocument,
|
2011-12-15 18:47:03 +04:00
|
|
|
nsContentUtils::eFORMS_PROPERTIES,
|
2010-02-25 08:58:17 +03:00
|
|
|
aWarningName,
|
2011-12-15 18:47:03 +04:00
|
|
|
aWarningArgs, aWarningArgsLen);
|
2010-02-25 08:58:17 +03:00
|
|
|
}
|
2002-08-06 08:26:35 +04:00
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
2002-08-06 08:26:35 +04:00
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
class nsFSURLEncoded : public nsEncodingFormSubmission
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
public:
|
2002-06-12 11:39:08 +04:00
|
|
|
/**
|
|
|
|
* @param aCharset the charset of the form as a string
|
|
|
|
* @param aMethod the method of the submit (either NS_FORM_METHOD_GET or
|
|
|
|
* NS_FORM_METHOD_POST).
|
|
|
|
*/
|
2003-06-11 22:16:03 +04:00
|
|
|
nsFSURLEncoded(const nsACString& aCharset,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aMethod,
|
2010-08-20 01:58:20 +04:00
|
|
|
nsIDocument* aDocument,
|
|
|
|
nsIContent* aOriginatingElement)
|
|
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement),
|
2010-02-25 08:58:16 +03:00
|
|
|
mMethod(aMethod),
|
|
|
|
mDocument(aDocument),
|
2011-10-17 18:59:28 +04:00
|
|
|
mWarnedFileControl(false)
|
2004-01-10 02:54:21 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
2004-01-10 02:54:21 +03:00
|
|
|
const nsAString& aValue);
|
2010-02-25 08:58:16 +03:00
|
|
|
virtual nsresult AddNameFilePair(const nsAString& aName,
|
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
|
|
|
File* aBlob);
|
2010-02-25 08:58:17 +03:00
|
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool SupportsIsindexSubmission()
|
2010-03-11 15:29:51 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-03-11 15:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsresult AddIsindex(const nsAString& aValue);
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
protected:
|
|
|
|
|
2002-06-12 11:39:08 +04:00
|
|
|
/**
|
|
|
|
* URL encode a Unicode string by encoding it to bytes, converting linebreaks
|
|
|
|
* properly, and then escaping many bytes as %xx.
|
|
|
|
*
|
|
|
|
* @param aStr the string to encode
|
|
|
|
* @param aEncoded the encoded string [OUT]
|
2002-07-18 04:23:09 +04:00
|
|
|
* @throws NS_ERROR_OUT_OF_MEMORY if we run out of memory
|
2002-06-12 11:39:08 +04:00
|
|
|
*/
|
2002-07-18 04:23:09 +04:00
|
|
|
nsresult URLEncode(const nsAString& aStr, nsCString& aEncoded);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
private:
|
2002-06-12 11:39:08 +04:00
|
|
|
/**
|
|
|
|
* The method of the submit (either NS_FORM_METHOD_GET or
|
|
|
|
* NS_FORM_METHOD_POST).
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mMethod;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2002-06-12 11:39:08 +04:00
|
|
|
/** The query string so far (the part after the ?) */
|
2002-02-16 04:19:24 +03:00
|
|
|
nsCString mQueryString;
|
2002-08-06 08:26:35 +04:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
/** The document whose URI to use when reporting errors */
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
|
2002-08-06 08:26:35 +04:00
|
|
|
/** Whether or not we have warned about a file control not being submitted */
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWarnedFileControl;
|
2002-02-16 04:19:24 +03:00
|
|
|
};
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2010-02-25 08:58:16 +03:00
|
|
|
nsFSURLEncoded::AddNameValuePair(const nsAString& aName,
|
2002-02-16 04:19:24 +03:00
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
// Encode value
|
|
|
|
nsCString convValue;
|
2010-02-25 08:58:16 +03:00
|
|
|
nsresult rv = URLEncode(aValue, convValue);
|
2002-07-18 04:23:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2005-07-13 20:55:59 +04:00
|
|
|
// Encode name
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString convName;
|
2005-07-13 20:55:59 +04:00
|
|
|
rv = URLEncode(aName, convName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
// Append data to string
|
2002-06-12 11:39:08 +04:00
|
|
|
if (mQueryString.IsEmpty()) {
|
2002-02-16 04:19:24 +03:00
|
|
|
mQueryString += convName + NS_LITERAL_CSTRING("=") + convValue;
|
|
|
|
} else {
|
|
|
|
mQueryString += NS_LITERAL_CSTRING("&") + convName
|
|
|
|
+ NS_LITERAL_CSTRING("=") + convValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-11 15:29:51 +03:00
|
|
|
nsresult
|
|
|
|
nsFSURLEncoded::AddIsindex(const nsAString& aValue)
|
|
|
|
{
|
|
|
|
// Encode value
|
|
|
|
nsCString convValue;
|
|
|
|
nsresult rv = URLEncode(aValue, convValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Append data to string
|
|
|
|
if (mQueryString.IsEmpty()) {
|
|
|
|
mQueryString.Assign(convValue);
|
|
|
|
} else {
|
|
|
|
mQueryString += NS_LITERAL_CSTRING("&isindex=") + convValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2010-02-25 08:58:16 +03:00
|
|
|
nsFSURLEncoded::AddNameFilePair(const nsAString& aName,
|
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
|
|
|
File* aBlob)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2010-02-25 08:58:16 +03:00
|
|
|
if (!mWarnedFileControl) {
|
2012-07-30 18:20:58 +04:00
|
|
|
SendJSWarning(mDocument, "ForgotFileEnctypeWarning", nullptr, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
mWarnedFileControl = true;
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
nsAutoString filename;
|
2015-01-29 04:04:28 +03:00
|
|
|
if (aBlob && aBlob->IsFile()) {
|
|
|
|
aBlob->GetName(filename);
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return AddNameValuePair(aName, filename);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2002-08-04 09:03:30 +04:00
|
|
|
static void
|
|
|
|
HandleMailtoSubject(nsCString& aPath) {
|
|
|
|
|
|
|
|
// Walk through the string and see if we have a subject already.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasSubject = false;
|
|
|
|
bool hasParams = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t paramSep = aPath.FindChar('?');
|
|
|
|
while (paramSep != kNotFound && paramSep < (int32_t)aPath.Length()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
hasParams = true;
|
2002-08-04 09:03:30 +04:00
|
|
|
|
|
|
|
// Get the end of the name at the = op. If it is *after* the next &,
|
|
|
|
// assume that someone made a parameter without an = in it
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nameEnd = aPath.FindChar('=', paramSep+1);
|
|
|
|
int32_t nextParamSep = aPath.FindChar('&', paramSep+1);
|
2002-08-04 09:03:30 +04:00
|
|
|
if (nextParamSep == kNotFound) {
|
|
|
|
nextParamSep = aPath.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the = op is after the &, this parameter is a name without value.
|
|
|
|
// If there is no = op, same thing.
|
|
|
|
if (nameEnd == kNotFound || nextParamSep < nameEnd) {
|
|
|
|
nameEnd = nextParamSep;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nameEnd != kNotFound) {
|
2007-02-28 07:57:13 +03:00
|
|
|
if (Substring(aPath, paramSep+1, nameEnd-(paramSep+1)).
|
|
|
|
LowerCaseEqualsLiteral("subject")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
hasSubject = true;
|
2002-08-04 09:03:30 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
paramSep = nextParamSep;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no subject, append a preformed subject to the mailto line
|
|
|
|
if (!hasSubject) {
|
|
|
|
if (hasParams) {
|
|
|
|
aPath.Append('&');
|
|
|
|
} else {
|
|
|
|
aPath.Append('?');
|
|
|
|
}
|
|
|
|
|
2004-12-21 19:17:32 +03:00
|
|
|
// Get the default subject
|
2005-04-03 21:16:28 +04:00
|
|
|
nsXPIDLString brandName;
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::GetLocalizedString(nsContentUtils::eBRAND_PROPERTIES,
|
|
|
|
"brandShortName", brandName);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *formatStrings[] = { brandName.get() };
|
2004-12-21 19:17:32 +03:00
|
|
|
nsXPIDLString subjectStr;
|
2005-04-03 21:16:28 +04:00
|
|
|
rv = nsContentUtils::FormatLocalizedString(
|
|
|
|
nsContentUtils::eFORMS_PROPERTIES,
|
|
|
|
"DefaultFormSubject",
|
|
|
|
formatStrings,
|
|
|
|
subjectStr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2004-12-21 19:17:32 +03:00
|
|
|
aPath.AppendLiteral("subject=");
|
|
|
|
nsCString subjectStrEscaped;
|
2005-04-03 21:16:28 +04:00
|
|
|
aPath.Append(NS_EscapeURL(NS_ConvertUTF16toUTF8(subjectStr), esc_Query,
|
|
|
|
subjectStrEscaped));
|
2002-08-04 09:03:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsresult
|
2002-02-16 04:19:24 +03:00
|
|
|
nsFSURLEncoded::GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPostDataStream = nullptr;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
if (mMethod == NS_FORM_METHOD_POST) {
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMailto = false;
|
2002-08-04 09:03:30 +04:00
|
|
|
aURI->SchemeIs("mailto", &isMailto);
|
|
|
|
if (isMailto) {
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2002-08-04 09:03:30 +04:00
|
|
|
rv = aURI->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
HandleMailtoSubject(path);
|
|
|
|
|
|
|
|
// Append the body to and force-plain-text args to the mailto line
|
|
|
|
nsCString escapedBody;
|
|
|
|
escapedBody.Adopt(nsEscape(mQueryString.get(), url_XAlphas));
|
|
|
|
|
|
|
|
path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;
|
|
|
|
|
|
|
|
rv = aURI->SetPath(path);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> dataStream;
|
|
|
|
// XXX We *really* need to either get the string to disown its data (and
|
|
|
|
// not destroy it), or make a string input stream that owns the CString
|
|
|
|
// that is passed to it. Right now this operation does a copy.
|
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(dataStream), mQueryString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEInputStream> mimeStream(
|
|
|
|
do_CreateInstance("@mozilla.org/network/mime-input-stream;1", &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
#ifdef SPECIFY_CHARSET_IN_CONTENT_TYPE
|
2002-08-04 09:03:30 +04:00
|
|
|
mimeStream->AddHeader("Content-Type",
|
|
|
|
PromiseFlatString(
|
|
|
|
"application/x-www-form-urlencoded; charset="
|
|
|
|
+ mCharset
|
|
|
|
).get());
|
2002-02-16 04:19:24 +03:00
|
|
|
#else
|
2002-08-04 09:03:30 +04:00
|
|
|
mimeStream->AddHeader("Content-Type",
|
|
|
|
"application/x-www-form-urlencoded");
|
2002-02-16 04:19:24 +03:00
|
|
|
#endif
|
2011-10-17 18:59:28 +04:00
|
|
|
mimeStream->SetAddContentLength(true);
|
2002-08-04 09:03:30 +04:00
|
|
|
mimeStream->SetData(dataStream);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2002-08-04 09:03:30 +04:00
|
|
|
*aPostDataStream = mimeStream;
|
|
|
|
NS_ADDREF(*aPostDataStream);
|
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Get the full query string
|
2011-09-29 10:19:26 +04:00
|
|
|
bool schemeIsJavaScript;
|
2002-02-16 04:19:24 +03:00
|
|
|
rv = aURI->SchemeIs("javascript", &schemeIsJavaScript);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (schemeIsJavaScript) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-09-06 08:29:17 +04:00
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
if (url) {
|
|
|
|
url->SetQuery(mQueryString);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2007-09-06 08:29:17 +04:00
|
|
|
else {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2007-09-06 08:29:17 +04:00
|
|
|
rv = aURI->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Bug 42616: Trim off named anchor and save it to add later
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t namedAnchorPos = path.FindChar('#');
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString namedAnchor;
|
2007-09-06 08:29:17 +04:00
|
|
|
if (kNotFound != namedAnchorPos) {
|
|
|
|
path.Right(namedAnchor, (path.Length() - namedAnchorPos));
|
|
|
|
path.Truncate(namedAnchorPos);
|
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2007-09-06 08:29:17 +04:00
|
|
|
// Chop off old query string (bug 25330, 57333)
|
|
|
|
// Only do this for GET not POST (bug 41585)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t queryStart = path.FindChar('?');
|
2007-09-06 08:29:17 +04:00
|
|
|
if (kNotFound != queryStart) {
|
|
|
|
path.Truncate(queryStart);
|
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2007-09-06 08:29:17 +04:00
|
|
|
path.Append('?');
|
|
|
|
// Bug 42616: Add named anchor to end after query string
|
|
|
|
path.Append(mQueryString + namedAnchor);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2007-09-06 08:29:17 +04:00
|
|
|
aURI->SetPath(path);
|
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// i18n helper routines
|
2002-07-18 04:23:09 +04:00
|
|
|
nsresult
|
2002-06-12 11:39:08 +04:00
|
|
|
nsFSURLEncoded::URLEncode(const nsAString& aStr, nsCString& aEncoded)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
// convert to CRLF breaks
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* convertedBuf =
|
2005-07-13 20:55:59 +04:00
|
|
|
nsLinebreakConverter::ConvertUnicharLineBreaks(PromiseFlatString(aStr).get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakNet);
|
|
|
|
NS_ENSURE_TRUE(convertedBuf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encodedBuf;
|
2010-11-23 11:50:55 +03:00
|
|
|
nsresult rv = EncodeVal(nsDependentString(convertedBuf), encodedBuf, false);
|
2015-03-27 03:01:12 +03:00
|
|
|
free(convertedBuf);
|
2005-07-13 20:55:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2005-07-13 20:55:59 +04:00
|
|
|
char* escapedBuf = nsEscape(encodedBuf.get(), url_XPAlphas);
|
|
|
|
NS_ENSURE_TRUE(escapedBuf, NS_ERROR_OUT_OF_MEMORY);
|
2002-02-16 04:19:24 +03:00
|
|
|
aEncoded.Adopt(escapedBuf);
|
2002-07-18 04:23:09 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-08-20 01:58:20 +04:00
|
|
|
nsFSMultipartFormData::nsFSMultipartFormData(const nsACString& aCharset,
|
|
|
|
nsIContent* aOriginatingElement)
|
|
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2010-02-25 08:58:16 +03:00
|
|
|
mPostDataStream =
|
|
|
|
do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1");
|
2012-09-20 02:15:32 +04:00
|
|
|
mTotalLength = 0;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
mBoundary.AssignLiteral("---------------------------");
|
|
|
|
mBoundary.AppendInt(rand());
|
|
|
|
mBoundary.AppendInt(rand());
|
|
|
|
mBoundary.AppendInt(rand());
|
2002-07-18 04:23:09 +04:00
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
nsFSMultipartFormData::~nsFSMultipartFormData()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPostDataChunk.IsEmpty(), "Left unsubmitted data");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIInputStream*
|
2012-09-20 02:15:32 +04:00
|
|
|
nsFSMultipartFormData::GetSubmissionBody(uint64_t* aContentLength)
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
|
|
|
// Finish data
|
|
|
|
mPostDataChunk += NS_LITERAL_CSTRING("--") + mBoundary
|
|
|
|
+ NS_LITERAL_CSTRING("--" CRLF);
|
|
|
|
|
|
|
|
// Add final data input stream
|
|
|
|
AddPostDataStream();
|
|
|
|
|
2012-09-20 02:15:32 +04:00
|
|
|
*aContentLength = mTotalLength;
|
2010-02-25 08:58:18 +03:00
|
|
|
return mPostDataStream;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2010-02-25 08:58:16 +03:00
|
|
|
nsFSMultipartFormData::AddNameValuePair(const nsAString& aName,
|
2002-07-18 04:23:09 +04:00
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
nsCString valueStr;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encodedVal;
|
2010-11-23 11:50:55 +03:00
|
|
|
nsresult rv = EncodeVal(aValue, encodedVal, false);
|
2010-02-25 08:58:16 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
valueStr.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(encodedVal.get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakNet));
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString nameStr;
|
2010-11-23 11:50:55 +03:00
|
|
|
rv = EncodeVal(aName, nameStr, true);
|
2002-07-18 04:23:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
// Make MIME block for name/value pair
|
2010-02-25 08:58:17 +03:00
|
|
|
|
2005-07-13 20:55:59 +04:00
|
|
|
// XXX: name parameter should be encoded per RFC 2231
|
|
|
|
// RFC 2388 specifies that RFC 2047 be used, but I think it's not
|
|
|
|
// consistent with MIME standard.
|
2002-02-16 04:19:24 +03:00
|
|
|
mPostDataChunk += NS_LITERAL_CSTRING("--") + mBoundary
|
2007-10-25 02:21:01 +04:00
|
|
|
+ NS_LITERAL_CSTRING(CRLF)
|
|
|
|
+ NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
|
|
|
|
+ nameStr + NS_LITERAL_CSTRING("\"" CRLF CRLF)
|
2002-02-16 04:19:24 +03:00
|
|
|
+ valueStr + NS_LITERAL_CSTRING(CRLF);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2010-02-25 08:58:16 +03:00
|
|
|
nsFSMultipartFormData::AddNameFilePair(const nsAString& aName,
|
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
|
|
|
File* aBlob)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2010-02-25 08:58:16 +03:00
|
|
|
// Encode the control name
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString nameStr;
|
2010-11-23 11:50:55 +03:00
|
|
|
nsresult rv = EncodeVal(aName, nameStr, true);
|
2002-07-18 04:23:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-11-23 11:50:55 +03:00
|
|
|
nsCString filename, contentType;
|
2010-02-25 08:58:16 +03:00
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
2010-10-14 03:25:33 +04:00
|
|
|
if (aBlob) {
|
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
|
|
|
// Since Bug 1127150, any Blob received from FormData must be a File
|
|
|
|
// instance with a valid name (possibly "blob").
|
|
|
|
MOZ_ASSERT(aBlob->IsFile());
|
|
|
|
nsAutoString filename16;
|
|
|
|
rv = aBlob->GetName(filename16);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-06-29 22:03:36 +04: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
|
|
|
nsAutoString filepath16;
|
|
|
|
rv = aBlob->GetPath(filepath16);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-03-03 22:30:13 +04: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
|
|
|
if (!filepath16.IsEmpty()) {
|
|
|
|
// File.path includes trailing "/"
|
|
|
|
filename16 = filepath16 + filename16;
|
2011-06-29 22:03:36 +04: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
|
|
|
rv = EncodeVal(filename16, filename, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
// Get content type
|
2010-11-23 11:50:55 +03:00
|
|
|
nsAutoString contentType16;
|
|
|
|
rv = aBlob->GetType(contentType16);
|
|
|
|
if (NS_FAILED(rv) || contentType16.IsEmpty()) {
|
|
|
|
contentType16.AssignLiteral("application/octet-stream");
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
2010-11-23 11:50:55 +03:00
|
|
|
contentType.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(NS_ConvertUTF16toUTF8(contentType16).get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakSpace));
|
2013-03-03 22:30:13 +04:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
// Get input stream
|
2010-10-14 03:25:33 +04:00
|
|
|
rv = aBlob->GetInternalStream(getter_AddRefs(fileStream));
|
2010-09-05 22:00:05 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-02-25 08:58:16 +03:00
|
|
|
if (fileStream) {
|
|
|
|
// Create buffered stream (for efficiency)
|
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
|
|
|
fileStream, 8192);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-03-03 22:30:13 +04:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
fileStream = bufferedStream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
contentType.AssignLiteral("application/octet-stream");
|
|
|
|
}
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
//
|
|
|
|
// Make MIME block for name/value pair
|
|
|
|
//
|
|
|
|
// more appropriate than always using binary?
|
|
|
|
mPostDataChunk += NS_LITERAL_CSTRING("--") + mBoundary
|
|
|
|
+ NS_LITERAL_CSTRING(CRLF);
|
2005-07-13 20:55:59 +04:00
|
|
|
// XXX: name/filename parameter should be encoded per RFC 2231
|
|
|
|
// RFC 2388 specifies that RFC 2047 be used, but I think it's not
|
|
|
|
// consistent with the MIME standard.
|
2002-02-16 04:19:24 +03:00
|
|
|
mPostDataChunk +=
|
|
|
|
NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
|
|
|
|
+ nameStr + NS_LITERAL_CSTRING("\"; filename=\"")
|
2010-11-23 11:50:55 +03:00
|
|
|
+ filename + NS_LITERAL_CSTRING("\"" CRLF)
|
|
|
|
+ NS_LITERAL_CSTRING("Content-Type: ")
|
|
|
|
+ contentType + NS_LITERAL_CSTRING(CRLF CRLF);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2013-03-16 01:57:35 +04:00
|
|
|
// We should not try to append an invalid stream. That will happen for example
|
|
|
|
// if we try to update a file that actually do not exist.
|
|
|
|
uint64_t size;
|
|
|
|
if (fileStream && NS_SUCCEEDED(aBlob->GetSize(&size))) {
|
2002-04-19 02:02:09 +04:00
|
|
|
// We need to dump the data up to this point into the POST data stream here,
|
|
|
|
// since we're about to add the file input stream
|
|
|
|
AddPostDataStream();
|
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
mPostDataStream->AppendStream(fileStream);
|
2012-09-20 02:15:32 +04:00
|
|
|
mTotalLength += size;
|
2002-04-07 04:17:56 +04:00
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
// CRLF after file
|
2004-06-17 04:13:25 +04:00
|
|
|
mPostDataChunk.AppendLiteral(CRLF);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsresult
|
2002-02-16 04:19:24 +03:00
|
|
|
nsFSMultipartFormData::GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Make header
|
|
|
|
nsCOMPtr<nsIMIMEInputStream> mimeStream
|
|
|
|
= do_CreateInstance("@mozilla.org/network/mime-input-stream;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contentType;
|
2010-02-25 08:58:18 +03:00
|
|
|
GetContentType(contentType);
|
|
|
|
mimeStream->AddHeader("Content-Type", contentType.get());
|
2011-10-17 18:59:28 +04:00
|
|
|
mimeStream->SetAddContentLength(true);
|
2012-09-20 02:15:32 +04:00
|
|
|
uint64_t unused;
|
|
|
|
mimeStream->SetData(GetSubmissionBody(&unused));
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-03-15 23:00:15 +04:00
|
|
|
mimeStream.forget(aPostDataStream);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFSMultipartFormData::AddPostDataStream()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> postDataChunkStream;
|
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(postDataChunkStream),
|
|
|
|
mPostDataChunk);
|
|
|
|
NS_ASSERTION(postDataChunkStream, "Could not open a stream for POST!");
|
|
|
|
if (postDataChunkStream) {
|
|
|
|
mPostDataStream->AppendStream(postDataChunkStream);
|
2012-09-20 02:15:32 +04:00
|
|
|
mTotalLength += mPostDataChunk.Length();
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mPostDataChunk.Truncate();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
class nsFSTextPlain : public nsEncodingFormSubmission
|
2002-07-12 03:32:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-08-20 01:58:20 +04:00
|
|
|
nsFSTextPlain(const nsACString& aCharset, nsIContent* aOriginatingElement)
|
|
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement)
|
2004-01-10 02:54:21 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
2004-01-10 02:54:21 +03:00
|
|
|
const nsAString& aValue);
|
2010-02-25 08:58:16 +03:00
|
|
|
virtual nsresult AddNameFilePair(const nsAString& aName,
|
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
|
|
|
File* aBlob);
|
2010-02-25 08:58:17 +03:00
|
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream);
|
2002-07-12 03:32:13 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mBody;
|
|
|
|
};
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2010-02-25 08:58:16 +03:00
|
|
|
nsFSTextPlain::AddNameValuePair(const nsAString& aName,
|
2002-07-12 03:32:13 +04:00
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
// XXX This won't work well with a name like "a=b" or "a\nb" but I suppose
|
|
|
|
// text/plain doesn't care about that. Parsers aren't built for escaped
|
|
|
|
// values so we'll have to live with it.
|
2010-02-25 08:58:16 +03:00
|
|
|
mBody.Append(aName + NS_LITERAL_STRING("=") + aValue +
|
|
|
|
NS_LITERAL_STRING(CRLF));
|
2002-07-12 03:32:13 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2010-02-25 08:58:16 +03:00
|
|
|
nsFSTextPlain::AddNameFilePair(const nsAString& aName,
|
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
|
|
|
File* aBlob)
|
2002-07-12 03:32:13 +04:00
|
|
|
{
|
2010-02-25 08:58:16 +03:00
|
|
|
nsAutoString filename;
|
2015-01-29 04:04:28 +03:00
|
|
|
if (aBlob && aBlob->IsFile()) {
|
|
|
|
aBlob->GetName(filename);
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
2013-03-03 22:30:13 +04:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
AddNameValuePair(aName, filename);
|
2002-07-12 03:32:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsresult
|
2002-07-12 03:32:13 +04:00
|
|
|
nsFSTextPlain::GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// XXX HACK We are using the standard URL mechanism to give the body to the
|
|
|
|
// mailer instead of passing the post data stream to it, since that sounds
|
|
|
|
// hard.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMailto = false;
|
2002-07-12 03:32:13 +04:00
|
|
|
aURI->SchemeIs("mailto", &isMailto);
|
|
|
|
if (isMailto) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2002-07-12 03:32:13 +04:00
|
|
|
rv = aURI->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-08-04 09:03:30 +04:00
|
|
|
HandleMailtoSubject(path);
|
2002-07-12 03:32:13 +04:00
|
|
|
|
|
|
|
// Append the body to and force-plain-text args to the mailto line
|
2005-07-13 20:55:59 +04:00
|
|
|
char* escapedBuf = nsEscape(NS_ConvertUTF16toUTF8(mBody).get(),
|
|
|
|
url_XAlphas);
|
|
|
|
NS_ENSURE_TRUE(escapedBuf, NS_ERROR_OUT_OF_MEMORY);
|
2002-07-12 03:32:13 +04:00
|
|
|
nsCString escapedBody;
|
2005-07-13 20:55:59 +04:00
|
|
|
escapedBody.Adopt(escapedBuf);
|
2002-07-12 03:32:13 +04:00
|
|
|
|
|
|
|
path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;
|
|
|
|
|
|
|
|
rv = aURI->SetPath(path);
|
|
|
|
|
|
|
|
} else {
|
2010-11-23 11:50:55 +03:00
|
|
|
// Create data stream.
|
|
|
|
// We do want to send the data through the charset encoder and we want to
|
|
|
|
// normalize linebreaks to use the "standard net" format (\r\n), but we
|
|
|
|
// don't want to perform any other encoding. This means that names and
|
|
|
|
// values which contains '=' or newlines are potentially ambigiously
|
|
|
|
// encoded, but that how text/plain is specced.
|
|
|
|
nsCString cbody;
|
|
|
|
EncodeVal(mBody, cbody, false);
|
|
|
|
cbody.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(cbody.get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakNet));
|
2002-07-12 03:32:13 +04:00
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
2010-11-23 11:50:55 +03:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(bodyStream), cbody);
|
2002-07-12 03:32:13 +04:00
|
|
|
if (!bodyStream) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create mime stream with headers and such
|
|
|
|
nsCOMPtr<nsIMIMEInputStream> mimeStream
|
|
|
|
= do_CreateInstance("@mozilla.org/network/mime-input-stream;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mimeStream->AddHeader("Content-Type", "text/plain");
|
2011-10-17 18:59:28 +04:00
|
|
|
mimeStream->SetAddContentLength(true);
|
2002-07-12 03:32:13 +04:00
|
|
|
mimeStream->SetData(bodyStream);
|
|
|
|
CallQueryInterface(mimeStream, aPostDataStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2010-08-20 01:58:20 +04:00
|
|
|
nsEncodingFormSubmission::nsEncodingFormSubmission(const nsACString& aCharset,
|
|
|
|
nsIContent* aOriginatingElement)
|
|
|
|
: nsFormSubmission(aCharset, aOriginatingElement)
|
2010-02-25 08:58:16 +03:00
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString charset(aCharset);
|
2010-02-25 08:58:17 +03:00
|
|
|
// canonical name is passed so that we just have to check against
|
|
|
|
// *our* canonical names listed in charsetaliases.properties
|
|
|
|
if (charset.EqualsLiteral("ISO-8859-1")) {
|
|
|
|
charset.AssignLiteral("windows-1252");
|
|
|
|
}
|
|
|
|
|
2012-01-02 18:18:30 +04:00
|
|
|
if (!(charset.EqualsLiteral("UTF-8") || charset.EqualsLiteral("gb18030"))) {
|
2012-03-02 09:19:30 +04:00
|
|
|
NS_ConvertUTF8toUTF16 charsetUtf16(charset);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* charsetPtr = charsetUtf16.get();
|
2012-01-02 18:18:30 +04:00
|
|
|
SendJSWarning(aOriginatingElement ? aOriginatingElement->GetOwnerDocument()
|
2012-07-30 18:20:58 +04:00
|
|
|
: nullptr,
|
2012-01-02 18:18:30 +04:00
|
|
|
"CannotEncodeAllUnicode",
|
|
|
|
&charsetPtr,
|
|
|
|
1);
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
mEncoder = do_CreateInstance(NS_SAVEASCHARSET_CONTRACTID);
|
|
|
|
if (mEncoder) {
|
|
|
|
nsresult rv =
|
|
|
|
mEncoder->Init(charset.get(),
|
|
|
|
(nsISaveAsCharset::attr_EntityAfterCharsetConv +
|
|
|
|
nsISaveAsCharset::attr_FallbackDecimalNCR),
|
|
|
|
0);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mEncoder = nullptr;
|
2010-02-25 08:58:17 +03:00
|
|
|
}
|
|
|
|
}
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
2002-07-12 03:32:13 +04:00
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsEncodingFormSubmission::~nsEncodingFormSubmission()
|
2002-08-06 08:26:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// i18n helper routines
|
|
|
|
nsresult
|
2010-11-23 11:50:55 +03:00
|
|
|
nsEncodingFormSubmission::EncodeVal(const nsAString& aStr, nsCString& aOut,
|
|
|
|
bool aHeaderEncode)
|
2010-02-25 08:58:17 +03:00
|
|
|
{
|
2010-11-23 11:50:55 +03:00
|
|
|
if (mEncoder && !aStr.IsEmpty()) {
|
2010-02-25 08:58:17 +03:00
|
|
|
aOut.Truncate();
|
2010-11-23 11:50:55 +03:00
|
|
|
nsresult rv = mEncoder->Convert(PromiseFlatString(aStr).get(),
|
|
|
|
getter_Copies(aOut));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-02-25 08:58:17 +03:00
|
|
|
}
|
2010-11-23 11:50:55 +03:00
|
|
|
else {
|
|
|
|
// fall back to UTF-8
|
|
|
|
CopyUTF16toUTF8(aStr, aOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHeaderEncode) {
|
|
|
|
aOut.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(aOut.get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakSpace));
|
|
|
|
aOut.ReplaceSubstring(NS_LITERAL_CSTRING("\""),
|
|
|
|
NS_LITERAL_CSTRING("\\\""));
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
2010-02-25 08:58:16 +03:00
|
|
|
GetSubmitCharset(nsGenericHTMLElement* aForm,
|
|
|
|
nsACString& oCharset)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
oCharset.AssignLiteral("UTF-8"); // default to utf-8
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
nsAutoString acceptCharsetValue;
|
2006-12-26 20:47:52 +03:00
|
|
|
aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::acceptcharset,
|
2005-01-25 03:02:58 +03:00
|
|
|
acceptCharsetValue);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t charsetLen = acceptCharsetValue.Length();
|
2002-02-16 04:19:24 +03:00
|
|
|
if (charsetLen > 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset=0;
|
|
|
|
int32_t spPos=0;
|
2002-02-16 04:19:24 +03:00
|
|
|
// get charset from charsets one by one
|
2012-03-05 07:57:51 +04:00
|
|
|
do {
|
2014-01-04 19:02:17 +04:00
|
|
|
spPos = acceptCharsetValue.FindChar(char16_t(' '), offset);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cnt = ((-1==spPos)?(charsetLen-offset):(spPos-offset));
|
2012-03-05 07:57:51 +04:00
|
|
|
if (cnt > 0) {
|
|
|
|
nsAutoString uCharset;
|
|
|
|
acceptCharsetValue.Mid(uCharset, offset, cnt);
|
|
|
|
|
2012-11-08 03:04:22 +04:00
|
|
|
if (EncodingUtils::FindEncodingForLabel(uCharset, oCharset))
|
2012-03-05 07:57:51 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
offset = spPos + 1;
|
|
|
|
} while (spPos != -1);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
// if there are no accept-charset or all the charset are not supported
|
|
|
|
// Get the charset from document
|
2014-08-23 00:11:27 +04:00
|
|
|
nsIDocument* doc = aForm->GetComposedDoc();
|
2002-08-06 08:26:35 +04:00
|
|
|
if (doc) {
|
2003-10-22 10:09:48 +04:00
|
|
|
oCharset = doc->GetDocumentCharacterSet();
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
static void
|
2010-02-25 08:58:16 +03:00
|
|
|
GetEnumAttr(nsGenericHTMLElement* aContent,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIAtom* atom, int32_t* aValue)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2005-01-25 03:02:58 +03:00
|
|
|
const nsAttrValue* value = aContent->GetParsedAttr(atom);
|
|
|
|
if (value && value->Type() == nsAttrValue::eEnum) {
|
|
|
|
*aValue = value->GetEnumValue();
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 20:55:59 +04:00
|
|
|
nsresult
|
2010-02-25 08:58:17 +03:00
|
|
|
GetSubmissionFromForm(nsGenericHTMLElement* aForm,
|
2010-08-20 21:47:30 +04:00
|
|
|
nsGenericHTMLElement* aOriginatingElement,
|
2010-02-25 08:58:17 +03:00
|
|
|
nsFormSubmission** aFormSubmission)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2010-02-25 08:58:17 +03:00
|
|
|
// Get all the information necessary to encode the form data
|
2014-10-02 23:07:24 +04:00
|
|
|
NS_ASSERTION(aForm->GetComposedDoc(),
|
2010-03-29 12:59:00 +04:00
|
|
|
"Should have doc if we're building submission!");
|
2010-02-25 08:58:17 +03:00
|
|
|
|
|
|
|
// Get encoding type (default: urlencoded)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t enctype = NS_FORM_ENCTYPE_URLENCODED;
|
2010-08-20 21:47:30 +04:00
|
|
|
if (aOriginatingElement &&
|
|
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formenctype)) {
|
|
|
|
GetEnumAttr(aOriginatingElement, nsGkAtoms::formenctype, &enctype);
|
|
|
|
} else {
|
|
|
|
GetEnumAttr(aForm, nsGkAtoms::enctype, &enctype);
|
|
|
|
}
|
2010-02-25 08:58:17 +03:00
|
|
|
|
|
|
|
// Get method (default: GET)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t method = NS_FORM_METHOD_GET;
|
2010-08-20 21:47:30 +04:00
|
|
|
if (aOriginatingElement &&
|
|
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formmethod)) {
|
|
|
|
GetEnumAttr(aOriginatingElement, nsGkAtoms::formmethod, &method);
|
|
|
|
} else {
|
|
|
|
GetEnumAttr(aForm, nsGkAtoms::method, &method);
|
|
|
|
}
|
2010-02-25 08:58:17 +03:00
|
|
|
|
|
|
|
// Get charset
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString charset;
|
2010-02-25 08:58:17 +03:00
|
|
|
GetSubmitCharset(aForm, charset);
|
2010-02-25 08:58:17 +03:00
|
|
|
|
2012-03-02 09:19:30 +04:00
|
|
|
// We now have a canonical charset name, so we only have to check it
|
|
|
|
// against canonical names.
|
|
|
|
|
|
|
|
// use UTF-8 for UTF-16* (per WHATWG and existing practice of
|
|
|
|
// MS IE/Opera).
|
|
|
|
if (StringBeginsWith(charset, NS_LITERAL_CSTRING("UTF-16"))) {
|
|
|
|
charset.AssignLiteral("UTF-8");
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// Choose encoder
|
|
|
|
if (method == NS_FORM_METHOD_POST &&
|
|
|
|
enctype == NS_FORM_ENCTYPE_MULTIPART) {
|
2010-08-20 01:58:20 +04:00
|
|
|
*aFormSubmission = new nsFSMultipartFormData(charset, aOriginatingElement);
|
2010-02-25 08:58:17 +03:00
|
|
|
} else if (method == NS_FORM_METHOD_POST &&
|
|
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
2010-08-20 01:58:20 +04:00
|
|
|
*aFormSubmission = new nsFSTextPlain(charset, aOriginatingElement);
|
2010-02-25 08:58:17 +03:00
|
|
|
} else {
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aForm->OwnerDoc();
|
2010-02-25 08:58:17 +03:00
|
|
|
if (enctype == NS_FORM_ENCTYPE_MULTIPART ||
|
|
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
|
|
|
nsAutoString enctypeStr;
|
2010-08-20 21:47:30 +04:00
|
|
|
if (aOriginatingElement &&
|
|
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None,
|
|
|
|
nsGkAtoms::formenctype)) {
|
|
|
|
aOriginatingElement->GetAttr(kNameSpaceID_None, nsGkAtoms::formenctype,
|
|
|
|
enctypeStr);
|
|
|
|
} else {
|
|
|
|
aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::enctype, enctypeStr);
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* enctypeStrPtr = enctypeStr.get();
|
2010-03-29 12:59:00 +04:00
|
|
|
SendJSWarning(doc, "ForgotPostWarning",
|
2010-02-25 08:58:17 +03:00
|
|
|
&enctypeStrPtr, 1);
|
|
|
|
}
|
2010-08-20 01:58:20 +04:00
|
|
|
*aFormSubmission = new nsFSURLEncoded(charset, method, doc,
|
|
|
|
aOriginatingElement);
|
2010-02-25 08:58:17 +03:00
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(*aFormSubmission, NS_ERROR_OUT_OF_MEMORY);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2005-07-13 20:55:59 +04:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|