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