20 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
21 #define NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
30 #include "net/instaweb/http/public/request_context.h"
33 #include "net/instaweb/util/public/property_cache.h"
34 #include "pagespeed/kernel/base/atomic_bool.h"
35 #include "pagespeed/kernel/base/basictypes.h"
36 #include "pagespeed/kernel/base/md5_hasher.h"
37 #include "pagespeed/kernel/base/ref_counted_ptr.h"
38 #include "pagespeed/kernel/base/scoped_ptr.h"
39 #include "pagespeed/kernel/base/string.h"
40 #include "pagespeed/kernel/base/string_util.h"
41 #include "pagespeed/kernel/thread/queued_worker_pool.h"
42 #include "pagespeed/kernel/util/simple_random.h"
44 namespace pagespeed {
namespace js {
struct JsTokenizerPatterns; } }
46 namespace net_instaweb {
50 class CacheHtmlInfoFinder;
52 class CachePropertyStore;
53 class CriticalCssFinder;
54 class CriticalImagesFinder;
55 class CriticalLineInfoFinder;
56 class CriticalSelectorFinder;
57 class RequestProperties;
58 class ExperimentMatcher;
60 class FlushEarlyInfoFinder;
66 class NamedLockManager;
69 class ResponseHeaders;
71 class RewriteDriverFactory;
72 class RewriteDriverPool;
75 class RewriteOptionsManager;
80 class StaticAssetManager;
82 class ThreadSynchronizer;
85 class UrlAsyncFetcher;
87 class UsageDataReporter;
88 class UserAgentMatcher;
91 typedef RefCountedPtr<OutputResource> OutputResourcePtr;
92 typedef std::vector<OutputResourcePtr> OutputResourceVector;
103 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair;
114 static const char kCacheKeyResourceNamePrefix[];
132 const ContentType* content_type, StringPiece charset,
133 StringPiece cache_control_suffix, ResponseHeaders* header)
const;
135 void set_filename_prefix(
const StringPiece& file_prefix);
136 void set_statistics(Statistics* x) { statistics_ = x; }
137 void set_rewrite_stats(
RewriteStats* x) { rewrite_stats_ = x; }
138 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; }
139 void set_enable_property_cache(
bool enabled);
140 void set_message_handler(MessageHandler* x) { message_handler_ = x; }
142 StringPiece filename_prefix()
const {
return file_prefix_; }
143 Statistics* statistics()
const {
return statistics_; }
144 NamedLockManager* lock_manager()
const {
return lock_manager_; }
145 RewriteDriverFactory* factory()
const {
return factory_; }
146 ThreadSynchronizer* thread_synchronizer() {
147 return thread_synchronizer_.get();
149 ExperimentMatcher* experiment_matcher() {
return experiment_matcher_.get(); }
160 ResponseHeaders* headers);
171 const RewriteOptions* options,
172 const UrlNamer* url_namer,
173 StringVector* decoded_urls)
const;
175 void ComputeSignature(RewriteOptions* rewrite_options)
const;
178 Hasher*
hasher()
const {
return hasher_; }
179 const Hasher* lock_hasher()
const {
return &lock_hasher_; }
180 const Hasher* contents_hasher()
const {
return &contents_hasher_; }
181 FileSystem* file_system() {
return file_system_; }
182 void set_file_system(FileSystem* fs ) { file_system_ = fs; }
183 UrlNamer* url_namer()
const {
return url_namer_; }
184 void set_url_namer(UrlNamer* n) { url_namer_ = n; }
185 RewriteOptionsManager* rewrite_options_manager()
const {
186 return rewrite_options_manager_.get();
188 SHA1Signature* signature()
const {
return signature_; }
191 StaticAssetManager* static_asset_manager()
const {
192 return static_asset_manager_;
194 void set_static_asset_manager(StaticAssetManager* manager) {
195 static_asset_manager_ = manager;
197 Scheduler* scheduler()
const {
return scheduler_; }
198 void set_scheduler(Scheduler* s) { scheduler_ = s; }
199 bool has_default_system_fetcher()
const {
200 return default_system_fetcher_ != NULL;
202 bool has_default_distributed_fetcher() {
203 return default_distributed_fetcher_ != NULL;
210 return default_distributed_fetcher_;
217 const RewriteOptions* options,
const GoogleString& fragment,
218 CacheUrlAsyncFetcher::AsyncOpHooks* hooks, UrlAsyncFetcher* fetcher);
220 Timer* timer()
const {
return timer_; }
225 HTTPCache* http_cache()
const {
return http_cache_.get(); }
226 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); }
231 PropertyCache* page_property_cache()
const {
232 return page_property_cache_.get();
235 const PropertyCache::Cohort* dom_cohort()
const {
return dom_cohort_; }
236 void set_dom_cohort(
const PropertyCache::Cohort* c) { dom_cohort_ = c; }
238 const PropertyCache::Cohort* blink_cohort()
const {
return blink_cohort_; }
239 void set_blink_cohort(
const PropertyCache::Cohort* c) { blink_cohort_ = c; }
241 const PropertyCache::Cohort* beacon_cohort()
const {
return beacon_cohort_; }
242 void set_beacon_cohort(
const PropertyCache::Cohort* c) { beacon_cohort_ = c; }
244 const PropertyCache::Cohort* fix_reflow_cohort()
const {
245 return fix_reflow_cohort_;
247 void set_fix_reflow_cohort(
const PropertyCache::Cohort* c) {
248 fix_reflow_cohort_ = c;
256 return filesystem_metadata_cache_;
258 void set_filesystem_metadata_cache(CacheInterface* x) {
259 filesystem_metadata_cache_ = x;
267 void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; }
269 CriticalCssFinder* critical_css_finder()
const {
270 return critical_css_finder_.get();
272 void set_critical_css_finder(CriticalCssFinder* finder);
274 CriticalImagesFinder* critical_images_finder()
const {
275 return critical_images_finder_.get();
277 void set_critical_images_finder(CriticalImagesFinder* finder);
279 CriticalSelectorFinder* critical_selector_finder()
const {
280 return critical_selector_finder_.get();
282 void set_critical_selector_finder(CriticalSelectorFinder* finder);
284 FlushEarlyInfoFinder* flush_early_info_finder()
const {
285 return flush_early_info_finder_.get();
287 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder);
289 UserAgentMatcher* user_agent_matcher()
const {
290 return user_agent_matcher_;
292 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; }
294 CacheHtmlInfoFinder* cache_html_info_finder()
const {
295 return cache_html_info_finder_.get();
298 SimpleRandom* simple_random() {
299 return &simple_random_;
302 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder);
304 CriticalLineInfoFinder* critical_line_info_finder()
const {
305 return critical_line_info_finder_.get();
314 void set_store_outputs_in_file_system(
bool store) {
315 store_outputs_in_file_system_ = store;
318 RewriteStats* rewrite_stats()
const {
return rewrite_stats_; }
319 MessageHandler* message_handler()
const {
return message_handler_; }
337 QueuedWorkerPool::Sequence* worker, Function* callback);
341 void set_signature(SHA1Signature* signature) { signature_ = signature; }
342 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) {
343 default_system_fetcher_ = fetcher;
345 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) {
346 default_distributed_fetcher_ = fetcher;
356 StringPiece user_agent,
357 const RequestContextPtr& request_context);
398 const RewriteOptions* domain_options,
399 GoogleUrl* request_url,
400 RequestHeaders* request_headers,
401 ResponseHeaders* response_headers,
402 RewriteQuery* rewrite_query);
409 const RewriteOptions* options,
417 RewriteOptions* domain_options,
418 RewriteOptions* query_options);
437 RewriteDriverPool* pool,
const RequestContextPtr& request_ctx);
453 RewriteDriverPool* pool, RewriteOptions* options,
454 const RequestContextPtr& request_ctx);
467 RewriteOptions* custom_options,
const RequestContextPtr& request_ctx);
483 ThreadSystem* thread_system() {
return thread_system_; }
484 UsageDataReporter* usage_data_reporter() {
return usage_data_reporter_; }
489 shutting_down_.set_value(
true);
492 bool shutting_down()
const {
493 return shutting_down_.value();
528 const OutputResourcePtr& output) {
530 output->response_headers());
535 ResponseHeaders* output_headers);
546 return low_priority_rewrite_workers_;
573 return response_headers_finalized_;
575 void set_response_headers_finalized(
bool x) {
576 response_headers_finalized_ = x;
583 return available_rewrite_drivers_.get();
590 void set_hostname(
const GoogleString& x) {
597 ResponseHeaders* headers);
605 RewriteDriver* driver);
626 void set_cache_property_store(CachePropertyStore* p);
639 const GoogleString& cohort_name,
640 PropertyCache* pcache);
647 const GoogleString& cohort_name,
648 CacheInterface* cache,
649 PropertyCache* pcache);
655 const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns()
const {
656 return js_tokenizer_patterns_;
667 AsyncFetch* fetch, RewriteOptions* options);
679 virtual GoogleString
FormatOption(StringPiece option_name, StringPiece args);
685 additional_driver_pools_.push_back(pool);
689 friend class ServerContextTest;
690 typedef std::set<RewriteDriver*> RewriteDriverSet;
693 void ReleaseRewriteDriverImpl(
RewriteDriver* rewrite_driver);
696 ThreadSystem* thread_system_;
698 GoogleString file_prefix_;
699 FileSystem* file_system_;
701 scoped_ptr<RewriteOptionsManager> rewrite_options_manager_;
702 UserAgentMatcher* user_agent_matcher_;
703 Scheduler* scheduler_;
707 SHA1Signature* signature_;
708 scoped_ptr<CriticalImagesFinder> critical_images_finder_;
709 scoped_ptr<CriticalCssFinder> critical_css_finder_;
710 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_;
711 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_;
712 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_;
713 scoped_ptr<CriticalLineInfoFinder> critical_line_info_finder_;
718 MD5Hasher lock_hasher_;
722 MD5Hasher contents_hasher_;
724 Statistics* statistics_;
727 scoped_ptr<HTTPCache> http_cache_;
728 scoped_ptr<PropertyCache> page_property_cache_;
729 CacheInterface* filesystem_metadata_cache_;
730 CacheInterface* metadata_cache_;
732 bool store_outputs_in_file_system_;
733 bool response_headers_finalized_;
734 bool enable_property_cache_;
736 NamedLockManager* lock_manager_;
737 MessageHandler* message_handler_;
739 const PropertyCache::Cohort* dom_cohort_;
740 const PropertyCache::Cohort* blink_cohort_;
741 const PropertyCache::Cohort* beacon_cohort_;
742 const PropertyCache::Cohort* fix_reflow_cohort_;
749 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_;
752 std::vector<RewriteDriverPool*> additional_driver_pools_;
759 RewriteDriverSet active_rewrite_drivers_;
768 bool trying_to_cleanup_rewrite_drivers_;
769 RewriteDriverSet deferred_release_rewrite_drivers_;
770 bool shutdown_drivers_called_;
776 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_;
780 scoped_ptr<RewriteOptions> base_class_options_;
787 QueuedWorkerPool* html_workers_;
788 QueuedWorkerPool* rewrite_workers_;
789 QueuedWorkerPool* low_priority_rewrite_workers_;
791 AtomicBool shutting_down_;
798 scoped_ptr<ThreadSynchronizer> thread_synchronizer_;
801 scoped_ptr<ExperimentMatcher> experiment_matcher_;
806 GoogleString hostname_;
810 SimpleRandom simple_random_;
812 const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_;
814 scoped_ptr<CachePropertyStore> cache_property_store_;
Definition: url_namer.h:37
CacheUrlAsyncFetcher * CreateCustomCacheFetcher(const RewriteOptions *options, const GoogleString &fragment, CacheUrlAsyncFetcher::AsyncOpHooks *hooks, UrlAsyncFetcher *fetcher)
const PropertyCache::Cohort * AddCohort(const GoogleString &cohort_name, PropertyCache *pcache)
const CacheInterface * pcache_cache_backend()
void MergeNonCachingResponseHeaders(const ResourcePtr &input, const OutputResourcePtr &output)
Definition: server_context.h:527
void SetDefaultLongCacheHeaders(const ContentType *content_type, StringPiece charset, StringPiece cache_control_suffix, ResponseHeaders *header) const
void LockForCreation(NamedLock *creation_lock, QueuedWorkerPool::Sequence *worker, Function *callback)
bool GetQueryOptions(const RequestContextPtr &request_context, const RewriteOptions *domain_options, GoogleUrl *request_url, RequestHeaders *request_headers, ResponseHeaders *response_headers, RewriteQuery *rewrite_query)
void ShowCacheHandler(Format format, StringPiece url, StringPiece ua, AsyncFetch *fetch, RewriteOptions *options)
Definition: static_asset_manager.h:44
RewriteOptions * GetCustomOptions(RequestHeaders *request_headers, RewriteOptions *domain_options, RewriteOptions *query_options)
void set_critical_line_info_finder(CriticalLineInfoFinder *finder)
Takes ownership of the passed in finder.
virtual void ApplySessionFetchers(const RequestContextPtr &req, RewriteDriver *driver)
virtual bool ProxiesHtml() const =0
bool TryLockForCreation(NamedLock *creation_lock)
Attempt to obtain a named lock without blocking. Return true if we do so.
void set_shutting_down()
Definition: server_context.h:488
static bool ScanSplitHtmlRequest(const RequestContextPtr &ctx, const RewriteOptions *options, GoogleString *url)
RewriteDriver * NewRewriteDriverFromPool(RewriteDriverPool *pool, const RequestContextPtr &request_ctx)
RewriteDriverPool * standard_rewrite_driver_pool()
Definition: server_context.h:582
virtual RewriteDriverPool * SelectDriverPool(bool using_spdy)
Chooses a driver pool based on the request protocol.
void set_decoding_driver(RewriteDriver *rd)
Definition: server_context.h:630
void set_timer(Timer *timer)
Note: doesn't take ownership.
Definition: server_context.h:223
NamedLock * MakeInputLock(const GoogleString &name)
Makes a lock used for fetching and optimizing an input resource.
NamedLock * MakeCreationLock(const GoogleString &name)
const RewriteFilter * FindFilterForDecoding(const StringPiece &id) const
RewriteDriver * NewUnmanagedRewriteDriver(RewriteDriverPool *pool, RewriteOptions *options, const RequestContextPtr &request_ctx)
virtual void PostInitHook()
To set up AdminSite for SystemServerContext.
Definition: rewrite_driver_pool.h:34
QueuedWorkerPool * html_workers()
Pool of worker-threads that can be used to handle html-parsing.
Definition: server_context.h:538
static const char kResourceEtagValue[]
Definition: server_context.h:113
static GoogleString ShowCacheForm(StringPiece user_agent)
virtual GoogleString FormatOption(StringPiece option_name, StringPiece args)
bool DecodeUrlGivenOptions(const GoogleUrl &url, const RewriteOptions *options, const UrlNamer *url_namer, StringVector *decoded_urls) const
See RewriteDriver::DecodeUrl.
static const int64 kCacheTtlForMismatchedContentMs
Short lifetime for generated resources fetched with mismatching hash.
Definition: server_context.h:108
RequestProperties * NewRequestProperties()
Makes a new RequestProperties.
void SetRewriteOptionsManager(RewriteOptionsManager *rom)
Takes ownership of RewriteOptionsManager.
static bool IsExcludedAttribute(const char *attribute)
QueuedWorkerPool * low_priority_rewrite_workers()
Definition: server_context.h:545
GoogleString GetRewriteOptionsSignatureHash(const RewriteOptions *options)
bool store_outputs_in_file_system()
Definition: server_context.h:313
bool response_headers_finalized() const
Definition: server_context.h:572
void reset_global_options(RewriteOptions *options)
void MakePagePropertyCache(PropertyStore *property_store)
Creates PagePropertyCache object with the provided PropertyStore object.
bool IsPagespeedResource(const GoogleUrl &url)
Is this URL a ref to a Pagespeed resource?
RewriteOptions * NewOptions()
Makes a new, empty set of RewriteOptions.
Definition: rewrite_driver.h:98
void ManageRewriteDriverPool(RewriteDriverPool *pool)
Definition: server_context.h:684
Definition: server_context.h:101
void ApplyInputCacheControl(const ResourceVector &inputs, ResponseHeaders *headers)
void DeleteCacheOnDestruction(CacheInterface *cache)
QueuedWorkerPool * rewrite_workers()
Pool of worker-threads that can be used to handle resource rewriting.
Definition: server_context.h:541
CacheInterface * metadata_cache() const
Definition: server_context.h:266
Definition: usage_data_reporter.h:35
const GoogleString & hostname() const
Returns the current server hostname.
Definition: server_context.h:587
static const char kStatisticsGroup[]
Default statistics group name.
Definition: server_context.h:117
void ReleaseRewriteDriver(RewriteDriver *rewrite_driver)
Hasher * hasher() const
Definition: server_context.h:178
bool HandleBeacon(StringPiece body, StringPiece user_agent, const RequestContextPtr &request_context)
CacheInterface * filesystem_metadata_cache() const
Definition: server_context.h:255
UrlAsyncFetcher * DefaultSystemFetcher()
Definition: server_context.h:207
void AddOriginalContentLengthHeader(const ResourceVector &inputs, ResponseHeaders *headers)
Collects a few specific statistics variables related to Rewriting.
Definition: rewrite_stats.h:38
RewriteOptions * global_options()
size_t num_active_rewrite_drivers()
static const int64 kGeneratedMaxAgeMs
The lifetime for cache-extended generated resources, in milliseconds.
Definition: server_context.h:106
RewriteDriver * NewCustomRewriteDriver(RewriteOptions *custom_options, const RequestContextPtr &request_ctx)
void set_hasher(Hasher *hasher)
Setters should probably only be used in testing.
Definition: server_context.h:340
RewriteDriver * NewRewriteDriver(const RequestContextPtr &request_ctx)
Definition: url_async_fetcher.h:33
virtual PropertyStore * CreatePropertyStore(CacheInterface *cache_backend)
Creates CachePropertyStore object which will be used by PagePropertyCache.
Definition: http_cache.h:46
const PropertyCache::Cohort * AddCohortWithCache(const GoogleString &cohort_name, CacheInterface *cache, PropertyCache *pcache)
Definition: rewrite_driver_factory.h:73