blob: f6656e35ace0aef4f331fe09e607e59984b82309 [file] [log] [blame]
[email protected]d807bf92009-04-22 20:38:071// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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.
[email protected]7eab0d2262009-10-14 22:05:5426class HttpResponseHeaders
27 : public base::RefCountedThreadSafe<HttpResponseHeaders> {
initial.commit586acc5fe2008-07-26 22:42:5228 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;
[email protected]8bf26f49a2009-06-12 17:35:5055 static const PersistOptions PERSIST_SANS_RANGES = 1 << 4;
[email protected]cd5b9a732008-11-20 08:14:3956
57 // Appends a representation of this object to the given pickle.
58 // The options argument can be a combination of PersistOptions.
59 void Persist(Pickle* pickle, PersistOptions options);
initial.commit586acc5fe2008-07-26 22:42:5260
61 // Performs header merging as described in 13.5.3 of RFC 2616.
62 void Update(const HttpResponseHeaders& new_headers);
63
[email protected]95792eb12009-06-22 21:30:4064 // Removes all instances of a particular header.
65 void RemoveHeader(const std::string& name);
66
67 // Adds a particular header. |header| has to be a single header without any
68 // EOL termination, just [<header-name>: <header-values>]
69 // If a header with the same name is already stored, the two headers are not
70 // merged together by this method; the one provided is simply put at the
71 // end of the list.
72 void AddHeader(const std::string& header);
73
[email protected]44f873a62009-08-12 00:14:4874 // Replaces the current status line with the provided one (|new_status| should
75 // not have any EOL).
76 void ReplaceStatusLine(const std::string& new_status);
77
initial.commit586acc5fe2008-07-26 22:42:5278 // Creates a normalized header string. The output will be formatted exactly
79 // like so:
80 // HTTP/<version> <status_code> <status_text>\n
81 // [<header-name>: <header-values>\n]*
82 // meaning, each line is \n-terminated, and there is no extra whitespace
83 // beyond the single space separators shown (of course, values can contain
84 // whitespace within them). If a given header-name appears more than once
85 // in the set of headers, they are combined into a single line like so:
86 // <header-name>: <header-value1>, <header-value2>, ...<header-valueN>\n
87 //
88 // DANGER: For some headers (e.g., "Set-Cookie"), the normalized form can be
89 // a lossy format. This is due to the fact that some servers generate
90 // Set-Cookie headers that contain unquoted commas (usually as part of the
91 // value of an "expires" attribute). So, use this function with caution. Do
92 // not expect to be able to re-parse Set-Cookie headers from this output.
93 //
94 // NOTE: Do not make any assumptions about the encoding of this output
95 // string. It may be non-ASCII, and the encoding used by the server is not
96 // necessarily known to us. Do not assume that this output is UTF-8!
97 //
98 // TODO(darin): remove this method
99 //
100 void GetNormalizedHeaders(std::string* output) const;
101
102 // Fetch the "normalized" value of a single header, where all values for the
103 // header name are separated by commas. See the GetNormalizedHeaders for
104 // format details. Returns false if this header wasn't found.
105 //
106 // NOTE: Do not make any assumptions about the encoding of this output
107 // string. It may be non-ASCII, and the encoding used by the server is not
108 // necessarily known to us. Do not assume that this output is UTF-8!
109 //
110 // TODO(darin): remove this method
111 //
112 bool GetNormalizedHeader(const std::string& name, std::string* value) const;
113
114 // Returns the normalized status line. For HTTP/0.9 responses (i.e.,
115 // responses that lack a status line), this is the manufactured string
116 // "HTTP/0.9 200 OK".
117 std::string GetStatusLine() const;
118
[email protected]231d5a32008-09-13 00:45:27119 // Get the HTTP version of the normalized status line.
120 HttpVersion GetHttpVersion() const {
121 return http_version_;
122 }
123
124 // Get the HTTP version determined while parsing; or (0,0) if parsing failed
125 HttpVersion GetParsedHttpVersion() const {
126 return parsed_http_version_;
127 }
128
129 // Get the HTTP status text of the normalized status line.
130 std::string GetStatusText() const;
131
initial.commit586acc5fe2008-07-26 22:42:52132 // Enumerate the "lines" of the response headers. This skips over the status
133 // line. Use GetStatusLine if you are interested in that. Note that this
134 // method returns the un-coalesced response header lines, so if a response
135 // header appears on multiple lines, then it will appear multiple times in
136 // this enumeration (in the order the header lines were received from the
137 // server). Initialize a 'void*' variable to NULL and pass it by address to
138 // EnumerateHeaderLines. Call EnumerateHeaderLines repeatedly until it
139 // returns false. The out-params 'name' and 'value' are set upon success.
140 bool EnumerateHeaderLines(void** iter,
141 std::string* name,
142 std::string* value) const;
143
144 // Enumerate the values of the specified header. If you are only interested
145 // in the first header, then you can pass NULL for the 'iter' parameter.
146 // Otherwise, to iterate across all values for the specified header,
147 // initialize a 'void*' variable to NULL and pass it by address to
148 // EnumerateHeader. Call EnumerateHeader repeatedly until it returns false.
149 bool EnumerateHeader(void** iter,
150 const std::string& name,
151 std::string* value) const;
152
153 // Returns true if the response contains the specified header-value pair.
154 // Both name and value are compared case insensitively.
155 bool HasHeaderValue(const std::string& name, const std::string& value) const;
156
157 // Get the mime type and charset values in lower case form from the headers.
158 // Empty strings are returned if the values are not present.
159 void GetMimeTypeAndCharset(std::string* mime_type,
160 std::string* charset) const;
161
162 // Get the mime type in lower case from the headers. If there's no mime
163 // type, returns false.
164 bool GetMimeType(std::string* mime_type) const;
165
166 // Get the charset in lower case from the headers. If there's no charset,
167 // returns false.
168 bool GetCharset(std::string* charset) const;
169
170 // Returns true if this response corresponds to a redirect. The target
171 // location of the redirect is optionally returned if location is non-null.
172 bool IsRedirect(std::string* location) const;
173
[email protected]86de13d2009-11-24 01:21:35174 // Returns true if the HTTP response code passed in corresponds to a
175 // redirect.
176 static bool IsRedirectResponseCode(int response_code);
177
initial.commit586acc5fe2008-07-26 22:42:52178 // Returns true if the response cannot be reused without validation. The
179 // result is relative to the current_time parameter, which is a parameter to
180 // support unit testing. The request_time parameter indicates the time at
181 // which the request was made that resulted in this response, which was
182 // received at response_time.
[email protected]e1acf6f2008-10-27 20:43:33183 bool RequiresValidation(const base::Time& request_time,
184 const base::Time& response_time,
185 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52186
187 // Returns the amount of time the server claims the response is fresh from
188 // the time the response was generated. See section 13.2.4 of RFC 2616. See
189 // RequiresValidation for a description of the response_time parameter.
[email protected]e1acf6f2008-10-27 20:43:33190 base::TimeDelta GetFreshnessLifetime(const base::Time& response_time) const;
initial.commit586acc5fe2008-07-26 22:42:52191
192 // Returns the age of the response. See section 13.2.3 of RFC 2616.
193 // See RequiresValidation for a description of this method's parameters.
[email protected]e1acf6f2008-10-27 20:43:33194 base::TimeDelta GetCurrentAge(const base::Time& request_time,
195 const base::Time& response_time,
196 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52197
198 // The following methods extract values from the response headers. If a
199 // value is not present, then false is returned. Otherwise, true is returned
200 // and the out param is assigned to the corresponding value.
[email protected]e1acf6f2008-10-27 20:43:33201 bool GetMaxAgeValue(base::TimeDelta* value) const;
202 bool GetAgeValue(base::TimeDelta* value) const;
203 bool GetDateValue(base::Time* value) const;
204 bool GetLastModifiedValue(base::Time* value) const;
205 bool GetExpiresValue(base::Time* value) const;
initial.commit586acc5fe2008-07-26 22:42:52206
207 // Extracts the time value of a particular header. This method looks for the
208 // first matching header value and parses its value as a HTTP-date.
[email protected]e1acf6f2008-10-27 20:43:33209 bool GetTimeValuedHeader(const std::string& name, base::Time* result) const;
initial.commit586acc5fe2008-07-26 22:42:52210
211 // Determines if this response indicates a keep-alive connection.
212 bool IsKeepAlive() const;
213
[email protected]dbd39fb2010-01-08 01:13:36214 // Returns true if this response has a strong etag or last-modified header.
215 // See section 13.3.3 of RFC 2616.
216 bool HasStrongValidators() const;
217
initial.commit586acc5fe2008-07-26 22:42:52218 // Extracts the value of the Content-Length header or returns -1 if there is
219 // no such header in the response.
220 int64 GetContentLength() const;
221
[email protected]7eab0d2262009-10-14 22:05:54222 // Extracts the values in a Content-Range header and returns true if they are
223 // valid for a 206 response; otherwise returns false.
[email protected]d807bf92009-04-22 20:38:07224 // The following values will be outputted:
225 // |*first_byte_position| = inclusive position of the first byte of the range
226 // |*last_byte_position| = inclusive position of the last byte of the range
227 // |*instance_length| = size in bytes of the object requested
228 // If any of the above values is unknown, its value will be -1.
229 bool GetContentRange(int64* first_byte_position,
230 int64* last_byte_position,
231 int64* instance_length) const;
232
initial.commit586acc5fe2008-07-26 22:42:52233 // Returns the HTTP response code. This is 0 if the response code text seems
234 // to exist but could not be parsed. Otherwise, it defaults to 200 if the
235 // response code is not found in the raw headers.
236 int response_code() const { return response_code_; }
237
238 // Returns the raw header string.
239 const std::string& raw_headers() const { return raw_headers_; }
240
241 private:
[email protected]8a2a25f2008-08-19 23:06:05242 friend class base::RefCountedThreadSafe<HttpResponseHeaders>;
initial.commit586acc5fe2008-07-26 22:42:52243
[email protected]95792eb12009-06-22 21:30:40244 typedef base::hash_set<std::string> HeaderSet;
245
initial.commit586acc5fe2008-07-26 22:42:52246 HttpResponseHeaders() {}
247 ~HttpResponseHeaders() {}
248
249 // Initializes from the given raw headers.
250 void Parse(const std::string& raw_input);
251
252 // Helper function for ParseStatusLine.
253 // Tries to extract the "HTTP/X.Y" from a status line formatted like:
254 // HTTP/1.1 200 OK
255 // with line_begin and end pointing at the begin and end of this line. If the
[email protected]231d5a32008-09-13 00:45:27256 // status line is malformed, returns HttpVersion(0,0).
257 static HttpVersion ParseVersion(std::string::const_iterator line_begin,
258 std::string::const_iterator line_end);
initial.commit586acc5fe2008-07-26 22:42:52259
260 // Tries to extract the status line from a header block, given the first
[email protected]72d1e592009-03-10 17:39:46261 // line of said header block. If the status line is malformed, we'll
262 // construct a valid one. Example input:
initial.commit586acc5fe2008-07-26 22:42:52263 // HTTP/1.1 200 OK
264 // with line_begin and end pointing at the begin and end of this line.
265 // Output will be a normalized version of this, with a trailing \n.
266 void ParseStatusLine(std::string::const_iterator line_begin,
[email protected]231d5a32008-09-13 00:45:27267 std::string::const_iterator line_end,
268 bool has_headers);
initial.commit586acc5fe2008-07-26 22:42:52269
initial.commit586acc5fe2008-07-26 22:42:52270 // Find the header in our list (case-insensitive) starting with parsed_ at
271 // index |from|. Returns string::npos if not found.
272 size_t FindHeader(size_t from, const std::string& name) const;
273
[email protected]79867b592008-08-21 21:23:52274 // Add a header->value pair to our list. If we already have header in our
275 // list, append the value to it.
initial.commit586acc5fe2008-07-26 22:42:52276 void AddHeader(std::string::const_iterator name_begin,
277 std::string::const_iterator name_end,
278 std::string::const_iterator value_begin,
279 std::string::const_iterator value_end);
280
281 // Add to parsed_ given the fields of a ParsedHeader object.
282 void AddToParsed(std::string::const_iterator name_begin,
283 std::string::const_iterator name_end,
284 std::string::const_iterator value_begin,
285 std::string::const_iterator value_end);
286
[email protected]95792eb12009-06-22 21:30:40287 // Replaces the current headers with the merged version of |raw_headers| and
288 // the current headers without the headers in |headers_to_remove|. Note that
289 // |headers_to_remove| are removed from the current headers (before the
290 // merge), not after the merge.
291 void MergeWithHeaders(const std::string& raw_headers,
292 const HeaderSet& headers_to_remove);
initial.commit586acc5fe2008-07-26 22:42:52293
[email protected]cd5b9a732008-11-20 08:14:39294 // Adds the values from any 'cache-control: no-cache="foo,bar"' headers.
295 void AddNonCacheableHeaders(HeaderSet* header_names) const;
296
297 // Adds the set of header names that contain cookie values.
298 static void AddSensitiveHeaders(HeaderSet* header_names);
299
300 // Adds the set of rfc2616 hop-by-hop response headers.
301 static void AddHopByHopHeaders(HeaderSet* header_names);
302
303 // Adds the set of challenge response headers.
304 static void AddChallengeHeaders(HeaderSet* header_names);
305
306 // Adds the set of cookie response headers.
307 static void AddCookieHeaders(HeaderSet* header_names);
initial.commit586acc5fe2008-07-26 22:42:52308
[email protected]8bf26f49a2009-06-12 17:35:50309 // Adds the set of content range response headers.
310 static void AddHopContentRangeHeaders(HeaderSet* header_names);
311
initial.commit586acc5fe2008-07-26 22:42:52312 // The members of this structure point into raw_headers_.
313 struct ParsedHeader {
314 std::string::const_iterator name_begin;
315 std::string::const_iterator name_end;
316 std::string::const_iterator value_begin;
317 std::string::const_iterator value_end;
318
319 // A header "continuation" contains only a subsequent value for the
320 // preceding header. (Header values are comma separated.)
321 bool is_continuation() const { return name_begin == name_end; }
322 };
323 typedef std::vector<ParsedHeader> HeaderList;
324
325 // We keep a list of ParsedHeader objects. These tell us where to locate the
326 // header-value pairs within raw_headers_.
327 HeaderList parsed_;
328
329 // The raw_headers_ consists of the normalized status line (terminated with a
330 // null byte) and then followed by the raw null-terminated headers from the
[email protected]036d8772008-09-06 01:00:53331 // input that was passed to our constructor. We preserve the input [*] to
initial.commit586acc5fe2008-07-26 22:42:52332 // maintain as much ancillary fidelity as possible (since it is sometimes
333 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest).
[email protected]036d8772008-09-06 01:00:53334 // [*] The status line may be modified.
initial.commit586acc5fe2008-07-26 22:42:52335 std::string raw_headers_;
336
337 // This is the parsed HTTP response code.
338 int response_code_;
339
[email protected]231d5a32008-09-13 00:45:27340 // The normalized http version (consistent with what GetStatusLine() returns).
341 HttpVersion http_version_;
342
343 // The parsed http version number (not normalized).
344 HttpVersion parsed_http_version_;
345
[email protected]8a2a25f2008-08-19 23:06:05346 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders);
initial.commit586acc5fe2008-07-26 22:42:52347};
348
349} // namespace net
350
[email protected]e2a23092009-03-17 15:35:18351#endif // NET_HTTP_HTTP_RESPONSE_HEADERS_H_