2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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
|
|
|
|
2016-06-16 10:24:16 +03:00
|
|
|
#include "HTMLFormSubmission.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"
|
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-16 04:19:24 +03:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#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"
|
2018-02-26 22:43:45 +03:00
|
|
|
#include "nsIURIMutator.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
|
|
|
|
2016-05-13 14:11:38 +03:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2018-01-08 16:05:05 +03:00
|
|
|
#include "mozilla/dom/DOMPrefs.h"
|
2015-01-29 04:04:28 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2012-11-08 03:04:22 +04:00
|
|
|
|
2016-06-16 10:24:16 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void
|
2010-02-25 08:58:16 +03:00
|
|
|
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
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
void
|
2016-01-20 20:25:03 +03:00
|
|
|
RetrieveFileName(Blob* aBlob, nsAString& aFilename)
|
|
|
|
{
|
2016-02-24 02:01:01 +03:00
|
|
|
if (!aBlob) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-20 20:25:03 +03:00
|
|
|
|
|
|
|
RefPtr<File> file = aBlob->ToFile();
|
|
|
|
if (file) {
|
|
|
|
file->GetName(aFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:58 +03:00
|
|
|
void
|
|
|
|
RetrieveDirectoryName(Directory* aDirectory, nsAString& aDirname)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDirectory);
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
aDirectory->GetName(aDirname, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
aDirname.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
2002-08-06 08:26:35 +04:00
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
class FSURLEncoded : public EncodingFormSubmission
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
public:
|
2002-06-12 11:39:08 +04:00
|
|
|
/**
|
2017-04-27 13:27:03 +03:00
|
|
|
* @param aEncoding the character encoding of the form
|
2002-06-12 11:39:08 +04:00
|
|
|
* @param aMethod the method of the submit (either NS_FORM_METHOD_GET or
|
|
|
|
* NS_FORM_METHOD_POST).
|
|
|
|
*/
|
2017-04-27 13:27:03 +03:00
|
|
|
FSURLEncoded(NotNull<const Encoding*> aEncoding,
|
2016-06-16 10:26:34 +03:00
|
|
|
int32_t aMethod,
|
|
|
|
nsIDocument* aDocument,
|
2017-12-07 21:13:50 +03:00
|
|
|
Element* aOriginatingElement)
|
2017-04-27 13:27:03 +03:00
|
|
|
: EncodingFormSubmission(aEncoding, aOriginatingElement)
|
|
|
|
, mMethod(aMethod)
|
|
|
|
, mDocument(aDocument)
|
|
|
|
, mWarnedFileControl(false)
|
2004-01-10 02:54:21 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
virtual nsresult
|
|
|
|
AddNameValuePair(const nsAString& aName, const nsAString& aValue) override;
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
AddNameBlobOrNullPair(const nsAString& aName, Blob* aBlob) override;
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
virtual nsresult
|
|
|
|
AddNameDirectoryPair(const nsAString& aName, Directory* aDirectory) override;
|
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
virtual nsresult
|
2017-09-22 09:12:03 +03:00
|
|
|
GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream,
|
2018-02-26 22:43:45 +03:00
|
|
|
int64_t* aPostDataStreamLength, nsCOMPtr<nsIURI>& aOutURI) override;
|
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
|
|
|
*/
|
2016-05-13 23:48:03 +03:00
|
|
|
nsresult URLEncode(const nsAString& aStr, nsACString& 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
|
2016-06-16 10:26:34 +03:00
|
|
|
FSURLEncoded::AddNameValuePair(const nsAString& aName,
|
|
|
|
const nsAString& aValue)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
FSURLEncoded::AddNameBlobOrNullPair(const nsAString& aName,
|
|
|
|
Blob* 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;
|
2016-01-20 20:25:03 +03:00
|
|
|
RetrieveFileName(aBlob, filename);
|
2010-02-25 08:58:16 +03:00
|
|
|
return AddNameValuePair(aName, filename);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
nsresult
|
|
|
|
FSURLEncoded::AddNameDirectoryPair(const nsAString& aName,
|
|
|
|
Directory* aDirectory)
|
|
|
|
{
|
2016-07-14 10:01:58 +03:00
|
|
|
// No warning about because Directory objects are never sent via form.
|
|
|
|
|
|
|
|
nsAutoString dirname;
|
|
|
|
RetrieveDirectoryName(aDirectory, dirname);
|
|
|
|
return AddNameValuePair(aName, dirname);
|
2016-07-14 10:01:31 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
void
|
|
|
|
HandleMailtoSubject(nsCString& aPath)
|
|
|
|
{
|
2002-08-04 09:03:30 +04:00
|
|
|
// 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
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString brandName;
|
2005-04-03 21:16:28 +04:00
|
|
|
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() };
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString 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;
|
2017-01-24 22:11:44 +03:00
|
|
|
rv = NS_EscapeURL(NS_ConvertUTF16toUTF8(subjectStr), esc_Query,
|
|
|
|
subjectStrEscaped, mozilla::fallible);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
aPath.Append(subjectStrEscaped);
|
2002-08-04 09:03:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
FSURLEncoded::GetEncodedSubmission(nsIURI* aURI,
|
2017-09-22 09:12:03 +03:00
|
|
|
nsIInputStream** aPostDataStream,
|
2018-02-26 22:43:45 +03:00
|
|
|
int64_t* aPostDataStreamLength,
|
|
|
|
nsCOMPtr<nsIURI>& aOutURI)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2018-02-26 22:43:45 +03:00
|
|
|
aOutURI = aURI;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPostDataStream = nullptr;
|
2017-09-22 09:12:03 +03:00
|
|
|
*aPostDataStreamLength = -1;
|
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;
|
2017-07-29 14:50:21 +03:00
|
|
|
rv = aURI->GetPathQueryRef(path);
|
2002-08-04 09:03:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
HandleMailtoSubject(path);
|
|
|
|
|
|
|
|
// Append the body to and force-plain-text args to the mailto line
|
2016-05-18 19:21:56 +03:00
|
|
|
nsAutoCString escapedBody;
|
|
|
|
if (NS_WARN_IF(!NS_Escape(mQueryString, escapedBody, url_XAlphas))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2002-08-04 09:03:30 +04:00
|
|
|
|
|
|
|
path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;
|
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
return NS_MutateURI(aURI)
|
|
|
|
.SetPathQueryRef(path)
|
|
|
|
.Finalize(aOutURI);
|
2002-08-04 09:03:30 +04:00
|
|
|
} 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
|
|
|
|
2002-08-04 09:03:30 +04:00
|
|
|
mimeStream->AddHeader("Content-Type",
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
mimeStream->SetData(dataStream);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2002-08-04 09:03:30 +04:00
|
|
|
*aPostDataStream = mimeStream;
|
|
|
|
NS_ADDREF(*aPostDataStream);
|
2017-09-22 09:12:03 +03:00
|
|
|
|
|
|
|
*aPostDataStreamLength = mQueryString.Length();
|
2002-08-04 09:03:30 +04:00
|
|
|
}
|
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) {
|
2018-02-26 22:43:45 +03:00
|
|
|
rv = NS_MutateURI(aURI)
|
|
|
|
.SetQuery(mQueryString)
|
|
|
|
.Finalize(aOutURI);
|
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;
|
2017-07-29 14:50:21 +03:00
|
|
|
rv = aURI->GetPathQueryRef(path);
|
2007-09-06 08:29:17 +04:00
|
|
|
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
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
rv = NS_MutateURI(aURI)
|
|
|
|
.SetPathQueryRef(path)
|
|
|
|
.Finalize(aOutURI);
|
2007-09-06 08:29:17 +04:00
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// i18n helper routines
|
2002-07-18 04:23:09 +04:00
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
FSURLEncoded::URLEncode(const nsAString& aStr, nsACString& aEncoded)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
// convert to CRLF breaks
|
2016-05-13 23:48:03 +03:00
|
|
|
int32_t convertedBufLength = 0;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* convertedBuf =
|
2016-05-13 23:48:03 +03:00
|
|
|
nsLinebreakConverter::ConvertUnicharLineBreaks(aStr.BeginReading(),
|
2005-07-13 20:55:59 +04:00
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
2016-05-13 23:48:03 +03:00
|
|
|
nsLinebreakConverter::eLinebreakNet,
|
|
|
|
aStr.Length(),
|
|
|
|
&convertedBufLength);
|
2005-07-13 20:55:59 +04:00
|
|
|
NS_ENSURE_TRUE(convertedBuf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2016-05-13 23:48:03 +03:00
|
|
|
nsAutoString convertedString;
|
|
|
|
convertedString.Adopt(convertedBuf, convertedBufLength);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encodedBuf;
|
2016-05-13 23:48:03 +03:00
|
|
|
nsresult rv = EncodeVal(convertedString, encodedBuf, false);
|
2005-07-13 20:55:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2016-05-13 23:48:03 +03:00
|
|
|
if (NS_WARN_IF(!NS_Escape(encodedBuf, aEncoded, url_XPAlphas))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2002-07-18 04:23:09 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
FSMultipartFormData::FSMultipartFormData(NotNull<const Encoding*> aEncoding,
|
2017-12-07 21:13:50 +03:00
|
|
|
Element* aOriginatingElement)
|
2017-04-27 13:27:03 +03:00
|
|
|
: EncodingFormSubmission(aEncoding, aOriginatingElement)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2017-09-19 17:26:21 +03:00
|
|
|
mPostData =
|
2010-02-25 08:58:16 +03:00
|
|
|
do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1");
|
2017-09-19 17:26:21 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream = do_QueryInterface(mPostData);
|
|
|
|
MOZ_ASSERT(SameCOMIdentity(mPostData, inputStream));
|
|
|
|
mPostDataStream = inputStream;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
FSMultipartFormData::~FSMultipartFormData()
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mPostDataChunk.IsEmpty(), "Left unsubmitted data");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIInputStream*
|
2016-06-16 10:26:34 +03:00
|
|
|
FSMultipartFormData::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
|
2016-06-16 10:26:34 +03:00
|
|
|
FSMultipartFormData::AddNameValuePair(const nsAString& aName,
|
|
|
|
const nsAString& aValue)
|
2002-07-18 04:23:09 +04:00
|
|
|
{
|
|
|
|
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
|
2016-02-24 02:01:01 +03:00
|
|
|
// RFC 2388 specifies that RFC 2047 be used, but I think it's not
|
2005-07-13 20:55:59 +04:00
|
|
|
// 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
|
2016-06-16 10:26:34 +03:00
|
|
|
FSMultipartFormData::AddNameBlobOrNullPair(const nsAString& aName, Blob* 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
|
|
|
|
2016-07-14 10:01:58 +03:00
|
|
|
ErrorResult error;
|
|
|
|
|
|
|
|
uint64_t size = 0;
|
2016-02-24 02:01:01 +03:00
|
|
|
nsAutoCString filename;
|
|
|
|
nsAutoCString contentType;
|
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
2016-02-23 20:38:16 +03:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
if (aBlob) {
|
|
|
|
nsAutoString filename16;
|
|
|
|
|
|
|
|
RefPtr<File> file = aBlob->ToFile();
|
|
|
|
if (file) {
|
2017-02-06 13:07:54 +03:00
|
|
|
nsAutoString relativePath;
|
|
|
|
file->GetRelativePath(relativePath);
|
2018-01-08 16:05:05 +03:00
|
|
|
if (DOMPrefs::WebkitBlinkDirectoryPickerEnabled() &&
|
2017-02-06 13:07:54 +03:00
|
|
|
!relativePath.IsEmpty()) {
|
|
|
|
filename16 = relativePath;
|
2016-02-24 02:01:01 +03:00
|
|
|
}
|
2013-03-03 22:30:13 +04:00
|
|
|
|
2016-05-13 14:11:38 +03:00
|
|
|
if (filename16.IsEmpty()) {
|
|
|
|
RetrieveFileName(aBlob, filename16);
|
|
|
|
}
|
2016-02-24 02:01:01 +03:00
|
|
|
}
|
2011-06-29 22:03:36 +04:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
rv = EncodeVal(filename16, filename, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
Bug 1127150 - Use File's name instead of explicit file name in form submission related classes. r=baku
Our nsFormSubmission and subclasses accepted a third filename argument to
explicitly specify the filename. Since switching from nsIDOMBlob to File in Bug
1085283, we can read out the filename directly from the File. This simplifies
the code, but introduces a change in the way Firefox submits form data to
servers.
Consider the code:
var fd = new FormData();
fd.append("blob1", new Blob(["hi"]), ""); // explicit empty filename as third arg
fd.append("file1", new File(["hi"], "")); // File's name is empty, no third arg.
xhr.send(fd);
Previously, the request body had filename="" in the first case, and filename="blob" in the second.
This patch will change it to both cases result in filename=""
This behaviour isn't exactly specced anywhere, nor in the HTML spec [1], nor in
RFC 2388. In addition Blink (at least Chromium v40) has the same behaviour
introduced by this patch. So shipping it seems ok to me.
[1]: http://www.w3.org/html/wg/drafts/html/master/semantics.html#multipart/form-data-encoding-algorithm
--HG--
extra : rebase_source : 6631e6900fe1a9b991c397b76e5be6b913715c5a
2015-02-21 22:54:44 +03:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
// Get content type
|
|
|
|
nsAutoString contentType16;
|
|
|
|
aBlob->GetType(contentType16);
|
|
|
|
if (contentType16.IsEmpty()) {
|
|
|
|
contentType16.AssignLiteral("application/octet-stream");
|
|
|
|
}
|
2015-05-19 17:36:37 +03:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
contentType.Adopt(nsLinebreakConverter::
|
|
|
|
ConvertLineBreaks(NS_ConvertUTF16toUTF8(contentType16).get(),
|
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakSpace));
|
2013-03-03 22:30:13 +04:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
// Get input stream
|
2017-10-02 14:53:12 +03:00
|
|
|
aBlob->CreateInputStream(getter_AddRefs(fileStream), error);
|
2016-02-24 02:01:01 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
2016-01-08 11:35:30 +03:00
|
|
|
|
2016-07-14 10:01:58 +03:00
|
|
|
// Get size
|
|
|
|
size = aBlob->GetSize(error);
|
|
|
|
if (error.Failed()) {
|
|
|
|
error.SuppressException();
|
|
|
|
fileStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
if (fileStream) {
|
|
|
|
// Create buffered stream (for efficiency)
|
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
2017-10-19 12:39:30 +03:00
|
|
|
fileStream.forget(), 8192);
|
2016-02-24 02:01:01 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-01-08 11:35:30 +03:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
fileStream = bufferedStream;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contentType.AssignLiteral("application/octet-stream");
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:58 +03:00
|
|
|
AddDataChunk(nameStr, filename, contentType, fileStream, size);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FSMultipartFormData::AddNameDirectoryPair(const nsAString& aName,
|
|
|
|
Directory* aDirectory)
|
|
|
|
{
|
2018-01-08 16:05:05 +03:00
|
|
|
if (!DOMPrefs::WebkitBlinkDirectoryPickerEnabled()) {
|
2016-07-14 10:01:58 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the control name
|
|
|
|
nsAutoCString nameStr;
|
|
|
|
nsresult rv = EncodeVal(aName, nameStr, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString dirname;
|
|
|
|
nsAutoString dirname16;
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
nsAutoString path;
|
|
|
|
aDirectory->GetPath(path, error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
error.SuppressException();
|
|
|
|
} else {
|
|
|
|
dirname16 = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirname16.IsEmpty()) {
|
|
|
|
RetrieveDirectoryName(aDirectory, dirname16);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = EncodeVal(dirname16, dirname, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
AddDataChunk(nameStr, dirname,
|
|
|
|
NS_LITERAL_CSTRING("application/octet-stream"),
|
|
|
|
nullptr, 0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FSMultipartFormData::AddDataChunk(const nsACString& aName,
|
|
|
|
const nsACString& aFilename,
|
|
|
|
const nsACString& aContentType,
|
|
|
|
nsIInputStream* aInputStream,
|
|
|
|
uint64_t aInputStreamSize)
|
|
|
|
{
|
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
|
2016-02-24 02:01:01 +03:00
|
|
|
// RFC 2388 specifies that RFC 2047 be used, but I think it's not
|
2005-07-13 20:55:59 +04:00
|
|
|
// consistent with the MIME standard.
|
2002-02-16 04:19:24 +03:00
|
|
|
mPostDataChunk +=
|
|
|
|
NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
|
2016-07-14 10:01:58 +03:00
|
|
|
+ aName + NS_LITERAL_CSTRING("\"; filename=\"")
|
|
|
|
+ aFilename + NS_LITERAL_CSTRING("\"" CRLF)
|
2010-11-23 11:50:55 +03:00
|
|
|
+ NS_LITERAL_CSTRING("Content-Type: ")
|
2016-07-14 10:01:58 +03:00
|
|
|
+ aContentType + 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.
|
2016-07-14 10:01:58 +03:00
|
|
|
if (aInputStream) {
|
|
|
|
// 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();
|
2015-05-19 17:36:37 +03:00
|
|
|
|
2017-09-19 17:26:21 +03:00
|
|
|
mPostData->AppendStream(aInputStream);
|
2016-07-14 10:01:58 +03:00
|
|
|
mTotalLength += aInputStreamSize;
|
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);
|
2016-07-14 10:01:31 +03:00
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
FSMultipartFormData::GetEncodedSubmission(nsIURI* aURI,
|
2017-09-22 09:12:03 +03:00
|
|
|
nsIInputStream** aPostDataStream,
|
2018-02-26 22:43:45 +03:00
|
|
|
int64_t* aPostDataStreamLength,
|
|
|
|
nsCOMPtr<nsIURI>& aOutURI)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2018-02-26 22:43:45 +03:00
|
|
|
aOutURI = aURI;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
// 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());
|
2017-09-22 09:12:03 +03:00
|
|
|
|
|
|
|
uint64_t bodySize;
|
|
|
|
mimeStream->SetData(GetSubmissionBody(&bodySize));
|
|
|
|
*aPostDataStreamLength = bodySize;
|
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
|
2016-06-16 10:26:34 +03:00
|
|
|
FSMultipartFormData::AddPostDataStream()
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2016-02-24 02:01:01 +03:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
nsCOMPtr<nsIInputStream> postDataChunkStream;
|
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(postDataChunkStream),
|
|
|
|
mPostDataChunk);
|
|
|
|
NS_ASSERTION(postDataChunkStream, "Could not open a stream for POST!");
|
|
|
|
if (postDataChunkStream) {
|
2017-09-19 17:26:21 +03:00
|
|
|
mPostData->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
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
class FSTextPlain : public EncodingFormSubmission
|
2002-07-12 03:32:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2017-04-27 13:27:03 +03:00
|
|
|
FSTextPlain(NotNull<const Encoding*> aEncoding,
|
2017-12-07 21:13:50 +03:00
|
|
|
Element* aOriginatingElement)
|
2017-04-27 13:27:03 +03:00
|
|
|
: EncodingFormSubmission(aEncoding, aOriginatingElement)
|
2004-01-10 02:54:21 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
virtual nsresult
|
|
|
|
AddNameValuePair(const nsAString& aName, const nsAString& aValue) override;
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
AddNameBlobOrNullPair(const nsAString& aName, Blob* aBlob) override;
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
virtual nsresult
|
|
|
|
AddNameDirectoryPair(const nsAString& aName, Directory* aDirectory) override;
|
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
virtual nsresult
|
2017-09-22 09:12:03 +03:00
|
|
|
GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream,
|
2018-02-26 22:43:45 +03:00
|
|
|
int64_t* aPostDataStreaLength, nsCOMPtr<nsIURI>& aOutURI) override;
|
2002-07-12 03:32:13 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mBody;
|
|
|
|
};
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
FSTextPlain::AddNameValuePair(const nsAString& aName, const nsAString& aValue)
|
2002-07-12 03:32:13 +04:00
|
|
|
{
|
|
|
|
// 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
|
2016-06-16 10:26:34 +03:00
|
|
|
FSTextPlain::AddNameBlobOrNullPair(const nsAString& aName, Blob* aBlob)
|
2002-07-12 03:32:13 +04:00
|
|
|
{
|
2016-01-20 20:25:03 +03:00
|
|
|
nsAutoString filename;
|
|
|
|
RetrieveFileName(aBlob, filename);
|
2010-02-25 08:58:16 +03:00
|
|
|
AddNameValuePair(aName, filename);
|
2002-07-12 03:32:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
nsresult
|
|
|
|
FSTextPlain::AddNameDirectoryPair(const nsAString& aName,
|
|
|
|
Directory* aDirectory)
|
|
|
|
{
|
2016-07-14 10:01:58 +03:00
|
|
|
nsAutoString dirname;
|
|
|
|
RetrieveDirectoryName(aDirectory, dirname);
|
|
|
|
AddNameValuePair(aName, dirname);
|
2016-07-14 10:01:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
FSTextPlain::GetEncodedSubmission(nsIURI* aURI,
|
2017-09-22 09:12:03 +03:00
|
|
|
nsIInputStream** aPostDataStream,
|
2018-02-26 22:43:45 +03:00
|
|
|
int64_t* aPostDataStreamLength,
|
|
|
|
nsCOMPtr<nsIURI>& aOutURI)
|
2002-07-12 03:32:13 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2018-02-26 22:43:45 +03:00
|
|
|
aOutURI = aURI;
|
2002-07-12 03:32:13 +04:00
|
|
|
|
2017-09-22 09:12:03 +03:00
|
|
|
*aPostDataStream = nullptr;
|
|
|
|
*aPostDataStreamLength = -1;
|
|
|
|
|
2002-07-12 03:32:13 +04:00
|
|
|
// 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;
|
2017-07-29 14:50:21 +03:00
|
|
|
rv = aURI->GetPathQueryRef(path);
|
2002-07-12 03:32:13 +04:00
|
|
|
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
|
2016-05-18 19:21:56 +03:00
|
|
|
nsAutoCString escapedBody;
|
|
|
|
if (NS_WARN_IF(!NS_Escape(NS_ConvertUTF16toUTF8(mBody), escapedBody,
|
|
|
|
url_XAlphas))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2002-07-12 03:32:13 +04:00
|
|
|
|
|
|
|
path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;
|
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
rv = NS_MutateURI(aURI)
|
|
|
|
.SetPathQueryRef(path)
|
|
|
|
.Finalize(aOutURI);
|
2002-07-12 03:32:13 +04:00
|
|
|
} 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));
|
2018-04-11 17:06:17 +03:00
|
|
|
uint32_t bodyLength = cbody.Length();
|
2002-07-12 03:32:13 +04:00
|
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
2018-04-11 17:06:17 +03:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(bodyStream), Move(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");
|
|
|
|
mimeStream->SetData(bodyStream);
|
|
|
|
CallQueryInterface(mimeStream, aPostDataStream);
|
2017-09-22 09:12:03 +03:00
|
|
|
|
2018-04-11 17:06:17 +03:00
|
|
|
*aPostDataStreamLength = bodyLength;
|
2002-07-12 03:32:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
EncodingFormSubmission::EncodingFormSubmission(
|
|
|
|
NotNull<const Encoding*> aEncoding,
|
2017-12-07 21:13:50 +03:00
|
|
|
Element* aOriginatingElement)
|
2017-04-27 13:27:03 +03:00
|
|
|
: HTMLFormSubmission(aEncoding, aOriginatingElement)
|
2010-02-25 08:58:16 +03:00
|
|
|
{
|
2017-04-27 13:27:03 +03:00
|
|
|
if (!aEncoding->CanEncodeEverything()) {
|
|
|
|
nsAutoCString name;
|
|
|
|
aEncoding->Name(name);
|
|
|
|
NS_ConvertUTF8toUTF16 nameUtf16(name);
|
|
|
|
const char16_t* namePtr = nameUtf16.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",
|
2017-04-27 13:27:03 +03:00
|
|
|
&namePtr,
|
2012-01-02 18:18:30 +04:00
|
|
|
1);
|
|
|
|
}
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
2002-07-12 03:32:13 +04:00
|
|
|
|
2016-06-16 10:26:34 +03:00
|
|
|
EncodingFormSubmission::~EncodingFormSubmission()
|
2002-08-06 08:26:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// i18n helper routines
|
|
|
|
nsresult
|
2016-06-16 10:26:34 +03:00
|
|
|
EncodingFormSubmission::EncodeVal(const nsAString& aStr, nsCString& aOut,
|
|
|
|
bool aHeaderEncode)
|
2010-02-25 08:58:17 +03:00
|
|
|
{
|
2017-04-27 13:27:03 +03:00
|
|
|
nsresult rv;
|
|
|
|
const Encoding* ignored;
|
|
|
|
Tie(rv, ignored) = mEncoding->Encode(aStr, aOut);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2010-11-23 11:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
namespace {
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
NotNull<const Encoding*>
|
|
|
|
GetSubmitEncoding(nsGenericHTMLElement* aForm)
|
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);
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
auto encoding = Encoding::ForLabelNoReplacement(uCharset);
|
|
|
|
if (encoding) {
|
|
|
|
return WrapNotNull(encoding);
|
|
|
|
}
|
2012-03-05 07:57:51 +04:00
|
|
|
}
|
|
|
|
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) {
|
2017-06-18 14:37:50 +03:00
|
|
|
return doc->GetDocumentCharacterSet();
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2017-06-20 16:04:18 +03:00
|
|
|
return UTF_8_ENCODING;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
void
|
2010-02-25 08:58:16 +03:00
|
|
|
GetEnumAttr(nsGenericHTMLElement* aContent,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:25:12 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-06-16 10:25:48 +03:00
|
|
|
/* static */ nsresult
|
|
|
|
HTMLFormSubmission::GetFromForm(nsGenericHTMLElement* aForm,
|
|
|
|
nsGenericHTMLElement* aOriginatingElement,
|
|
|
|
HTMLFormSubmission** 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
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
// Get encoding
|
|
|
|
auto encoding = GetSubmitEncoding(aForm)->OutputEncoding();
|
2012-03-02 09:19:30 +04:00
|
|
|
|
2010-02-25 08:58:17 +03:00
|
|
|
// Choose encoder
|
|
|
|
if (method == NS_FORM_METHOD_POST &&
|
|
|
|
enctype == NS_FORM_ENCTYPE_MULTIPART) {
|
2017-04-27 13:27:03 +03:00
|
|
|
*aFormSubmission = new FSMultipartFormData(encoding, aOriginatingElement);
|
2010-02-25 08:58:17 +03:00
|
|
|
} else if (method == NS_FORM_METHOD_POST &&
|
|
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
2017-04-27 13:27:03 +03:00
|
|
|
*aFormSubmission = new FSTextPlain(encoding, 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);
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
*aFormSubmission =
|
|
|
|
new FSURLEncoded(encoding, method, doc, aOriginatingElement);
|
2010-02-25 08:58:17 +03:00
|
|
|
}
|
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
|
|
|
}
|
2016-06-16 10:24:16 +03:00
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|