OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_HTTP_HTTP_RESPONSE_HEADERS_H_ | 5 #ifndef NET_HTTP_HTTP_RESPONSE_HEADERS_H_ |
6 #define NET_HTTP_HTTP_RESPONSE_HEADERS_H_ | 6 #define NET_HTTP_HTTP_RESPONSE_HEADERS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/hash_tables.h" | 13 #include "base/hash_tables.h" |
14 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
15 #include "net/http/http_version.h" | 15 #include "net/http/http_version.h" |
16 | 16 |
17 class Pickle; | 17 class Pickle; |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class Time; | 20 class Time; |
21 class TimeDelta; | 21 class TimeDelta; |
22 } | 22 } |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 // HttpResponseHeaders: parses and holds HTTP response headers. | 26 // HttpResponseHeaders: parses and holds HTTP response headers. |
27 class HttpResponseHeaders | 27 class HttpResponseHeaders |
28 : public base::RefCountedThreadSafe<HttpResponseHeaders> { | 28 : public base::RefCountedThreadSafe<HttpResponseHeaders> { |
29 public: | 29 public: |
| 30 // Persist options. |
| 31 typedef int PersistOptions; |
| 32 static const PersistOptions PERSIST_RAW = -1; // Raw, unparsed headers. |
| 33 static const PersistOptions PERSIST_ALL = 0; // Parsed headers. |
| 34 static const PersistOptions PERSIST_SANS_COOKIES = 1 << 0; |
| 35 static const PersistOptions PERSIST_SANS_CHALLENGES = 1 << 1; |
| 36 static const PersistOptions PERSIST_SANS_HOP_BY_HOP = 1 << 2; |
| 37 static const PersistOptions PERSIST_SANS_NON_CACHEABLE = 1 << 3; |
| 38 static const PersistOptions PERSIST_SANS_RANGES = 1 << 4; |
| 39 |
30 // Parses the given raw_headers. raw_headers should be formatted thus: | 40 // Parses the given raw_headers. raw_headers should be formatted thus: |
31 // includes the http status response line, each line is \0-terminated, and | 41 // includes the http status response line, each line is \0-terminated, and |
32 // it's terminated by an empty line (ie, 2 \0s in a row). | 42 // it's terminated by an empty line (ie, 2 \0s in a row). |
33 // (Note that line continuations should have already been joined; | 43 // (Note that line continuations should have already been joined; |
34 // see HttpUtil::AssembleRawHeaders) | 44 // see HttpUtil::AssembleRawHeaders) |
35 // | 45 // |
36 // NOTE: For now, raw_headers is not really 'raw' in that this constructor is | 46 // NOTE: For now, raw_headers is not really 'raw' in that this constructor is |
37 // called with a 'NativeMB' string on Windows because WinHTTP does not allow | 47 // called with a 'NativeMB' string on Windows because WinHTTP does not allow |
38 // us to access the raw byte sequence as sent by a web server. In any case, | 48 // us to access the raw byte sequence as sent by a web server. In any case, |
39 // HttpResponseHeaders does not perform any encoding changes on the input. | 49 // HttpResponseHeaders does not perform any encoding changes on the input. |
40 // | 50 // |
41 explicit HttpResponseHeaders(const std::string& raw_headers); | 51 explicit HttpResponseHeaders(const std::string& raw_headers); |
42 | 52 |
43 // Initializes from the representation stored in the given pickle. The data | 53 // Initializes from the representation stored in the given pickle. The data |
44 // for this object is found relative to the given pickle_iter, which should | 54 // for this object is found relative to the given pickle_iter, which should |
45 // be passed to the pickle's various Read* methods. | 55 // be passed to the pickle's various Read* methods. |
46 HttpResponseHeaders(const Pickle& pickle, void** pickle_iter); | 56 HttpResponseHeaders(const Pickle& pickle, void** pickle_iter); |
47 | 57 |
48 // Persist options. | |
49 typedef int PersistOptions; | |
50 static const PersistOptions PERSIST_RAW = -1; // Raw, unparsed headers. | |
51 static const PersistOptions PERSIST_ALL = 0; // Parsed headers. | |
52 static const PersistOptions PERSIST_SANS_COOKIES = 1 << 0; | |
53 static const PersistOptions PERSIST_SANS_CHALLENGES = 1 << 1; | |
54 static const PersistOptions PERSIST_SANS_HOP_BY_HOP = 1 << 2; | |
55 static const PersistOptions PERSIST_SANS_NON_CACHEABLE = 1 << 3; | |
56 static const PersistOptions PERSIST_SANS_RANGES = 1 << 4; | |
57 | |
58 // Appends a representation of this object to the given pickle. | 58 // Appends a representation of this object to the given pickle. |
59 // The options argument can be a combination of PersistOptions. | 59 // The options argument can be a combination of PersistOptions. |
60 void Persist(Pickle* pickle, PersistOptions options); | 60 void Persist(Pickle* pickle, PersistOptions options); |
61 | 61 |
62 // Performs header merging as described in 13.5.3 of RFC 2616. | 62 // Performs header merging as described in 13.5.3 of RFC 2616. |
63 void Update(const HttpResponseHeaders& new_headers); | 63 void Update(const HttpResponseHeaders& new_headers); |
64 | 64 |
65 // Removes all instances of a particular header. | 65 // Removes all instances of a particular header. |
66 void RemoveHeader(const std::string& name); | 66 void RemoveHeader(const std::string& name); |
67 | 67 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 int response_code() const { return response_code_; } | 246 int response_code() const { return response_code_; } |
247 | 247 |
248 // Returns the raw header string. | 248 // Returns the raw header string. |
249 const std::string& raw_headers() const { return raw_headers_; } | 249 const std::string& raw_headers() const { return raw_headers_; } |
250 | 250 |
251 private: | 251 private: |
252 friend class base::RefCountedThreadSafe<HttpResponseHeaders>; | 252 friend class base::RefCountedThreadSafe<HttpResponseHeaders>; |
253 | 253 |
254 typedef base::hash_set<std::string> HeaderSet; | 254 typedef base::hash_set<std::string> HeaderSet; |
255 | 255 |
| 256 // The members of this structure point into raw_headers_. |
| 257 struct ParsedHeader { |
| 258 std::string::const_iterator name_begin; |
| 259 std::string::const_iterator name_end; |
| 260 std::string::const_iterator value_begin; |
| 261 std::string::const_iterator value_end; |
| 262 |
| 263 // A header "continuation" contains only a subsequent value for the |
| 264 // preceding header. (Header values are comma separated.) |
| 265 bool is_continuation() const { return name_begin == name_end; } |
| 266 }; |
| 267 typedef std::vector<ParsedHeader> HeaderList; |
| 268 |
256 HttpResponseHeaders(); | 269 HttpResponseHeaders(); |
257 ~HttpResponseHeaders(); | 270 ~HttpResponseHeaders(); |
258 | 271 |
259 // Initializes from the given raw headers. | 272 // Initializes from the given raw headers. |
260 void Parse(const std::string& raw_input); | 273 void Parse(const std::string& raw_input); |
261 | 274 |
262 // Helper function for ParseStatusLine. | 275 // Helper function for ParseStatusLine. |
263 // Tries to extract the "HTTP/X.Y" from a status line formatted like: | 276 // Tries to extract the "HTTP/X.Y" from a status line formatted like: |
264 // HTTP/1.1 200 OK | 277 // HTTP/1.1 200 OK |
265 // with line_begin and end pointing at the begin and end of this line. If the | 278 // with line_begin and end pointing at the begin and end of this line. If the |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 | 325 |
313 // Adds the set of challenge response headers. | 326 // Adds the set of challenge response headers. |
314 static void AddChallengeHeaders(HeaderSet* header_names); | 327 static void AddChallengeHeaders(HeaderSet* header_names); |
315 | 328 |
316 // Adds the set of cookie response headers. | 329 // Adds the set of cookie response headers. |
317 static void AddCookieHeaders(HeaderSet* header_names); | 330 static void AddCookieHeaders(HeaderSet* header_names); |
318 | 331 |
319 // Adds the set of content range response headers. | 332 // Adds the set of content range response headers. |
320 static void AddHopContentRangeHeaders(HeaderSet* header_names); | 333 static void AddHopContentRangeHeaders(HeaderSet* header_names); |
321 | 334 |
322 // The members of this structure point into raw_headers_. | |
323 struct ParsedHeader { | |
324 std::string::const_iterator name_begin; | |
325 std::string::const_iterator name_end; | |
326 std::string::const_iterator value_begin; | |
327 std::string::const_iterator value_end; | |
328 | |
329 // A header "continuation" contains only a subsequent value for the | |
330 // preceding header. (Header values are comma separated.) | |
331 bool is_continuation() const { return name_begin == name_end; } | |
332 }; | |
333 typedef std::vector<ParsedHeader> HeaderList; | |
334 | |
335 // We keep a list of ParsedHeader objects. These tell us where to locate the | 335 // We keep a list of ParsedHeader objects. These tell us where to locate the |
336 // header-value pairs within raw_headers_. | 336 // header-value pairs within raw_headers_. |
337 HeaderList parsed_; | 337 HeaderList parsed_; |
338 | 338 |
339 // The raw_headers_ consists of the normalized status line (terminated with a | 339 // The raw_headers_ consists of the normalized status line (terminated with a |
340 // null byte) and then followed by the raw null-terminated headers from the | 340 // null byte) and then followed by the raw null-terminated headers from the |
341 // input that was passed to our constructor. We preserve the input [*] to | 341 // input that was passed to our constructor. We preserve the input [*] to |
342 // maintain as much ancillary fidelity as possible (since it is sometimes | 342 // maintain as much ancillary fidelity as possible (since it is sometimes |
343 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest). | 343 // hard to tell what may matter down-stream to a consumer of XMLHttpRequest). |
344 // [*] The status line may be modified. | 344 // [*] The status line may be modified. |
345 std::string raw_headers_; | 345 std::string raw_headers_; |
346 | 346 |
347 // This is the parsed HTTP response code. | 347 // This is the parsed HTTP response code. |
348 int response_code_; | 348 int response_code_; |
349 | 349 |
350 // The normalized http version (consistent with what GetStatusLine() returns). | 350 // The normalized http version (consistent with what GetStatusLine() returns). |
351 HttpVersion http_version_; | 351 HttpVersion http_version_; |
352 | 352 |
353 // The parsed http version number (not normalized). | 353 // The parsed http version number (not normalized). |
354 HttpVersion parsed_http_version_; | 354 HttpVersion parsed_http_version_; |
355 | 355 |
356 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders); | 356 DISALLOW_COPY_AND_ASSIGN(HttpResponseHeaders); |
357 }; | 357 }; |
358 | 358 |
359 } // namespace net | 359 } // namespace net |
360 | 360 |
361 #endif // NET_HTTP_HTTP_RESPONSE_HEADERS_H_ | 361 #endif // NET_HTTP_HTTP_RESPONSE_HEADERS_H_ |
OLD | NEW |