mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
b9329817d7
- Bug 1164425: Cleanup interfaces of |ListenSocket|, r=kmachulis (1a158f323)
- Bug 1164417: Add |ConnectionOrientedSocket::PrepareAccept| for accepting socket connections, r=kmachulis (94c6234e0)
- Bug 1154281: Don't include UnixSocket.h in NFC, r=allstars.chh (c4eff006b)
- Bug 1146270 - Move HCI Event Origin into NfcOptions.webidl. r=smaug, dimi (1d7b28488)
- Bug 1127735 - Update TechDiscoveredNotication. r=smaug, dimi (c27f2350c)
- Bug 1152667 - Remove NfcGonkMessage.h r=dimi (c7651156f)
- Bug 1166638: Use |StreamSocketConsumer| in |StreamSocket|, r=kmachulis, dlee, chucklee, htsai (e885dd269)
- Bug 1166638: Use |ListenSocketConsumer| in |ListenSocket|, r=kmachulis, dlee, chucklee, shuang (53315fa5b)
- Bug 1152095 - Check mResultHandlerQ is empty before dispatching DispatchError. r=tzimmermann (c3a187ac4)
- Bug 1159685 - Fix BluetoothSspVariant argument passing for SspRequestNotification. r=shuang (ed398bcd4)
- Bug 1161003: Don't send PDU when connection to Bluetooth daemon is closed, r=shuang (5bb9c69d2)
- Bug 1167207: Remove |BluetoothDaemonChannel|, r=shuang (4470926f6)
- Bug 1167210: Replace |union sockaddr_any| with |struct sockaddr_storage|, r=kmachulis (69af8f130)
- Bug 1159709: Cleanup inherited methods of Bluedroid's |BluetoothSocket|, r=kmachulis (29a11ec67)
- Bug 1168446: Remove |BluetoothSocket::GetSocketAddr|, r=shuang (f1ba86da8)
- Bug 1168446: Change |BluetoothSocket::{ListenSocket,ConnectSocket}| interface, r=shuang (ef9644d68)
- Bug 1168446: Don't store connection parameters in |BluetoothSocket|, r=shuang (c8acbb880)
- Bug 1166231 - Make nsIDOMBlob an empty interface, r=ehsan (4fae64ce9)
- Bug 1176800 part 1. When getting all types, include the types Promises are parametrized over. r=peterv (8ddb8a0c6)
- Bug 1176800 part 2. Add a ToJSValue overload for owning unions. r=peterv (e0a3625d2)
- Bug 1177688, part 1 - Add API and functionality to the BlobImpl classes so that BlobImpl's that are created from an nsIFile can provide information about whether or not the nsIFile was a directory. r=baku (d45beda77)
- Bug 1177688, part 2 - Add support to the FileSystem code for obtaining a listing of a Directory's Directory and File contents via a sequence of Blobs, forking appropriately to support both for e10s and non-e10s. r=baku (d0e5e3205)
- Bug 1177688, part 3 - Implement .getFilesAndDirectories() and .path for DOM Directory objects. r=baku (f22f86472)
- Bug 1177688, part 4 - Add DeviceStorage tests for the new .getFilesAndDirectories() and .path API on Directory. r=baku (70607068a)
- Bug 1177688, part 5 - Follow-up to use nsAutoString on the stack in Directory to avoid unnecessarily allocating. r=baku (d400df943)
- Bug 1177688, part 6 - Follow-up to provide a default BlobDirState::eUnknownIfDir arg to BlobImplBase's ctor to avoid excessively specifying the same argument. r=baku (7779e7b2d)
- Bug 1097479 - Allow nested content process to embed apps and add a nested pref check function. r=kanru (b11bf2b80)
- Bug 1151495 - Support permission prompting from workers for IDB, r=bent (f3983875d)
- Bug 1173525 - Add JS::Construct overload for specifying new.target. (r=Waldo) (1067d0fba)
- Bug 1138198 - Remove unused/undefined member function ParticularProcessPriorityManager::OnChannelConnected. r=khuey (21a5c2590)
- Bug 852925 - Freeze priority changes when the screen is turned off. r=khuey (812a9e303)
- Bug 1137151: Marked destructor of |nsScreenManagerGonk| as protected, r=mwu (4943f388c)
- Bug 1157874 - Remove screen related global variables from nsWindow.cpp r=mwu (3bdc584ad)
- Bug 1157030 - Prevent visibility changes from affecting the process priority when the screen is off. r=khuey (6582a9303)
- Bug 1157030 - Stop special-casing the homescreen in the process priority manager. r=khuey, r=dhylands (bb97a1b61)
- Bug 1161748 - Rehydrate ActivityOptions in a system scope. r=fabrice,gabor (70dbc3b1b)
- Bug 1144132 - Adjust the priority of an activity opener so that it's above other background applications. r=fabrice (9789a87b1)
- Bug 1150315 - Keep SpeechSynthesis state in sync after successive cancel/speak calls. r=smaug (93abc527d)
- Bug 1154741 - Disable test_speech_cancel.html on b2g. r=me (4bcc59b0e)
- Bug 1155034 - Let indirect audio speech services send their own events. Make pause/resume IPC safe. r=smaug (b91bf210b)
- Bug 1086545 - Bind speech task streams to a parent stream held by voice registry. r=padenot (cef7476ec)
- Bug 1155917 - Added volume argument to nsISpeechService.speak(). r=smaug (a945c472a)
- Bug 1162699 - Replace mochitest test synth services with global services to simplify tests. r=smaug (7724212d9)
- Bug 1162699 followup: Add 'override' keyword to overriding methods on FakeSynthCallback. rs=ehsan (15377091b)
880 lines
27 KiB
C++
880 lines
27 KiB
C++
/* -*- 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
|
|
* 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/. */
|
|
|
|
#include "nsFormSubmission.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
#include "nsIForm.h"
|
|
#include "nsILinkHandler.h"
|
|
#include "nsIDocument.h"
|
|
#include "nsGkAtoms.h"
|
|
#include "nsIFormControl.h"
|
|
#include "nsIDOMHTMLFormElement.h"
|
|
#include "nsError.h"
|
|
#include "nsGenericHTMLElement.h"
|
|
#include "nsAttrValueInlines.h"
|
|
#include "nsISaveAsCharset.h"
|
|
#include "nsIFile.h"
|
|
#include "nsDirectoryServiceDefs.h"
|
|
#include "nsStringStream.h"
|
|
#include "nsIURI.h"
|
|
#include "nsIURL.h"
|
|
#include "nsNetUtil.h"
|
|
#include "nsLinebreakConverter.h"
|
|
#include "nsEscape.h"
|
|
#include "nsUnicharUtils.h"
|
|
#include "nsIMultiplexInputStream.h"
|
|
#include "nsIMIMEInputStream.h"
|
|
#include "nsIMIMEService.h"
|
|
#include "nsIConsoleService.h"
|
|
#include "nsIScriptError.h"
|
|
#include "nsIStringBundle.h"
|
|
#include "nsCExternalHandlerService.h"
|
|
#include "nsIFileStreams.h"
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
#include "mozilla/dom/File.h"
|
|
|
|
using namespace mozilla;
|
|
using mozilla::dom::EncodingUtils;
|
|
|
|
static void
|
|
SendJSWarning(nsIDocument* aDocument,
|
|
const char* aWarningName,
|
|
const char16_t** aWarningArgs, uint32_t aWarningArgsLen)
|
|
{
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
NS_LITERAL_CSTRING("HTML"), aDocument,
|
|
nsContentUtils::eFORMS_PROPERTIES,
|
|
aWarningName,
|
|
aWarningArgs, aWarningArgsLen);
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
class nsFSURLEncoded : public nsEncodingFormSubmission
|
|
{
|
|
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,
|
|
int32_t aMethod,
|
|
nsIDocument* aDocument,
|
|
nsIContent* aOriginatingElement)
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement),
|
|
mMethod(aMethod),
|
|
mDocument(aDocument),
|
|
mWarnedFileControl(false)
|
|
{
|
|
}
|
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
|
const nsAString& aValue) override;
|
|
virtual nsresult AddNameFilePair(const nsAString& aName,
|
|
mozilla::dom::File* aFile) override;
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
nsIInputStream** aPostDataStream)
|
|
override;
|
|
|
|
virtual bool SupportsIsindexSubmission() override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
virtual nsresult AddIsindex(const nsAString& aValue) override;
|
|
|
|
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).
|
|
*/
|
|
int32_t mMethod;
|
|
|
|
/** The query string so far (the part after the ?) */
|
|
nsCString mQueryString;
|
|
|
|
/** The document whose URI to use when reporting errors */
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
/** Whether or not we have warned about a file control not being submitted */
|
|
bool mWarnedFileControl;
|
|
};
|
|
|
|
nsresult
|
|
nsFSURLEncoded::AddNameValuePair(const nsAString& aName,
|
|
const nsAString& aValue)
|
|
{
|
|
// Encode value
|
|
nsCString convValue;
|
|
nsresult rv = URLEncode(aValue, convValue);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Encode name
|
|
nsAutoCString convName;
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
nsresult
|
|
nsFSURLEncoded::AddNameFilePair(const nsAString& aName,
|
|
mozilla::dom::File* aFile)
|
|
{
|
|
if (!mWarnedFileControl) {
|
|
SendJSWarning(mDocument, "ForgotFileEnctypeWarning", nullptr, 0);
|
|
mWarnedFileControl = true;
|
|
}
|
|
|
|
nsAutoString filename;
|
|
if (aFile) {
|
|
aFile->GetName(filename);
|
|
}
|
|
|
|
return AddNameValuePair(aName, filename);
|
|
}
|
|
|
|
static void
|
|
HandleMailtoSubject(nsCString& aPath) {
|
|
|
|
// Walk through the string and see if we have a subject already.
|
|
bool hasSubject = false;
|
|
bool hasParams = false;
|
|
int32_t paramSep = aPath.FindChar('?');
|
|
while (paramSep != kNotFound && paramSep < (int32_t)aPath.Length()) {
|
|
hasParams = true;
|
|
|
|
// 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
|
|
int32_t nameEnd = aPath.FindChar('=', paramSep+1);
|
|
int32_t nextParamSep = aPath.FindChar('&', paramSep+1);
|
|
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")) {
|
|
hasSubject = true;
|
|
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;
|
|
const char16_t *formatStrings[] = { brandName.get() };
|
|
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));
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
nsFSURLEncoded::GetEncodedSubmission(nsIURI* aURI,
|
|
nsIInputStream** aPostDataStream)
|
|
{
|
|
nsresult rv = NS_OK;
|
|
|
|
*aPostDataStream = nullptr;
|
|
|
|
if (mMethod == NS_FORM_METHOD_POST) {
|
|
|
|
bool isMailto = false;
|
|
aURI->SchemeIs("mailto", &isMailto);
|
|
if (isMailto) {
|
|
|
|
nsAutoCString path;
|
|
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
|
|
mimeStream->SetAddContentLength(true);
|
|
mimeStream->SetData(dataStream);
|
|
|
|
*aPostDataStream = mimeStream;
|
|
NS_ADDREF(*aPostDataStream);
|
|
}
|
|
|
|
} else {
|
|
// Get the full query string
|
|
bool schemeIsJavaScript;
|
|
rv = aURI->SchemeIs("javascript", &schemeIsJavaScript);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
if (schemeIsJavaScript) {
|
|
return NS_OK;
|
|
}
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
if (url) {
|
|
url->SetQuery(mQueryString);
|
|
}
|
|
else {
|
|
nsAutoCString path;
|
|
rv = aURI->GetPath(path);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
// Bug 42616: Trim off named anchor and save it to add later
|
|
int32_t namedAnchorPos = path.FindChar('#');
|
|
nsAutoCString namedAnchor;
|
|
if (kNotFound != namedAnchorPos) {
|
|
path.Right(namedAnchor, (path.Length() - namedAnchorPos));
|
|
path.Truncate(namedAnchorPos);
|
|
}
|
|
|
|
// Chop off old query string (bug 25330, 57333)
|
|
// Only do this for GET not POST (bug 41585)
|
|
int32_t queryStart = path.FindChar('?');
|
|
if (kNotFound != queryStart) {
|
|
path.Truncate(queryStart);
|
|
}
|
|
|
|
path.Append('?');
|
|
// Bug 42616: Add named anchor to end after query string
|
|
path.Append(mQueryString + namedAnchor);
|
|
|
|
aURI->SetPath(path);
|
|
}
|
|
}
|
|
|
|
return rv;
|
|
}
|
|
|
|
// i18n helper routines
|
|
nsresult
|
|
nsFSURLEncoded::URLEncode(const nsAString& aStr, nsCString& aEncoded)
|
|
{
|
|
// convert to CRLF breaks
|
|
char16_t* convertedBuf =
|
|
nsLinebreakConverter::ConvertUnicharLineBreaks(PromiseFlatString(aStr).get(),
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
nsLinebreakConverter::eLinebreakNet);
|
|
NS_ENSURE_TRUE(convertedBuf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
nsAutoCString encodedBuf;
|
|
nsresult rv = EncodeVal(nsDependentString(convertedBuf), encodedBuf, false);
|
|
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;
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
nsFSMultipartFormData::nsFSMultipartFormData(const nsACString& aCharset,
|
|
nsIContent* aOriginatingElement)
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement)
|
|
{
|
|
mPostDataStream =
|
|
do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1");
|
|
mTotalLength = 0;
|
|
|
|
mBoundary.AssignLiteral("---------------------------");
|
|
mBoundary.AppendInt(rand());
|
|
mBoundary.AppendInt(rand());
|
|
mBoundary.AppendInt(rand());
|
|
}
|
|
|
|
nsFSMultipartFormData::~nsFSMultipartFormData()
|
|
{
|
|
NS_ASSERTION(mPostDataChunk.IsEmpty(), "Left unsubmitted data");
|
|
}
|
|
|
|
nsIInputStream*
|
|
nsFSMultipartFormData::GetSubmissionBody(uint64_t* aContentLength)
|
|
{
|
|
// Finish data
|
|
mPostDataChunk += NS_LITERAL_CSTRING("--") + mBoundary
|
|
+ NS_LITERAL_CSTRING("--" CRLF);
|
|
|
|
// Add final data input stream
|
|
AddPostDataStream();
|
|
|
|
*aContentLength = mTotalLength;
|
|
return mPostDataStream;
|
|
}
|
|
|
|
nsresult
|
|
nsFSMultipartFormData::AddNameValuePair(const nsAString& aName,
|
|
const nsAString& aValue)
|
|
{
|
|
nsCString valueStr;
|
|
nsAutoCString encodedVal;
|
|
nsresult rv = EncodeVal(aValue, encodedVal, false);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
valueStr.Adopt(nsLinebreakConverter::
|
|
ConvertLineBreaks(encodedVal.get(),
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
nsLinebreakConverter::eLinebreakNet));
|
|
|
|
nsAutoCString nameStr;
|
|
rv = EncodeVal(aName, nameStr, true);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Make MIME block for name/value pair
|
|
|
|
// 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
|
|
+ NS_LITERAL_CSTRING(CRLF)
|
|
+ NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
|
|
+ nameStr + NS_LITERAL_CSTRING("\"" CRLF CRLF)
|
|
+ valueStr + NS_LITERAL_CSTRING(CRLF);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
nsFSMultipartFormData::AddNameFilePair(const nsAString& aName,
|
|
mozilla::dom::File* aFile)
|
|
{
|
|
// Encode the control name
|
|
nsAutoCString nameStr;
|
|
nsresult rv = EncodeVal(aName, nameStr, true);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCString filename, contentType;
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
|
if (aFile) {
|
|
nsAutoString filename16;
|
|
aFile->GetName(filename16);
|
|
|
|
ErrorResult error;
|
|
nsAutoString filepath16;
|
|
aFile->GetPath(filepath16, error);
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
return error.StealNSResult();
|
|
}
|
|
|
|
if (!filepath16.IsEmpty()) {
|
|
// File.path includes trailing "/"
|
|
filename16 = filepath16 + filename16;
|
|
}
|
|
|
|
rv = EncodeVal(filename16, filename, true);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Get content type
|
|
nsAutoString contentType16;
|
|
aFile->GetType(contentType16);
|
|
if (contentType16.IsEmpty()) {
|
|
contentType16.AssignLiteral("application/octet-stream");
|
|
}
|
|
contentType.Adopt(nsLinebreakConverter::
|
|
ConvertLineBreaks(NS_ConvertUTF16toUTF8(contentType16).get(),
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
nsLinebreakConverter::eLinebreakSpace));
|
|
|
|
// Get input stream
|
|
aFile->GetInternalStream(getter_AddRefs(fileStream), error);
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
return error.StealNSResult();
|
|
}
|
|
|
|
if (fileStream) {
|
|
// Create buffered stream (for efficiency)
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
|
fileStream, 8192);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
fileStream = bufferedStream;
|
|
}
|
|
}
|
|
else {
|
|
contentType.AssignLiteral("application/octet-stream");
|
|
}
|
|
|
|
//
|
|
// 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=\"")
|
|
+ filename + NS_LITERAL_CSTRING("\"" CRLF)
|
|
+ NS_LITERAL_CSTRING("Content-Type: ")
|
|
+ contentType + NS_LITERAL_CSTRING(CRLF CRLF);
|
|
|
|
// 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.
|
|
if (fileStream) {
|
|
ErrorResult error;
|
|
uint64_t size = aFile->GetSize(error);
|
|
if (error.Failed()) {
|
|
error.SuppressException();
|
|
} else {
|
|
// 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();
|
|
|
|
mPostDataStream->AppendStream(fileStream);
|
|
mTotalLength += size;
|
|
}
|
|
}
|
|
|
|
// CRLF after file
|
|
mPostDataChunk.AppendLiteral(CRLF);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
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);
|
|
|
|
nsAutoCString contentType;
|
|
GetContentType(contentType);
|
|
mimeStream->AddHeader("Content-Type", contentType.get());
|
|
mimeStream->SetAddContentLength(true);
|
|
uint64_t unused;
|
|
mimeStream->SetData(GetSubmissionBody(&unused));
|
|
|
|
mimeStream.forget(aPostDataStream);
|
|
|
|
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);
|
|
mTotalLength += mPostDataChunk.Length();
|
|
}
|
|
|
|
mPostDataChunk.Truncate();
|
|
|
|
return rv;
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
class nsFSTextPlain : public nsEncodingFormSubmission
|
|
{
|
|
public:
|
|
nsFSTextPlain(const nsACString& aCharset, nsIContent* aOriginatingElement)
|
|
: nsEncodingFormSubmission(aCharset, aOriginatingElement)
|
|
{
|
|
}
|
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
|
const nsAString& aValue) override;
|
|
virtual nsresult AddNameFilePair(const nsAString& aName,
|
|
mozilla::dom::File* aFile) override;
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
nsIInputStream** aPostDataStream)
|
|
override;
|
|
|
|
private:
|
|
nsString mBody;
|
|
};
|
|
|
|
nsresult
|
|
nsFSTextPlain::AddNameValuePair(const nsAString& aName,
|
|
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.
|
|
mBody.Append(aName + NS_LITERAL_STRING("=") + aValue +
|
|
NS_LITERAL_STRING(CRLF));
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
nsFSTextPlain::AddNameFilePair(const nsAString& aName,
|
|
mozilla::dom::File* aFile)
|
|
{
|
|
nsAutoString filename;
|
|
if (aFile) {
|
|
aFile->GetName(filename);
|
|
}
|
|
|
|
AddNameValuePair(aName, filename);
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
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.
|
|
bool isMailto = false;
|
|
aURI->SchemeIs("mailto", &isMailto);
|
|
if (isMailto) {
|
|
nsAutoCString path;
|
|
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 {
|
|
// 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));
|
|
nsCOMPtr<nsIInputStream> bodyStream;
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(bodyStream), cbody);
|
|
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->SetAddContentLength(true);
|
|
mimeStream->SetData(bodyStream);
|
|
CallQueryInterface(mimeStream, aPostDataStream);
|
|
}
|
|
|
|
return rv;
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
nsEncodingFormSubmission::nsEncodingFormSubmission(const nsACString& aCharset,
|
|
nsIContent* aOriginatingElement)
|
|
: nsFormSubmission(aCharset, aOriginatingElement)
|
|
{
|
|
nsAutoCString charset(aCharset);
|
|
// 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");
|
|
}
|
|
|
|
if (!(charset.EqualsLiteral("UTF-8") || charset.EqualsLiteral("gb18030"))) {
|
|
NS_ConvertUTF8toUTF16 charsetUtf16(charset);
|
|
const char16_t* charsetPtr = charsetUtf16.get();
|
|
SendJSWarning(aOriginatingElement ? aOriginatingElement->GetOwnerDocument()
|
|
: nullptr,
|
|
"CannotEncodeAllUnicode",
|
|
&charsetPtr,
|
|
1);
|
|
}
|
|
|
|
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)) {
|
|
mEncoder = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
nsEncodingFormSubmission::~nsEncodingFormSubmission()
|
|
{
|
|
}
|
|
|
|
// i18n helper routines
|
|
nsresult
|
|
nsEncodingFormSubmission::EncodeVal(const nsAString& aStr, nsCString& aOut,
|
|
bool aHeaderEncode)
|
|
{
|
|
if (mEncoder && !aStr.IsEmpty()) {
|
|
aOut.Truncate();
|
|
nsresult rv = mEncoder->Convert(PromiseFlatString(aStr).get(),
|
|
getter_Copies(aOut));
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
}
|
|
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("\\\""));
|
|
}
|
|
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
static void
|
|
GetSubmitCharset(nsGenericHTMLElement* aForm,
|
|
nsACString& oCharset)
|
|
{
|
|
oCharset.AssignLiteral("UTF-8"); // default to utf-8
|
|
|
|
nsAutoString acceptCharsetValue;
|
|
aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::acceptcharset,
|
|
acceptCharsetValue);
|
|
|
|
int32_t charsetLen = acceptCharsetValue.Length();
|
|
if (charsetLen > 0) {
|
|
int32_t offset=0;
|
|
int32_t spPos=0;
|
|
// get charset from charsets one by one
|
|
do {
|
|
spPos = acceptCharsetValue.FindChar(char16_t(' '), offset);
|
|
int32_t cnt = ((-1==spPos)?(charsetLen-offset):(spPos-offset));
|
|
if (cnt > 0) {
|
|
nsAutoString uCharset;
|
|
acceptCharsetValue.Mid(uCharset, offset, cnt);
|
|
|
|
if (EncodingUtils::FindEncodingForLabel(uCharset, oCharset))
|
|
return;
|
|
}
|
|
offset = spPos + 1;
|
|
} while (spPos != -1);
|
|
}
|
|
// if there are no accept-charset or all the charset are not supported
|
|
// Get the charset from document
|
|
nsIDocument* doc = aForm->GetComposedDoc();
|
|
if (doc) {
|
|
oCharset = doc->GetDocumentCharacterSet();
|
|
}
|
|
}
|
|
|
|
static void
|
|
GetEnumAttr(nsGenericHTMLElement* aContent,
|
|
nsIAtom* atom, int32_t* aValue)
|
|
{
|
|
const nsAttrValue* value = aContent->GetParsedAttr(atom);
|
|
if (value && value->Type() == nsAttrValue::eEnum) {
|
|
*aValue = value->GetEnumValue();
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
GetSubmissionFromForm(nsGenericHTMLElement* aForm,
|
|
nsGenericHTMLElement* aOriginatingElement,
|
|
nsFormSubmission** aFormSubmission)
|
|
{
|
|
// Get all the information necessary to encode the form data
|
|
NS_ASSERTION(aForm->GetComposedDoc(),
|
|
"Should have doc if we're building submission!");
|
|
|
|
// Get encoding type (default: urlencoded)
|
|
int32_t enctype = NS_FORM_ENCTYPE_URLENCODED;
|
|
if (aOriginatingElement &&
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formenctype)) {
|
|
GetEnumAttr(aOriginatingElement, nsGkAtoms::formenctype, &enctype);
|
|
} else {
|
|
GetEnumAttr(aForm, nsGkAtoms::enctype, &enctype);
|
|
}
|
|
|
|
// Get method (default: GET)
|
|
int32_t method = NS_FORM_METHOD_GET;
|
|
if (aOriginatingElement &&
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formmethod)) {
|
|
GetEnumAttr(aOriginatingElement, nsGkAtoms::formmethod, &method);
|
|
} else {
|
|
GetEnumAttr(aForm, nsGkAtoms::method, &method);
|
|
}
|
|
|
|
// Get charset
|
|
nsAutoCString charset;
|
|
GetSubmitCharset(aForm, charset);
|
|
|
|
// 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");
|
|
}
|
|
|
|
// Choose encoder
|
|
if (method == NS_FORM_METHOD_POST &&
|
|
enctype == NS_FORM_ENCTYPE_MULTIPART) {
|
|
*aFormSubmission = new nsFSMultipartFormData(charset, aOriginatingElement);
|
|
} else if (method == NS_FORM_METHOD_POST &&
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
|
*aFormSubmission = new nsFSTextPlain(charset, aOriginatingElement);
|
|
} else {
|
|
nsIDocument* doc = aForm->OwnerDoc();
|
|
if (enctype == NS_FORM_ENCTYPE_MULTIPART ||
|
|
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
|
|
nsAutoString enctypeStr;
|
|
if (aOriginatingElement &&
|
|
aOriginatingElement->HasAttr(kNameSpaceID_None,
|
|
nsGkAtoms::formenctype)) {
|
|
aOriginatingElement->GetAttr(kNameSpaceID_None, nsGkAtoms::formenctype,
|
|
enctypeStr);
|
|
} else {
|
|
aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::enctype, enctypeStr);
|
|
}
|
|
const char16_t* enctypeStrPtr = enctypeStr.get();
|
|
SendJSWarning(doc, "ForgotPostWarning",
|
|
&enctypeStrPtr, 1);
|
|
}
|
|
*aFormSubmission = new nsFSURLEncoded(charset, method, doc,
|
|
aOriginatingElement);
|
|
}
|
|
NS_ENSURE_TRUE(*aFormSubmission, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NS_OK;
|
|
}
|