File: InternalHeaders.h

package info (click to toggle)
thunderbird 1:60.8.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,339,208 kB
  • sloc: cpp: 5,456,704; ansic: 2,360,384; python: 596,095; asm: 340,963; java: 326,291; xml: 258,664; sh: 84,366; makefile: 23,702; perl: 17,317; objc: 3,768; yacc: 1,766; ada: 1,681; lex: 1,364; pascal: 1,264; cs: 879; exp: 527; php: 436; lisp: 258; ruby: 153; awk: 152; sed: 53; csh: 27
file content (157 lines) | stat: -rw-r--r-- 5,231 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- 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"
#include "mozilla/dom/UnionTypes.h"

#include "nsClassHashtable.h"
#include "nsWrapperCache.h"

namespace mozilla {

class ErrorResult;

namespace dom {

template <typename K, typename V>
class Record;
class HeadersEntry;

class InternalHeaders final {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InternalHeaders)

 public:
  struct Entry {
    Entry(const nsACString& aName, const nsACString& aValue)
        : mName(aName), mValue(aValue) {}

    Entry() {}

    nsCString mName;
    nsCString mValue;
  };

 private:
  HeadersGuardEnum mGuard;
  nsTArray<Entry> mList;

  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;

 public:
  explicit InternalHeaders(HeadersGuardEnum aGuard = HeadersGuardEnum::None)
      : mGuard(aGuard), mListDirty(false) {}

  explicit InternalHeaders(const InternalHeaders& aOther)
      : mGuard(HeadersGuardEnum::None), mListDirty(true) {
    ErrorResult result;
    Fill(aOther, result);
    MOZ_ASSERT(!result.Failed());
    // 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;
  }

  explicit InternalHeaders(const nsTArray<Entry>&& aHeaders,
                           HeadersGuardEnum aGuard = HeadersGuardEnum::None);

  InternalHeaders(const nsTArray<HeadersEntry>& aHeadersEntryList,
                  HeadersGuardEnum aGuard);

  void ToIPC(nsTArray<HeadersEntry>& aIPCHeaders, HeadersGuardEnum& aGuard);

  void Append(const nsACString& aName, const nsACString& aValue,
              ErrorResult& aRv);
  void Delete(const nsACString& aName, ErrorResult& aRv);
  void Get(const nsACString& aName, nsACString& aValue, ErrorResult& aRv) const;
  void GetFirst(const nsACString& aName, nsACString& aValue,
                ErrorResult& aRv) const;
  bool Has(const nsACString& aName, ErrorResult& aRv) const;
  void Set(const nsACString& aName, const nsACString& aValue, ErrorResult& aRv);

  uint32_t GetIterableLength() {
    MaybeSortList();
    return mSortedList.Length();
  }
  const NS_ConvertASCIItoUTF16 GetKeyAtIndex(unsigned aIndex) {
    MaybeSortList();
    MOZ_ASSERT(aIndex < mSortedList.Length());
    return NS_ConvertASCIItoUTF16(mSortedList[aIndex].mName);
  }
  const NS_ConvertASCIItoUTF16 GetValueAtIndex(unsigned aIndex) {
    MaybeSortList();
    MOZ_ASSERT(aIndex < mSortedList.Length());
    return NS_ConvertASCIItoUTF16(mSortedList[aIndex].mValue);
  }

  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);
  void Fill(const Record<nsCString, nsCString>& aInit, ErrorResult& aRv);
  void FillResponseHeaders(nsIRequest* aRequest);

  bool HasOnlySimpleHeaders() const;

  bool HasRevalidationHeaders() const;

  static already_AddRefed<InternalHeaders> BasicHeaders(
      InternalHeaders* aHeaders);

  static already_AddRefed<InternalHeaders> CORSHeaders(
      InternalHeaders* aHeaders);

  void GetEntries(nsTArray<InternalHeaders::Entry>& aEntries) const;

  void GetUnsafeHeaders(nsTArray<nsCString>& aNames) const;

 private:
  virtual ~InternalHeaders();

  static bool IsInvalidName(const nsACString& aName, ErrorResult& aRv);
  static bool IsInvalidValue(const nsACString& aValue, ErrorResult& aRv);
  bool IsImmutable(ErrorResult& aRv) const;
  bool IsForbiddenRequestHeader(const nsACString& aName) const;
  bool IsForbiddenRequestNoCorsHeader(const nsACString& aName) const;
  bool IsForbiddenRequestNoCorsHeader(const nsACString& aName,
                                      const nsACString& aValue) const;
  bool IsForbiddenResponseHeader(const nsACString& aName) const;

  bool IsInvalidMutableHeader(const nsACString& aName, ErrorResult& aRv) const {
    return IsInvalidMutableHeader(aName, EmptyCString(), aRv);
  }

  bool IsInvalidMutableHeader(const nsACString& aName, const nsACString& aValue,
                              ErrorResult& aRv) const {
    return IsInvalidName(aName, aRv) || IsInvalidValue(aValue, aRv) ||
           IsImmutable(aRv) || IsForbiddenRequestHeader(aName) ||
           IsForbiddenRequestNoCorsHeader(aName, aValue) ||
           IsForbiddenResponseHeader(aName);
  }

  static bool IsSimpleHeader(const nsACString& aName, const nsACString& aValue);

  static bool IsRevalidationHeader(const nsACString& aName);

  void MaybeSortList();
  void SetListDirty();
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_InternalHeaders_h