mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-09 09:18:42 +09:00
Issue #1442 - Part 10a - Unify body extraction in Fetch/Beacon/XHR. https://bugzilla.mozilla.org/show_bug.cgi?id=1329298 Pre-requisite for Part 11.
This commit is contained in:
parent
3979e4847c
commit
a4146b60a4
15 changed files with 439 additions and 447 deletions
236
dom/fetch/BodyExtractor.cpp
Normal file
236
dom/fetch/BodyExtractor.cpp
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
/* -*- 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 "BodyExtractor.h"
|
||||
#include "mozilla/dom/EncodingUtils.h"
|
||||
#include "mozilla/dom/File.h"
|
||||
#include "mozilla/dom/FormData.h"
|
||||
#include "mozilla/dom/TypedArray.h"
|
||||
#include "mozilla/dom/URLSearchParams.h"
|
||||
#include "mozilla/dom/XMLHttpRequest.h"
|
||||
#include "nsContentUtils.h"
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMSerializer.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIOutputStream.h"
|
||||
#include "nsIStorageStream.h"
|
||||
#include "nsStringStream.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
static nsresult
|
||||
GetBufferDataAsStream(const uint8_t* aData, uint32_t aDataLength,
|
||||
nsIInputStream** aResult, uint64_t* aContentLength,
|
||||
nsACString& aContentType, nsACString& aCharset)
|
||||
{
|
||||
aContentType.SetIsVoid(true);
|
||||
aCharset.Truncate();
|
||||
|
||||
*aContentLength = aDataLength;
|
||||
const char* data = reinterpret_cast<const char*>(aData);
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), data, aDataLength,
|
||||
NS_ASSIGNMENT_COPY);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
stream.forget(aResult);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<const ArrayBuffer>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
mBody->ComputeLengthAndData();
|
||||
return GetBufferDataAsStream(mBody->Data(), mBody->Length(),
|
||||
aResult, aContentLength, aContentTypeWithCharset,
|
||||
aCharset);
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<const ArrayBufferView>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
mBody->ComputeLengthAndData();
|
||||
return GetBufferDataAsStream(mBody->Data(), mBody->Length(),
|
||||
aResult, aContentLength, aContentTypeWithCharset,
|
||||
aCharset);
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<nsIDocument>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(mBody));
|
||||
NS_ENSURE_STATE(domdoc);
|
||||
aCharset.AssignLiteral("UTF-8");
|
||||
|
||||
nsresult rv;
|
||||
nsCOMPtr<nsIStorageStream> storStream;
|
||||
rv = NS_NewStorageStream(4096, UINT32_MAX, getter_AddRefs(storStream));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIOutputStream> output;
|
||||
rv = storStream->GetOutputStream(0, getter_AddRefs(output));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
if (mBody->IsHTMLDocument()) {
|
||||
aContentTypeWithCharset.AssignLiteral("text/html;charset=UTF-8");
|
||||
|
||||
nsString serialized;
|
||||
if (!nsContentUtils::SerializeNodeToMarkup(mBody, true, serialized)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
nsAutoCString utf8Serialized;
|
||||
if (!AppendUTF16toUTF8(serialized, utf8Serialized, fallible)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
uint32_t written;
|
||||
rv = output->Write(utf8Serialized.get(), utf8Serialized.Length(), &written);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
MOZ_ASSERT(written == utf8Serialized.Length());
|
||||
} else {
|
||||
aContentTypeWithCharset.AssignLiteral("application/xml;charset=UTF-8");
|
||||
|
||||
nsCOMPtr<nsIDOMSerializer> serializer =
|
||||
do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
// Make sure to use the encoding we'll send
|
||||
rv = serializer->SerializeToStream(domdoc, output, aCharset);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
output->Close();
|
||||
|
||||
uint32_t length;
|
||||
rv = storStream->GetLength(&length);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
*aContentLength = length;
|
||||
|
||||
rv = storStream->NewInputStream(0, aResult);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<const nsAString>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
nsCOMPtr<nsIUnicodeEncoder> encoder =
|
||||
EncodingUtils::EncoderForEncoding("UTF-8");
|
||||
if (!encoder) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
int32_t destBufferLen;
|
||||
nsresult rv = encoder->GetMaxLength(mBody->BeginReading(), mBody->Length(),
|
||||
&destBufferLen);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsCString encoded;
|
||||
if (!encoded.SetCapacity(destBufferLen, fallible)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
char* destBuffer = encoded.BeginWriting();
|
||||
int32_t srcLen = (int32_t) mBody->Length();
|
||||
int32_t outLen = destBufferLen;
|
||||
rv = encoder->Convert(mBody->BeginReading(), &srcLen, destBuffer, &outLen);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(outLen <= destBufferLen);
|
||||
encoded.SetLength(outLen);
|
||||
|
||||
rv = NS_NewCStringInputStream(aResult, encoded);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
*aContentLength = outLen;
|
||||
aContentTypeWithCharset.AssignLiteral("text/plain;charset=UTF-8");
|
||||
aCharset.AssignLiteral("UTF-8");
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<nsIInputStream>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
aContentTypeWithCharset.AssignLiteral("text/plain");
|
||||
aCharset.Truncate();
|
||||
|
||||
nsresult rv = mBody->Available(aContentLength);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream(mBody);
|
||||
stream.forget(aResult);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<Blob>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
return mBody->GetSendInfo(aResult, aContentLength, aContentTypeWithCharset,
|
||||
aCharset);
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<FormData>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
return mBody->GetSendInfo(aResult, aContentLength, aContentTypeWithCharset,
|
||||
aCharset);
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<URLSearchParams>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
return mBody->GetSendInfo(aResult, aContentLength, aContentTypeWithCharset,
|
||||
aCharset);
|
||||
}
|
||||
|
||||
template<> nsresult
|
||||
BodyExtractor<nsIXHRSendable>::GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const
|
||||
{
|
||||
return mBody->GetSendInfo(aResult, aContentLength, aContentTypeWithCharset,
|
||||
aCharset);
|
||||
}
|
||||
|
||||
} // dom namespace
|
||||
} // mozilla namespace
|
||||
46
dom/fetch/BodyExtractor.h
Normal file
46
dom/fetch/BodyExtractor.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* -*- 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/. */
|
||||
|
||||
#ifndef mozilla_dom_BodyExtractor_h
|
||||
#define mozilla_dom_BodyExtractor_h
|
||||
|
||||
#include "nsString.h"
|
||||
|
||||
class nsIInputStream;
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
class BodyExtractorBase
|
||||
{
|
||||
public:
|
||||
virtual nsresult GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const = 0;
|
||||
};
|
||||
|
||||
// The implementation versions of this template are:
|
||||
// ArrayBuffer, ArrayBufferView, Blob, FormData, nsAString, nsIDocument,
|
||||
// nsIInputStream, nsIXHRSendable, URLSearchParams
|
||||
template<typename Type>
|
||||
class BodyExtractor final : public BodyExtractorBase
|
||||
{
|
||||
Type* mBody;
|
||||
public:
|
||||
explicit BodyExtractor(Type* aBody) : mBody(aBody)
|
||||
{}
|
||||
|
||||
nsresult GetAsStream(nsIInputStream** aResult,
|
||||
uint64_t* aContentLength,
|
||||
nsACString& aContentTypeWithCharset,
|
||||
nsACString& aCharset) const override;
|
||||
};
|
||||
|
||||
} // dom namespace
|
||||
} // mozilla namespace
|
||||
|
||||
#endif // mozilla_dom_BodyExtractor_h
|
||||
|
|
@ -12,7 +12,6 @@
|
|||
#include "nsIStreamLoader.h"
|
||||
#include "nsIThreadRetargetableRequest.h"
|
||||
#include "nsIUnicodeDecoder.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
|
||||
#include "nsDOMString.h"
|
||||
#include "nsNetUtil.h"
|
||||
|
|
@ -24,7 +23,6 @@
|
|||
#include "mozilla/ErrorResult.h"
|
||||
#include "mozilla/dom/BodyUtil.h"
|
||||
#include "mozilla/dom/DOMError.h"
|
||||
#include "mozilla/dom/EncodingUtils.h"
|
||||
#include "mozilla/dom/Exceptions.h"
|
||||
#include "mozilla/dom/FetchDriver.h"
|
||||
#include "mozilla/dom/File.h"
|
||||
|
|
@ -40,6 +38,7 @@
|
|||
#include "mozilla/dom/WorkerPrivate.h"
|
||||
#include "mozilla/dom/workers/ServiceWorkerManager.h"
|
||||
|
||||
#include "BodyExtractor.h"
|
||||
#include "FetchObserver.h"
|
||||
#include "InternalRequest.h"
|
||||
#include "InternalResponse.h"
|
||||
|
|
@ -725,154 +724,48 @@ WorkerFetchResolver::FlushConsoleReport()
|
|||
mReporter->FlushConsoleReports(worker->GetDocument());
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
nsresult
|
||||
ExtractFromArrayBuffer(const ArrayBuffer& aBuffer,
|
||||
nsIInputStream** aStream,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
aBuffer.ComputeLengthAndData();
|
||||
aContentLength = aBuffer.Length();
|
||||
//XXXnsm reinterpret_cast<> is used in DOMParser, should be ok.
|
||||
return NS_NewByteInputStream(aStream,
|
||||
reinterpret_cast<char*>(aBuffer.Data()),
|
||||
aBuffer.Length(), NS_ASSIGNMENT_COPY);
|
||||
}
|
||||
|
||||
nsresult
|
||||
ExtractFromArrayBufferView(const ArrayBufferView& aBuffer,
|
||||
nsIInputStream** aStream,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
aBuffer.ComputeLengthAndData();
|
||||
aContentLength = aBuffer.Length();
|
||||
//XXXnsm reinterpret_cast<> is used in DOMParser, should be ok.
|
||||
return NS_NewByteInputStream(aStream,
|
||||
reinterpret_cast<char*>(aBuffer.Data()),
|
||||
aBuffer.Length(), NS_ASSIGNMENT_COPY);
|
||||
}
|
||||
|
||||
nsresult
|
||||
ExtractFromBlob(const Blob& aBlob,
|
||||
nsIInputStream** aStream,
|
||||
nsCString& aContentType,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
RefPtr<BlobImpl> impl = aBlob.Impl();
|
||||
ErrorResult rv;
|
||||
aContentLength = impl->GetSize(rv);
|
||||
if (NS_WARN_IF(rv.Failed())) {
|
||||
return rv.StealNSResult();
|
||||
}
|
||||
|
||||
impl->GetInternalStream(aStream, rv);
|
||||
if (NS_WARN_IF(rv.Failed())) {
|
||||
return rv.StealNSResult();
|
||||
}
|
||||
|
||||
nsAutoString type;
|
||||
impl->GetType(type);
|
||||
aContentType = NS_ConvertUTF16toUTF8(type);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
ExtractFromFormData(FormData& aFormData,
|
||||
nsIInputStream** aStream,
|
||||
nsCString& aContentType,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
nsAutoCString unusedCharset;
|
||||
return aFormData.GetSendInfo(aStream, &aContentLength,
|
||||
aContentType, unusedCharset);
|
||||
}
|
||||
|
||||
nsresult
|
||||
ExtractFromUSVString(const nsString& aStr,
|
||||
nsIInputStream** aStream,
|
||||
nsCString& aContentType,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
nsCOMPtr<nsIUnicodeEncoder> encoder = EncodingUtils::EncoderForEncoding("UTF-8");
|
||||
if (!encoder) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
int32_t destBufferLen;
|
||||
nsresult rv = encoder->GetMaxLength(aStr.get(), aStr.Length(), &destBufferLen);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsCString encoded;
|
||||
if (!encoded.SetCapacity(destBufferLen, fallible)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
char* destBuffer = encoded.BeginWriting();
|
||||
int32_t srcLen = (int32_t) aStr.Length();
|
||||
int32_t outLen = destBufferLen;
|
||||
rv = encoder->Convert(aStr.get(), &srcLen, destBuffer, &outLen);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(outLen <= destBufferLen);
|
||||
encoded.SetLength(outLen);
|
||||
|
||||
aContentType = NS_LITERAL_CSTRING("text/plain;charset=UTF-8");
|
||||
aContentLength = outLen;
|
||||
|
||||
return NS_NewCStringInputStream(aStream, encoded);
|
||||
}
|
||||
|
||||
nsresult
|
||||
ExtractFromURLSearchParams(const URLSearchParams& aParams,
|
||||
nsIInputStream** aStream,
|
||||
nsCString& aContentType,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
nsAutoString serialized;
|
||||
aParams.Stringify(serialized);
|
||||
aContentType = NS_LITERAL_CSTRING("application/x-www-form-urlencoded;charset=UTF-8");
|
||||
aContentLength = serialized.Length();
|
||||
return NS_NewCStringInputStream(aStream, NS_ConvertUTF16toUTF8(serialized));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
nsresult
|
||||
ExtractByteStreamFromBody(const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& aBodyInit,
|
||||
nsIInputStream** aStream,
|
||||
nsCString& aContentType,
|
||||
nsCString& aContentTypeWithCharset,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
MOZ_ASSERT(aStream);
|
||||
nsAutoCString charset;
|
||||
aContentTypeWithCharset.SetIsVoid(true);
|
||||
|
||||
if (aBodyInit.IsArrayBuffer()) {
|
||||
const ArrayBuffer& buf = aBodyInit.GetAsArrayBuffer();
|
||||
return ExtractFromArrayBuffer(buf, aStream, aContentLength);
|
||||
BodyExtractor<const ArrayBuffer> body(&aBodyInit.GetAsArrayBuffer());
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsArrayBufferView()) {
|
||||
const ArrayBufferView& buf = aBodyInit.GetAsArrayBufferView();
|
||||
return ExtractFromArrayBufferView(buf, aStream, aContentLength);
|
||||
BodyExtractor<const ArrayBufferView> body(&aBodyInit.GetAsArrayBufferView());
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsBlob()) {
|
||||
const Blob& blob = aBodyInit.GetAsBlob();
|
||||
return ExtractFromBlob(blob, aStream, aContentType, aContentLength);
|
||||
Blob& blob = aBodyInit.GetAsBlob();
|
||||
BodyExtractor<Blob> body(&blob);
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsFormData()) {
|
||||
FormData& form = aBodyInit.GetAsFormData();
|
||||
return ExtractFromFormData(form, aStream, aContentType, aContentLength);
|
||||
FormData& formData = aBodyInit.GetAsFormData();
|
||||
BodyExtractor<FormData> body(&formData);
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsUSVString()) {
|
||||
nsAutoString str;
|
||||
str.Assign(aBodyInit.GetAsUSVString());
|
||||
return ExtractFromUSVString(str, aStream, aContentType, aContentLength);
|
||||
BodyExtractor<const nsAString> body(&aBodyInit.GetAsUSVString());
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsURLSearchParams()) {
|
||||
URLSearchParams& params = aBodyInit.GetAsURLSearchParams();
|
||||
return ExtractFromURLSearchParams(params, aStream, aContentType, aContentLength);
|
||||
URLSearchParams& usp = aBodyInit.GetAsURLSearchParams();
|
||||
BodyExtractor<URLSearchParams> body(&usp);
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
|
||||
NS_NOTREACHED("Should never reach here");
|
||||
|
|
@ -882,36 +775,47 @@ ExtractByteStreamFromBody(const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDa
|
|||
nsresult
|
||||
ExtractByteStreamFromBody(const ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& aBodyInit,
|
||||
nsIInputStream** aStream,
|
||||
nsCString& aContentType,
|
||||
nsCString& aContentTypeWithCharset,
|
||||
uint64_t& aContentLength)
|
||||
{
|
||||
MOZ_ASSERT(aStream);
|
||||
MOZ_ASSERT(!*aStream);
|
||||
|
||||
nsAutoCString charset;
|
||||
aContentTypeWithCharset.SetIsVoid(true);
|
||||
|
||||
if (aBodyInit.IsArrayBuffer()) {
|
||||
const ArrayBuffer& buf = aBodyInit.GetAsArrayBuffer();
|
||||
return ExtractFromArrayBuffer(buf, aStream, aContentLength);
|
||||
BodyExtractor<const ArrayBuffer> body(&aBodyInit.GetAsArrayBuffer());
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsArrayBufferView()) {
|
||||
const ArrayBufferView& buf = aBodyInit.GetAsArrayBufferView();
|
||||
return ExtractFromArrayBufferView(buf, aStream, aContentLength);
|
||||
BodyExtractor<const ArrayBufferView> body(&aBodyInit.GetAsArrayBufferView());
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsBlob()) {
|
||||
const Blob& blob = aBodyInit.GetAsBlob();
|
||||
return ExtractFromBlob(blob, aStream, aContentType, aContentLength);
|
||||
Blob& blob = aBodyInit.GetAsBlob();
|
||||
BodyExtractor<Blob> body(&blob);
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsFormData()) {
|
||||
FormData& form = aBodyInit.GetAsFormData();
|
||||
return ExtractFromFormData(form, aStream, aContentType, aContentLength);
|
||||
FormData& formData = aBodyInit.GetAsFormData();
|
||||
BodyExtractor<FormData> body(&formData);
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsUSVString()) {
|
||||
nsAutoString str;
|
||||
str.Assign(aBodyInit.GetAsUSVString());
|
||||
return ExtractFromUSVString(str, aStream, aContentType, aContentLength);
|
||||
BodyExtractor<const nsAString> body(&aBodyInit.GetAsUSVString());
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
if (aBodyInit.IsURLSearchParams()) {
|
||||
URLSearchParams& params = aBodyInit.GetAsURLSearchParams();
|
||||
return ExtractFromURLSearchParams(params, aStream, aContentType, aContentLength);
|
||||
URLSearchParams& usp = aBodyInit.GetAsURLSearchParams();
|
||||
BodyExtractor<URLSearchParams> body(&usp);
|
||||
return body.GetAsStream(aStream, &aContentLength, aContentTypeWithCharset,
|
||||
charset);
|
||||
}
|
||||
|
||||
NS_NOTREACHED("Should never reach here");
|
||||
|
|
|
|||
|
|
@ -580,11 +580,11 @@ Request::Constructor(const GlobalObject& aGlobal,
|
|||
const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& bodyInit =
|
||||
bodyInitNullable.Value();
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
nsAutoCString contentType;
|
||||
nsAutoCString contentTypeWithCharset;
|
||||
uint64_t contentLengthUnused;
|
||||
aRv = ExtractByteStreamFromBody(bodyInit,
|
||||
getter_AddRefs(stream),
|
||||
contentType,
|
||||
contentTypeWithCharset,
|
||||
contentLengthUnused);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
|
|
@ -592,10 +592,10 @@ Request::Constructor(const GlobalObject& aGlobal,
|
|||
|
||||
temporaryBody = stream;
|
||||
|
||||
if (!contentType.IsVoid() &&
|
||||
if (!contentTypeWithCharset.IsVoid() &&
|
||||
!requestHeaders->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
|
||||
requestHeaders->Append(NS_LITERAL_CSTRING("Content-Type"),
|
||||
contentType, aRv);
|
||||
contentTypeWithCharset, aRv);
|
||||
}
|
||||
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
|
|
|
|||
|
|
@ -224,22 +224,23 @@ Response::Constructor(const GlobalObject& aGlobal,
|
|||
}
|
||||
|
||||
nsCOMPtr<nsIInputStream> bodyStream;
|
||||
nsCString contentType;
|
||||
nsCString contentTypeWithCharset;
|
||||
uint64_t bodySize = 0;
|
||||
aRv = ExtractByteStreamFromBody(aBody.Value().Value(),
|
||||
getter_AddRefs(bodyStream),
|
||||
contentType,
|
||||
contentTypeWithCharset,
|
||||
bodySize);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
}
|
||||
internalResponse->SetBody(bodyStream, bodySize);
|
||||
|
||||
if (!contentType.IsVoid() &&
|
||||
if (!contentTypeWithCharset.IsVoid() &&
|
||||
!internalResponse->Headers()->Has(NS_LITERAL_CSTRING("Content-Type"), aRv)) {
|
||||
// Ignore Append() failing here.
|
||||
ErrorResult error;
|
||||
internalResponse->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"), contentType, error);
|
||||
internalResponse->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"),
|
||||
contentTypeWithCharset, error);
|
||||
error.SuppressException();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
EXPORTS.mozilla.dom += [
|
||||
'BodyExtractor.h',
|
||||
'ChannelInfo.h',
|
||||
'Fetch.h',
|
||||
'FetchDriver.h',
|
||||
|
|
@ -19,6 +20,7 @@ EXPORTS.mozilla.dom += [
|
|||
]
|
||||
|
||||
UNIFIED_SOURCES += [
|
||||
'BodyExtractor.cpp',
|
||||
'ChannelInfo.cpp',
|
||||
'Fetch.cpp',
|
||||
'FetchConsumer.cpp',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue