netwerk/protocol/http/nsHttpHeaderArray.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim: set sw=4 ts=8 et tw=80 : */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef nsHttpHeaderArray_h__
michael@0 8 #define nsHttpHeaderArray_h__
michael@0 9
michael@0 10 #include "nsHttp.h"
michael@0 11 #include "nsTArray.h"
michael@0 12 #include "nsString.h"
michael@0 13
michael@0 14 class nsIHttpHeaderVisitor;
michael@0 15
michael@0 16 namespace mozilla { namespace net {
michael@0 17
michael@0 18 class nsHttpHeaderArray
michael@0 19 {
michael@0 20 public:
michael@0 21 const char *PeekHeader(nsHttpAtom header) const;
michael@0 22
michael@0 23 // Used by internal setters: to set header from network use SetHeaderFromNet
michael@0 24 nsresult SetHeader(nsHttpAtom header, const nsACString &value,
michael@0 25 bool merge = false);
michael@0 26
michael@0 27 // Merges supported headers. For other duplicate values, determines if error
michael@0 28 // needs to be thrown or 1st value kept.
michael@0 29 nsresult SetHeaderFromNet(nsHttpAtom header, const nsACString &value);
michael@0 30
michael@0 31 nsresult GetHeader(nsHttpAtom header, nsACString &value) const;
michael@0 32 void ClearHeader(nsHttpAtom h);
michael@0 33
michael@0 34 // Find the location of the given header value, or null if none exists.
michael@0 35 const char *FindHeaderValue(nsHttpAtom header, const char *value) const
michael@0 36 {
michael@0 37 return nsHttp::FindToken(PeekHeader(header), value,
michael@0 38 HTTP_HEADER_VALUE_SEPS);
michael@0 39 }
michael@0 40
michael@0 41 // Determine if the given header value exists.
michael@0 42 bool HasHeaderValue(nsHttpAtom header, const char *value) const
michael@0 43 {
michael@0 44 return FindHeaderValue(header, value) != nullptr;
michael@0 45 }
michael@0 46
michael@0 47 nsresult VisitHeaders(nsIHttpHeaderVisitor *visitor);
michael@0 48
michael@0 49 // parse a header line, return the header atom and a pointer to the
michael@0 50 // header value (the substring of the header line -- do not free).
michael@0 51 nsresult ParseHeaderLine(const char *line,
michael@0 52 nsHttpAtom *header=nullptr,
michael@0 53 char **value=nullptr);
michael@0 54
michael@0 55 void Flatten(nsACString &, bool pruneProxyHeaders=false);
michael@0 56
michael@0 57 uint32_t Count() const { return mHeaders.Length(); }
michael@0 58
michael@0 59 const char *PeekHeaderAt(uint32_t i, nsHttpAtom &header) const;
michael@0 60
michael@0 61 void Clear();
michael@0 62
michael@0 63 // Must be copy-constructable and assignable
michael@0 64 struct nsEntry
michael@0 65 {
michael@0 66 nsHttpAtom header;
michael@0 67 nsCString value;
michael@0 68
michael@0 69 struct MatchHeader {
michael@0 70 bool Equals(const nsEntry &entry, const nsHttpAtom &header) const {
michael@0 71 return entry.header == header;
michael@0 72 }
michael@0 73 };
michael@0 74 };
michael@0 75
michael@0 76 private:
michael@0 77 int32_t LookupEntry(nsHttpAtom header, const nsEntry **) const;
michael@0 78 int32_t LookupEntry(nsHttpAtom header, nsEntry **);
michael@0 79 void MergeHeader(nsHttpAtom header, nsEntry *entry, const nsACString &value);
michael@0 80
michael@0 81 // Header cannot be merged: only one value possible
michael@0 82 bool IsSingletonHeader(nsHttpAtom header);
michael@0 83 // For some headers we want to track empty values to prevent them being
michael@0 84 // combined with non-empty ones as a CRLF attack vector
michael@0 85 bool TrackEmptyHeader(nsHttpAtom header);
michael@0 86
michael@0 87 // Subset of singleton headers: should never see multiple, different
michael@0 88 // instances of these, else something fishy may be going on (like CLRF
michael@0 89 // injection)
michael@0 90 bool IsSuspectDuplicateHeader(nsHttpAtom header);
michael@0 91
michael@0 92 // All members must be copy-constructable and assignable
michael@0 93 nsTArray<nsEntry> mHeaders;
michael@0 94
michael@0 95 friend struct IPC::ParamTraits<nsHttpHeaderArray>;
michael@0 96 };
michael@0 97
michael@0 98
michael@0 99 //-----------------------------------------------------------------------------
michael@0 100 // nsHttpHeaderArray <private>: inline functions
michael@0 101 //-----------------------------------------------------------------------------
michael@0 102
michael@0 103 inline int32_t
michael@0 104 nsHttpHeaderArray::LookupEntry(nsHttpAtom header, const nsEntry **entry) const
michael@0 105 {
michael@0 106 uint32_t index = mHeaders.IndexOf(header, 0, nsEntry::MatchHeader());
michael@0 107 if (index != UINT32_MAX)
michael@0 108 *entry = &mHeaders[index];
michael@0 109 return index;
michael@0 110 }
michael@0 111
michael@0 112 inline int32_t
michael@0 113 nsHttpHeaderArray::LookupEntry(nsHttpAtom header, nsEntry **entry)
michael@0 114 {
michael@0 115 uint32_t index = mHeaders.IndexOf(header, 0, nsEntry::MatchHeader());
michael@0 116 if (index != UINT32_MAX)
michael@0 117 *entry = &mHeaders[index];
michael@0 118 return index;
michael@0 119 }
michael@0 120
michael@0 121 inline bool
michael@0 122 nsHttpHeaderArray::IsSingletonHeader(nsHttpAtom header)
michael@0 123 {
michael@0 124 return header == nsHttp::Content_Type ||
michael@0 125 header == nsHttp::Content_Disposition ||
michael@0 126 header == nsHttp::Content_Length ||
michael@0 127 header == nsHttp::User_Agent ||
michael@0 128 header == nsHttp::Referer ||
michael@0 129 header == nsHttp::Host ||
michael@0 130 header == nsHttp::Authorization ||
michael@0 131 header == nsHttp::Proxy_Authorization ||
michael@0 132 header == nsHttp::If_Modified_Since ||
michael@0 133 header == nsHttp::If_Unmodified_Since ||
michael@0 134 header == nsHttp::From ||
michael@0 135 header == nsHttp::Location ||
michael@0 136 header == nsHttp::Max_Forwards;
michael@0 137 }
michael@0 138
michael@0 139 inline bool
michael@0 140 nsHttpHeaderArray::TrackEmptyHeader(nsHttpAtom header)
michael@0 141 {
michael@0 142 return header == nsHttp::Content_Length ||
michael@0 143 header == nsHttp::Location;
michael@0 144 }
michael@0 145
michael@0 146 inline void
michael@0 147 nsHttpHeaderArray::MergeHeader(nsHttpAtom header,
michael@0 148 nsEntry *entry,
michael@0 149 const nsACString &value)
michael@0 150 {
michael@0 151 if (value.IsEmpty())
michael@0 152 return; // merge of empty header = no-op
michael@0 153
michael@0 154 // Append the new value to the existing value
michael@0 155 if (header == nsHttp::Set_Cookie ||
michael@0 156 header == nsHttp::WWW_Authenticate ||
michael@0 157 header == nsHttp::Proxy_Authenticate)
michael@0 158 {
michael@0 159 // Special case these headers and use a newline delimiter to
michael@0 160 // delimit the values from one another as commas may appear
michael@0 161 // in the values of these headers contrary to what the spec says.
michael@0 162 entry->value.Append('\n');
michael@0 163 } else {
michael@0 164 // Delimit each value from the others using a comma (per HTTP spec)
michael@0 165 entry->value.AppendLiteral(", ");
michael@0 166 }
michael@0 167 entry->value.Append(value);
michael@0 168 }
michael@0 169
michael@0 170 inline bool
michael@0 171 nsHttpHeaderArray::IsSuspectDuplicateHeader(nsHttpAtom header)
michael@0 172 {
michael@0 173 bool retval = header == nsHttp::Content_Length ||
michael@0 174 header == nsHttp::Content_Disposition ||
michael@0 175 header == nsHttp::Location;
michael@0 176
michael@0 177 MOZ_ASSERT(!retval || IsSingletonHeader(header),
michael@0 178 "Only non-mergeable headers should be in this list\n");
michael@0 179
michael@0 180 return retval;
michael@0 181 }
michael@0 182
michael@0 183 }} // namespace mozilla::net
michael@0 184
michael@0 185 #endif

mercurial