def __init__(self, request, response, services=None, content_type='text/html; charset=UTF-8'): """Load and parse the template, saving it for later use.""" super(Servlet, self).__init__(request, response) if self._PAGE_TEMPLATE: # specified in subclasses template_path = self._TEMPLATE_PATH + self._PAGE_TEMPLATE self.template = template_helpers.GetTemplate( template_path, eliminate_blank_lines=self._ELIMINATE_BLANK_LINES) else: self.template = None self._missing_permissions_template = template_helpers.MonorailTemplate( self._TEMPLATE_PATH + self._MISSING_PERMISSIONS_TEMPLATE) self.services = services or self.app.config.get('services') self.content_type = content_type self.mr = None self.ratelimiter = ratelimiter.RateLimiter()
def dispatch(self): """Do common stuff then dispatch the request to get() or put() methods.""" handler_start_time = time.time() logging.info('\n\n\nRequest handler: %r', self) count0, count1, count2 = gc.get_count() logging.info('gc counts: %d %d %d', count0, count1, count2) GC_COUNT.add(count0, {'generation': 0}) GC_COUNT.add(count1, {'generation': 1}) GC_COUNT.add(count2, {'generation': 2}) self.mr = monorailrequest.MonorailRequest(self.services) self.ratelimiter.CheckStart(self.request) self.response.headers.add('Strict-Transport-Security', 'max-age=31536000; includeSubDomains') if 'X-Cloud-Trace-Context' in self.request.headers: self.mr.profiler.trace_context = ( self.request.headers.get('X-Cloud-Trace-Context')) if trace_service is not None: self.mr.profiler.trace_service = trace_service if self.services.cache_manager: # TODO(jrobbins): don't do this step if invalidation_timestep was # passed via the request and matches our last timestep try: with self.mr.profiler.Phase('distributed invalidation'): self.services.cache_manager.DoDistributedInvalidation( self.mr.cnxn) except MySQLdb.OperationalError as e: logging.exception(e) page_data = { 'http_response_code': httplib.SERVICE_UNAVAILABLE, 'requested_url': self.request.url, } self.template = template_helpers.GetTemplate( 'templates/framework/database-maintenance.ezt', eliminate_blank_lines=self._ELIMINATE_BLANK_LINES) self.template.WriteResponse(self.response, page_data, content_type='text/html') return try: with self.mr.profiler.Phase('parsing request and doing lookups'): self.mr.ParseRequest(self.request, self.services) self.response.headers['X-Frame-Options'] = 'SAMEORIGIN' webapp2.RequestHandler.dispatch(self) except exceptions.NoSuchUserException as e: logging.warning('Trapped NoSuchUserException %s', e) self.abort(404, 'user not found') except exceptions.NoSuchGroupException as e: logging.warning('Trapped NoSuchGroupException %s', e) self.abort(404, 'user group not found') except exceptions.InputException as e: logging.info('Rejecting invalid input: %r', e) self.response.status = httplib.BAD_REQUEST except exceptions.NoSuchProjectException as e: logging.info('Rejecting invalid request: %r', e) self.response.status = httplib.NOT_FOUND except xsrf.TokenIncorrect as e: logging.info('Bad XSRF token: %r', e.message) self.response.status = httplib.BAD_REQUEST except permissions.BannedUserException as e: logging.warning('The user has been banned') url = framework_helpers.FormatAbsoluteURL(self.mr, urls.BANNED, include_project=False, copy_params=False) self.redirect(url, abort=True) except ratelimiter.RateLimitExceeded as e: logging.info('RateLimitExceeded Exception %s', e) self.response.status = httplib.BAD_REQUEST self.response.body = 'Slow your roll.' finally: self.mr.CleanUp() self.ratelimiter.CheckEnd(self.request, time.time(), handler_start_time) total_processing_time = time.time() - handler_start_time logging.warn('Processed request in %d ms', int(total_processing_time * 1000)) end_count0, end_count1, end_count2 = gc.get_count() logging.info('gc counts: %d %d %d', end_count0, end_count1, end_count2) if (end_count0 < count0) or (end_count1 < count1) or (end_count2 < count2): GC_EVENT_REQUEST.increment() if settings.enable_profiler_logging: self.mr.profiler.LogStats() if (self.mr.profiler.trace_context is not None and random.random() < settings.trace_fraction): self.mr.profiler.ReportTrace()
def dispatch(self): """Do common stuff then dispatch the request to get() or put() methods.""" handler_start_time = time.time() logging.info('\n\n\nRequest handler: %r', self) self.mr = monorailrequest.MonorailRequest() self.ratelimiter.CheckStart(self.request) self.response.headers.add('Strict-Transport-Security', 'max-age=31536000; includeSubDomains') if self.services.cache_manager: # TODO(jrobbins): don't do this step if invalidation_timestep was # passed via the request and matches our last timestep try: with self.profiler.Phase('distributed invalidation'): self.services.cache_manager.DoDistributedInvalidation( self.mr.cnxn) except MySQLdb.OperationalError as e: logging.exception(e) page_data = { 'http_response_code': httplib.SERVICE_UNAVAILABLE, 'requested_url': self.request.url, } self.template = template_helpers.GetTemplate( 'templates/framework/database-maintenance.ezt', eliminate_blank_lines=self._ELIMINATE_BLANK_LINES) self.template.WriteResponse(self.response, page_data, content_type='text/html') return try: with self.profiler.Phase('parsing request and doing lookups'): self.mr.ParseRequest(self.request, self.services, self.profiler) self.response.headers['X-Frame-Options'] = 'SAMEORIGIN' webapp2.RequestHandler.dispatch(self) except user_svc.NoSuchUserException as e: logging.warning('Trapped NoSuchUserException %s', e) self.abort(404, 'user not found') except usergroup_svc.NoSuchGroupException as e: logging.warning('Trapped NoSuchGroupException %s', e) self.abort(404, 'user group not found') except monorailrequest.InputException as e: logging.info('Rejecting invalid input: %r', e) self.response.status = httplib.BAD_REQUEST except project_svc.NoSuchProjectException as e: logging.info('Rejecting invalid request: %r', e) self.response.status = httplib.BAD_REQUEST except xsrf.TokenIncorrect as e: logging.info('Bad XSRF token: %r', e.message) self.response.status = httplib.BAD_REQUEST except AlreadySentResponseException: # If servlet already sent response, then do nothing more. E.g., # when serving attachment content, we do not use templates. pass except permissions.BannedUserException as e: logging.warning('The user has been banned') url = framework_helpers.FormatAbsoluteURL(self.mr, urls.BANNED, include_project=False, copy_params=False) self.redirect(url, abort=True) except actionlimit.ExcessiveActivityException: logging.info('Excessive Activity Exception %r', self.mr.auth.user_id) url = framework_helpers.FormatAbsoluteURL(self.mr, urls.EXCESSIVE_ACTIVITY, include_project=False, copy_params=False) self.redirect(url, abort=True) except ratelimiter.RateLimitExceeded as e: logging.info('RateLimitExceeded Exception %s', e) self.response.status = httplib.BAD_REQUEST self.response.body = 'Slow your roll.' finally: self.mr.CleanUp() self.ratelimiter.CheckEnd(self.request, time.time(), handler_start_time) total_processing_time = time.time() - handler_start_time logging.warn('Processed request in %d ms', int(total_processing_time * 1000)) if settings.enable_profiler_logging: self.profiler.LogStats()