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