2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsNetUtil_h__
|
|
|
|
#define nsNetUtil_h__
|
|
|
|
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prio.h" // for read/write flags, permissions, etc.
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "nsHashKeys.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "plstr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIURI.h"
|
2010-10-15 14:55:53 -07:00
|
|
|
#include "nsIStandardURL.h"
|
2010-11-19 12:58:59 -08:00
|
|
|
#include "nsIURLParser.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsISafeOutputStream.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIRequestObserverProxy.h"
|
|
|
|
#include "nsISimpleStreamListener.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIInputStreamChannel.h"
|
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIDownloader.h"
|
|
|
|
#include "nsIStreamLoader.h"
|
|
|
|
#include "nsIUnicharStreamLoader.h"
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
|
|
|
#include "nsIFileProtocolHandler.h"
|
|
|
|
#include "nsIStringStream.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIFileStreams.h"
|
2008-08-27 18:15:32 -07:00
|
|
|
#include "nsIFileURL.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIProtocolProxyService.h"
|
|
|
|
#include "nsIProxyInfo.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIBufferedStreams.h"
|
|
|
|
#include "nsIInputStreamPump.h"
|
|
|
|
#include "nsIAsyncStreamCopier.h"
|
|
|
|
#include "nsIPersistentProperties2.h"
|
|
|
|
#include "nsISyncStreamListener.h"
|
|
|
|
#include "nsInterfaceRequestorAgg.h"
|
|
|
|
#include "nsINetUtil.h"
|
2010-03-02 23:51:09 -08:00
|
|
|
#include "nsIURIWithPrincipal.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIAuthPrompt.h"
|
|
|
|
#include "nsIAuthPrompt2.h"
|
|
|
|
#include "nsIAuthPromptAdapterFactory.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsINestedURI.h"
|
|
|
|
#include "nsIMutable.h"
|
2007-11-23 12:20:41 -08:00
|
|
|
#include "nsIPropertyBag2.h"
|
2010-04-10 13:08:21 -07:00
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2008-01-31 00:16:54 -08:00
|
|
|
#include "nsIIDNService.h"
|
2009-01-21 14:50:28 -08:00
|
|
|
#include "nsIChannelEventSink.h"
|
2010-11-23 04:01:30 -08:00
|
|
|
#include "nsISocketProviderService.h"
|
|
|
|
#include "nsISocketProvider.h"
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "nsIRedirectChannelRegistrar.h"
|
2011-09-09 15:41:04 -07:00
|
|
|
#include "nsIMIMEHeaderParam.h"
|
2012-07-22 15:35:33 -07:00
|
|
|
#include "nsILoadContext.h"
|
2014-08-22 20:06:56 -07:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2010-04-12 08:44:28 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-09-04 17:37:45 -07:00
|
|
|
#include "nsIPrivateBrowsingChannel.h"
|
2012-10-02 17:39:09 -07:00
|
|
|
#include "mozIApplicationClearPrivateDataParams.h"
|
2012-10-17 22:00:16 -07:00
|
|
|
#include "nsIOfflineCacheUpdate.h"
|
2012-11-20 16:19:53 -08:00
|
|
|
#include "nsIContentSniffer.h"
|
|
|
|
#include "nsCategoryCache.h"
|
2013-06-28 20:04:42 -07:00
|
|
|
#include "nsStringStream.h"
|
2013-09-10 10:40:35 -07:00
|
|
|
#include "nsIViewSourceChannel.h"
|
2014-07-17 14:52:25 -07:00
|
|
|
#include "mozilla/LoadInfo.h"
|
|
|
|
#include "nsINode.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-28 03:04:32 -07:00
|
|
|
#include <limits>
|
|
|
|
|
2010-04-12 08:44:28 -07:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
|
2013-06-28 20:04:42 -07:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
2010-04-12 08:44:28 -07:00
|
|
|
inline already_AddRefed<nsIIOService>
|
|
|
|
do_GetIOService(nsresult* error = 0)
|
|
|
|
{
|
2014-03-15 12:00:15 -07:00
|
|
|
nsCOMPtr<nsIIOService> io = mozilla::services::GetIOService();
|
2010-04-12 08:44:28 -07:00
|
|
|
if (error)
|
2014-03-15 12:00:15 -07:00
|
|
|
*error = io ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
return io.forget();
|
2010-04-12 08:44:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline already_AddRefed<nsINetUtil>
|
|
|
|
do_GetNetUtil(nsresult *error = 0)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIOService> io = mozilla::services::GetIOService();
|
2014-03-15 12:00:15 -07:00
|
|
|
nsCOMPtr<nsINetUtil> util;
|
2010-04-12 08:44:28 -07:00
|
|
|
if (io)
|
2014-03-15 12:00:15 -07:00
|
|
|
util = do_QueryInterface(io);
|
2010-04-12 08:44:28 -07:00
|
|
|
|
|
|
|
if (error)
|
2014-03-15 12:00:15 -07:00
|
|
|
*error = !!util ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
return util.forget();
|
2010-04-12 08:44:28 -07:00
|
|
|
}
|
|
|
|
#else
|
2007-03-22 10:30:00 -07:00
|
|
|
// Helper, to simplify getting the I/O service.
|
|
|
|
inline const nsGetServiceByContractIDWithError
|
|
|
|
do_GetIOService(nsresult* error = 0)
|
|
|
|
{
|
|
|
|
return nsGetServiceByContractIDWithError(NS_IOSERVICE_CONTRACTID, error);
|
|
|
|
}
|
|
|
|
|
2010-04-12 08:44:28 -07:00
|
|
|
// An alias to do_GetIOService
|
|
|
|
inline const nsGetServiceByContractIDWithError
|
|
|
|
do_GetNetUtil(nsresult* error = 0)
|
|
|
|
{
|
|
|
|
return do_GetIOService(error);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// private little helper function... don't call this directly!
|
|
|
|
inline nsresult
|
|
|
|
net_EnsureIOService(nsIIOService **ios, nsCOMPtr<nsIIOService> &grip)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!*ios) {
|
|
|
|
grip = do_GetIOService(&rv);
|
|
|
|
*ios = grip;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewURI(nsIURI **result,
|
|
|
|
const nsACString &spec,
|
2012-07-30 07:20:58 -07:00
|
|
|
const char *charset = nullptr,
|
|
|
|
nsIURI *baseURI = nullptr,
|
|
|
|
nsIIOService *ioService = nullptr) // pass in nsIIOService to optimize callers
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService)
|
|
|
|
rv = ioService->NewURI(spec, charset, baseURI, result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewURI(nsIURI* *result,
|
|
|
|
const nsAString& spec,
|
2012-07-30 07:20:58 -07:00
|
|
|
const char *charset = nullptr,
|
|
|
|
nsIURI* baseURI = nullptr,
|
|
|
|
nsIIOService* ioService = nullptr) // pass in nsIIOService to optimize callers
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_NewURI(result, NS_ConvertUTF16toUTF8(spec), charset, baseURI, ioService);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewURI(nsIURI* *result,
|
|
|
|
const char *spec,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIURI* baseURI = nullptr,
|
|
|
|
nsIIOService* ioService = nullptr) // pass in nsIIOService to optimize callers
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewURI(result, nsDependentCString(spec), nullptr, baseURI, ioService);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewFileURI(nsIURI* *result,
|
|
|
|
nsIFile* spec,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService* ioService = nullptr) // pass in nsIIOService to optimize callers
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService)
|
|
|
|
rv = ioService->NewFileURI(spec, result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:52:25 -07:00
|
|
|
/*
|
2014-12-12 09:05:09 -08:00
|
|
|
* How to create a new Channel, using NS_NewChannel,
|
2015-01-11 20:26:40 -08:00
|
|
|
* NS_NewChannelWithTriggeringPrincipal,
|
2014-12-12 09:05:09 -08:00
|
|
|
* NS_NewInputStreamChannel, NS_NewChannelInternal
|
|
|
|
* and it's variations:
|
|
|
|
*
|
2014-12-12 13:45:51 -08:00
|
|
|
* What specific API function to use:
|
|
|
|
* * The NS_NewChannelInternal functions should almost never be directly
|
|
|
|
* called outside of necko code.
|
|
|
|
* * If possible, use NS_NewChannel() providing a loading *nsINode*
|
|
|
|
* * If no loading *nsINode* is avaialable, call NS_NewChannel() providing
|
|
|
|
* a loading *nsIPrincipal*.
|
|
|
|
* * Call NS_NewChannelWithTriggeringPrincipal if the triggeringPrincipal
|
|
|
|
* is different from the loadingPrincipal.
|
|
|
|
* * Call NS_NewChannelInternal() providing aLoadInfo object in cases where
|
|
|
|
* you already have loadInfo object, e.g in case of a channel redirect.
|
2014-12-12 09:05:09 -08:00
|
|
|
*
|
|
|
|
* @param aURI
|
|
|
|
* nsIURI from which to make a channel
|
|
|
|
* @param aLoadingNode
|
|
|
|
* The loadingDocument of the channel.
|
|
|
|
* The element or document where the result of this request will be
|
|
|
|
* used. This is the document/element that will get access to the
|
|
|
|
* result of this request. For example for an image load, it's the
|
|
|
|
* document in which the image will be loaded. And for a CSS
|
|
|
|
* stylesheet it's the document whose rendering will be affected by
|
|
|
|
* the stylesheet.
|
|
|
|
* If possible, pass in the element which is performing the load. But
|
|
|
|
* if the load is coming from a JS API (such as XMLHttpRequest) or if
|
|
|
|
* the load might be coalesced across multiple elements (such as
|
|
|
|
* for <img>) then pass in the Document node instead.
|
|
|
|
* For loads that are not related to any document, such as loads coming
|
|
|
|
* from addons or internal browser features, use null here.
|
|
|
|
* @param aLoadingPrincipal
|
|
|
|
* The loadingPrincipal of the channel.
|
|
|
|
* The principal of the document where the result of this request will
|
|
|
|
* be used.
|
2015-04-13 13:37:14 -07:00
|
|
|
* This defaults to the principal of aLoadingNode, so when aLoadingNode
|
|
|
|
* is passed this can be left as null. However for loads where
|
|
|
|
* aLoadingNode is null this argument must be passed.
|
|
|
|
* For example for loads from a WebWorker, pass the principal
|
2014-12-12 09:05:09 -08:00
|
|
|
* of that worker. For loads from an addon or from internal browser
|
|
|
|
* features, pass the system principal.
|
|
|
|
* This principal should almost always be the system principal if
|
|
|
|
* aLoadingNode is null. The only exception to this is for loads
|
|
|
|
* from WebWorkers since they don't have any nodes to be passed as
|
|
|
|
* aLoadingNode.
|
|
|
|
* Please note, aLoadingPrincipal is *not* the principal of the
|
|
|
|
* resource being loaded. But rather the principal of the context
|
|
|
|
* where the resource will be used.
|
|
|
|
* @param aTriggeringPrincipal
|
|
|
|
* The triggeringPrincipal of the load.
|
|
|
|
* The triggeringPrincipal is the principal of the resource that caused
|
|
|
|
* this particular URL to be loaded.
|
|
|
|
* Most likely the triggeringPrincipal and the loadingPrincipal are
|
|
|
|
* identical, in which case the triggeringPrincipal can be left out.
|
|
|
|
* In some cases the loadingPrincipal and the triggeringPrincipal are
|
|
|
|
* different however, e.g. a stylesheet may import a subresource. In
|
|
|
|
* that case the principal of the stylesheet which contains the
|
|
|
|
* import command is the triggeringPrincipal, and the principal of
|
|
|
|
* the document whose rendering is affected is the loadingPrincipal.
|
|
|
|
* @param aSecurityFlags
|
|
|
|
* The securityFlags of the channel.
|
|
|
|
* Any of the securityflags defined in nsILoadInfo.idl
|
|
|
|
* @param aContentPolicyType
|
|
|
|
* The contentPolicyType of the channel.
|
|
|
|
* Any of the content types defined in nsIContentPolicy.idl
|
|
|
|
*
|
2014-12-12 13:45:51 -08:00
|
|
|
* Please note, if you provide both a loadingNode and a loadingPrincipal,
|
|
|
|
* then loadingPrincipal must be equal to loadingNode->NodePrincipal().
|
|
|
|
* But less error prone is to just supply a loadingNode.
|
2015-01-06 17:01:07 -08:00
|
|
|
*
|
|
|
|
* Keep in mind that URIs coming from a webpage should *never* use the
|
|
|
|
* systemPrincipal as the loadingPrincipal.
|
2014-12-12 09:05:09 -08:00
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
inline nsresult
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_NewChannelInternal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsINode* aLoadingNode,
|
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-12-07 19:53:33 -08:00
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(outChannel);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
nsresult rv = net_EnsureIOService(&aIoService, grip);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-12-12 09:05:09 -08:00
|
|
|
rv = aIoService->NewChannelFromURI2(
|
|
|
|
aUri,
|
|
|
|
aLoadingNode ?
|
|
|
|
aLoadingNode->AsDOMNode() : nullptr,
|
|
|
|
aLoadingPrincipal,
|
|
|
|
aTriggeringPrincipal,
|
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
getter_AddRefs(channel));
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aLoadGroup) {
|
|
|
|
rv = channel->SetLoadGroup(aLoadGroup);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallbacks) {
|
|
|
|
rv = channel->SetNotificationCallbacks(aCallbacks);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLoadFlags != nsIRequest::LOAD_NORMAL) {
|
|
|
|
// Retain the LOAD_REPLACE load flag if set.
|
|
|
|
nsLoadFlags normalLoadFlags = 0;
|
|
|
|
channel->GetLoadFlags(&normalLoadFlags);
|
|
|
|
rv = channel->SetLoadFlags(aLoadFlags | (normalLoadFlags & nsIChannel::LOAD_REPLACE));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2014-12-07 19:53:33 -08:00
|
|
|
|
2014-07-17 14:52:25 -07:00
|
|
|
channel.forget(outChannel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
// See NS_NewChannelInternal for usage and argument description
|
2014-07-17 14:52:25 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewChannelInternal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
2014-12-07 19:53:33 -08:00
|
|
|
nsILoadInfo* aLoadInfo,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
|
|
|
{
|
2015-02-18 15:13:23 -08:00
|
|
|
// NS_NewChannelInternal is mostly called for channel redirects. We should allow
|
|
|
|
// the creation of a channel even if the original channel did not have a loadinfo
|
|
|
|
// attached.
|
|
|
|
if (!aLoadInfo) {
|
|
|
|
return NS_NewChannelInternal(outChannel,
|
|
|
|
aUri,
|
|
|
|
nullptr, // aLoadingNode
|
|
|
|
nullptr, // aLoadingPrincipal
|
|
|
|
nullptr, // aTriggeringPrincipal
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aIoService);
|
|
|
|
}
|
2014-12-07 19:53:33 -08:00
|
|
|
nsresult rv = NS_NewChannelInternal(outChannel,
|
|
|
|
aUri,
|
|
|
|
aLoadInfo->LoadingNode(),
|
|
|
|
aLoadInfo->LoadingPrincipal(),
|
|
|
|
aLoadInfo->TriggeringPrincipal(),
|
|
|
|
aLoadInfo->GetSecurityFlags(),
|
|
|
|
aLoadInfo->GetContentPolicyType(),
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aIoService);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-12-12 09:05:09 -08:00
|
|
|
// Please note that we still call SetLoadInfo on the channel because
|
|
|
|
// we want the same instance of the loadInfo to be set on the channel.
|
|
|
|
(*outChannel)->SetLoadInfo(aLoadInfo);
|
2014-12-07 19:53:33 -08:00
|
|
|
return NS_OK;
|
2014-07-17 14:52:25 -07:00
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
// See NS_NewChannelInternal for usage and argument description
|
2014-11-14 08:56:39 -08:00
|
|
|
inline nsresult /*NS_NewChannelWithNodeAndTriggeringPrincipal */
|
|
|
|
NS_NewChannelWithTriggeringPrincipal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsINode* aLoadingNode,
|
2014-11-14 08:56:39 -08:00
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
|
|
|
{
|
2014-12-12 09:05:09 -08:00
|
|
|
MOZ_ASSERT(aLoadingNode);
|
2014-11-14 08:56:39 -08:00
|
|
|
NS_ASSERTION(aTriggeringPrincipal, "Can not create channel without a triggering Principal!");
|
|
|
|
return NS_NewChannelInternal(outChannel,
|
|
|
|
aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
aLoadingNode,
|
|
|
|
aLoadingNode->NodePrincipal(),
|
2014-11-14 08:56:39 -08:00
|
|
|
aTriggeringPrincipal,
|
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aIoService);
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
// See NS_NewChannelInternal for usage and argument description
|
2014-11-14 08:56:39 -08:00
|
|
|
inline nsresult /*NS_NewChannelWithPrincipalAndTriggeringPrincipal */
|
|
|
|
NS_NewChannelWithTriggeringPrincipal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
|
|
|
{
|
2014-12-12 09:05:09 -08:00
|
|
|
NS_ASSERTION(aLoadingPrincipal, "Can not create channel without a loading Principal!");
|
2014-11-14 08:56:39 -08:00
|
|
|
return NS_NewChannelInternal(outChannel,
|
|
|
|
aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nullptr, // aLoadingNode
|
|
|
|
aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
aTriggeringPrincipal,
|
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aIoService);
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
// See NS_NewChannelInternal for usage and argument description
|
2014-07-17 14:52:25 -07:00
|
|
|
inline nsresult /* NS_NewChannelNode */
|
|
|
|
NS_NewChannel(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsINode* aLoadingNode,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
|
|
|
{
|
2014-12-12 09:05:09 -08:00
|
|
|
NS_ASSERTION(aLoadingNode, "Can not create channel without a loading Node!");
|
2014-07-17 14:52:25 -07:00
|
|
|
return NS_NewChannelInternal(outChannel,
|
|
|
|
aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
aLoadingNode,
|
|
|
|
aLoadingNode->NodePrincipal(),
|
2014-11-14 08:56:39 -08:00
|
|
|
nullptr, // aTriggeringPrincipal
|
2014-07-17 14:52:25 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aIoService);
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
// See NS_NewChannelInternal for usage and argument description
|
2014-07-17 14:52:25 -07:00
|
|
|
inline nsresult /* NS_NewChannelPrincipal */
|
|
|
|
NS_NewChannel(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
|
|
|
{
|
|
|
|
return NS_NewChannelInternal(outChannel,
|
|
|
|
aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nullptr, // aLoadingNode,
|
|
|
|
aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
nullptr, // aTriggeringPrincipal
|
2014-07-17 14:52:25 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aIoService);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_MakeAbsoluteURI(nsACString &result,
|
|
|
|
const nsACString &spec,
|
2013-03-08 05:30:13 -08:00
|
|
|
nsIURI *baseURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!baseURI) {
|
|
|
|
NS_WARNING("It doesn't make sense to not supply a base URI");
|
|
|
|
result = spec;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
else if (spec.IsEmpty())
|
|
|
|
rv = baseURI->GetSpec(result);
|
|
|
|
else
|
|
|
|
rv = baseURI->Resolve(spec, result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_MakeAbsoluteURI(char **result,
|
|
|
|
const char *spec,
|
2013-03-08 05:30:13 -08:00
|
|
|
nsIURI *baseURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString resultBuf;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_MakeAbsoluteURI(resultBuf, nsDependentCString(spec), baseURI);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = ToNewCString(resultBuf);
|
|
|
|
if (!*result)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_MakeAbsoluteURI(nsAString &result,
|
|
|
|
const nsAString &spec,
|
2013-03-08 05:30:13 -08:00
|
|
|
nsIURI *baseURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!baseURI) {
|
|
|
|
NS_WARNING("It doesn't make sense to not supply a base URI");
|
|
|
|
result = spec;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
else {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString resultBuf;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (spec.IsEmpty())
|
|
|
|
rv = baseURI->GetSpec(resultBuf);
|
|
|
|
else
|
|
|
|
rv = baseURI->Resolve(NS_ConvertUTF16toUTF8(spec), resultBuf);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
CopyUTF8toUTF16(resultBuf, result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-01-31 00:16:54 -08:00
|
|
|
/**
|
|
|
|
* This function is a helper function to get a scheme's default port.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline int32_t
|
2008-01-31 00:16:54 -08:00
|
|
|
NS_GetDefaultPort(const char *scheme,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService* ioService = nullptr)
|
2008-01-31 00:16:54 -08:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
net_EnsureIOService(&ioService, grip);
|
|
|
|
if (!ioService)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
rv = ioService->GetProtocolHandler(scheme, getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t port;
|
2008-01-31 00:16:54 -08:00
|
|
|
rv = handler->GetDefaultPort(&port);
|
|
|
|
return NS_SUCCEEDED(rv) ? port : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is a helper function to apply the ToAscii conversion
|
|
|
|
* to a string
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2008-01-31 00:16:54 -08:00
|
|
|
NS_StringToACE(const nsACString &idn, nsACString &result)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIDNService> idnSrv = do_GetService(NS_IDNSERVICE_CONTRACTID);
|
|
|
|
if (!idnSrv)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-01-31 00:16:54 -08:00
|
|
|
nsresult rv = idnSrv->ConvertUTF8toACE(idn, result);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-01-31 00:16:54 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-01-31 00:16:54 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* This function is a helper function to get a protocol's default port if the
|
|
|
|
* URI does not specify a port explicitly. Returns -1 if this protocol has no
|
|
|
|
* concept of ports or if there was an error getting the port.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline int32_t
|
2013-03-08 05:30:13 -08:00
|
|
|
NS_GetRealPort(nsIURI* aURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t port;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = aURI->GetPort(&port);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (port != -1)
|
|
|
|
return port; // explicitly specified
|
|
|
|
|
|
|
|
// Otherwise, we have to get the default port from the protocol handler
|
|
|
|
|
|
|
|
// Need the scheme first
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString scheme;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = aURI->GetScheme(scheme);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
2008-01-31 00:16:54 -08:00
|
|
|
return NS_GetDefaultPort(scheme.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
inline nsresult /* NS_NewInputStreamChannelWithLoadInfo */
|
2014-10-16 11:14:05 -07:00
|
|
|
NS_NewInputStreamChannelInternal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
const nsACString& aContentType,
|
|
|
|
const nsACString& aContentCharset,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsILoadInfo* aLoadInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-10-16 11:14:05 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStreamChannel> isc =
|
|
|
|
do_CreateInstance(NS_INPUTSTREAMCHANNEL_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = isc->SetURI(aUri);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = isc->SetContentStream(aStream);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-10-16 11:14:05 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(isc, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!aContentType.IsEmpty()) {
|
|
|
|
rv = channel->SetContentType(aContentType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aContentCharset.IsEmpty()) {
|
|
|
|
rv = channel->SetContentCharset(aContentCharset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
channel->SetLoadInfo(aLoadInfo);
|
2014-12-11 21:58:21 -08:00
|
|
|
|
|
|
|
// If we're sandboxed, make sure to clear any owner the channel
|
|
|
|
// might already have.
|
2015-02-19 11:47:18 -08:00
|
|
|
if (aLoadInfo && aLoadInfo->GetLoadingSandboxed()) {
|
2014-12-11 21:58:21 -08:00
|
|
|
channel->SetOwner(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.forget(outChannel);
|
|
|
|
return NS_OK;
|
2014-12-11 20:45:16 -08:00
|
|
|
}
|
|
|
|
|
2014-12-12 09:05:09 -08:00
|
|
|
inline nsresult
|
|
|
|
NS_NewInputStreamChannelInternal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
const nsACString& aContentType,
|
|
|
|
const nsACString& aContentCharset,
|
|
|
|
nsINode* aLoadingNode,
|
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsIURI* aBaseURI = nullptr)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo =
|
|
|
|
new mozilla::LoadInfo(aLoadingPrincipal,
|
|
|
|
aTriggeringPrincipal,
|
|
|
|
aLoadingNode,
|
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aBaseURI);
|
|
|
|
if (!loadInfo) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_NewInputStreamChannelInternal(outChannel,
|
|
|
|
aUri,
|
|
|
|
aStream,
|
|
|
|
aContentType,
|
|
|
|
aContentCharset,
|
|
|
|
loadInfo);
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:14:05 -07:00
|
|
|
inline nsresult /* NS_NewInputStreamChannelPrincipal */
|
|
|
|
NS_NewInputStreamChannel(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
|
|
|
nsIInputStream* aStream,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-10-16 11:14:05 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
const nsACString& aContentType = EmptyCString(),
|
|
|
|
const nsACString& aContentCharset = EmptyCString())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-10-16 11:14:05 -07:00
|
|
|
return NS_NewInputStreamChannelInternal(outChannel,
|
|
|
|
aUri,
|
|
|
|
aStream,
|
|
|
|
aContentType,
|
|
|
|
aContentCharset,
|
2014-12-12 09:05:09 -08:00
|
|
|
nullptr, // aLoadingNode
|
|
|
|
aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
nullptr, // aTriggeringPrincipal
|
2014-10-16 11:14:05 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-28 20:04:42 -07:00
|
|
|
inline nsresult
|
2014-10-16 11:14:05 -07:00
|
|
|
NS_NewInputStreamChannelInternal(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
|
|
|
const nsAString& aData,
|
|
|
|
const nsACString& aContentType,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsINode* aLoadingNode,
|
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
2014-10-16 11:14:05 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
2014-11-04 16:34:00 -08:00
|
|
|
bool aIsSrcdocChannel = false,
|
|
|
|
nsIURI* aBaseURI = nullptr)
|
2013-06-28 20:04:42 -07:00
|
|
|
{
|
2014-10-16 11:14:05 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStringInputStream> stream;
|
|
|
|
stream = do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-06-28 20:04:42 -07:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
uint32_t len;
|
2014-10-16 11:14:05 -07:00
|
|
|
char* utf8Bytes = ToNewUTF8String(aData, &len);
|
2013-06-28 20:04:42 -07:00
|
|
|
rv = stream->AdoptData(utf8Bytes, len);
|
|
|
|
#else
|
2014-10-16 11:14:05 -07:00
|
|
|
char* utf8Bytes = ToNewUTF8String(aData);
|
2013-06-28 20:04:42 -07:00
|
|
|
rv = stream->AdoptData(utf8Bytes, strlen(utf8Bytes));
|
|
|
|
#endif
|
|
|
|
|
2014-10-16 11:14:05 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewInputStreamChannelInternal(getter_AddRefs(channel),
|
|
|
|
aUri,
|
|
|
|
stream,
|
|
|
|
aContentType,
|
|
|
|
NS_LITERAL_CSTRING("UTF-8"),
|
2014-12-12 09:05:09 -08:00
|
|
|
aLoadingNode,
|
|
|
|
aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
aTriggeringPrincipal,
|
2014-10-16 11:14:05 -07:00
|
|
|
aSecurityFlags,
|
2014-11-04 16:34:00 -08:00
|
|
|
aContentPolicyType,
|
|
|
|
aBaseURI);
|
2013-06-28 20:04:42 -07:00
|
|
|
|
2014-10-16 11:14:05 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-06-28 20:04:42 -07:00
|
|
|
|
2014-10-16 11:14:05 -07:00
|
|
|
if (aIsSrcdocChannel) {
|
|
|
|
nsCOMPtr<nsIInputStreamChannel> inStrmChan = do_QueryInterface(channel);
|
|
|
|
NS_ENSURE_TRUE(inStrmChan, NS_ERROR_FAILURE);
|
|
|
|
inStrmChan->SetSrcdocData(aData);
|
|
|
|
}
|
|
|
|
channel.forget(outChannel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-28 20:04:42 -07:00
|
|
|
|
2014-10-16 11:14:05 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewInputStreamChannel(nsIChannel** outChannel,
|
|
|
|
nsIURI* aUri,
|
|
|
|
const nsAString& aData,
|
|
|
|
const nsACString& aContentType,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-10-16 11:14:05 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
2014-11-04 16:34:00 -08:00
|
|
|
bool aIsSrcdocChannel = false,
|
|
|
|
nsIURI* aBaseURI = nullptr)
|
2014-10-16 11:14:05 -07:00
|
|
|
{
|
|
|
|
return NS_NewInputStreamChannelInternal(outChannel,
|
|
|
|
aUri,
|
|
|
|
aData,
|
|
|
|
aContentType,
|
2014-12-12 09:05:09 -08:00
|
|
|
nullptr, // aLoadingNode
|
|
|
|
aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
nullptr, // aTriggeringPrincipal
|
2014-10-16 11:14:05 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
2014-11-04 16:34:00 -08:00
|
|
|
aIsSrcdocChannel,
|
|
|
|
aBaseURI);
|
2013-06-28 20:04:42 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewInputStreamPump(nsIInputStreamPump **result,
|
|
|
|
nsIInputStream *stream,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t streamPos = int64_t(-1),
|
|
|
|
int64_t streamLen = int64_t(-1),
|
|
|
|
uint32_t segsize = 0,
|
|
|
|
uint32_t segcount = 0,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool closeWhenDone = false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump =
|
|
|
|
do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = pump->Init(stream, streamPos, streamLen,
|
|
|
|
segsize, segcount, closeWhenDone);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
pump.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: you will need to specify whether or not your streams are buffered
|
|
|
|
// (i.e., do they implement ReadSegments/WriteSegments). the default
|
|
|
|
// assumption of TRUE for both streams might not be right for you!
|
|
|
|
inline nsresult
|
|
|
|
NS_NewAsyncStreamCopier(nsIAsyncStreamCopier **result,
|
|
|
|
nsIInputStream *source,
|
|
|
|
nsIOutputStream *sink,
|
|
|
|
nsIEventTarget *target,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sourceBuffered = true,
|
|
|
|
bool sinkBuffered = true,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t chunkSize = 0,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool closeSource = true,
|
|
|
|
bool closeSink = true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIAsyncStreamCopier> copier =
|
|
|
|
do_CreateInstance(NS_ASYNCSTREAMCOPIER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-03-30 12:10:13 -07:00
|
|
|
rv = copier->Init(source, sink, target, sourceBuffered, sinkBuffered,
|
|
|
|
chunkSize, closeSource, closeSink);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
copier.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewLoadGroup(nsILoadGroup **result,
|
|
|
|
nsIRequestObserver *obs)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILoadGroup> group =
|
|
|
|
do_CreateInstance(NS_LOADGROUP_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = group->SetGroupObserver(obs);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
group.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-12-15 05:39:00 -08:00
|
|
|
// Create a new nsILoadGroup that will match the given principal.
|
|
|
|
nsresult
|
|
|
|
NS_NewLoadGroup(nsILoadGroup** aResult, nsIPrincipal* aPrincipal);
|
|
|
|
|
|
|
|
// Determine if the given loadGroup/principal pair will produce a principal
|
|
|
|
// with similar permissions when passed to NS_NewChannel(). This checks for
|
|
|
|
// things like making sure the appId and browser element flags match. Without
|
|
|
|
// an appropriate load group these values can be lost when getting the result
|
|
|
|
// principal back out of the channel. Null principals are also always allowed
|
|
|
|
// as they do not have permissions to actually use the load group.
|
|
|
|
bool
|
|
|
|
NS_LoadGroupMatchesPrincipal(nsILoadGroup* aLoadGroup,
|
|
|
|
nsIPrincipal* aPrincipal);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewDownloader(nsIStreamListener **result,
|
|
|
|
nsIDownloadObserver *observer,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIFile *downloadLocation = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDownloader> downloader =
|
|
|
|
do_CreateInstance(NS_DOWNLOADER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = downloader->Init(observer, downloadLocation);
|
2015-03-31 07:03:49 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
downloader.forget(result);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewStreamLoader(nsIStreamLoader **result,
|
2015-05-04 07:17:47 -07:00
|
|
|
nsIStreamLoaderObserver *observer,
|
|
|
|
nsIRequestObserver *requestObserver = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader =
|
|
|
|
do_CreateInstance(NS_STREAMLOADER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-05-04 07:17:47 -07:00
|
|
|
rv = loader->Init(observer, requestObserver);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
loader.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_NewStreamLoaderInternal(nsIStreamLoader** outStream,
|
|
|
|
nsIURI* aUri,
|
|
|
|
nsIStreamLoaderObserver* aObserver,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsINode* aLoadingNode,
|
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsISupports* aContext = nullptr,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIURI* aReferrer = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-17 14:52:25 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv = NS_NewChannelInternal(getter_AddRefs(channel),
|
|
|
|
aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
aLoadingNode,
|
|
|
|
aLoadingPrincipal,
|
2014-11-14 08:56:39 -08:00
|
|
|
nullptr, // aTriggeringPrincipal
|
2014-07-17 14:52:25 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel) {
|
|
|
|
httpChannel->SetReferrer(aReferrer);
|
|
|
|
}
|
|
|
|
rv = NS_NewStreamLoader(outStream, aObserver);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return channel->AsyncOpen(*outStream, aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline nsresult /* NS_NewStreamLoaderNode */
|
|
|
|
NS_NewStreamLoader(nsIStreamLoader** outStream,
|
|
|
|
nsIURI* aUri,
|
|
|
|
nsIStreamLoaderObserver* aObserver,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsINode* aLoadingNode,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsISupports* aContext = nullptr,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIURI* aReferrer = nullptr)
|
|
|
|
{
|
2014-12-12 09:05:09 -08:00
|
|
|
NS_ASSERTION(aLoadingNode, "Can not create stream loader without a loading Node!");
|
2014-07-17 14:52:25 -07:00
|
|
|
return NS_NewStreamLoaderInternal(outStream,
|
|
|
|
aUri,
|
|
|
|
aObserver,
|
2014-12-12 09:05:09 -08:00
|
|
|
aLoadingNode,
|
|
|
|
aLoadingNode->NodePrincipal(),
|
2014-07-17 14:52:25 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aContext,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aReferrer);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult /* NS_NewStreamLoaderPrincipal */
|
|
|
|
NS_NewStreamLoader(nsIStreamLoader** outStream,
|
|
|
|
nsIURI* aUri,
|
|
|
|
nsIStreamLoaderObserver* aObserver,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsSecurityFlags aSecurityFlags,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsISupports* aContext = nullptr,
|
|
|
|
nsILoadGroup* aLoadGroup = nullptr,
|
|
|
|
nsIInterfaceRequestor* aCallbacks = nullptr,
|
|
|
|
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIURI* aReferrer = nullptr)
|
|
|
|
{
|
|
|
|
return NS_NewStreamLoaderInternal(outStream,
|
|
|
|
aUri,
|
|
|
|
aObserver,
|
2014-12-12 09:05:09 -08:00
|
|
|
nullptr, // aLoadingNode
|
|
|
|
aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
aContext,
|
|
|
|
aLoadGroup,
|
|
|
|
aCallbacks,
|
|
|
|
aLoadFlags,
|
|
|
|
aReferrer);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **result,
|
2011-05-18 18:22:59 -07:00
|
|
|
nsIUnicharStreamLoaderObserver *observer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUnicharStreamLoader> loader =
|
|
|
|
do_CreateInstance(NS_UNICHARSTREAMLOADER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-05-18 18:22:59 -07:00
|
|
|
rv = loader->Init(observer);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
loader.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewSyncStreamListener(nsIStreamListener **result,
|
|
|
|
nsIInputStream **stream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISyncStreamListener> listener =
|
|
|
|
do_CreateInstance(NS_SYNCSTREAMLISTENER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = listener->GetInputStream(stream);
|
2015-03-31 07:03:49 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
listener.forget(result);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement the nsIChannel::Open(nsIInputStream**) method using the channel's
|
|
|
|
* AsyncOpen method.
|
|
|
|
*
|
|
|
|
* NOTE: Reading from the returned nsIInputStream may spin the current
|
|
|
|
* thread's event queue, which could result in any event being processed.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_ImplementChannelOpen(nsIChannel *channel,
|
|
|
|
nsIInputStream **result)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = NS_NewSyncStreamListener(getter_AddRefs(listener),
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = channel->AsyncOpen(listener, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t n;
|
2007-03-22 10:30:00 -07:00
|
|
|
// block until the initial response is received or an error occurs.
|
|
|
|
rv = stream->Available(&n);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
stream.swap(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewRequestObserverProxy(nsIRequestObserver **result,
|
2013-03-19 09:04:57 -07:00
|
|
|
nsIRequestObserver *observer,
|
|
|
|
nsISupports *context)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIRequestObserverProxy> proxy =
|
|
|
|
do_CreateInstance(NS_REQUESTOBSERVERPROXY_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2013-03-19 09:04:57 -07:00
|
|
|
rv = proxy->Init(observer, context);
|
2015-03-31 07:03:49 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
proxy.forget(result);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewSimpleStreamListener(nsIStreamListener **result,
|
|
|
|
nsIOutputStream *sink,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIRequestObserver *observer = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISimpleStreamListener> listener =
|
|
|
|
do_CreateInstance(NS_SIMPLESTREAMLISTENER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = listener->Init(sink, observer);
|
2015-03-31 07:03:49 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
listener.forget(result);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_CheckPortSafety(int32_t port,
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *scheme,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *ioService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool allow;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = ioService->AllowPort(port, scheme, &allow);
|
|
|
|
if (NS_SUCCEEDED(rv) && !allow) {
|
|
|
|
NS_WARNING("port blocked");
|
|
|
|
rv = NS_ERROR_PORT_ACCESS_NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this URI is using a safe port.
|
|
|
|
inline nsresult
|
|
|
|
NS_CheckPortSafety(nsIURI *uri) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t port;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = uri->GetPort(&port);
|
|
|
|
if (NS_FAILED(rv) || port == -1) // port undefined or default-valued
|
|
|
|
return NS_OK;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString scheme;
|
2007-03-22 10:30:00 -07:00
|
|
|
uri->GetScheme(scheme);
|
|
|
|
return NS_CheckPortSafety(port, scheme.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewProxyInfo(const nsACString &type,
|
|
|
|
const nsACString &host,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t port,
|
|
|
|
uint32_t flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIProxyInfo **result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIProtocolProxyService> pps =
|
|
|
|
do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2012-09-27 23:57:33 -07:00
|
|
|
rv = pps->NewProxyInfo(type, host, port, flags, UINT32_MAX, nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_GetFileProtocolHandler(nsIFileProtocolHandler **result,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *ioService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
rv = ioService->GetProtocolHandler("file", getter_AddRefs(handler));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = CallQueryInterface(handler, result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_GetFileFromURLSpec(const nsACString &inURL,
|
|
|
|
nsIFile **result,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *ioService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetFileFromURLSpec(inURL, result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_GetURLSpecFromFile(nsIFile *file,
|
|
|
|
nsACString &url,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *ioService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetURLSpecFromFile(file, url);
|
|
|
|
return rv;
|
2007-11-23 12:20:41 -08:00
|
|
|
}
|
|
|
|
|
2009-10-06 06:43:59 -07:00
|
|
|
/**
|
|
|
|
* Converts the nsIFile to the corresponding URL string.
|
|
|
|
* Should only be called on files which are not directories,
|
|
|
|
* is otherwise identical to NS_GetURLSpecFromFile, but is
|
|
|
|
* usually more efficient.
|
|
|
|
* Warning: this restriction may not be enforced at runtime!
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetURLSpecFromActualFile(nsIFile *file,
|
|
|
|
nsACString &url,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *ioService = nullptr)
|
2009-10-06 06:43:59 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetURLSpecFromActualFile(file, url);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the nsIFile to the corresponding URL string.
|
|
|
|
* Should only be called on files which are directories,
|
|
|
|
* is otherwise identical to NS_GetURLSpecFromFile, but is
|
|
|
|
* usually more efficient.
|
|
|
|
* Warning: this restriction may not be enforced at runtime!
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetURLSpecFromDir(nsIFile *file,
|
|
|
|
nsACString &url,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *ioService = nullptr)
|
2009-10-06 06:43:59 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetURLSpecFromDir(file, url);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-11-23 12:20:41 -08:00
|
|
|
/**
|
|
|
|
* Obtains the referrer for a given channel. This first tries to obtain the
|
|
|
|
* referrer from the property docshell.internalReferrer, and if that doesn't
|
|
|
|
* work and the channel is an nsIHTTPChannel, we check it's referrer property.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_NOT_AVAILABLE if no referrer is available.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetReferrerFromChannel(nsIChannel *channel,
|
|
|
|
nsIURI **referrer)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
2012-07-30 07:20:58 -07:00
|
|
|
*referrer = nullptr;
|
2007-11-23 12:20:41 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props(do_QueryInterface(channel));
|
|
|
|
if (props) {
|
|
|
|
// We have to check for a property on a property bag because the
|
|
|
|
// referrer may be empty for security reasons (for example, when loading
|
|
|
|
// an http page with an https referrer).
|
|
|
|
rv = props->GetPropertyAsInterface(NS_LITERAL_STRING("docshell.internalReferrer"),
|
|
|
|
NS_GET_IID(nsIURI),
|
|
|
|
reinterpret_cast<void **>(referrer));
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 07:20:58 -07:00
|
|
|
*referrer = nullptr;
|
2007-11-23 12:20:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if that didn't work, we can still try to get the referrer from the
|
|
|
|
// nsIHttpChannel (if we can QI to it)
|
|
|
|
if (!(*referrer)) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> chan(do_QueryInterface(channel));
|
|
|
|
if (chan) {
|
|
|
|
rv = chan->GetReferrer(referrer);
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 07:20:58 -07:00
|
|
|
*referrer = nullptr;
|
2007-11-23 12:20:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_ParseContentType(const nsACString &rawContentType,
|
|
|
|
nsCString &contentType,
|
|
|
|
nsCString &contentCharset)
|
|
|
|
{
|
|
|
|
// contentCharset is left untouched if not present in rawContentType
|
|
|
|
nsresult rv;
|
2010-04-12 08:44:28 -07:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCString charset;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hadCharset;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = util->ParseContentType(rawContentType, charset, &hadCharset,
|
|
|
|
contentType);
|
|
|
|
if (NS_SUCCEEDED(rv) && hadCharset)
|
|
|
|
contentCharset = charset;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-12-03 13:34:44 -08:00
|
|
|
inline nsresult
|
|
|
|
NS_ExtractCharsetFromContentType(const nsACString &rawContentType,
|
|
|
|
nsCString &contentCharset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *hadCharset,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *charsetStart,
|
|
|
|
int32_t *charsetEnd)
|
2007-12-03 13:34:44 -08:00
|
|
|
{
|
|
|
|
// contentCharset is left untouched if not present in rawContentType
|
|
|
|
nsresult rv;
|
2010-04-12 08:44:28 -07:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
2007-12-03 13:34:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return util->ExtractCharsetFromContentType(rawContentType,
|
|
|
|
contentCharset,
|
|
|
|
charsetStart,
|
|
|
|
charsetEnd,
|
|
|
|
hadCharset);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewLocalFileInputStream(nsIInputStream **result,
|
|
|
|
nsIFile *file,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ioFlags = -1,
|
|
|
|
int32_t perm = -1,
|
|
|
|
int32_t behaviorFlags = 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileInputStream> in =
|
|
|
|
do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = in->Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2012-05-22 21:23:11 -07:00
|
|
|
in.forget(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-10-13 16:25:33 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewPartialLocalFileInputStream(nsIInputStream **result,
|
|
|
|
nsIFile *file,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t offset,
|
|
|
|
uint64_t length,
|
|
|
|
int32_t ioFlags = -1,
|
|
|
|
int32_t perm = -1,
|
|
|
|
int32_t behaviorFlags = 0)
|
2010-10-13 16:25:33 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPartialFileInputStream> in =
|
|
|
|
do_CreateInstance(NS_PARTIALLOCALFILEINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = in->Init(file, offset, length, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = CallQueryInterface(in, result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_NewLocalFileOutputStream(nsIOutputStream **result,
|
|
|
|
nsIFile *file,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ioFlags = -1,
|
|
|
|
int32_t perm = -1,
|
|
|
|
int32_t behaviorFlags = 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileOutputStream> out =
|
|
|
|
do_CreateInstance(NS_LOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2012-05-22 21:23:11 -07:00
|
|
|
out.forget(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-12-02 09:51:25 -08:00
|
|
|
// returns a file output stream which can be QI'ed to nsISafeOutputStream.
|
|
|
|
inline nsresult
|
|
|
|
NS_NewAtomicFileOutputStream(nsIOutputStream **result,
|
|
|
|
nsIFile *file,
|
|
|
|
int32_t ioFlags = -1,
|
|
|
|
int32_t perm = -1,
|
|
|
|
int32_t behaviorFlags = 0)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileOutputStream> out =
|
|
|
|
do_CreateInstance(NS_ATOMICLOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
out.forget(result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// returns a file output stream which can be QI'ed to nsISafeOutputStream.
|
|
|
|
inline nsresult
|
|
|
|
NS_NewSafeLocalFileOutputStream(nsIOutputStream **result,
|
|
|
|
nsIFile *file,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ioFlags = -1,
|
|
|
|
int32_t perm = -1,
|
|
|
|
int32_t behaviorFlags = 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileOutputStream> out =
|
|
|
|
do_CreateInstance(NS_SAFELOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2012-05-22 21:23:11 -07:00
|
|
|
out.forget(result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewLocalFileStream(nsIFileStream **result,
|
|
|
|
nsIFile *file,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ioFlags = -1,
|
|
|
|
int32_t perm = -1,
|
|
|
|
int32_t behaviorFlags = 0)
|
2012-05-22 21:23:11 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileStream> stream =
|
|
|
|
do_CreateInstance(NS_LOCALFILESTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = stream->Init(file, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
stream.forget(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the input end of a pipe. the output end of the pipe
|
|
|
|
// is attached to the original stream. data from the original
|
|
|
|
// stream is read into the pipe on a background thread.
|
|
|
|
inline nsresult
|
|
|
|
NS_BackgroundInputStream(nsIInputStream **result,
|
|
|
|
nsIInputStream *stream,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t segmentSize = 0,
|
|
|
|
uint32_t segmentCount = 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITransport> inTransport;
|
2012-08-22 08:56:38 -07:00
|
|
|
rv = sts->CreateInputTransport(stream, int64_t(-1), int64_t(-1),
|
2011-10-17 07:59:28 -07:00
|
|
|
true, getter_AddRefs(inTransport));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = inTransport->OpenInputStream(nsITransport::OPEN_BLOCKING,
|
|
|
|
segmentSize, segmentCount,
|
|
|
|
result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the output end of a pipe. the input end of the pipe
|
|
|
|
// is attached to the original stream. data written to the pipe
|
|
|
|
// is copied to the original stream on a background thread.
|
|
|
|
inline nsresult
|
|
|
|
NS_BackgroundOutputStream(nsIOutputStream **result,
|
|
|
|
nsIOutputStream *stream,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t segmentSize = 0,
|
|
|
|
uint32_t segmentCount = 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITransport> inTransport;
|
2012-08-22 08:56:38 -07:00
|
|
|
rv = sts->CreateOutputTransport(stream, int64_t(-1), int64_t(-1),
|
2011-10-17 07:59:28 -07:00
|
|
|
true, getter_AddRefs(inTransport));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = inTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING,
|
|
|
|
segmentSize, segmentCount,
|
|
|
|
result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-10-16 01:38:20 -07:00
|
|
|
MOZ_WARN_UNUSED_RESULT inline nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewBufferedInputStream(nsIInputStream **result,
|
|
|
|
nsIInputStream *str,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bufferSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIBufferedInputStream> in =
|
|
|
|
do_CreateInstance(NS_BUFFEREDINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = in->Init(str, bufferSize);
|
2015-03-31 07:03:49 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
in.forget(result);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: the resulting stream can be QI'ed to nsISafeOutputStream iff the
|
|
|
|
// provided stream supports it.
|
|
|
|
inline nsresult
|
|
|
|
NS_NewBufferedOutputStream(nsIOutputStream **result,
|
|
|
|
nsIOutputStream *str,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bufferSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIBufferedOutputStream> out =
|
|
|
|
do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Init(str, bufferSize);
|
2015-03-31 07:03:49 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
out.forget(result);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-01-04 09:55:02 -08:00
|
|
|
/**
|
|
|
|
* Attempts to buffer a given output stream. If this fails, it returns the
|
|
|
|
* passed-in output stream.
|
|
|
|
*
|
|
|
|
* @param aOutputStream
|
|
|
|
* The output stream we want to buffer. This cannot be null.
|
|
|
|
* @param aBufferSize
|
|
|
|
* The size of the buffer for the buffered output stream.
|
|
|
|
* @returns an nsIOutputStream that is buffered with the specified buffer size,
|
|
|
|
* or is aOutputStream if creating the new buffered stream failed.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<nsIOutputStream>
|
|
|
|
NS_BufferOutputStream(nsIOutputStream *aOutputStream,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aBufferSize)
|
2008-01-04 09:55:02 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aOutputStream, "No output stream given!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> bos;
|
|
|
|
nsresult rv = NS_NewBufferedOutputStream(getter_AddRefs(bos), aOutputStream,
|
|
|
|
aBufferSize);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return bos.forget();
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
bos = aOutputStream;
|
|
|
|
return bos.forget();
|
2008-01-04 09:55:02 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// returns an input stream compatible with nsIUploadChannel::SetUploadStream()
|
|
|
|
inline nsresult
|
|
|
|
NS_NewPostDataStream(nsIInputStream **result,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFile,
|
2013-03-08 05:30:13 -08:00
|
|
|
const nsACString &data)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (isFile) {
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = NS_NewNativeLocalFile(data, false, getter_AddRefs(file));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileStream), file);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// wrap the file stream with a buffered input stream
|
|
|
|
rv = NS_NewBufferedInputStream(result, fileStream, 8192);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, create a string stream for the data (copies)
|
|
|
|
nsCOMPtr<nsIStringInputStream> stream
|
|
|
|
(do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = stream->SetData(data.BeginReading(), data.Length());
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2015-03-31 07:03:49 -07:00
|
|
|
stream.forget(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-30 15:30:28 -07:00
|
|
|
inline nsresult
|
2011-06-08 11:11:41 -07:00
|
|
|
NS_ReadInputStreamToBuffer(nsIInputStream *aInputStream,
|
|
|
|
void** aDest,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount)
|
2010-05-30 15:30:28 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-06-08 11:11:41 -07:00
|
|
|
if (!*aDest) {
|
|
|
|
*aDest = malloc(aCount);
|
|
|
|
if (!*aDest)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * p = reinterpret_cast<char*>(*aDest);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesRead;
|
|
|
|
uint32_t totalRead = 0;
|
2010-05-30 15:30:28 -07:00
|
|
|
while (1) {
|
|
|
|
rv = aInputStream->Read(p + totalRead, aCount - totalRead, &bytesRead);
|
|
|
|
if (!NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
totalRead += bytesRead;
|
|
|
|
if (totalRead == aCount)
|
|
|
|
break;
|
|
|
|
// if Read reads 0 bytes, we've hit EOF
|
|
|
|
if (bytesRead == 0)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-05-14 14:35:49 -07:00
|
|
|
// external code can't see fallible_t
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
|
2011-06-08 11:11:41 -07:00
|
|
|
inline nsresult
|
|
|
|
NS_ReadInputStreamToString(nsIInputStream *aInputStream,
|
|
|
|
nsACString &aDest,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount)
|
2011-06-08 11:11:41 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!aDest.SetLength(aCount, mozilla::fallible))
|
2011-06-08 11:11:41 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
void* dest = aDest.BeginWriting();
|
|
|
|
return NS_ReadInputStreamToBuffer(aInputStream, &dest, aCount);
|
|
|
|
}
|
|
|
|
|
2012-05-14 14:35:49 -07:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
inline nsresult
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties** outResult,
|
|
|
|
nsIURI* aUri,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2015-01-11 20:26:40 -08:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aUri,
|
|
|
|
aLoadingPrincipal,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
aContentPolicyType,
|
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
aIoService);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
2015-01-11 20:26:40 -08:00
|
|
|
rv = channel->Open(getter_AddRefs(in));
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-01-11 20:26:40 -08:00
|
|
|
|
2014-07-17 14:52:25 -07:00
|
|
|
nsCOMPtr<nsIPersistentProperties> properties =
|
|
|
|
do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = properties->Load(in);
|
2015-01-11 20:26:40 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
properties.swap(*outResult);
|
|
|
|
return NS_OK;
|
2014-07-17 14:52:25 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
inline nsresult
|
2014-07-17 14:52:25 -07:00
|
|
|
NS_LoadPersistentPropertiesFromURISpec(nsIPersistentProperties** outResult,
|
|
|
|
const nsACString& aSpec,
|
2014-12-12 09:05:09 -08:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
const char* aCharset = nullptr,
|
|
|
|
nsIURI* aBaseURI = nullptr,
|
|
|
|
nsIIOService* aIoService = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2014-07-17 14:52:25 -07:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri),
|
|
|
|
aSpec,
|
|
|
|
aCharset,
|
|
|
|
aBaseURI,
|
|
|
|
aIoService);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-17 14:52:25 -07:00
|
|
|
return NS_LoadPersistentPropertiesFromURI(outResult,
|
|
|
|
uri,
|
2014-12-12 09:05:09 -08:00
|
|
|
aLoadingPrincipal,
|
2014-07-17 14:52:25 -07:00
|
|
|
aContentPolicyType,
|
|
|
|
aIoService);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NS_QueryNotificationCallbacks implements the canonical algorithm for
|
|
|
|
* querying interfaces from a channel's notification callbacks. It first
|
|
|
|
* searches the channel's notificationCallbacks attribute, and if the interface
|
|
|
|
* is not found there, then it inspects the notificationCallbacks attribute of
|
|
|
|
* the channel's loadGroup.
|
2012-08-06 21:47:48 -07:00
|
|
|
*
|
|
|
|
* Note: templatized only because nsIWebSocketChannel is currently not an
|
|
|
|
* nsIChannel.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-08-06 21:47:48 -07:00
|
|
|
template <class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(T *channel,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsIID &iid,
|
|
|
|
void **result)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(channel, "null channel");
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
|
|
|
channel->GetNotificationCallbacks(getter_AddRefs(cbs));
|
|
|
|
if (cbs)
|
|
|
|
cbs->GetInterface(iid, result);
|
|
|
|
if (!*result) {
|
|
|
|
// try load group's notification callbacks...
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
channel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (loadGroup) {
|
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
|
|
|
if (cbs)
|
|
|
|
cbs->GetInterface(iid, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 21:47:48 -07:00
|
|
|
// template helper:
|
|
|
|
// Note: "class C" templatized only because nsIWebSocketChannel is currently not
|
|
|
|
// an nsIChannel.
|
|
|
|
|
|
|
|
template <class C, class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(C *channel,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<T> &result)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(channel, NS_GET_TEMPLATE_IID(T),
|
|
|
|
getter_AddRefs(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternate form of NS_QueryNotificationCallbacks designed for use by
|
|
|
|
* nsIChannel implementations.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_QueryNotificationCallbacks(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
|
|
|
const nsIID &iid,
|
|
|
|
void **result)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (callbacks)
|
|
|
|
callbacks->GetInterface(iid, result);
|
|
|
|
if (!*result) {
|
|
|
|
// try load group's notification callbacks...
|
|
|
|
if (loadGroup) {
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
|
|
|
if (cbs)
|
|
|
|
cbs->GetInterface(iid, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-22 15:35:33 -07:00
|
|
|
/**
|
|
|
|
* Returns true if channel is using Private Browsing, or false if not.
|
2012-07-23 11:12:03 -07:00
|
|
|
* Returns false if channel's callbacks don't implement nsILoadContext.
|
2012-07-22 15:35:33 -07:00
|
|
|
*/
|
|
|
|
inline bool
|
|
|
|
NS_UsePrivateBrowsing(nsIChannel *channel)
|
|
|
|
{
|
2012-09-04 17:37:45 -07:00
|
|
|
bool isPrivate = false;
|
|
|
|
bool isOverriden = false;
|
|
|
|
nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(channel);
|
|
|
|
if (pbChannel &&
|
|
|
|
NS_SUCCEEDED(pbChannel->IsPrivateModeOverriden(&isPrivate, &isOverriden)) &&
|
|
|
|
isOverriden) {
|
|
|
|
return isPrivate;
|
|
|
|
}
|
2012-07-23 11:12:03 -07:00
|
|
|
nsCOMPtr<nsILoadContext> loadContext;
|
|
|
|
NS_QueryNotificationCallbacks(channel, loadContext);
|
|
|
|
return loadContext && loadContext->UsePrivateBrowsing();
|
|
|
|
}
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
// Constants duplicated from nsIScriptSecurityManager so we avoid having necko
|
|
|
|
// know about script security manager.
|
|
|
|
#define NECKO_NO_APP_ID 0
|
2012-10-01 21:44:29 -07:00
|
|
|
#define NECKO_UNKNOWN_APP_ID UINT32_MAX
|
2013-08-19 12:31:24 -07:00
|
|
|
// special app id reserved for separating the safebrowsing cookie
|
|
|
|
#define NECKO_SAFEBROWSING_APP_ID UINT32_MAX - 1
|
2012-09-18 09:04:04 -07:00
|
|
|
|
2012-07-23 11:12:03 -07:00
|
|
|
/**
|
|
|
|
* Gets AppId and isInBrowserElement from channel's nsILoadContext.
|
|
|
|
* Returns false if error or channel's callbacks don't implement nsILoadContext.
|
|
|
|
*/
|
|
|
|
inline bool
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_GetAppInfo(nsIChannel *aChannel, uint32_t *aAppID, bool *aIsInBrowserElement)
|
2012-07-23 11:12:03 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext;
|
|
|
|
NS_QueryNotificationCallbacks(aChannel, loadContext);
|
|
|
|
if (!loadContext) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = loadContext->GetAppId(aAppID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
rv = loadContext->GetIsInBrowserElement(aIsInBrowserElement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
return true;
|
2012-07-22 15:35:33 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:09 -07:00
|
|
|
/**
|
|
|
|
* Gets appId and browserOnly parameters from the TOPIC_WEB_APP_CLEAR_DATA
|
|
|
|
* nsIObserverService notification. Used when clearing user data or
|
|
|
|
* uninstalling web apps.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetAppInfoFromClearDataNotification(nsISupports *aSubject,
|
|
|
|
uint32_t *aAppID, bool* aBrowserOnly)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIApplicationClearPrivateDataParams>
|
|
|
|
clearParams(do_QueryInterface(aSubject));
|
|
|
|
MOZ_ASSERT(clearParams);
|
|
|
|
if (!clearParams) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t appId;
|
|
|
|
rv = clearParams->GetAppId(&appId);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(appId != NECKO_UNKNOWN_APP_ID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-10-23 09:31:19 -07:00
|
|
|
if (appId == NECKO_UNKNOWN_APP_ID) {
|
2012-10-02 17:39:09 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool browserOnly = false;
|
|
|
|
rv = clearParams->GetBrowserOnly(&browserOnly);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aAppID = appId;
|
|
|
|
*aBrowserOnly = browserOnly;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-17 22:00:16 -07:00
|
|
|
/**
|
|
|
|
* Determines whether appcache should be checked for a given URI.
|
|
|
|
*/
|
|
|
|
inline bool
|
|
|
|
NS_ShouldCheckAppCache(nsIURI *aURI, bool usePrivateBrowsing)
|
|
|
|
{
|
|
|
|
if (usePrivateBrowsing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdateService> offlineService =
|
|
|
|
do_GetService("@mozilla.org/offlinecacheupdate-service;1");
|
|
|
|
if (!offlineService) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allowed;
|
|
|
|
nsresult rv = offlineService->OfflineAppAllowedForURI(aURI,
|
|
|
|
nullptr,
|
|
|
|
&allowed);
|
|
|
|
return NS_SUCCEEDED(rv) && allowed;
|
2014-02-03 16:52:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
NS_ShouldCheckAppCache(nsIPrincipal * aPrincipal, bool usePrivateBrowsing)
|
|
|
|
{
|
|
|
|
if (usePrivateBrowsing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOfflineCacheUpdateService> offlineService =
|
|
|
|
do_GetService("@mozilla.org/offlinecacheupdate-service;1");
|
|
|
|
if (!offlineService) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allowed;
|
|
|
|
nsresult rv = offlineService->OfflineAppAllowed(aPrincipal,
|
|
|
|
nullptr,
|
|
|
|
&allowed);
|
|
|
|
return NS_SUCCEEDED(rv) && allowed;
|
2012-10-17 22:00:16 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This
|
|
|
|
* method is provided mainly for use by other methods in this file.
|
|
|
|
*
|
|
|
|
* *aAuthPrompt2 should be set to null before calling this function.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_WrapAuthPrompt(nsIAuthPrompt *aAuthPrompt, nsIAuthPrompt2** aAuthPrompt2)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAuthPromptAdapterFactory> factory =
|
|
|
|
do_GetService(NS_AUTHPROMPT_ADAPTER_FACTORY_CONTRACTID);
|
|
|
|
if (!factory)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_WARNING("Using deprecated nsIAuthPrompt");
|
|
|
|
factory->CreateAdapter(aAuthPrompt, aAuthPrompt2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an auth prompt from an interface requestor. This takes care of wrapping
|
|
|
|
* an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_QueryAuthPrompt2(nsIInterfaceRequestor *aCallbacks,
|
|
|
|
nsIAuthPrompt2 **aAuthPrompt)
|
|
|
|
{
|
|
|
|
CallGetInterface(aCallbacks, aAuthPrompt);
|
|
|
|
if (*aAuthPrompt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Maybe only nsIAuthPrompt is provided and we have to wrap it.
|
|
|
|
nsCOMPtr<nsIAuthPrompt> prompt(do_GetInterface(aCallbacks));
|
|
|
|
if (!prompt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_WrapAuthPrompt(prompt, aAuthPrompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an nsIAuthPrompt2 from a channel. Use this instead of
|
|
|
|
* NS_QueryNotificationCallbacks for better backwards compatibility.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_QueryAuthPrompt2(nsIChannel *aChannel,
|
|
|
|
nsIAuthPrompt2 **aAuthPrompt)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aAuthPrompt = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We want to use any auth prompt we can find on the channel's callbacks,
|
|
|
|
// and if that fails use the loadgroup's prompt (if any)
|
|
|
|
// Therefore, we can't just use NS_QueryNotificationCallbacks, because
|
|
|
|
// that would prefer a loadgroup's nsIAuthPrompt2 over a channel's
|
|
|
|
// nsIAuthPrompt.
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
aChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
if (callbacks) {
|
|
|
|
NS_QueryAuthPrompt2(callbacks, aAuthPrompt);
|
|
|
|
if (*aAuthPrompt)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> group;
|
|
|
|
aChannel->GetLoadGroup(getter_AddRefs(group));
|
|
|
|
if (!group)
|
|
|
|
return;
|
|
|
|
|
|
|
|
group->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
if (!callbacks)
|
|
|
|
return;
|
|
|
|
NS_QueryAuthPrompt2(callbacks, aAuthPrompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
|
|
|
nsCOMPtr<T> &result)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(callbacks, loadGroup,
|
|
|
|
NS_GET_TEMPLATE_IID(T),
|
|
|
|
getter_AddRefs(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(const nsCOMPtr<nsIInterfaceRequestor> &aCallbacks,
|
|
|
|
const nsCOMPtr<nsILoadGroup> &aLoadGroup,
|
|
|
|
nsCOMPtr<T> &aResult)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(aCallbacks.get(), aLoadGroup.get(), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(const nsCOMPtr<nsIChannel> &aChannel,
|
|
|
|
nsCOMPtr<T> &aResult)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(aChannel.get(), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns a nsIInterfaceRequestor instance that returns the
|
|
|
|
* same result as NS_QueryNotificationCallbacks when queried. It is useful
|
|
|
|
* as the value for nsISocketTransport::securityCallbacks.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_NewNotificationCallbacksAggregation(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
2012-11-14 08:00:44 -08:00
|
|
|
nsIEventTarget *target,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIInterfaceRequestor **result)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
|
|
|
if (loadGroup)
|
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
2012-11-14 08:00:44 -08:00
|
|
|
return NS_NewInterfaceRequestorAggregation(callbacks, cbs, target, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewNotificationCallbacksAggregation(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
|
|
|
nsIInterfaceRequestor **result)
|
|
|
|
{
|
|
|
|
return NS_NewNotificationCallbacksAggregation(callbacks, loadGroup, nullptr, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for testing online/offline state of the browser.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IsOffline()
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool offline = true;
|
2015-05-01 14:14:39 -07:00
|
|
|
bool connectivity = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIIOService> ios = do_GetIOService();
|
2015-05-01 14:14:39 -07:00
|
|
|
if (ios) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ios->GetOffline(&offline);
|
2015-05-01 14:14:39 -07:00
|
|
|
ios->GetConnectivity(&connectivity);
|
|
|
|
}
|
|
|
|
return offline || !connectivity;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-22 20:06:56 -07:00
|
|
|
inline bool
|
|
|
|
NS_IsAppOffline(uint32_t appId)
|
|
|
|
{
|
|
|
|
bool appOffline = false;
|
|
|
|
nsCOMPtr<nsIIOService> io(
|
|
|
|
do_GetService("@mozilla.org/network/io-service;1"));
|
|
|
|
if (io) {
|
|
|
|
io->IsAppOffline(appId, &appOffline);
|
|
|
|
}
|
|
|
|
return appOffline;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
NS_IsAppOffline(nsIPrincipal * principal)
|
|
|
|
{
|
|
|
|
if (!principal) {
|
|
|
|
return NS_IsOffline();
|
|
|
|
}
|
|
|
|
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
|
|
|
principal->GetAppId(&appId);
|
|
|
|
|
|
|
|
return NS_IsAppOffline(appId);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Helper functions for implementing nsINestedURI::innermostURI.
|
|
|
|
*
|
|
|
|
* Note that NS_DoImplGetInnermostURI is "private" -- call
|
|
|
|
* NS_ImplGetInnermostURI instead.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_DoImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nestedURI, "Must have a nested URI!");
|
|
|
|
NS_PRECONDITION(!*result, "Must have null *result");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> inner;
|
|
|
|
nsresult rv = nestedURI->GetInnerURI(getter_AddRefs(inner));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// We may need to loop here until we reach the innermost
|
|
|
|
// URI.
|
|
|
|
nsCOMPtr<nsINestedURI> nestedInner(do_QueryInterface(inner));
|
|
|
|
while (nestedInner) {
|
|
|
|
rv = nestedInner->GetInnerURI(getter_AddRefs(inner));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nestedInner = do_QueryInterface(inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found the innermost one if we reach here.
|
|
|
|
inner.swap(*result);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_ImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result)
|
|
|
|
{
|
|
|
|
// Make it safe to use swap()
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_DoImplGetInnermostURI(nestedURI, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that ensures that |result| is a URI that's safe to
|
|
|
|
* return. If |uri| is immutable, just returns it, otherwise returns
|
|
|
|
* a clone. |uri| must not be null.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_EnsureSafeToReturn(nsIURI* uri, nsIURI** result)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(uri, "Must have a URI");
|
|
|
|
|
|
|
|
// Assume mutable until told otherwise
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isMutable = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(uri));
|
|
|
|
if (mutableObj) {
|
|
|
|
nsresult rv = mutableObj->GetMutable(&isMutable);
|
|
|
|
isMutable = NS_FAILED(rv) || isMutable;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isMutable) {
|
|
|
|
NS_ADDREF(*result = uri);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-04 15:00:00 -07:00
|
|
|
nsresult rv = uri->Clone(result);
|
|
|
|
if (NS_SUCCEEDED(rv) && !*result) {
|
|
|
|
NS_ERROR("nsIURI.clone contract was violated");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that tries to set the argument URI to be immutable
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_TryToSetImmutable(nsIURI* uri)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(uri));
|
|
|
|
if (mutableObj) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mutableObj->SetMutable(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for calling ToImmutableURI. If all else fails, returns
|
|
|
|
* the input URI. The optional second arg indicates whether we had to fall
|
|
|
|
* back to the input URI. Passing in a null URI is ok.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<nsIURI>
|
|
|
|
NS_TryToMakeImmutable(nsIURI* uri,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult* outRv = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2010-04-12 08:44:28 -07:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
nsCOMPtr<nsIURI> result;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-04-12 08:44:28 -07:00
|
|
|
NS_ASSERTION(util, "do_GetNetUtil lied");
|
2013-04-22 04:15:59 -07:00
|
|
|
rv = util->ToImmutableURI(uri, getter_AddRefs(result));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-04-22 04:15:59 -07:00
|
|
|
result = uri;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outRv) {
|
|
|
|
*outRv = rv;
|
|
|
|
}
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
return result.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for testing whether the given URI, or any of its
|
|
|
|
* inner URIs, has all the given protocol flags.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_URIChainHasFlags(nsIURI *uri,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2010-04-12 08:44:28 -07:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return util->URIChainHasFlags(uri, flags, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for getting the innermost URI for a given URI. The return
|
|
|
|
* value could be just the object passed in if it's not a nested URI.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<nsIURI>
|
2013-04-22 04:15:59 -07:00
|
|
|
NS_GetInnermostURI(nsIURI* aURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-22 04:15:59 -07:00
|
|
|
NS_PRECONDITION(aURI, "Must have URI");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri = aURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINestedURI> nestedURI(do_QueryInterface(uri));
|
|
|
|
if (!nestedURI) {
|
2013-04-22 04:15:59 -07:00
|
|
|
return uri.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
nsresult rv = nestedURI->GetInnermostURI(getter_AddRefs(uri));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
return uri.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-10-23 14:56:41 -07:00
|
|
|
/**
|
|
|
|
* Get the "final" URI for a channel. This is either the same as GetURI or
|
|
|
|
* GetOriginalURI, depending on whether this channel has
|
|
|
|
* nsIChanel::LOAD_REPLACE set. For channels without that flag set, the final
|
|
|
|
* URI is the original URI, while for ones with the flag the final URI is the
|
|
|
|
* channel URI.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*uri = nullptr;
|
2007-10-23 14:56:41 -07:00
|
|
|
nsLoadFlags loadFlags = 0;
|
|
|
|
nsresult rv = channel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (loadFlags & nsIChannel::LOAD_REPLACE) {
|
|
|
|
return channel->GetURI(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel->GetOriginalURI(uri);
|
|
|
|
}
|
|
|
|
|
2008-10-08 06:16:27 -07:00
|
|
|
// NS_SecurityHashURI must return the same hash value for any two URIs that
|
|
|
|
// compare equal according to NS_SecurityCompareURIs. Unfortunately, in the
|
|
|
|
// case of files, it's not clear we can do anything better than returning
|
|
|
|
// the schemeHash, so hashing files degenerates to storing them in a list.
|
2012-08-22 08:56:38 -07:00
|
|
|
inline uint32_t
|
2008-10-08 06:16:27 -07:00
|
|
|
NS_SecurityHashURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> baseURI = NS_GetInnermostURI(aURI);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString scheme;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t schemeHash = 0;
|
2008-10-08 06:16:27 -07:00
|
|
|
if (NS_SUCCEEDED(baseURI->GetScheme(scheme)))
|
2012-03-12 15:53:18 -07:00
|
|
|
schemeHash = mozilla::HashString(scheme);
|
2008-10-08 06:16:27 -07:00
|
|
|
|
|
|
|
// TODO figure out how to hash file:// URIs
|
|
|
|
if (scheme.EqualsLiteral("file"))
|
|
|
|
return schemeHash; // sad face
|
|
|
|
|
2011-08-31 17:12:50 -07:00
|
|
|
if (scheme.EqualsLiteral("imap") ||
|
|
|
|
scheme.EqualsLiteral("mailbox") ||
|
|
|
|
scheme.EqualsLiteral("news"))
|
2008-10-08 06:16:27 -07:00
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec;
|
2012-08-14 02:10:42 -07:00
|
|
|
uint32_t specHash;
|
|
|
|
nsresult res = baseURI->GetSpec(spec);
|
|
|
|
if (NS_SUCCEEDED(res))
|
2012-03-12 15:53:18 -07:00
|
|
|
specHash = mozilla::HashString(spec);
|
2012-08-14 02:10:42 -07:00
|
|
|
else
|
|
|
|
specHash = static_cast<uint32_t>(res);
|
2008-10-08 06:16:27 -07:00
|
|
|
return specHash;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t hostHash = 0;
|
2010-10-15 14:55:53 -07:00
|
|
|
if (NS_SUCCEEDED(baseURI->GetAsciiHost(host)))
|
2012-03-12 15:53:18 -07:00
|
|
|
hostHash = mozilla::HashString(host);
|
2008-10-08 06:16:27 -07:00
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
return mozilla::AddToHash(schemeHash, hostHash, NS_GetRealPort(baseURI));
|
2008-10-08 06:16:27 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2008-08-27 18:15:32 -07:00
|
|
|
NS_SecurityCompareURIs(nsIURI* aSourceURI,
|
|
|
|
nsIURI* aTargetURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aStrictFileOriginPolicy)
|
2008-08-27 18:15:32 -07:00
|
|
|
{
|
|
|
|
// Note that this is not an Equals() test on purpose -- for URIs that don't
|
|
|
|
// support host/port, we want equality to basically be object identity, for
|
|
|
|
// security purposes. Otherwise, for example, two javascript: URIs that
|
|
|
|
// are otherwise unrelated could end up "same origin", which would be
|
|
|
|
// unfortunate.
|
|
|
|
if (aSourceURI && aSourceURI == aTargetURI)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-08-27 18:15:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aTargetURI || !aSourceURI)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If either URI is a nested URI, get the base URI
|
|
|
|
nsCOMPtr<nsIURI> sourceBaseURI = NS_GetInnermostURI(aSourceURI);
|
|
|
|
nsCOMPtr<nsIURI> targetBaseURI = NS_GetInnermostURI(aTargetURI);
|
|
|
|
|
2010-03-02 23:51:09 -08:00
|
|
|
// If either uri is an nsIURIWithPrincipal
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(sourceBaseURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
uriPrinc->GetPrincipalUri(getter_AddRefs(sourceBaseURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
uriPrinc = do_QueryInterface(targetBaseURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
uriPrinc->GetPrincipalUri(getter_AddRefs(targetBaseURI));
|
|
|
|
}
|
|
|
|
|
2008-08-27 18:15:32 -07:00
|
|
|
if (!sourceBaseURI || !targetBaseURI)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
|
|
|
|
// Compare schemes
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString targetScheme;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sameScheme = false;
|
2008-08-27 18:15:32 -07:00
|
|
|
if (NS_FAILED( targetBaseURI->GetScheme(targetScheme) ) ||
|
|
|
|
NS_FAILED( sourceBaseURI->SchemeIs(targetScheme.get(), &sameScheme) ) ||
|
|
|
|
!sameScheme)
|
|
|
|
{
|
|
|
|
// Not same-origin if schemes differ
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:33:51 -07:00
|
|
|
// For file scheme, reject unless the files are identical. See
|
|
|
|
// NS_RelaxStrictFileOriginPolicy for enforcing file same-origin checking
|
2008-08-27 18:15:32 -07:00
|
|
|
if (targetScheme.EqualsLiteral("file"))
|
|
|
|
{
|
|
|
|
// in traditional unsafe behavior all files are the same origin
|
|
|
|
if (!aStrictFileOriginPolicy)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-08-27 18:15:32 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFileURL> sourceFileURL(do_QueryInterface(sourceBaseURI));
|
|
|
|
nsCOMPtr<nsIFileURL> targetFileURL(do_QueryInterface(targetBaseURI));
|
|
|
|
|
|
|
|
if (!sourceFileURL || !targetFileURL)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> sourceFile, targetFile;
|
|
|
|
|
|
|
|
sourceFileURL->GetFile(getter_AddRefs(sourceFile));
|
|
|
|
targetFileURL->GetFile(getter_AddRefs(targetFile));
|
|
|
|
|
|
|
|
if (!sourceFile || !targetFile)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
|
|
|
|
// Otherwise they had better match
|
2011-09-28 23:19:26 -07:00
|
|
|
bool filesAreEqual = false;
|
2008-08-27 18:15:32 -07:00
|
|
|
nsresult rv = sourceFile->Equals(targetFile, &filesAreEqual);
|
|
|
|
return NS_SUCCEEDED(rv) && filesAreEqual;
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:12:50 -07:00
|
|
|
// Special handling for mailnews schemes
|
|
|
|
if (targetScheme.EqualsLiteral("imap") ||
|
|
|
|
targetScheme.EqualsLiteral("mailbox") ||
|
|
|
|
targetScheme.EqualsLiteral("news"))
|
2008-08-27 18:15:32 -07:00
|
|
|
{
|
2011-08-31 17:12:50 -07:00
|
|
|
// Each message is a distinct trust domain; use the
|
|
|
|
// whole spec for comparison
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString targetSpec;
|
|
|
|
nsAutoCString sourceSpec;
|
2008-08-27 18:15:32 -07:00
|
|
|
return ( NS_SUCCEEDED( targetBaseURI->GetSpec(targetSpec) ) &&
|
|
|
|
NS_SUCCEEDED( sourceBaseURI->GetSpec(sourceSpec) ) &&
|
|
|
|
targetSpec.Equals(sourceSpec) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare hosts
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString targetHost;
|
|
|
|
nsAutoCString sourceHost;
|
2008-08-27 18:15:32 -07:00
|
|
|
if (NS_FAILED( targetBaseURI->GetAsciiHost(targetHost) ) ||
|
|
|
|
NS_FAILED( sourceBaseURI->GetAsciiHost(sourceHost) ))
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
}
|
|
|
|
|
2010-10-15 14:55:53 -07:00
|
|
|
nsCOMPtr<nsIStandardURL> targetURL(do_QueryInterface(targetBaseURI));
|
|
|
|
nsCOMPtr<nsIStandardURL> sourceURL(do_QueryInterface(sourceBaseURI));
|
|
|
|
if (!targetURL || !sourceURL)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-10-15 14:55:53 -07:00
|
|
|
}
|
|
|
|
|
2008-08-27 18:15:32 -07:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
if (!targetHost.Equals(sourceHost, nsCaseInsensitiveCStringComparator() ))
|
|
|
|
#else
|
|
|
|
if (!targetHost.Equals(sourceHost, CaseInsensitiveCompare))
|
|
|
|
#endif
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-08-27 18:15:32 -07:00
|
|
|
}
|
|
|
|
|
2008-11-03 08:00:54 -08:00
|
|
|
return NS_GetRealPort(targetBaseURI) == NS_GetRealPort(sourceBaseURI);
|
2008-08-27 18:15:32 -07:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:33:51 -07:00
|
|
|
inline bool
|
|
|
|
NS_URIIsLocalFile(nsIURI *aURI)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil();
|
|
|
|
|
|
|
|
bool isFile;
|
|
|
|
return util && NS_SUCCEEDED(util->ProtocolHasFlags(aURI,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_FILE,
|
|
|
|
&isFile)) &&
|
|
|
|
isFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When strict file origin policy is enabled, SecurityCompareURIs will fail for
|
|
|
|
// file URIs that do not point to the same local file. This call provides an
|
|
|
|
// alternate file-specific origin check that allows target files that are
|
|
|
|
// contained in the same directory as the source.
|
|
|
|
//
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs
|
|
|
|
inline bool
|
|
|
|
NS_RelaxStrictFileOriginPolicy(nsIURI *aTargetURI,
|
|
|
|
nsIURI *aSourceURI,
|
|
|
|
bool aAllowDirectoryTarget = false)
|
|
|
|
{
|
|
|
|
if (!NS_URIIsLocalFile(aTargetURI)) {
|
|
|
|
// This is probably not what the caller intended
|
|
|
|
NS_NOTREACHED("NS_RelaxStrictFileOriginPolicy called with non-file URI");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NS_URIIsLocalFile(aSourceURI)) {
|
|
|
|
// If the source is not also a file: uri then forget it
|
|
|
|
// (don't want resource: principals in a file: doc)
|
|
|
|
//
|
|
|
|
// note: we're not de-nesting jar: uris here, we want to
|
|
|
|
// keep archive content bottled up in its own little island
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// pull out the internal files
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIFileURL> targetFileURL(do_QueryInterface(aTargetURI));
|
|
|
|
nsCOMPtr<nsIFileURL> sourceFileURL(do_QueryInterface(aSourceURI));
|
|
|
|
nsCOMPtr<nsIFile> targetFile;
|
|
|
|
nsCOMPtr<nsIFile> sourceFile;
|
|
|
|
bool targetIsDir;
|
|
|
|
|
|
|
|
// Make sure targetFile is not a directory (bug 209234)
|
|
|
|
// and that it exists w/out unescaping (bug 395343)
|
|
|
|
if (!sourceFileURL || !targetFileURL ||
|
|
|
|
NS_FAILED(targetFileURL->GetFile(getter_AddRefs(targetFile))) ||
|
|
|
|
NS_FAILED(sourceFileURL->GetFile(getter_AddRefs(sourceFile))) ||
|
|
|
|
!targetFile || !sourceFile ||
|
|
|
|
NS_FAILED(targetFile->Normalize()) ||
|
|
|
|
#ifndef MOZ_WIDGET_ANDROID
|
|
|
|
NS_FAILED(sourceFile->Normalize()) ||
|
|
|
|
#endif
|
|
|
|
(!aAllowDirectoryTarget &&
|
|
|
|
(NS_FAILED(targetFile->IsDirectory(&targetIsDir)) || targetIsDir))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// If the file to be loaded is in a subdirectory of the source
|
|
|
|
// (or same-dir if source is not a directory) then it will
|
|
|
|
// inherit its source principal and be scriptable by that source.
|
|
|
|
//
|
|
|
|
bool sourceIsDir;
|
2013-08-07 14:35:20 -07:00
|
|
|
bool allowed = false;
|
2013-05-31 14:33:51 -07:00
|
|
|
nsresult rv = sourceFile->IsDirectory(&sourceIsDir);
|
|
|
|
if (NS_SUCCEEDED(rv) && sourceIsDir) {
|
2014-05-07 13:03:41 -07:00
|
|
|
rv = sourceFile->Contains(targetFile, &allowed);
|
2013-05-31 14:33:51 -07:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIFile> sourceParent;
|
|
|
|
rv = sourceFile->GetParent(getter_AddRefs(sourceParent));
|
|
|
|
if (NS_SUCCEEDED(rv) && sourceParent) {
|
2013-08-07 14:35:20 -07:00
|
|
|
rv = sourceParent->Equals(targetFile, &allowed);
|
|
|
|
if (NS_FAILED(rv) || !allowed) {
|
2014-05-07 13:03:41 -07:00
|
|
|
rv = sourceParent->Contains(targetFile, &allowed);
|
2013-08-07 14:35:20 -07:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aAllowDirectoryTarget,
|
|
|
|
"sourceFile->Parent == targetFile, but targetFile "
|
|
|
|
"should've been disallowed if it is a directory");
|
|
|
|
}
|
2013-05-31 14:33:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 14:35:20 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && allowed) {
|
2013-05-31 14:33:51 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool
|
2009-01-21 14:50:28 -08:00
|
|
|
NS_IsInternalSameURIRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2009-01-21 14:50:28 -08:00
|
|
|
{
|
|
|
|
if (!(aFlags & nsIChannelEventSink::REDIRECT_INTERNAL)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-01-21 14:50:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> oldURI, newURI;
|
|
|
|
aOldChannel->GetURI(getter_AddRefs(oldURI));
|
|
|
|
aNewChannel->GetURI(getter_AddRefs(newURI));
|
|
|
|
|
|
|
|
if (!oldURI || !newURI) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-01-21 14:50:28 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool res;
|
2009-01-21 14:50:28 -08:00
|
|
|
return NS_SUCCEEDED(oldURI->Equals(newURI, &res)) && res;
|
|
|
|
}
|
|
|
|
|
2015-02-10 05:00:00 -08:00
|
|
|
inline bool
|
|
|
|
NS_IsHSTSUpgradeRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (!(aFlags & nsIChannelEventSink::REDIRECT_STS_UPGRADE)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> oldURI, newURI;
|
|
|
|
aOldChannel->GetURI(getter_AddRefs(oldURI));
|
|
|
|
aNewChannel->GetURI(getter_AddRefs(newURI));
|
|
|
|
|
|
|
|
if (!oldURI || !newURI) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isHttp;
|
|
|
|
if (NS_FAILED(oldURI->SchemeIs("http", &isHttp)) || !isHttp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isHttps;
|
|
|
|
if (NS_FAILED(newURI->SchemeIs("https", &isHttps)) || !isHttps) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> upgradedURI;
|
|
|
|
if (NS_FAILED(oldURI->Clone(getter_AddRefs(upgradedURI)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(upgradedURI->SetScheme(NS_LITERAL_CSTRING("https")))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t oldPort = -1;
|
|
|
|
if (NS_FAILED(oldURI->GetPort(&oldPort))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (oldPort == 80 || oldPort == -1) {
|
|
|
|
upgradedURI->SetPort(-1);
|
|
|
|
} else {
|
|
|
|
upgradedURI->SetPort(oldPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool res;
|
|
|
|
return NS_SUCCEEDED(upgradedURI->Equals(newURI, &res)) && res;
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:56:06 -08:00
|
|
|
inline nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_LinkRedirectChannels(uint32_t channelId,
|
2010-11-23 14:56:06 -08:00
|
|
|
nsIParentChannel *parentChannel,
|
|
|
|
nsIChannel** _result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRedirectChannelRegistrar> registrar =
|
|
|
|
do_GetService("@mozilla.org/redirectchannelregistrar;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return registrar->LinkChannels(channelId,
|
|
|
|
parentChannel,
|
|
|
|
_result);
|
|
|
|
}
|
|
|
|
|
2010-11-19 12:58:59 -08:00
|
|
|
/**
|
|
|
|
* Helper function to create a random URL string that's properly formed
|
|
|
|
* but guaranteed to be invalid.
|
|
|
|
*/
|
|
|
|
#define NS_FAKE_SCHEME "http://"
|
|
|
|
#define NS_FAKE_TLD ".invalid"
|
|
|
|
inline nsresult
|
|
|
|
NS_MakeRandomInvalidURLString(nsCString& result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsID idee;
|
|
|
|
rv = uuidgen->GenerateUUIDInPlace(&idee);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
idee.ToProvidedString(chars);
|
|
|
|
|
|
|
|
result.AssignLiteral(NS_FAKE_SCHEME);
|
|
|
|
// Strip off the '{' and '}' at the beginning and end of the UUID
|
|
|
|
result.Append(chars + 1, NSID_LENGTH - 3);
|
|
|
|
result.AppendLiteral(NS_FAKE_TLD);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#undef NS_FAKE_SCHEME
|
|
|
|
#undef NS_FAKE_TLD
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to determine whether urlString is Java-compatible --
|
|
|
|
* whether it can be passed to the Java URL(String) constructor without the
|
|
|
|
* latter throwing a MalformedURLException, or without Java otherwise
|
2011-01-11 08:00:36 -08:00
|
|
|
* mishandling it. This function (in effect) implements a scheme whitelist
|
|
|
|
* for Java.
|
2010-11-19 12:58:59 -08:00
|
|
|
*/
|
|
|
|
inline nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_CheckIsJavaCompatibleURLString(nsCString& urlString, bool *result)
|
2010-11-19 12:58:59 -08:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false; // Default to "no"
|
2010-11-19 12:58:59 -08:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIURLParser> urlParser =
|
|
|
|
do_GetService(NS_STDURLPARSER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv) || !urlParser)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool compatible = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t schemePos = 0;
|
|
|
|
int32_t schemeLen = 0;
|
2010-11-19 12:58:59 -08:00
|
|
|
urlParser->ParseURL(urlString.get(), -1, &schemePos, &schemeLen,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, nullptr, nullptr);
|
2010-11-19 12:58:59 -08:00
|
|
|
if (schemeLen != -1) {
|
|
|
|
nsCString scheme;
|
|
|
|
scheme.Assign(urlString.get() + schemePos, schemeLen);
|
|
|
|
// By default Java only understands a small number of URL schemes, and of
|
2011-01-11 08:00:36 -08:00
|
|
|
// these only some can legitimately represent a browser page's "origin"
|
|
|
|
// (and be something we can legitimately expect Java to handle ... or not
|
|
|
|
// to mishandle).
|
|
|
|
//
|
|
|
|
// Besides those listed below, the OJI plugin understands the "jar",
|
|
|
|
// "mailto", "netdoc", "javascript" and "rmi" schemes, and Java Plugin2
|
|
|
|
// also understands the "about" scheme. We actually pass "about" URLs
|
|
|
|
// to Java ("about:blank" when processing a javascript: URL (one that
|
|
|
|
// calls Java) from the location bar of a blank page, and (in FF4 and up)
|
|
|
|
// "about:home" when processing a javascript: URL from the home page).
|
|
|
|
// And Java doesn't appear to mishandle them (for example it doesn't allow
|
|
|
|
// connections to "about" URLs). But it doesn't make any sense to do
|
|
|
|
// same-origin checks on "about" URLs, so we don't include them in our
|
|
|
|
// scheme whitelist.
|
|
|
|
//
|
|
|
|
// The OJI plugin doesn't understand "chrome" URLs (only Java Plugin2
|
|
|
|
// does) -- so we mustn't pass them to the OJI plugin. But we do need to
|
|
|
|
// pass "chrome" URLs to Java Plugin2: Java Plugin2 grants additional
|
|
|
|
// privileges to chrome "origins", and some extensions take advantage of
|
|
|
|
// this. For more information see bug 620773.
|
|
|
|
//
|
|
|
|
// As of FF4, we no longer support the OJI plugin.
|
2010-11-19 12:58:59 -08:00
|
|
|
if (PL_strcasecmp(scheme.get(), "http") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "https") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "file") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "ftp") &&
|
2011-01-11 08:00:36 -08:00
|
|
|
PL_strcasecmp(scheme.get(), "gopher") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "chrome"))
|
2011-10-17 07:59:28 -07:00
|
|
|
compatible = false;
|
2010-11-19 12:58:59 -08:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
compatible = false;
|
2010-11-19 12:58:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = compatible;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-09 15:41:04 -07:00
|
|
|
/** Given the first (disposition) token from a Content-Disposition header,
|
|
|
|
* tell whether it indicates the content is inline or attachment
|
|
|
|
* @param aDispToken the disposition token from the content-disposition header
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline uint32_t
|
2011-09-09 15:41:04 -07:00
|
|
|
NS_GetContentDispositionFromToken(const nsAString& aDispToken)
|
|
|
|
{
|
|
|
|
// RFC 2183, section 2.8 says that an unknown disposition
|
|
|
|
// value should be treated as "attachment"
|
|
|
|
// If all of these tests eval to false, then we have a content-disposition of
|
|
|
|
// "attachment" or unknown
|
|
|
|
if (aDispToken.IsEmpty() ||
|
|
|
|
aDispToken.LowerCaseEqualsLiteral("inline") ||
|
|
|
|
// Broken sites just send
|
|
|
|
// Content-Disposition: filename="file"
|
|
|
|
// without a disposition token... screen those out.
|
2012-11-03 06:24:26 -07:00
|
|
|
StringHead(aDispToken, 8).LowerCaseEqualsLiteral("filename"))
|
2011-09-09 15:41:04 -07:00
|
|
|
return nsIChannel::DISPOSITION_INLINE;
|
|
|
|
|
|
|
|
return nsIChannel::DISPOSITION_ATTACHMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Determine the disposition (inline/attachment) of the content based on the
|
|
|
|
* Content-Disposition header
|
|
|
|
* @param aHeader the content-disposition header (full value)
|
|
|
|
* @param aChan the channel the header came from
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline uint32_t
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_GetContentDispositionFromHeader(const nsACString& aHeader, nsIChannel *aChan = nullptr)
|
2011-09-09 15:41:04 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsIChannel::DISPOSITION_ATTACHMENT;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString fallbackCharset;
|
2011-09-09 15:41:04 -07:00
|
|
|
if (aChan) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aChan->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri)
|
|
|
|
uri->GetOriginCharset(fallbackCharset);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString dispToken;
|
2013-02-21 05:36:00 -08:00
|
|
|
rv = mimehdrpar->GetParameterHTTP(aHeader, "", fallbackCharset, true, nullptr,
|
|
|
|
dispToken);
|
2011-09-12 20:14:16 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// special case (see bug 272541): empty disposition type handled as "inline"
|
2011-09-13 15:15:28 -07:00
|
|
|
if (rv == NS_ERROR_FIRST_HEADER_FIELD_COMPONENT_EMPTY)
|
|
|
|
return nsIChannel::DISPOSITION_INLINE;
|
|
|
|
return nsIChannel::DISPOSITION_ATTACHMENT;
|
2011-09-12 20:14:16 -07:00
|
|
|
}
|
2011-09-09 15:41:04 -07:00
|
|
|
|
|
|
|
return NS_GetContentDispositionFromToken(dispToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Extracts the filename out of a content-disposition header
|
|
|
|
* @param aFilename [out] The filename. Can be empty on error.
|
|
|
|
* @param aDisposition Value of a Content-Disposition header
|
|
|
|
* @param aURI Optional. Will be used to get a fallback charset for the
|
|
|
|
* filename, if it is QI'able to nsIURL
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetFilenameFromDisposition(nsAString& aFilename,
|
|
|
|
const nsACString& aDisposition,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIURI* aURI = nullptr)
|
2011-09-09 15:41:04 -07:00
|
|
|
{
|
|
|
|
aFilename.Truncate();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar =
|
|
|
|
do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString fallbackCharset;
|
2011-09-09 15:41:04 -07:00
|
|
|
if (url)
|
|
|
|
url->GetOriginCharset(fallbackCharset);
|
|
|
|
// Get the value of 'filename' parameter
|
2013-02-21 05:36:00 -08:00
|
|
|
rv = mimehdrpar->GetParameterHTTP(aDisposition, "filename",
|
|
|
|
fallbackCharset, true, nullptr,
|
|
|
|
aFilename);
|
2011-09-09 15:41:04 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aFilename.Truncate();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFilename.IsEmpty())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-23 04:01:30 -08:00
|
|
|
/**
|
|
|
|
* Make sure Personal Security Manager is initialized
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
net_EnsurePSMInit()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISocketProviderService> spserv =
|
|
|
|
do_GetService(NS_SOCKETPROVIDERSERVICE_CONTRACTID);
|
|
|
|
if (spserv) {
|
|
|
|
nsCOMPtr<nsISocketProvider> provider;
|
|
|
|
spserv->GetSocketProvider("ssl", getter_AddRefs(provider));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-20 14:00:41 -07:00
|
|
|
/**
|
|
|
|
* Test whether a URI is "about:blank". |uri| must not be null
|
|
|
|
*/
|
|
|
|
inline bool
|
|
|
|
NS_IsAboutBlank(nsIURI *uri)
|
|
|
|
{
|
|
|
|
// GetSpec can be expensive for some URIs, so check the scheme first.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isAbout = false;
|
2011-09-20 14:00:41 -07:00
|
|
|
if (NS_FAILED(uri->SchemeIs("about", &isAbout)) || !isAbout) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString str;
|
2011-09-20 14:00:41 -07:00
|
|
|
uri->GetSpec(str);
|
|
|
|
return str.EqualsLiteral("about:blank");
|
|
|
|
}
|
|
|
|
|
2011-12-22 18:21:20 -08:00
|
|
|
|
|
|
|
inline nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_GenerateHostPort(const nsCString& host, int32_t port,
|
2014-04-22 12:46:06 -07:00
|
|
|
nsACString& hostLine)
|
2011-12-22 18:21:20 -08:00
|
|
|
{
|
|
|
|
if (strchr(host.get(), ':')) {
|
|
|
|
// host is an IPv6 address literal and must be encapsulated in []'s
|
|
|
|
hostLine.Assign('[');
|
|
|
|
// scope id is not needed for Host header.
|
|
|
|
int scopeIdPos = host.FindChar('%');
|
|
|
|
if (scopeIdPos == -1)
|
|
|
|
hostLine.Append(host);
|
|
|
|
else if (scopeIdPos > 0)
|
|
|
|
hostLine.Append(Substring(host, 0, scopeIdPos));
|
|
|
|
else
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
hostLine.Append(']');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
hostLine.Assign(host);
|
|
|
|
if (port != -1) {
|
|
|
|
hostLine.Append(':');
|
|
|
|
hostLine.AppendInt(port);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:53 -08:00
|
|
|
/**
|
|
|
|
* Sniff the content type for a given request or a given buffer.
|
|
|
|
*
|
|
|
|
* aSnifferType can be either NS_CONTENT_SNIFFER_CATEGORY or
|
|
|
|
* NS_DATA_SNIFFER_CATEGORY. The function returns the sniffed content type
|
|
|
|
* in the aSniffedType argument. This argument will not be modified if the
|
|
|
|
* content type could not be sniffed.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_SniffContent(const char* aSnifferType, nsIRequest* aRequest,
|
|
|
|
const uint8_t* aData, uint32_t aLength,
|
|
|
|
nsACString& aSniffedType)
|
|
|
|
{
|
|
|
|
typedef nsCategoryCache<nsIContentSniffer> ContentSnifferCache;
|
2014-06-02 05:08:21 -07:00
|
|
|
extern ContentSnifferCache* gNetSniffers;
|
|
|
|
extern ContentSnifferCache* gDataSniffers;
|
2012-11-20 16:19:53 -08:00
|
|
|
ContentSnifferCache* cache = nullptr;
|
|
|
|
if (!strcmp(aSnifferType, NS_CONTENT_SNIFFER_CATEGORY)) {
|
|
|
|
if (!gNetSniffers) {
|
|
|
|
gNetSniffers = new ContentSnifferCache(NS_CONTENT_SNIFFER_CATEGORY);
|
|
|
|
}
|
|
|
|
cache = gNetSniffers;
|
|
|
|
} else if (!strcmp(aSnifferType, NS_DATA_SNIFFER_CATEGORY)) {
|
|
|
|
if (!gDataSniffers) {
|
|
|
|
gDataSniffers = new ContentSnifferCache(NS_DATA_SNIFFER_CATEGORY);
|
|
|
|
}
|
|
|
|
cache = gDataSniffers;
|
|
|
|
} else {
|
|
|
|
// Invalid content sniffer type was requested
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-10 05:48:03 -07:00
|
|
|
nsCOMArray<nsIContentSniffer> sniffers;
|
|
|
|
cache->GetEntries(sniffers);
|
2012-11-20 16:19:53 -08:00
|
|
|
for (int32_t i = 0; i < sniffers.Count(); ++i) {
|
|
|
|
nsresult rv = sniffers[i]->GetMIMETypeFromContent(aRequest, aData, aLength, aSniffedType);
|
|
|
|
if (NS_SUCCEEDED(rv) && !aSniffedType.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aSniffedType.Truncate();
|
|
|
|
}
|
|
|
|
|
2013-09-10 10:40:35 -07:00
|
|
|
/**
|
|
|
|
* Whether the channel was created to load a srcdoc document.
|
|
|
|
* Note that view-source:about:srcdoc is classified as a srcdoc document by
|
|
|
|
* this function, which may not be applicable everywhere.
|
|
|
|
*/
|
|
|
|
inline bool
|
|
|
|
NS_IsSrcdocChannel(nsIChannel *aChannel)
|
|
|
|
{
|
|
|
|
bool isSrcdoc;
|
|
|
|
nsCOMPtr<nsIInputStreamChannel> isr = do_QueryInterface(aChannel);
|
|
|
|
if (isr) {
|
|
|
|
isr->GetIsSrcdocChannel(&isSrcdoc);
|
|
|
|
return isSrcdoc;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIViewSourceChannel> vsc = do_QueryInterface(aChannel);
|
|
|
|
if (vsc) {
|
|
|
|
vsc->GetIsSrcdocChannel(&isSrcdoc);
|
|
|
|
return isSrcdoc;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-24 09:38:55 -07:00
|
|
|
/**
|
|
|
|
* Return true if the given string is a reasonable HTTP header value given the
|
|
|
|
* definition in RFC 2616 section 4.2. Currently we don't pay the cost to do
|
|
|
|
* full, sctrict validation here since it would require fulling parsing the
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
bool NS_IsReasonableHTTPHeaderValue(const nsACString& aValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the given string is a valid HTTP token per RFC 2616 section
|
|
|
|
* 2.2.
|
|
|
|
*/
|
|
|
|
bool NS_IsValidHTTPToken(const nsACString& aToken);
|
|
|
|
|
2015-01-08 11:48:52 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
const static uint64_t kJS_MAX_SAFE_UINTEGER = +9007199254740991ULL;
|
|
|
|
const static int64_t kJS_MIN_SAFE_INTEGER = -9007199254740991LL;
|
|
|
|
const static int64_t kJS_MAX_SAFE_INTEGER = +9007199254740991LL;
|
|
|
|
|
|
|
|
// Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
|
|
|
|
inline bool
|
|
|
|
InScriptableRange(int64_t val)
|
|
|
|
{
|
|
|
|
return (val <= kJS_MAX_SAFE_INTEGER) && (val >= kJS_MIN_SAFE_INTEGER);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
|
|
|
|
inline bool
|
|
|
|
InScriptableRange(uint64_t val)
|
|
|
|
{
|
|
|
|
return val <= kJS_MAX_SAFE_UINTEGER;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
} // namespace mozilla::net
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif // !nsNetUtil_h__
|