initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
[email protected] | 8a2a25f | 2008-08-19 23:06:05 | [diff] [blame] | 30 | #ifndef NET_HTTP_RESPONSE_HEADERS_H_ |
| 31 | #define NET_HTTP_RESPONSE_HEADERS_H_ |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 32 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "base/basictypes.h" |
[email protected] | 8a2a25f | 2008-08-19 23:06:05 | [diff] [blame] | 37 | #include "base/hash_tables.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "base/ref_counted.h" |
| 39 | |
| 40 | class Pickle; |
| 41 | class Time; |
| 42 | class TimeDelta; |
| 43 | |
| 44 | namespace net { |
| 45 | |
| 46 | // HttpResponseHeaders: parses and holds HTTP response headers. |
| 47 | class HttpResponseHeaders : |
| 48 | public base::RefCountedThreadSafe<HttpResponseHeaders> { |
| 49 | public: |
| 50 | // Parses the given raw_headers. raw_headers should be formatted thus: |
| 51 | // includes the http status response line, each line is \0-terminated, and |
| 52 | // it's terminated by an empty line (ie, 2 \0s in a row). |
| 53 | // |
| 54 | // NOTE: For now, raw_headers is not really 'raw' in that this constructor is |
| 55 | // called with a 'NativeMB' string on Windows because WinHTTP does not allow |
| 56 | // us to access the raw byte sequence as sent by a web server. In any case, |
| 57 | // HttpResponseHeaders does not perform any encoding changes on the input. |
| 58 | // |
| 59 | explicit HttpResponseHeaders(const std::string& raw_headers); |
| 60 | |
| 61 | // Initializes from the representation stored in the given pickle. The data |
| 62 | // for this object is found relative to the given pickle_iter, which should |
| 63 | // be passed to the pickle's various Read* methods. |
| 64 | HttpResponseHeaders(const Pickle& pickle, void** pickle_iter); |
| 65 | |
| 66 | // Appends a representation of this object to the given pickle. If the |
| 67 | // for_cache argument is true, then non-cacheable headers will be pruned from |
| 68 | // the persisted version of the response headers. |
| 69 | void Persist(Pickle* pickle, bool for_cache); |
| 70 | |
| 71 | // Performs header merging as described in 13.5.3 of RFC 2616. |
| 72 | void Update(const HttpResponseHeaders& new_headers); |
| 73 | |
| 74 | // Creates a normalized header string. The output will be formatted exactly |
| 75 | // like so: |
| 76 | // HTTP/<version> <status_code> <status_text>\n |
| 77 | // [<header-name>: <header-values>\n]* |
| 78 | // meaning, each line is \n-terminated, and there is no extra whitespace |
| 79 | // beyond the single space separators shown (of course, values can contain |
| 80 | // whitespace within them). If a given header-name appears more than once |
| 81 | // in the set of headers, they are combined into a single line like so: |
| 82 | // <header-name>: <header-value1>, <header-value2>, ...<header-valueN>\n |
| 83 | // |
| 84 | // DANGER: For some headers (e.g., "Set-Cookie"), the normalized form can be |
| 85 | // a lossy format. This is due to the fact that some servers generate |
| 86 | // Set-Cookie headers that contain unquoted commas (usually as part of the |
| 87 | // value of an "expires" attribute). So, use this function with caution. Do |
| 88 | // not expect to be able to re-parse Set-Cookie headers from this output. |
| 89 | // |
| 90 | // NOTE: Do not make any assumptions about the encoding of this output |
| 91 | // string. It may be non-ASCII, and the encoding used by the server is not |
| 92 | // necessarily known to us. Do not assume that this output is UTF-8! |
| 93 | // |
| 94 | // TODO(darin): remove this method |
| 95 | // |
| 96 | void GetNormalizedHeaders(std::string* output) const; |
| 97 | |
| 98 | // Fetch the "normalized" value of a single header, where all values for the |
| 99 | // header name are separated by commas. See the GetNormalizedHeaders for |
| 100 | // format details. Returns false if this header wasn't found. |
| 101 | // |
| 102 | // NOTE: Do not make any assumptions about the encoding of this output |
| 103 | // string. It may be non-ASCII, and the encoding used by the server is not |
| 104 | // necessarily known to us. Do not assume that this output is UTF-8! |
| 105 | // |
| 106 | // TODO(darin): remove this method |
| 107 | // |
| 108 | bool GetNormalizedHeader(const std::string& name, std::string* value) const; |
| 109 | |
| 110 | // Returns the normalized status line. For HTTP/0.9 responses (i.e., |
| 111 | // responses that lack a status line), this is the manufactured string |
| 112 | // "HTTP/0.9 200 OK". |
| 113 | std::string GetStatusLine() const; |
| 114 | |
| 115 | // Enumerate the "lines" of the response headers. This skips over the status |
| 116 | // line. Use GetStatusLine if you are interested in that. Note that this |
| 117 | // method returns the un-coalesced response header lines, so if a response |
| 118 | // header appears on multiple lines, then it will appear multiple times in |
| 119 | // this enumeration (in the order the header lines were received from the |
| 120 | // server). Initialize a 'void*' variable to NULL and pass it by address to |
| 121 | // EnumerateHeaderLines. Call EnumerateHeaderLines repeatedly until it |
| 122 | // returns false. The out-params 'name' and 'value' are set upon success. |
| 123 | bool EnumerateHeaderLines(void** iter, |
| 124 | std::string* name, |
| 125 | std::string* value) const; |
| 126 | |
| 127 | // Enumerate the values of the specified header. If you are only interested |
| 128 | // in the first header, then you can pass NULL for the 'iter' parameter. |
| 129 | // Otherwise, to iterate across all values for the specified header, |
| 130 | // initialize a 'void*' variable to NULL and pass it by address to |
| 131 | // EnumerateHeader. Call EnumerateHeader repeatedly until it returns false. |
| 132 | bool EnumerateHeader(void** iter, |
| 133 | const std::string& name, |
| 134 | std::string* value) const; |
| 135 | |
| 136 | // Returns true if the response contains the specified header-value pair. |
| 137 | // Both name and value are compared case insensitively. |
| 138 | bool HasHeaderValue(const std::string& name, const std::string& value) const; |
| 139 | |
| 140 | // Get the mime type and charset values in lower case form from the headers. |
| 141 | // Empty strings are returned if the values are not present. |
| 142 | void GetMimeTypeAndCharset(std::string* mime_type, |
| 143 | std::string* charset) const; |
| 144 | |
| 145 | // Get the mime type in lower case from the headers. If there's no mime |
| 146 | // type, returns false. |
| 147 | bool GetMimeType(std::string* mime_type) const; |
| 148 | |
| 149 | // Get the charset in lower case from the headers. If there's no charset, |
| 150 | // returns false. |
| 151 | bool GetCharset(std::string* charset) const; |
| 152 | |
| 153 | // Returns true if this response corresponds to a redirect. The target |
| 154 | // location of the redirect is optionally returned if location is non-null. |
| 155 | bool IsRedirect(std::string* location) const; |
| 156 | |
| 157 | // Returns true if the response cannot be reused without validation. The |
| 158 | // result is relative to the current_time parameter, which is a parameter to |
| 159 | // support unit testing. The request_time parameter indicates the time at |
| 160 | // which the request was made that resulted in this response, which was |
| 161 | // received at response_time. |
| 162 | bool RequiresValidation(const Time& request_time, |
| 163 | const Time& response_time, |
| 164 | const Time& current_time) const; |
| 165 | |
| 166 | // Returns the amount of time the server claims the response is fresh from |
| 167 | // the time the response was generated. See section 13.2.4 of RFC 2616. See |
| 168 | // RequiresValidation for a description of the response_time parameter. |
| 169 | TimeDelta GetFreshnessLifetime(const Time& response_time) const; |
| 170 | |
| 171 | // Returns the age of the response. See section 13.2.3 of RFC 2616. |
| 172 | // See RequiresValidation for a description of this method's parameters. |
| 173 | TimeDelta GetCurrentAge(const Time& request_time, |
| 174 | const Time& response_time, |
| 175 | const Time& current_time) const; |
| 176 | |
| 177 | // The following methods extract values from the response headers. If a |
| 178 | // value is not present, then false is returned. Otherwise, true is returned |
| 179 | // and the out param is assigned to the corresponding value. |
| 180 | bool GetMaxAgeValue(TimeDelta* value) const; |
| 181 | bool GetAgeValue(TimeDelta* value) const; |
| 182 | bool GetDateValue(Time* value) const; |
| 183 | bool GetLastModifiedValue(Time* value) const; |
| 184 | bool GetExpiresValue(Time* value) const; |
| 185 | |
| 186 | // Extracts the time value of a particular header. This method looks for the |
| 187 | // first matching header value and parses its value as a HTTP-date. |
| 188 | bool GetTimeValuedHeader(const std::string& name, Time* result) const; |
| 189 | |
| 190 | // Determines if this response indicates a keep-alive connection. |
| 191 | bool IsKeepAlive() const; |
| 192 | |
| 193 | // Extracts the value of the Content-Length header or returns -1 if there is |
| 194 | // no such header in the response. |
| 195 | int64 GetContentLength() const; |
| 196 | |
| 197 | // Returns the HTTP response code. This is 0 if the response code text seems |
| 198 | // to exist but could not be parsed. Otherwise, it defaults to 200 if the |
| 199 | // response code is not found in the raw headers. |
| 200 | int response_code() const { return response_code_; } |
| 201 | |
| 202 | // Returns the raw header string. |
| 203 | const std::string& raw_headers() const { return raw_headers_; } |
| 204 | |
| 205 | private: |
[email protected] | 8a2a25f | 2008-08-19 23:06:05 | [diff] [blame] | 206 | friend class base::RefCountedThreadSafe<HttpResponseHeaders>; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 207 | |
| 208 | HttpResponseHeaders() {} |
| 209 | ~HttpResponseHeaders() {} |
| 210 | |
| 211 | // Initializes from the given raw headers. |
| 212 | void Parse(const std::string& raw_input); |
| 213 | |
| 214 | // Helper function for ParseStatusLine. |
| 215 | // Tries to extract the "HTTP/X.Y" from a status line formatted like: |
| 216 | // HTTP/1.1 200 OK |
| 217 | // with line_begin and end pointing at the begin and end of this line. If the |
| 218 | // status line is malformed, we'll guess a version number. |
| 219 | // Output will be a normalized version of this, with a trailing \n. |
| 220 | void ParseVersion(std::string::const_iterator line_begin, |
| 221 | std::string::const_iterator line_end); |
| 222 | |
| 223 | // Tries to extract the status line from a header block, given the first |
| 224 | // line of said header block. If the status line is malformed, we'll construct |
| 225 | // a valid one. Example input: |
| 226 | // HTTP/1.1 200 OK |
| 227 | // with line_begin and end pointing at the begin and end of this line. |
| 228 | // Output will be a normalized version of this, with a trailing \n. |
| 229 | void ParseStatusLine(std::string::const_iterator line_begin, |
| 230 | std::string::const_iterator line_end); |
| 231 | |
| 232 | // Tries to extract the header line from a header block, given a single |
| 233 | // line of said header block. If the header is malformed, we skip it. |
| 234 | // Example input: |
| 235 | // Content-Length : text/html; charset=utf-8 |
| 236 | void ParseHeaderLine(std::string::const_iterator line_begin, |
| 237 | std::string::const_iterator line_end); |
| 238 | |
| 239 | // Find the header in our list (case-insensitive) starting with parsed_ at |
| 240 | // index |from|. Returns string::npos if not found. |
| 241 | size_t FindHeader(size_t from, const std::string& name) const; |
| 242 | |
[email protected] | 79867b59 | 2008-08-21 21:23:52 | [diff] [blame^] | 243 | // Add a header->value pair to our list. If we already have header in our |
| 244 | // list, append the value to it. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 245 | void AddHeader(std::string::const_iterator name_begin, |
| 246 | std::string::const_iterator name_end, |
| 247 | std::string::const_iterator value_begin, |
| 248 | std::string::const_iterator value_end); |
| 249 | |
| 250 | // Add to parsed_ given the fields of a ParsedHeader object. |
| 251 | void AddToParsed(std::string::const_iterator name_begin, |
| 252 | std::string::const_iterator name_end, |
| 253 | std::string::const_iterator value_begin, |
| 254 | std::string::const_iterator value_end); |
| 255 | |
[email protected] | 8a2a25f | 2008-08-19 23:06:05 | [diff] [blame] | 256 | typedef base::hash_set<std::string> HeaderSet; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 257 | |
| 258 | // Returns the values from any 'cache-control: no-cache="foo,bar"' headers as |
| 259 | // well as other known-to-be-transient header names. The header names are |
| 260 | // all lowercase to support fast lookup. |
| 261 | void GetTransientHeaders(HeaderSet* header_names) const; |
| 262 | |
| 263 | // The members of this structure point into raw_headers_. |
| 264 | struct ParsedHeader { |
| 265 | std::string::const_iterator name_begin; |
| 266 | std::string::const_iterator name_end; |
| 267 | std::string::const_iterator value_begin; |
| 268 | std::string::const_iterator value_end; |
| 269 | |
| 270 | // A header "continuation" contains only a subsequent value for the |
| 271 | // preceding header. (Header values are comma separated.) |
| 272 | bool is_continuation() const { return name_begin == name_end; } |
| 273 | }; |
| 274 | typedef std::vector<ParsedHeader> HeaderList; |
| 275 | |
| 276 | // We keep a list of ParsedHeader objects. These tell us where to locate the |
| 277 | // header-value pairs within raw_headers_. |
| 278 | HeaderList parsed_; |
| 279 | |
| 280 | // The raw_headers_ consists of the normalized status line (terminated with a |
| 281 | // null byte) and then followed by the raw null-terminated headers from the |
| 282 | // input that was passed to our constructor. We preserve the input to |
| 283 | // maintain as much ancillary fidelity as possible (since it is sometimes |
| 284 | // hard to tell what may matter down-stream to a consumer of XMLHttpRequest). |
| 285 | std::string raw_headers_; |
| 286 | |
| 287 | // This is the parsed HTTP response code. |
| 288 | int response_code_; |
| 289 | |
[email protected] | 8a2a25f | 2008-08-19 23:06:05 | [diff] [blame] | 290 | DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | } // namespace net |
| 294 | |
[email protected] | 8a2a25f | 2008-08-19 23:06:05 | [diff] [blame] | 295 | #endif // NET_HTTP_RESPONSE_HEADERS_H_ |