netwerk/protocol/http/nsHttpHeaderArray.cpp

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 ts=4 sw=4 sts=4 ci et: */
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 // HttpLog.h should generally be included first
michael@0 8 #include "HttpLog.h"
michael@0 9
michael@0 10 #include "nsHttpHeaderArray.h"
michael@0 11 #include "nsURLHelper.h"
michael@0 12 #include "nsIHttpHeaderVisitor.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15 namespace net {
michael@0 16
michael@0 17 //-----------------------------------------------------------------------------
michael@0 18 // nsHttpHeaderArray <public>
michael@0 19 //-----------------------------------------------------------------------------
michael@0 20 nsresult
michael@0 21 nsHttpHeaderArray::SetHeader(nsHttpAtom header,
michael@0 22 const nsACString &value,
michael@0 23 bool merge)
michael@0 24 {
michael@0 25 nsEntry *entry = nullptr;
michael@0 26 int32_t index;
michael@0 27
michael@0 28 index = LookupEntry(header, &entry);
michael@0 29
michael@0 30 // If an empty value is passed in, then delete the header entry...
michael@0 31 // unless we are merging, in which case this function becomes a NOP.
michael@0 32 if (value.IsEmpty()) {
michael@0 33 if (!merge && entry)
michael@0 34 mHeaders.RemoveElementAt(index);
michael@0 35 return NS_OK;
michael@0 36 }
michael@0 37
michael@0 38 if (!entry) {
michael@0 39 entry = mHeaders.AppendElement(); // new nsEntry()
michael@0 40 if (!entry)
michael@0 41 return NS_ERROR_OUT_OF_MEMORY;
michael@0 42 entry->header = header;
michael@0 43 entry->value = value;
michael@0 44 } else if (merge && !IsSingletonHeader(header)) {
michael@0 45 MergeHeader(header, entry, value);
michael@0 46 } else {
michael@0 47 // Replace the existing string with the new value
michael@0 48 entry->value = value;
michael@0 49 }
michael@0 50
michael@0 51 return NS_OK;
michael@0 52 }
michael@0 53
michael@0 54 nsresult
michael@0 55 nsHttpHeaderArray::SetHeaderFromNet(nsHttpAtom header, const nsACString &value)
michael@0 56 {
michael@0 57 nsEntry *entry = nullptr;
michael@0 58
michael@0 59 LookupEntry(header, &entry);
michael@0 60
michael@0 61 if (!entry) {
michael@0 62 if (value.IsEmpty()) {
michael@0 63 if (!TrackEmptyHeader(header)) {
michael@0 64 LOG(("Ignoring Empty Header: %s\n", header.get()));
michael@0 65 return NS_OK; // ignore empty headers by default
michael@0 66 }
michael@0 67 }
michael@0 68 entry = mHeaders.AppendElement(); //new nsEntry(header, value);
michael@0 69 if (!entry)
michael@0 70 return NS_ERROR_OUT_OF_MEMORY;
michael@0 71 entry->header = header;
michael@0 72 entry->value = value;
michael@0 73 } else if (!IsSingletonHeader(header)) {
michael@0 74 MergeHeader(header, entry, value);
michael@0 75 } else {
michael@0 76 // Multiple instances of non-mergeable header received from network
michael@0 77 // - ignore if same value
michael@0 78 if (!entry->value.Equals(value)) {
michael@0 79 if (IsSuspectDuplicateHeader(header)) {
michael@0 80 // reply may be corrupt/hacked (ex: CLRF injection attacks)
michael@0 81 return NS_ERROR_CORRUPTED_CONTENT;
michael@0 82 } // else silently drop value: keep value from 1st header seen
michael@0 83 LOG(("Header %s silently dropped as non mergeable header\n",
michael@0 84 header.get()));
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 return NS_OK;
michael@0 89 }
michael@0 90
michael@0 91 void
michael@0 92 nsHttpHeaderArray::ClearHeader(nsHttpAtom header)
michael@0 93 {
michael@0 94 mHeaders.RemoveElement(header, nsEntry::MatchHeader());
michael@0 95 }
michael@0 96
michael@0 97 const char *
michael@0 98 nsHttpHeaderArray::PeekHeader(nsHttpAtom header) const
michael@0 99 {
michael@0 100 const nsEntry *entry = nullptr;
michael@0 101 LookupEntry(header, &entry);
michael@0 102 return entry ? entry->value.get() : nullptr;
michael@0 103 }
michael@0 104
michael@0 105 nsresult
michael@0 106 nsHttpHeaderArray::GetHeader(nsHttpAtom header, nsACString &result) const
michael@0 107 {
michael@0 108 const nsEntry *entry = nullptr;
michael@0 109 LookupEntry(header, &entry);
michael@0 110 if (!entry)
michael@0 111 return NS_ERROR_NOT_AVAILABLE;
michael@0 112 result = entry->value;
michael@0 113 return NS_OK;
michael@0 114 }
michael@0 115
michael@0 116 nsresult
michael@0 117 nsHttpHeaderArray::VisitHeaders(nsIHttpHeaderVisitor *visitor)
michael@0 118 {
michael@0 119 NS_ENSURE_ARG_POINTER(visitor);
michael@0 120 uint32_t i, count = mHeaders.Length();
michael@0 121 for (i = 0; i < count; ++i) {
michael@0 122 const nsEntry &entry = mHeaders[i];
michael@0 123 if (NS_FAILED(visitor->VisitHeader(nsDependentCString(entry.header),
michael@0 124 entry.value)))
michael@0 125 break;
michael@0 126 }
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 nsresult
michael@0 131 nsHttpHeaderArray::ParseHeaderLine(const char *line,
michael@0 132 nsHttpAtom *hdr,
michael@0 133 char **val)
michael@0 134 {
michael@0 135 //
michael@0 136 // BNF from section 4.2 of RFC 2616:
michael@0 137 //
michael@0 138 // message-header = field-name ":" [ field-value ]
michael@0 139 // field-name = token
michael@0 140 // field-value = *( field-content | LWS )
michael@0 141 // field-content = <the OCTETs making up the field-value
michael@0 142 // and consisting of either *TEXT or combinations
michael@0 143 // of token, separators, and quoted-string>
michael@0 144 //
michael@0 145
michael@0 146 // We skip over mal-formed headers in the hope that we'll still be able to
michael@0 147 // do something useful with the response.
michael@0 148
michael@0 149 char *p = (char *) strchr(line, ':');
michael@0 150 if (!p) {
michael@0 151 LOG(("malformed header [%s]: no colon\n", line));
michael@0 152 return NS_OK;
michael@0 153 }
michael@0 154
michael@0 155 // make sure we have a valid token for the field-name
michael@0 156 if (!nsHttp::IsValidToken(line, p)) {
michael@0 157 LOG(("malformed header [%s]: field-name not a token\n", line));
michael@0 158 return NS_OK;
michael@0 159 }
michael@0 160
michael@0 161 *p = 0; // null terminate field-name
michael@0 162
michael@0 163 nsHttpAtom atom = nsHttp::ResolveAtom(line);
michael@0 164 if (!atom) {
michael@0 165 LOG(("failed to resolve atom [%s]\n", line));
michael@0 166 return NS_OK;
michael@0 167 }
michael@0 168
michael@0 169 // skip over whitespace
michael@0 170 p = net_FindCharNotInSet(++p, HTTP_LWS);
michael@0 171
michael@0 172 // trim trailing whitespace - bug 86608
michael@0 173 char *p2 = net_RFindCharNotInSet(p, HTTP_LWS);
michael@0 174
michael@0 175 *++p2 = 0; // null terminate header value; if all chars starting at |p|
michael@0 176 // consisted of LWS, then p2 would have pointed at |p-1|, so
michael@0 177 // the prefix increment is always valid.
michael@0 178
michael@0 179 // assign return values
michael@0 180 if (hdr) *hdr = atom;
michael@0 181 if (val) *val = p;
michael@0 182
michael@0 183 // assign response header
michael@0 184 return SetHeaderFromNet(atom, nsDependentCString(p, p2 - p));
michael@0 185 }
michael@0 186
michael@0 187 void
michael@0 188 nsHttpHeaderArray::Flatten(nsACString &buf, bool pruneProxyHeaders)
michael@0 189 {
michael@0 190 uint32_t i, count = mHeaders.Length();
michael@0 191 for (i = 0; i < count; ++i) {
michael@0 192 const nsEntry &entry = mHeaders[i];
michael@0 193 // prune proxy headers if requested
michael@0 194 if (pruneProxyHeaders && ((entry.header == nsHttp::Proxy_Authorization) ||
michael@0 195 (entry.header == nsHttp::Proxy_Connection)))
michael@0 196 continue;
michael@0 197 buf.Append(entry.header);
michael@0 198 buf.AppendLiteral(": ");
michael@0 199 buf.Append(entry.value);
michael@0 200 buf.AppendLiteral("\r\n");
michael@0 201 }
michael@0 202 }
michael@0 203
michael@0 204 const char *
michael@0 205 nsHttpHeaderArray::PeekHeaderAt(uint32_t index, nsHttpAtom &header) const
michael@0 206 {
michael@0 207 const nsEntry &entry = mHeaders[index];
michael@0 208
michael@0 209 header = entry.header;
michael@0 210 return entry.value.get();
michael@0 211 }
michael@0 212
michael@0 213 void
michael@0 214 nsHttpHeaderArray::Clear()
michael@0 215 {
michael@0 216 mHeaders.Clear();
michael@0 217 }
michael@0 218
michael@0 219 } // namespace mozilla::net
michael@0 220 } // namespace mozilla

mercurial