| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/filter/sdch_filter.h" | |
| 6 | |
| 7 #include <ctype.h> | |
| 8 #include <limits.h> | |
| 9 #include <algorithm> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/metrics/histogram_macros.h" | |
| 14 #include "base/values.h" | |
| 15 #include "net/base/sdch_manager.h" | |
| 16 #include "net/base/sdch_net_log_params.h" | |
| 17 #include "net/base/sdch_problem_codes.h" | |
| 18 #include "net/log/net_log_capture_mode.h" | |
| 19 #include "net/log/net_log_event_type.h" | |
| 20 #include "net/log/net_log_with_source.h" | |
| 21 #include "net/url_request/url_request_context.h" | |
| 22 #include "sdch/open-vcdiff/src/google/vcdecoder.h" | |
| 23 | |
| 24 namespace net { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const size_t kServerIdLength = 9; // Dictionary hash plus null from server. | |
| 29 | |
| 30 // Disambiguate various types of responses that trigger a meta-refresh, | |
| 31 // failure, or fallback to pass-through. | |
| 32 enum ResponseCorruptionDetectionCause { | |
| 33 RESPONSE_NONE, | |
| 34 | |
| 35 // 404 Http Response Code | |
| 36 RESPONSE_404 = 1, | |
| 37 | |
| 38 // Not a 200 Http Response Code | |
| 39 RESPONSE_NOT_200 = 2, | |
| 40 | |
| 41 // Cached before dictionary retrieved. | |
| 42 RESPONSE_OLD_UNENCODED = 3, | |
| 43 | |
| 44 // Speculative but incorrect SDCH filtering was added added. | |
| 45 RESPONSE_TENTATIVE_SDCH = 4, | |
| 46 | |
| 47 // Missing correct dict for decoding. | |
| 48 RESPONSE_NO_DICTIONARY = 5, | |
| 49 | |
| 50 // Not an SDCH response but should be. | |
| 51 RESPONSE_CORRUPT_SDCH = 6, | |
| 52 | |
| 53 // No dictionary was advertised with the request, the server claims | |
| 54 // to have encoded with SDCH anyway, but it isn't an SDCH response. | |
| 55 RESPONSE_ENCODING_LIE = 7, | |
| 56 | |
| 57 RESPONSE_MAX, | |
| 58 }; | |
| 59 | |
| 60 const char* ResponseCorruptionDetectionCauseToString( | |
| 61 ResponseCorruptionDetectionCause cause) { | |
| 62 const char* cause_string = "<unknown>"; | |
| 63 switch (cause) { | |
| 64 case RESPONSE_NONE: | |
| 65 cause_string = "NONE"; | |
| 66 break; | |
| 67 case RESPONSE_404: | |
| 68 cause_string = "404"; | |
| 69 break; | |
| 70 case RESPONSE_NOT_200: | |
| 71 cause_string = "NOT_200"; | |
| 72 break; | |
| 73 case RESPONSE_OLD_UNENCODED: | |
| 74 cause_string = "OLD_UNENCODED"; | |
| 75 break; | |
| 76 case RESPONSE_TENTATIVE_SDCH: | |
| 77 cause_string = "TENTATIVE_SDCH"; | |
| 78 break; | |
| 79 case RESPONSE_NO_DICTIONARY: | |
| 80 cause_string = "NO_DICTIONARY"; | |
| 81 break; | |
| 82 case RESPONSE_CORRUPT_SDCH: | |
| 83 cause_string = "CORRUPT_SDCH"; | |
| 84 break; | |
| 85 case RESPONSE_ENCODING_LIE: | |
| 86 cause_string = "ENCODING_LIE"; | |
| 87 break; | |
| 88 case RESPONSE_MAX: | |
| 89 cause_string = "<Error: max enum value>"; | |
| 90 break; | |
| 91 } | |
| 92 return cause_string; | |
| 93 } | |
| 94 | |
| 95 std::unique_ptr<base::Value> NetLogSdchResponseCorruptionDetectionCallback( | |
| 96 ResponseCorruptionDetectionCause cause, | |
| 97 bool cached, | |
| 98 NetLogCaptureMode capture_mode) { | |
| 99 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 100 dict->SetString("cause", ResponseCorruptionDetectionCauseToString(cause)); | |
| 101 dict->SetBoolean("cached", cached); | |
| 102 return std::move(dict); | |
| 103 } | |
| 104 | |
| 105 } // namespace | |
| 106 | |
| 107 SdchFilter::SdchFilter(FilterType type, const FilterContext& filter_context) | |
| 108 : Filter(type), | |
| 109 filter_context_(filter_context), | |
| 110 decoding_status_(DECODING_UNINITIALIZED), | |
| 111 dictionary_hash_(), | |
| 112 dictionary_hash_is_plausible_(false), | |
| 113 url_request_context_(filter_context.GetURLRequestContext()), | |
| 114 dest_buffer_excess_(), | |
| 115 dest_buffer_excess_index_(0), | |
| 116 source_bytes_(0), | |
| 117 output_bytes_(0), | |
| 118 possible_pass_through_(false) { | |
| 119 bool success = filter_context.GetMimeType(&mime_type_); | |
| 120 DCHECK(success); | |
| 121 success = filter_context.GetURL(&url_); | |
| 122 DCHECK(success); | |
| 123 DCHECK(url_request_context_->sdch_manager()); | |
| 124 } | |
| 125 | |
| 126 SdchFilter::~SdchFilter() { | |
| 127 // All code here is for gathering stats, and can be removed when SDCH is | |
| 128 // considered stable. | |
| 129 | |
| 130 // References to filter_context_ and vcdiff_streaming_decoder_ (which | |
| 131 // contains a reference to the dictionary text) are safe because | |
| 132 // ~URLRequestHttpJob calls URLRequestJob::DestroyFilters, destroying | |
| 133 // this object before the filter context in URLRequestHttpJob and its | |
| 134 // members go out of scope. | |
| 135 | |
| 136 static int filter_use_count = 0; | |
| 137 ++filter_use_count; | |
| 138 if (META_REFRESH_RECOVERY == decoding_status_) { | |
| 139 UMA_HISTOGRAM_COUNTS("Sdch3.FilterUseBeforeDisabling", filter_use_count); | |
| 140 } | |
| 141 | |
| 142 if (vcdiff_streaming_decoder_.get()) { | |
| 143 if (!vcdiff_streaming_decoder_->FinishDecoding()) { | |
| 144 decoding_status_ = DECODING_ERROR; | |
| 145 LogSdchProblem(SDCH_INCOMPLETE_SDCH_CONTENT); | |
| 146 // Make it possible for the user to hit reload, and get non-sdch content. | |
| 147 // Note this will "wear off" quickly enough, and is just meant to assure | |
| 148 // in some rare case that the user is not stuck. | |
| 149 url_request_context_->sdch_manager()->BlacklistDomain( | |
| 150 url_, SDCH_INCOMPLETE_SDCH_CONTENT); | |
| 151 UMA_HISTOGRAM_COUNTS("Sdch3.PartialBytesIn", | |
| 152 static_cast<int>(filter_context_.GetByteReadCount())); | |
| 153 UMA_HISTOGRAM_COUNTS("Sdch3.PartialVcdiffIn", source_bytes_); | |
| 154 UMA_HISTOGRAM_COUNTS("Sdch3.PartialVcdiffOut", output_bytes_); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 if (!dest_buffer_excess_.empty()) { | |
| 159 // Filter chaining error, or premature teardown. | |
| 160 LogSdchProblem(SDCH_UNFLUSHED_CONTENT); | |
| 161 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedBytesIn", | |
| 162 static_cast<int>(filter_context_.GetByteReadCount())); | |
| 163 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedBufferSize", | |
| 164 dest_buffer_excess_.size()); | |
| 165 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedVcdiffIn", source_bytes_); | |
| 166 UMA_HISTOGRAM_COUNTS("Sdch3.UnflushedVcdiffOut", output_bytes_); | |
| 167 } | |
| 168 | |
| 169 if (filter_context_.IsCachedContent()) { | |
| 170 // Not a real error, but it is useful to have this tally. | |
| 171 // TODO(jar): Remove this stat after SDCH stability is validated. | |
| 172 LogSdchProblem(SDCH_CACHE_DECODED); | |
| 173 return; // We don't need timing stats, and we aready got ratios. | |
| 174 } | |
| 175 | |
| 176 switch (decoding_status_) { | |
| 177 case DECODING_IN_PROGRESS: { | |
| 178 if (output_bytes_) { | |
| 179 UMA_HISTOGRAM_PERCENTAGE("Sdch3.Network_Decode_Ratio_a", | |
| 180 static_cast<int>( | |
| 181 (filter_context_.GetByteReadCount() * 100) / output_bytes_)); | |
| 182 UMA_HISTOGRAM_COUNTS("Sdch3.NetworkBytesSavedByCompression", | |
| 183 output_bytes_ - source_bytes_); | |
| 184 } | |
| 185 UMA_HISTOGRAM_COUNTS("Sdch3.Network_Decode_Bytes_VcdiffOut_a", | |
| 186 output_bytes_); | |
| 187 filter_context_.RecordPacketStats(FilterContext::SDCH_DECODE); | |
| 188 | |
| 189 // Allow latency experiments to proceed. | |
| 190 url_request_context_->sdch_manager()->SetAllowLatencyExperiment( | |
| 191 url_, true); | |
| 192 | |
| 193 // Notify successful dictionary usage. | |
| 194 url_request_context_->sdch_manager()->OnDictionaryUsed( | |
| 195 std::string(dictionary_hash_, 0, kServerIdLength - 1)); | |
| 196 | |
| 197 return; | |
| 198 } | |
| 199 case PASS_THROUGH: { | |
| 200 filter_context_.RecordPacketStats(FilterContext::SDCH_PASSTHROUGH); | |
| 201 return; | |
| 202 } | |
| 203 case DECODING_UNINITIALIZED: { | |
| 204 LogSdchProblem(SDCH_UNINITIALIZED); | |
| 205 return; | |
| 206 } | |
| 207 case WAITING_FOR_DICTIONARY_SELECTION: { | |
| 208 LogSdchProblem(SDCH_PRIOR_TO_DICTIONARY); | |
| 209 return; | |
| 210 } | |
| 211 case DECODING_ERROR: { | |
| 212 LogSdchProblem(SDCH_DECODE_ERROR); | |
| 213 return; | |
| 214 } | |
| 215 case META_REFRESH_RECOVERY: { | |
| 216 // Already accounted for when set. | |
| 217 return; | |
| 218 } | |
| 219 } // end of switch. | |
| 220 } | |
| 221 | |
| 222 bool SdchFilter::InitDecoding(Filter::FilterType filter_type) { | |
| 223 if (decoding_status_ != DECODING_UNINITIALIZED) | |
| 224 return false; | |
| 225 | |
| 226 // Handle case where sdch filter is guessed, but not required. | |
| 227 if (FILTER_TYPE_SDCH_POSSIBLE == filter_type) | |
| 228 possible_pass_through_ = true; | |
| 229 | |
| 230 // Initialize decoder only after we have a dictionary in hand. | |
| 231 decoding_status_ = WAITING_FOR_DICTIONARY_SELECTION; | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 235 #ifndef NDEBUG | |
| 236 static const char* kDecompressionErrorHtml = | |
| 237 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>" | |
| 238 "<div style=\"position:fixed;top:0;left:0;width:100%;border-width:thin;" | |
| 239 "border-color:black;border-style:solid;text-align:left;font-family:arial;" | |
| 240 "font-size:10pt;foreground-color:black;background-color:white\">" | |
| 241 "An error occurred. This page will be reloaded shortly. " | |
| 242 "Or press the \"reload\" button now to reload it immediately." | |
| 243 "</div>"; | |
| 244 #else | |
| 245 static const char* kDecompressionErrorHtml = | |
| 246 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>"; | |
| 247 #endif | |
| 248 | |
| 249 Filter::FilterStatus SdchFilter::ReadFilteredData(char* dest_buffer, | |
| 250 int* dest_len) { | |
| 251 int available_space = *dest_len; | |
| 252 *dest_len = 0; // Nothing output yet. | |
| 253 | |
| 254 if (!dest_buffer || available_space <= 0) | |
| 255 return FILTER_ERROR; | |
| 256 | |
| 257 if (WAITING_FOR_DICTIONARY_SELECTION == decoding_status_) { | |
| 258 FilterStatus status = InitializeDictionary(); | |
| 259 if (FILTER_NEED_MORE_DATA == status) | |
| 260 return FILTER_NEED_MORE_DATA; | |
| 261 if (FILTER_ERROR == status) { | |
| 262 DCHECK_EQ(DECODING_ERROR, decoding_status_); | |
| 263 DCHECK_EQ(0u, dest_buffer_excess_index_); | |
| 264 DCHECK(dest_buffer_excess_.empty()); | |
| 265 // This is where we try very hard to do error recovery, and make this | |
| 266 // protocol robust in the face of proxies that do many different things. | |
| 267 // If we decide that things are looking very bad (too hard to recover), | |
| 268 // we may even issue a "meta-refresh" to reload the page without an SDCH | |
| 269 // advertisement (so that we are sure we're not hurting anything). | |
| 270 // | |
| 271 // Watch out for an error page inserted by the proxy as part of a 40x | |
| 272 // error response. When we see such content molestation, we certainly | |
| 273 // need to fall into the meta-refresh case. | |
| 274 ResponseCorruptionDetectionCause cause = RESPONSE_NONE; | |
| 275 if (filter_context_.GetResponseCode() == 404) { | |
| 276 // We could be more generous, but for now, only a "NOT FOUND" code will | |
| 277 // cause a pass through. All other bad codes will fall into a | |
| 278 // meta-refresh. | |
| 279 LogSdchProblem(SDCH_PASS_THROUGH_404_CODE); | |
| 280 cause = RESPONSE_404; | |
| 281 decoding_status_ = PASS_THROUGH; | |
| 282 } else if (filter_context_.GetResponseCode() != 200) { | |
| 283 // We need to meta-refresh, with SDCH disabled. | |
| 284 cause = RESPONSE_NOT_200; | |
| 285 } else if (filter_context_.IsCachedContent() | |
| 286 && !dictionary_hash_is_plausible_) { | |
| 287 // We must have hit the back button, and gotten content that was fetched | |
| 288 // before we *really* advertised SDCH and a dictionary. | |
| 289 LogSdchProblem(SDCH_PASS_THROUGH_OLD_CACHED); | |
| 290 decoding_status_ = PASS_THROUGH; | |
| 291 cause = RESPONSE_OLD_UNENCODED; | |
| 292 } else if (possible_pass_through_) { | |
| 293 // This is the potentially most graceful response. There really was no | |
| 294 // error. We were just overly cautious when we added a TENTATIVE_SDCH. | |
| 295 // We added the sdch coding tag, and it should not have been added. | |
| 296 // This can happen in server experiments, where the server decides | |
| 297 // not to use sdch, even though there is a dictionary. To be | |
| 298 // conservative, we locally added the tentative sdch (fearing that a | |
| 299 // proxy stripped it!) and we must now recant (pass through). | |
| 300 // | |
| 301 // However.... just to be sure we don't get burned by proxies that | |
| 302 // re-compress with gzip or other system, we can sniff to see if this | |
| 303 // is compressed data etc. For now, we do nothing, which gets us into | |
| 304 // the meta-refresh result. | |
| 305 // TODO(jar): Improve robustness by sniffing for valid text that we can | |
| 306 // actual use re: decoding_status_ = PASS_THROUGH; | |
| 307 cause = RESPONSE_TENTATIVE_SDCH; | |
| 308 } else if (dictionary_hash_is_plausible_) { | |
| 309 // We need a meta-refresh since we don't have the dictionary. | |
| 310 // The common cause is a restart of the browser, where we try to render | |
| 311 // cached content that was saved when we had a dictionary. | |
| 312 cause = RESPONSE_NO_DICTIONARY; | |
| 313 } else if (filter_context_.SdchDictionariesAdvertised()) { | |
| 314 // This is a very corrupt SDCH request response. We can't decode it. | |
| 315 // We'll use a meta-refresh, and get content without asking for SDCH. | |
| 316 // This will also progressively disable SDCH for this domain. | |
| 317 cause = RESPONSE_CORRUPT_SDCH; | |
| 318 } else { | |
| 319 // One of the first 9 bytes precluded consideration as a hash. | |
| 320 // This can't be an SDCH payload, even though the server said it was. | |
| 321 // This is a major error, as the server or proxy tagged this SDCH even | |
| 322 // though it is not! | |
| 323 // Meta-refresh won't help, as we didn't advertise an SDCH dictionary!! | |
| 324 // Worse yet, meta-refresh could lead to an infinite refresh loop. | |
| 325 LogSdchProblem(SDCH_PASSING_THROUGH_NON_SDCH); | |
| 326 decoding_status_ = PASS_THROUGH; | |
| 327 // ... but further back-off on advertising SDCH support. | |
| 328 url_request_context_->sdch_manager()->BlacklistDomain( | |
| 329 url_, SDCH_PASSING_THROUGH_NON_SDCH); | |
| 330 cause = RESPONSE_ENCODING_LIE; | |
| 331 } | |
| 332 DCHECK_NE(RESPONSE_NONE, cause); | |
| 333 | |
| 334 // Use if statement rather than ?: because UMA_HISTOGRAM_ENUMERATION | |
| 335 // caches the histogram name based on the call site. | |
| 336 if (filter_context_.IsCachedContent()) { | |
| 337 UMA_HISTOGRAM_ENUMERATION( | |
| 338 "Sdch3.ResponseCorruptionDetection.Cached", cause, RESPONSE_MAX); | |
| 339 } else { | |
| 340 UMA_HISTOGRAM_ENUMERATION( | |
| 341 "Sdch3.ResponseCorruptionDetection.Uncached", cause, RESPONSE_MAX); | |
| 342 } | |
| 343 filter_context_.GetNetLog().AddEvent( | |
| 344 NetLogEventType::SDCH_RESPONSE_CORRUPTION_DETECTION, | |
| 345 base::Bind(&NetLogSdchResponseCorruptionDetectionCallback, cause, | |
| 346 filter_context_.IsCachedContent())); | |
| 347 | |
| 348 if (decoding_status_ == PASS_THROUGH) { | |
| 349 dest_buffer_excess_ = dictionary_hash_; // Send what we scanned. | |
| 350 } else { | |
| 351 // This is where we try to do the expensive meta-refresh. | |
| 352 if (std::string::npos == mime_type_.find("text/html")) { | |
| 353 // Since we can't do a meta-refresh (along with an exponential | |
| 354 // backoff), we'll just make sure this NEVER happens again. | |
| 355 SdchProblemCode problem = (filter_context_.IsCachedContent() | |
| 356 ? SDCH_CACHED_META_REFRESH_UNSUPPORTED | |
| 357 : SDCH_META_REFRESH_UNSUPPORTED); | |
| 358 url_request_context_->sdch_manager()->BlacklistDomainForever( | |
| 359 url_, problem); | |
| 360 LogSdchProblem(problem); | |
| 361 return FILTER_ERROR; | |
| 362 } | |
| 363 // HTML content means we can issue a meta-refresh, and get the content | |
| 364 // again, perhaps without SDCH (to be safe). | |
| 365 if (filter_context_.IsCachedContent()) { | |
| 366 // Cached content is probably a startup tab, so we'll just get fresh | |
| 367 // content and try again, without disabling sdch. | |
| 368 LogSdchProblem(SDCH_META_REFRESH_CACHED_RECOVERY); | |
| 369 } else { | |
| 370 // Since it wasn't in the cache, we definately need at least some | |
| 371 // period of blacklisting to get the correct content. | |
| 372 url_request_context_->sdch_manager()->BlacklistDomain( | |
| 373 url_, SDCH_META_REFRESH_RECOVERY); | |
| 374 LogSdchProblem(SDCH_META_REFRESH_RECOVERY); | |
| 375 } | |
| 376 decoding_status_ = META_REFRESH_RECOVERY; | |
| 377 // Issue a meta redirect with SDCH disabled. | |
| 378 dest_buffer_excess_ = kDecompressionErrorHtml; | |
| 379 } | |
| 380 } else { | |
| 381 DCHECK_EQ(DECODING_IN_PROGRESS, decoding_status_); | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 int amount = OutputBufferExcess(dest_buffer, available_space); | |
| 386 *dest_len += amount; | |
| 387 dest_buffer += amount; | |
| 388 available_space -= amount; | |
| 389 DCHECK_GE(available_space, 0); | |
| 390 | |
| 391 if (available_space <= 0) | |
| 392 return FILTER_OK; | |
| 393 DCHECK(dest_buffer_excess_.empty()); | |
| 394 DCHECK_EQ(0u, dest_buffer_excess_index_); | |
| 395 | |
| 396 if (decoding_status_ != DECODING_IN_PROGRESS) { | |
| 397 if (META_REFRESH_RECOVERY == decoding_status_) { | |
| 398 // Absorb all input data. We've already output page reload HTML. | |
| 399 next_stream_data_ = NULL; | |
| 400 stream_data_len_ = 0; | |
| 401 return FILTER_NEED_MORE_DATA; | |
| 402 } | |
| 403 if (PASS_THROUGH == decoding_status_) { | |
| 404 // We must pass in available_space, but it will be changed to bytes_used. | |
| 405 FilterStatus result = CopyOut(dest_buffer, &available_space); | |
| 406 // Accumulate the returned count of bytes_used (a.k.a., available_space). | |
| 407 *dest_len += available_space; | |
| 408 return result; | |
| 409 } | |
| 410 DCHECK(false); | |
| 411 decoding_status_ = DECODING_ERROR; | |
| 412 return FILTER_ERROR; | |
| 413 } | |
| 414 | |
| 415 if (!next_stream_data_ || stream_data_len_ <= 0) | |
| 416 return FILTER_NEED_MORE_DATA; | |
| 417 | |
| 418 // A note on accounting: DecodeChunk() appends to its output buffer, so any | |
| 419 // preexisting data in |dest_buffer_excess_| could skew the value of | |
| 420 // |output_bytes_|. However, OutputBufferExcess guarantees that it will | |
| 421 // consume all of |dest_buffer_excess_| when called above unless the | |
| 422 // destination buffer runs out of space, and if the destination buffer runs | |
| 423 // out of space, this code returns FILTER_OK early above. Therefore, if | |
| 424 // execution reaches this point, |dest_buffer_excess_| is empty, which is | |
| 425 // DCHECKed above. | |
| 426 bool ret = vcdiff_streaming_decoder_->DecodeChunk( | |
| 427 next_stream_data_, stream_data_len_, &dest_buffer_excess_); | |
| 428 // Assume all data was used in decoding. | |
| 429 next_stream_data_ = NULL; | |
| 430 source_bytes_ += stream_data_len_; | |
| 431 stream_data_len_ = 0; | |
| 432 output_bytes_ += dest_buffer_excess_.size(); | |
| 433 if (!ret) { | |
| 434 vcdiff_streaming_decoder_.reset(NULL); // Don't call it again. | |
| 435 decoding_status_ = DECODING_ERROR; | |
| 436 LogSdchProblem(SDCH_DECODE_BODY_ERROR); | |
| 437 return FILTER_ERROR; | |
| 438 } | |
| 439 | |
| 440 amount = OutputBufferExcess(dest_buffer, available_space); | |
| 441 *dest_len += amount; | |
| 442 dest_buffer += amount; | |
| 443 available_space -= amount; | |
| 444 if (0 == available_space && !dest_buffer_excess_.empty()) | |
| 445 return FILTER_OK; | |
| 446 return FILTER_NEED_MORE_DATA; | |
| 447 } | |
| 448 | |
| 449 Filter::FilterStatus SdchFilter::InitializeDictionary() { | |
| 450 size_t bytes_needed = kServerIdLength - dictionary_hash_.size(); | |
| 451 DCHECK_GT(bytes_needed, 0u); | |
| 452 if (!next_stream_data_) | |
| 453 return FILTER_NEED_MORE_DATA; | |
| 454 if (static_cast<size_t>(stream_data_len_) < bytes_needed) { | |
| 455 dictionary_hash_.append(next_stream_data_, stream_data_len_); | |
| 456 next_stream_data_ = NULL; | |
| 457 stream_data_len_ = 0; | |
| 458 return FILTER_NEED_MORE_DATA; | |
| 459 } | |
| 460 dictionary_hash_.append(next_stream_data_, bytes_needed); | |
| 461 DCHECK(kServerIdLength == dictionary_hash_.size()); | |
| 462 stream_data_len_ -= bytes_needed; | |
| 463 DCHECK_LE(0, stream_data_len_); | |
| 464 if (stream_data_len_ > 0) | |
| 465 next_stream_data_ += bytes_needed; | |
| 466 else | |
| 467 next_stream_data_ = NULL; | |
| 468 | |
| 469 const std::string* dictionary_text = nullptr; | |
| 470 dictionary_hash_is_plausible_ = true; // Assume plausible, but check. | |
| 471 | |
| 472 SdchProblemCode rv = SDCH_OK; | |
| 473 if ('\0' == dictionary_hash_[kServerIdLength - 1]) { | |
| 474 std::string server_hash(dictionary_hash_, 0, kServerIdLength - 1); | |
| 475 SdchManager::DictionarySet* handle = | |
| 476 filter_context_.SdchDictionariesAdvertised(); | |
| 477 if (handle) | |
| 478 dictionary_text = handle->GetDictionaryText(server_hash); | |
| 479 if (!dictionary_text) { | |
| 480 // This is a hack. Naively, the dictionaries available for | |
| 481 // decoding should be only the ones advertised. However, there are | |
| 482 // cases, specifically resources encoded with old dictionaries living | |
| 483 // in the cache, that mean the full set of dictionaries should be made | |
| 484 // available for decoding. It's not known how often this happens; | |
| 485 // if it happens rarely enough, this code can be removed. | |
| 486 // | |
| 487 // TODO(rdsmith): Long-term, a better solution is necessary, since | |
| 488 // an entry in the cache being encoded with the dictionary doesn't | |
| 489 // guarantee that the dictionary is present. That solution probably | |
| 490 // involves storing unencoded resources in the cache, but might | |
| 491 // involve evicting encoded resources on dictionary removal. | |
| 492 // See http://crbug.com/383405. | |
| 493 unexpected_dictionary_handle_ = | |
| 494 url_request_context_->sdch_manager()->GetDictionarySetByHash( | |
| 495 url_, server_hash, &rv); | |
| 496 if (unexpected_dictionary_handle_) { | |
| 497 dictionary_text = | |
| 498 unexpected_dictionary_handle_->GetDictionaryText(server_hash); | |
| 499 // Override SDCH_OK rv; this is still worth logging. | |
| 500 rv = (filter_context_.IsCachedContent() ? | |
| 501 SDCH_UNADVERTISED_DICTIONARY_USED_CACHED : | |
| 502 SDCH_UNADVERTISED_DICTIONARY_USED); | |
| 503 } else { | |
| 504 // Since dictionary was not found, check to see if hash was | |
| 505 // even plausible. | |
| 506 DCHECK(dictionary_hash_.size() == kServerIdLength); | |
| 507 rv = SDCH_DICTIONARY_HASH_NOT_FOUND; | |
| 508 for (size_t i = 0; i < kServerIdLength - 1; ++i) { | |
| 509 char base64_char = dictionary_hash_[i]; | |
| 510 if (!isalnum(base64_char) && | |
| 511 '-' != base64_char && '_' != base64_char) { | |
| 512 dictionary_hash_is_plausible_ = false; | |
| 513 rv = SDCH_DICTIONARY_HASH_MALFORMED; | |
| 514 break; | |
| 515 } | |
| 516 } | |
| 517 } | |
| 518 } | |
| 519 } else { | |
| 520 dictionary_hash_is_plausible_ = false; | |
| 521 rv = SDCH_DICTIONARY_HASH_MALFORMED; | |
| 522 } | |
| 523 | |
| 524 if (rv != SDCH_OK) | |
| 525 LogSdchProblem(rv); | |
| 526 | |
| 527 if (!dictionary_text) { | |
| 528 decoding_status_ = DECODING_ERROR; | |
| 529 return FILTER_ERROR; | |
| 530 } | |
| 531 | |
| 532 vcdiff_streaming_decoder_.reset(new open_vcdiff::VCDiffStreamingDecoder); | |
| 533 vcdiff_streaming_decoder_->SetAllowVcdTarget(false); | |
| 534 | |
| 535 // The validity of the dictionary_text pointer is guaranteed for the | |
| 536 // lifetime of the SdchFilter by the ownership of the DictionarySet by | |
| 537 // the FilterContext/URLRequestHttpJob. All URLRequestJob filters are | |
| 538 // torn down in ~URLRequestHttpJob by a call to | |
| 539 // URLRequestJob::DestroyFilters. | |
| 540 vcdiff_streaming_decoder_->StartDecoding(dictionary_text->data(), | |
| 541 dictionary_text->size()); | |
| 542 decoding_status_ = DECODING_IN_PROGRESS; | |
| 543 return FILTER_OK; | |
| 544 } | |
| 545 | |
| 546 int SdchFilter::OutputBufferExcess(char* const dest_buffer, | |
| 547 size_t available_space) { | |
| 548 if (dest_buffer_excess_.empty()) | |
| 549 return 0; | |
| 550 DCHECK(dest_buffer_excess_.size() > dest_buffer_excess_index_); | |
| 551 size_t amount = std::min(available_space, | |
| 552 dest_buffer_excess_.size() - dest_buffer_excess_index_); | |
| 553 memcpy(dest_buffer, dest_buffer_excess_.data() + dest_buffer_excess_index_, | |
| 554 amount); | |
| 555 dest_buffer_excess_index_ += amount; | |
| 556 if (dest_buffer_excess_.size() <= dest_buffer_excess_index_) { | |
| 557 DCHECK(dest_buffer_excess_.size() == dest_buffer_excess_index_); | |
| 558 dest_buffer_excess_.clear(); | |
| 559 dest_buffer_excess_index_ = 0; | |
| 560 } | |
| 561 return amount; | |
| 562 } | |
| 563 | |
| 564 void SdchFilter::LogSdchProblem(SdchProblemCode problem) { | |
| 565 SdchManager::SdchErrorRecovery(problem); | |
| 566 filter_context_.GetNetLog().AddEvent( | |
| 567 NetLogEventType::SDCH_DECODING_ERROR, | |
| 568 base::Bind(&NetLogSdchResourceProblemCallback, problem)); | |
| 569 } | |
| 570 | |
| 571 } // namespace net | |
| OLD | NEW |