Page Speed Optimization Libraries
1.7.30.4
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00019 00020 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_ 00021 #define NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_ 00022 00023 #include <cstddef> 00024 #include <set> 00025 #include <utility> 00026 #include <vector> 00027 00028 #include "net/instaweb/http/public/http_cache.h" 00029 #include "net/instaweb/http/public/request_context.h" 00030 #include "net/instaweb/rewriter/public/output_resource.h" 00031 #include "net/instaweb/rewriter/public/resource.h" 00032 #include "net/instaweb/util/public/atomic_bool.h" 00033 #include "net/instaweb/util/public/basictypes.h" 00034 #include "net/instaweb/util/public/md5_hasher.h" 00035 #include "net/instaweb/util/public/property_cache.h" 00036 #include "net/instaweb/util/public/queued_worker_pool.h" 00037 #include "net/instaweb/util/public/ref_counted_ptr.h" 00038 #include "net/instaweb/util/public/scoped_ptr.h" 00039 #include "net/instaweb/util/public/string.h" 00040 #include "net/instaweb/util/public/string_util.h" 00041 #include "pagespeed/kernel/util/simple_random.h" 00042 00043 namespace pagespeed { namespace js { struct JsTokenizerPatterns; } } 00044 00045 namespace net_instaweb { 00046 00047 class AbstractMutex; 00048 class CacheHtmlInfoFinder; 00049 class CacheInterface; 00050 class CachePropertyStore; 00051 class CriticalCssFinder; 00052 class CriticalImagesFinder; 00053 class CriticalLineInfoFinder; 00054 class CriticalSelectorFinder; 00055 class RequestProperties; 00056 class ExperimentMatcher; 00057 class FileSystem; 00058 class FilenameEncoder; 00059 class FlushEarlyInfoFinder; 00060 class Function; 00061 class GoogleUrl; 00062 class Hasher; 00063 class MessageHandler; 00064 class NamedLock; 00065 class NamedLockManager; 00066 class PropertyStore; 00067 class RequestHeaders; 00068 class ResponseHeaders; 00069 class RewriteDriver; 00070 class RewriteDriverFactory; 00071 class RewriteDriverPool; 00072 class RewriteFilter; 00073 class RewriteOptions; 00074 class RewriteOptionsManager; 00075 class RewriteQuery; 00076 class RewriteStats; 00077 class Scheduler; 00078 class StaticAssetManager; 00079 class Statistics; 00080 class ThreadSynchronizer; 00081 class ThreadSystem; 00082 class Timer; 00083 class UrlAsyncFetcher; 00084 class UrlNamer; 00085 class UsageDataReporter; 00086 class UserAgentMatcher; 00087 struct ContentType; 00088 00089 typedef RefCountedPtr<OutputResource> OutputResourcePtr; 00090 typedef std::vector<OutputResourcePtr> OutputResourceVector; 00091 00099 class ServerContext { 00100 public: 00101 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair; 00102 00104 static const int64 kGeneratedMaxAgeMs; 00105 00109 static const char kResourceEtagValue[]; 00110 static const char kCacheKeyResourceNamePrefix[]; 00111 00113 static const char kStatisticsGroup[]; 00114 00115 explicit ServerContext(RewriteDriverFactory* factory); 00116 virtual ~ServerContext(); 00117 00123 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00124 ResponseHeaders* header) const { 00125 SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header); 00126 } 00127 00130 void SetDefaultLongCacheHeadersWithCharset( 00131 const ContentType* content_type, StringPiece charset, 00132 ResponseHeaders* header) const; 00133 00134 void set_filename_prefix(const StringPiece& file_prefix); 00135 void set_statistics(Statistics* x) { statistics_ = x; } 00136 void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; } 00137 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; } 00138 void set_enable_property_cache(bool enabled); 00139 void set_message_handler(MessageHandler* x) { message_handler_ = x; } 00140 00141 StringPiece filename_prefix() const { return file_prefix_; } 00142 Statistics* statistics() const { return statistics_; } 00143 NamedLockManager* lock_manager() const { return lock_manager_; } 00144 RewriteDriverFactory* factory() const { return factory_; } 00145 ThreadSynchronizer* thread_synchronizer() { 00146 return thread_synchronizer_.get(); 00147 } 00148 ExperimentMatcher* experiment_matcher() { return experiment_matcher_.get(); } 00149 00158 void ApplyInputCacheControl(const ResourceVector& inputs, 00159 ResponseHeaders* headers); 00160 00162 bool IsPagespeedResource(const GoogleUrl& url); 00163 00166 const RewriteFilter* FindFilterForDecoding(const StringPiece& id) const; 00167 00169 bool DecodeUrlGivenOptions(const GoogleUrl& url, 00170 const RewriteOptions* options, 00171 const UrlNamer* url_namer, 00172 StringVector* decoded_urls) const; 00173 00174 void ComputeSignature(RewriteOptions* rewrite_options) const; 00175 00177 Hasher* hasher() const { return hasher_; } 00178 const Hasher* lock_hasher() const { return &lock_hasher_; } 00179 const Hasher* contents_hasher() const { return &contents_hasher_; } 00180 FileSystem* file_system() { return file_system_; } 00181 void set_file_system(FileSystem* fs ) { file_system_ = fs; } 00182 FilenameEncoder* filename_encoder() const { return filename_encoder_; } 00183 void set_filename_encoder(FilenameEncoder* x) { filename_encoder_ = x; } 00184 UrlNamer* url_namer() const { return url_namer_; } 00185 void set_url_namer(UrlNamer* n) { url_namer_ = n; } 00186 RewriteOptionsManager* rewrite_options_manager() const { 00187 return rewrite_options_manager_.get(); 00188 } 00190 void SetRewriteOptionsManager(RewriteOptionsManager* rom); 00191 StaticAssetManager* static_asset_manager() const { 00192 return static_asset_manager_; 00193 } 00194 void set_static_asset_manager(StaticAssetManager* manager) { 00195 static_asset_manager_ = manager; 00196 } 00197 Scheduler* scheduler() const { return scheduler_; } 00198 void set_scheduler(Scheduler* s) { scheduler_ = s; } 00199 bool has_default_system_fetcher() const { 00200 return default_system_fetcher_ != NULL; 00201 } 00202 bool has_default_distributed_fetcher() { 00203 return default_distributed_fetcher_ != NULL; 00204 } 00207 UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; } 00208 00209 UrlAsyncFetcher* DefaultDistributedFetcher() { 00210 return default_distributed_fetcher_; 00211 } 00212 00213 Timer* timer() const { return timer_; } 00214 00216 void set_timer(Timer* timer) { timer_ = timer; } 00217 00218 HTTPCache* http_cache() const { return http_cache_.get(); } 00219 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); } 00220 00222 void MakePagePropertyCache(PropertyStore* property_store); 00223 00224 PropertyCache* page_property_cache() const { 00225 return page_property_cache_.get(); 00226 } 00227 00228 const PropertyCache::Cohort* dom_cohort() const { return dom_cohort_; } 00229 void set_dom_cohort(const PropertyCache::Cohort* c) { dom_cohort_ = c; } 00230 00231 const PropertyCache::Cohort* blink_cohort() const { return blink_cohort_; } 00232 void set_blink_cohort(const PropertyCache::Cohort* c) { blink_cohort_ = c; } 00233 00234 const PropertyCache::Cohort* beacon_cohort() const { return beacon_cohort_; } 00235 void set_beacon_cohort(const PropertyCache::Cohort* c) { beacon_cohort_ = c; } 00236 00237 const PropertyCache::Cohort* fix_reflow_cohort() const { 00238 return fix_reflow_cohort_; 00239 } 00240 void set_fix_reflow_cohort(const PropertyCache::Cohort* c) { 00241 fix_reflow_cohort_ = c; 00242 } 00243 00248 CacheInterface* filesystem_metadata_cache() const { 00249 return filesystem_metadata_cache_; 00250 } 00251 void set_filesystem_metadata_cache(CacheInterface* x) { 00252 filesystem_metadata_cache_ = x; 00253 } 00254 00259 CacheInterface* metadata_cache() const { return metadata_cache_; } 00260 void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; } 00261 00262 CriticalCssFinder* critical_css_finder() const { 00263 return critical_css_finder_.get(); 00264 } 00265 void set_critical_css_finder(CriticalCssFinder* finder); 00266 00267 CriticalImagesFinder* critical_images_finder() const { 00268 return critical_images_finder_.get(); 00269 } 00270 void set_critical_images_finder(CriticalImagesFinder* finder); 00271 00272 CriticalSelectorFinder* critical_selector_finder() const { 00273 return critical_selector_finder_.get(); 00274 } 00275 void set_critical_selector_finder(CriticalSelectorFinder* finder); 00276 00277 FlushEarlyInfoFinder* flush_early_info_finder() const { 00278 return flush_early_info_finder_.get(); 00279 } 00280 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder); 00281 00282 UserAgentMatcher* user_agent_matcher() const { 00283 return user_agent_matcher_; 00284 } 00285 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; } 00286 00287 CacheHtmlInfoFinder* cache_html_info_finder() const { 00288 return cache_html_info_finder_.get(); 00289 } 00290 00291 SimpleRandom* simple_random() { 00292 return &simple_random_; 00293 } 00294 00295 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder); 00296 00297 CriticalLineInfoFinder* critical_line_info_finder() const { 00298 return critical_line_info_finder_.get(); 00299 } 00300 00302 void set_critical_line_info_finder(CriticalLineInfoFinder* finder); 00303 00306 bool store_outputs_in_file_system() { return store_outputs_in_file_system_; } 00307 void set_store_outputs_in_file_system(bool store) { 00308 store_outputs_in_file_system_ = store; 00309 } 00310 00311 RewriteStats* rewrite_stats() const { return rewrite_stats_; } 00312 MessageHandler* message_handler() const { return message_handler_; } 00313 00318 NamedLock* MakeCreationLock(const GoogleString& name); 00319 00321 NamedLock* MakeInputLock(const GoogleString& name); 00322 00324 bool TryLockForCreation(NamedLock* creation_lock); 00325 00329 void LockForCreation(NamedLock* creation_lock, 00330 QueuedWorkerPool::Sequence* worker, Function* callback); 00331 00333 void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00334 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) { 00335 default_system_fetcher_ = fetcher; 00336 } 00337 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) { 00338 default_distributed_fetcher_ = fetcher; 00339 } 00340 00347 bool HandleBeacon(StringPiece body, 00348 StringPiece user_agent, 00349 const RequestContextPtr& request_context); 00350 00356 RewriteOptions* global_options(); 00357 00360 const RewriteOptions* global_options() const; 00361 00365 void reset_global_options(RewriteOptions* options); 00366 00368 RewriteOptions* NewOptions(); 00369 00382 bool GetQueryOptions(GoogleUrl* request_url, 00383 RequestHeaders* request_headers, 00384 ResponseHeaders* response_headers, 00385 RewriteQuery* rewrite_query); 00386 00391 static bool ScanSplitHtmlRequest(const RequestContextPtr& ctx, 00392 const RewriteOptions* options, 00393 GoogleString* url); 00394 00399 RewriteOptions* GetCustomOptions(RequestHeaders* request_headers, 00400 RewriteOptions* domain_options, 00401 RewriteOptions* query_options); 00402 00405 GoogleString GetRewriteOptionsSignatureHash(const RewriteOptions* options); 00406 00415 RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx); 00416 00419 RewriteDriver* NewRewriteDriverFromPool( 00420 RewriteDriverPool* pool, const RequestContextPtr& request_ctx); 00421 00435 RewriteDriver* NewUnmanagedRewriteDriver( 00436 RewriteDriverPool* pool, RewriteOptions* options, 00437 const RequestContextPtr& request_ctx); 00438 00449 RewriteDriver* NewCustomRewriteDriver( 00450 RewriteOptions* custom_options, const RequestContextPtr& request_ctx); 00451 00464 void ReleaseRewriteDriver(RewriteDriver* rewrite_driver); 00465 00466 ThreadSystem* thread_system() { return thread_system_; } 00467 UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; } 00468 00471 void set_shutting_down() { 00472 shutting_down_.set_value(true); 00473 } 00474 00475 bool shutting_down() const { 00476 return shutting_down_.value(); 00477 } 00478 00488 void ShutDownDrivers(); 00489 00510 void MergeNonCachingResponseHeaders(const ResourcePtr& input, 00511 const OutputResourcePtr& output) { 00512 MergeNonCachingResponseHeaders(*input->response_headers(), 00513 output->response_headers()); 00514 } 00515 00517 void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers, 00518 ResponseHeaders* output_headers); 00519 00521 QueuedWorkerPool* html_workers() { return html_workers_; } 00522 00524 QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; } 00525 00528 QueuedWorkerPool* low_priority_rewrite_workers() { 00529 return low_priority_rewrite_workers_; 00530 } 00531 00535 size_t num_active_rewrite_drivers(); 00536 00539 void InitWorkers(); 00540 00543 static bool IsExcludedAttribute(const char* attribute); 00544 00552 bool response_headers_finalized() const { 00553 return response_headers_finalized_; 00554 } 00555 void set_response_headers_finalized(bool x) { 00556 response_headers_finalized_ = x; 00557 } 00558 00562 RewriteDriverPool* standard_rewrite_driver_pool() { 00563 return available_rewrite_drivers_.get(); 00564 } 00565 00567 const GoogleString& hostname() const { 00568 return hostname_; 00569 } 00570 void set_hostname(const GoogleString& x) { 00571 hostname_ = x; 00572 } 00573 00576 void AddOriginalContentLengthHeader(const ResourceVector& inputs, 00577 ResponseHeaders* headers); 00578 00580 virtual RewriteDriverPool* SelectDriverPool(bool using_spdy); 00581 00584 virtual void ApplySessionFetchers(const RequestContextPtr& req, 00585 RewriteDriver* driver); 00586 00597 virtual bool ProxiesHtml() const = 0; 00598 00600 RequestProperties* NewRequestProperties(); 00601 00604 void DeleteCacheOnDestruction(CacheInterface* cache); 00605 00606 void set_cache_property_store(CachePropertyStore* p); 00607 00610 void set_decoding_driver(RewriteDriver* rd) { decoding_driver_ = rd; } 00611 00613 virtual PropertyStore* CreatePropertyStore(CacheInterface* cache_backend); 00614 00618 const PropertyCache::Cohort* AddCohort( 00619 const GoogleString& cohort_name, 00620 PropertyCache* pcache); 00621 00626 const PropertyCache::Cohort* AddCohortWithCache( 00627 const GoogleString& cohort_name, 00628 CacheInterface* cache, 00629 PropertyCache* pcache); 00630 00633 const CacheInterface* pcache_cache_backend(); 00634 00635 const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns() const { 00636 return js_tokenizer_patterns_; 00637 } 00638 00639 protected: 00642 void ManageRewriteDriverPool(RewriteDriverPool* pool) { 00643 additional_driver_pools_.push_back(pool); 00644 } 00645 00646 private: 00647 friend class ServerContextTest; 00648 typedef std::set<RewriteDriver*> RewriteDriverSet; 00649 00651 void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver); 00652 00654 ThreadSystem* thread_system_; 00655 RewriteStats* rewrite_stats_; 00656 GoogleString file_prefix_; 00657 FileSystem* file_system_; 00658 FilenameEncoder* filename_encoder_; 00659 UrlNamer* url_namer_; 00660 scoped_ptr<RewriteOptionsManager> rewrite_options_manager_; 00661 UserAgentMatcher* user_agent_matcher_; 00662 Scheduler* scheduler_; 00663 UrlAsyncFetcher* default_system_fetcher_; 00664 UrlAsyncFetcher* default_distributed_fetcher_; 00665 Hasher* hasher_; 00666 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00667 scoped_ptr<CriticalCssFinder> critical_css_finder_; 00668 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_; 00669 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_; 00670 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_; 00671 scoped_ptr<CriticalLineInfoFinder> critical_line_info_finder_; 00672 00676 MD5Hasher lock_hasher_; 00677 00680 MD5Hasher contents_hasher_; 00681 00682 Statistics* statistics_; 00683 00684 Timer* timer_; 00685 scoped_ptr<HTTPCache> http_cache_; 00686 scoped_ptr<PropertyCache> page_property_cache_; 00687 CacheInterface* filesystem_metadata_cache_; 00688 CacheInterface* metadata_cache_; 00689 00690 bool store_outputs_in_file_system_; 00691 bool response_headers_finalized_; 00692 bool enable_property_cache_; 00693 00694 NamedLockManager* lock_manager_; 00695 MessageHandler* message_handler_; 00696 00697 const PropertyCache::Cohort* dom_cohort_; 00698 const PropertyCache::Cohort* blink_cohort_; 00699 const PropertyCache::Cohort* beacon_cohort_; 00700 const PropertyCache::Cohort* fix_reflow_cohort_; 00701 00707 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_; 00708 00710 std::vector<RewriteDriverPool*> additional_driver_pools_; 00711 00717 RewriteDriverSet active_rewrite_drivers_; 00718 00726 bool trying_to_cleanup_rewrite_drivers_; 00727 RewriteDriverSet deferred_release_rewrite_drivers_; 00728 bool shutdown_drivers_called_; 00729 00732 RewriteDriverFactory* factory_; 00733 00734 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_; 00735 00738 scoped_ptr<RewriteOptions> base_class_options_; 00739 00743 RewriteDriver* decoding_driver_; 00744 00745 QueuedWorkerPool* html_workers_; 00746 QueuedWorkerPool* rewrite_workers_; 00747 QueuedWorkerPool* low_priority_rewrite_workers_; 00748 00749 AtomicBool shutting_down_; 00750 00752 StaticAssetManager* static_asset_manager_; 00753 00756 scoped_ptr<ThreadSynchronizer> thread_synchronizer_; 00757 00759 scoped_ptr<ExperimentMatcher> experiment_matcher_; 00760 00761 UsageDataReporter* usage_data_reporter_; 00762 00764 GoogleString hostname_; 00765 00768 SimpleRandom simple_random_; 00770 const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_; 00771 00772 scoped_ptr<CachePropertyStore> cache_property_store_; 00773 00774 DISALLOW_COPY_AND_ASSIGN(ServerContext); 00775 }; 00776 00777 } 00778 00779 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_