示例#1
0
文件: bootstrap.py 项目: sabren/drupy
def drupal_page_header():
    """
   Set HTTP headers in preparation for a page response.
  
   Authenticated users are always given a 'no-cache' php.header, and will
   fetch a fresh page on every request.  This prevents authenticated
   users seeing locally cached pages that show them as logged out.
  
   @see page_set_cache()
  """
    php.header("Expires: Sun, 19 Nov 1978 05:00:00 GMT")
    php.header("Last-Modified: " + php.gmdate("%D, %d %M %Y %H:%i:%s") + " GMT")
    php.header("Cache-Control: store, no-cache, must-revalidate")
    php.header("Cache-Control: post-check=0, pre-check=0", False)
示例#2
0
def drupal_page_header():
    """
   Set HTTP headers in preparation for a page response.
  
   Authenticated users are always given a 'no-cache' php.header, and will
   fetch a fresh page on every request.  This prevents authenticated
   users seeing locally cached pages that show them as logged out.
  
   @see page_set_cache()
  """
    php.header("Expires: Sun, 19 Nov 1978 05:00:00 GMT")
    php.header("Last-Modified: " + php.gmdate("%D, %d %M %Y %H:%i:%s") +
               " GMT")
    php.header("Cache-Control: store, no-cache, must-revalidate")
    php.header("Cache-Control: post-check=0, pre-check=0", False)
示例#3
0
def _drupal_bootstrap(phase):
    if phase == DRUPAL_BOOTSTRAP_CONFIGURATION:
        drupal_initialize_variables()
        # Start a page timer:
        timer_start('page')
        # Initialize the configuration
        conf_init()
    elif phase == DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE:
        # Allow specifying special cache handlers in settings.php, like
        # using memcached or files for storing cache information.
        # If the page_cache_fastpath is set to TRUE in settings.php and
        # page_cache_fastpath (implemented in the special implementation of
        # cache.inc) printed the page and indicated this with a returned TRUE
        # then we are done.
        if (variable_get('page_cache_fastpath', False)
                and page_cache_fastpath()):
            exit()
    elif phase == DRUPAL_BOOTSTRAP_DATABASE:
        # Initialize the database system.  Note that the connection
        # won't be initialized until it is actually requested.
        # ! do nothing !
        # Register autoload functions so that we can access classes and interfaces.
        # spl_autoload_register('drupal_autoload_class')
        # spl_autoload_register('drupal_autoload_interface')
        pass
    elif phase == DRUPAL_BOOTSTRAP_ACCESS:
        # Deny access to blocked IP addresses - t() is not yet available
        if (drupal_is_denied(ip_address())):
            php.header(php.SERVER['SERVER_PROTOCOL'] + ' 403 Forbidden')
            print 'Sorry, ' + check_plain(ip_address()) + ' has been banned.'
            exit()
    elif phase == DRUPAL_BOOTSTRAP_SESSION:
        php.session_set_save_handler('_sess_open', '_sess_close', '_sess_read', \
          '_sess_write', '_sess_destroy_sid', '_sess_gc')
        php.session_start()
    elif phase == DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE:
        # Initialize configuration variables, using values from settings.php
        # if available.
        settings.conf = variable_init( ({} if (settings.conf == None) else \
          settings.conf) )
        # Load plugin handling.
        cache_mode = variable_get('cache', CACHE_DISABLED)
        # Get the page from the cache.
        cache = ('' if (cache_mode == CACHE_DISABLED) else page_get_cache())
        # If the skipping of the bootstrap hooks is not enforced, call hook_boot.
        if (cache_mode != CACHE_AGGRESSIVE):
            invoke_all('boot')
        # If there is a cached page, display it.
        if (cache):
            drupal_page_cache_header(cache)
            # If the skipping of the bootstrap hooks is not enforced, call hook_exit.
            if (cache_mode != CACHE_AGGRESSIVE):
                bootstrap_invoke_all('exit')
            # We are done.
            exit()
        # Prepare for non-cached page workflow.
        drupal_page_header()
    elif phase == DRUPAL_BOOTSTRAP_LANGUAGE:
        drupal_init_language()
    elif phase == DRUPAL_BOOTSTRAP_PATH:
        # Initialize php.GET['q'] prior to loading plugins and invoking hook_init().
        #lib_path.drupal_init_path();
        pass
    elif phase == DRUPAL_BOOTSTRAP_FULL:
        lib_common._drupal_bootstrap_full()
