def _RedirectSpecialCases(self): path = self._request.path if not path or path == 'index.html': return Response.Redirect('http://developer.google.com/chrome') if path == 'apps.html': return Response.Redirect('/apps/about_apps.html') return None
def Get(self): if (not IsDevServer() and not fnmatch( urlparse(self._request.host).netloc, '*.appspot.com')): # Only allow patches on appspot URLs; it doesn't matter if appspot.com is # XSS'ed, but it matters for chrome.com. redirect_host = 'https://chrome-apps-doc.appspot.com' logging.info('Redirecting from XSS-able host %s to %s' % (self._request.host, redirect_host)) return Response.Redirect('%s/_patch/%s' % (redirect_host, self._request.path)) path_with_issue = self._request.path.lstrip('/') if '/' in path_with_issue: issue, path_without_issue = path_with_issue.split('/', 1) else: return Response.NotFound( 'Malformed URL. It should look like ' + 'https://developer.chrome.com/_patch/12345/extensions/...') try: response = RenderServlet( Request(path_without_issue, self._request.host, self._request.headers), _PatchServletDelegate(issue, self._delegate)).Get() # Disable cache for patched content. response.headers.pop('cache-control', None) except RietveldPatcherError as e: response = Response.NotFound(e.message, {'Content-Type': 'text/plain'}) redirect_url, permanent = response.GetRedirect() if redirect_url is not None: response = Response.Redirect( '/_patch/%s%s' % (issue, redirect_url), permanent) return response
def _GetSuccessResponse(self, request_path, server_instance): '''Returns the Response from trying to render |path| with |server_instance|. If |path| isn't found then a FileNotFoundError will be raised, such that the only responses that will be returned from this method are Ok and Redirect. ''' content_provider, serve_from, path = ( server_instance.content_providers.GetByServeFrom(request_path)) assert content_provider, 'No ContentProvider found for %s' % path redirect = Redirector( server_instance.compiled_fs_factory, content_provider.file_system).Redirect(self._request.host, path) if redirect is not None: # Absolute redirects stay absolute, relative redirects are relative to # |serve_from|; all redirects eventually need to be *served* as absolute. if not redirect.startswith('/'): redirect = '/' + posixpath.join(serve_from, redirect) return Response.Redirect(redirect, permanent=False) canonical_path = content_provider.GetCanonicalPath(path) if canonical_path != path: redirect_path = posixpath.join(serve_from, canonical_path) return Response.Redirect('/' + redirect_path, permanent=False) if request_path.endswith('/'): # Directory request hasn't been redirected by now. Default behaviour is # to redirect as though it were a file. return Response.Redirect('/' + request_path.rstrip('/'), permanent=False) if not path: # Empty-path request hasn't been redirected by now. It doesn't exist. raise FileNotFoundError('Empty path') content_and_type = content_provider.GetContentAndType(path).Get() if not content_and_type.content: logging.error('%s had empty content' % path) content = content_and_type.content if isinstance(content, Handlebar): template_content, template_warnings = ( server_instance.template_renderer.Render(content, self._request)) # HACK: the site verification file (google2ed...) doesn't have a title. content, doc_warnings = server_instance.document_renderer.Render( template_content, path, render_title=path != SITE_VERIFICATION_FILE) warnings = template_warnings + doc_warnings if warnings: sep = '\n - ' logging.warning('Rendering %s:%s%s' % (path, sep, sep.join(warnings))) content_type = content_and_type.content_type if isinstance(content, unicode): content = content.encode('utf-8') content_type += '; charset=utf-8' return Response.Ok(content, headers=_MakeHeaders(content_type))
def testOldHostsRedirect(self): self.assertEqual( Response.Redirect('https://developer.chrome.com/extensions', permanent=False), self._Render('/chrome/extensions', host='http://code.google.com')) self.assertEqual( Response.Redirect('https://developer.chrome.com/extensions', permanent=False), self._Render('/chrome/extensions', host='https://code.google.com'))
def _GetSuccessResponse(self, path, server_instance): '''Returns the Response from trying to render |path| with |server_instance|. If |path| isn't found then a FileNotFoundError will be raised, such that the only responses that will be returned from this method are Ok and Redirect. ''' content_provider, path = ( server_instance.content_providers.GetByServeFrom(path)) assert content_provider, 'No ContentProvider found for %s' % path redirect = Redirector(server_instance.compiled_fs_factory, content_provider.file_system).Redirect( self._request.host, path) if redirect is not None: return Response.Redirect(redirect, permanent=False) content_and_type = content_provider.GetContentAndType( self._request.host, path).Get() if not content_and_type.content: logging.error('%s had empty content' % path) if isinstance(content_and_type.content, Handlebar): content_and_type.content = server_instance.template_renderer.Render( content_and_type.content, self._request) return Response.Ok(content_and_type.content, headers=_MakeHeaders(content_and_type.content_type))
def Get(self): ''' Render the page for a request. ''' path = self._request.path.lstrip('/') # The server used to be partitioned based on Chrome channel, but it isn't # anymore. Redirect from the old state. channel_name, path = BranchUtility.SplitChannelNameFromPath(path) if channel_name is not None: return Response.Redirect('/' + path, permanent=True) server_instance = self._delegate.CreateServerInstance() try: return self._GetSuccessResponse(path, server_instance) except FileNotFoundError: # Find the closest 404.html file and serve that, e.g. if the path is # extensions/manifest/typo.html then first look for # extensions/manifest/404.html, then extensions/404.html, then 404.html. # # Failing that just print 'Not Found' but that should preferrably never # happen, because it would look really bad. path_components = path.split('/') for i in xrange(len(path_components) - 1, -1, -1): try: path_404 = posixpath.join(*(path_components[0:i] + ['404'])) response = self._GetSuccessResponse(path_404, server_instance) if response.status != 200: continue return Response.NotFound(response.content.ToString(), headers=response.headers) except FileNotFoundError: continue logging.warning('No 404.html found in %s' % path) return Response.NotFound('Not Found', headers=_MakeHeaders('text/plain'))
def _GetSuccessResponse(self, path, server_instance): '''Returns the Response from trying to render |path| with |server_instance|. If |path| isn't found then a FileNotFoundError will be raised, such that the only responses that will be returned from this method are Ok and Redirect. ''' content_provider, serve_from, path = ( server_instance.content_providers.GetByServeFrom(path)) assert content_provider, 'No ContentProvider found for %s' % path redirect = Redirector(server_instance.compiled_fs_factory, content_provider.file_system).Redirect( self._request.host, path) if redirect is not None: return Response.Redirect(redirect, permanent=False) canonical_path = content_provider.GetCanonicalPath(path) if canonical_path != path: redirect_path = posixpath.join(serve_from, canonical_path) return Response.Redirect('/' + redirect_path, permanent=False) content_and_type = content_provider.GetContentAndType(path).Get() if not content_and_type.content: logging.error('%s had empty content' % path) content = content_and_type.content if isinstance(content, Handlebar): template_content, template_warnings = ( server_instance.template_renderer.Render( content, self._request)) # HACK: the site verification file (google2ed...) doesn't have a title. content, doc_warnings = server_instance.document_renderer.Render( template_content, path, render_title=path != SITE_VERIFICATION_FILE) warnings = template_warnings + doc_warnings if warnings: sep = '\n - ' logging.warning('Rendering %s:%s%s' % (path, sep, sep.join(warnings))) content_type = content_and_type.content_type if isinstance(content, unicode): content = content.encode('utf-8') content_type += '; charset=utf-8' return Response.Ok(content, headers=_MakeHeaders(content_type))
def Get(self): ''' Render the page for a request. ''' # TODO(kalman): a consistent path syntax (even a Path class?) so that we # can stop being so conservative with stripping and adding back the '/'s. path = self._request.path.lstrip('/') server_instance = self._delegate.CreateServerInstance() try: return self._GetSuccessResponse(path, server_instance) except FileNotFoundError: if IsPreviewServer(): logging.error(traceback.format_exc()) # Maybe it didn't find the file because its canonical location is # somewhere else; this is distinct from "redirects", which are typically # explicit. This is implicit. canonical_result = server_instance.path_canonicalizer.Canonicalize( path) redirect = canonical_result.path.lstrip('/') if path != redirect: return Response.Redirect('/' + redirect, permanent=canonical_result.permanent) # Not found for reals. Find the closest 404.html file and serve that; # e.g. if the path is extensions/manifest/typo.html then first look for # extensions/manifest/404.html, then extensions/404.html, then 404.html. # # Failing that just print 'Not Found' but that should preferrably never # happen, because it would look really bad. path_components = path.split('/') for i in xrange(len(path_components) - 1, -1, -1): try: path_404 = posixpath.join(*(path_components[0:i] + ['404.html'])) response = self._GetSuccessResponse( path_404, server_instance) return Response.NotFound(response.content.ToString(), headers=response.headers) except FileNotFoundError: continue logging.warning('No 404.html found in %s' % path) return Response.NotFound('Not Found', headers=_MakeHeaders('text/plain'))
def _RedirectFromCodeDotGoogleDotCom(self): host, path = (self._request.host, self._request.path) if not host in ('http://code.google.com', 'https://code.google.com'): return None new_host = 'http://developer.chrome.com' # switch to https if necessary if host.startswith('https'): new_host = new_host.replace('http', 'https', 1) new_path = path.split('/') if len(new_path) > 0 and new_path[0] == 'chrome': new_path.pop(0) for channel in BranchUtility.GetAllChannelNames(): if channel in new_path: position = new_path.index(channel) new_path.pop(position) new_path.insert(0, channel) return Response.Redirect('/'.join([new_host] + new_path))
def Get(self): path_with_channel, headers = (self._request.path, self._request.headers) # Redirect "extensions" and "extensions/" to "extensions/index.html", etc. if (os.path.splitext(path_with_channel)[1] == '' and path_with_channel.find('/') == -1): path_with_channel += '/' if path_with_channel.endswith('/'): return Response.Redirect('/%sindex.html' % path_with_channel) channel, path = BranchUtility.SplitChannelNameFromPath( path_with_channel) if channel == self._default_channel: return Response.Redirect('/%s' % path) if channel is None: channel = self._default_channel server_instance = self._delegate.CreateServerInstanceForChannel( channel) canonical_path = ( server_instance.path_canonicalizer.Canonicalize(path).lstrip('/')) if path != canonical_path: redirect_path = (canonical_path if channel is None else '%s/%s' % (channel, canonical_path)) return Response.Redirect('/%s' % redirect_path) templates = server_instance.template_data_source_factory.Create( self._request, path) content = None content_type = None try: if fnmatch(path, 'extensions/examples/*.zip'): content = server_instance.example_zipper.Create( path[len('extensions/'):-len('.zip')]) content_type = 'application/zip' elif path.startswith('extensions/examples/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.content_cache.GetFromFile( '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.startswith('static/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.content_cache.GetFromFile( ('%s/%s' % (svn_constants.DOCS_PATH, path)), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.endswith('.html'): content = templates.Render(path) content_type = 'text/html' except FileNotFoundError as e: logging.warning(traceback.format_exc()) content = None headers = {'x-frame-options': 'sameorigin'} if content is None: doc_class = path.split('/', 1)[0] content = templates.Render('%s/404' % doc_class) if not content: content = templates.Render('extensions/404') return Response.NotFound(content, headers=headers) if not content: logging.error('%s had empty content' % path) headers.update({ 'content-type': content_type, 'cache-control': 'max-age=300', }) return Response.Ok(content, headers=headers)
def _GetSuccessResponse(self, request_path, server_instance): '''Returns the Response from trying to render |path| with |server_instance|. If |path| isn't found then a FileNotFoundError will be raised, such that the only responses that will be returned from this method are Ok and Redirect. ''' content_provider, serve_from, path = ( server_instance.content_providers.GetByServeFrom(request_path)) assert content_provider, 'No ContentProvider found for %s' % path redirect = Redirector(server_instance.compiled_fs_factory, content_provider.file_system).Redirect( self._request.host, path) if redirect is not None: # Absolute redirects stay absolute, relative redirects are relative to # |serve_from|; all redirects eventually need to be *served* as absolute. if not redirect.startswith('/'): redirect = '/' + posixpath.join(serve_from, redirect) return Response.Redirect(redirect, permanent=False) canonical_path = content_provider.GetCanonicalPath(path) if canonical_path != path: redirect_path = posixpath.join(serve_from, canonical_path) return Response.Redirect('/' + redirect_path, permanent=False) if request_path.endswith('/'): # Directory request hasn't been redirected by now. Default behaviour is # to redirect as though it were a file. return Response.Redirect('/' + request_path.rstrip('/'), permanent=False) if not path: # Empty-path request hasn't been redirected by now. It doesn't exist. raise FileNotFoundError('Empty path') content_and_type = content_provider.GetContentAndType(path).Get() if not content_and_type.content: logging.error('%s had empty content' % path) content = content_and_type.content if isinstance(content, Handlebar): template_content, template_warnings = ( server_instance.template_renderer.Render( content, self._request)) # HACK: the site verification file (google2ed...) doesn't have a title. content, doc_warnings = server_instance.document_renderer.Render( template_content, path, render_title=path != SITE_VERIFICATION_FILE) warnings = template_warnings + doc_warnings if warnings: sep = '\n - ' logging.warning('Rendering %s:%s%s' % (path, sep, sep.join(warnings))) # Content was dynamic. The new etag is a hash of the content. etag = None elif content_and_type.version is not None: # Content was static. The new etag is the version of the content. Hash it # to make sure it's valid. etag = '"%s"' % hashlib.md5(str( content_and_type.version)).hexdigest() else: # Sometimes non-dynamic content does not have a version, for example # .zip files. The new etag is a hash of the content. etag = None content_type = content_and_type.content_type if isinstance(content, unicode): content = content.encode('utf-8') content_type += '; charset=utf-8' if etag is None: # Note: we're using md5 as a convenient and fast-enough way to identify # content. It's not intended to be cryptographic in any way, and this # is *not* what etags is for. That's what SSL is for, this is unrelated. etag = '"%s"' % hashlib.md5(content).hexdigest() headers = _MakeHeaders(content_type, etag=etag) if etag == self._request.headers.get('If-None-Match'): return Response.NotModified('Not Modified', headers=headers) return Response.Ok(content, headers=headers)
def testChannelRedirect(self): for channel in ('stable', 'beta', 'dev', 'trunk'): self.assertEqual( Response.Redirect('/extensions/storage', permanent=True), self._Render('%s/extensions/storage' % channel))
def testExtensionAppRedirect(self): self.assertEqual(Response.Redirect('/apps/storage', permanent=False), self._Render('storage'))
def testChannelRedirect(self): request = Request.ForTest('stable/extensions/storage.html') self.assertEqual( Response.Redirect('/extensions/storage.html', permanent=True), RenderServlet(request, _RenderServletDelegate()).Get())
def Get(self): ''' Render the page for a request. ''' # TODO(kalman): a consistent path syntax (even a Path class?) so that we # can stop being so conservative with stripping and adding back the '/'s. path = self._request.path.lstrip('/') if path.split('/')[-1] == 'redirects.json': return Response.Ok('') server_instance = self._delegate.CreateServerInstance() redirect = server_instance.redirector.Redirect(self._request.host, path) if redirect is not None: return Response.Redirect(redirect) canonical_result = server_instance.path_canonicalizer.Canonicalize( path) redirect = canonical_result.path.lstrip('/') if path != redirect: return Response.Redirect('/' + redirect, permanent=canonical_result.permanent) templates = server_instance.template_data_source_factory.Create( self._request, path) content = None content_type = None try: # At this point, any valid paths ending with '/' have been redirected. # Therefore, the response should be a 404 Not Found. if path.endswith('/'): pass elif fnmatch(path, 'extensions/examples/*.zip'): content = server_instance.example_zipper.Create( path[len('extensions/'):-len('.zip')]) content_type = 'application/zip' elif path.startswith('extensions/examples/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.host_file_system.ReadSingle( '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.startswith('static/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.host_file_system.ReadSingle( ('%s/%s' % (svn_constants.DOCS_PATH, path)), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.endswith('.html'): content = templates.Render(path) content_type = 'text/html' except FileNotFoundError: logging.warning(traceback.format_exc()) content = None headers = {'x-frame-options': 'sameorigin'} if content is None: doc_class = path.split('/', 1)[0] content = templates.Render('%s/404' % doc_class) if not content: content = templates.Render('extensions/404') return Response.NotFound(content, headers=headers) if not content: logging.error('%s had empty content' % path) headers.update({ 'content-type': content_type, 'cache-control': 'max-age=300', }) return Response.Ok(content, headers=headers)
def testChannelRedirect(self): self.assertEqual( Response.Redirect('/extensions/storage.html', permanent=True), self._Render('stable/extensions/storage.html'))
def Get(self): ''' Render the page for a request. ''' headers = self._request.headers channel, path = BranchUtility.SplitChannelNameFromPath( self._request.path) if path.split('/')[-1] == 'redirects.json': return Response.Ok('') if channel == self._default_channel: return Response.Redirect('/' + path) if channel is None: channel = self._default_channel server_instance = self._delegate.CreateServerInstanceForChannel( channel) redirect = server_instance.redirector.Redirect(self._request.host, path) if redirect is not None: if (channel != self._default_channel and not urlsplit(redirect).scheme in ('http', 'https')): redirect = '/%s%s' % (channel, redirect) return Response.Redirect(redirect) canonical_path = server_instance.path_canonicalizer.Canonicalize(path) redirect = canonical_path.lstrip('/') if path != redirect: if channel is not None: redirect = '%s/%s' % (channel, canonical_path) return Response.Redirect('/' + redirect) templates = server_instance.template_data_source_factory.Create( self._request, path) content = None content_type = None try: if fnmatch(path, 'extensions/examples/*.zip'): content = server_instance.example_zipper.Create( path[len('extensions/'):-len('.zip')]) content_type = 'application/zip' elif path.startswith('extensions/examples/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.content_cache.GetFromFile( '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.startswith('static/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.content_cache.GetFromFile( ('%s/%s' % (svn_constants.DOCS_PATH, path)), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.endswith('.html'): content = templates.Render(path) content_type = 'text/html' except FileNotFoundError: logging.warning(traceback.format_exc()) content = None headers = {'x-frame-options': 'sameorigin'} if content is None: doc_class = path.split('/', 1)[0] content = templates.Render('%s/404' % doc_class) if not content: content = templates.Render('extensions/404') return Response.NotFound(content, headers=headers) if not content: logging.error('%s had empty content' % path) headers.update({ 'content-type': content_type, 'cache-control': 'max-age=300', }) return Response.Ok(content, headers=headers)
def Get(self, server_instance=None): path_with_channel, headers = (self._request.path.lstrip('/'), self._request.headers) # Redirect "extensions" and "extensions/" to "extensions/index.html", etc. if (os.path.splitext(path_with_channel)[1] == '' and path_with_channel.find('/') == -1): path_with_channel += '/' if path_with_channel.endswith('/'): return Response.Redirect(path_with_channel + 'index.html') channel, path = BranchUtility.SplitChannelNameFromPath( path_with_channel) if channel == _DEFAULT_CHANNEL: return Response.Redirect('/%s' % path) if channel is None: channel = _DEFAULT_CHANNEL # AppEngine instances should never need to call out to SVN. That should # only ever be done by the cronjobs, which then write the result into # DataStore, which is as far as instances look. To enable this, crons can # pass a custom (presumably online) ServerInstance into Get(). # # Why? SVN is slow and a bit flaky. Cronjobs failing is annoying but # temporary. Instances failing affects users, and is really bad. # # Anyway - to enforce this, we actually don't give instances access to SVN. # If anything is missing from datastore, it'll be a 404. If the cronjobs # don't manage to catch everything - uhoh. On the other hand, we'll figure # it out pretty soon, and it also means that legitimate 404s are caught # before a round trip to SVN. if server_instance is None: # The ALWAYS_ONLINE thing is for tests and preview.py that shouldn't need # to run the cron before rendering things. constructor = (ServerInstance.CreateOnline if _ALWAYS_ONLINE else ServerInstance.GetOrCreateOffline) server_instance = constructor(channel) canonical_path = server_instance.path_canonicalizer.Canonicalize(path) if path != canonical_path: return Response.Redirect( canonical_path if channel is None else '%s/%s' % (channel, canonical_path)) templates = server_instance.template_data_source_factory.Create( self._request, path) content = None content_type = None try: if fnmatch(path, 'extensions/examples/*.zip'): content = server_instance.example_zipper.Create( path[len('extensions/'):-len('.zip')]) content_type = 'application/zip' elif path.startswith('extensions/examples/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.content_cache.GetFromFile( '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.startswith('static/'): mimetype = mimetypes.guess_type(path)[0] or 'text/plain' content = server_instance.content_cache.GetFromFile( ('%s/%s' % (svn_constants.DOCS_PATH, path)), binary=_IsBinaryMimetype(mimetype)) content_type = mimetype elif path.endswith('.html'): content = templates.Render(path) content_type = 'text/html' except FileNotFoundError as e: logging.warning(traceback.format_exc()) content = None headers = {'x-frame-options': 'sameorigin'} if content is None: return Response.NotFound(templates.Render('404'), headers=headers) if not content: logging.error('%s had empty content' % path) headers.update({ 'content-type': content_type, 'cache-control': 'max-age=300', }) return Response.Ok(content, headers=headers)