2014-10-02 21:59:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_InternalHeaders_h
|
|
|
|
#define mozilla_dom_InternalHeaders_h
|
|
|
|
|
|
|
|
// needed for HeadersGuardEnum.
|
|
|
|
#include "mozilla/dom/HeadersBinding.h"
|
2019-07-16 11:50:14 +03:00
|
|
|
#include "mozilla/dom/RequestBinding.h"
|
2014-10-02 21:59:20 +04:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
|
|
|
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class ErrorResult;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2017-02-15 08:01:35 +03:00
|
|
|
template <typename K, typename V>
|
|
|
|
class Record;
|
2016-06-07 12:46:03 +03:00
|
|
|
class HeadersEntry;
|
2014-10-02 21:59:20 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class InternalHeaders final {
|
2014-10-02 21:59:20 +04:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InternalHeaders)
|
|
|
|
|
2014-12-10 09:35:22 +03:00
|
|
|
public:
|
2014-10-02 21:59:20 +04:00
|
|
|
struct Entry {
|
|
|
|
Entry(const nsACString& aName, const nsACString& aValue)
|
|
|
|
: mName(aName), mValue(aValue) {}
|
|
|
|
|
|
|
|
Entry() {}
|
|
|
|
|
|
|
|
nsCString mName;
|
|
|
|
nsCString mValue;
|
|
|
|
};
|
|
|
|
|
2014-12-10 09:35:22 +03:00
|
|
|
private:
|
2014-10-02 21:59:20 +04:00
|
|
|
HeadersGuardEnum mGuard;
|
|
|
|
nsTArray<Entry> mList;
|
|
|
|
|
2017-09-17 12:18:20 +03:00
|
|
|
nsTArray<Entry> mSortedList;
|
|
|
|
|
|
|
|
// This boolean is set to true at any writing operation to mList. It's set to
|
|
|
|
// false when mSortedList is regenerated. This happens when the header is
|
|
|
|
// iterated.
|
|
|
|
bool mListDirty;
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
public:
|
|
|
|
explicit InternalHeaders(HeadersGuardEnum aGuard = HeadersGuardEnum::None)
|
2017-09-17 12:18:20 +03:00
|
|
|
: mGuard(aGuard), mListDirty(false) {}
|
2014-10-02 21:59:20 +04:00
|
|
|
|
|
|
|
explicit InternalHeaders(const InternalHeaders& aOther)
|
2015-10-19 21:23:28 +03:00
|
|
|
: mGuard(HeadersGuardEnum::None), mListDirty(true) {
|
2014-10-02 21:59:20 +04:00
|
|
|
ErrorResult result;
|
|
|
|
Fill(aOther, result);
|
|
|
|
MOZ_ASSERT(!result.Failed());
|
2015-10-19 21:23:28 +03:00
|
|
|
// Note that it's important to set the guard after Fill(), to make sure
|
|
|
|
// that Fill() doesn't fail if aOther is immutable.
|
|
|
|
mGuard = aOther.mGuard;
|
2014-10-02 21:59:20 +04:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
explicit InternalHeaders(const nsTArray<Entry>&& aHeaders,
|
2015-03-02 16:08:00 +03:00
|
|
|
HeadersGuardEnum aGuard = HeadersGuardEnum::None);
|
|
|
|
|
2016-06-07 12:46:03 +03:00
|
|
|
InternalHeaders(const nsTArray<HeadersEntry>& aHeadersEntryList,
|
|
|
|
HeadersGuardEnum aGuard);
|
|
|
|
|
|
|
|
void ToIPC(nsTArray<HeadersEntry>& aIPCHeaders, HeadersGuardEnum& aGuard);
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
void Append(const nsACString& aName, const nsACString& aValue,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
void Delete(const nsACString& aName, ErrorResult& aRv);
|
2016-10-10 20:20:14 +03:00
|
|
|
void Get(const nsACString& aName, nsACString& aValue, ErrorResult& aRv) const;
|
|
|
|
void GetFirst(const nsACString& aName, nsACString& aValue,
|
|
|
|
ErrorResult& aRv) const;
|
2014-10-02 21:59:20 +04:00
|
|
|
bool Has(const nsACString& aName, ErrorResult& aRv) const;
|
|
|
|
void Set(const nsACString& aName, const nsACString& aValue, ErrorResult& aRv);
|
|
|
|
|
2017-09-17 12:18:20 +03:00
|
|
|
uint32_t GetIterableLength() {
|
|
|
|
MaybeSortList();
|
|
|
|
return mSortedList.Length();
|
2015-10-19 20:58:03 +03:00
|
|
|
}
|
2017-09-17 12:18:20 +03:00
|
|
|
const NS_ConvertASCIItoUTF16 GetKeyAtIndex(unsigned aIndex) {
|
|
|
|
MaybeSortList();
|
|
|
|
MOZ_ASSERT(aIndex < mSortedList.Length());
|
|
|
|
return NS_ConvertASCIItoUTF16(mSortedList[aIndex].mName);
|
2015-10-19 20:58:03 +03:00
|
|
|
}
|
2017-09-17 12:18:20 +03:00
|
|
|
const NS_ConvertASCIItoUTF16 GetValueAtIndex(unsigned aIndex) {
|
|
|
|
MaybeSortList();
|
|
|
|
MOZ_ASSERT(aIndex < mSortedList.Length());
|
|
|
|
return NS_ConvertASCIItoUTF16(mSortedList[aIndex].mValue);
|
2015-10-19 20:58:03 +03:00
|
|
|
}
|
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
void Clear();
|
|
|
|
|
|
|
|
HeadersGuardEnum Guard() const { return mGuard; }
|
|
|
|
void SetGuard(HeadersGuardEnum aGuard, ErrorResult& aRv);
|
|
|
|
|
|
|
|
void Fill(const InternalHeaders& aInit, ErrorResult& aRv);
|
|
|
|
void Fill(const Sequence<Sequence<nsCString>>& aInit, ErrorResult& aRv);
|
2017-02-15 08:01:35 +03:00
|
|
|
void Fill(const Record<nsCString, nsCString>& aInit, ErrorResult& aRv);
|
2017-02-14 18:06:39 +03:00
|
|
|
void FillResponseHeaders(nsIRequest* aRequest);
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
bool HasOnlySimpleHeaders() const;
|
|
|
|
|
2016-03-03 02:09:30 +03:00
|
|
|
bool HasRevalidationHeaders() const;
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
static already_AddRefed<InternalHeaders> BasicHeaders(
|
|
|
|
InternalHeaders* aHeaders);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
static already_AddRefed<InternalHeaders> CORSHeaders(
|
2019-07-16 11:50:14 +03:00
|
|
|
InternalHeaders* aHeaders,
|
|
|
|
RequestCredentials mCredentialsMode = RequestCredentials::Omit);
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
void GetEntries(nsTArray<InternalHeaders::Entry>& aEntries) const;
|
2014-12-10 09:35:22 +03:00
|
|
|
|
|
|
|
void GetUnsafeHeaders(nsTArray<nsCString>& aNames) const;
|
2015-01-08 02:50:54 +03:00
|
|
|
|
2014-10-02 21:59:20 +04:00
|
|
|
private:
|
|
|
|
virtual ~InternalHeaders();
|
|
|
|
|
|
|
|
static bool IsInvalidName(const nsACString& aName, ErrorResult& aRv);
|
|
|
|
static bool IsInvalidValue(const nsACString& aValue, ErrorResult& aRv);
|
2019-04-16 09:34:05 +03:00
|
|
|
bool IsValidHeaderValue(const nsCString& aLowerName,
|
|
|
|
const nsCString& aNormalizedValue, ErrorResult& aRv);
|
2014-10-02 21:59:20 +04:00
|
|
|
bool IsImmutable(ErrorResult& aRv) const;
|
2018-12-19 17:48:30 +03:00
|
|
|
bool IsForbiddenRequestHeader(const nsCString& aName) const;
|
|
|
|
bool IsForbiddenRequestNoCorsHeader(const nsCString& aName) const;
|
|
|
|
bool IsForbiddenRequestNoCorsHeader(const nsCString& aName,
|
2014-10-02 21:59:20 +04:00
|
|
|
const nsACString& aValue) const;
|
2018-12-19 17:48:30 +03:00
|
|
|
bool IsForbiddenResponseHeader(const nsCString& aName) const;
|
2014-10-02 21:59:20 +04:00
|
|
|
|
2018-12-19 17:48:30 +03:00
|
|
|
bool IsInvalidMutableHeader(const nsCString& aName, ErrorResult& aRv) const {
|
2014-10-02 21:59:20 +04:00
|
|
|
return IsInvalidMutableHeader(aName, EmptyCString(), aRv);
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:48:30 +03:00
|
|
|
bool IsInvalidMutableHeader(const nsCString& aName, const nsACString& aValue,
|
2014-10-02 21:59:20 +04:00
|
|
|
ErrorResult& aRv) const {
|
|
|
|
return IsInvalidName(aName, aRv) || IsInvalidValue(aValue, aRv) ||
|
|
|
|
IsImmutable(aRv) || IsForbiddenRequestHeader(aName) ||
|
|
|
|
IsForbiddenRequestNoCorsHeader(aName, aValue) ||
|
|
|
|
IsForbiddenResponseHeader(aName);
|
|
|
|
}
|
2015-01-08 02:50:54 +03:00
|
|
|
|
2018-12-19 17:48:30 +03:00
|
|
|
// This method updates the passed name to match the capitalization of a header
|
|
|
|
// with the same name (ignoring case, per the spec).
|
|
|
|
void ReuseExistingNameIfExists(nsCString& aName) const;
|
2016-03-03 02:09:30 +03:00
|
|
|
|
2019-04-16 09:34:05 +03:00
|
|
|
void RemovePrivilegedNoCorsRequestHeaders();
|
|
|
|
|
|
|
|
void GetInternal(const nsCString& aLowerName, nsACString& aValue,
|
|
|
|
ErrorResult& aRv) const;
|
|
|
|
|
|
|
|
bool DeleteInternal(const nsCString& aLowerName, ErrorResult& aRv);
|
|
|
|
|
|
|
|
static bool IsNoCorsSafelistedRequestHeaderName(const nsCString& aName);
|
|
|
|
|
|
|
|
static bool IsPrivilegedNoCorsRequestHeaderName(const nsCString& aName);
|
|
|
|
|
2018-12-19 17:48:30 +03:00
|
|
|
static bool IsSimpleHeader(const nsCString& aName, const nsACString& aValue);
|
|
|
|
|
|
|
|
static bool IsRevalidationHeader(const nsCString& aName);
|
2017-09-17 12:18:20 +03:00
|
|
|
|
|
|
|
void MaybeSortList();
|
|
|
|
void SetListDirty();
|
2014-10-02 21:59:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_InternalHeaders_h
|