1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim: set sw=4 ts=8 et tw=80 : */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications.
20 : * Portions created by the Initial Developer are Copyright (C) 2001
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Darin Fisher <darin@netscape.com> (original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef nsHttpHeaderArray_h__
41 : #define nsHttpHeaderArray_h__
42 :
43 : #include "nsHttp.h"
44 : #include "nsTArray.h"
45 : #include "nsIHttpChannel.h"
46 : #include "nsIHttpHeaderVisitor.h"
47 : #include "nsCOMPtr.h"
48 : #include "nsString.h"
49 :
50 : class nsHttpHeaderArray
51 0 : {
52 : public:
53 6911 : nsHttpHeaderArray() {}
54 6637 : ~nsHttpHeaderArray() { Clear(); }
55 :
56 : const char *PeekHeader(nsHttpAtom header);
57 :
58 : // Used by internal setters: to set header from network use SetHeaderFromNet
59 : nsresult SetHeader(nsHttpAtom header, const nsACString &value,
60 : bool merge = false);
61 :
62 : // Merges supported headers. For other duplicate values, determines if error
63 : // needs to be thrown or 1st value kept.
64 : nsresult SetHeaderFromNet(nsHttpAtom header, const nsACString &value);
65 :
66 : nsresult GetHeader(nsHttpAtom header, nsACString &value);
67 : void ClearHeader(nsHttpAtom h);
68 :
69 : // Find the location of the given header value, or null if none exists.
70 11612 : const char *FindHeaderValue(nsHttpAtom header, const char *value) {
71 : return nsHttp::FindToken(PeekHeader(header), value,
72 11612 : HTTP_HEADER_VALUE_SEPS);
73 : }
74 :
75 : // Determine if the given header value exists.
76 11612 : bool HasHeaderValue(nsHttpAtom header, const char *value) {
77 11612 : return FindHeaderValue(header, value) != nsnull;
78 : }
79 :
80 : nsresult VisitHeaders(nsIHttpHeaderVisitor *visitor);
81 :
82 : // parse a header line, return the header atom and a pointer to the
83 : // header value (the substring of the header line -- do not free).
84 : nsresult ParseHeaderLine(const char *line,
85 : nsHttpAtom *header=nsnull,
86 : char **value=nsnull);
87 :
88 : void Flatten(nsACString &, bool pruneProxyHeaders=false);
89 :
90 1295 : PRUint32 Count() { return mHeaders.Length(); }
91 :
92 : const char *PeekHeaderAt(PRUint32 i, nsHttpAtom &header);
93 :
94 : void Clear();
95 :
96 : struct nsEntry
97 44743 : {
98 46387 : nsEntry() {}
99 :
100 : nsHttpAtom header;
101 : nsCString value;
102 :
103 : struct MatchHeader {
104 1509450 : bool Equals(const nsEntry &entry, const nsHttpAtom &header) const {
105 1509450 : return entry.header == header;
106 : }
107 : };
108 : };
109 :
110 : private:
111 : PRInt32 LookupEntry(nsHttpAtom header, nsEntry **);
112 : void MergeHeader(nsHttpAtom header, nsEntry *entry, const nsACString &value);
113 :
114 : // Header cannot be merged: only one value possible
115 : bool IsSingletonHeader(nsHttpAtom header);
116 : // For some headers we want to track empty values to prevent them being
117 : // combined with non-empty ones as a CRLF attack vector
118 : bool TrackEmptyHeader(nsHttpAtom header);
119 :
120 : // Subset of singleton headers: should never see multiple, different
121 : // instances of these, else something fishy may be going on (like CLRF
122 : // injection)
123 : bool IsSuspectDuplicateHeader(nsHttpAtom header);
124 :
125 : nsTArray<nsEntry> mHeaders;
126 :
127 : friend struct IPC::ParamTraits<nsHttpHeaderArray>;
128 : };
129 :
130 :
131 : //-----------------------------------------------------------------------------
132 : // nsHttpHeaderArray <private>: inline functions
133 : //-----------------------------------------------------------------------------
134 :
135 : inline PRInt32
136 109219 : nsHttpHeaderArray::LookupEntry(nsHttpAtom header, nsEntry **entry)
137 : {
138 109219 : PRUint32 index = mHeaders.IndexOf(header, 0, nsEntry::MatchHeader());
139 109219 : if (index != PR_UINT32_MAX)
140 13083 : *entry = &mHeaders[index];
141 109219 : return index;
142 : }
143 :
144 : inline bool
145 35 : nsHttpHeaderArray::IsSingletonHeader(nsHttpAtom header)
146 : {
147 35 : return header == nsHttp::Content_Type ||
148 35 : header == nsHttp::Content_Disposition ||
149 32 : header == nsHttp::Content_Length ||
150 19 : header == nsHttp::User_Agent ||
151 19 : header == nsHttp::Referer ||
152 18 : header == nsHttp::Host ||
153 18 : header == nsHttp::Authorization ||
154 18 : header == nsHttp::Proxy_Authorization ||
155 18 : header == nsHttp::If_Modified_Since ||
156 18 : header == nsHttp::If_Unmodified_Since ||
157 18 : header == nsHttp::From ||
158 18 : header == nsHttp::Location ||
159 266 : header == nsHttp::Max_Forwards;
160 : }
161 :
162 : inline bool
163 39 : nsHttpHeaderArray::TrackEmptyHeader(nsHttpAtom header)
164 : {
165 39 : return header == nsHttp::Content_Length ||
166 39 : header == nsHttp::Location;
167 : }
168 :
169 : inline void
170 10 : nsHttpHeaderArray::MergeHeader(nsHttpAtom header,
171 : nsEntry *entry,
172 : const nsACString &value)
173 : {
174 10 : if (value.IsEmpty())
175 0 : return; // merge of empty header = no-op
176 :
177 : // Append the new value to the existing value
178 28 : if (header == nsHttp::Set_Cookie ||
179 10 : header == nsHttp::WWW_Authenticate ||
180 8 : header == nsHttp::Proxy_Authenticate)
181 : {
182 : // Special case these headers and use a newline delimiter to
183 : // delimit the values from one another as commas may appear
184 : // in the values of these headers contrary to what the spec says.
185 6 : entry->value.Append('\n');
186 : } else {
187 : // Delimit each value from the others using a comma (per HTTP spec)
188 4 : entry->value.AppendLiteral(", ");
189 : }
190 10 : entry->value.Append(value);
191 : }
192 :
193 : inline bool
194 12 : nsHttpHeaderArray::IsSuspectDuplicateHeader(nsHttpAtom header)
195 : {
196 12 : bool retval = header == nsHttp::Content_Length ||
197 6 : header == nsHttp::Content_Disposition ||
198 18 : header == nsHttp::Location;
199 :
200 12 : NS_ASSERTION(!retval || IsSingletonHeader(header),
201 : "Only non-mergeable headers should be in this list\n");
202 :
203 12 : return retval;
204 : }
205 :
206 : #endif
|