def load_user_scripts_into_handlers(app_info):
    """Preloads user scripts, wrapped in env_config middleware if present.

  Args:
    app_info: AppInfoExternal object mostly used to get it's handlers.

  Returns:
    A list of tuples suitable for configuring the dispatcher() app,
    where the tuples are (url, app):
      - url_re: The url regular expression which matches this handler.
      - app: The fully loaded app corresponding to the script.
  """
    loaded_handlers = []
    for handler in app_info.handlers:
        if handler.script:  # An application, not a static files directive.
            url_re = handler.url
            app = app_for_script(handler.script)
        else:  # A static files directive, either with static_files or static_dir.
            if handler.static_files:
                url_re = handler.url
            else:  # This is a "static_dir" directive.
                url_re = static_dir_url_re(handler)
            expiration = (handler.expiration or app_info.default_expiration
                          or DEFAULT_STATIC_CONTENT_EXPIRATION)
            app = static_app_for_handler(handler,
                                         appinfo.ParseExpiration(expiration))
        loaded_handlers.append((url_re, app))
    logging.info('Parsed handlers: %r',
                 [url_re for (url_re, _) in loaded_handlers])
    return loaded_handlers
 def test_static_file_default_expires(self):
     response = self.client.get('/favicon.ico')
     self.assertEqual(response.status_code, httplib.OK)
     with open(static_path('test_statics/favicon.ico')) as f:
         self.assertEqual(response.data, f.read())
     current_time = FAKE_CURRENT_TIME
     extra_time = datetime.timedelta(
         seconds=appinfo.ParseExpiration('2d 3h'))
     expired_time = current_time + extra_time
     self.assertEqual(response.headers['Expires'],
                      http.http_date(expired_time))
Esempio n. 3
0
def ServeStaticPage(tree, page):
  """Respond by serving a single static file.

  Args:
    tree: A tree object to use to retrieve files.
    page: A StaticPage object describing the file to be served.
  """
  file_path = page.file_path
  logging.info('Serving static page %s', file_path)
  file_data = tree.GetFileContents(file_path)
  if file_data is None:
    RespondWithStatus(httplib.NOT_FOUND,
                      content_type='text/html; charset=utf-8',
                      data=_NOT_FOUND_PAGE % file_path)
    return
  if page.mime_type is not None:
    content_type = page.mime_type
  else:
    content_type = common.GuessMimeType(file_path)
  # should not raise ConfigurationError, but even that would be ok
  expiration_s = appinfo.ParseExpiration(page.expiration)
  RespondWithStatus(httplib.OK, content_type=content_type,
                    data=file_data, expiration_s=expiration_s)
 def _get_expires_header_value(self, expiration):
     expires_delta_seconds = appinfo.ParseExpiration(expiration)
     expiration_datetime = (
         Now() + datetime.timedelta(seconds=expires_delta_seconds))
     return wsgiref.handlers.format_date_time(
         time.mktime(expiration_datetime.timetuple()))