20 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
21 #define NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
34 #include "net/instaweb/util/public/md5_hasher.h"
36 #include "net/instaweb/util/public/queued_worker_pool.h"
37 #include "net/instaweb/util/public/ref_counted_ptr.h"
41 #include "pagespeed/kernel/util/simple_random.h"
43 namespace pagespeed {
namespace js {
struct JsTokenizerPatterns; } }
45 namespace net_instaweb {
49 class CacheHtmlInfoFinder;
51 class CachePropertyStore;
52 class CriticalCssFinder;
53 class CriticalImagesFinder;
54 class CriticalLineInfoFinder;
55 class CriticalSelectorFinder;
56 class RequestProperties;
57 class ExperimentMatcher;
59 class FlushEarlyInfoFinder;
65 class NamedLockManager;
68 class ResponseHeaders;
70 class RewriteDriverFactory;
71 class RewriteDriverPool;
74 class RewriteOptionsManager;
79 class StaticAssetManager;
81 class ThreadSynchronizer;
84 class UrlAsyncFetcher;
86 class UsageDataReporter;
87 class UserAgentMatcher;
90 typedef RefCountedPtr<OutputResource> OutputResourcePtr;
91 typedef std::vector<OutputResourcePtr> OutputResourceVector;
102 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair;
113 static const char kCacheKeyResourceNamePrefix[];
131 const ContentType* content_type, StringPiece charset,
132 StringPiece cache_control_suffix, ResponseHeaders* header)
const;
134 void set_filename_prefix(
const StringPiece& file_prefix);
135 void set_statistics(Statistics* x) { statistics_ = x; }
136 void set_rewrite_stats(
RewriteStats* x) { rewrite_stats_ = x; }
137 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; }
138 void set_enable_property_cache(
bool enabled);
139 void set_message_handler(MessageHandler* x) { message_handler_ = x; }
141 StringPiece filename_prefix()
const {
return file_prefix_; }
142 Statistics* statistics()
const {
return statistics_; }
143 NamedLockManager* lock_manager()
const {
return lock_manager_; }
144 RewriteDriverFactory* factory()
const {
return factory_; }
145 ThreadSynchronizer* thread_synchronizer() {
146 return thread_synchronizer_.get();
148 ExperimentMatcher* experiment_matcher() {
return experiment_matcher_.get(); }
159 ResponseHeaders* headers);
170 const RewriteOptions* options,
171 const UrlNamer* url_namer,
172 StringVector* decoded_urls)
const;
174 void ComputeSignature(RewriteOptions* rewrite_options)
const;
177 Hasher*
hasher()
const {
return hasher_; }
178 const Hasher* lock_hasher()
const {
return &lock_hasher_; }
179 const Hasher* contents_hasher()
const {
return &contents_hasher_; }
180 FileSystem* file_system() {
return file_system_; }
181 void set_file_system(FileSystem* fs ) { file_system_ = fs; }
182 UrlNamer* url_namer()
const {
return url_namer_; }
183 void set_url_namer(UrlNamer* n) { url_namer_ = n; }
184 RewriteOptionsManager* rewrite_options_manager()
const {
185 return rewrite_options_manager_.get();
187 SHA1Signature* signature()
const {
return signature_; }
190 StaticAssetManager* static_asset_manager()
const {
191 return static_asset_manager_;
193 void set_static_asset_manager(StaticAssetManager* manager) {
194 static_asset_manager_ = manager;
196 Scheduler* scheduler()
const {
return scheduler_; }
197 void set_scheduler(Scheduler* s) { scheduler_ = s; }
198 bool has_default_system_fetcher()
const {
199 return default_system_fetcher_ != NULL;
201 bool has_default_distributed_fetcher() {
202 return default_distributed_fetcher_ != NULL;
209 return default_distributed_fetcher_;
212 Timer* timer()
const {
return timer_; }
217 HTTPCache* http_cache()
const {
return http_cache_.get(); }
218 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); }
223 PropertyCache* page_property_cache()
const {
224 return page_property_cache_.get();
227 const PropertyCache::Cohort* dom_cohort()
const {
return dom_cohort_; }
228 void set_dom_cohort(
const PropertyCache::Cohort* c) { dom_cohort_ = c; }
230 const PropertyCache::Cohort* blink_cohort()
const {
return blink_cohort_; }
231 void set_blink_cohort(
const PropertyCache::Cohort* c) { blink_cohort_ = c; }
233 const PropertyCache::Cohort* beacon_cohort()
const {
return beacon_cohort_; }
234 void set_beacon_cohort(
const PropertyCache::Cohort* c) { beacon_cohort_ = c; }
236 const PropertyCache::Cohort* fix_reflow_cohort()
const {
237 return fix_reflow_cohort_;
239 void set_fix_reflow_cohort(
const PropertyCache::Cohort* c) {
240 fix_reflow_cohort_ = c;
248 return filesystem_metadata_cache_;
250 void set_filesystem_metadata_cache(CacheInterface* x) {
251 filesystem_metadata_cache_ = x;
259 void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; }
261 CriticalCssFinder* critical_css_finder()
const {
262 return critical_css_finder_.get();
264 void set_critical_css_finder(CriticalCssFinder* finder);
266 CriticalImagesFinder* critical_images_finder()
const {
267 return critical_images_finder_.get();
269 void set_critical_images_finder(CriticalImagesFinder* finder);
271 CriticalSelectorFinder* critical_selector_finder()
const {
272 return critical_selector_finder_.get();
274 void set_critical_selector_finder(CriticalSelectorFinder* finder);
276 FlushEarlyInfoFinder* flush_early_info_finder()
const {
277 return flush_early_info_finder_.get();
279 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder);
281 UserAgentMatcher* user_agent_matcher()
const {
282 return user_agent_matcher_;
284 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; }
286 CacheHtmlInfoFinder* cache_html_info_finder()
const {
287 return cache_html_info_finder_.get();
290 SimpleRandom* simple_random() {
291 return &simple_random_;
294 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder);
296 CriticalLineInfoFinder* critical_line_info_finder()
const {
297 return critical_line_info_finder_.get();
306 void set_store_outputs_in_file_system(
bool store) {
307 store_outputs_in_file_system_ = store;
310 RewriteStats* rewrite_stats()
const {
return rewrite_stats_; }
311 MessageHandler* message_handler()
const {
return message_handler_; }
329 QueuedWorkerPool::Sequence* worker, Function* callback);
333 void set_signature(SHA1Signature* signature) { signature_ = signature; }
334 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) {
335 default_system_fetcher_ = fetcher;
337 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) {
338 default_distributed_fetcher_ = fetcher;
348 StringPiece user_agent,
349 const RequestContextPtr& request_context);
390 const RewriteOptions* domain_options,
391 GoogleUrl* request_url,
392 RequestHeaders* request_headers,
393 ResponseHeaders* response_headers,
394 RewriteQuery* rewrite_query);
401 const RewriteOptions* options,
409 RewriteOptions* domain_options,
410 RewriteOptions* query_options);
429 RewriteDriverPool* pool,
const RequestContextPtr& request_ctx);
445 RewriteDriverPool* pool, RewriteOptions* options,
446 const RequestContextPtr& request_ctx);
459 RewriteOptions* custom_options,
const RequestContextPtr& request_ctx);
475 ThreadSystem* thread_system() {
return thread_system_; }
476 UsageDataReporter* usage_data_reporter() {
return usage_data_reporter_; }
481 shutting_down_.set_value(
true);
484 bool shutting_down()
const {
485 return shutting_down_.value();
520 const OutputResourcePtr& output) {
522 output->response_headers());
527 ResponseHeaders* output_headers);
538 return low_priority_rewrite_workers_;
565 return response_headers_finalized_;
567 void set_response_headers_finalized(
bool x) {
568 response_headers_finalized_ = x;
575 return available_rewrite_drivers_.get();
582 void set_hostname(
const GoogleString& x) {
589 ResponseHeaders* headers);
597 RewriteDriver* driver);
618 void set_cache_property_store(CachePropertyStore* p);
631 const GoogleString& cohort_name,
639 const GoogleString& cohort_name,
640 CacheInterface* cache,
647 const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns()
const {
648 return js_tokenizer_patterns_;
654 RewriteOptions* options);
666 virtual GoogleString
FormatOption(StringPiece option_name, StringPiece args);
672 additional_driver_pools_.push_back(pool);
676 friend class ServerContextTest;
677 typedef std::set<RewriteDriver*> RewriteDriverSet;
680 void ReleaseRewriteDriverImpl(
RewriteDriver* rewrite_driver);
683 ThreadSystem* thread_system_;
685 GoogleString file_prefix_;
686 FileSystem* file_system_;
688 scoped_ptr<RewriteOptionsManager> rewrite_options_manager_;
689 UserAgentMatcher* user_agent_matcher_;
690 Scheduler* scheduler_;
694 SHA1Signature* signature_;
695 scoped_ptr<CriticalImagesFinder> critical_images_finder_;
696 scoped_ptr<CriticalCssFinder> critical_css_finder_;
697 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_;
698 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_;
699 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_;
700 scoped_ptr<CriticalLineInfoFinder> critical_line_info_finder_;
705 MD5Hasher lock_hasher_;
709 MD5Hasher contents_hasher_;
711 Statistics* statistics_;
714 scoped_ptr<HTTPCache> http_cache_;
715 scoped_ptr<PropertyCache> page_property_cache_;
716 CacheInterface* filesystem_metadata_cache_;
717 CacheInterface* metadata_cache_;
719 bool store_outputs_in_file_system_;
720 bool response_headers_finalized_;
721 bool enable_property_cache_;
723 NamedLockManager* lock_manager_;
724 MessageHandler* message_handler_;
736 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_;
739 std::vector<RewriteDriverPool*> additional_driver_pools_;
746 RewriteDriverSet active_rewrite_drivers_;
755 bool trying_to_cleanup_rewrite_drivers_;
756 RewriteDriverSet deferred_release_rewrite_drivers_;
757 bool shutdown_drivers_called_;
763 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_;
767 scoped_ptr<RewriteOptions> base_class_options_;
774 QueuedWorkerPool* html_workers_;
775 QueuedWorkerPool* rewrite_workers_;
776 QueuedWorkerPool* low_priority_rewrite_workers_;
778 AtomicBool shutting_down_;
785 scoped_ptr<ThreadSynchronizer> thread_synchronizer_;
788 scoped_ptr<ExperimentMatcher> experiment_matcher_;
793 GoogleString hostname_;
797 SimpleRandom simple_random_;
799 const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_;
801 scoped_ptr<CachePropertyStore> cache_property_store_;
Definition: url_namer.h:37
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:519
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)
Definition: property_cache.h:186
bool GetQueryOptions(const RequestContextPtr &request_context, const RewriteOptions *domain_options, GoogleUrl *request_url, RequestHeaders *request_headers, ResponseHeaders *response_headers, RewriteQuery *rewrite_query)
Adds property-semantics to a raw cache API.
Definition: property_cache.h:180
Definition: static_asset_manager.h:44
Definition: property_store.h:41
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:480
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:574
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:622
void set_timer(Timer *timer)
Note: doesn't take ownership.
Definition: server_context.h:215
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
void ShowCacheHandler(StringPiece url, AsyncFetch *fetch, RewriteOptions *options)
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:530
static const char kResourceEtagValue[]
Definition: server_context.h:112
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:107
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:537
GoogleString GetRewriteOptionsSignatureHash(const RewriteOptions *options)
bool store_outputs_in_file_system()
Definition: server_context.h:305
bool response_headers_finalized() const
Definition: server_context.h:564
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:671
Definition: server_context.h:100
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:533
CacheInterface * metadata_cache() const
Definition: server_context.h:258
Definition: usage_data_reporter.h:35
const GoogleString & hostname() const
Returns the current server hostname.
Definition: server_context.h:579
static const char kStatisticsGroup[]
Default statistics group name.
Definition: server_context.h:116
void ReleaseRewriteDriver(RewriteDriver *rewrite_driver)
Hasher * hasher() const
Definition: server_context.h:177
bool HandleBeacon(StringPiece body, StringPiece user_agent, const RequestContextPtr &request_context)
CacheInterface * filesystem_metadata_cache() const
Definition: server_context.h:247
UrlAsyncFetcher * DefaultSystemFetcher()
Definition: server_context.h:206
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:105
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:332
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