blob: e9b2c4bc909ef505715822ad46b808dcb1d71ad2 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]e2a23092009-03-17 15:35:185#ifndef NET_HTTP_HTTP_RESPONSE_HEADERS_H_
6#define NET_HTTP_HTTP_RESPONSE_HEADERS_H_
initial.commit586acc5fe2008-07-26 22:42:527
initial.commit586acc5fe2008-07-26 22:42:528#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
[email protected]8a2a25f2008-08-19 23:06:0512#include "base/hash_tables.h"
initial.commit586acc5fe2008-07-26 22:42:5213#include "base/ref_counted.h"
[email protected]231d5a32008-09-13 00:45:2714#include "net/http/http_version.h"
initial.commit586acc5fe2008-07-26 22:42:5215
16class Pickle;
[email protected]e1acf6f2008-10-27 20:43:3317
18namespace base {
initial.commit586acc5fe2008-07-26 22:42:5219class Time;
20class TimeDelta;
[email protected]e1acf6f2008-10-27 20:43:3321}
initial.commit586acc5fe2008-07-26 22:42:5222
23namespace net {
24
25// HttpResponseHeaders: parses and holds HTTP response headers.
26class HttpResponseHeaders :
27 public base::RefCountedThreadSafe<HttpResponseHeaders> {
28 public:
29 // Parses the given raw_headers. raw_headers should be formatted thus:
30 // includes the http status response line, each line is \0-terminated, and
31 // it's terminated by an empty line (ie, 2 \0s in a row).
[email protected]036d8772008-09-06 01:00:5332 // (Note that line continuations should have already been joined;
33 // see HttpUtil::AssembleRawHeaders)
initial.commit586acc5fe2008-07-26 22:42:5234 //
35 // NOTE: For now, raw_headers is not really 'raw' in that this constructor is
36 // called with a 'NativeMB' string on Windows because WinHTTP does not allow
37 // us to access the raw byte sequence as sent by a web server. In any case,
38 // HttpResponseHeaders does not perform any encoding changes on the input.
39 //
40 explicit HttpResponseHeaders(const std::string& raw_headers);
41
42 // Initializes from the representation stored in the given pickle. The data
43 // for this object is found relative to the given pickle_iter, which should
44 // be passed to the pickle's various Read* methods.
45 HttpResponseHeaders(const Pickle& pickle, void** pickle_iter);
46
[email protected]cd5b9a732008-11-20 08:14:3947 // Persist options.
48 typedef int PersistOptions;
49 static const PersistOptions PERSIST_RAW = -1; // Raw, unparsed headers.
50 static const PersistOptions PERSIST_ALL = 0; // Parsed headers.
51 static const PersistOptions PERSIST_SANS_COOKIES = 1 << 0;
52 static const PersistOptions PERSIST_SANS_CHALLENGES = 1 << 1;
53 static const PersistOptions PERSIST_SANS_HOP_BY_HOP = 1 << 2;
54 static const PersistOptions PERSIST_SANS_NON_CACHEABLE = 1 << 3;
55
56 // Appends a representation of this object to the given pickle.
57 // The options argument can be a combination of PersistOptions.
58 void Persist(Pickle* pickle, PersistOptions options);
initial.commit586acc5fe2008-07-26 22:42:5259
60 // Performs header merging as described in 13.5.3 of RFC 2616.
61 void Update(const HttpResponseHeaders& new_headers);
62
63 // Creates a normalized header string. The output will be formatted exactly
64 // like so:
65 // HTTP/<version> <status_code> <status_text>\n
66 // [<header-name>: <header-values>\n]*
67 // meaning, each line is \n-terminated, and there is no extra whitespace
68 // beyond the single space separators shown (of course, values can contain
69 // whitespace within them). If a given header-name appears more than once
70 // in the set of headers, they are combined into a single line like so:
71 // <header-name>: <header-value1>, <header-value2>, ...<header-valueN>\n
72 //
73 // DANGER: For some headers (e.g., "Set-Cookie"), the normalized form can be
74 // a lossy format. This is due to the fact that some servers generate
75 // Set-Cookie headers that contain unquoted commas (usually as part of the
76 // value of an "expires" attribute). So, use this function with caution. Do
77 // not expect to be able to re-parse Set-Cookie headers from this output.
78 //
79 // NOTE: Do not make any assumptions about the encoding of this output
80 // string. It may be non-ASCII, and the encoding used by the server is not
81 // necessarily known to us. Do not assume that this output is UTF-8!
82 //
83 // TODO(darin): remove this method
84 //
85 void GetNormalizedHeaders(std::string* output) const;
86
87 // Fetch the "normalized" value of a single header, where all values for the
88 // header name are separated by commas. See the GetNormalizedHeaders for
89 // format details. Returns false if this header wasn't found.
90 //
91 // NOTE: Do not make any assumptions about the encoding of this output
92 // string. It may be non-ASCII, and the encoding used by the server is not
93 // necessarily known to us. Do not assume that this output is UTF-8!
94 //
95 // TODO(darin): remove this method
96 //
97 bool GetNormalizedHeader(const std::string& name, std::string* value) const;
98
99 // Returns the normalized status line. For HTTP/0.9 responses (i.e.,
100 // responses that lack a status line), this is the manufactured string
101 // "HTTP/0.9 200 OK".
102 std::string GetStatusLine() const;
103
[email protected]231d5a32008-09-13 00:45:27104 // Get the HTTP version of the normalized status line.
105 HttpVersion GetHttpVersion() const {
106 return http_version_;
107 }
108
109 // Get the HTTP version determined while parsing; or (0,0) if parsing failed
110 HttpVersion GetParsedHttpVersion() const {
111 return parsed_http_version_;
112 }
113
114 // Get the HTTP status text of the normalized status line.
115 std::string GetStatusText() const;
116
initial.commit586acc5fe2008-07-26 22:42:52117 // Enumerate the "lines" of the response headers. This skips over the status
118 // line. Use GetStatusLine if you are interested in that. Note that this
119 // method returns the un-coalesced response header lines, so if a response
120 // header appears on multiple lines, then it will appear multiple times in
121 // this enumeration (in the order the header lines were received from the
122 // server). Initialize a 'void*' variable to NULL and pass it by address to
123 // EnumerateHeaderLines. Call EnumerateHeaderLines repeatedly until it
124 // returns false. The out-params 'name' and 'value' are set upon success.
125 bool EnumerateHeaderLines(void** iter,
126 std::string* name,
127 std::string* value) const;
128
129 // Enumerate the values of the specified header. If you are only interested
130 // in the first header, then you can pass NULL for the 'iter' parameter.
131 // Otherwise, to iterate across all values for the specified header,
132 // initialize a 'void*' variable to NULL and pass it by address to
133 // EnumerateHeader. Call EnumerateHeader repeatedly until it returns false.
134 bool EnumerateHeader(void** iter,
135 const std::string& name,
136 std::string* value) const;
137
138 // Returns true if the response contains the specified header-value pair.
139 // Both name and value are compared case insensitively.
140 bool HasHeaderValue(const std::string& name, const std::string& value) const;
141
142 // Get the mime type and charset values in lower case form from the headers.
143 // Empty strings are returned if the values are not present.
144 void GetMimeTypeAndCharset(std::string* mime_type,
145 std::string* charset) const;
146
147 // Get the mime type in lower case from the headers. If there's no mime
148 // type, returns false.
149 bool GetMimeType(std::string* mime_type) const;
150
151 // Get the charset in lower case from the headers. If there's no charset,
152 // returns false.
153 bool GetCharset(std::string* charset) const;
154
155 // Returns true if this response corresponds to a redirect. The target
156 // location of the redirect is optionally returned if location is non-null.
157 bool IsRedirect(std::string* location) const;
158
159 // Returns true if the response cannot be reused without validation. The
160 // result is relative to the current_time parameter, which is a parameter to
161 // support unit testing. The request_time parameter indicates the time at
162 // which the request was made that resulted in this response, which was
163 // received at response_time.
[email protected]e1acf6f2008-10-27 20:43:33164 bool RequiresValidation(const base::Time& request_time,
165 const base::Time& response_time,
166 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52167
168 // Returns the amount of time the server claims the response is fresh from
169 // the time the response was generated. See section 13.2.4 of RFC 2616. See
170 // RequiresValidation for a description of the response_time parameter.
[email protected]e1acf6f2008-10-27 20:43:33171 base::TimeDelta GetFreshnessLifetime(const base::Time& response_time) const;
initial.commit586acc5fe2008-07-26 22:42:52172
173 // Returns the age of the response. See section 13.2.3 of RFC 2616.
174 // See RequiresValidation for a description of this method's parameters.
[email protected]e1acf6f2008-10-27 20:43:33175 base::TimeDelta GetCurrentAge(const base::Time& request_time,
176 const base::Time& response_time,
177 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52178
179 // The following methods extract values from the response headers. If a
180 // value is not present, then false is returned. Otherwise, true is returned
181 // and the out param is assigned to the corresponding value.
[email protected]e1acf6f2008-10-27 20:43:33182 bool GetMaxAgeValue(base::TimeDelta* value) const;
183 bool GetAgeValue(base::TimeDelta* value) const;
184 bool GetDateValue(base::Time* value) const;
185 bool GetLastModifiedValue(base::Time* value) const;
186 bool GetExpiresValue(base::Time* value) const;
initial.commit586acc5fe2008-07-26 22:42:52187
188 // Extracts the time value of a particular header. This method looks for the
189 // first matching header value and parses its value as a HTTP-date.
[email protected]e1acf6f2008-10-27 20:43:33190 bool GetTimeValuedHeader(const std::string& name, base::Time* result) const;
initial.commit586acc5fe2008-07-26 22:42:52191
192 // Determines if this response indicates a keep-alive connection.
193 bool IsKeepAlive() const;
194
195 // Extracts the value of the Content-Length header or returns -1 if there is
196 // no such header in the response.
197 int64 GetContentLength() const;
198
199 // Returns the HTTP response code. This is 0 if the response code text seems
200 // to exist but could not be parsed. Otherwise, it defaults to 200 if the
201 // response code is not found in the raw headers.
202 int response_code() const { return response_code_; }
203
204 // Returns the raw header string.
205 const std::string& raw_headers() const { return raw_headers_; }
206
207 private:
[email protected]8a2a25f2008-08-19 23:06:05208 friend class base::RefCountedThreadSafe<HttpResponseHeaders>;
initial.commit586acc5fe2008-07-26 22:42:52209
210 HttpResponseHeaders() {}
211 ~HttpResponseHeaders() {}
212
213 // Initializes from the given raw headers.
214 void Parse(const std::string& raw_input);
215
216 // Helper function for ParseStatusLine.
217 // Tries to extract the "HTTP/X.Y" from a status line formatted like:
218 // HTTP/1.1 200 OK
219 // with line_begin and end pointing at the begin and end of this line. If the
[email protected]231d5a32008-09-13 00:45:27220 // status line is malformed, returns HttpVersion(0,0).
221 static HttpVersion ParseVersion(std::string::const_iterator line_begin,
222 std::string::const_iterator line_end);
initial.commit586acc5fe2008-07-26 22:42:52223
224 // Tries to extract the status line from a header block, given the first
[email protected]72d1e592009-03-10 17:39:46225 // line of said header block. If the status line is malformed, we'll
226 // construct a valid one. Example input:
initial.commit586acc5fe2008-07-26 22:42:52227 // HTTP/1.1 200 OK
228 // with line_begin and end pointing at the begin and end of this line.
229 // Output will be a normalized version of this, with a trailing \n.
230 void ParseStatusLine(std::string::const_iterator line_begin,
[email protected]231d5a32008-09-13 00:45:27231 std::string::const_iterator line_end,
232 bool has_headers);
initial.commit586acc5fe2008-07-26 22:42:52233
initial.commit586acc5fe2008-07-26 22:42:52234 // Find the header in our list (case-insensitive) starting with parsed_ at
235 // index |from|. Returns string::npos if not found.
236 size_t FindHeader(size_t from, const std::string& name) const;
237
[email protected]79867b592008-08-21 21:23:52238 // Add a header->value pair to our list. If we already have header in our
239 // list, append the value to it.
initial.commit586acc5fe2008-07-26 22:42:52240 void AddHeader(std::string::const_iterator name_begin,
241 std::string::const_iterator name_end,
242 std::string::const_iterator value_begin,
243 std::string::const_iterator value_end);
244
245 // Add to parsed_ given the fields of a ParsedHeader object.
246 void AddToParsed(std::string::const_iterator name_begin,
247 std::string::const_iterator name_end,
248 std::string::const_iterator value_begin,
249 std::string::const_iterator value_end);
250
[email protected]8a2a25f2008-08-19 23:06:05251 typedef base::hash_set<std::string> HeaderSet;
initial.commit586acc5fe2008-07-26 22:42:52252
[email protected]cd5b9a732008-11-20 08:14:39253 // Adds the values from any 'cache-control: no-cache="foo,bar"' headers.
254 void AddNonCacheableHeaders(HeaderSet* header_names) const;
255
256 // Adds the set of header names that contain cookie values.
257 static void AddSensitiveHeaders(HeaderSet* header_names);
258
259 // Adds the set of rfc2616 hop-by-hop response headers.
260 static void AddHopByHopHeaders(HeaderSet* header_names);
261
262 // Adds the set of challenge response headers.
263 static void AddChallengeHeaders(HeaderSet* header_names);
264
265 // Adds the set of cookie response headers.
266 static void AddCookieHeaders(HeaderSet* header_names);
initial.commit586acc5fe2008-07-26 22:42:52267
268 // The members of this structure point into raw_headers_.
269 struct ParsedHeader {
270 std::string::const_iterator name_begin;
271 std::string::const_iterator name_end;
272 std::string::const_iterator value_begin;
273 std::string::const_iterator value_end;
274
275 // A header "continuation" contains only a subsequent value for the
276 // preceding header. (Header values are comma separated.)
277 bool is_continuation() const { return name_begin == name_end; }
278 };
279 typedef std::vector<ParsedHeader> HeaderList;
280
281 // We keep a list of ParsedHeader objects. These tell us where to locate the
282 // header-value pairs within raw_headers_.
283 HeaderList parsed_;
284
285 // The raw_headers_ consists of the normalized status line (terminated with a
286 // null byte) and then followed by the raw null-terminated headers from the
[email protected]036d8772008-09-06 01:00:53287 // input that was passed to our constructor. We preserve the input [*] to
initial.commit586acc5fe2008-07-26 22:42:52288 // maintain as much ancillary fidelity as possible (since it is sometimes
289 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest).
[email protected]036d8772008-09-06 01:00:53290 // [*] The status line may be modified.
initial.commit586acc5fe2008-07-26 22:42:52291 std::string raw_headers_;
292
293 // This is the parsed HTTP response code.
294 int response_code_;
295
[email protected]231d5a32008-09-13 00:45:27296 // The normalized http version (consistent with what GetStatusLine() returns).
297 HttpVersion http_version_;
298
299 // The parsed http version number (not normalized).
300 HttpVersion parsed_http_version_;
301
[email protected]8a2a25f2008-08-19 23:06:05302 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders);
initial.commit586acc5fe2008-07-26 22:42:52303};
304
305} // namespace net
306
[email protected]e2a23092009-03-17 15:35:18307#endif // NET_HTTP_HTTP_RESPONSE_HEADERS_H_