| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/filter/sdch_filter.h" | 5 #include "net/filter/sdch_filter.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <limits.h> | 8 #include <limits.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 | 46 |
| 47 if (vcdiff_streaming_decoder_.get()) { | 47 if (vcdiff_streaming_decoder_.get()) { |
| 48 if (!vcdiff_streaming_decoder_->FinishDecoding()) { | 48 if (!vcdiff_streaming_decoder_->FinishDecoding()) { |
| 49 decoding_status_ = DECODING_ERROR; | 49 decoding_status_ = DECODING_ERROR; |
| 50 SdchManager::SdchErrorRecovery(SdchManager::INCOMPLETE_SDCH_CONTENT); | 50 SdchManager::SdchErrorRecovery(SdchManager::INCOMPLETE_SDCH_CONTENT); |
| 51 // Make it possible for the user to hit reload, and get non-sdch content. | 51 // Make it possible for the user to hit reload, and get non-sdch content. |
| 52 // Note this will "wear off" quickly enough, and is just meant to assure | 52 // Note this will "wear off" quickly enough, and is just meant to assure |
| 53 // in some rare case that the user is not stuck. | 53 // in some rare case that the user is not stuck. |
| 54 SdchManager::BlacklistDomain(url_); | 54 SdchManager::BlacklistDomain(url_); |
| 55 UMA_HISTOGRAM_COUNTS("Sdch3.PartialBytesIn", | 55 UMA_HISTOGRAM_COUNTS( |
| 56 static_cast<int>(filter_context_.GetByteReadCount())); | 56 "Sdch3.PartialBytesIn", |
| 57 static_cast<int>(filter_context_.GetByteReadCount())); |
| 57 UMA_HISTOGRAM_COUNTS("Sdch3.PartialVcdiffIn", source_bytes_); | 58 UMA_HISTOGRAM_COUNTS("Sdch3.PartialVcdiffIn", source_bytes_); |
| 58 UMA_HISTOGRAM_COUNTS("Sdch3.PartialVcdiffOut", output_bytes_); | 59 UMA_HISTOGRAM_COUNTS("Sdch3.PartialVcdiffOut", output_bytes_); |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 if (!dest_buffer_excess_.empty()) { | 63 if (!dest_buffer_excess_.empty()) { |
| 63 // Filter chaining error, or premature teardown. | 64 // Filter chaining error, or premature teardown. |
| 64 SdchManager::SdchErrorRecovery(SdchManager::UNFLUSHED_CONTENT); | 65 SdchManager::SdchErrorRecovery(SdchManager::UNFLUSHED_CONTENT); |
| 65 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedBytesIn", | 66 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedBytesIn", |
| 66 static_cast<int>(filter_context_.GetByteReadCount())); | 67 static_cast<int>(filter_context_.GetByteReadCount())); |
| 67 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedBufferSize", | 68 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedBufferSize", |
| 68 dest_buffer_excess_.size()); | 69 dest_buffer_excess_.size()); |
| 69 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedVcdiffIn", source_bytes_); | 70 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedVcdiffIn", source_bytes_); |
| 70 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedVcdiffOut", output_bytes_); | 71 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedVcdiffOut", output_bytes_); |
| 71 } | 72 } |
| 72 | 73 |
| 73 if (filter_context_.IsCachedContent()) { | 74 if (filter_context_.IsCachedContent()) { |
| 74 // Not a real error, but it is useful to have this tally. | 75 // Not a real error, but it is useful to have this tally. |
| 75 // TODO(jar): Remove this stat after SDCH stability is validated. | 76 // TODO(jar): Remove this stat after SDCH stability is validated. |
| 76 SdchManager::SdchErrorRecovery(SdchManager::CACHE_DECODED); | 77 SdchManager::SdchErrorRecovery(SdchManager::CACHE_DECODED); |
| 77 return; // We don't need timing stats, and we aready got ratios. | 78 return; // We don't need timing stats, and we aready got ratios. |
| 78 } | 79 } |
| 79 | 80 |
| 80 switch (decoding_status_) { | 81 switch (decoding_status_) { |
| 81 case DECODING_IN_PROGRESS: { | 82 case DECODING_IN_PROGRESS: { |
| 82 if (output_bytes_) | 83 if (output_bytes_) |
| 83 UMA_HISTOGRAM_PERCENTAGE("Sdch3.Network_Decode_Ratio_a", | 84 UMA_HISTOGRAM_PERCENTAGE( |
| 84 static_cast<int>( | 85 "Sdch3.Network_Decode_Ratio_a", |
| 85 (filter_context_.GetByteReadCount() * 100) / output_bytes_)); | 86 static_cast<int>((filter_context_.GetByteReadCount() * 100) / |
| 87 output_bytes_)); |
| 86 UMA_HISTOGRAM_COUNTS("Sdch3.Network_Decode_Bytes_VcdiffOut_a", | 88 UMA_HISTOGRAM_COUNTS("Sdch3.Network_Decode_Bytes_VcdiffOut_a", |
| 87 output_bytes_); | 89 output_bytes_); |
| 88 filter_context_.RecordPacketStats(FilterContext::SDCH_DECODE); | 90 filter_context_.RecordPacketStats(FilterContext::SDCH_DECODE); |
| 89 | 91 |
| 90 // Allow latency experiments to proceed. | 92 // Allow latency experiments to proceed. |
| 91 SdchManager::Global()->SetAllowLatencyExperiment(url_, true); | 93 SdchManager::Global()->SetAllowLatencyExperiment(url_, true); |
| 92 return; | 94 return; |
| 93 } | 95 } |
| 94 case PASS_THROUGH: { | 96 case PASS_THROUGH: { |
| 95 filter_context_.RecordPacketStats(FilterContext::SDCH_PASSTHROUGH); | 97 filter_context_.RecordPacketStats(FilterContext::SDCH_PASSTHROUGH); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 122 if (FILTER_TYPE_SDCH_POSSIBLE == filter_type) | 124 if (FILTER_TYPE_SDCH_POSSIBLE == filter_type) |
| 123 possible_pass_through_ = true; | 125 possible_pass_through_ = true; |
| 124 | 126 |
| 125 // Initialize decoder only after we have a dictionary in hand. | 127 // Initialize decoder only after we have a dictionary in hand. |
| 126 decoding_status_ = WAITING_FOR_DICTIONARY_SELECTION; | 128 decoding_status_ = WAITING_FOR_DICTIONARY_SELECTION; |
| 127 return true; | 129 return true; |
| 128 } | 130 } |
| 129 | 131 |
| 130 #ifndef NDEBUG | 132 #ifndef NDEBUG |
| 131 static const char* kDecompressionErrorHtml = | 133 static const char* kDecompressionErrorHtml = |
| 132 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>" | 134 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>" |
| 133 "<div style=\"position:fixed;top:0;left:0;width:100%;border-width:thin;" | 135 "<div style=\"position:fixed;top:0;left:0;width:100%;border-width:thin;" |
| 134 "border-color:black;border-style:solid;text-align:left;font-family:arial;" | 136 "border-color:black;border-style:solid;text-align:left;font-family:arial;" |
| 135 "font-size:10pt;foreground-color:black;background-color:white\">" | 137 "font-size:10pt;foreground-color:black;background-color:white\">" |
| 136 "An error occurred. This page will be reloaded shortly. " | 138 "An error occurred. This page will be reloaded shortly. " |
| 137 "Or press the \"reload\" button now to reload it immediately." | 139 "Or press the \"reload\" button now to reload it immediately." |
| 138 "</div>"; | 140 "</div>"; |
| 139 #else | 141 #else |
| 140 static const char* kDecompressionErrorHtml = | 142 static const char* kDecompressionErrorHtml = |
| 141 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>"; | 143 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>"; |
| 142 #endif | 144 #endif |
| 143 | 145 |
| 144 Filter::FilterStatus SdchFilter::ReadFilteredData(char* dest_buffer, | 146 Filter::FilterStatus SdchFilter::ReadFilteredData(char* dest_buffer, |
| 145 int* dest_len) { | 147 int* dest_len) { |
| 146 int available_space = *dest_len; | 148 int available_space = *dest_len; |
| 147 *dest_len = 0; // Nothing output yet. | 149 *dest_len = 0; // Nothing output yet. |
| 148 | 150 |
| 149 if (!dest_buffer || available_space <= 0) | 151 if (!dest_buffer || available_space <= 0) |
| 150 return FILTER_ERROR; | 152 return FILTER_ERROR; |
| 151 | 153 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 167 // error response. When we see such content molestation, we certainly | 169 // error response. When we see such content molestation, we certainly |
| 168 // need to fall into the meta-refresh case. | 170 // need to fall into the meta-refresh case. |
| 169 if (filter_context_.GetResponseCode() == 404) { | 171 if (filter_context_.GetResponseCode() == 404) { |
| 170 // We could be more generous, but for now, only a "NOT FOUND" code will | 172 // We could be more generous, but for now, only a "NOT FOUND" code will |
| 171 // cause a pass through. All other bad codes will fall into a | 173 // cause a pass through. All other bad codes will fall into a |
| 172 // meta-refresh. | 174 // meta-refresh. |
| 173 SdchManager::SdchErrorRecovery(SdchManager::PASS_THROUGH_404_CODE); | 175 SdchManager::SdchErrorRecovery(SdchManager::PASS_THROUGH_404_CODE); |
| 174 decoding_status_ = PASS_THROUGH; | 176 decoding_status_ = PASS_THROUGH; |
| 175 } else if (filter_context_.GetResponseCode() != 200) { | 177 } else if (filter_context_.GetResponseCode() != 200) { |
| 176 // We need to meta-refresh, with SDCH disabled. | 178 // We need to meta-refresh, with SDCH disabled. |
| 177 } else if (filter_context_.IsCachedContent() | 179 } else if (filter_context_.IsCachedContent() && |
| 178 && !dictionary_hash_is_plausible_) { | 180 !dictionary_hash_is_plausible_) { |
| 179 // We must have hit the back button, and gotten content that was fetched | 181 // We must have hit the back button, and gotten content that was fetched |
| 180 // before we *really* advertised SDCH and a dictionary. | 182 // before we *really* advertised SDCH and a dictionary. |
| 181 SdchManager::SdchErrorRecovery(SdchManager::PASS_THROUGH_OLD_CACHED); | 183 SdchManager::SdchErrorRecovery(SdchManager::PASS_THROUGH_OLD_CACHED); |
| 182 decoding_status_ = PASS_THROUGH; | 184 decoding_status_ = PASS_THROUGH; |
| 183 } else if (possible_pass_through_) { | 185 } else if (possible_pass_through_) { |
| 184 // This is the potentially most graceful response. There really was no | 186 // This is the potentially most graceful response. There really was no |
| 185 // error. We were just overly cautious when we added a TENTATIVE_SDCH. | 187 // error. We were just overly cautious when we added a TENTATIVE_SDCH. |
| 186 // We added the sdch coding tag, and it should not have been added. | 188 // We added the sdch coding tag, and it should not have been added. |
| 187 // This can happen in server experiments, where the server decides | 189 // This can happen in server experiments, where the server decides |
| 188 // not to use sdch, even though there is a dictionary. To be | 190 // not to use sdch, even though there is a dictionary. To be |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } | 283 } |
| 282 DCHECK(false); | 284 DCHECK(false); |
| 283 decoding_status_ = DECODING_ERROR; | 285 decoding_status_ = DECODING_ERROR; |
| 284 return FILTER_ERROR; | 286 return FILTER_ERROR; |
| 285 } | 287 } |
| 286 | 288 |
| 287 if (!next_stream_data_ || stream_data_len_ <= 0) | 289 if (!next_stream_data_ || stream_data_len_ <= 0) |
| 288 return FILTER_NEED_MORE_DATA; | 290 return FILTER_NEED_MORE_DATA; |
| 289 | 291 |
| 290 bool ret = vcdiff_streaming_decoder_->DecodeChunk( | 292 bool ret = vcdiff_streaming_decoder_->DecodeChunk( |
| 291 next_stream_data_, stream_data_len_, &dest_buffer_excess_); | 293 next_stream_data_, stream_data_len_, &dest_buffer_excess_); |
| 292 // Assume all data was used in decoding. | 294 // Assume all data was used in decoding. |
| 293 next_stream_data_ = NULL; | 295 next_stream_data_ = NULL; |
| 294 source_bytes_ += stream_data_len_; | 296 source_bytes_ += stream_data_len_; |
| 295 stream_data_len_ = 0; | 297 stream_data_len_ = 0; |
| 296 output_bytes_ += dest_buffer_excess_.size(); | 298 output_bytes_ += dest_buffer_excess_.size(); |
| 297 if (!ret) { | 299 if (!ret) { |
| 298 vcdiff_streaming_decoder_.reset(NULL); // Don't call it again. | 300 vcdiff_streaming_decoder_.reset(NULL); // Don't call it again. |
| 299 decoding_status_ = DECODING_ERROR; | 301 decoding_status_ = DECODING_ERROR; |
| 300 SdchManager::SdchErrorRecovery(SdchManager::DECODE_BODY_ERROR); | 302 SdchManager::SdchErrorRecovery(SdchManager::DECODE_BODY_ERROR); |
| 301 return FILTER_ERROR; | 303 return FILTER_ERROR; |
| 302 } | 304 } |
| 303 | 305 |
| 304 amount = OutputBufferExcess(dest_buffer, available_space); | 306 amount = OutputBufferExcess(dest_buffer, available_space); |
| 305 *dest_len += amount; | 307 *dest_len += amount; |
| 306 dest_buffer += amount; | 308 dest_buffer += amount; |
| 307 available_space -= amount; | 309 available_space -= amount; |
| 308 if (0 == available_space && !dest_buffer_excess_.empty()) | 310 if (0 == available_space && !dest_buffer_excess_.empty()) |
| 309 return FILTER_OK; | 311 return FILTER_OK; |
| 310 return FILTER_NEED_MORE_DATA; | 312 return FILTER_NEED_MORE_DATA; |
| 311 } | 313 } |
| 312 | 314 |
| 313 Filter::FilterStatus SdchFilter::InitializeDictionary() { | 315 Filter::FilterStatus SdchFilter::InitializeDictionary() { |
| 314 const size_t kServerIdLength = 9; // Dictionary hash plus null from server. | 316 const size_t kServerIdLength = 9; // Dictionary hash plus null from server. |
| 315 size_t bytes_needed = kServerIdLength - dictionary_hash_.size(); | 317 size_t bytes_needed = kServerIdLength - dictionary_hash_.size(); |
| 316 DCHECK_GT(bytes_needed, 0u); | 318 DCHECK_GT(bytes_needed, 0u); |
| 317 if (!next_stream_data_) | 319 if (!next_stream_data_) |
| 318 return FILTER_NEED_MORE_DATA; | 320 return FILTER_NEED_MORE_DATA; |
| 319 if (static_cast<size_t>(stream_data_len_) < bytes_needed) { | 321 if (static_cast<size_t>(stream_data_len_) < bytes_needed) { |
| 320 dictionary_hash_.append(next_stream_data_, stream_data_len_); | 322 dictionary_hash_.append(next_stream_data_, stream_data_len_); |
| 321 next_stream_data_ = NULL; | 323 next_stream_data_ = NULL; |
| 322 stream_data_len_ = 0; | 324 stream_data_len_ = 0; |
| 323 return FILTER_NEED_MORE_DATA; | 325 return FILTER_NEED_MORE_DATA; |
| 324 } | 326 } |
| 325 dictionary_hash_.append(next_stream_data_, bytes_needed); | 327 dictionary_hash_.append(next_stream_data_, bytes_needed); |
| 326 DCHECK(kServerIdLength == dictionary_hash_.size()); | 328 DCHECK(kServerIdLength == dictionary_hash_.size()); |
| 327 stream_data_len_ -= bytes_needed; | 329 stream_data_len_ -= bytes_needed; |
| 328 DCHECK_LE(0, stream_data_len_); | 330 DCHECK_LE(0, stream_data_len_); |
| 329 if (stream_data_len_ > 0) | 331 if (stream_data_len_ > 0) |
| 330 next_stream_data_ += bytes_needed; | 332 next_stream_data_ += bytes_needed; |
| 331 else | 333 else |
| 332 next_stream_data_ = NULL; | 334 next_stream_data_ = NULL; |
| 333 | 335 |
| 334 DCHECK(!dictionary_.get()); | 336 DCHECK(!dictionary_.get()); |
| 335 dictionary_hash_is_plausible_ = true; // Assume plausible, but check. | 337 dictionary_hash_is_plausible_ = true; // Assume plausible, but check. |
| 336 | 338 |
| 337 SdchManager::Dictionary* dictionary = NULL; | 339 SdchManager::Dictionary* dictionary = NULL; |
| 338 if ('\0' == dictionary_hash_[kServerIdLength - 1]) | 340 if ('\0' == dictionary_hash_[kServerIdLength - 1]) |
| 339 SdchManager::Global()->GetVcdiffDictionary(std::string(dictionary_hash_, 0, | 341 SdchManager::Global()->GetVcdiffDictionary( |
| 340 kServerIdLength - 1), | 342 std::string(dictionary_hash_, 0, kServerIdLength - 1), |
| 341 url_, &dictionary); | 343 url_, |
| 344 &dictionary); |
| 342 else | 345 else |
| 343 dictionary_hash_is_plausible_ = false; | 346 dictionary_hash_is_plausible_ = false; |
| 344 | 347 |
| 345 if (!dictionary) { | 348 if (!dictionary) { |
| 346 DCHECK(dictionary_hash_.size() == kServerIdLength); | 349 DCHECK(dictionary_hash_.size() == kServerIdLength); |
| 347 // Since dictionary was not found, check to see if hash was even plausible. | 350 // Since dictionary was not found, check to see if hash was even plausible. |
| 348 for (size_t i = 0; i < kServerIdLength - 1; ++i) { | 351 for (size_t i = 0; i < kServerIdLength - 1; ++i) { |
| 349 char base64_char = dictionary_hash_[i]; | 352 char base64_char = dictionary_hash_[i]; |
| 350 if (!isalnum(base64_char) && '-' != base64_char && '_' != base64_char) { | 353 if (!isalnum(base64_char) && '-' != base64_char && '_' != base64_char) { |
| 351 dictionary_hash_is_plausible_ = false; | 354 dictionary_hash_is_plausible_ = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 366 dictionary_->text().size()); | 369 dictionary_->text().size()); |
| 367 decoding_status_ = DECODING_IN_PROGRESS; | 370 decoding_status_ = DECODING_IN_PROGRESS; |
| 368 return FILTER_OK; | 371 return FILTER_OK; |
| 369 } | 372 } |
| 370 | 373 |
| 371 int SdchFilter::OutputBufferExcess(char* const dest_buffer, | 374 int SdchFilter::OutputBufferExcess(char* const dest_buffer, |
| 372 size_t available_space) { | 375 size_t available_space) { |
| 373 if (dest_buffer_excess_.empty()) | 376 if (dest_buffer_excess_.empty()) |
| 374 return 0; | 377 return 0; |
| 375 DCHECK(dest_buffer_excess_.size() > dest_buffer_excess_index_); | 378 DCHECK(dest_buffer_excess_.size() > dest_buffer_excess_index_); |
| 376 size_t amount = std::min(available_space, | 379 size_t amount = std::min( |
| 377 dest_buffer_excess_.size() - dest_buffer_excess_index_); | 380 available_space, dest_buffer_excess_.size() - dest_buffer_excess_index_); |
| 378 memcpy(dest_buffer, dest_buffer_excess_.data() + dest_buffer_excess_index_, | 381 memcpy(dest_buffer, |
| 382 dest_buffer_excess_.data() + dest_buffer_excess_index_, |
| 379 amount); | 383 amount); |
| 380 dest_buffer_excess_index_ += amount; | 384 dest_buffer_excess_index_ += amount; |
| 381 if (dest_buffer_excess_.size() <= dest_buffer_excess_index_) { | 385 if (dest_buffer_excess_.size() <= dest_buffer_excess_index_) { |
| 382 DCHECK(dest_buffer_excess_.size() == dest_buffer_excess_index_); | 386 DCHECK(dest_buffer_excess_.size() == dest_buffer_excess_index_); |
| 383 dest_buffer_excess_.clear(); | 387 dest_buffer_excess_.clear(); |
| 384 dest_buffer_excess_index_ = 0; | 388 dest_buffer_excess_index_ = 0; |
| 385 } | 389 } |
| 386 return amount; | 390 return amount; |
| 387 } | 391 } |
| 388 | 392 |
| 389 } // namespace net | 393 } // namespace net |
| OLD | NEW |