r59522 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59521‎ | r59522 | r59523 >
Date:19:13, 28 November 2009
Author:philip
Status:resolved (Comments)
Tags:
Comment:
bug 21672: Add Accept-Language to Vary and XVO headers to help Squid to determine user's perferred local language which has been eagerly wanted in Chinese Wikipedia.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -50,6 +50,8 @@
5151
5252 private $mIndexPolicy = 'index';
5353 private $mFollowPolicy = 'follow';
 54+ private $mVaryHeader = array( 'Accept-Encoding', 'Cookie' );
 55+ private $mXVOHeader = array( 'Accept-Encoding' => array('list-contains=gzip') );
5456
5557 /**
5658 * Constructor
@@ -805,19 +807,45 @@
806808 return false;
807809 }
808810
 811+ public function addXVOHeader( $header, $option = null ) {
 812+ if ( !array_key_exists( $header, $this->mXVOHeader ) ) {
 813+ $this->mXVOHeader[$header] = $option;
 814+ }
 815+ elseif( is_array( $option ) ) {
 816+ if( is_array( $this->mXVOHeader[$header] ) ) {
 817+ $this->mXVOHeader[$header] = array_merge( $this->mXVOHeader[$header], $option );
 818+ }
 819+ else {
 820+ $this->mXVOHeader[$header] = $option;
 821+ }
 822+ }
 823+ }
 824+
 825+ public function addVaryHeader( $header ) {
 826+ if ( !in_array( $header, $this->mVaryHeader ) ) {
 827+ $this->mVaryHeader[] = $header;
 828+ }
 829+ }
 830+
809831 /** Get a complete X-Vary-Options header */
810832 public function getXVO() {
811833 $cvCookies = $this->getCacheVaryCookies();
812 - $xvo = 'X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;';
813 - $first = true;
 834+
 835+ $cookiesOption = array();
814836 foreach ( $cvCookies as $cookieName ) {
815 - if ( $first ) {
816 - $first = false;
817 - } else {
818 - $xvo .= ';';
819 - }
820 - $xvo .= 'string-contains=' . $cookieName;
 837+ $cookiesOption[] = 'string-contains=' . $cookieName;
821838 }
 839+ $this->addXVOHeader( 'Cookie', $cookiesOption );
 840+
 841+ $headers = array();
 842+ foreach( $this->mXVOHeader as $header => $option ) {
 843+ $newheader = $header;
 844+ if( is_array( $option ) )
 845+ $newheader .= ';' . implode( ';', $option );
 846+ $headers[] = $newheader;
 847+ }
 848+ $xvo = 'X-Vary-Options: ' . implode( ',', $headers );
 849+
822850 return $xvo;
823851 }
824852
@@ -830,7 +858,7 @@
831859
832860 # don't serve compressed data to clients who can't handle it
833861 # maintain different caches for logged-in users and non-logged in ones
834 - $response->header( 'Vary: Accept-Encoding, Cookie' );
 862+ $response->header( 'Vary: ' . join( ', ', $this->mVaryHeader ) );
835863
836864 if ( $wgUseXVO ) {
837865 # Add an X-Vary-Options header for Squid with Wikimedia patches
Index: trunk/phase3/languages/LanguageConverter.php
@@ -185,6 +185,12 @@
186186 // variable in case this is called before the user's
187187 // preference is loaded
188188 if( array_key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) ) {
 189+ // bug 21672: Add Accept-Language to Vary and XVO headers
 190+ // to help Squid to determine user's perferred local language
 191+ global $wgOut, $wgUseXVO;
 192+ $wgOut->addVaryHeader( 'Accept-Language' );
 193+ if( $wgUseXVO )
 194+ $wgOut->addXVOHeader( 'Accept-Language' );
189195 $acceptLanguage = strtolower( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
190196
191197 // explode by comma
Index: trunk/phase3/RELEASE-NOTES
@@ -649,6 +649,7 @@
650650 * (bug 21403) memcached class renamed to MWMemecached to avoid conflict with
651651 PHP's memcached extension
652652 * (bug 21650) Both calls to SkinTemplateTabs hook are now compatible
 653+* (bug 21672) Add missing Accept-Language to both Vary and XVO headers
653654
654655 == API changes in 1.16 ==
655656

Follow-up revisions

RevisionCommit summaryAuthorDate
r59523follow-up r59522, add missing credit.philip19:15, 28 November 2009
r59527follow-up r59522. ONLY add Accept-Language when a valid variant has been foun...philip20:00, 28 November 2009
r59529follow-up r59522. "string-contains" added.philip20:13, 28 November 2009
r59530follow-up r59522, r59529. Just include the current variant.philip20:20, 28 November 2009
r59541follow-up r59522, r59523, r59527, r59529, r59530....philip06:47, 29 November 2009
r59735follow-up r59522 and r59541. To make the condition when we'll use Accept-Lang...philip15:47, 4 December 2009
r59754follow-up r59522 and r59735. only redirect to a variant URL when logged out.philip05:39, 5 December 2009
r60272follow-up r59522. follow Tim's suggestion to serve same vary and XVO headers ...philip18:55, 21 December 2009

Comments

#Comment by Catrope (talk | contribs)   19:18, 28 November 2009

If I understand XVO correctly, this revision tells Squid that any request with an Accept-Language: header (i.e. pretty much every request to wikis using LanguageConverter) is uncacheable. I'm pretty sure this is not acceptable.

A better solution would be to add another hack to Squid's XVO handling so it can instruct Squid to cache separate versions of a page based on a header; right now, all it does is a binary "either cache or don't cache" switch AFAIK.

#Comment by PhiLiP (talk | contribs)   19:42, 28 November 2009

trying to fix it now.

#Comment by PhiLiP (talk | contribs)   20:01, 28 November 2009

Fixed on r59527. Now only Accept-Language contains valid variants will be applied.

#Comment by PhiLiP (talk | contribs)   20:14, 28 November 2009

r59529: "string-contains" added.

#Comment by PhiLiP (talk | contribs)   20:21, 28 November 2009

r59530: Just include the current variant in "string-contains".

#Comment by Tim Starling (talk | contribs)   04:15, 21 December 2009

You can't serve different vary (or XVO) headers for the same URL. If you do that, Squid will serve a cache miss every time the vary header changes, and attempt (IIRC it is buggy in this case) to delete the cache entry. The patch would be OK if it set the vary header depending only on $wgContLang and the URL.

Catrope is incorrect, Squid most certainly can store multiple versions of a page for the same URL. We use this feature for Accept-Encoding. We can do it for Accept-Language as well. However, as I say, it only works if the vary headers are consistent.

#Comment by PhiLiP (talk | contribs)   18:59, 21 December 2009

Tim, I followed your suggestion to commit r60272 to make the vary and XVO headers to be consistent in the same URL. Please help review that again, thank you very much!

#Comment by Tim Starling (talk | contribs)   05:07, 12 January 2010

Looks good.

Status & tagging log