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 #include "net/base/filter.h" | 5 #include "net/base/filter.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "net/base/gzip_filter.h" | 8 #include "net/base/gzip_filter.h" |
9 #include "net/base/bzip2_filter.h" | 9 #include "net/base/bzip2_filter.h" |
10 #include "net/base/sdch_filter.h" | 10 #include "net/base/sdch_filter.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 const char kApplicationGzip[] = "application/gzip"; | 31 const char kApplicationGzip[] = "application/gzip"; |
32 const char kApplicationXGunzip[] = "application/x-gunzip"; | 32 const char kApplicationXGunzip[] = "application/x-gunzip"; |
33 const char kApplicationXCompress[] = "application/x-compress"; | 33 const char kApplicationXCompress[] = "application/x-compress"; |
34 const char kApplicationCompress[] = "application/compress"; | 34 const char kApplicationCompress[] = "application/compress"; |
35 const char kTextHtml[] = "text/html"; | 35 const char kTextHtml[] = "text/html"; |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, | 39 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, |
40 const FilterContext& filter_context) { | 40 const FilterContext& filter_context) { |
41 DCHECK(filter_context.GetInputStreambufferSize() > 0); | 41 DCHECK(filter_context.GetInputStreamBufferSize() > 0); |
42 if (filter_types.empty() || filter_context.GetInputStreambufferSize() <= 0) | 42 if (filter_types.empty() || filter_context.GetInputStreamBufferSize() <= 0) |
43 return NULL; | 43 return NULL; |
44 | 44 |
45 | 45 |
46 Filter* filter_list = NULL; // Linked list of filters. | 46 Filter* filter_list = NULL; // Linked list of filters. |
47 for (size_t i = 0; i < filter_types.size(); i++) { | 47 for (size_t i = 0; i < filter_types.size(); i++) { |
48 filter_list = PrependNewFilter(filter_types[i], filter_context, | 48 filter_list = PrependNewFilter(filter_types[i], filter_context, |
49 filter_list); | 49 filter_list); |
50 if (!filter_list) | 50 if (!filter_list) |
51 return NULL; | 51 return NULL; |
52 } | 52 } |
53 | |
54 // TODO(jar): These settings should go into the derived classes, on an as-need
ed basis. | |
55 std::string mime_type; | |
56 bool success = filter_context.GetMimeType(&mime_type); | |
57 DCHECK(success); | |
58 GURL gurl; | |
59 success = filter_context.GetURL(&gurl); | |
60 DCHECK(success); | |
61 base::Time request_time = filter_context.GetRequestTime(); | |
62 bool is_cached_content = filter_context.IsCachedContent(); | |
63 | |
64 filter_list->SetMimeType(mime_type); | |
65 filter_list->SetURL(gurl); | |
66 // Approximate connect time with request_time. If it is not cached, then | |
67 // this is a good approximation for when the first bytes went on the | |
68 // wire. | |
69 filter_list->SetConnectTime(request_time, is_cached_content); | |
70 | |
71 return filter_list; | 53 return filter_list; |
72 } | 54 } |
73 | 55 |
74 // static | 56 // static |
75 Filter::FilterType Filter::ConvertEncodingToType( | 57 Filter::FilterType Filter::ConvertEncodingToType( |
76 const std::string& filter_type) { | 58 const std::string& filter_type) { |
77 FilterType type_id; | 59 FilterType type_id; |
78 if (LowerCaseEqualsASCII(filter_type, kDeflate)) { | 60 if (LowerCaseEqualsASCII(filter_type, kDeflate)) { |
79 type_id = FILTER_TYPE_DEFLATE; | 61 type_id = FILTER_TYPE_DEFLATE; |
80 } else if (LowerCaseEqualsASCII(filter_type, kGZip) || | 62 } else if (LowerCaseEqualsASCII(filter_type, kGZip) || |
81 LowerCaseEqualsASCII(filter_type, kXGZip)) { | 63 LowerCaseEqualsASCII(filter_type, kXGZip)) { |
82 type_id = FILTER_TYPE_GZIP; | 64 type_id = FILTER_TYPE_GZIP; |
83 } else if (LowerCaseEqualsASCII(filter_type, kBZip2) || | 65 } else if (LowerCaseEqualsASCII(filter_type, kBZip2) || |
84 LowerCaseEqualsASCII(filter_type, kXBZip2)) { | 66 LowerCaseEqualsASCII(filter_type, kXBZip2)) { |
85 type_id = FILTER_TYPE_BZIP2; | 67 type_id = FILTER_TYPE_BZIP2; |
86 } else if (LowerCaseEqualsASCII(filter_type, kSdch)) { | 68 } else if (LowerCaseEqualsASCII(filter_type, kSdch)) { |
87 type_id = FILTER_TYPE_SDCH; | 69 type_id = FILTER_TYPE_SDCH; |
88 } else { | 70 } else { |
89 // Note we also consider "identity" and "uncompressed" UNSUPPORTED as | 71 // Note we also consider "identity" and "uncompressed" UNSUPPORTED as |
90 // filter should be disabled in such cases. | 72 // filter should be disabled in such cases. |
91 type_id = FILTER_TYPE_UNSUPPORTED; | 73 type_id = FILTER_TYPE_UNSUPPORTED; |
92 } | 74 } |
93 return type_id; | 75 return type_id; |
94 } | 76 } |
95 | 77 |
96 // static | 78 // static |
97 void Filter::FixupEncodingTypes( | 79 void Filter::FixupEncodingTypes( |
98 bool is_sdch_response, | 80 const FilterContext& filter_context, |
99 const std::string& mime_type, | |
100 std::vector<FilterType>* encoding_types) { | 81 std::vector<FilterType>* encoding_types) { |
| 82 std::string mime_type; |
| 83 bool success = filter_context.GetMimeType(&mime_type); |
| 84 DCHECK(success); |
101 | 85 |
102 if ((1 == encoding_types->size()) && | 86 if ((1 == encoding_types->size()) && |
103 (FILTER_TYPE_GZIP == encoding_types->front())) { | 87 (FILTER_TYPE_GZIP == encoding_types->front())) { |
104 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || | 88 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || |
105 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || | 89 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || |
106 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) | 90 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) |
107 // The server has told us that it sent us gziped content with a gzip | 91 // The server has told us that it sent us gziped content with a gzip |
108 // content encoding. Sadly, Apache mistakenly sets these headers for all | 92 // content encoding. Sadly, Apache mistakenly sets these headers for all |
109 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore | 93 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore |
110 // the Content-Encoding here. | 94 // the Content-Encoding here. |
111 encoding_types->clear(); | 95 encoding_types->clear(); |
112 } | 96 } |
113 | 97 |
114 if (!is_sdch_response) { | 98 if (!filter_context.IsSdchResponse()) { |
115 if (1 < encoding_types->size()) { | 99 if (1 < encoding_types->size()) { |
116 // Multiple filters were intended to only be used for SDCH (thus far!) | 100 // Multiple filters were intended to only be used for SDCH (thus far!) |
117 SdchManager::SdchErrorRecovery( | 101 SdchManager::SdchErrorRecovery( |
118 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); | 102 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); |
119 } | 103 } |
120 if ((1 == encoding_types->size()) && | 104 if ((1 == encoding_types->size()) && |
121 (FILTER_TYPE_SDCH == encoding_types->front())) { | 105 (FILTER_TYPE_SDCH == encoding_types->front())) { |
122 SdchManager::SdchErrorRecovery( | 106 SdchManager::SdchErrorRecovery( |
123 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | 107 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); |
124 } | 108 } |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 226 |
243 first_filter->next_filter_.reset(filter_list); | 227 first_filter->next_filter_.reset(filter_list); |
244 return first_filter; | 228 return first_filter; |
245 } | 229 } |
246 | 230 |
247 Filter::Filter(const FilterContext& filter_context) | 231 Filter::Filter(const FilterContext& filter_context) |
248 : stream_buffer_(NULL), | 232 : stream_buffer_(NULL), |
249 stream_buffer_size_(0), | 233 stream_buffer_size_(0), |
250 next_stream_data_(NULL), | 234 next_stream_data_(NULL), |
251 stream_data_len_(0), | 235 stream_data_len_(0), |
252 url_(), | |
253 connect_time_(), | |
254 was_cached_(false), | |
255 mime_type_(), | |
256 next_filter_(NULL), | 236 next_filter_(NULL), |
257 last_status_(FILTER_NEED_MORE_DATA), | 237 last_status_(FILTER_NEED_MORE_DATA), |
258 filter_context_(filter_context) { | 238 filter_context_(filter_context) { |
259 } | 239 } |
260 | 240 |
261 Filter::~Filter() {} | 241 Filter::~Filter() {} |
262 | 242 |
263 bool Filter::InitBuffer() { | 243 bool Filter::InitBuffer() { |
264 int buffer_size = filter_context_.GetInputStreambufferSize(); | 244 int buffer_size = filter_context_.GetInputStreamBufferSize(); |
265 DCHECK(buffer_size > 0); | 245 DCHECK(buffer_size > 0); |
266 if (buffer_size <= 0 || stream_buffer()) | 246 if (buffer_size <= 0 || stream_buffer()) |
267 return false; | 247 return false; |
268 | 248 |
269 stream_buffer_ = new net::IOBuffer(buffer_size); | 249 stream_buffer_ = new net::IOBuffer(buffer_size); |
270 | 250 |
271 if (stream_buffer()) { | 251 if (stream_buffer()) { |
272 stream_buffer_size_ = buffer_size; | 252 stream_buffer_size_ = buffer_size; |
273 return true; | 253 return true; |
274 } | 254 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 338 |
359 DCHECK(stream_buffer()); | 339 DCHECK(stream_buffer()); |
360 // Bail out if there is more data in the stream buffer to be filtered. | 340 // Bail out if there is more data in the stream buffer to be filtered. |
361 if (!stream_buffer() || stream_data_len_) | 341 if (!stream_buffer() || stream_data_len_) |
362 return false; | 342 return false; |
363 | 343 |
364 next_stream_data_ = stream_buffer()->data(); | 344 next_stream_data_ = stream_buffer()->data(); |
365 stream_data_len_ = stream_data_len; | 345 stream_data_len_ = stream_data_len; |
366 return true; | 346 return true; |
367 } | 347 } |
368 | |
369 void Filter::SetURL(const GURL& url) { | |
370 url_ = url; | |
371 if (next_filter_.get()) | |
372 next_filter_->SetURL(url); | |
373 } | |
374 | |
375 void Filter::SetMimeType(const std::string& mime_type) { | |
376 mime_type_ = mime_type; | |
377 if (next_filter_.get()) | |
378 next_filter_->SetMimeType(mime_type); | |
379 } | |
380 | |
381 void Filter::SetConnectTime(const base::Time& time, bool was_cached) { | |
382 connect_time_ = time; | |
383 was_cached_ = was_cached; | |
384 if (next_filter_.get()) | |
385 next_filter_->SetConnectTime(time, was_cached_); | |
386 } | |
OLD | NEW |