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