blob: 1c9f518354c37c6e48d6efbf67196755bb3bfab8 [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]8a2a25f2008-08-19 23:06:055#ifndef NET_HTTP_RESPONSE_HEADERS_H_
6#define NET_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"
14
15class Pickle;
16class Time;
17class TimeDelta;
18
19namespace net {
20
21// HttpResponseHeaders: parses and holds HTTP response headers.
22class HttpResponseHeaders :
23 public base::RefCountedThreadSafe<HttpResponseHeaders> {
24 public:
25 // Parses the given raw_headers. raw_headers should be formatted thus:
26 // includes the http status response line, each line is \0-terminated, and
27 // it's terminated by an empty line (ie, 2 \0s in a row).
[email protected]036d8772008-09-06 01:00:5328 // (Note that line continuations should have already been joined;
29 // see HttpUtil::AssembleRawHeaders)
initial.commit586acc5fe2008-07-26 22:42:5230 //
31 // NOTE: For now, raw_headers is not really 'raw' in that this constructor is
32 // called with a 'NativeMB' string on Windows because WinHTTP does not allow
33 // us to access the raw byte sequence as sent by a web server. In any case,
34 // HttpResponseHeaders does not perform any encoding changes on the input.
35 //
36 explicit HttpResponseHeaders(const std::string& raw_headers);
37
38 // Initializes from the representation stored in the given pickle. The data
39 // for this object is found relative to the given pickle_iter, which should
40 // be passed to the pickle's various Read* methods.
41 HttpResponseHeaders(const Pickle& pickle, void** pickle_iter);
42
43 // Appends a representation of this object to the given pickle. If the
44 // for_cache argument is true, then non-cacheable headers will be pruned from
45 // the persisted version of the response headers.
46 void Persist(Pickle* pickle, bool for_cache);
47
48 // Performs header merging as described in 13.5.3 of RFC 2616.
49 void Update(const HttpResponseHeaders& new_headers);
50
51 // Creates a normalized header string. The output will be formatted exactly
52 // like so:
53 // HTTP/<version> <status_code> <status_text>\n
54 // [<header-name>: <header-values>\n]*
55 // meaning, each line is \n-terminated, and there is no extra whitespace
56 // beyond the single space separators shown (of course, values can contain
57 // whitespace within them). If a given header-name appears more than once
58 // in the set of headers, they are combined into a single line like so:
59 // <header-name>: <header-value1>, <header-value2>, ...<header-valueN>\n
60 //
61 // DANGER: For some headers (e.g., "Set-Cookie"), the normalized form can be
62 // a lossy format. This is due to the fact that some servers generate
63 // Set-Cookie headers that contain unquoted commas (usually as part of the
64 // value of an "expires" attribute). So, use this function with caution. Do
65 // not expect to be able to re-parse Set-Cookie headers from this output.
66 //
67 // NOTE: Do not make any assumptions about the encoding of this output
68 // string. It may be non-ASCII, and the encoding used by the server is not
69 // necessarily known to us. Do not assume that this output is UTF-8!
70 //
71 // TODO(darin): remove this method
72 //
73 void GetNormalizedHeaders(std::string* output) const;
74
75 // Fetch the "normalized" value of a single header, where all values for the
76 // header name are separated by commas. See the GetNormalizedHeaders for
77 // format details. Returns false if this header wasn't found.
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 bool GetNormalizedHeader(const std::string& name, std::string* value) const;
86
87 // Returns the normalized status line. For HTTP/0.9 responses (i.e.,
88 // responses that lack a status line), this is the manufactured string
89 // "HTTP/0.9 200 OK".
90 std::string GetStatusLine() const;
91
92 // Enumerate the "lines" of the response headers. This skips over the status
93 // line. Use GetStatusLine if you are interested in that. Note that this
94 // method returns the un-coalesced response header lines, so if a response
95 // header appears on multiple lines, then it will appear multiple times in
96 // this enumeration (in the order the header lines were received from the
97 // server). Initialize a 'void*' variable to NULL and pass it by address to
98 // EnumerateHeaderLines. Call EnumerateHeaderLines repeatedly until it
99 // returns false. The out-params 'name' and 'value' are set upon success.
100 bool EnumerateHeaderLines(void** iter,
101 std::string* name,
102 std::string* value) const;
103
104 // Enumerate the values of the specified header. If you are only interested
105 // in the first header, then you can pass NULL for the 'iter' parameter.
106 // Otherwise, to iterate across all values for the specified header,
107 // initialize a 'void*' variable to NULL and pass it by address to
108 // EnumerateHeader. Call EnumerateHeader repeatedly until it returns false.
109 bool EnumerateHeader(void** iter,
110 const std::string& name,
111 std::string* value) const;
112
113 // Returns true if the response contains the specified header-value pair.
114 // Both name and value are compared case insensitively.
115 bool HasHeaderValue(const std::string& name, const std::string& value) const;
116
117 // Get the mime type and charset values in lower case form from the headers.
118 // Empty strings are returned if the values are not present.
119 void GetMimeTypeAndCharset(std::string* mime_type,
120 std::string* charset) const;
121
122 // Get the mime type in lower case from the headers. If there's no mime
123 // type, returns false.
124 bool GetMimeType(std::string* mime_type) const;
125
126 // Get the charset in lower case from the headers. If there's no charset,
127 // returns false.
128 bool GetCharset(std::string* charset) const;
129
130 // Returns true if this response corresponds to a redirect. The target
131 // location of the redirect is optionally returned if location is non-null.
132 bool IsRedirect(std::string* location) const;
133
134 // Returns true if the response cannot be reused without validation. The
135 // result is relative to the current_time parameter, which is a parameter to
136 // support unit testing. The request_time parameter indicates the time at
137 // which the request was made that resulted in this response, which was
138 // received at response_time.
139 bool RequiresValidation(const Time& request_time,
140 const Time& response_time,
141 const Time& current_time) const;
142
143 // Returns the amount of time the server claims the response is fresh from
144 // the time the response was generated. See section 13.2.4 of RFC 2616. See
145 // RequiresValidation for a description of the response_time parameter.
146 TimeDelta GetFreshnessLifetime(const Time& response_time) const;
147
148 // Returns the age of the response. See section 13.2.3 of RFC 2616.
149 // See RequiresValidation for a description of this method's parameters.
150 TimeDelta GetCurrentAge(const Time& request_time,
151 const Time& response_time,
152 const Time& current_time) const;
153
154 // The following methods extract values from the response headers. If a
155 // value is not present, then false is returned. Otherwise, true is returned
156 // and the out param is assigned to the corresponding value.
157 bool GetMaxAgeValue(TimeDelta* value) const;
158 bool GetAgeValue(TimeDelta* value) const;
159 bool GetDateValue(Time* value) const;
160 bool GetLastModifiedValue(Time* value) const;
161 bool GetExpiresValue(Time* value) const;
162
163 // Extracts the time value of a particular header. This method looks for the
164 // first matching header value and parses its value as a HTTP-date.
165 bool GetTimeValuedHeader(const std::string& name, Time* result) const;
166
167 // Determines if this response indicates a keep-alive connection.
168 bool IsKeepAlive() const;
169
170 // Extracts the value of the Content-Length header or returns -1 if there is
171 // no such header in the response.
172 int64 GetContentLength() const;
173
174 // Returns the HTTP response code. This is 0 if the response code text seems
175 // to exist but could not be parsed. Otherwise, it defaults to 200 if the
176 // response code is not found in the raw headers.
177 int response_code() const { return response_code_; }
178
179 // Returns the raw header string.
180 const std::string& raw_headers() const { return raw_headers_; }
181
182 private:
[email protected]8a2a25f2008-08-19 23:06:05183 friend class base::RefCountedThreadSafe<HttpResponseHeaders>;
initial.commit586acc5fe2008-07-26 22:42:52184
185 HttpResponseHeaders() {}
186 ~HttpResponseHeaders() {}
187
188 // Initializes from the given raw headers.
189 void Parse(const std::string& raw_input);
190
191 // Helper function for ParseStatusLine.
192 // Tries to extract the "HTTP/X.Y" from a status line formatted like:
193 // HTTP/1.1 200 OK
194 // with line_begin and end pointing at the begin and end of this line. If the
195 // status line is malformed, we'll guess a version number.
196 // Output will be a normalized version of this, with a trailing \n.
197 void ParseVersion(std::string::const_iterator line_begin,
198 std::string::const_iterator line_end);
199
200 // Tries to extract the status line from a header block, given the first
201 // line of said header block. If the status line is malformed, we'll construct
202 // a valid one. Example input:
203 // HTTP/1.1 200 OK
204 // with line_begin and end pointing at the begin and end of this line.
205 // Output will be a normalized version of this, with a trailing \n.
206 void ParseStatusLine(std::string::const_iterator line_begin,
207 std::string::const_iterator line_end);
208
initial.commit586acc5fe2008-07-26 22:42:52209 // Find the header in our list (case-insensitive) starting with parsed_ at
210 // index |from|. Returns string::npos if not found.
211 size_t FindHeader(size_t from, const std::string& name) const;
212
[email protected]79867b592008-08-21 21:23:52213 // Add a header->value pair to our list. If we already have header in our
214 // list, append the value to it.
initial.commit586acc5fe2008-07-26 22:42:52215 void AddHeader(std::string::const_iterator name_begin,
216 std::string::const_iterator name_end,
217 std::string::const_iterator value_begin,
218 std::string::const_iterator value_end);
219
220 // Add to parsed_ given the fields of a ParsedHeader object.
221 void AddToParsed(std::string::const_iterator name_begin,
222 std::string::const_iterator name_end,
223 std::string::const_iterator value_begin,
224 std::string::const_iterator value_end);
225
[email protected]8a2a25f2008-08-19 23:06:05226 typedef base::hash_set<std::string> HeaderSet;
initial.commit586acc5fe2008-07-26 22:42:52227
228 // Returns the values from any 'cache-control: no-cache="foo,bar"' headers as
229 // well as other known-to-be-transient header names. The header names are
230 // all lowercase to support fast lookup.
231 void GetTransientHeaders(HeaderSet* header_names) const;
232
233 // The members of this structure point into raw_headers_.
234 struct ParsedHeader {
235 std::string::const_iterator name_begin;
236 std::string::const_iterator name_end;
237 std::string::const_iterator value_begin;
238 std::string::const_iterator value_end;
239
240 // A header "continuation" contains only a subsequent value for the
241 // preceding header. (Header values are comma separated.)
242 bool is_continuation() const { return name_begin == name_end; }
243 };
244 typedef std::vector<ParsedHeader> HeaderList;
245
246 // We keep a list of ParsedHeader objects. These tell us where to locate the
247 // header-value pairs within raw_headers_.
248 HeaderList parsed_;
249
250 // The raw_headers_ consists of the normalized status line (terminated with a
251 // null byte) and then followed by the raw null-terminated headers from the
[email protected]036d8772008-09-06 01:00:53252 // input that was passed to our constructor. We preserve the input [*] to
initial.commit586acc5fe2008-07-26 22:42:52253 // maintain as much ancillary fidelity as possible (since it is sometimes
254 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest).
[email protected]036d8772008-09-06 01:00:53255 // [*] The status line may be modified.
initial.commit586acc5fe2008-07-26 22:42:52256 std::string raw_headers_;
257
258 // This is the parsed HTTP response code.
259 int response_code_;
260
[email protected]8a2a25f2008-08-19 23:06:05261 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders);
initial.commit586acc5fe2008-07-26 22:42:52262};
263
264} // namespace net
265
[email protected]8a2a25f2008-08-19 23:06:05266#endif // NET_HTTP_RESPONSE_HEADERS_H_
license.botbf09a502008-08-24 00:55:55267