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"
|
|
|
|
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
|
|
|
#include "mozilla/dom/InternalRequest.h"
|
|
|
|
#include "mozilla/dom/Request.h"
|
|
|
|
#include "mozilla/dom/Response.h"
|
2015-03-22 09:52:12 +03:00
|
|
|
#include "mozilla/dom/cache/CachePushStreamChild.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"
|
|
|
|
#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-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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
nsAutoTArray<nsCString, 16> varyHeaders;
|
|
|
|
ErrorResult rv;
|
|
|
|
aHeaders->GetAll(NS_LITERAL_CSTRING("vary"), varyHeaders, rv);
|
|
|
|
MOZ_ALWAYS_TRUE(!rv.Failed());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < varyHeaders.Length(); ++i) {
|
|
|
|
nsAutoCString varyValue(varyHeaders[i]);
|
|
|
|
char* rawBuffer = varyValue.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
SerializeNormalStream(nsIInputStream* aStream, CacheReadStream& aReadStreamOut)
|
2015-03-22 09:52:12 +03:00
|
|
|
{
|
|
|
|
nsAutoTArray<FileDescriptor, 4> fds;
|
|
|
|
SerializeInputStream(aStream, aReadStreamOut.params(), fds);
|
|
|
|
|
|
|
|
PFileDescriptorSetChild* fdSet = nullptr;
|
|
|
|
if (!fds.IsEmpty()) {
|
|
|
|
// We should not be serializing until we have an actor ready
|
|
|
|
PBackgroundChild* manager = BackgroundChild::GetForCurrentThread();
|
|
|
|
MOZ_ASSERT(manager);
|
|
|
|
|
|
|
|
fdSet = manager->SendPFileDescriptorSetConstructor(fds[0]);
|
|
|
|
for (uint32_t i = 1; i < fds.Length(); ++i) {
|
|
|
|
unused << fdSet->SendAddFileDescriptor(fds[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fdSet) {
|
|
|
|
aReadStreamOut.fds() = fdSet;
|
|
|
|
} else {
|
|
|
|
aReadStreamOut.fds() = void_t();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void
|
|
|
|
ToHeadersEntryList(nsTArray<HeadersEntry>& aOut, InternalHeaders* aHeaders)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aHeaders);
|
|
|
|
|
|
|
|
nsAutoTArray<InternalHeaders::Entry, 16> entryList;
|
|
|
|
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-03-02 16:20:00 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
already_AddRefed<InternalRequest>
|
|
|
|
TypeUtils::ToInternalRequest(const RequestOrUSVString& aIn,
|
|
|
|
BodyAction aBodyAction, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aIn.IsRequest()) {
|
|
|
|
Request& request = aIn.GetAsRequest();
|
|
|
|
|
|
|
|
// Check and set bodyUsed flag immediately because its on Request
|
|
|
|
// instead of InternalRequest.
|
|
|
|
CheckAndSetBodyUsed(&request, aBodyAction, aRv);
|
|
|
|
if (aRv.Failed()) { return nullptr; }
|
|
|
|
|
|
|
|
return request.GetInternalRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToInternalRequest(aIn.GetAsUSVString(), aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<InternalRequest>
|
|
|
|
TypeUtils::ToInternalRequest(const OwningRequestOrUSVString& aIn,
|
|
|
|
BodyAction aBodyAction, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (aIn.IsRequest()) {
|
|
|
|
nsRefPtr<Request> request = aIn.GetAsRequest().get();
|
|
|
|
|
|
|
|
// Check and set bodyUsed flag immediately because its on Request
|
|
|
|
// instead of InternalRequest.
|
|
|
|
CheckAndSetBodyUsed(request, aBodyAction, aRv);
|
|
|
|
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,
|
|
|
|
ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIn);
|
|
|
|
|
|
|
|
aIn->GetMethod(aOut.method());
|
|
|
|
|
|
|
|
nsAutoCString url;
|
|
|
|
aIn->GetURL(url);
|
|
|
|
CopyUTF8toUTF16(url, aOut.url());
|
|
|
|
|
|
|
|
bool schemeValid;
|
|
|
|
ProcessURL(aOut.url(), &schemeValid, &aOut.urlWithoutQuery(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!schemeValid) {
|
|
|
|
if (aSchemeAction == TypeErrorOnInvalidScheme) {
|
|
|
|
NS_NAMED_LITERAL_STRING(label, "Request");
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL_SCHEME, &label, &aOut.url());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aIn->GetReferrer(aOut.referrer());
|
|
|
|
|
|
|
|
nsRefPtr<InternalHeaders> headers = aIn->Headers();
|
|
|
|
MOZ_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();
|
|
|
|
aOut.context() = aIn->Context();
|
2015-03-22 20:12:43 +03:00
|
|
|
aOut.requestCache() = aIn->GetCacheMode();
|
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));
|
|
|
|
SerializeCacheStream(stream, &aOut.body(), aRv);
|
|
|
|
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();
|
|
|
|
|
|
|
|
nsAutoCString url;
|
|
|
|
aIn.GetUrl(url);
|
|
|
|
CopyUTF8toUTF16(url, aOut.url());
|
|
|
|
|
|
|
|
if (aOut.url() != EmptyString()) {
|
|
|
|
// Pass all Response URL schemes through... The spec only requires we take
|
|
|
|
// action on invalid schemes for Request objects.
|
|
|
|
ProcessURL(aOut.url(), nullptr, nullptr, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aOut.status() = aIn.GetStatus();
|
|
|
|
aOut.statusText() = aIn.GetStatusText();
|
2015-03-17 18:47:01 +03:00
|
|
|
nsRefPtr<InternalHeaders> headers = aIn.UnfilteredHeaders();
|
2015-03-02 16:20:00 +03:00
|
|
|
MOZ_ASSERT(headers);
|
2015-04-07 16:13:05 +03:00
|
|
|
if (HasVaryStar(headers)) {
|
|
|
|
aRv.ThrowTypeError(MSG_RESPONSE_HAS_VARY_STAR);
|
|
|
|
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-03-05 00:18:47 +03:00
|
|
|
aOut.securityInfo() = aIn.GetSecurityInfo();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
TypeUtils::ToCacheResponse(CacheResponse& aOut, Response& aIn, ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
if (aIn.BodyUsed()) {
|
|
|
|
aRv.ThrowTypeError(MSG_FETCH_BODY_CONSUMED_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<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;
|
|
|
|
aIn.GetBody(getter_AddRefs(stream));
|
|
|
|
if (stream) {
|
|
|
|
aIn.SetBodyUsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializeCacheStream(stream, &aOut.body(), aRv);
|
|
|
|
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) {
|
|
|
|
nsRefPtr<InternalResponse> error = InternalResponse::NetworkError();
|
|
|
|
nsRefPtr<Response> r = new Response(GetGlobalObject(), error);
|
|
|
|
return r.forget();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
nsRefPtr<InternalResponse> ir = new InternalResponse(aIn.status(),
|
|
|
|
aIn.statusText());
|
2015-03-02 16:20:00 +03:00
|
|
|
ir->SetUrl(NS_ConvertUTF16toUTF8(aIn.url()));
|
|
|
|
|
|
|
|
nsRefPtr<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;
|
|
|
|
ir->Headers()->SetGuard(aIn.headersGuard(), result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
|
|
|
ir->Headers()->Fill(*internalHeaders, result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
|
|
|
|
2015-03-05 00:18:47 +03:00
|
|
|
ir->SetSecurityInfo(aIn.securityInfo());
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = ReadStream::Create(aIn.body());
|
|
|
|
ir->SetBody(stream);
|
|
|
|
|
2015-03-17 18:47:01 +03:00
|
|
|
switch (aIn.type())
|
|
|
|
{
|
|
|
|
case ResponseType::Default:
|
|
|
|
break;
|
|
|
|
case ResponseType::Opaque:
|
|
|
|
ir = ir->OpaqueResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Basic:
|
|
|
|
ir = ir->BasicResponse();
|
|
|
|
break;
|
|
|
|
case ResponseType::Cors:
|
|
|
|
ir = ir->CORSResponse();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected ResponseType!");
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(ir);
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
nsRefPtr<Response> ref = new Response(GetGlobalObject(), ir);
|
|
|
|
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
|
|
|
{
|
|
|
|
nsRefPtr<InternalRequest> internalRequest = new InternalRequest();
|
|
|
|
|
|
|
|
internalRequest->SetMethod(aIn.method());
|
|
|
|
internalRequest->SetURL(NS_ConvertUTF16toUTF8(aIn.url()));
|
|
|
|
internalRequest->SetReferrer(aIn.referrer());
|
|
|
|
internalRequest->SetMode(aIn.mode());
|
|
|
|
internalRequest->SetCredentialsMode(aIn.credentials());
|
2015-03-25 23:38:42 +03:00
|
|
|
internalRequest->SetContentPolicyType(aIn.contentPolicyType());
|
|
|
|
DebugOnly<RequestContext> contextAfterSetContentPolicyType = internalRequest->Context();
|
|
|
|
internalRequest->SetContext(aIn.context());
|
|
|
|
MOZ_ASSERT(contextAfterSetContentPolicyType.value == internalRequest->Context(),
|
|
|
|
"The RequestContext and nsContentPolicyType values should not get out of sync");
|
2015-03-22 20:12:43 +03:00
|
|
|
internalRequest->SetCacheMode(aIn.requestCache());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
nsRefPtr<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;
|
|
|
|
internalRequest->Headers()->SetGuard(aIn.headersGuard(), result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
|
|
|
internalRequest->Headers()->Fill(*internalHeaders, result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream = ReadStream::Create(aIn.body());
|
|
|
|
|
|
|
|
internalRequest->SetBody(stream);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
nsRefPtr<InternalRequest> internalRequest = ToInternalRequest(aIn);
|
|
|
|
nsRefPtr<Request> request = new Request(GetGlobalObject(), internalRequest);
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<InternalHeaders> ref = new InternalHeaders(Move(entryList), aGuard);
|
|
|
|
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
|
|
|
|
TypeUtils::ProcessURL(nsAString& aUrl, bool* aSchemeValidOut,
|
|
|
|
nsAString* aUrlWithoutQueryOut, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
NS_ConvertUTF16toUTF8 flatURL(aUrl);
|
|
|
|
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") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("https");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t queryPos;
|
|
|
|
int32_t queryLen;
|
|
|
|
uint32_t refPos;
|
|
|
|
int32_t refLen;
|
|
|
|
|
|
|
|
aRv = urlParser->ParsePath(url + pathPos, flatURL.Length() - pathPos,
|
|
|
|
nullptr, nullptr, // ignore filepath
|
|
|
|
&queryPos, &queryLen,
|
|
|
|
&refPos, &refLen);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove this once Request/Response properly strip the fragment (bug 1110476)
|
|
|
|
if (refLen >= 0) {
|
|
|
|
// ParsePath gives us ref position relative to the start of the path
|
|
|
|
refPos += pathPos;
|
|
|
|
|
|
|
|
aUrl = Substring(aUrl, 0, refPos - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aUrlWithoutQueryOut) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queryLen < 0) {
|
|
|
|
*aUrlWithoutQueryOut = aUrl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePath gives us query position relative to the start of the path
|
|
|
|
queryPos += pathPos;
|
|
|
|
|
|
|
|
// We want everything before the query sine we already removed the trailing
|
|
|
|
// fragment
|
|
|
|
*aUrlWithoutQueryOut = Substring(aUrl, 0, queryPos - 1);
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void
|
|
|
|
TypeUtils::CheckAndSetBodyUsed(Request* aRequest, BodyAction aBodyAction,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
|
|
|
if (aBodyAction == IgnoreBody) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRequest->BodyUsed()) {
|
|
|
|
aRv.ThrowTypeError(MSG_FETCH_BODY_CONSUMED_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
aRequest->GetBody(getter_AddRefs(stream));
|
|
|
|
if (stream) {
|
|
|
|
aRequest->SetBodyUsed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
MOZ_ASSERT(!global.Failed());
|
|
|
|
|
|
|
|
nsRefPtr<Request> request = Request::Constructor(global, requestOrString,
|
|
|
|
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,
|
2015-03-02 16:20:00 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
*aStreamOut = void_t();
|
|
|
|
if (!aStream) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
// Option 1: Send a cache-specific ReadStream if we can.
|
2015-03-02 16:20:00 +03:00
|
|
|
nsRefPtr<ReadStream> controlled = do_QueryObject(aStream);
|
|
|
|
if (controlled) {
|
|
|
|
controlled->Serialize(aStreamOut);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheReadStream readStream;
|
2015-03-02 16:20:00 +03:00
|
|
|
readStream.controlChild() = nullptr;
|
|
|
|
readStream.controlParent() = nullptr;
|
2015-03-22 09:52:12 +03:00
|
|
|
readStream.pushStreamChild() = nullptr;
|
|
|
|
readStream.pushStreamParent() = nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
// Option 2: Do normal stream serialization if its supported.
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serial = do_QueryInterface(aStream);
|
|
|
|
if (serial) {
|
|
|
|
SerializeNormalStream(aStream, readStream);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
// Option 3: As a last resort push data across manually. Should only be
|
|
|
|
// needed for nsPipe input stream. Only works for async,
|
|
|
|
// non-blocking streams.
|
2015-03-02 16:20:00 +03:00
|
|
|
} else {
|
2015-03-22 09:52:12 +03:00
|
|
|
SerializePushStream(aStream, readStream, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) { return; }
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*aStreamOut = readStream;
|
|
|
|
}
|
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
void
|
|
|
|
TypeUtils::SerializePushStream(nsIInputStream* aStream,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheReadStream& aReadStreamOut,
|
2015-03-22 09:52:12 +03:00
|
|
|
ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-22 09:52:12 +03:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(aStream);
|
|
|
|
if (NS_WARN_IF(!asyncStream)) {
|
|
|
|
aRv = NS_ERROR_FAILURE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nonBlocking = false;
|
|
|
|
aRv = asyncStream->IsNonBlocking(&nonBlocking);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) { return; }
|
|
|
|
if (NS_WARN_IF(!nonBlocking)) {
|
|
|
|
aRv = NS_ERROR_FAILURE;
|
|
|
|
return;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
aReadStreamOut.pushStreamChild() = CreatePushStream(asyncStream);
|
|
|
|
MOZ_ASSERT(aReadStreamOut.pushStreamChild());
|
|
|
|
aReadStreamOut.params() = void_t();
|
|
|
|
aReadStreamOut.fds() = void_t();
|
|
|
|
|
|
|
|
// CachePushStreamChild::Start() must be called after sending the stream
|
|
|
|
// across to the parent side.
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|