2015-03-02 16:20:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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 "mozilla/dom/cache/TypeUtils.h"
|
|
|
|
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
|
|
|
#include "mozilla/dom/InternalRequest.h"
|
|
|
|
#include "mozilla/dom/Request.h"
|
|
|
|
#include "mozilla/dom/Response.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/ReadStream.h"
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PFileDescriptorSetChild.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2015-06-17 03:39:05 +03:00
|
|
|
#include "nsPromiseFlatString.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsURLParsers.h"
|
2015-04-07 16:13:05 +03:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsHttp.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
using mozilla::ipc::AutoIPCStream;
|
2015-03-22 09:52:12 +03:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
using mozilla::ipc::FileDescriptor;
|
|
|
|
using mozilla::ipc::PBackgroundChild;
|
|
|
|
using mozilla::ipc::PFileDescriptorSetChild;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
namespace {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-07 16:13:05 +03:00
|
|
|
static bool
|
|
|
|
HasVaryStar(mozilla::dom::InternalHeaders* aHeaders)
|
|
|
|
{
|
2016-10-10 20:20:14 +03:00
|
|
|
nsCString varyHeaders;
|
2015-04-07 16:13:05 +03:00
|
|
|
ErrorResult rv;
|
2016-10-10 20:20:14 +03:00
|
|
|
aHeaders->Get(NS_LITERAL_CSTRING("vary"), varyHeaders, rv);
|
2015-04-07 16:13:05 +03:00
|
|
|
MOZ_ALWAYS_TRUE(!rv.Failed());
|
|
|
|
|
2016-10-10 20:20:14 +03:00
|
|
|
char* rawBuffer = varyHeaders.BeginWriting();
|
|
|
|
char* token = nsCRT::strtok(rawBuffer, NS_HTTP_HEADER_SEPS, &rawBuffer);
|
|
|
|
for (; token;
|
|
|
|
token = nsCRT::strtok(rawBuffer, NS_HTTP_HEADER_SEPS, &rawBuffer)) {
|
|
|
|
nsDependentCString header(token);
|
|
|
|
if (header.EqualsLiteral("*")) {
|
|
|
|
return true;
|
2015-04-07 16:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void
|
|
|
|
ToHeadersEntryList(nsTArray<HeadersEntry>& aOut, InternalHeaders* aHeaders)
|
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aHeaders);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<InternalHeaders::Entry, 16> entryList;
|
2015-04-16 22:00:15 +03:00
|
|
|
aHeaders->GetEntries(entryList);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < entryList.Length(); ++i) {
|
|
|
|
InternalHeaders::Entry& entry = entryList[i];
|
|
|
|
aOut.AppendElement(HeadersEntry(entry.mName, entry.mValue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
already_AddRefed<InternalRequest>
|
2017-08-11 04:04:55 +03:00
|
|
|
TypeUtils::ToInternalRequest(JSContext* aCx, const RequestOrUSVString& aIn,
|
2015-03-02 16:20:00 +03:00
|
|
|
BodyAction aBodyAction, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aIn.IsRequest()) {
|
|
|
|
Request& request = aIn.GetAsRequest();
|
|
|
|
|
|
|
|
// Check and set bodyUsed flag immediately because its on Request
|
|
|
|
// instead of InternalRequest.
|
2017-08-11 04:04:55 +03:00
|
|
|
CheckAndSetBodyUsed(aCx, &request, aBodyAction, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (aRv.Failed()) { return nullptr; }
|
|
|
|
|
|
|
|
return request.GetInternalRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToInternalRequest(aIn.GetAsUSVString(), aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<InternalRequest>
|
2017-08-11 04:04:55 +03:00
|
|
|
TypeUtils::ToInternalRequest(JSContext* aCx,
|
|
|
|
const OwningRequestOrUSVString& aIn,
|
2015-03-02 16:20:00 +03:00
|
|
|
BodyAction aBodyAction, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (aIn.IsRequest()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> request = aIn.GetAsRequest().get();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// Check and set bodyUsed flag immediately because its on Request
|
|
|
|
// instead of InternalRequest.
|
2017-08-11 04:04:55 +03:00
|
|
|
CheckAndSetBodyUsed(aCx, request, aBodyAction, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (aRv.Failed()) { return nullptr; }
|
|
|
|
|
|
|
|
return request->GetInternalRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToInternalRequest(aIn.GetAsUSVString(), aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToCacheRequest(CacheRequest& aOut, InternalRequest* aIn,
|
2015-04-29 18:59:43 +03:00
|
|
|
BodyAction aBodyAction, SchemeAction aSchemeAction,
|
2016-05-15 20:32:09 +03:00
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
2015-04-29 18:59:43 +03:00
|
|
|
ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aIn);
|
2015-03-02 16:20:00 +03:00
|
|
|
aIn->GetMethod(aOut.method());
|
2016-11-07 05:16:34 +03:00
|
|
|
nsCString url(aIn->GetURLWithoutFragment());
|
2015-03-02 16:20:00 +03:00
|
|
|
bool schemeValid;
|
2015-06-17 03:39:05 +03:00
|
|
|
ProcessURL(url, &schemeValid, &aOut.urlWithoutQuery(), &aOut.urlQuery(), aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!schemeValid) {
|
|
|
|
if (aSchemeAction == TypeErrorOnInvalidScheme) {
|
2015-06-17 03:39:05 +03:00
|
|
|
NS_ConvertUTF8toUTF16 urlUTF16(url);
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL_SCHEME>(NS_LITERAL_STRING("Request"),
|
|
|
|
urlUTF16);
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 05:16:34 +03:00
|
|
|
aOut.urlFragment() = aIn->GetFragment();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
aIn->GetReferrer(aOut.referrer());
|
2016-02-27 22:08:58 +03:00
|
|
|
aOut.referrerPolicy() = aIn->ReferrerPolicy_();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> headers = aIn->Headers();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(headers);
|
2015-04-16 22:00:15 +03:00
|
|
|
ToHeadersEntryList(aOut.headers(), headers);
|
2015-03-02 16:20:00 +03:00
|
|
|
aOut.headersGuard() = headers->Guard();
|
|
|
|
aOut.mode() = aIn->Mode();
|
|
|
|
aOut.credentials() = aIn->GetCredentialsMode();
|
2015-03-25 23:38:42 +03:00
|
|
|
aOut.contentPolicyType() = aIn->ContentPolicyType();
|
2015-03-22 20:12:43 +03:00
|
|
|
aOut.requestCache() = aIn->GetCacheMode();
|
2015-09-01 00:26:30 +03:00
|
|
|
aOut.requestRedirect() = aIn->GetRedirectMode();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
aOut.integrity() = aIn->GetIntegrity();
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
if (aBodyAction == IgnoreBody) {
|
|
|
|
aOut.body() = void_t();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BodyUsed flag is checked and set previously in ToInternalRequest()
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
aIn->GetBody(getter_AddRefs(stream));
|
2016-05-15 20:32:09 +03:00
|
|
|
SerializeCacheStream(stream, &aOut.body(), aStreamCleanupList, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToCacheResponseWithoutBody(CacheResponse& aOut,
|
|
|
|
InternalResponse& aIn, ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
aOut.type() = aIn.Type();
|
|
|
|
|
2016-05-03 04:48:40 +03:00
|
|
|
aIn.GetUnfilteredURLList(aOut.urlList());
|
|
|
|
AutoTArray<nsCString, 4> urlList;
|
|
|
|
aIn.GetURLList(urlList);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-05-03 04:48:40 +03:00
|
|
|
for (uint32_t i = 0; i < aOut.urlList().Length(); i++) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!aOut.urlList()[i].IsEmpty());
|
2015-03-02 16:20:00 +03:00
|
|
|
// Pass all Response URL schemes through... The spec only requires we take
|
|
|
|
// action on invalid schemes for Request objects.
|
2016-05-03 04:48:40 +03:00
|
|
|
ProcessURL(aOut.urlList()[i], nullptr, nullptr, nullptr, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:30 +03:00
|
|
|
aOut.status() = aIn.GetUnfilteredStatus();
|
|
|
|
aOut.statusText() = aIn.GetUnfilteredStatusText();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> headers = aIn.UnfilteredHeaders();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(headers);
|
2015-04-07 16:13:05 +03:00
|
|
|
if (HasVaryStar(headers)) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_RESPONSE_HAS_VARY_STAR>();
|
2015-04-07 16:13:05 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
ToHeadersEntryList(aOut.headers(), headers);
|
2015-03-02 16:20:00 +03:00
|
|
|
aOut.headersGuard() = headers->Guard();
|
2015-05-25 21:21:05 +03:00
|
|
|
aOut.channelInfo() = aIn.GetChannelInfo().AsIPCChannelInfo();
|
2015-06-05 07:39:34 +03:00
|
|
|
if (aIn.GetPrincipalInfo()) {
|
|
|
|
aOut.principalInfo() = *aIn.GetPrincipalInfo();
|
|
|
|
} else {
|
|
|
|
aOut.principalInfo() = void_t();
|
|
|
|
}
|
2017-07-10 12:11:29 +03:00
|
|
|
|
2017-07-10 12:03:24 +03:00
|
|
|
aOut.paddingInfo() = aIn.GetPaddingInfo();
|
2017-07-10 12:11:29 +03:00
|
|
|
aOut.paddingSize() = aIn.GetPaddingSize();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-11 04:04:55 +03:00
|
|
|
TypeUtils::ToCacheResponse(JSContext* aCx, CacheResponse& aOut, Response& aIn,
|
2016-05-15 20:32:09 +03:00
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
|
|
|
ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2018-10-11 22:18:43 +03:00
|
|
|
bool bodyUsed = aIn.GetBodyUsed(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bodyUsed) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> ir = aIn.GetInternalResponse();
|
2015-04-16 22:00:15 +03:00
|
|
|
ToCacheResponseWithoutBody(aOut, *ir, aRv);
|
2015-04-07 16:13:05 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-09-01 00:26:30 +03:00
|
|
|
ir->GetUnfilteredBody(getter_AddRefs(stream));
|
2015-03-02 16:20:00 +03:00
|
|
|
if (stream) {
|
2017-08-11 04:04:55 +03:00
|
|
|
aIn.SetBodyUsed(aCx, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
SerializeCacheStream(stream, &aOut.body(), aStreamCleanupList, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToCacheQueryParams(CacheQueryParams& aOut,
|
|
|
|
const CacheQueryOptions& aIn)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
aOut.ignoreSearch() = aIn.mIgnoreSearch;
|
|
|
|
aOut.ignoreMethod() = aIn.mIgnoreMethod;
|
|
|
|
aOut.ignoreVary() = aIn.mIgnoreVary;
|
|
|
|
aOut.cacheNameSet() = aIn.mCacheName.WasPassed();
|
|
|
|
if (aOut.cacheNameSet()) {
|
|
|
|
aOut.cacheName() = aIn.mCacheName.Value();
|
|
|
|
} else {
|
|
|
|
aOut.cacheName() = NS_LITERAL_STRING("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Response>
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToResponse(const CacheResponse& aIn)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-17 18:47:01 +03:00
|
|
|
if (aIn.type() == ResponseType::Error) {
|
2017-11-15 22:53:42 +03:00
|
|
|
// We don't bother tracking the internal error code for cached responses...
|
|
|
|
RefPtr<InternalResponse> error = InternalResponse::NetworkError(NS_ERROR_FAILURE);
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> r = new Response(GetGlobalObject(), error, nullptr);
|
2015-03-17 18:47:01 +03:00
|
|
|
return r.forget();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> ir = new InternalResponse(aIn.status(),
|
2015-03-17 18:47:01 +03:00
|
|
|
aIn.statusText());
|
2016-05-03 04:48:40 +03:00
|
|
|
ir->SetURLList(aIn.urlList());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> internalHeaders =
|
2015-04-16 22:00:15 +03:00
|
|
|
ToInternalHeaders(aIn.headers(), aIn.headersGuard());
|
2015-03-02 16:20:00 +03:00
|
|
|
ErrorResult result;
|
2016-11-29 01:18:27 +03:00
|
|
|
|
|
|
|
// Be careful to fill the headers before setting the guard in order to
|
|
|
|
// correctly re-create the original headers.
|
2015-03-02 16:20:00 +03:00
|
|
|
ir->Headers()->Fill(*internalHeaders, result);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!result.Failed());
|
2016-11-29 01:18:27 +03:00
|
|
|
ir->Headers()->SetGuard(aIn.headersGuard(), result);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!result.Failed());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
ir->InitChannelInfo(aIn.channelInfo());
|
2015-06-05 07:39:34 +03:00
|
|
|
if (aIn.principalInfo().type() == mozilla::ipc::OptionalPrincipalInfo::TPrincipalInfo) {
|
|
|
|
UniquePtr<mozilla::ipc::PrincipalInfo> info(new mozilla::ipc::PrincipalInfo(aIn.principalInfo().get_PrincipalInfo()));
|
2018-05-30 22:15:35 +03:00
|
|
|
ir->SetPrincipalInfo(std::move(info));
|
2015-06-05 07:39:34 +03:00
|
|
|
}
|
2015-03-05 00:18:47 +03:00
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = ReadStream::Create(aIn.body());
|
2016-06-02 00:02:29 +03:00
|
|
|
ir->SetBody(stream, InternalResponse::UNKNOWN_BODY_SIZE);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
switch (aIn.type())
|
|
|
|
{
|
|
|
|
case ResponseType::Basic:
|
|
|
|
ir = ir->BasicResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Cors:
|
|
|
|
ir = ir->CORSResponse();
|
|
|
|
break;
|
2015-09-01 00:26:30 +03:00
|
|
|
case ResponseType::Default:
|
|
|
|
break;
|
|
|
|
case ResponseType::Opaque:
|
|
|
|
ir = ir->OpaqueResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Opaqueredirect:
|
|
|
|
ir = ir->OpaqueRedirectResponse();
|
|
|
|
break;
|
2015-03-17 18:47:01 +03:00
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected ResponseType!");
|
|
|
|
}
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(ir);
|
2015-03-17 18:47:01 +03:00
|
|
|
|
2017-07-10 12:11:29 +03:00
|
|
|
ir->SetPaddingSize(aIn.paddingSize());
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Response> ref = new Response(GetGlobalObject(), ir, nullptr);
|
2015-03-02 16:20:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
already_AddRefed<InternalRequest>
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToInternalRequest(const CacheRequest& aIn)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2016-05-10 13:50:15 +03:00
|
|
|
nsAutoCString url(aIn.urlWithoutQuery());
|
|
|
|
url.Append(aIn.urlQuery());
|
2016-11-07 05:16:34 +03:00
|
|
|
RefPtr<InternalRequest> internalRequest =
|
|
|
|
new InternalRequest(url, aIn.urlFragment());
|
2016-05-03 04:48:40 +03:00
|
|
|
internalRequest->SetMethod(aIn.method());
|
2015-03-02 16:20:00 +03:00
|
|
|
internalRequest->SetReferrer(aIn.referrer());
|
2016-02-27 22:08:58 +03:00
|
|
|
internalRequest->SetReferrerPolicy(aIn.referrerPolicy());
|
2015-03-02 16:20:00 +03:00
|
|
|
internalRequest->SetMode(aIn.mode());
|
|
|
|
internalRequest->SetCredentialsMode(aIn.credentials());
|
2015-03-25 23:38:42 +03:00
|
|
|
internalRequest->SetContentPolicyType(aIn.contentPolicyType());
|
2015-03-22 20:12:43 +03:00
|
|
|
internalRequest->SetCacheMode(aIn.requestCache());
|
2015-09-01 00:26:30 +03:00
|
|
|
internalRequest->SetRedirectMode(aIn.requestRedirect());
|
2016-09-07 05:20:23 +03:00
|
|
|
internalRequest->SetIntegrity(aIn.integrity());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> internalHeaders =
|
2015-04-16 22:00:15 +03:00
|
|
|
ToInternalHeaders(aIn.headers(), aIn.headersGuard());
|
2015-03-02 16:20:00 +03:00
|
|
|
ErrorResult result;
|
2015-10-27 19:23:34 +03:00
|
|
|
|
|
|
|
// Be careful to fill the headers before setting the guard in order to
|
|
|
|
// correctly re-create the original headers.
|
2015-03-02 16:20:00 +03:00
|
|
|
internalRequest->Headers()->Fill(*internalHeaders, result);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!result.Failed());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-10-27 19:23:34 +03:00
|
|
|
internalRequest->Headers()->SetGuard(aIn.headersGuard(), result);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!result.Failed());
|
2015-10-27 19:23:34 +03:00
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = ReadStream::Create(aIn.body());
|
|
|
|
|
2017-09-08 17:06:26 +03:00
|
|
|
internalRequest->SetBody(stream, -1);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return internalRequest.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Request>
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToRequest(const CacheRequest& aIn)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> internalRequest = ToInternalRequest(aIn);
|
2017-08-29 12:31:06 +03:00
|
|
|
RefPtr<Request> request =
|
|
|
|
new Request(GetGlobalObject(), internalRequest, nullptr);
|
2015-03-02 16:20:00 +03:00
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<InternalHeaders>
|
|
|
|
TypeUtils::ToInternalHeaders(const nsTArray<HeadersEntry>& aHeadersEntryList,
|
|
|
|
HeadersGuardEnum aGuard)
|
|
|
|
{
|
|
|
|
nsTArray<InternalHeaders::Entry> entryList(aHeadersEntryList.Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aHeadersEntryList.Length(); ++i) {
|
|
|
|
const HeadersEntry& headersEntry = aHeadersEntryList[i];
|
|
|
|
entryList.AppendElement(InternalHeaders::Entry(headersEntry.name(),
|
|
|
|
headersEntry.value()));
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
RefPtr<InternalHeaders> ref = new InternalHeaders(std::move(entryList), aGuard);
|
2015-04-16 22:00:15 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
// Utility function to remove the fragment from a URL, check its scheme, and optionally
|
|
|
|
// provide a URL without the query. We're not using nsIURL or URL to do this because
|
|
|
|
// they require going to the main thread.
|
|
|
|
// static
|
|
|
|
void
|
2015-06-17 03:39:05 +03:00
|
|
|
TypeUtils::ProcessURL(nsACString& aUrl, bool* aSchemeValidOut,
|
2015-06-17 03:39:05 +03:00
|
|
|
nsACString* aUrlWithoutQueryOut,nsACString* aUrlQueryOut,
|
|
|
|
ErrorResult& aRv)
|
2015-04-29 18:59:43 +03:00
|
|
|
{
|
2017-06-20 12:19:05 +03:00
|
|
|
const nsCString& flatURL = PromiseFlatCString(aUrl);
|
2015-04-29 18:59:43 +03:00
|
|
|
const char* url = flatURL.get();
|
|
|
|
|
|
|
|
// off the main thread URL parsing using nsStdURLParser.
|
|
|
|
nsCOMPtr<nsIURLParser> urlParser = new nsStdURLParser();
|
|
|
|
|
|
|
|
uint32_t pathPos;
|
|
|
|
int32_t pathLen;
|
|
|
|
uint32_t schemePos;
|
|
|
|
int32_t schemeLen;
|
|
|
|
aRv = urlParser->ParseURL(url, flatURL.Length(), &schemePos, &schemeLen,
|
|
|
|
nullptr, nullptr, // ignore authority
|
|
|
|
&pathPos, &pathLen);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) { return; }
|
|
|
|
|
|
|
|
if (aSchemeValidOut) {
|
|
|
|
nsAutoCString scheme(Substring(flatURL, schemePos, schemeLen));
|
|
|
|
*aSchemeValidOut = scheme.LowerCaseEqualsLiteral("http") ||
|
2016-10-06 06:49:34 +03:00
|
|
|
scheme.LowerCaseEqualsLiteral("https");
|
2015-04-29 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t queryPos;
|
|
|
|
int32_t queryLen;
|
|
|
|
|
|
|
|
aRv = urlParser->ParsePath(url + pathPos, flatURL.Length() - pathPos,
|
|
|
|
nullptr, nullptr, // ignore filepath
|
|
|
|
&queryPos, &queryLen,
|
2015-08-13 18:22:19 +03:00
|
|
|
nullptr, nullptr);
|
2015-04-29 18:59:43 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aUrlWithoutQueryOut) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUrlQueryOut);
|
2015-06-17 03:39:05 +03:00
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
if (queryLen < 0) {
|
|
|
|
*aUrlWithoutQueryOut = aUrl;
|
2015-06-17 03:39:05 +03:00
|
|
|
*aUrlQueryOut = EmptyCString();
|
2015-04-29 18:59:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePath gives us query position relative to the start of the path
|
|
|
|
queryPos += pathPos;
|
|
|
|
|
|
|
|
*aUrlWithoutQueryOut = Substring(aUrl, 0, queryPos - 1);
|
2015-06-17 03:39:05 +03:00
|
|
|
*aUrlQueryOut = Substring(aUrl, queryPos - 1, queryLen + 1);
|
2015-04-29 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void
|
2017-08-11 04:04:55 +03:00
|
|
|
TypeUtils::CheckAndSetBodyUsed(JSContext* aCx, Request* aRequest,
|
|
|
|
BodyAction aBodyAction, ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRequest);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
if (aBodyAction == IgnoreBody) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
bool bodyUsed = aRequest->GetBodyUsed(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bodyUsed) {
|
2015-10-05 19:38:14 +03:00
|
|
|
aRv.ThrowTypeError<MSG_FETCH_BODY_CONSUMED_ERROR>();
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
aRequest->GetBody(getter_AddRefs(stream));
|
|
|
|
if (stream) {
|
2017-08-11 04:04:55 +03:00
|
|
|
aRequest->SetBodyUsed(aCx, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<InternalRequest>
|
|
|
|
TypeUtils::ToInternalRequest(const nsAString& aIn, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RequestOrUSVString requestOrString;
|
|
|
|
requestOrString.SetAsUSVString().Rebind(aIn.Data(), aIn.Length());
|
|
|
|
|
|
|
|
// Re-create a GlobalObject stack object so we can use webidl Constructors.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetGlobalObject()))) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
GlobalObject global(cx, GetGlobalObject()->GetGlobalJSObject());
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!global.Failed());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Request> request = Request::Constructor(global, requestOrString,
|
2015-03-02 16:20:00 +03:00
|
|
|
RequestInit(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) { return nullptr; }
|
|
|
|
|
|
|
|
return request->GetInternalRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TypeUtils::SerializeCacheStream(nsIInputStream* aStream,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheReadStreamOrVoid* aStreamOut,
|
2016-05-15 20:32:09 +03:00
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
2015-03-02 16:20:00 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
*aStreamOut = void_t();
|
|
|
|
if (!aStream) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ReadStream> controlled = do_QueryObject(aStream);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (controlled) {
|
2016-05-15 20:32:09 +03:00
|
|
|
controlled->Serialize(aStreamOut, aStreamCleanupList, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
*aStreamOut = CacheReadStream();
|
|
|
|
CacheReadStream& cacheStream = aStreamOut->get_CacheReadStream();
|
2015-03-22 09:52:12 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
cacheStream.controlChild() = nullptr;
|
|
|
|
cacheStream.controlParent() = nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
UniquePtr<AutoIPCStream> autoStream(new AutoIPCStream(cacheStream.stream()));
|
|
|
|
autoStream->Serialize(aStream, GetIPCManager());
|
2015-03-22 09:52:12 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
aStreamCleanupList.AppendElement(std::move(autoStream));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|