Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / netwerk / protocol / http / src / nsHttpHeaderArray.cpp
blobf17354a1f3ecfc69a25c0f3efdbe9a806942c0e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 sts=4 ci et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla.
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.
23 * Contributor(s):
24 * Darin Fisher <darin@netscape.com> (original author)
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.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsHttpHeaderArray.h"
41 #include "nsHttp.h"
43 //-----------------------------------------------------------------------------
44 // nsHttpHeaderArray <public>
45 //-----------------------------------------------------------------------------
47 nsresult
48 nsHttpHeaderArray::SetHeader(nsHttpAtom header,
49 const nsACString &value,
50 PRBool merge)
52 nsEntry *entry = nsnull;
53 PRInt32 index;
55 index = LookupEntry(header, &entry);
57 // If an empty value is passed in, then delete the header entry...
58 // unless we are merging, in which case this function becomes a NOP.
59 if (value.IsEmpty()) {
60 if (!merge && entry)
61 mHeaders.RemoveElementAt(index);
62 return NS_OK;
65 // Create a new entry, or...
66 if (!entry) {
67 entry = mHeaders.AppendElement(); //new nsEntry(header, value);
68 if (!entry)
69 return NS_ERROR_OUT_OF_MEMORY;
70 entry->header = header;
71 entry->value = value;
73 // Append the new value to the existing value iff...
74 else if (merge && CanAppendToHeader(header)) {
75 if (header == nsHttp::Set_Cookie ||
76 header == nsHttp::WWW_Authenticate ||
77 header == nsHttp::Proxy_Authenticate)
78 // Special case these headers and use a newline delimiter to
79 // delimit the values from one another as commas may appear
80 // in the values of these headers contrary to what the spec says.
81 entry->value.Append('\n');
82 else
83 // Delimit each value from the others using a comma (per HTTP spec)
84 entry->value.AppendLiteral(", ");
85 entry->value.Append(value);
87 // Replace the existing string with the new value
88 else
89 entry->value = value;
90 return NS_OK;
93 void
94 nsHttpHeaderArray::ClearHeader(nsHttpAtom header)
96 mHeaders.RemoveElement(header, nsEntry::MatchHeader());
99 const char *
100 nsHttpHeaderArray::PeekHeader(nsHttpAtom header)
102 nsEntry *entry = nsnull;
103 LookupEntry(header, &entry);
104 return entry ? entry->value.get() : nsnull;
107 nsresult
108 nsHttpHeaderArray::GetHeader(nsHttpAtom header, nsACString &result)
110 nsEntry *entry = nsnull;
111 LookupEntry(header, &entry);
112 if (!entry)
113 return NS_ERROR_NOT_AVAILABLE;
114 result = entry->value;
115 return NS_OK;
118 nsresult
119 nsHttpHeaderArray::VisitHeaders(nsIHttpHeaderVisitor *visitor)
121 NS_ENSURE_ARG_POINTER(visitor);
122 PRUint32 i, count = mHeaders.Length();
123 for (i = 0; i < count; ++i) {
124 const nsEntry &entry = mHeaders[i];
125 if (NS_FAILED(visitor->VisitHeader(nsDependentCString(entry.header),
126 entry.value)))
127 break;
129 return NS_OK;
132 void
133 nsHttpHeaderArray::ParseHeaderLine(char *line, nsHttpAtom *hdr, char **val)
136 // BNF from section 4.2 of RFC 2616:
138 // message-header = field-name ":" [ field-value ]
139 // field-name = token
140 // field-value = *( field-content | LWS )
141 // field-content = <the OCTETs making up the field-value
142 // and consisting of either *TEXT or combinations
143 // of token, separators, and quoted-string>
146 // We skip over mal-formed headers in the hope that we'll still be able to
147 // do something useful with the response.
149 char *p = (char *) strchr(line, ':');
150 if (!p) {
151 LOG(("malformed header [%s]: no colon\n", line));
152 return;
155 // make sure we have a valid token for the field-name
156 if (!nsHttp::IsValidToken(line, p)) {
157 LOG(("malformed header [%s]: field-name not a token\n", line));
158 return;
161 *p = 0; // null terminate field-name
163 nsHttpAtom atom = nsHttp::ResolveAtom(line);
164 if (!atom) {
165 LOG(("failed to resolve atom [%s]\n", line));
166 return;
169 // skip over whitespace
170 p = net_FindCharNotInSet(++p, HTTP_LWS);
172 // trim trailing whitespace - bug 86608
173 char *p2 = net_RFindCharNotInSet(p, HTTP_LWS);
175 *++p2 = 0; // null terminate header value; if all chars starting at |p|
176 // consisted of LWS, then p2 would have pointed at |p-1|, so
177 // the prefix increment is always valid.
179 // assign return values
180 if (hdr) *hdr = atom;
181 if (val) *val = p;
183 // assign response header
184 SetHeader(atom, nsDependentCString(p, p2 - p), PR_TRUE);
187 void
188 nsHttpHeaderArray::Flatten(nsACString &buf, PRBool pruneProxyHeaders)
190 PRUint32 i, count = mHeaders.Length();
191 for (i = 0; i < count; ++i) {
192 const nsEntry &entry = mHeaders[i];
193 // prune proxy headers if requested
194 if (pruneProxyHeaders && ((entry.header == nsHttp::Proxy_Authorization) ||
195 (entry.header == nsHttp::Proxy_Connection)))
196 continue;
197 buf.Append(entry.header);
198 buf.AppendLiteral(": ");
199 buf.Append(entry.value);
200 buf.AppendLiteral("\r\n");
204 const char *
205 nsHttpHeaderArray::PeekHeaderAt(PRUint32 index, nsHttpAtom &header)
207 const nsEntry &entry = mHeaders[index];
209 header = entry.header;
210 return entry.value.get();
213 void
214 nsHttpHeaderArray::Clear()
216 mHeaders.Clear();
219 //-----------------------------------------------------------------------------
220 // nsHttpHeaderArray <private>
221 //-----------------------------------------------------------------------------
223 PRInt32
224 nsHttpHeaderArray::LookupEntry(nsHttpAtom header, nsEntry **entry)
226 PRUint32 index = mHeaders.IndexOf(header, 0, nsEntry::MatchHeader());
227 if (index != PR_UINT32_MAX)
228 *entry = &mHeaders[index];
229 return index;
232 PRBool
233 nsHttpHeaderArray::CanAppendToHeader(nsHttpAtom header)
235 return header != nsHttp::Content_Type &&
236 header != nsHttp::Content_Length &&
237 header != nsHttp::User_Agent &&
238 header != nsHttp::Referer &&
239 header != nsHttp::Host &&
240 header != nsHttp::Authorization &&
241 header != nsHttp::Proxy_Authorization &&
242 header != nsHttp::If_Modified_Since &&
243 header != nsHttp::If_Unmodified_Since &&
244 header != nsHttp::From &&
245 header != nsHttp::Location &&
246 header != nsHttp::Max_Forwards;