blob: aacd35a5ecdfa575834b62b38ff63ac7cbfc3c13 [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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit586acc5fe2008-07-26 22:42:528
initial.commit586acc5fe2008-07-26 22:42:529#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
[email protected]8a2a25f2008-08-19 23:06:0513#include "base/hash_tables.h"
initial.commit586acc5fe2008-07-26 22:42:5214#include "base/ref_counted.h"
[email protected]231d5a32008-09-13 00:45:2715#include "net/http/http_version.h"
initial.commit586acc5fe2008-07-26 22:42:5216
17class Pickle;
[email protected]e1acf6f2008-10-27 20:43:3318
19namespace base {
initial.commit586acc5fe2008-07-26 22:42:5220class Time;
21class TimeDelta;
[email protected]e1acf6f2008-10-27 20:43:3322}
initial.commit586acc5fe2008-07-26 22:42:5223
24namespace net {
25
26// HttpResponseHeaders: parses and holds HTTP response headers.
[email protected]7eab0d2262009-10-14 22:05:5427class HttpResponseHeaders
28 : public base::RefCountedThreadSafe<HttpResponseHeaders> {
initial.commit586acc5fe2008-07-26 22:42:5229 public:
30 // Parses the given raw_headers. raw_headers should be formatted thus:
31 // includes the http status response line, each line is \0-terminated, and
32 // it's terminated by an empty line (ie, 2 \0s in a row).
[email protected]036d8772008-09-06 01:00:5333 // (Note that line continuations should have already been joined;
34 // see HttpUtil::AssembleRawHeaders)
initial.commit586acc5fe2008-07-26 22:42:5235 //
36 // NOTE: For now, raw_headers is not really 'raw' in that this constructor is
37 // called with a 'NativeMB' string on Windows because WinHTTP does not allow
38 // us to access the raw byte sequence as sent by a web server. In any case,
39 // HttpResponseHeaders does not perform any encoding changes on the input.
40 //
41 explicit HttpResponseHeaders(const std::string& raw_headers);
42
43 // Initializes from the representation stored in the given pickle. The data
44 // for this object is found relative to the given pickle_iter, which should
45 // be passed to the pickle's various Read* methods.
46 HttpResponseHeaders(const Pickle& pickle, void** pickle_iter);
47
[email protected]cd5b9a732008-11-20 08:14:3948 // Persist options.
49 typedef int PersistOptions;
50 static const PersistOptions PERSIST_RAW = -1; // Raw, unparsed headers.
51 static const PersistOptions PERSIST_ALL = 0; // Parsed headers.
52 static const PersistOptions PERSIST_SANS_COOKIES = 1 << 0;
53 static const PersistOptions PERSIST_SANS_CHALLENGES = 1 << 1;
54 static const PersistOptions PERSIST_SANS_HOP_BY_HOP = 1 << 2;
55 static const PersistOptions PERSIST_SANS_NON_CACHEABLE = 1 << 3;
[email protected]8bf26f49a2009-06-12 17:35:5056 static const PersistOptions PERSIST_SANS_RANGES = 1 << 4;
[email protected]cd5b9a732008-11-20 08:14:3957
58 // Appends a representation of this object to the given pickle.
59 // The options argument can be a combination of PersistOptions.
60 void Persist(Pickle* pickle, PersistOptions options);
initial.commit586acc5fe2008-07-26 22:42:5261
62 // Performs header merging as described in 13.5.3 of RFC 2616.
63 void Update(const HttpResponseHeaders& new_headers);
64
[email protected]95792eb12009-06-22 21:30:4065 // Removes all instances of a particular header.
66 void RemoveHeader(const std::string& name);
67
68 // Adds a particular header. |header| has to be a single header without any
69 // EOL termination, just [<header-name>: <header-values>]
70 // If a header with the same name is already stored, the two headers are not
71 // merged together by this method; the one provided is simply put at the
72 // end of the list.
73 void AddHeader(const std::string& header);
74
[email protected]44f873a62009-08-12 00:14:4875 // Replaces the current status line with the provided one (|new_status| should
76 // not have any EOL).
77 void ReplaceStatusLine(const std::string& new_status);
78
initial.commit586acc5fe2008-07-26 22:42:5279 // Creates a normalized header string. The output will be formatted exactly
80 // like so:
81 // HTTP/<version> <status_code> <status_text>\n
82 // [<header-name>: <header-values>\n]*
83 // meaning, each line is \n-terminated, and there is no extra whitespace
84 // beyond the single space separators shown (of course, values can contain
85 // whitespace within them). If a given header-name appears more than once
86 // in the set of headers, they are combined into a single line like so:
87 // <header-name>: <header-value1>, <header-value2>, ...<header-valueN>\n
88 //
89 // DANGER: For some headers (e.g., "Set-Cookie"), the normalized form can be
90 // a lossy format. This is due to the fact that some servers generate
91 // Set-Cookie headers that contain unquoted commas (usually as part of the
92 // value of an "expires" attribute). So, use this function with caution. Do
93 // not expect to be able to re-parse Set-Cookie headers from this output.
94 //
95 // NOTE: Do not make any assumptions about the encoding of this output
96 // string. It may be non-ASCII, and the encoding used by the server is not
97 // necessarily known to us. Do not assume that this output is UTF-8!
98 //
99 // TODO(darin): remove this method
100 //
101 void GetNormalizedHeaders(std::string* output) const;
102
[email protected]aef04272010-06-28 18:03:04103 // Gets the raw stored headers, in human-readable form.
104 void GetRawHeaders(std::string* output) const;
105
initial.commit586acc5fe2008-07-26 22:42:52106 // Fetch the "normalized" value of a single header, where all values for the
107 // header name are separated by commas. See the GetNormalizedHeaders for
108 // format details. Returns false if this header wasn't found.
109 //
110 // NOTE: Do not make any assumptions about the encoding of this output
111 // string. It may be non-ASCII, and the encoding used by the server is not
112 // necessarily known to us. Do not assume that this output is UTF-8!
113 //
114 // TODO(darin): remove this method
115 //
116 bool GetNormalizedHeader(const std::string& name, std::string* value) const;
117
118 // Returns the normalized status line. For HTTP/0.9 responses (i.e.,
119 // responses that lack a status line), this is the manufactured string
120 // "HTTP/0.9 200 OK".
121 std::string GetStatusLine() const;
122
[email protected]231d5a32008-09-13 00:45:27123 // Get the HTTP version of the normalized status line.
124 HttpVersion GetHttpVersion() const {
125 return http_version_;
126 }
127
128 // Get the HTTP version determined while parsing; or (0,0) if parsing failed
129 HttpVersion GetParsedHttpVersion() const {
130 return parsed_http_version_;
131 }
132
133 // Get the HTTP status text of the normalized status line.
134 std::string GetStatusText() const;
135
initial.commit586acc5fe2008-07-26 22:42:52136 // Enumerate the "lines" of the response headers. This skips over the status
137 // line. Use GetStatusLine if you are interested in that. Note that this
138 // method returns the un-coalesced response header lines, so if a response
139 // header appears on multiple lines, then it will appear multiple times in
140 // this enumeration (in the order the header lines were received from the
[email protected]2adf2882010-09-27 08:30:37141 // server). Also, a given header might have an empty value. Initialize a
142 // 'void*' variable to NULL and pass it by address to EnumerateHeaderLines.
143 // Call EnumerateHeaderLines repeatedly until it returns false. The
144 // out-params 'name' and 'value' are set upon success.
initial.commit586acc5fe2008-07-26 22:42:52145 bool EnumerateHeaderLines(void** iter,
146 std::string* name,
147 std::string* value) const;
148
149 // Enumerate the values of the specified header. If you are only interested
150 // in the first header, then you can pass NULL for the 'iter' parameter.
151 // Otherwise, to iterate across all values for the specified header,
152 // initialize a 'void*' variable to NULL and pass it by address to
[email protected]2adf2882010-09-27 08:30:37153 // EnumerateHeader. Note that a header might have an empty value. Call
154 // EnumerateHeader repeatedly until it returns false.
initial.commit586acc5fe2008-07-26 22:42:52155 bool EnumerateHeader(void** iter,
156 const std::string& name,
157 std::string* value) const;
158
159 // Returns true if the response contains the specified header-value pair.
160 // Both name and value are compared case insensitively.
161 bool HasHeaderValue(const std::string& name, const std::string& value) const;
162
[email protected]3f662f12010-03-25 19:56:12163 // Returns true if the response contains the specified header.
164 // The name is compared case insensitively.
165 bool HasHeader(const std::string& name) const;
166
initial.commit586acc5fe2008-07-26 22:42:52167 // Get the mime type and charset values in lower case form from the headers.
168 // Empty strings are returned if the values are not present.
169 void GetMimeTypeAndCharset(std::string* mime_type,
170 std::string* charset) const;
171
172 // Get the mime type in lower case from the headers. If there's no mime
173 // type, returns false.
174 bool GetMimeType(std::string* mime_type) const;
175
176 // Get the charset in lower case from the headers. If there's no charset,
177 // returns false.
178 bool GetCharset(std::string* charset) const;
179
180 // Returns true if this response corresponds to a redirect. The target
181 // location of the redirect is optionally returned if location is non-null.
182 bool IsRedirect(std::string* location) const;
183
[email protected]86de13d2009-11-24 01:21:35184 // Returns true if the HTTP response code passed in corresponds to a
185 // redirect.
186 static bool IsRedirectResponseCode(int response_code);
187
initial.commit586acc5fe2008-07-26 22:42:52188 // Returns true if the response cannot be reused without validation. The
189 // result is relative to the current_time parameter, which is a parameter to
190 // support unit testing. The request_time parameter indicates the time at
191 // which the request was made that resulted in this response, which was
192 // received at response_time.
[email protected]e1acf6f2008-10-27 20:43:33193 bool RequiresValidation(const base::Time& request_time,
194 const base::Time& response_time,
195 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52196
197 // Returns the amount of time the server claims the response is fresh from
198 // the time the response was generated. See section 13.2.4 of RFC 2616. See
199 // RequiresValidation for a description of the response_time parameter.
[email protected]e1acf6f2008-10-27 20:43:33200 base::TimeDelta GetFreshnessLifetime(const base::Time& response_time) const;
initial.commit586acc5fe2008-07-26 22:42:52201
202 // Returns the age of the response. See section 13.2.3 of RFC 2616.
203 // See RequiresValidation for a description of this method's parameters.
[email protected]e1acf6f2008-10-27 20:43:33204 base::TimeDelta GetCurrentAge(const base::Time& request_time,
205 const base::Time& response_time,
206 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52207
208 // The following methods extract values from the response headers. If a
209 // value is not present, then false is returned. Otherwise, true is returned
210 // and the out param is assigned to the corresponding value.
[email protected]e1acf6f2008-10-27 20:43:33211 bool GetMaxAgeValue(base::TimeDelta* value) const;
212 bool GetAgeValue(base::TimeDelta* value) const;
213 bool GetDateValue(base::Time* value) const;
214 bool GetLastModifiedValue(base::Time* value) const;
215 bool GetExpiresValue(base::Time* value) const;
initial.commit586acc5fe2008-07-26 22:42:52216
217 // Extracts the time value of a particular header. This method looks for the
218 // first matching header value and parses its value as a HTTP-date.
[email protected]e1acf6f2008-10-27 20:43:33219 bool GetTimeValuedHeader(const std::string& name, base::Time* result) const;
initial.commit586acc5fe2008-07-26 22:42:52220
221 // Determines if this response indicates a keep-alive connection.
222 bool IsKeepAlive() const;
223
[email protected]dbd39fb2010-01-08 01:13:36224 // Returns true if this response has a strong etag or last-modified header.
225 // See section 13.3.3 of RFC 2616.
226 bool HasStrongValidators() const;
227
initial.commit586acc5fe2008-07-26 22:42:52228 // Extracts the value of the Content-Length header or returns -1 if there is
229 // no such header in the response.
230 int64 GetContentLength() const;
231
[email protected]7eab0d2262009-10-14 22:05:54232 // Extracts the values in a Content-Range header and returns true if they are
233 // valid for a 206 response; otherwise returns false.
[email protected]d807bf92009-04-22 20:38:07234 // The following values will be outputted:
235 // |*first_byte_position| = inclusive position of the first byte of the range
236 // |*last_byte_position| = inclusive position of the last byte of the range
237 // |*instance_length| = size in bytes of the object requested
238 // If any of the above values is unknown, its value will be -1.
239 bool GetContentRange(int64* first_byte_position,
240 int64* last_byte_position,
241 int64* instance_length) const;
242
initial.commit586acc5fe2008-07-26 22:42:52243 // Returns the HTTP response code. This is 0 if the response code text seems
244 // to exist but could not be parsed. Otherwise, it defaults to 200 if the
245 // response code is not found in the raw headers.
246 int response_code() const { return response_code_; }
247
248 // Returns the raw header string.
249 const std::string& raw_headers() const { return raw_headers_; }
250
251 private:
[email protected]8a2a25f2008-08-19 23:06:05252 friend class base::RefCountedThreadSafe<HttpResponseHeaders>;
initial.commit586acc5fe2008-07-26 22:42:52253
[email protected]95792eb12009-06-22 21:30:40254 typedef base::hash_set<std::string> HeaderSet;
255
[email protected]9349cfb2010-08-31 18:00:53256 HttpResponseHeaders();
257 ~HttpResponseHeaders();
initial.commit586acc5fe2008-07-26 22:42:52258
259 // Initializes from the given raw headers.
260 void Parse(const std::string& raw_input);
261
262 // Helper function for ParseStatusLine.
263 // Tries to extract the "HTTP/X.Y" from a status line formatted like:
264 // HTTP/1.1 200 OK
265 // with line_begin and end pointing at the begin and end of this line. If the
[email protected]231d5a32008-09-13 00:45:27266 // status line is malformed, returns HttpVersion(0,0).
267 static HttpVersion ParseVersion(std::string::const_iterator line_begin,
268 std::string::const_iterator line_end);
initial.commit586acc5fe2008-07-26 22:42:52269
270 // Tries to extract the status line from a header block, given the first
[email protected]72d1e592009-03-10 17:39:46271 // line of said header block. If the status line is malformed, we'll
272 // construct a valid one. Example input:
initial.commit586acc5fe2008-07-26 22:42:52273 // HTTP/1.1 200 OK
274 // with line_begin and end pointing at the begin and end of this line.
275 // Output will be a normalized version of this, with a trailing \n.
276 void ParseStatusLine(std::string::const_iterator line_begin,
[email protected]231d5a32008-09-13 00:45:27277 std::string::const_iterator line_end,
278 bool has_headers);
initial.commit586acc5fe2008-07-26 22:42:52279
initial.commit586acc5fe2008-07-26 22:42:52280 // Find the header in our list (case-insensitive) starting with parsed_ at
281 // index |from|. Returns string::npos if not found.
282 size_t FindHeader(size_t from, const std::string& name) const;
283
[email protected]79867b592008-08-21 21:23:52284 // Add a header->value pair to our list. If we already have header in our
285 // list, append the value to it.
initial.commit586acc5fe2008-07-26 22:42:52286 void AddHeader(std::string::const_iterator name_begin,
287 std::string::const_iterator name_end,
288 std::string::const_iterator value_begin,
289 std::string::const_iterator value_end);
290
291 // Add to parsed_ given the fields of a ParsedHeader object.
292 void AddToParsed(std::string::const_iterator name_begin,
293 std::string::const_iterator name_end,
294 std::string::const_iterator value_begin,
295 std::string::const_iterator value_end);
296
[email protected]95792eb12009-06-22 21:30:40297 // Replaces the current headers with the merged version of |raw_headers| and
298 // the current headers without the headers in |headers_to_remove|. Note that
299 // |headers_to_remove| are removed from the current headers (before the
300 // merge), not after the merge.
301 void MergeWithHeaders(const std::string& raw_headers,
302 const HeaderSet& headers_to_remove);
initial.commit586acc5fe2008-07-26 22:42:52303
[email protected]cd5b9a732008-11-20 08:14:39304 // Adds the values from any 'cache-control: no-cache="foo,bar"' headers.
305 void AddNonCacheableHeaders(HeaderSet* header_names) const;
306
307 // Adds the set of header names that contain cookie values.
308 static void AddSensitiveHeaders(HeaderSet* header_names);
309
310 // Adds the set of rfc2616 hop-by-hop response headers.
311 static void AddHopByHopHeaders(HeaderSet* header_names);
312
313 // Adds the set of challenge response headers.
314 static void AddChallengeHeaders(HeaderSet* header_names);
315
316 // Adds the set of cookie response headers.
317 static void AddCookieHeaders(HeaderSet* header_names);
initial.commit586acc5fe2008-07-26 22:42:52318
[email protected]8bf26f49a2009-06-12 17:35:50319 // Adds the set of content range response headers.
320 static void AddHopContentRangeHeaders(HeaderSet* header_names);
321
initial.commit586acc5fe2008-07-26 22:42:52322 // The members of this structure point into raw_headers_.
323 struct ParsedHeader {
324 std::string::const_iterator name_begin;
325 std::string::const_iterator name_end;
326 std::string::const_iterator value_begin;
327 std::string::const_iterator value_end;
328
329 // A header "continuation" contains only a subsequent value for the
330 // preceding header. (Header values are comma separated.)
331 bool is_continuation() const { return name_begin == name_end; }
332 };
333 typedef std::vector<ParsedHeader> HeaderList;
334
335 // We keep a list of ParsedHeader objects. These tell us where to locate the
336 // header-value pairs within raw_headers_.
337 HeaderList parsed_;
338
339 // The raw_headers_ consists of the normalized status line (terminated with a
340 // null byte) and then followed by the raw null-terminated headers from the
[email protected]036d8772008-09-06 01:00:53341 // input that was passed to our constructor. We preserve the input [*] to
initial.commit586acc5fe2008-07-26 22:42:52342 // maintain as much ancillary fidelity as possible (since it is sometimes
343 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest).
[email protected]036d8772008-09-06 01:00:53344 // [*] The status line may be modified.
initial.commit586acc5fe2008-07-26 22:42:52345 std::string raw_headers_;
346
347 // This is the parsed HTTP response code.
348 int response_code_;
349
[email protected]231d5a32008-09-13 00:45:27350 // The normalized http version (consistent with what GetStatusLine() returns).
351 HttpVersion http_version_;
352
353 // The parsed http version number (not normalized).
354 HttpVersion parsed_http_version_;
355
[email protected]8a2a25f2008-08-19 23:06:05356 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders);
initial.commit586acc5fe2008-07-26 22:42:52357};
358
359} // namespace net
360
[email protected]e2a23092009-03-17 15:35:18361#endif // NET_HTTP_HTTP_RESPONSE_HEADERS_H_