Skip to content

Commit 4a54143

Browse files
ddoughertyAndroid (Google) Code Review
authored andcommitted
Merge "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." into froyo
2 parents 3804ab4 + 35ecf6a commit 4a54143

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

scripts/app_engine_server/memcache_zipserve.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def TrueGet(self, reqUri):
115115
urlLangName = None
116116
retry = False
117117
isValidIntl = False
118+
isStripped = False
118119

119120
# Try to retrieve the user's lang pref from the cookie. If there is no
120121
# lang pref cookie in the request, add set-cookie to the response with the
@@ -124,7 +125,7 @@ def TrueGet(self, reqUri):
124125
except KeyError:
125126
resetLangCookie = True
126127
#logging.info('==========================EXCEPTION: NO LANG COOKIE FOUND, USING [%s]', langName)
127-
logging.info('==========================REQ INIT name [%s] langName [%s]', reqUri, langName)
128+
logging.info('==========================REQ INIT name [%s] langName [%s] resetLangCookie [%s]', reqUri, langName, resetLangCookie)
128129

129130
# Preprocess the req url. If it references a directory or the domain itself,
130131
# append '/index.html' to the url and 302 redirect. Otherwise, continue
@@ -142,19 +143,23 @@ def TrueGet(self, reqUri):
142143
if isValidIntl:
143144
urlLangName = sections[1]
144145
contentUri = sections[2]
145-
if (langName != urlLangName):
146+
logging.info(' Content URI is [%s]...', contentUri)
147+
if (urlLangName != langName) or (langName == 'en'):
146148
# if the lang code in the request is different from that in
147-
# the cookie, reset the cookie to the url lang value.
148-
langName = urlLangName
149-
resetLangCookie = True
150-
#logging.info('INTL PREP resetting langName to urlLangName [%s]', langName)
151-
#else:
152-
# logging.info('INTL PREP no need to reset langName')
149+
# the cookie, or if the target lang is en, strip the
150+
# intl/nn substring. It will later be redirected to
151+
# the user's preferred language url.
152+
# logging.info(' Handling a MISMATCHED intl request')
153+
name = contentUri
154+
isStripped = True
155+
isValidIntl = False
156+
isIntl = False
153157

154158
# Send for processing
155-
if self.isCleanUrl(name, langName, isValidIntl):
159+
if self.isCleanUrl(name, langName, isValidIntl, isStripped):
156160
# handle a 'clean' request.
157161
# Try to form a response using the actual request url.
162+
# logging.info(' Request being handled as clean: [%s]', name)
158163
if not self.CreateResponse(name, langName, isValidIntl, resetLangCookie):
159164
# If CreateResponse returns False, there was no such document
160165
# in the intl/lang tree. Before going to 404, see if there is an
@@ -167,7 +172,7 @@ def TrueGet(self, reqUri):
167172
# for processing (so as to get 404 as appropriate). This is needed
168173
# because intl urls are passed through clean and retried in English,
169174
# if necessary.
170-
logging.info(' Handling an invalid intl request...')
175+
# logging.info(' Handling an invalid intl request...')
171176
self.CreateResponse(name, langName, isValidIntl, resetLangCookie)
172177

173178
else:
@@ -178,7 +183,7 @@ def TrueGet(self, reqUri):
178183
# request will be handled as a clean url.
179184
self.RedirToIntl(name, self.intlString, langName)
180185

181-
def isCleanUrl(self, name, langName, isValidIntl):
186+
def isCleanUrl(self, name, langName, isValidIntl, isStripped):
182187
"""Determine whether to pass an incoming url straight to processing.
183188
184189
Args:
@@ -187,13 +192,14 @@ def isCleanUrl(self, name, langName, isValidIntl):
187192
Returns:
188193
boolean: Whether the URL should be sent straight to processing
189194
"""
190-
if (langName == 'en') or isValidIntl or not ('.html' in name) or (not isValidIntl and not langName):
195+
# logging.info(' >>>> isCleanUrl name [%s] langName [%s] isValidIntl [%s]', name, langName, isValidIntl)
196+
if (langName == 'en' and not isStripped) or isValidIntl or not ('.html' in name) or (not isValidIntl and not langName):
191197
return True
192198

193199
def PreprocessUrl(self, name, langName):
194200
"""Any preprocessing work on the URL when it comes in.
195201
196-
Put any work related to interpretting the incoming URL here. For example,
202+
Put any work related to interpreting the incoming URL here. For example,
197203
this is used to redirect requests for a directory to the index.html file
198204
in that directory. Subclasses should override this method to do different
199205
preprocessing.
@@ -216,7 +222,7 @@ def PreprocessUrl(self, name, langName):
216222
# if this is a directory or the domain itself, redirect to /index.html
217223
if not name or (name[len(name) - 1:] == '/'):
218224
uri = ''.join(['/', name, 'index.html'])
219-
logging.info('--->PREPROCESSING REDIRECT [%s] to [%s] with langName [%s]', name, uri, langName)
225+
# logging.info('--->PREPROCESSING REDIRECT [%s] to [%s] with langName [%s]', name, uri, langName)
220226
self.redirect(uri, False)
221227
return False
222228
else:
@@ -225,18 +231,21 @@ def PreprocessUrl(self, name, langName):
225231
def RedirToIntl(self, name, intlString, langName):
226232
"""Redirect an incoming request to the appropriate intl uri.
227233
228-
Builds the intl/lang string from a base (en) string
229-
and redirects (302) the request to look for a version
230-
of the file in the language that matches the client-
231-
supplied cookie value.
234+
For non-en langName, builds the intl/lang string from a
235+
base (en) string and redirects (302) the request to look for
236+
a version of the file in langName. For en langName, simply
237+
redirects a stripped uri string (intl/nn removed).
232238
233239
Args:
234240
name: The incoming, preprocessed URL
235241
236242
Returns:
237243
The lang-specific URL
238244
"""
239-
builtIntlLangUri = ''.join([intlString, langName, '/', name, '?', self.request.query_string])
245+
if not (langName == 'en'):
246+
builtIntlLangUri = ''.join([intlString, langName, '/', name, '?', self.request.query_string])
247+
else:
248+
builtIntlLangUri = name
240249
uri = ''.join(['/', builtIntlLangUri])
241250
logging.info('-->>REDIRECTING %s to %s', name, uri)
242251
self.redirect(uri, False)
@@ -313,7 +322,7 @@ def CreateResponse(self, name, langName, isValidIntl, resetLangCookie):
313322
# revalidate html files -- workaround for cache inconsistencies for
314323
# negotiated responses
315324
mustRevalidate = True
316-
logging.info(' Adding [Vary: Cookie] to response...')
325+
#logging.info(' Adding [Vary: Cookie] to response...')
317326
self.response.headers.add_header('Vary', 'Cookie')
318327
content_type, encoding = mimetypes.guess_type(name)
319328
if content_type:
@@ -491,7 +500,7 @@ def SetCachingHeaders(self, revalidate):
491500
max_age = self.MAX_AGE
492501
#self.response.headers['Expires'] = email.Utils.formatdate(
493502
# time.time() + max_age, usegmt=True)
494-
cache_control = []
503+
cache_control = []
495504
if self.PUBLIC:
496505
cache_control.append('public')
497506
cache_control.append('max-age=%d' % max_age)

0 commit comments

Comments
 (0)