Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't store webp-animated and non-webp capable image opts under same …
…cache key.

Change the non-webp cache key to avoid picking up any that we previously messed up.
Also restructure the code a bit to lower the risk of this occuring again.

Part of issue #1216
  • Loading branch information
morlovich authored and jeffkaufman committed Dec 15, 2015
1 parent 0f77262 commit e398794
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
22 changes: 22 additions & 0 deletions net/instaweb/rewriter/image_rewrite_filter_test.cc
Expand Up @@ -3740,4 +3740,26 @@ TEST_F(ImageRewriteTest, GifToWebpLosslessWithWebpAnimatedUa) {
true);
}

TEST_F(ImageRewriteTest, AnimatedNoCacheReuse) {
// Make sure we don't reuse results for animated webp-capable UAs for
// non-webp targets.
AddFileToMockFetcher(StrCat(kTestDomain, "a.jpeg"), kPuzzleJpgFile,
kContentTypeJpeg, 100);

options()->EnableFilter(RewriteOptions::kConvertJpegToWebp);
options()->EnableFilter(RewriteOptions::kConvertToWebpAnimated);
options()->set_image_recompress_quality(85);
rewrite_driver()->AddFilters();

// WebP capable browser --- made a WebP image.
SetCurrentUserAgent("webp-animated");
ValidateExpected("webp broswer", "<img src=a.jpeg>",
"<img src=xa.jpeg.pagespeed.ic.0.webp>");
ClearRewriteDriver();

// Not a WebP browser -- don't!
SetCurrentUserAgent("curl");
ValidateNoChanges("non-webp broswer", "<img src=a.jpeg>");
}

} // namespace net_instaweb
24 changes: 17 additions & 7 deletions net/instaweb/rewriter/image_url_encoder.cc
Expand Up @@ -42,6 +42,10 @@ const char kMissingDimension = 'N';
// Constants for UserAgent cache key enteries.
const char kWebpLossyUserAgentKey[] = "w";
const char kWebpLossyLossLessAlphaUserAgentKey[] = "v";
const char kWebpAnimatedUserAgentKey[] = "a";
// This used to not have a separate key, but we mixed up animated and it
// at one point, so this is now here to force a flush.
const char kWebpNoneUserAgentKey[] = ".";
const char kMobileUserAgentKey[] = "m";
const char kSmallScreenKey[] = "ss";

Expand Down Expand Up @@ -305,13 +309,19 @@ void ImageUrlEncoder::SetSmallScreen(const RewriteDriver& driver,
GoogleString ImageUrlEncoder::CacheKeyFromResourceContext(
const ResourceContext& resource_context) {
GoogleString user_agent_cache_key = "";
if (resource_context.libwebp_level() ==
ResourceContext::LIBWEBP_LOSSY_LOSSLESS_ALPHA) {
StrAppend(&user_agent_cache_key, kWebpLossyLossLessAlphaUserAgentKey);
}
if (resource_context.libwebp_level() ==
ResourceContext::LIBWEBP_LOSSY_ONLY) {
StrAppend(&user_agent_cache_key, kWebpLossyUserAgentKey);
switch (resource_context.libwebp_level()) {
case ResourceContext::LIBWEBP_NONE:
StrAppend(&user_agent_cache_key, kWebpNoneUserAgentKey);
break;
case ResourceContext::LIBWEBP_LOSSY_LOSSLESS_ALPHA:
StrAppend(&user_agent_cache_key, kWebpLossyLossLessAlphaUserAgentKey);
break;
case ResourceContext::LIBWEBP_LOSSY_ONLY:
StrAppend(&user_agent_cache_key, kWebpLossyUserAgentKey);
break;
case ResourceContext::LIBWEBP_ANIMATED:
StrAppend(&user_agent_cache_key, kWebpAnimatedUserAgentKey);
break;
}
if (resource_context.mobile_user_agent()) {
StrAppend(&user_agent_cache_key, kMobileUserAgentKey);
Expand Down
20 changes: 19 additions & 1 deletion net/instaweb/rewriter/image_url_encoder_test.cc
Expand Up @@ -363,7 +363,25 @@ TEST_F(ImageUrlEncoderTest, SmallScreen) {
context.set_use_small_screen_quality(true);
GoogleString cache_key =
ImageUrlEncoder::CacheKeyFromResourceContext(context);
EXPECT_EQ("ss", cache_key);
EXPECT_EQ(".ss", cache_key);
}

TEST_F(ImageUrlEncoderTest, DifferentWebpLevels) {
// Make sure different levels of WebP support get different cache keys.
std::set<GoogleString> seen;
for (int webp_level = ResourceContext::LibWebpLevel_MIN;
webp_level <= ResourceContext::LibWebpLevel_MAX;
++webp_level) {
if (ResourceContext::LibWebpLevel_IsValid(webp_level)) {
ResourceContext ctx;
ctx.set_libwebp_level(
static_cast<ResourceContext::LibWebpLevel>(webp_level));
GoogleString cache_key =
ImageUrlEncoder::CacheKeyFromResourceContext(ctx);
EXPECT_EQ(seen.find(cache_key), seen.end());
seen.insert(cache_key);
}
}
}

TEST_F(ImageUrlEncoderTest, BadFirst) {
Expand Down

0 comments on commit e398794

Please sign in to comment.