2002-02-16 01:19:24 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00: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 01:19:24 +00:00
|
|
|
|
2010-05-26 12:49:38 +00:00
|
|
|
#include "nsFormSubmission.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsILinkHandler.h"
|
|
|
|
#include "nsIDocument.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
#include "nsIFormControl.h"
|
|
|
|
#include "nsIDOMHTMLFormElement.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2005-01-12 19:45:38 +00:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2012-09-30 16:40:24 +00:00
|
|
|
#include "nsAttrValueInlines.h"
|
2002-02-27 15:49:54 +00:00
|
|
|
#include "nsISaveAsCharset.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
#include "nsIFile.h"
|
2010-09-05 18:00:05 +00:00
|
|
|
#include "nsIDOMFile.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2006-01-02 02:30:32 +00:00
|
|
|
#include "nsStringStream.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
#include "nsIURI.h"
|
2007-09-06 04:29:17 +00:00
|
|
|
#include "nsIURL.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsLinebreakConverter.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsIMultiplexInputStream.h"
|
|
|
|
#include "nsIMIMEInputStream.h"
|
2010-02-25 05:58:16 +00:00
|
|
|
#include "nsIMIMEService.h"
|
2002-08-06 04:26:35 +00:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIStringBundle.h"
|
2010-02-25 05:58:16 +00:00
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIFileStreams.h"
|
2011-08-11 13:29:50 +00:00
|
|
|
#include "nsContentUtils.h"
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-11-07 23:04:22 +00:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2015-01-29 01:04:28 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2012-11-07 23:04:22 +00:00
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
using namespace mozilla;
|
2012-11-07 23:04:22 +00:00
|
|
|
using mozilla::dom::EncodingUtils;
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
static void
|
|
|
|
SendJSWarning(nsIDocument* aDocument,
|
2004-12-17 20:40:48 +00:00
|
|
|
const char* aWarningName,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t** aWarningArgs, uint32_t aWarningArgsLen)
|
2010-02-25 05:58:17 +00:00
|
|
|
{
|
2011-12-15 14:47:03 +00:00
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
2013-08-21 19:28:26 +00:00
|
|
|
NS_LITERAL_CSTRING("HTML"), aDocument,
|
2011-12-15 14:47:03 +00:00
|
|
|
nsContentUtils::eFORMS_PROPERTIES,
|
2010-02-25 05:58:17 +00:00
|
|
|
aWarningName,
|
2011-12-15 14:47:03 +00:00
|
|
|
aWarningArgs, aWarningArgsLen);
|
2010-02-25 05:58:17 +00:00
|
|
|
}
|
2002-08-06 04:26:35 +00:00
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2002-08-06 04:26:35 +00:00
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
class nsFSURLEncoded : public nsEncodingFormSubmission
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
|
|
|
public:
|
2002-06-12 07:39:08 +00: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 18:16:03 +00:00
|
|
|
nsFSURLEncoded(const nsACString& aCharset,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aMethod,
|
2010-08-19 21:58:20 +00:00
|
|
|
nsIDocument* aDocument,
|
|
|
|
nsIContent* aOriginatingElement)
|
|
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement),
|
2010-02-25 05:58:16 +00:00
|
|
|
mMethod(aMethod),
|
|
|
|
mDocument(aDocument),
|
2011-10-17 14:59:28 +00:00
|
|
|
mWarnedFileControl(false)
|
2004-01-09 23:54:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
2004-01-09 23:54:21 +00:00
|
|
|
const nsAString& aValue);
|
2010-02-25 05:58:16 +00: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 19:54:44 +00:00
|
|
|
File* aBlob);
|
2010-02-25 05:58:17 +00:00
|
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool SupportsIsindexSubmission()
|
2010-03-11 12:29:51 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-03-11 12:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsresult AddIsindex(const nsAString& aValue);
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
protected:
|
|
|
|
|
2002-06-12 07:39:08 +00: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 00:23:09 +00:00
|
|
|
* @throws NS_ERROR_OUT_OF_MEMORY if we run out of memory
|
2002-06-12 07:39:08 +00:00
|
|
|
*/
|
2002-07-18 00:23:09 +00:00
|
|
|
nsresult URLEncode(const nsAString& aStr, nsCString& aEncoded);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
private:
|
2002-06-12 07:39:08 +00:00
|
|
|
/**
|
|
|
|
* The method of the submit (either NS_FORM_METHOD_GET or
|
|
|
|
* NS_FORM_METHOD_POST).
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mMethod;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2002-06-12 07:39:08 +00:00
|
|
|
/** The query string so far (the part after the ?) */
|
2002-02-16 01:19:24 +00:00
|
|
|
nsCString mQueryString;
|
2002-08-06 04:26:35 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
/** The document whose URI to use when reporting errors */
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
|
2002-08-06 04:26:35 +00:00
|
|
|
/** Whether or not we have warned about a file control not being submitted */
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mWarnedFileControl;
|
2002-02-16 01:19:24 +00:00
|
|
|
};
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:16 +00:00
|
|
|
nsFSURLEncoded::AddNameValuePair(const nsAString& aName,
|
2002-02-16 01:19:24 +00:00
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
// Encode value
|
|
|
|
nsCString convValue;
|
2010-02-25 05:58:16 +00:00
|
|
|
nsresult rv = URLEncode(aValue, convValue);
|
2002-07-18 00:23:09 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2005-07-13 16:55:59 +00:00
|
|
|
// Encode name
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString convName;
|
2005-07-13 16:55:59 +00:00
|
|
|
rv = URLEncode(aName, convName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
// Append data to string
|
2002-06-12 07:39:08 +00:00
|
|
|
if (mQueryString.IsEmpty()) {
|
2002-02-16 01:19:24 +00:00
|
|
|
mQueryString += convName + NS_LITERAL_CSTRING("=") + convValue;
|
|
|
|
} else {
|
|
|
|
mQueryString += NS_LITERAL_CSTRING("&") + convName
|
|
|
|
+ NS_LITERAL_CSTRING("=") + convValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-11 12:29:51 +00: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-09 23:54:21 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:16 +00: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 19:54:44 +00:00
|
|
|
File* aBlob)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2010-02-25 05:58:16 +00:00
|
|
|
if (!mWarnedFileControl) {
|
2012-07-30 14:20:58 +00:00
|
|
|
SendJSWarning(mDocument, "ForgotFileEnctypeWarning", nullptr, 0);
|
2011-10-17 14:59:28 +00:00
|
|
|
mWarnedFileControl = true;
|
2010-02-25 05:58:16 +00:00
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
nsAutoString filename;
|
2015-01-29 01:04:28 +00:00
|
|
|
if (aBlob && aBlob->IsFile()) {
|
|
|
|
aBlob->GetName(filename);
|
2010-02-25 05:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return AddNameValuePair(aName, filename);
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
2002-08-04 05:03:30 +00:00
|
|
|
static void
|
|
|
|
HandleMailtoSubject(nsCString& aPath) {
|
|
|
|
|
|
|
|
// Walk through the string and see if we have a subject already.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasSubject = false;
|
|
|
|
bool hasParams = false;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t paramSep = aPath.FindChar('?');
|
|
|
|
while (paramSep != kNotFound && paramSep < (int32_t)aPath.Length()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
hasParams = true;
|
2002-08-04 05:03:30 +00: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 15:56:38 +00:00
|
|
|
int32_t nameEnd = aPath.FindChar('=', paramSep+1);
|
|
|
|
int32_t nextParamSep = aPath.FindChar('&', paramSep+1);
|
2002-08-04 05:03:30 +00: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 04:57:13 +00:00
|
|
|
if (Substring(aPath, paramSep+1, nameEnd-(paramSep+1)).
|
|
|
|
LowerCaseEqualsLiteral("subject")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
hasSubject = true;
|
2002-08-04 05:03:30 +00: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 16:17:32 +00:00
|
|
|
// Get the default subject
|
2005-04-03 17:16:28 +00:00
|
|
|
nsXPIDLString brandName;
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::GetLocalizedString(nsContentUtils::eBRAND_PROPERTIES,
|
|
|
|
"brandShortName", brandName);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *formatStrings[] = { brandName.get() };
|
2004-12-21 16:17:32 +00:00
|
|
|
nsXPIDLString subjectStr;
|
2005-04-03 17:16:28 +00:00
|
|
|
rv = nsContentUtils::FormatLocalizedString(
|
|
|
|
nsContentUtils::eFORMS_PROPERTIES,
|
|
|
|
"DefaultFormSubject",
|
|
|
|
formatStrings,
|
|
|
|
subjectStr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2004-12-21 16:17:32 +00:00
|
|
|
aPath.AppendLiteral("subject=");
|
|
|
|
nsCString subjectStrEscaped;
|
2005-04-03 17:16:28 +00:00
|
|
|
aPath.Append(NS_EscapeURL(NS_ConvertUTF16toUTF8(subjectStr), esc_Query,
|
|
|
|
subjectStrEscaped));
|
2002-08-04 05:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
nsresult
|
2002-02-16 01:19:24 +00:00
|
|
|
nsFSURLEncoded::GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aPostDataStream = nullptr;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
if (mMethod == NS_FORM_METHOD_POST) {
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isMailto = false;
|
2002-08-04 05:03:30 +00:00
|
|
|
aURI->SchemeIs("mailto", &isMailto);
|
|
|
|
if (isMailto) {
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString path;
|
2002-08-04 05:03:30 +00: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 01:19:24 +00:00
|
|
|
|
|
|
|
#ifdef SPECIFY_CHARSET_IN_CONTENT_TYPE
|
2002-08-04 05:03:30 +00:00
|
|
|
mimeStream->AddHeader("Content-Type",
|
|
|
|
PromiseFlatString(
|
|
|
|
"application/x-www-form-urlencoded; charset="
|
|
|
|
+ mCharset
|
|
|
|
).get());
|
2002-02-16 01:19:24 +00:00
|
|
|
#else
|
2002-08-04 05:03:30 +00:00
|
|
|
mimeStream->AddHeader("Content-Type",
|
|
|
|
"application/x-www-form-urlencoded");
|
2002-02-16 01:19:24 +00:00
|
|
|
#endif
|
2011-10-17 14:59:28 +00:00
|
|
|
mimeStream->SetAddContentLength(true);
|
2002-08-04 05:03:30 +00:00
|
|
|
mimeStream->SetData(dataStream);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2002-08-04 05:03:30 +00:00
|
|
|
*aPostDataStream = mimeStream;
|
|
|
|
NS_ADDREF(*aPostDataStream);
|
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Get the full query string
|
2011-09-29 06:19:26 +00:00
|
|
|
bool schemeIsJavaScript;
|
2002-02-16 01:19:24 +00:00
|
|
|
rv = aURI->SchemeIs("javascript", &schemeIsJavaScript);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (schemeIsJavaScript) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-09-06 04:29:17 +00:00
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
if (url) {
|
|
|
|
url->SetQuery(mQueryString);
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
2007-09-06 04:29:17 +00:00
|
|
|
else {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString path;
|
2007-09-06 04:29:17 +00: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 15:56:38 +00:00
|
|
|
int32_t namedAnchorPos = path.FindChar('#');
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString namedAnchor;
|
2007-09-06 04:29:17 +00:00
|
|
|
if (kNotFound != namedAnchorPos) {
|
|
|
|
path.Right(namedAnchor, (path.Length() - namedAnchorPos));
|
|
|
|
path.Truncate(namedAnchorPos);
|
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2007-09-06 04:29:17 +00:00
|
|
|
// Chop off old query string (bug 25330, 57333)
|
|
|
|
// Only do this for GET not POST (bug 41585)
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t queryStart = path.FindChar('?');
|
2007-09-06 04:29:17 +00:00
|
|
|
if (kNotFound != queryStart) {
|
|
|
|
path.Truncate(queryStart);
|
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2007-09-06 04:29:17 +00:00
|
|
|
path.Append('?');
|
|
|
|
// Bug 42616: Add named anchor to end after query string
|
|
|
|
path.Append(mQueryString + namedAnchor);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2007-09-06 04:29:17 +00:00
|
|
|
aURI->SetPath(path);
|
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// i18n helper routines
|
2002-07-18 00:23:09 +00:00
|
|
|
nsresult
|
2002-06-12 07:39:08 +00:00
|
|
|
nsFSURLEncoded::URLEncode(const nsAString& aStr, nsCString& aEncoded)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
|
|
|
// convert to CRLF breaks
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t* convertedBuf =
|
2005-07-13 16:55:59 +00:00
|
|
|
nsLinebreakConverter::ConvertUnicharLineBreaks(PromiseFlatString(aStr).get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakNet);
|
|
|
|
NS_ENSURE_TRUE(convertedBuf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString encodedBuf;
|
2010-11-23 08:50:55 +00:00
|
|
|
nsresult rv = EncodeVal(nsDependentString(convertedBuf), encodedBuf, false);
|
2015-03-27 00:01:12 +00:00
|
|
|
free(convertedBuf);
|
2005-07-13 16:55:59 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2005-07-13 16:55:59 +00:00
|
|
|
char* escapedBuf = nsEscape(encodedBuf.get(), url_XPAlphas);
|
|
|
|
NS_ENSURE_TRUE(escapedBuf, NS_ERROR_OUT_OF_MEMORY);
|
2002-02-16 01:19:24 +00:00
|
|
|
aEncoded.Adopt(escapedBuf);
|
2002-07-18 00:23:09 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2010-08-19 21:58:20 +00:00
|
|
|
nsFSMultipartFormData::nsFSMultipartFormData(const nsACString& aCharset,
|
|
|
|
nsIContent* aOriginatingElement)
|
|
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2010-02-25 05:58:16 +00:00
|
|
|
mPostDataStream =
|
|
|
|
do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1");
|
2012-09-19 22:15:32 +00:00
|
|
|
mTotalLength = 0;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
mBoundary.AssignLiteral("---------------------------");
|
|
|
|
mBoundary.AppendInt(rand());
|
|
|
|
mBoundary.AppendInt(rand());
|
|
|
|
mBoundary.AppendInt(rand());
|
2002-07-18 00:23:09 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:18 +00:00
|
|
|
nsFSMultipartFormData::~nsFSMultipartFormData()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPostDataChunk.IsEmpty(), "Left unsubmitted data");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIInputStream*
|
2012-09-19 22:15:32 +00:00
|
|
|
nsFSMultipartFormData::GetSubmissionBody(uint64_t* aContentLength)
|
2010-02-25 05:58:18 +00:00
|
|
|
{
|
|
|
|
// Finish data
|
|
|
|
mPostDataChunk += NS_LITERAL_CSTRING("--") + mBoundary
|
|
|
|
+ NS_LITERAL_CSTRING("--" CRLF);
|
|
|
|
|
|
|
|
// Add final data input stream
|
|
|
|
AddPostDataStream();
|
|
|
|
|
2012-09-19 22:15:32 +00:00
|
|
|
*aContentLength = mTotalLength;
|
2010-02-25 05:58:18 +00:00
|
|
|
return mPostDataStream;
|
|
|
|
}
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:16 +00:00
|
|
|
nsFSMultipartFormData::AddNameValuePair(const nsAString& aName,
|
2002-07-18 00:23:09 +00:00
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
nsCString valueStr;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString encodedVal;
|
2010-11-23 08:50:55 +00:00
|
|
|
nsresult rv = EncodeVal(aValue, encodedVal, false);
|
2010-02-25 05:58:16 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
valueStr.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(encodedVal.get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakNet));
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nameStr;
|
2010-11-23 08:50:55 +00:00
|
|
|
rv = EncodeVal(aName, nameStr, true);
|
2002-07-18 00:23:09 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
// Make MIME block for name/value pair
|
2010-02-25 05:58:17 +00:00
|
|
|
|
2005-07-13 16:55:59 +00: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 01:19:24 +00:00
|
|
|
mPostDataChunk += NS_LITERAL_CSTRING("--") + mBoundary
|
2007-10-24 22:21:01 +00:00
|
|
|
+ NS_LITERAL_CSTRING(CRLF)
|
|
|
|
+ NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
|
|
|
|
+ nameStr + NS_LITERAL_CSTRING("\"" CRLF CRLF)
|
2002-02-16 01:19:24 +00:00
|
|
|
+ valueStr + NS_LITERAL_CSTRING(CRLF);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:16 +00: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 19:54:44 +00:00
|
|
|
File* aBlob)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2010-02-25 05:58:16 +00:00
|
|
|
// Encode the control name
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nameStr;
|
2010-11-23 08:50:55 +00:00
|
|
|
nsresult rv = EncodeVal(aName, nameStr, true);
|
2002-07-18 00:23:09 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2010-11-23 08:50:55 +00:00
|
|
|
nsCString filename, contentType;
|
2010-02-25 05:58:16 +00:00
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
2010-10-13 23:25:33 +00: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 19:54:44 +00: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 18:03:36 +00: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 19:54:44 +00:00
|
|
|
nsAutoString filepath16;
|
|
|
|
rv = aBlob->GetPath(filepath16);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-03-03 18:30:13 +00: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 19:54:44 +00:00
|
|
|
if (!filepath16.IsEmpty()) {
|
|
|
|
// File.path includes trailing "/"
|
|
|
|
filename16 = filepath16 + filename16;
|
2011-06-29 18:03:36 +00: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 19:54:44 +00:00
|
|
|
rv = EncodeVal(filename16, filename, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
// Get content type
|
2010-11-23 08:50:55 +00:00
|
|
|
nsAutoString contentType16;
|
|
|
|
rv = aBlob->GetType(contentType16);
|
|
|
|
if (NS_FAILED(rv) || contentType16.IsEmpty()) {
|
|
|
|
contentType16.AssignLiteral("application/octet-stream");
|
2010-02-25 05:58:16 +00:00
|
|
|
}
|
2010-11-23 08:50:55 +00:00
|
|
|
contentType.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(NS_ConvertUTF16toUTF8(contentType16).get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakSpace));
|
2013-03-03 18:30:13 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
// Get input stream
|
2010-10-13 23:25:33 +00:00
|
|
|
rv = aBlob->GetInternalStream(getter_AddRefs(fileStream));
|
2010-09-05 18:00:05 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-02-25 05:58:16 +00: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 18:30:13 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
fileStream = bufferedStream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
contentType.AssignLiteral("application/octet-stream");
|
|
|
|
}
|
|
|
|
|
2002-02-16 01:19:24 +00: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 16:55:59 +00: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 01:19:24 +00:00
|
|
|
mPostDataChunk +=
|
|
|
|
NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
|
|
|
|
+ nameStr + NS_LITERAL_CSTRING("\"; filename=\"")
|
2010-11-23 08:50:55 +00:00
|
|
|
+ filename + NS_LITERAL_CSTRING("\"" CRLF)
|
|
|
|
+ NS_LITERAL_CSTRING("Content-Type: ")
|
|
|
|
+ contentType + NS_LITERAL_CSTRING(CRLF CRLF);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2013-03-15 21:57:35 +00: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-18 22:02:09 +00: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 05:58:16 +00:00
|
|
|
mPostDataStream->AppendStream(fileStream);
|
2012-09-19 22:15:32 +00:00
|
|
|
mTotalLength += size;
|
2002-04-07 00:17:56 +00:00
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
// CRLF after file
|
2004-06-17 00:13:25 +00:00
|
|
|
mPostDataChunk.AppendLiteral(CRLF);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
nsresult
|
2002-02-16 01:19:24 +00: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 02:35:17 +00:00
|
|
|
nsAutoCString contentType;
|
2010-02-25 05:58:18 +00:00
|
|
|
GetContentType(contentType);
|
|
|
|
mimeStream->AddHeader("Content-Type", contentType.get());
|
2011-10-17 14:59:28 +00:00
|
|
|
mimeStream->SetAddContentLength(true);
|
2012-09-19 22:15:32 +00:00
|
|
|
uint64_t unused;
|
|
|
|
mimeStream->SetData(GetSubmissionBody(&unused));
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2014-03-15 19:00:15 +00:00
|
|
|
mimeStream.forget(aPostDataStream);
|
2002-02-16 01:19:24 +00: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-19 22:15:32 +00:00
|
|
|
mTotalLength += mPostDataChunk.Length();
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mPostDataChunk.Truncate();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
class nsFSTextPlain : public nsEncodingFormSubmission
|
2002-07-11 23:32:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2010-08-19 21:58:20 +00:00
|
|
|
nsFSTextPlain(const nsACString& aCharset, nsIContent* aOriginatingElement)
|
|
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement)
|
2004-01-09 23:54:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
2004-01-09 23:54:21 +00:00
|
|
|
const nsAString& aValue);
|
2010-02-25 05:58:16 +00: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 19:54:44 +00:00
|
|
|
File* aBlob);
|
2010-02-25 05:58:17 +00:00
|
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream);
|
2002-07-11 23:32:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mBody;
|
|
|
|
};
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:16 +00:00
|
|
|
nsFSTextPlain::AddNameValuePair(const nsAString& aName,
|
2002-07-11 23:32:13 +00: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 05:58:16 +00:00
|
|
|
mBody.Append(aName + NS_LITERAL_STRING("=") + aValue +
|
|
|
|
NS_LITERAL_STRING(CRLF));
|
2002-07-11 23:32:13 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:16 +00: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 19:54:44 +00:00
|
|
|
File* aBlob)
|
2002-07-11 23:32:13 +00:00
|
|
|
{
|
2010-02-25 05:58:16 +00:00
|
|
|
nsAutoString filename;
|
2015-01-29 01:04:28 +00:00
|
|
|
if (aBlob && aBlob->IsFile()) {
|
|
|
|
aBlob->GetName(filename);
|
2010-02-25 05:58:16 +00:00
|
|
|
}
|
2013-03-03 18:30:13 +00:00
|
|
|
|
2010-02-25 05:58:16 +00:00
|
|
|
AddNameValuePair(aName, filename);
|
2002-07-11 23:32:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
nsresult
|
2002-07-11 23:32:13 +00: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 06:19:26 +00:00
|
|
|
bool isMailto = false;
|
2002-07-11 23:32:13 +00:00
|
|
|
aURI->SchemeIs("mailto", &isMailto);
|
|
|
|
if (isMailto) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString path;
|
2002-07-11 23:32:13 +00:00
|
|
|
rv = aURI->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-08-04 05:03:30 +00:00
|
|
|
HandleMailtoSubject(path);
|
2002-07-11 23:32:13 +00:00
|
|
|
|
|
|
|
// Append the body to and force-plain-text args to the mailto line
|
2005-07-13 16:55:59 +00:00
|
|
|
char* escapedBuf = nsEscape(NS_ConvertUTF16toUTF8(mBody).get(),
|
|
|
|
url_XAlphas);
|
|
|
|
NS_ENSURE_TRUE(escapedBuf, NS_ERROR_OUT_OF_MEMORY);
|
2002-07-11 23:32:13 +00:00
|
|
|
nsCString escapedBody;
|
2005-07-13 16:55:59 +00:00
|
|
|
escapedBody.Adopt(escapedBuf);
|
2002-07-11 23:32:13 +00:00
|
|
|
|
|
|
|
path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;
|
|
|
|
|
|
|
|
rv = aURI->SetPath(path);
|
|
|
|
|
|
|
|
} else {
|
2010-11-23 08:50:55 +00: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-11 23:32:13 +00:00
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
2010-11-23 08:50:55 +00:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(bodyStream), cbody);
|
2002-07-11 23:32:13 +00: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 14:59:28 +00:00
|
|
|
mimeStream->SetAddContentLength(true);
|
2002-07-11 23:32:13 +00:00
|
|
|
mimeStream->SetData(bodyStream);
|
|
|
|
CallQueryInterface(mimeStream, aPostDataStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2010-08-19 21:58:20 +00:00
|
|
|
nsEncodingFormSubmission::nsEncodingFormSubmission(const nsACString& aCharset,
|
|
|
|
nsIContent* aOriginatingElement)
|
|
|
|
: nsFormSubmission(aCharset, aOriginatingElement)
|
2010-02-25 05:58:16 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString charset(aCharset);
|
2010-02-25 05:58:17 +00: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 14:18:30 +00:00
|
|
|
if (!(charset.EqualsLiteral("UTF-8") || charset.EqualsLiteral("gb18030"))) {
|
2012-03-02 05:19:30 +00:00
|
|
|
NS_ConvertUTF8toUTF16 charsetUtf16(charset);
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* charsetPtr = charsetUtf16.get();
|
2012-01-02 14:18:30 +00:00
|
|
|
SendJSWarning(aOriginatingElement ? aOriginatingElement->GetOwnerDocument()
|
2012-07-30 14:20:58 +00:00
|
|
|
: nullptr,
|
2012-01-02 14:18:30 +00:00
|
|
|
"CannotEncodeAllUnicode",
|
|
|
|
&charsetPtr,
|
|
|
|
1);
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00: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 14:20:58 +00:00
|
|
|
mEncoder = nullptr;
|
2010-02-25 05:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-25 05:58:16 +00:00
|
|
|
}
|
2002-07-11 23:32:13 +00:00
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
nsEncodingFormSubmission::~nsEncodingFormSubmission()
|
2002-08-06 04:26:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
// i18n helper routines
|
|
|
|
nsresult
|
2010-11-23 08:50:55 +00:00
|
|
|
nsEncodingFormSubmission::EncodeVal(const nsAString& aStr, nsCString& aOut,
|
|
|
|
bool aHeaderEncode)
|
2010-02-25 05:58:17 +00:00
|
|
|
{
|
2010-11-23 08:50:55 +00:00
|
|
|
if (mEncoder && !aStr.IsEmpty()) {
|
2010-02-25 05:58:17 +00:00
|
|
|
aOut.Truncate();
|
2010-11-23 08:50:55 +00:00
|
|
|
nsresult rv = mEncoder->Convert(PromiseFlatString(aStr).get(),
|
|
|
|
getter_Copies(aOut));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-02-25 05:58:17 +00:00
|
|
|
}
|
2010-11-23 08:50:55 +00: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 05:58:17 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
2010-02-25 05:58:16 +00:00
|
|
|
GetSubmitCharset(nsGenericHTMLElement* aForm,
|
|
|
|
nsACString& oCharset)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2004-06-17 00:13:25 +00:00
|
|
|
oCharset.AssignLiteral("UTF-8"); // default to utf-8
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
nsAutoString acceptCharsetValue;
|
2006-12-26 17:47:52 +00:00
|
|
|
aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::acceptcharset,
|
2005-01-25 00:02:58 +00:00
|
|
|
acceptCharsetValue);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t charsetLen = acceptCharsetValue.Length();
|
2002-02-16 01:19:24 +00:00
|
|
|
if (charsetLen > 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset=0;
|
|
|
|
int32_t spPos=0;
|
2002-02-16 01:19:24 +00:00
|
|
|
// get charset from charsets one by one
|
2012-03-05 03:57:51 +00:00
|
|
|
do {
|
2014-01-04 15:02:17 +00:00
|
|
|
spPos = acceptCharsetValue.FindChar(char16_t(' '), offset);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t cnt = ((-1==spPos)?(charsetLen-offset):(spPos-offset));
|
2012-03-05 03:57:51 +00:00
|
|
|
if (cnt > 0) {
|
|
|
|
nsAutoString uCharset;
|
|
|
|
acceptCharsetValue.Mid(uCharset, offset, cnt);
|
|
|
|
|
2012-11-07 23:04:22 +00:00
|
|
|
if (EncodingUtils::FindEncodingForLabel(uCharset, oCharset))
|
2012-03-05 03:57:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
offset = spPos + 1;
|
|
|
|
} while (spPos != -1);
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
// if there are no accept-charset or all the charset are not supported
|
|
|
|
// Get the charset from document
|
2014-08-22 20:11:27 +00:00
|
|
|
nsIDocument* doc = aForm->GetComposedDoc();
|
2002-08-06 04:26:35 +00:00
|
|
|
if (doc) {
|
2003-10-22 06:09:48 +00:00
|
|
|
oCharset = doc->GetDocumentCharacterSet();
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:17 +00:00
|
|
|
static void
|
2010-02-25 05:58:16 +00:00
|
|
|
GetEnumAttr(nsGenericHTMLElement* aContent,
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIAtom* atom, int32_t* aValue)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2005-01-25 00:02:58 +00:00
|
|
|
const nsAttrValue* value = aContent->GetParsedAttr(atom);
|
|
|
|
if (value && value->Type() == nsAttrValue::eEnum) {
|
|
|
|
*aValue = value->GetEnumValue();
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 16:55:59 +00:00
|
|
|
nsresult
|
2010-02-25 05:58:17 +00:00
|
|
|
GetSubmissionFromForm(nsGenericHTMLElement* aForm,
|
2010-08-20 17:47:30 +00:00
|
|
|
nsGenericHTMLElement* aOriginatingElement,
|
2010-02-25 05:58:17 +00:00
|
|
|
nsFormSubmission** aFormSubmission)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2010-02-25 05:58:17 +00:00
|
|
|
// Get all the information necessary to encode the form data
|
2014-10-02 19:07:24 +00:00
|
|
|
NS_ASSERTION(aForm->GetComposedDoc(),
|
2010-03-29 08:59:00 +00:00
|
|
|
"Should have doc if we're building submission!");
|
2010-02-25 05:58:17 +00:00
|
|
|
|
|
|
|
// Get encoding type (default: urlencoded)
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t enctype = NS_FORM_ENCTYPE_URLENCODED;
|
2010-08-20 17:47:30 +00:00
|
|
|
if (aOriginatingElement &&
|
|
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formenctype)) {
|
|
|
|
GetEnumAttr(aOriginatingElement, nsGkAtoms::formenctype, &enctype);
|
|
|
|
} else {
|
|
|
|
GetEnumAttr(aForm, nsGkAtoms::enctype, &enctype);
|
|
|
|
}
|
2010-02-25 05:58:17 +00:00
|
|
|
|
|
|
|
// Get method (default: GET)
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t method = NS_FORM_METHOD_GET;
|
2010-08-20 17:47:30 +00:00
|
|
|
if (aOriginatingElement &&
|
|
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formmethod)) {
|
|
|
|
GetEnumAttr(aOriginatingElement, nsGkAtoms::formmethod, &method);
|
|
|
|
} else {
|
|
|
|
GetEnumAttr(aForm, nsGkAtoms::method, &method);
|
|
|
|
}
|
2010-02-25 05:58:17 +00:00
|
|
|
|
|
|
|
// Get charset
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString charset;
|
2010-02-25 05:58:17 +00:00
|
|
|
GetSubmitCharset(aForm, charset);
|
2010-02-25 05:58:17 +00:00
|
|
|
|
2012-03-02 05:19:30 +00: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 05:58:17 +00:00
|
|
|
// Choose encoder
|
|
|
|
if (method == NS_FORM_METHOD_POST &&
|
|
|
|
enctype == NS_FORM_ENCTYPE_MULTIPART) {
|
2010-08-19 21:58:20 +00:00
|
|
|
*aFormSubmission = new nsFSMultipartFormData(charset, aOriginatingElement);
|
2010-02-25 05:58:17 +00:00
|
|
|
} else if (method == NS_FORM_METHOD_POST &&
|
|
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
2010-08-19 21:58:20 +00:00
|
|
|
*aFormSubmission = new nsFSTextPlain(charset, aOriginatingElement);
|
2010-02-25 05:58:17 +00:00
|
|
|
} else {
|
2011-10-18 10:53:36 +00:00
|
|
|
nsIDocument* doc = aForm->OwnerDoc();
|
2010-02-25 05:58:17 +00:00
|
|
|
if (enctype == NS_FORM_ENCTYPE_MULTIPART ||
|
|
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
|
|
|
nsAutoString enctypeStr;
|
2010-08-20 17:47:30 +00: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 15:02:17 +00:00
|
|
|
const char16_t* enctypeStrPtr = enctypeStr.get();
|
2010-03-29 08:59:00 +00:00
|
|
|
SendJSWarning(doc, "ForgotPostWarning",
|
2010-02-25 05:58:17 +00:00
|
|
|
&enctypeStrPtr, 1);
|
|
|
|
}
|
2010-08-19 21:58:20 +00:00
|
|
|
*aFormSubmission = new nsFSURLEncoded(charset, method, doc,
|
|
|
|
aOriginatingElement);
|
2010-02-25 05:58:17 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(*aFormSubmission, NS_ERROR_OUT_OF_MEMORY);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2005-07-13 16:55:59 +00:00
|
|
|
return NS_OK;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|