2014-09-24 05:03:20 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_InternalRequest_h
|
|
|
|
#define mozilla_dom_InternalRequest_h
|
|
|
|
|
2014-10-02 17:59:20 +00:00
|
|
|
#include "mozilla/dom/HeadersBinding.h"
|
|
|
|
#include "mozilla/dom/InternalHeaders.h"
|
2014-09-24 05:03:20 +00:00
|
|
|
#include "mozilla/dom/RequestBinding.h"
|
|
|
|
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2014-12-23 15:56:19 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include "nsIURLParser.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#endif
|
2014-09-24 05:03:20 +00:00
|
|
|
|
|
|
|
class nsIDocument;
|
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-03-25 20:38:42 +00:00
|
|
|
/*
|
|
|
|
* The mapping of RequestContext and nsContentPolicyType is currently as the
|
|
|
|
* following. Note that this mapping is not perfect yet (see the TODO comments
|
|
|
|
* below for examples), so for now we'll have to keep both an mContext and an
|
|
|
|
* mContentPolicyType, because we cannot have a two way conversion.
|
|
|
|
*
|
|
|
|
* RequestContext | nsContentPolicyType
|
|
|
|
* ------------------+--------------------
|
|
|
|
* audio | TYPE_MEDIA
|
|
|
|
* beacon | TYPE_BEACON
|
|
|
|
* cspreport | TYPE_CSP_REPORT
|
|
|
|
* download |
|
|
|
|
* embed | TYPE_OBJECT
|
|
|
|
* eventsource |
|
|
|
|
* favicon |
|
|
|
|
* fetch | TYPE_FETCH
|
|
|
|
* font | TYPE_FONT
|
|
|
|
* form |
|
|
|
|
* frame | TYPE_SUBDOCUMENT
|
|
|
|
* hyperlink |
|
|
|
|
* iframe | TYPE_SUBDOCUMENT
|
|
|
|
* image | TYPE_IMAGE
|
|
|
|
* imageset | TYPE_IMAGESET
|
|
|
|
* import | Not supported by Gecko
|
|
|
|
* internal | TYPE_DOCUMENT, TYPE_XBL, TYPE_OTHER
|
|
|
|
* location |
|
|
|
|
* manifest |
|
|
|
|
* object | TYPE_OBJECT
|
|
|
|
* ping | TYPE_PING
|
|
|
|
* plugin | TYPE_OBJECT_SUBREQUEST
|
|
|
|
* prefetch |
|
|
|
|
* script | TYPE_SCRIPT
|
|
|
|
* serviceworker |
|
|
|
|
* sharedworker |
|
|
|
|
* subresource | Not supported by Gecko
|
|
|
|
* style | TYPE_STYLESHEET
|
|
|
|
* track | TYPE_MEDIA
|
|
|
|
* video | TYPE_MEDIA
|
|
|
|
* worker |
|
|
|
|
* xmlhttprequest | TYPE_XMLHTTPREQUEST
|
|
|
|
* xslt | TYPE_XSLT
|
|
|
|
*
|
|
|
|
* TODO: Figure out if TYPE_REFRESH maps to anything useful
|
|
|
|
* TODO: Figure out if TYPE_DTD maps to anything useful
|
|
|
|
* TODO: Split TYPE_MEDIA into TYPE_AUDIO, TYPE_VIDEO and TYPE_TRACK
|
|
|
|
* TODO: Split TYPE_XMLHTTPREQUEST and TYPE_DATAREQUEST for EventSource
|
|
|
|
* TODO: Figure out if TYPE_WEBSOCKET maps to anything useful
|
|
|
|
* TODO: Differentiate between frame and iframe
|
|
|
|
* TODO: Add content types for different kinds of workers
|
|
|
|
* TODO: Add a content type for prefetch
|
|
|
|
* TODO: Use the content type for manifest when it becomes available
|
|
|
|
* TODO: Add a content type for location
|
|
|
|
* TODO: Add a content type for hyperlink
|
|
|
|
* TODO: Add a content type for form
|
|
|
|
* TODO: Add a content type for favicon
|
|
|
|
* TODO: Add a content type for download
|
|
|
|
* TODO: Split TYPE_OBJECT into TYPE_EMBED and TYPE_OBJECT
|
|
|
|
*/
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
class FetchBodyStream;
|
|
|
|
class Request;
|
|
|
|
|
2014-12-23 15:56:19 +00:00
|
|
|
#define kFETCH_CLIENT_REFERRER_STR "about:client"
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class InternalRequest final
|
2014-09-24 05:03:20 +00:00
|
|
|
{
|
|
|
|
friend class Request;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InternalRequest)
|
|
|
|
|
|
|
|
enum ResponseTainting
|
|
|
|
{
|
|
|
|
RESPONSETAINT_BASIC,
|
|
|
|
RESPONSETAINT_CORS,
|
|
|
|
RESPONSETAINT_OPAQUE,
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit InternalRequest()
|
|
|
|
: mMethod("GET")
|
2014-10-02 17:59:20 +00:00
|
|
|
, mHeaders(new InternalHeaders(HeadersGuardEnum::None))
|
2014-12-23 15:56:19 +00:00
|
|
|
, mReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR))
|
2014-09-24 05:03:20 +00:00
|
|
|
, mMode(RequestMode::No_cors)
|
|
|
|
, mCredentialsMode(RequestCredentials::Omit)
|
|
|
|
, mResponseTainting(RESPONSETAINT_BASIC)
|
2015-01-08 00:24:40 +00:00
|
|
|
, mCacheMode(RequestCache::Default)
|
2014-09-24 05:03:20 +00:00
|
|
|
, mAuthenticationFlag(false)
|
|
|
|
, mForceOriginHeader(false)
|
|
|
|
, mPreserveContentCodings(false)
|
2014-10-06 18:01:20 +00:00
|
|
|
// FIXME(nsm): This should be false by default, but will lead to the
|
|
|
|
// algorithm never loading data: URLs right now. See Bug 1018872 about
|
|
|
|
// how certain contexts will override it to set it to true. Fetch
|
|
|
|
// specification does not handle this yet.
|
|
|
|
, mSameOriginDataURL(true)
|
2014-09-24 05:03:20 +00:00
|
|
|
, mSkipServiceWorker(false)
|
|
|
|
, mSynchronous(false)
|
|
|
|
, mUnsafeRequest(false)
|
|
|
|
, mUseURLCredentials(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:24:24 +00:00
|
|
|
already_AddRefed<InternalRequest> Clone();
|
2014-09-24 05:03:20 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
GetMethod(nsCString& aMethod) const
|
|
|
|
{
|
|
|
|
aMethod.Assign(mMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetMethod(const nsACString& aMethod)
|
|
|
|
{
|
|
|
|
mMethod.Assign(aMethod);
|
|
|
|
}
|
|
|
|
|
2014-10-06 18:01:20 +00:00
|
|
|
bool
|
|
|
|
HasSimpleMethod() const
|
|
|
|
{
|
|
|
|
return mMethod.LowerCaseEqualsASCII("get") ||
|
|
|
|
mMethod.LowerCaseEqualsASCII("post") ||
|
|
|
|
mMethod.LowerCaseEqualsASCII("head");
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
void
|
|
|
|
GetURL(nsCString& aURL) const
|
|
|
|
{
|
|
|
|
aURL.Assign(mURL);
|
|
|
|
}
|
|
|
|
|
2015-01-07 21:47:18 +00:00
|
|
|
void
|
|
|
|
SetURL(const nsACString& aURL)
|
|
|
|
{
|
|
|
|
mURL.Assign(aURL);
|
|
|
|
}
|
|
|
|
|
2014-12-23 15:56:19 +00:00
|
|
|
void
|
|
|
|
GetReferrer(nsAString& aReferrer) const
|
2014-09-24 05:03:20 +00:00
|
|
|
{
|
2014-12-23 15:56:19 +00:00
|
|
|
aReferrer.Assign(mReferrer);
|
2014-09-24 05:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-23 15:56:19 +00:00
|
|
|
SetReferrer(const nsAString& aReferrer)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool validReferrer = false;
|
|
|
|
if (aReferrer.IsEmpty() ||
|
|
|
|
aReferrer.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
|
|
|
|
validReferrer = true;
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIURLParser> parser = do_GetService(NS_STDURLPARSER_CONTRACTID);
|
|
|
|
if (!parser) {
|
|
|
|
NS_WARNING("Could not get parser to validate URL!");
|
|
|
|
} else {
|
|
|
|
uint32_t schemePos;
|
|
|
|
int32_t schemeLen;
|
|
|
|
uint32_t authorityPos;
|
|
|
|
int32_t authorityLen;
|
|
|
|
uint32_t pathPos;
|
|
|
|
int32_t pathLen;
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 ref(aReferrer);
|
|
|
|
nsresult rv = parser->ParseURL(ref.get(), ref.Length(),
|
|
|
|
&schemePos, &schemeLen,
|
|
|
|
&authorityPos, &authorityLen,
|
|
|
|
&pathPos, &pathLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Invalid referrer URL!");
|
|
|
|
} else if (schemeLen < 0 || authorityLen < 0) {
|
|
|
|
NS_WARNING("Invalid referrer URL!");
|
|
|
|
} else {
|
|
|
|
validReferrer = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(validReferrer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mReferrer.Assign(aReferrer);
|
2014-09-24 05:03:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-24 10:08:41 +00:00
|
|
|
bool
|
|
|
|
SkipServiceWorker() const
|
|
|
|
{
|
|
|
|
return mSkipServiceWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetSkipServiceWorker()
|
|
|
|
{
|
|
|
|
mSkipServiceWorker = true;
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
bool
|
|
|
|
IsSynchronous() const
|
|
|
|
{
|
|
|
|
return mSynchronous;
|
|
|
|
}
|
|
|
|
|
2014-10-02 17:59:20 +00:00
|
|
|
RequestMode
|
|
|
|
Mode() const
|
|
|
|
{
|
|
|
|
return mMode;
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
void
|
|
|
|
SetMode(RequestMode aMode)
|
|
|
|
{
|
|
|
|
mMode = aMode;
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:50:54 +00:00
|
|
|
RequestCredentials
|
|
|
|
GetCredentialsMode() const
|
|
|
|
{
|
|
|
|
return mCredentialsMode;
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
void
|
|
|
|
SetCredentialsMode(RequestCredentials aCredentialsMode)
|
|
|
|
{
|
|
|
|
mCredentialsMode = aCredentialsMode;
|
|
|
|
}
|
|
|
|
|
2014-10-06 18:01:20 +00:00
|
|
|
ResponseTainting
|
|
|
|
GetResponseTainting() const
|
|
|
|
{
|
|
|
|
return mResponseTainting;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetResponseTainting(ResponseTainting aTainting)
|
|
|
|
{
|
|
|
|
mResponseTainting = aTainting;
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:24:40 +00:00
|
|
|
RequestCache
|
|
|
|
GetCacheMode() const
|
|
|
|
{
|
|
|
|
return mCacheMode;
|
|
|
|
}
|
|
|
|
|
2015-03-22 17:12:43 +00:00
|
|
|
void
|
|
|
|
SetCacheMode(RequestCache aCacheMode)
|
|
|
|
{
|
|
|
|
mCacheMode = aCacheMode;
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
nsContentPolicyType
|
2015-01-27 23:43:09 +00:00
|
|
|
ContentPolicyType() const
|
2014-09-24 05:03:20 +00:00
|
|
|
{
|
2015-01-27 23:43:09 +00:00
|
|
|
return mContentPolicyType;
|
2014-09-24 05:03:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 13:08:00 +00:00
|
|
|
void
|
2015-03-25 20:38:42 +00:00
|
|
|
SetContentPolicyType(nsContentPolicyType aContentPolicyType);
|
|
|
|
|
|
|
|
RequestContext
|
|
|
|
Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetContext(RequestContext aContext)
|
2015-03-02 13:08:00 +00:00
|
|
|
{
|
2015-03-25 20:38:42 +00:00
|
|
|
mContext = aContext;
|
2015-03-02 13:08:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 18:01:20 +00:00
|
|
|
bool
|
|
|
|
UnsafeRequest() const
|
|
|
|
{
|
|
|
|
return mUnsafeRequest;
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:50:54 +00:00
|
|
|
void
|
|
|
|
SetUnsafeRequest()
|
|
|
|
{
|
|
|
|
mUnsafeRequest = true;
|
|
|
|
}
|
|
|
|
|
2014-10-02 17:59:20 +00:00
|
|
|
InternalHeaders*
|
|
|
|
Headers()
|
2014-09-24 05:03:20 +00:00
|
|
|
{
|
|
|
|
return mHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ForceOriginHeader()
|
|
|
|
{
|
|
|
|
return mForceOriginHeader;
|
|
|
|
}
|
|
|
|
|
2014-10-06 18:01:20 +00:00
|
|
|
bool
|
|
|
|
SameOriginDataURL() const
|
|
|
|
{
|
|
|
|
return mSameOriginDataURL;
|
|
|
|
}
|
|
|
|
|
2015-01-07 21:47:18 +00:00
|
|
|
void
|
|
|
|
UnsetSameOriginDataURL()
|
|
|
|
{
|
|
|
|
mSameOriginDataURL = false;
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
void
|
|
|
|
SetBody(nsIInputStream* aStream)
|
|
|
|
{
|
2014-12-10 08:51:59 +00:00
|
|
|
// A request's body may not be reset once set.
|
|
|
|
MOZ_ASSERT(!mBodyStream);
|
2014-09-24 05:03:20 +00:00
|
|
|
mBodyStream = aStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will return the original stream!
|
|
|
|
// Use a tee or copy if you don't want to erase the original.
|
|
|
|
void
|
|
|
|
GetBody(nsIInputStream** aStream)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> s = mBodyStream;
|
|
|
|
s.forget(aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The global is used as the client for the new object.
|
|
|
|
already_AddRefed<InternalRequest>
|
|
|
|
GetRequestConstructorCopy(nsIGlobalObject* aGlobal, ErrorResult& aRv) const;
|
|
|
|
|
2015-03-27 10:47:00 +00:00
|
|
|
bool
|
|
|
|
WasCreatedByFetchEvent() const
|
|
|
|
{
|
|
|
|
return mCreatedByFetchEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetCreatedByFetchEvent()
|
|
|
|
{
|
|
|
|
mCreatedByFetchEvent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearCreatedByFetchEvent()
|
|
|
|
{
|
|
|
|
mCreatedByFetchEvent = false;
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
private:
|
2015-02-20 01:24:24 +00:00
|
|
|
// Does not copy mBodyStream. Use fallible Clone() for complete copy.
|
|
|
|
explicit InternalRequest(const InternalRequest& aOther);
|
|
|
|
|
2014-09-24 05:03:20 +00:00
|
|
|
~InternalRequest();
|
|
|
|
|
|
|
|
nsCString mMethod;
|
|
|
|
nsCString mURL;
|
2014-10-02 17:59:20 +00:00
|
|
|
nsRefPtr<InternalHeaders> mHeaders;
|
2014-09-24 05:03:20 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mBodyStream;
|
|
|
|
|
2015-01-27 23:43:09 +00:00
|
|
|
nsContentPolicyType mContentPolicyType;
|
2015-03-25 20:38:42 +00:00
|
|
|
RequestContext mContext;
|
2014-09-24 05:03:20 +00:00
|
|
|
|
2014-12-23 15:56:19 +00:00
|
|
|
// Empty string: no-referrer
|
|
|
|
// "about:client": client (default)
|
|
|
|
// URL: an URL
|
|
|
|
nsString mReferrer;
|
2014-09-24 05:03:20 +00:00
|
|
|
|
|
|
|
RequestMode mMode;
|
|
|
|
RequestCredentials mCredentialsMode;
|
|
|
|
ResponseTainting mResponseTainting;
|
2015-01-08 00:24:40 +00:00
|
|
|
RequestCache mCacheMode;
|
2014-09-24 05:03:20 +00:00
|
|
|
|
|
|
|
bool mAuthenticationFlag;
|
|
|
|
bool mForceOriginHeader;
|
|
|
|
bool mPreserveContentCodings;
|
|
|
|
bool mSameOriginDataURL;
|
|
|
|
bool mSandboxedStorageAreaURLs;
|
|
|
|
bool mSkipServiceWorker;
|
|
|
|
bool mSynchronous;
|
|
|
|
bool mUnsafeRequest;
|
|
|
|
bool mUseURLCredentials;
|
2015-03-27 10:47:00 +00:00
|
|
|
// This is only set when a Request object is created by a fetch event. We
|
|
|
|
// use it to check if Service Workers are simply fetching intercepted Request
|
|
|
|
// objects without modifying them.
|
|
|
|
bool mCreatedByFetchEvent = false;
|
2014-09-24 05:03:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_InternalRequest_h
|