Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 2c6d3c6

Browse files
committed
json: stop changing the content type on ipro'd json files
nginx side of the change: apache/incubator-pagespeed-ngx#1224 Fixes #1321
1 parent ecfdcc8 commit 2c6d3c6

File tree

8 files changed

+29
-10
lines changed

8 files changed

+29
-10
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "example.json",
3+
"contents": "an example of json"
4+
}

net/instaweb/rewriter/in_place_rewrite_context.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ bool RecordingFetch::CanInPlaceRewrite() {
257257
return false;
258258
}
259259
if (type->type() == ContentType::kCss ||
260-
type->IsJs() ||
260+
type->IsJsLike() ||
261261
type->IsImage()) {
262262
RewriteDriver* driver = context_->Driver();
263263
HTTPCache* const cache = driver->server_context()->http_cache();
@@ -483,7 +483,7 @@ RewriteFilter* InPlaceRewriteContext::GetRewriteFilter(
483483
options->Enabled(RewriteOptions::kRewriteCss)) {
484484
return Driver()->FindFilter(RewriteOptions::kCssFilterId);
485485
}
486-
if (type.IsJs() &&
486+
if (type.IsJsLike() &&
487487
options->Enabled(RewriteOptions::kRewriteJavascriptExternal)) {
488488
return Driver()->FindFilter(RewriteOptions::kJavascriptMinId);
489489
}

net/instaweb/rewriter/javascript_filter.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ class JavascriptFilter::Context : public SingleRewriteContext {
312312
// Try to preserve original content type to avoid breaking upstream proxies
313313
// and the like.
314314
const ContentType* content_type = script_resource->type();
315-
if (content_type == nullptr ||
316-
content_type->type() != ContentType::kJavascript) {
315+
if (content_type == nullptr || !content_type->IsJsLike()) {
317316
content_type = &kContentTypeJavascript;
318317
}
319318
if (Driver()->Write(ResourceVector(1, script_resource),

net/instaweb/rewriter/rewrite_context.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3278,7 +3278,7 @@ bool RewriteContext::SendFallbackResponse(StringPiece output_url_base,
32783278
const ContentType* content_type =
32793279
async_fetch->response_headers()->DetermineContentType();
32803280
if (content_type == NULL ||
3281-
!(content_type->IsJs() ||
3281+
!(content_type->IsJsLike() ||
32823282
content_type->IsCss() ||
32833283
content_type->IsImage() ||
32843284
content_type == &kContentTypePdf)) {

pagespeed/kernel/http/content_type.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ bool ContentType::IsCss() const {
133133
return type_ == kCss;
134134
}
135135

136-
bool ContentType::IsJs() const {
136+
bool ContentType::IsJsLike() const {
137137
switch (type_) {
138138
case kJavascript:
139139
case kJson:
@@ -294,7 +294,7 @@ void MimeTypeListToContentTypeSet(
294294

295295
bool ContentType::IsCompressible() const {
296296
// TODO(jcrowell): Investigate images with exif data as compressible.
297-
return IsXmlLike() || IsHtmlLike() || IsJs() || IsCss() || type_ == kText;
297+
return IsXmlLike() || IsHtmlLike() || IsJsLike() || IsCss() || type_ == kText;
298298
}
299299

300300
bool ContentType::IsLikelyStaticResource() const {

pagespeed/kernel/http/content_type.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ struct ContentType {
6767
// Return true iff this content type is CSS.
6868
bool IsCss() const;
6969

70-
// Return true iff this content type is JS.
71-
bool IsJs() const;
70+
// Return true iff this content type is JS, or something similar like JSON.
71+
bool IsJsLike() const;
7272

7373
// Return true iff this content type is HTML, or XHTML, or some other such
7474
// thing (e.g. CE-HTML) that we can rewrite.

pagespeed/system/in_place_resource_recorder.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void InPlaceResourceRecorder::ConsiderResponseHeaders(
188188
if (content_type == NULL ||
189189
!(content_type->IsImage() ||
190190
content_type->IsCss() ||
191-
content_type->IsJs())) {
191+
content_type->IsJsLike())) {
192192
// We remember wrong mimetypes as uncacheable. This is slightly goofy,
193193
// and is inconsistent with how they are treated on normal rewrite path...
194194
DroppedAsUncacheable();

pagespeed/system/system_test.sh

+16
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ echo $WGET_DUMP $TEST_ROOT/ipro/instant/deadline/purple.css
366366
OUT=$($WGET_DUMP $TEST_ROOT/ipro/instant/deadline/purple.css)
367367
check_from "$OUT" fgrep -q 'body{background:#9370db}'
368368

369+
start_test json keeps its content type
370+
URL="$TEST_ROOT/example.json"
371+
OUT=$($WGET_DUMP "$URL?PageSpeed=off")
372+
# Verify that it's application/json without PageSpeed touching it.
373+
check_from "$OUT" grep '^Content-Type: application/json'
374+
OUT=$($WGET_DUMP "$URL")
375+
# Verify that it's application/json on the first PageSpeed load.
376+
check_from "$OUT" grep '^Content-Type: application/json'
377+
# Fetch it repeatedly until it's been IPRO-optimized. This grep command is kind
378+
# of awkward, because fetch_until doesn't do quoting well.
379+
WGET_ARGS="--save-headers" fetch_until -save "$URL" \
380+
"grep -c .title.:.example.json" 1
381+
OUT=$(cat $FETCH_UNTIL_OUTFILE)
382+
# Make sure we didn't change the content type to application/javascript.
383+
check_from "$OUT" grep '^Content-Type: application/json'
384+
369385
start_test ShardDomain directive in per-directory config
370386
fetch_until -save $TEST_ROOT/shard/shard.html 'fgrep -c .pagespeed.ce' 4
371387
check [ $(grep -ce href=\"http://shard1 $FETCH_FILE) = 2 ];

0 commit comments

Comments
 (0)