Commit fa7e141
Fix: parse Accept header q-values in wpsc_get_accept_header() (#1057)
* Fix: parse Accept header q-values in wpsc_get_accept_header()
The previous implementation used a naive str_contains() check: if any
known JSON media type appeared anywhere in the Accept header string, the
request was classified as application/json. This ignored RFC 7231 §5.3.2
quality values (q=), so a valid HTML-preferring header such as the one
sent by New Relic Synthetics —
Accept: text/html,application/xhtml+xml,application/json;q=0.9,...
— was misclassified as a JSON request. Two downstream effects:
1. wp_cache_serve_cache_file() refused to serve the cached file.
2. A separate application/json cache bucket was populated on every
synthetic check, triggering a fresh page build each time.
This commit extracts a new wpsc_parse_accept_header() helper that
parses q-values and classifies the request as application/json only
when a known JSON type has a strictly higher q-value than text/html.
Ties resolve to text/html (safe default: serve the cached page).
The wpsc_accept_headers filter continues to work — filtered types
participate in the q-value comparison as JSON types.
Fixes #1045
* fix: drop */* wildcard fallback from wpsc_parse_accept_header()
JSON/Fediverse clients commonly send Accept: application/json, */*. The
*/* wildcard was lifting effective_html_q to 1.0 when text/html was absent,
causing ties that resolved to text/html — serving cached HTML to clients
that explicitly requested JSON.
Fix: ignore */* entirely when computing effective_html_q. Use only the
explicit text/html q-value, defaulting to 0.0 when absent. This preserves
the New Relic Synthetics case (explicit text/html;q=1.0 beats json;q=0.9)
while correctly routing application/json, */* to the application layer.
Test changes:
- Rename and flip test_wildcard_covers_html_when_not_explicit to
test_wildcard_does_not_cover_html_when_not_explicit (asserts JSON)
- Add test_fediverse_json_with_wildcard_classifies_as_json as a
regression guard for the specific Fediverse case from the review
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: address CI failures — PHPUnit attribute, PHPCS, stale comments
PHPUnit (PHP 8.2): replace @Covers doc-comment with #[CoversFunction]
attribute. phpunit.11.xml.dist sets failOnPhpunitDeprecation=true and
PHPUnit 11 (used on 8.2) still reads doc-comment metadata and emits a
deprecation; PHPUnit 12 (8.3+) ignores it. The attribute works on both.
PHPCS: the apply_filters() stub triggered "file should contain either
function declarations or OO structure declarations, but not both". Moved
the stub to tests/php/bootstrap.php (where it belongs — the stub is a
test infrastructure concern, not part of the test class) and fixed the
bootstrap @Package tag while there.
Remaining PHPCS fixes in WpscParseAcceptHeaderTest.php:
- $json_types property: add @var string[] to the docblock
- Four wildcard-behaviour method comments: // → /** */
- Lowercase short description: "application/activity+json classified"
→ "Classifies application/activity+json as JSON"
- Stale inline comment in test_malformed_q_value_treated_as_default:
removed the "effective html_q from */*=0.7" reasoning (wildcard no
longer applies); updated to "text/html absent → effective_html_q = 0.0"
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Christopher Ross <thisismyurl@users.noreply.github.com>
Co-authored-by: Christopher Ross <184154038+thisismyurl@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 6ff7c35 commit fa7e141
3 files changed
Lines changed: 187 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
8 | 16 | | |
9 | 17 | | |
10 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
537 | 537 | | |
538 | 538 | | |
539 | 539 | | |
540 | | - | |
541 | | - | |
| 540 | + | |
| 541 | + | |
542 | 542 | | |
543 | | - | |
544 | | - | |
545 | | - | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
546 | 587 | | |
547 | 588 | | |
548 | 589 | | |
549 | | - | |
550 | | - | |
| 590 | + | |
| 591 | + | |
551 | 592 | | |
552 | 593 | | |
553 | | - | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
554 | 597 | | |
555 | 598 | | |
556 | | - | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
557 | 605 | | |
558 | 606 | | |
559 | 607 | | |
| |||
0 commit comments