SDK doc change: update gae request handler to use new intl/nn handling behaviors. Now always honors any locally set lang cookie and redirects unsupported url langs to en version.

Change-Id: Id778ba96936c6ed5c3c5ec6c76e476f1fa7036bf
This commit is contained in:
Dirk Dougherty
2010-04-09 18:45:51 -07:00
parent 3356ba35fa
commit 35ecf6a8b7

View File

@@ -115,6 +115,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
urlLangName = None urlLangName = None
retry = False retry = False
isValidIntl = False isValidIntl = False
isStripped = False
# Try to retrieve the user's lang pref from the cookie. If there is no # Try to retrieve the user's lang pref from the cookie. If there is no
# lang pref cookie in the request, add set-cookie to the response with the # lang pref cookie in the request, add set-cookie to the response with the
@@ -124,7 +125,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
except KeyError: except KeyError:
resetLangCookie = True resetLangCookie = True
#logging.info('==========================EXCEPTION: NO LANG COOKIE FOUND, USING [%s]', langName) #logging.info('==========================EXCEPTION: NO LANG COOKIE FOUND, USING [%s]', langName)
logging.info('==========================REQ INIT name [%s] langName [%s]', reqUri, langName) logging.info('==========================REQ INIT name [%s] langName [%s] resetLangCookie [%s]', reqUri, langName, resetLangCookie)
# Preprocess the req url. If it references a directory or the domain itself, # Preprocess the req url. If it references a directory or the domain itself,
# append '/index.html' to the url and 302 redirect. Otherwise, continue # append '/index.html' to the url and 302 redirect. Otherwise, continue
@@ -142,19 +143,23 @@ class MemcachedZipHandler(webapp.RequestHandler):
if isValidIntl: if isValidIntl:
urlLangName = sections[1] urlLangName = sections[1]
contentUri = sections[2] contentUri = sections[2]
if (langName != urlLangName): logging.info(' Content URI is [%s]...', contentUri)
if (urlLangName != langName) or (langName == 'en'):
# if the lang code in the request is different from that in # if the lang code in the request is different from that in
# the cookie, reset the cookie to the url lang value. # the cookie, or if the target lang is en, strip the
langName = urlLangName # intl/nn substring. It will later be redirected to
resetLangCookie = True # the user's preferred language url.
#logging.info('INTL PREP resetting langName to urlLangName [%s]', langName) # logging.info(' Handling a MISMATCHED intl request')
#else: name = contentUri
# logging.info('INTL PREP no need to reset langName') isStripped = True
isValidIntl = False
isIntl = False
# Send for processing # Send for processing
if self.isCleanUrl(name, langName, isValidIntl): if self.isCleanUrl(name, langName, isValidIntl, isStripped):
# handle a 'clean' request. # handle a 'clean' request.
# Try to form a response using the actual request url. # Try to form a response using the actual request url.
# logging.info(' Request being handled as clean: [%s]', name)
if not self.CreateResponse(name, langName, isValidIntl, resetLangCookie): if not self.CreateResponse(name, langName, isValidIntl, resetLangCookie):
# If CreateResponse returns False, there was no such document # If CreateResponse returns False, there was no such document
# in the intl/lang tree. Before going to 404, see if there is an # in the intl/lang tree. Before going to 404, see if there is an
@@ -167,7 +172,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
# for processing (so as to get 404 as appropriate). This is needed # for processing (so as to get 404 as appropriate). This is needed
# because intl urls are passed through clean and retried in English, # because intl urls are passed through clean and retried in English,
# if necessary. # if necessary.
logging.info(' Handling an invalid intl request...') # logging.info(' Handling an invalid intl request...')
self.CreateResponse(name, langName, isValidIntl, resetLangCookie) self.CreateResponse(name, langName, isValidIntl, resetLangCookie)
else: else:
@@ -178,7 +183,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
# request will be handled as a clean url. # request will be handled as a clean url.
self.RedirToIntl(name, self.intlString, langName) self.RedirToIntl(name, self.intlString, langName)
def isCleanUrl(self, name, langName, isValidIntl): def isCleanUrl(self, name, langName, isValidIntl, isStripped):
"""Determine whether to pass an incoming url straight to processing. """Determine whether to pass an incoming url straight to processing.
Args: Args:
@@ -187,13 +192,14 @@ class MemcachedZipHandler(webapp.RequestHandler):
Returns: Returns:
boolean: Whether the URL should be sent straight to processing boolean: Whether the URL should be sent straight to processing
""" """
if (langName == 'en') or isValidIntl or not ('.html' in name) or (not isValidIntl and not langName): # logging.info(' >>>> isCleanUrl name [%s] langName [%s] isValidIntl [%s]', name, langName, isValidIntl)
if (langName == 'en' and not isStripped) or isValidIntl or not ('.html' in name) or (not isValidIntl and not langName):
return True return True
def PreprocessUrl(self, name, langName): def PreprocessUrl(self, name, langName):
"""Any preprocessing work on the URL when it comes in. """Any preprocessing work on the URL when it comes in.
Put any work related to interpretting the incoming URL here. For example, Put any work related to interpreting the incoming URL here. For example,
this is used to redirect requests for a directory to the index.html file this is used to redirect requests for a directory to the index.html file
in that directory. Subclasses should override this method to do different in that directory. Subclasses should override this method to do different
preprocessing. preprocessing.
@@ -216,7 +222,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
# if this is a directory or the domain itself, redirect to /index.html # if this is a directory or the domain itself, redirect to /index.html
if not name or (name[len(name) - 1:] == '/'): if not name or (name[len(name) - 1:] == '/'):
uri = ''.join(['/', name, 'index.html']) uri = ''.join(['/', name, 'index.html'])
logging.info('--->PREPROCESSING REDIRECT [%s] to [%s] with langName [%s]', name, uri, langName) # logging.info('--->PREPROCESSING REDIRECT [%s] to [%s] with langName [%s]', name, uri, langName)
self.redirect(uri, False) self.redirect(uri, False)
return False return False
else: else:
@@ -225,10 +231,10 @@ class MemcachedZipHandler(webapp.RequestHandler):
def RedirToIntl(self, name, intlString, langName): def RedirToIntl(self, name, intlString, langName):
"""Redirect an incoming request to the appropriate intl uri. """Redirect an incoming request to the appropriate intl uri.
Builds the intl/lang string from a base (en) string For non-en langName, builds the intl/lang string from a
and redirects (302) the request to look for a version base (en) string and redirects (302) the request to look for
of the file in the language that matches the client- a version of the file in langName. For en langName, simply
supplied cookie value. redirects a stripped uri string (intl/nn removed).
Args: Args:
name: The incoming, preprocessed URL name: The incoming, preprocessed URL
@@ -236,7 +242,10 @@ class MemcachedZipHandler(webapp.RequestHandler):
Returns: Returns:
The lang-specific URL The lang-specific URL
""" """
builtIntlLangUri = ''.join([intlString, langName, '/', name, '?', self.request.query_string]) if not (langName == 'en'):
builtIntlLangUri = ''.join([intlString, langName, '/', name, '?', self.request.query_string])
else:
builtIntlLangUri = name
uri = ''.join(['/', builtIntlLangUri]) uri = ''.join(['/', builtIntlLangUri])
logging.info('-->>REDIRECTING %s to %s', name, uri) logging.info('-->>REDIRECTING %s to %s', name, uri)
self.redirect(uri, False) self.redirect(uri, False)
@@ -313,7 +322,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
# revalidate html files -- workaround for cache inconsistencies for # revalidate html files -- workaround for cache inconsistencies for
# negotiated responses # negotiated responses
mustRevalidate = True mustRevalidate = True
logging.info(' Adding [Vary: Cookie] to response...') #logging.info(' Adding [Vary: Cookie] to response...')
self.response.headers.add_header('Vary', 'Cookie') self.response.headers.add_header('Vary', 'Cookie')
content_type, encoding = mimetypes.guess_type(name) content_type, encoding = mimetypes.guess_type(name)
if content_type: if content_type:
@@ -491,7 +500,7 @@ class MemcachedZipHandler(webapp.RequestHandler):
max_age = self.MAX_AGE max_age = self.MAX_AGE
#self.response.headers['Expires'] = email.Utils.formatdate( #self.response.headers['Expires'] = email.Utils.formatdate(
# time.time() + max_age, usegmt=True) # time.time() + max_age, usegmt=True)
cache_control = [] cache_control = []
if self.PUBLIC: if self.PUBLIC:
cache_control.append('public') cache_control.append('public')
cache_control.append('max-age=%d' % max_age) cache_control.append('max-age=%d' % max_age)