blob: 547370d90f1ccde607a0dad1b917a9f910fc96c5 [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.
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;
[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
174 // Returns true if the response cannot be reused without validation. The
175 // result is relative to the current_time parameter, which is a parameter to
176 // support unit testing. The request_time parameter indicates the time at
177 // which the request was made that resulted in this response, which was
178 // received at response_time.
[email protected]e1acf6f2008-10-27 20:43:33179 bool RequiresValidation(const base::Time& request_time,
180 const base::Time& response_time,
181 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52182
183 // Returns the amount of time the server claims the response is fresh from
184 // the time the response was generated. See section 13.2.4 of RFC 2616. See
185 // RequiresValidation for a description of the response_time parameter.
[email protected]e1acf6f2008-10-27 20:43:33186 base::TimeDelta GetFreshnessLifetime(const base::Time& response_time) const;
initial.commit586acc5fe2008-07-26 22:42:52187
188 // Returns the age of the response. See section 13.2.3 of RFC 2616.
189 // See RequiresValidation for a description of this method's parameters.
[email protected]e1acf6f2008-10-27 20:43:33190 base::TimeDelta GetCurrentAge(const base::Time& request_time,
191 const base::Time& response_time,
192 const base::Time& current_time) const;
initial.commit586acc5fe2008-07-26 22:42:52193
194 // The following methods extract values from the response headers. If a
195 // value is not present, then false is returned. Otherwise, true is returned
196 // and the out param is assigned to the corresponding value.
[email protected]e1acf6f2008-10-27 20:43:33197 bool GetMaxAgeValue(base::TimeDelta* value) const;
198 bool GetAgeValue(base::TimeDelta* value) const;
199 bool GetDateValue(base::Time* value) const;
200 bool GetLastModifiedValue(base::Time* value) const;
201 bool GetExpiresValue(base::Time* value) const;
initial.commit586acc5fe2008-07-26 22:42:52202
203 // Extracts the time value of a particular header. This method looks for the
204 // first matching header value and parses its value as a HTTP-date.
[email protected]e1acf6f2008-10-27 20:43:33205 bool GetTimeValuedHeader(const std::string& name, base::Time* result) const;
initial.commit586acc5fe2008-07-26 22:42:52206
207 // Determines if this response indicates a keep-alive connection.
208 bool IsKeepAlive() const;
209
210 // Extracts the value of the Content-Length header or returns -1 if there is
211 // no such header in the response.
212 int64 GetContentLength() const;
213
[email protected]d807bf92009-04-22 20:38:07214 // Extracts the values in Content-Range header, if the header exists and is
215 // well formatted returns true, else returns false.
216 // The following values will be outputted:
217 // |*first_byte_position| = inclusive position of the first byte of the range
218 // |*last_byte_position| = inclusive position of the last byte of the range
219 // |*instance_length| = size in bytes of the object requested
220 // If any of the above values is unknown, its value will be -1.
221 bool GetContentRange(int64* first_byte_position,
222 int64* last_byte_position,
223 int64* instance_length) const;
224
initial.commit586acc5fe2008-07-26 22:42:52225 // Returns the HTTP response code. This is 0 if the response code text seems
226 // to exist but could not be parsed. Otherwise, it defaults to 200 if the
227 // response code is not found in the raw headers.
228 int response_code() const { return response_code_; }
229
230 // Returns the raw header string.
231 const std::string& raw_headers() const { return raw_headers_; }
232
233 private:
[email protected]8a2a25f2008-08-19 23:06:05234 friend class base::RefCountedThreadSafe<HttpResponseHeaders>;
initial.commit586acc5fe2008-07-26 22:42:52235
[email protected]95792eb12009-06-22 21:30:40236 typedef base::hash_set<std::string> HeaderSet;
237
initial.commit586acc5fe2008-07-26 22:42:52238 HttpResponseHeaders() {}
239 ~HttpResponseHeaders() {}
240
241 // Initializes from the given raw headers.
242 void Parse(const std::string& raw_input);
243
244 // Helper function for ParseStatusLine.
245 // Tries to extract the "HTTP/X.Y" from a status line formatted like:
246 // HTTP/1.1 200 OK
247 // with line_begin and end pointing at the begin and end of this line. If the
[email protected]231d5a32008-09-13 00:45:27248 // status line is malformed, returns HttpVersion(0,0).
249 static HttpVersion ParseVersion(std::string::const_iterator line_begin,
250 std::string::const_iterator line_end);
initial.commit586acc5fe2008-07-26 22:42:52251
252 // Tries to extract the status line from a header block, given the first
[email protected]72d1e592009-03-10 17:39:46253 // line of said header block. If the status line is malformed, we'll
254 // construct a valid one. Example input:
initial.commit586acc5fe2008-07-26 22:42:52255 // HTTP/1.1 200 OK
256 // with line_begin and end pointing at the begin and end of this line.
257 // Output will be a normalized version of this, with a trailing \n.
258 void ParseStatusLine(std::string::const_iterator line_begin,
[email protected]231d5a32008-09-13 00:45:27259 std::string::const_iterator line_end,
260 bool has_headers);
initial.commit586acc5fe2008-07-26 22:42:52261
initial.commit586acc5fe2008-07-26 22:42:52262 // Find the header in our list (case-insensitive) starting with parsed_ at
263 // index |from|. Returns string::npos if not found.
264 size_t FindHeader(size_t from, const std::string& name) const;
265
[email protected]79867b592008-08-21 21:23:52266 // Add a header->value pair to our list. If we already have header in our
267 // list, append the value to it.
initial.commit586acc5fe2008-07-26 22:42:52268 void AddHeader(std::string::const_iterator name_begin,
269 std::string::const_iterator name_end,
270 std::string::const_iterator value_begin,
271 std::string::const_iterator value_end);
272
273 // Add to parsed_ given the fields of a ParsedHeader object.
274 void AddToParsed(std::string::const_iterator name_begin,
275 std::string::const_iterator name_end,
276 std::string::const_iterator value_begin,
277 std::string::const_iterator value_end);
278
[email protected]95792eb12009-06-22 21:30:40279 // Replaces the current headers with the merged version of |raw_headers| and
280 // the current headers without the headers in |headers_to_remove|. Note that
281 // |headers_to_remove| are removed from the current headers (before the
282 // merge), not after the merge.
283 void MergeWithHeaders(const std::string& raw_headers,
284 const HeaderSet& headers_to_remove);
initial.commit586acc5fe2008-07-26 22:42:52285
[email protected]cd5b9a732008-11-20 08:14:39286 // Adds the values from any 'cache-control: no-cache="foo,bar"' headers.
287 void AddNonCacheableHeaders(HeaderSet* header_names) const;
288
289 // Adds the set of header names that contain cookie values.
290 static void AddSensitiveHeaders(HeaderSet* header_names);
291
292 // Adds the set of rfc2616 hop-by-hop response headers.
293 static void AddHopByHopHeaders(HeaderSet* header_names);
294
295 // Adds the set of challenge response headers.
296 static void AddChallengeHeaders(HeaderSet* header_names);
297
298 // Adds the set of cookie response headers.
299 static void AddCookieHeaders(HeaderSet* header_names);
initial.commit586acc5fe2008-07-26 22:42:52300
[email protected]8bf26f49a2009-06-12 17:35:50301 // Adds the set of content range response headers.
302 static void AddHopContentRangeHeaders(HeaderSet* header_names);
303
initial.commit586acc5fe2008-07-26 22:42:52304 // The members of this structure point into raw_headers_.
305 struct ParsedHeader {
306 std::string::const_iterator name_begin;
307 std::string::const_iterator name_end;
308 std::string::const_iterator value_begin;
309 std::string::const_iterator value_end;
310
311 // A header "continuation" contains only a subsequent value for the
312 // preceding header. (Header values are comma separated.)
313 bool is_continuation() const { return name_begin == name_end; }
314 };
315 typedef std::vector<ParsedHeader> HeaderList;
316
317 // We keep a list of ParsedHeader objects. These tell us where to locate the
318 // header-value pairs within raw_headers_.
319 HeaderList parsed_;
320
321 // The raw_headers_ consists of the normalized status line (terminated with a
322 // null byte) and then followed by the raw null-terminated headers from the
[email protected]036d8772008-09-06 01:00:53323 // input that was passed to our constructor. We preserve the input [*] to
initial.commit586acc5fe2008-07-26 22:42:52324 // maintain as much ancillary fidelity as possible (since it is sometimes
325 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest).
[email protected]036d8772008-09-06 01:00:53326 // [*] The status line may be modified.
initial.commit586acc5fe2008-07-26 22:42:52327 std::string raw_headers_;
328
329 // This is the parsed HTTP response code.
330 int response_code_;
331
[email protected]231d5a32008-09-13 00:45:27332 // The normalized http version (consistent with what GetStatusLine() returns).
333 HttpVersion http_version_;
334
335 // The parsed http version number (not normalized).
336 HttpVersion parsed_http_version_;
337
[email protected]8a2a25f2008-08-19 23:06:05338 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders);
initial.commit586acc5fe2008-07-26 22:42:52339};
340
341} // namespace net
342
[email protected]e2a23092009-03-17 15:35:18343#endif // NET_HTTP_HTTP_RESPONSE_HEADERS_H_