Page Speed Optimization Libraries
1.6.29.3
|
#include "server_context.h"
Public Types | |
typedef std::pair < RewriteOptions *, bool > | OptionsBoolPair |
Public Member Functions | |
ServerContext (RewriteDriverFactory *factory) | |
void | SetDefaultLongCacheHeaders (const ContentType *content_type, ResponseHeaders *header) const |
void | SetDefaultLongCacheHeadersWithCharset (const ContentType *content_type, StringPiece charset, ResponseHeaders *header) const |
void | SetContentType (const ContentType *content_type, ResponseHeaders *header) |
Changes the content type of a pre-initialized header. | |
void | set_filename_prefix (const StringPiece &file_prefix) |
void | set_statistics (Statistics *x) |
void | set_rewrite_stats (RewriteStats *x) |
void | set_lock_manager (NamedLockManager *x) |
void | set_enable_property_cache (bool enabled) |
void | set_message_handler (MessageHandler *x) |
StringPiece | filename_prefix () const |
Statistics * | statistics () const |
NamedLockManager * | lock_manager () const |
RewriteDriverFactory * | factory () const |
ThreadSynchronizer * | thread_synchronizer () |
ExperimentMatcher * | experiment_matcher () |
void | ApplyInputCacheControl (const ResourceVector &inputs, ResponseHeaders *headers) |
bool | IsPagespeedResource (const GoogleUrl &url) |
Is this URL a ref to a Pagespeed resource? | |
void | ComputeSignature (RewriteOptions *rewrite_options) const |
Hasher * | hasher () const |
const Hasher * | lock_hasher () const |
const Hasher * | contents_hasher () const |
FileSystem * | file_system () |
void | set_file_system (FileSystem *fs) |
FilenameEncoder * | filename_encoder () const |
void | set_filename_encoder (FilenameEncoder *x) |
UrlNamer * | url_namer () const |
void | set_url_namer (UrlNamer *n) |
RewriteOptionsManager * | rewrite_options_manager () const |
void | SetRewriteOptionsManager (RewriteOptionsManager *rom) |
Takes ownership of RewriteOptionsManager. | |
StaticAssetManager * | static_asset_manager () const |
void | set_static_asset_manager (StaticAssetManager *manager) |
Scheduler * | scheduler () const |
void | set_scheduler (Scheduler *s) |
bool | has_default_system_fetcher () |
bool | has_default_distributed_fetcher () |
UrlAsyncFetcher * | DefaultSystemFetcher () |
UrlAsyncFetcher * | DefaultDistributedFetcher () |
Timer * | timer () const |
void | MakePropertyCaches (CacheInterface *backend_cache) |
HTTPCache * | http_cache () const |
void | set_http_cache (HTTPCache *x) |
PropertyCache * | page_property_cache () const |
const PropertyCache::Cohort * | dom_cohort () const |
void | set_dom_cohort (const PropertyCache::Cohort *c) |
const PropertyCache::Cohort * | blink_cohort () const |
void | set_blink_cohort (const PropertyCache::Cohort *c) |
const PropertyCache::Cohort * | beacon_cohort () const |
void | set_beacon_cohort (const PropertyCache::Cohort *c) |
CacheInterface * | filesystem_metadata_cache () const |
void | set_filesystem_metadata_cache (CacheInterface *x) |
CacheInterface * | metadata_cache () const |
void | set_metadata_cache (CacheInterface *x) |
CriticalCssFinder * | critical_css_finder () const |
void | set_critical_css_finder (CriticalCssFinder *finder) |
CriticalImagesFinder * | critical_images_finder () const |
void | set_critical_images_finder (CriticalImagesFinder *finder) |
CriticalSelectorFinder * | critical_selector_finder () const |
void | set_critical_selector_finder (CriticalSelectorFinder *finder) |
FlushEarlyInfoFinder * | flush_early_info_finder () const |
void | set_flush_early_info_finder (FlushEarlyInfoFinder *finder) |
UserAgentMatcher * | user_agent_matcher () const |
void | set_user_agent_matcher (UserAgentMatcher *n) |
CacheHtmlInfoFinder * | cache_html_info_finder () const |
SimpleRandom * | simple_random () |
void | set_cache_html_info_finder (CacheHtmlInfoFinder *finder) |
bool | store_outputs_in_file_system () |
void | set_store_outputs_in_file_system (bool store) |
RewriteStats * | rewrite_stats () const |
MessageHandler * | message_handler () const |
NamedLock * | MakeCreationLock (const GoogleString &name) |
NamedLock * | MakeInputLock (const GoogleString &name) |
Makes a lock used for fetching and optimizing an input resource. | |
bool | TryLockForCreation (NamedLock *creation_lock) |
Attempt to obtain a named lock without blocking. Return true if we do so. | |
void | LockForCreation (NamedLock *creation_lock, QueuedWorkerPool::Sequence *worker, Function *callback) |
void | set_hasher (Hasher *hasher) |
Setters should probably only be used in testing. | |
void | set_default_system_fetcher (UrlAsyncFetcher *fetcher) |
void | set_default_distributed_fetcher (UrlAsyncFetcher *fetcher) |
bool | HandleBeacon (StringPiece body, StringPiece user_agent, const RequestContextPtr &request_context) |
RewriteOptions * | global_options () |
const RewriteOptions * | global_options () const |
void | reset_global_options (RewriteOptions *options) |
RewriteOptions * | NewOptions () |
Makes a new, empty set of RewriteOptions. | |
OptionsBoolPair | GetQueryOptions (GoogleUrl *request_url, RequestHeaders *request_headers, ResponseHeaders *response_headers) |
RewriteOptions * | GetCustomOptions (RequestHeaders *request_headers, RewriteOptions *domain_options, RewriteOptions *query_options) |
GoogleString | GetPagePropertyCacheKey (StringPiece url, const RewriteOptions *options, StringPiece device_type_suffix) |
GoogleString | GetPagePropertyCacheKey (StringPiece url, StringPiece options_signature_hash, StringPiece device_type_suffix) |
GoogleString | GetFallbackPagePropertyCacheKey (const GoogleUrl &request_url, const RewriteOptions *options, StringPiece device_type_suffix) |
RewriteDriver * | NewRewriteDriver (const RequestContextPtr &request_ctx) |
RewriteDriver * | NewRewriteDriverFromPool (RewriteDriverPool *pool, const RequestContextPtr &request_ctx) |
RewriteDriver * | NewUnmanagedRewriteDriver (RewriteDriverPool *pool, RewriteOptions *options, const RequestContextPtr &request_ctx) |
RewriteDriver * | NewCustomRewriteDriver (RewriteOptions *custom_options, const RequestContextPtr &request_ctx) |
void | ReleaseRewriteDriver (RewriteDriver *rewrite_driver) |
ThreadSystem * | thread_system () |
UsageDataReporter * | usage_data_reporter () |
void | set_shutting_down () |
bool | shutting_down () const |
void | ShutDownDrivers () |
void | MergeNonCachingResponseHeaders (const ResourcePtr &input, const OutputResourcePtr &output) |
void | MergeNonCachingResponseHeaders (const ResponseHeaders &input_headers, ResponseHeaders *output_headers) |
Entry-point with the same functionality, exposed for easier testing. | |
QueuedWorkerPool * | html_workers () |
Pool of worker-threads that can be used to handle html-parsing. | |
QueuedWorkerPool * | rewrite_workers () |
Pool of worker-threads that can be used to handle resource rewriting. | |
QueuedWorkerPool * | low_priority_rewrite_workers () |
size_t | num_active_rewrite_drivers () |
void | InitWorkersAndDecodingDriver () |
bool | response_headers_finalized () const |
void | set_response_headers_finalized (bool x) |
RewriteDriverPool * | standard_rewrite_driver_pool () |
PropertyCache * | MakePropertyCache (const GoogleString &cache_key_prefix, CacheInterface *cache) const |
Builds a PropertyCache given a key prefix and a CacheInterface. | |
const GoogleString & | hostname () const |
Returns the current server hostname. | |
void | set_hostname (const GoogleString &x) |
void | AddOriginalContentLengthHeader (const ResourceVector &inputs, ResponseHeaders *headers) |
virtual RewriteDriverPool * | SelectDriverPool (bool using_spdy) |
Chooses a driver pool based on the request protocol. | |
virtual void | ApplySessionFetchers (const RequestContextPtr &req, RewriteDriver *driver) |
const RewriteDriver * | decoding_driver () const |
virtual bool | ProxiesHtml () const =0 |
RequestProperties * | NewRequestProperties () |
Makes a new RequestProperties. | |
void | DeleteCacheOnDestruction (CacheInterface *cache) |
Static Public Member Functions | |
static bool | ScanSplitHtmlRequest (const RequestContextPtr &ctx, const RewriteOptions *options, GoogleString *url) |
static bool | IsExcludedAttribute (const char *attribute) |
Static Public Attributes | |
static const int64 | kGeneratedMaxAgeMs |
The lifetime for cache-extended generated resources, in milliseconds. | |
static const char | kResourceEtagValue [] |
static const char | kCacheKeyResourceNamePrefix [] |
static const char | kStatisticsGroup [] |
Default statistics group name. | |
Protected Member Functions | |
void | ManageRewriteDriverPool (RewriteDriverPool *pool) |
Friends | |
class | ServerContextTest |
Server-specific context and platform adaption: threads, file system, locking, and so on.
void net_instaweb::ServerContext::AddOriginalContentLengthHeader | ( | const ResourceVector & | inputs, |
ResponseHeaders * | headers | ||
) |
Adds an X-Original-Content-Length header to the response headers based on the size of the input resources.
void net_instaweb::ServerContext::ApplyInputCacheControl | ( | const ResourceVector & | inputs, |
ResponseHeaders * | headers | ||
) |
Computes the most restrictive Cache-Control intersection of the input resources, and the provided headers, and sets that cache-control on the provided headers. Does nothing if all of the resources are fully cacheable, since in that case we will want to cache-extend.
Disregards Cache-Control directives other than max-age, no-cache, no-store, and private, and strips them if any resource is no-cache or private. By assumption, a resource can only be no-store if it is also no-cache.
virtual void net_instaweb::ServerContext::ApplySessionFetchers | ( | const RequestContextPtr & | req, |
RewriteDriver * | driver | ||
) | [virtual] |
Provides a hook for ServerContext implementations to determine the fetcher implementation based on the request.
Reimplemented in net_instaweb::ApacheServerContext.
Note: for rewriting user content, you want to use RewriteDriver's async_fetcher() instead, as it may apply session-specific optimizations.
void net_instaweb::ServerContext::DeleteCacheOnDestruction | ( | CacheInterface * | cache | ) |
Puts the cache on a list to be destroyed at the last phase of system shutdown.
CacheInterface* net_instaweb::ServerContext::filesystem_metadata_cache | ( | ) | const [inline] |
Cache for storing file system metadata. It must be private to a server, preferably but not necessarily shared between its processes, and is required if using load-from-file and memcached (or any cache shared between servers). This class does not take ownership.
RewriteOptions* net_instaweb::ServerContext::GetCustomOptions | ( | RequestHeaders * | request_headers, |
RewriteOptions * | domain_options, | ||
RewriteOptions * | query_options | ||
) |
Returns any custom options required for this request, incorporating any domain-specific options from the UrlNamer, options set in query-params, and options set in request headers. Takes ownership of domain_options and query_options.
GoogleString net_instaweb::ServerContext::GetFallbackPagePropertyCacheKey | ( | const GoogleUrl & | request_url, |
const RewriteOptions * | options, | ||
StringPiece | device_type_suffix | ||
) |
Returns the page property cache key for the page containing fallback values (i.e. wihtout query params or without leaf) to be used for the proxy interface flow. Options are expected to be frozen.
GoogleString net_instaweb::ServerContext::GetPagePropertyCacheKey | ( | StringPiece | url, |
const RewriteOptions * | options, | ||
StringPiece | device_type_suffix | ||
) |
Returns the page property cache key to be used for the proxy interface flow. options is expected to be frozen.
OptionsBoolPair net_instaweb::ServerContext::GetQueryOptions | ( | GoogleUrl * | request_url, |
RequestHeaders * | request_headers, | ||
ResponseHeaders * | response_headers | ||
) |
Returns any options set in query-params or in the headers. Possible return-value scenarios for the pair are: first==*, .second==false: query-params or headers failed in parse. We return 405 in this case (see ProxyInterface::ProxyRequest). first==NULL, .second==true: No query-params or headers are present. This is treated as if there are no query param (or header) options. first!=NULL, .second==true: Use query-params. It also strips off the ModPageSpeed query parameters and headers from the request_url, request headers, and response headers respectively.
Returns a pointer to the master global_options. These are not used directly in RewriteDrivers, but are Cloned into the drivers as they are created. We generally do not expect global_options() to change once the system is processing requests, except in Apache when someone does a cache-flush by touching a file "cache.flush" in the file-cache directory.
const RewriteOptions* net_instaweb::ServerContext::global_options | ( | ) | const |
Returns a pointer to the master global_options without modifying the ServerContext.
bool net_instaweb::ServerContext::HandleBeacon | ( | StringPiece | body, |
StringPiece | user_agent, | ||
const RequestContextPtr & | request_context | ||
) |
Handles an incoming beacon request by incrementing the appropriate variables. Returns true if the url was parsed and handled correctly; in this case a 204 No Content response should be sent. Returns false if the url could not be parsed; in this case the request should be declined. body should be either the query params or the POST body, depending on how the beacon was sent, from the beacon request.
Hasher* net_instaweb::ServerContext::hasher | ( | ) | const [inline] |
A ServerContext may be created in one phase, and later populated with all its dependencies. This populates the worker threads and a RewriteDriver used just for quickly decoding (but not serving) URLs.
static bool net_instaweb::ServerContext::IsExcludedAttribute | ( | const char * | attribute | ) | [static] |
Returns whether or not this attribute can be merged into headers without additional considerations.
void net_instaweb::ServerContext::LockForCreation | ( | NamedLock * | creation_lock, |
QueuedWorkerPool::Sequence * | worker, | ||
Function * | callback | ||
) |
Attempt to obtain a named lock. When the lock has been obtained, queue the callback on the given worker Sequence. If the lock times out, cancel the callback, running the cancel on the worker.
QueuedWorkerPool* net_instaweb::ServerContext::low_priority_rewrite_workers | ( | ) | [inline] |
Pool of worker-threads that can be used to handle low-priority/high CPU portions of resource rewriting.
NamedLock* net_instaweb::ServerContext::MakeCreationLock | ( | const GoogleString & | name | ) |
Allocate an NamedLock to guard the creation of the given resource. If the object is expensive to create, this lock should be held during its creation to avoid multiple rewrites happening at once. The lock will be unlocked when creation_lock is reset or destructed.
void net_instaweb::ServerContext::ManageRewriteDriverPool | ( | RewriteDriverPool * | pool | ) | [inline, protected] |
Takes ownership of the given pool, making sure to clean it up at the appropriate spot during shutdown.
void net_instaweb::ServerContext::MergeNonCachingResponseHeaders | ( | const ResourcePtr & | input, |
const OutputResourcePtr & | output | ||
) | [inline] |
Take any headers that are not caching-related, and not otherwise filled in by SetDefaultLongCacheHeaders or SetContentType, but were* set on the input resource, and copy them to the output resource. This allows user headers to be preserved. This must be called as needed by individual filters, prior to Write().
Note that this API is only usable for single-input rewriters. Combiners will need to execute some kind of merge, union, or intersection policy, if we wish to preserve origin response headers.
Note: this does not call ComputeCaching() on the output headers, so that method must be called prior to invoking any caching predicates on the output's ResponseHeader. In theory we shouldn't mark the caching bits dirty because we are only adding headers that will not affect caching, but at the moment the dirty-bit is set independent of that.
CacheInterface* net_instaweb::ServerContext::metadata_cache | ( | ) | const [inline] |
Cache for small non-HTTP objects. This class does not take ownership.
Note that this might share namespace with the HTTP cache, so make sure your key names do not start with http://.
RewriteDriver* net_instaweb::ServerContext::NewCustomRewriteDriver | ( | RewriteOptions * | custom_options, |
const RequestContextPtr & | request_ctx | ||
) |
Like NewUnmanagedRewriteDriver, but uses standard semi-automatic memory management for RewriteDrivers.
NOTE: This does not merge custom_options with global_options(), the caller must do that if they want them merged.
Filters allocated using this mechanism have their filter-chain already frozen (see AddFilters()).
Takes ownership of 'custom_options'.
RewriteDriver* net_instaweb::ServerContext::NewRewriteDriver | ( | const RequestContextPtr & | request_ctx | ) |
Generates a new managed RewriteDriver using the RewriteOptions managed by this class. Each RewriteDriver is not thread-safe, but you can generate a RewriteDriver* for each thread. The returned drivers manage themselves: when the HTML parsing and rewriting is done they will be returned to the pool.
Filters allocated using this mechanism have their filter-chain already frozen (see AddFilters()).
RewriteDriver* net_instaweb::ServerContext::NewRewriteDriverFromPool | ( | RewriteDriverPool * | pool, |
const RequestContextPtr & | request_ctx | ||
) |
As above, but uses a specific RewriteDriverPool to determine the options and manage the lifetime of the result. 'pool' must not be NULL.
RewriteDriver* net_instaweb::ServerContext::NewUnmanagedRewriteDriver | ( | RewriteDriverPool * | pool, |
RewriteOptions * | options, | ||
const RequestContextPtr & | request_ctx | ||
) |
Generates a new unmanaged RewriteDriver with given RewriteOptions, which are assumed to correspond to drivers managed by 'pool' (which may be NULL if the options are custom). Each RewriteDriver is not thread-safe, but you can generate a RewriteDriver* for each thread. The returned drivers must be explicitly deleted by the caller.
RewriteDrivers allocated using this mechanism have not yet frozen their filters, and so callers may explicitly enable individual filters on the driver -- beyond those indicated in the options. After all extra filters are added, AddFilters must be called to freeze them and instantiate the filter-chain.
Takes ownership of 'options'.
Returns the number of rewrite drivers that we were aware of at the time of the call. This includes those created via NewCustomRewriteDriver and NewRewriteDriver, but not via NewUnmanagedRewriteDriver.
virtual bool net_instaweb::ServerContext::ProxiesHtml | ( | ) | const [pure virtual] |
Determines whether in this server, it makes sense to proxy HTML from external sources. If we're acting as a reverse proxy that talks to the backend over HTTP, it makes sense to set this to 'true'. The JavaScript loaded from the HTML on the origin domain will be given full access to cookies on the proxied domain.
For resource-proxying (e.g. ModPagespeedMapProxyDomain) this should be set to 'false' as that command is intended only for reosurces, not for HTML.
Implemented in net_instaweb::ApacheServerContext.
void net_instaweb::ServerContext::ReleaseRewriteDriver | ( | RewriteDriver * | rewrite_driver | ) |
Puts a RewriteDriver back on the free pool. This is intended to be called by a RewriteDriver on itself, once all pending activites on it have completed, including HTML Parsing (FinishParse) and all pending Rewrites.
void net_instaweb::ServerContext::reset_global_options | ( | RewriteOptions * | options | ) |
Note that you have to ensure the argument has the right type in case a subclass of RewriteOptions is in use. This should also not be called once request processing has commenced.
bool net_instaweb::ServerContext::response_headers_finalized | ( | ) | const [inline] |
Determines whether we can assume that the response headers we see in rewrite_drivers when filters are applied reflect the final form from the origin. In proxy applications, this is generally true. But in Apache, it depends when the output_filter is applied relative to mod_headers and mod_expires.
The default-value is 'true'.
static bool net_instaweb::ServerContext::ScanSplitHtmlRequest | ( | const RequestContextPtr & | ctx, |
const RewriteOptions * | options, | ||
GoogleString * | url | ||
) | [static] |
Checks the url for the split html ATF/BTF query param. If present, it strips the param from the url, and sets a bit in the request context indicating which chunk of the split response was requested. Returns true if it found a query param.
void net_instaweb::ServerContext::set_shutting_down | ( | ) | [inline] |
Calling this method will stop results of rewrites being cached in the metadata cache. This is meant for the shutdown sequence.
void net_instaweb::ServerContext::SetDefaultLongCacheHeaders | ( | const ContentType * | content_type, |
ResponseHeaders * | header | ||
) | const [inline] |
Set time and cache headers with long TTL (including Date, Last-Modified, Cache-Control, Etags, Expires).
Also sets Content-Type headers if content_type is provided. If content_type is null, the Content-Type header is omitted.
void net_instaweb::ServerContext::SetDefaultLongCacheHeadersWithCharset | ( | const ContentType * | content_type, |
StringPiece | charset, | ||
ResponseHeaders * | header | ||
) | const |
As above, but also sets charset if it's non-empty and content_type is non-NULL.
Waits a bounded amount of time for all currently running jobs to complete. This is meant for use when shutting down processing, so that jobs running in background do not access objects that are about to be deleted. If there are long-running outstanding tasks, the drivers may stay running past this call.
Returns the RewriteDriverPool that's used by NewRewriteDriver (so calling NewRewriteDriverFromPool(standard_rewrite_driver_pool()) is equivalent to calling NewRewriteDriver.
bool net_instaweb::ServerContext::store_outputs_in_file_system | ( | ) | [inline] |
Whether or not dumps of rewritten resources should be stored to the filesystem. This is meant for testing purposes only.
const char net_instaweb::ServerContext::kResourceEtagValue[] [static] |
This value is a shared constant so that it can also be used in the Apache-specific code that repairs our caching headers downstream of mod_headers.