def dispatch(self, request, *args, **kwargs): """See docs on django.views.View.dispatch.""" # If the X-AppEngine-TaskQueue header is set, it means the request came # from App Engine, not an external user: # https://cloud.google.com/appengine/docs/standard/python/taskqueue/push/creating-handlers#reading_request_headers # There are various reasons we'd prefer to prevent external users from # starting up tasks (e.g., because some tasks might be expensive # operations). # Django renames headers, so X-AppEngine-TaskName will be in the META # dictionary as HTTP_X_APPENGINE_TASKNAME: # https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.META if not (request.META.get('HTTP_X_APPENGINE_TASKNAME') or utils.is_dev_app_server()): logging.warn('Non-taskqueue access of: %s' % self.request.path) return self.error(403) return super(TasksBaseView, self).dispatch(request, args, kwargs)
def sign_url(self, object_name, url_lifetime): """ Generates Cloud Storage signed URL to download Google Cloud Storage object without sign in. See: https://cloud.google.com/storage/docs/access-control/signed-urls This only works on a real App Engine app, not in a dev app server. Args: object_name (str): The name of the object which is signed. url_lifetime (datetime.timedelta): Lifetime of the signed URL. The server rejects any requests received after this time from now. """ if utils.is_dev_app_server(): # Not working on a dev app server because it doesn't support # app_identity.sign_blob(). An alternative implementation would # be needed to make it work on a dev app server. raise Exception( 'sign_url only works on a real App Engine app, not on a dev ' 'app server.') method = 'GET' expiration_time = utils.get_utcnow() + url_lifetime expiration_sec = int(time.mktime(expiration_time.timetuple())) path = '/%s/%s' % (self.bucket_name, object_name) # These are unused in our use case. content_md5 = '' content_type = '' signed_text = '\n'.join([ method, content_md5, content_type, str(expiration_sec), path, ]) (_, signature) = app_identity.sign_blob(signed_text.encode('utf-8')) query_params = { 'GoogleAccessId': app_identity.get_service_account_name(), 'Expires': str(expiration_sec), 'Signature': base64.b64encode(signature), } return 'https://storage.googleapis.com%s?%s' % (path, urllib.urlencode(query_params))
def initialize(self, request, response): webapp.RequestHandler.initialize(self, request, response) # If requested, set the clock before doing anything clock-related. # Only works on localhost for testing. Specify ?utcnow=1293840000 to # set the clock to 2011-01-01, or ?utcnow=real to revert to real time. utcnow = request.get('utcnow') if request.remote_addr == '127.0.0.1' and utcnow: if utcnow == 'real': utils.set_utcnow_for_test(None) else: utils.set_utcnow_for_test(float(utcnow)) # If requested, flush caches before we touch anything that uses them. # This is used for certain tests. if utils.is_dev_app_server(): flush_caches(*request.get('flush', '').split(',')) # Gather commonly used information into self.env. self.env = setup_env(request) # Force a redirect if requested, except where https is not supported: # - for cron jobs # - for task queue jobs # - in development if (self.env.force_https and self.env.scheme == 'http' and not is_cron_task(self.request) and not is_task_queue_task(self.request) and not is_development_server()): self.redirect(self.env.url.replace('http:', 'https:')) # Activate the selected language. response.headers['Content-Language'] = self.env.lang response.headers['Set-Cookie'] = \ 'django_language=%s; path=/' % self.env.lang django_setup.activate(self.env.lang) # Activate the appropriate resource bundle. resources.set_active_bundle_name(self.env.resource_bundle)