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