示例#4
0
def drupal_page_cache_header(cache):
    """
   Set HTTP headers in preparation for a cached page response.
  
   The general approach here is that anonymous users can keep a local
   cache of the page, but must revalidate it on every request.  Then,
   they are given a '304 Not Modified' response as long as they stay
   logged out and the page has not been modified.
  """
    # Set default values:
    last_modified = php.gmdate('D, d M Y H:i:s', cache.created) + ' GMT'
    etag = '"' + drupy_md5(last_modified) + '"'
    # See if the client has provided the required HTTP headers:
    if_modified_since =  (php.stripslashes(php.SERVER['HTTP_IF_MODIFIED_SINCE']) \
      if php.isset(php.SERVER, 'HTTP_IF_MODIFIED_SINCE') else False)
    if_none_match = (php.stripslashes(php.SERVER['HTTP_IF_NONE_MATCH']) \
      if php.isset(php.SERVER, 'HTTP_IF_NONE_MATCH') else False)
    if (if_modified_since and if_none_match
            and if_none_match == etag  # etag must match
            and if_modified_since
            == last_modified):  # if-modified-since must match
        php.header(php.SERVER['SERVER_PROTOCOL'] + ' 304 Not Modified')
        # All 304 responses must send an etag if the 200 response for the same
        # object contained an etag
        php.header("Etag: %(etag)s" % {'etag': etag})
        exit()
    # Send appropriate response:
    php.header("Last-Modified: %(last_modified)s" % \
      {'last_modified':last_modified})
    php.header("Etag: %(etag)s" % {'etag': etag})
    # The following headers force validation of cache:
    php.header("Expires: Sun, 19 Nov 1978 05:00:00 GMT")
    php.header("Cache-Control: must-revalidate")
    if (variable_get('page_compression', True)):
        # Determine if the browser accepts gzipped data.
        if (php.strpos(php.SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') == False and \
            php.function_exists('gzencode')):
            # Strip the gzip php.header and run uncompress.
            cache.data = php.gzinflate(
                php.substr(php.substr(cache.data, 10), 0, -8))
        elif (php.function_exists('gzencode')):
            php.header('Content-Encoding: gzip')
    # Send the original request's headers. We send them one after
    # another so PHP's php.header() def can deal with duplicate
    # headers.
    headers = php.explode("\n", cache.headers)
    for php.header_ in headers:
        php.header(php.header_)
    print cache.data
示例#5
0
文件: bootstrap.py 项目: sabren/drupy
def _drupal_bootstrap(phase):
    if phase == DRUPAL_BOOTSTRAP_CONFIGURATION:
        drupal_initialize_variables()
        # Start a page timer:
        timer_start("page")
        # Initialize the configuration
        conf_init()
    elif phase == DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE:
        # Allow specifying special cache handlers in settings.php, like
        # using memcached or files for storing cache information.
        # If the page_cache_fastpath is set to TRUE in settings.php and
        # page_cache_fastpath (implemented in the special implementation of
        # cache.inc) printed the page and indicated this with a returned TRUE
        # then we are done.
        if variable_get("page_cache_fastpath", False) and page_cache_fastpath():
            exit()
    elif phase == DRUPAL_BOOTSTRAP_DATABASE:
        # Initialize the database system.  Note that the connection
        # won't be initialized until it is actually requested.
        # ! do nothing !
        # Register autoload functions so that we can access classes and interfaces.
        # spl_autoload_register('drupal_autoload_class')
        # spl_autoload_register('drupal_autoload_interface')
        pass
    elif phase == DRUPAL_BOOTSTRAP_ACCESS:
        # Deny access to blocked IP addresses - t() is not yet available
        if drupal_is_denied(ip_address()):
            php.header(php.SERVER["SERVER_PROTOCOL"] + " 403 Forbidden")
            print "Sorry, " + check_plain(ip_address()) + " has been banned."
            exit()
    elif phase == DRUPAL_BOOTSTRAP_SESSION:
        php.session_set_save_handler(
            "_sess_open", "_sess_close", "_sess_read", "_sess_write", "_sess_destroy_sid", "_sess_gc"
        )
        php.session_start()
    elif phase == DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE:
        # Initialize configuration variables, using values from settings.php
        # if available.
        settings.conf = variable_init(({} if (settings.conf == None) else settings.conf))
        # Load plugin handling.
        cache_mode = variable_get("cache", CACHE_DISABLED)
        # Get the page from the cache.
        cache = "" if (cache_mode == CACHE_DISABLED) else page_get_cache()
        # If the skipping of the bootstrap hooks is not enforced, call hook_boot.
        if cache_mode != CACHE_AGGRESSIVE:
            invoke_all("boot")
        # If there is a cached page, display it.
        if cache:
            drupal_page_cache_header(cache)
            # If the skipping of the bootstrap hooks is not enforced, call hook_exit.
            if cache_mode != CACHE_AGGRESSIVE:
                bootstrap_invoke_all("exit")
            # We are done.
            exit()
        # Prepare for non-cached page workflow.
        drupal_page_header()
    elif phase == DRUPAL_BOOTSTRAP_LANGUAGE:
        drupal_init_language()
    elif phase == DRUPAL_BOOTSTRAP_PATH:
        # Initialize php.GET['q'] prior to loading plugins and invoking hook_init().
        # lib_path.drupal_init_path();
        pass
    elif phase == DRUPAL_BOOTSTRAP_FULL:
        lib_common._drupal_bootstrap_full()
示例#6
0
文件: bootstrap.py 项目: sabren/drupy
def drupal_page_cache_header(cache):
    """
   Set HTTP headers in preparation for a cached page response.
  
   The general approach here is that anonymous users can keep a local
   cache of the page, but must revalidate it on every request.  Then,
   they are given a '304 Not Modified' response as long as they stay
   logged out and the page has not been modified.
  """
    # Set default values:
    last_modified = php.gmdate("D, d M Y H:i:s", cache.created) + " GMT"
    etag = '"' + drupy_md5(last_modified) + '"'
    # See if the client has provided the required HTTP headers:
    if_modified_since = (
        php.stripslashes(php.SERVER["HTTP_IF_MODIFIED_SINCE"])
        if php.isset(php.SERVER, "HTTP_IF_MODIFIED_SINCE")
        else False
    )
    if_none_match = (
        php.stripslashes(php.SERVER["HTTP_IF_NONE_MATCH"]) if php.isset(php.SERVER, "HTTP_IF_NONE_MATCH") else False
    )
    if (
        if_modified_since
        and if_none_match
        and if_none_match == etag  # etag must match
        and if_modified_since == last_modified
    ):  # if-modified-since must match
        php.header(php.SERVER["SERVER_PROTOCOL"] + " 304 Not Modified")
        # All 304 responses must send an etag if the 200 response for the same
        # object contained an etag
        php.header("Etag: %(etag)s" % {"etag": etag})
        exit()
    # Send appropriate response:
    php.header("Last-Modified: %(last_modified)s" % {"last_modified": last_modified})
    php.header("Etag: %(etag)s" % {"etag": etag})
    # The following headers force validation of cache:
    php.header("Expires: Sun, 19 Nov 1978 05:00:00 GMT")
    php.header("Cache-Control: must-revalidate")
    if variable_get("page_compression", True):
        # Determine if the browser accepts gzipped data.
        if php.strpos(php.SERVER["HTTP_ACCEPT_ENCODING"], "gzip") == False and php.function_exists("gzencode"):
            # Strip the gzip php.header and run uncompress.
            cache.data = php.gzinflate(php.substr(php.substr(cache.data, 10), 0, -8))
        elif php.function_exists("gzencode"):
            php.header("Content-Encoding: gzip")
    # Send the original request's headers. We send them one after
    # another so PHP's php.header() def can deal with duplicate
    # headers.
    headers = php.explode("\n", cache.headers)
    for php.header_ in headers:
        php.header(php.header_)
    print cache.data