OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // 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 | 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 // The rules for header parsing were borrowed from Firefox: | 5 // The rules for header parsing were borrowed from Firefox: |
6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo
nseHead.cpp | 6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo
nseHead.cpp |
7 // The rules for parsing content-types were also borrowed from Firefox: | 7 // The rules for parsing content-types were also borrowed from Firefox: |
8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 | 8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 |
9 | 9 |
10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/pickle.h" | 15 #include "base/pickle.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
19 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
20 | 20 |
21 using base::Time; | 21 using base::Time; |
22 using base::TimeDelta; | 22 using base::TimeDelta; |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 //----------------------------------------------------------------------------- | 26 //----------------------------------------------------------------------------- |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // These response headers are not persisted in a cached representation of the | 30 // These headers are RFC 2616 hop-by-hop headers; |
31 // response headers. (This list comes from Mozilla's nsHttpResponseHead.cpp) | 31 // not to be stored by caches. |
32 const char* const kTransientHeaders[] = { | 32 const char* const kHopByHopResponseHeaders[] = { |
33 "connection", | 33 "connection", |
34 "proxy-connection", | 34 "proxy-connection", |
35 "keep-alive", | 35 "keep-alive", |
36 "www-authenticate", | |
37 "proxy-authenticate", | |
38 "trailer", | 36 "trailer", |
39 "transfer-encoding", | 37 "transfer-encoding", |
40 "upgrade", | 38 "upgrade" |
| 39 }; |
| 40 |
| 41 // These headers are challenge response headers; |
| 42 // not to be stored by caches. |
| 43 const char* const kChallengeResponseHeaders[] = { |
| 44 "www-authenticate", |
| 45 "proxy-authenticate" |
| 46 }; |
| 47 |
| 48 // These headers are cookie setting headers; |
| 49 // not to be stored by caches or disclosed otherwise. |
| 50 const char* const kCookieResponseHeaders[] = { |
41 "set-cookie", | 51 "set-cookie", |
42 "set-cookie2" | 52 "set-cookie2" |
43 }; | 53 }; |
44 | 54 |
45 // These respones headers are not copied from a 304/206 response to the cached | 55 // These response headers are not copied from a 304/206 response to the cached |
46 // response headers. This list is based on Mozilla's nsHttpResponseHead.cpp. | 56 // response headers. This list is based on Mozilla's nsHttpResponseHead.cpp. |
47 const char* const kNonUpdatedHeaders[] = { | 57 const char* const kNonUpdatedHeaders[] = { |
48 "connection", | 58 "connection", |
49 "proxy-connection", | 59 "proxy-connection", |
50 "keep-alive", | 60 "keep-alive", |
51 "www-authenticate", | 61 "www-authenticate", |
52 "proxy-authenticate", | 62 "proxy-authenticate", |
53 "trailer", | 63 "trailer", |
54 "transfer-encoding", | 64 "transfer-encoding", |
55 "upgrade", | 65 "upgrade", |
(...skipping 27 matching lines...) Expand all Loading... |
83 Parse(raw_input); | 93 Parse(raw_input); |
84 } | 94 } |
85 | 95 |
86 HttpResponseHeaders::HttpResponseHeaders(const Pickle& pickle, void** iter) | 96 HttpResponseHeaders::HttpResponseHeaders(const Pickle& pickle, void** iter) |
87 : response_code_(-1) { | 97 : response_code_(-1) { |
88 std::string raw_input; | 98 std::string raw_input; |
89 if (pickle.ReadString(iter, &raw_input)) | 99 if (pickle.ReadString(iter, &raw_input)) |
90 Parse(raw_input); | 100 Parse(raw_input); |
91 } | 101 } |
92 | 102 |
93 void HttpResponseHeaders::Persist(Pickle* pickle, bool for_cache) { | 103 void HttpResponseHeaders::Persist(Pickle* pickle, PersistOptions options) { |
94 if (for_cache) { | 104 if (options == PERSIST_RAW) { |
95 HeaderSet transient_headers; | 105 pickle->WriteString(raw_headers_); |
96 GetTransientHeaders(&transient_headers); | 106 return; // Done. |
| 107 } |
97 | 108 |
98 std::string blob; | 109 HeaderSet filter_headers; |
99 blob.reserve(raw_headers_.size()); | |
100 | 110 |
101 // this just copies the status line w/ terminator | 111 // Construct set of headers to filter out based on options. |
102 blob.assign(raw_headers_.c_str(), strlen(raw_headers_.c_str()) + 1); | 112 if ((options & PERSIST_SANS_NON_CACHEABLE) == PERSIST_SANS_NON_CACHEABLE) |
| 113 AddNonCacheableHeaders(&filter_headers); |
103 | 114 |
104 for (size_t i = 0; i < parsed_.size(); ++i) { | 115 if ((options & PERSIST_SANS_COOKIES) == PERSIST_SANS_COOKIES) |
105 DCHECK(!parsed_[i].is_continuation()); | 116 AddCookieHeaders(&filter_headers); |
106 | 117 |
107 // locate the start of the next header | 118 if ((options & PERSIST_SANS_CHALLENGES) == PERSIST_SANS_CHALLENGES) |
108 size_t k = i; | 119 AddChallengeHeaders(&filter_headers); |
109 while (++k < parsed_.size() && parsed_[k].is_continuation()); | |
110 --k; | |
111 | 120 |
112 std::string header_name(parsed_[i].name_begin, parsed_[i].name_end); | 121 if ((options & PERSIST_SANS_HOP_BY_HOP) == PERSIST_SANS_HOP_BY_HOP) |
113 StringToLowerASCII(&header_name); | 122 AddHopByHopHeaders(&filter_headers); |
114 | 123 |
115 if (transient_headers.find(header_name) == transient_headers.end()) { | 124 std::string blob; |
116 // includes terminator | 125 blob.reserve(raw_headers_.size()); |
117 blob.append(parsed_[i].name_begin, parsed_[k].value_end + 1); | |
118 } | |
119 | 126 |
120 i = k; | 127 // This copies the status line w/ terminator null. |
| 128 // Note raw_headers_ has embedded nulls instead of \n, |
| 129 // so this just copies the first header line. |
| 130 blob.assign(raw_headers_.c_str(), strlen(raw_headers_.c_str()) + 1); |
| 131 |
| 132 for (size_t i = 0; i < parsed_.size(); ++i) { |
| 133 DCHECK(!parsed_[i].is_continuation()); |
| 134 |
| 135 // Locate the start of the next header. |
| 136 size_t k = i; |
| 137 while (++k < parsed_.size() && parsed_[k].is_continuation()); |
| 138 --k; |
| 139 |
| 140 std::string header_name(parsed_[i].name_begin, parsed_[i].name_end); |
| 141 StringToLowerASCII(&header_name); |
| 142 |
| 143 if (filter_headers.find(header_name) == filter_headers.end()) { |
| 144 // Includes terminator null due to the + 1. |
| 145 blob.append(parsed_[i].name_begin, parsed_[k].value_end + 1); |
121 } | 146 } |
122 blob.push_back('\0'); | |
123 | 147 |
124 pickle->WriteString(blob); | 148 i = k; |
125 } else { | |
126 pickle->WriteString(raw_headers_); | |
127 } | 149 } |
| 150 blob.push_back('\0'); |
| 151 |
| 152 pickle->WriteString(blob); |
128 } | 153 } |
129 | 154 |
130 void HttpResponseHeaders::Update(const HttpResponseHeaders& new_headers) { | 155 void HttpResponseHeaders::Update(const HttpResponseHeaders& new_headers) { |
131 DCHECK(new_headers.response_code() == 304 || | 156 DCHECK(new_headers.response_code() == 304 || |
132 new_headers.response_code() == 206); | 157 new_headers.response_code() == 206); |
133 | 158 |
134 // copy up to the null byte. this just copies the status line. | 159 // copy up to the null byte. this just copies the status line. |
135 std::string new_raw_headers(raw_headers_.c_str()); | 160 std::string new_raw_headers(raw_headers_.c_str()); |
136 new_raw_headers.push_back('\0'); | 161 new_raw_headers.push_back('\0'); |
137 | 162 |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 std::string::const_iterator value_begin, | 575 std::string::const_iterator value_begin, |
551 std::string::const_iterator value_end) { | 576 std::string::const_iterator value_end) { |
552 ParsedHeader header; | 577 ParsedHeader header; |
553 header.name_begin = name_begin; | 578 header.name_begin = name_begin; |
554 header.name_end = name_end; | 579 header.name_end = name_end; |
555 header.value_begin = value_begin; | 580 header.value_begin = value_begin; |
556 header.value_end = value_end; | 581 header.value_end = value_end; |
557 parsed_.push_back(header); | 582 parsed_.push_back(header); |
558 } | 583 } |
559 | 584 |
560 void HttpResponseHeaders::GetTransientHeaders(HeaderSet* result) const { | 585 void HttpResponseHeaders::AddNonCacheableHeaders(HeaderSet* result) const { |
561 // Add server specified transients. Any 'cache-control: no-cache="foo,bar"' | 586 // Add server specified transients. Any 'cache-control: no-cache="foo,bar"' |
562 // headers present in the response specify additional headers that we should | 587 // headers present in the response specify additional headers that we should |
563 // not store in the cache. | 588 // not store in the cache. |
564 const std::string kCacheControl = "cache-control"; | 589 const std::string kCacheControl = "cache-control"; |
565 const std::string kPrefix = "no-cache=\""; | 590 const std::string kPrefix = "no-cache=\""; |
566 std::string value; | 591 std::string value; |
567 void* iter = NULL; | 592 void* iter = NULL; |
568 while (EnumerateHeader(&iter, kCacheControl, &value)) { | 593 while (EnumerateHeader(&iter, kCacheControl, &value)) { |
569 if (value.size() > kPrefix.size() && | 594 if (value.size() > kPrefix.size() && |
570 value.compare(0, kPrefix.size(), kPrefix) == 0) { | 595 value.compare(0, kPrefix.size(), kPrefix) == 0) { |
(...skipping 24 matching lines...) Expand all Loading... |
595 | 620 |
596 // repeat | 621 // repeat |
597 begin_pos = comma_pos + 1; | 622 begin_pos = comma_pos + 1; |
598 while (begin_pos < value.size() && strchr(HTTP_LWS, value[begin_pos])) | 623 while (begin_pos < value.size() && strchr(HTTP_LWS, value[begin_pos])) |
599 begin_pos++; | 624 begin_pos++; |
600 if (begin_pos >= value.size()) | 625 if (begin_pos >= value.size()) |
601 break; | 626 break; |
602 } | 627 } |
603 } | 628 } |
604 } | 629 } |
| 630 } |
605 | 631 |
606 // Add standard transient headers. Perhaps we should move this to a | 632 void HttpResponseHeaders::AddHopByHopHeaders(HeaderSet* result) { |
607 // statically cached hash_set to avoid duplicated work? | 633 for (size_t i = 0; i < arraysize(kHopByHopResponseHeaders); ++i) |
608 for (size_t i = 0; i < arraysize(kTransientHeaders); ++i) | 634 result->insert(std::string(kHopByHopResponseHeaders[i])); |
609 result->insert(std::string(kTransientHeaders[i])); | 635 } |
| 636 |
| 637 void HttpResponseHeaders::AddCookieHeaders(HeaderSet* result) { |
| 638 for (size_t i = 0; i < arraysize(kCookieResponseHeaders); ++i) |
| 639 result->insert(std::string(kCookieResponseHeaders[i])); |
| 640 } |
| 641 |
| 642 void HttpResponseHeaders::AddChallengeHeaders(HeaderSet* result) { |
| 643 for (size_t i = 0; i < arraysize(kChallengeResponseHeaders); ++i) |
| 644 result->insert(std::string(kChallengeResponseHeaders[i])); |
610 } | 645 } |
611 | 646 |
612 void HttpResponseHeaders::GetMimeTypeAndCharset(std::string* mime_type, | 647 void HttpResponseHeaders::GetMimeTypeAndCharset(std::string* mime_type, |
613 std::string* charset) const { | 648 std::string* charset) const { |
614 mime_type->clear(); | 649 mime_type->clear(); |
615 charset->clear(); | 650 charset->clear(); |
616 | 651 |
617 std::string name = "content-type"; | 652 std::string name = "content-type"; |
618 std::string value; | 653 std::string value; |
619 | 654 |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 int64 result; | 968 int64 result; |
934 bool ok = StringToInt64(content_length_val, &result); | 969 bool ok = StringToInt64(content_length_val, &result); |
935 if (!ok || result < 0) | 970 if (!ok || result < 0) |
936 return -1; | 971 return -1; |
937 | 972 |
938 return result; | 973 return result; |
939 } | 974 } |
940 | 975 |
941 } // namespace net | 976 } // namespace net |
942 | 977 |
OLD | NEW |