Page Speed Optimization Libraries
1.7.30.3
|
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 net_instaweb { 00044 00045 class AbstractMutex; 00046 class CacheHtmlInfoFinder; 00047 class CacheInterface; 00048 class CachePropertyStore; 00049 class CriticalCssFinder; 00050 class CriticalImagesFinder; 00051 class CriticalLineInfoFinder; 00052 class CriticalSelectorFinder; 00053 class RequestProperties; 00054 class ExperimentMatcher; 00055 class FileSystem; 00056 class FilenameEncoder; 00057 class FlushEarlyInfoFinder; 00058 class Function; 00059 class GoogleUrl; 00060 class Hasher; 00061 class MessageHandler; 00062 class NamedLock; 00063 class NamedLockManager; 00064 class PropertyStore; 00065 class RequestHeaders; 00066 class ResponseHeaders; 00067 class RewriteDriver; 00068 class RewriteDriverFactory; 00069 class RewriteDriverPool; 00070 class RewriteOptions; 00071 class RewriteOptionsManager; 00072 class RewriteStats; 00073 class Scheduler; 00074 class StaticAssetManager; 00075 class Statistics; 00076 class ThreadSynchronizer; 00077 class ThreadSystem; 00078 class Timer; 00079 class UrlAsyncFetcher; 00080 class UrlNamer; 00081 class UsageDataReporter; 00082 class UserAgentMatcher; 00083 struct ContentType; 00084 00085 typedef RefCountedPtr<OutputResource> OutputResourcePtr; 00086 typedef std::vector<OutputResourcePtr> OutputResourceVector; 00087 00095 class ServerContext { 00096 public: 00097 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair; 00098 00100 static const int64 kGeneratedMaxAgeMs; 00101 00105 static const char kResourceEtagValue[]; 00106 static const char kCacheKeyResourceNamePrefix[]; 00107 00109 static const char kStatisticsGroup[]; 00110 00111 explicit ServerContext(RewriteDriverFactory* factory); 00112 virtual ~ServerContext(); 00113 00119 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00120 ResponseHeaders* header) const { 00121 SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header); 00122 } 00123 00126 void SetDefaultLongCacheHeadersWithCharset( 00127 const ContentType* content_type, StringPiece charset, 00128 ResponseHeaders* header) const; 00129 00130 void set_filename_prefix(const StringPiece& file_prefix); 00131 void set_statistics(Statistics* x) { statistics_ = x; } 00132 void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; } 00133 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; } 00134 void set_enable_property_cache(bool enabled); 00135 void set_message_handler(MessageHandler* x) { message_handler_ = x; } 00136 00137 StringPiece filename_prefix() const { return file_prefix_; } 00138 Statistics* statistics() const { return statistics_; } 00139 NamedLockManager* lock_manager() const { return lock_manager_; } 00140 RewriteDriverFactory* factory() const { return factory_; } 00141 ThreadSynchronizer* thread_synchronizer() { 00142 return thread_synchronizer_.get(); 00143 } 00144 ExperimentMatcher* experiment_matcher() { return experiment_matcher_.get(); } 00145 00154 void ApplyInputCacheControl(const ResourceVector& inputs, 00155 ResponseHeaders* headers); 00156 00158 bool IsPagespeedResource(const GoogleUrl& url); 00159 00160 void ComputeSignature(RewriteOptions* rewrite_options) const; 00161 00163 Hasher* hasher() const { return hasher_; } 00164 const Hasher* lock_hasher() const { return &lock_hasher_; } 00165 const Hasher* contents_hasher() const { return &contents_hasher_; } 00166 FileSystem* file_system() { return file_system_; } 00167 void set_file_system(FileSystem* fs ) { file_system_ = fs; } 00168 FilenameEncoder* filename_encoder() const { return filename_encoder_; } 00169 void set_filename_encoder(FilenameEncoder* x) { filename_encoder_ = x; } 00170 UrlNamer* url_namer() const { return url_namer_; } 00171 void set_url_namer(UrlNamer* n) { url_namer_ = n; } 00172 RewriteOptionsManager* rewrite_options_manager() const { 00173 return rewrite_options_manager_.get(); 00174 } 00176 void SetRewriteOptionsManager(RewriteOptionsManager* rom); 00177 StaticAssetManager* static_asset_manager() const { 00178 return static_asset_manager_; 00179 } 00180 void set_static_asset_manager(StaticAssetManager* manager) { 00181 static_asset_manager_ = manager; 00182 } 00183 Scheduler* scheduler() const { return scheduler_; } 00184 void set_scheduler(Scheduler* s) { scheduler_ = s; } 00185 bool has_default_system_fetcher() const { 00186 return default_system_fetcher_ != NULL; 00187 } 00188 bool has_default_distributed_fetcher() { 00189 return default_distributed_fetcher_ != NULL; 00190 } 00193 UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; } 00194 00195 UrlAsyncFetcher* DefaultDistributedFetcher() { 00196 return default_distributed_fetcher_; 00197 } 00198 00199 Timer* timer() const { return http_cache_->timer(); } 00200 00201 HTTPCache* http_cache() const { return http_cache_.get(); } 00202 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); } 00203 00205 void MakePagePropertyCache(PropertyStore* property_store); 00206 00207 PropertyCache* page_property_cache() const { 00208 return page_property_cache_.get(); 00209 } 00210 00211 const PropertyCache::Cohort* dom_cohort() const { return dom_cohort_; } 00212 void set_dom_cohort(const PropertyCache::Cohort* c) { dom_cohort_ = c; } 00213 00214 const PropertyCache::Cohort* blink_cohort() const { return blink_cohort_; } 00215 void set_blink_cohort(const PropertyCache::Cohort* c) { blink_cohort_ = c; } 00216 00217 const PropertyCache::Cohort* beacon_cohort() const { return beacon_cohort_; } 00218 void set_beacon_cohort(const PropertyCache::Cohort* c) { beacon_cohort_ = c; } 00219 00220 const PropertyCache::Cohort* fix_reflow_cohort() const { 00221 return fix_reflow_cohort_; 00222 } 00223 void set_fix_reflow_cohort(const PropertyCache::Cohort* c) { 00224 fix_reflow_cohort_ = c; 00225 } 00226 00231 CacheInterface* filesystem_metadata_cache() const { 00232 return filesystem_metadata_cache_; 00233 } 00234 void set_filesystem_metadata_cache(CacheInterface* x) { 00235 filesystem_metadata_cache_ = x; 00236 } 00237 00242 CacheInterface* metadata_cache() const { return metadata_cache_; } 00243 void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; } 00244 00245 CriticalCssFinder* critical_css_finder() const { 00246 return critical_css_finder_.get(); 00247 } 00248 void set_critical_css_finder(CriticalCssFinder* finder); 00249 00250 CriticalImagesFinder* critical_images_finder() const { 00251 return critical_images_finder_.get(); 00252 } 00253 void set_critical_images_finder(CriticalImagesFinder* finder); 00254 00255 CriticalSelectorFinder* critical_selector_finder() const { 00256 return critical_selector_finder_.get(); 00257 } 00258 void set_critical_selector_finder(CriticalSelectorFinder* finder); 00259 00260 FlushEarlyInfoFinder* flush_early_info_finder() const { 00261 return flush_early_info_finder_.get(); 00262 } 00263 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder); 00264 00265 UserAgentMatcher* user_agent_matcher() const { 00266 return user_agent_matcher_; 00267 } 00268 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; } 00269 00270 CacheHtmlInfoFinder* cache_html_info_finder() const { 00271 return cache_html_info_finder_.get(); 00272 } 00273 00274 SimpleRandom* simple_random() { 00275 return &simple_random_; 00276 } 00277 00278 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder); 00279 00280 CriticalLineInfoFinder* critical_line_info_finder() const { 00281 return critical_line_info_finder_.get(); 00282 } 00283 00284 void set_critical_line_info_finder(CriticalLineInfoFinder* finder); 00285 00288 bool store_outputs_in_file_system() { return store_outputs_in_file_system_; } 00289 void set_store_outputs_in_file_system(bool store) { 00290 store_outputs_in_file_system_ = store; 00291 } 00292 00293 RewriteStats* rewrite_stats() const { return rewrite_stats_; } 00294 MessageHandler* message_handler() const { return message_handler_; } 00295 00300 NamedLock* MakeCreationLock(const GoogleString& name); 00301 00303 NamedLock* MakeInputLock(const GoogleString& name); 00304 00306 bool TryLockForCreation(NamedLock* creation_lock); 00307 00311 void LockForCreation(NamedLock* creation_lock, 00312 QueuedWorkerPool::Sequence* worker, Function* callback); 00313 00315 void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00316 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) { 00317 default_system_fetcher_ = fetcher; 00318 } 00319 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) { 00320 default_distributed_fetcher_ = fetcher; 00321 } 00322 00329 bool HandleBeacon(StringPiece body, 00330 StringPiece user_agent, 00331 const RequestContextPtr& request_context); 00332 00338 RewriteOptions* global_options(); 00339 00342 const RewriteOptions* global_options() const; 00343 00347 void reset_global_options(RewriteOptions* options); 00348 00350 RewriteOptions* NewOptions(); 00351 00361 OptionsBoolPair GetQueryOptions(GoogleUrl* request_url, 00362 RequestHeaders* request_headers, 00363 ResponseHeaders* response_headers); 00364 00369 static bool ScanSplitHtmlRequest(const RequestContextPtr& ctx, 00370 const RewriteOptions* options, 00371 GoogleString* url); 00372 00377 RewriteOptions* GetCustomOptions(RequestHeaders* request_headers, 00378 RewriteOptions* domain_options, 00379 RewriteOptions* query_options); 00380 00383 GoogleString GetRewriteOptionsSignatureHash(const RewriteOptions* options); 00384 00393 RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx); 00394 00397 RewriteDriver* NewRewriteDriverFromPool( 00398 RewriteDriverPool* pool, const RequestContextPtr& request_ctx); 00399 00413 RewriteDriver* NewUnmanagedRewriteDriver( 00414 RewriteDriverPool* pool, RewriteOptions* options, 00415 const RequestContextPtr& request_ctx); 00416 00427 RewriteDriver* NewCustomRewriteDriver( 00428 RewriteOptions* custom_options, const RequestContextPtr& request_ctx); 00429 00442 void ReleaseRewriteDriver(RewriteDriver* rewrite_driver); 00443 00444 ThreadSystem* thread_system() { return thread_system_; } 00445 UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; } 00446 00449 void set_shutting_down() { 00450 shutting_down_.set_value(true); 00451 } 00452 00453 bool shutting_down() const { 00454 return shutting_down_.value(); 00455 } 00456 00466 void ShutDownDrivers(); 00467 00488 void MergeNonCachingResponseHeaders(const ResourcePtr& input, 00489 const OutputResourcePtr& output) { 00490 MergeNonCachingResponseHeaders(*input->response_headers(), 00491 output->response_headers()); 00492 } 00493 00495 void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers, 00496 ResponseHeaders* output_headers); 00497 00499 QueuedWorkerPool* html_workers() { return html_workers_; } 00500 00502 QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; } 00503 00506 QueuedWorkerPool* low_priority_rewrite_workers() { 00507 return low_priority_rewrite_workers_; 00508 } 00509 00513 size_t num_active_rewrite_drivers(); 00514 00518 void InitWorkersAndDecodingDriver(); 00519 00522 static bool IsExcludedAttribute(const char* attribute); 00523 00531 bool response_headers_finalized() const { 00532 return response_headers_finalized_; 00533 } 00534 void set_response_headers_finalized(bool x) { 00535 response_headers_finalized_ = x; 00536 } 00537 00541 RewriteDriverPool* standard_rewrite_driver_pool() { 00542 return available_rewrite_drivers_.get(); 00543 } 00544 00546 const GoogleString& hostname() const { 00547 return hostname_; 00548 } 00549 void set_hostname(const GoogleString& x) { 00550 hostname_ = x; 00551 } 00552 00555 void AddOriginalContentLengthHeader(const ResourceVector& inputs, 00556 ResponseHeaders* headers); 00557 00559 virtual RewriteDriverPool* SelectDriverPool(bool using_spdy); 00560 00563 virtual void ApplySessionFetchers(const RequestContextPtr& req, 00564 RewriteDriver* driver); 00565 00566 const RewriteDriver* decoding_driver() const { 00567 return decoding_driver_.get(); 00568 } 00569 00580 virtual bool ProxiesHtml() const = 0; 00581 00583 RequestProperties* NewRequestProperties(); 00584 00587 void DeleteCacheOnDestruction(CacheInterface* cache); 00588 00589 void set_cache_property_store(CachePropertyStore* p); 00590 00592 virtual PropertyStore* CreatePropertyStore(CacheInterface* cache_backend); 00593 00597 const PropertyCache::Cohort* AddCohort( 00598 const GoogleString& cohort_name, 00599 PropertyCache* pcache); 00600 00605 const PropertyCache::Cohort* AddCohortWithCache( 00606 const GoogleString& cohort_name, 00607 CacheInterface* cache, 00608 PropertyCache* pcache); 00609 00612 const CacheInterface* pcache_cache_backend(); 00613 00614 protected: 00617 void ManageRewriteDriverPool(RewriteDriverPool* pool) { 00618 additional_driver_pools_.push_back(pool); 00619 } 00620 00621 private: 00622 friend class ServerContextTest; 00623 typedef std::set<RewriteDriver*> RewriteDriverSet; 00624 00626 void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver); 00627 00629 ThreadSystem* thread_system_; 00630 RewriteStats* rewrite_stats_; 00631 GoogleString file_prefix_; 00632 FileSystem* file_system_; 00633 FilenameEncoder* filename_encoder_; 00634 UrlNamer* url_namer_; 00635 scoped_ptr<RewriteOptionsManager> rewrite_options_manager_; 00636 UserAgentMatcher* user_agent_matcher_; 00637 Scheduler* scheduler_; 00638 UrlAsyncFetcher* default_system_fetcher_; 00639 UrlAsyncFetcher* default_distributed_fetcher_; 00640 Hasher* hasher_; 00641 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00642 scoped_ptr<CriticalCssFinder> critical_css_finder_; 00643 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_; 00644 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_; 00645 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_; 00646 scoped_ptr<CriticalLineInfoFinder> critical_line_info_finder_; 00647 00651 MD5Hasher lock_hasher_; 00652 00655 MD5Hasher contents_hasher_; 00656 00657 Statistics* statistics_; 00658 00659 scoped_ptr<HTTPCache> http_cache_; 00660 scoped_ptr<PropertyCache> page_property_cache_; 00661 CacheInterface* filesystem_metadata_cache_; 00662 CacheInterface* metadata_cache_; 00663 00664 bool store_outputs_in_file_system_; 00665 bool response_headers_finalized_; 00666 bool enable_property_cache_; 00667 00668 NamedLockManager* lock_manager_; 00669 MessageHandler* message_handler_; 00670 00671 const PropertyCache::Cohort* dom_cohort_; 00672 const PropertyCache::Cohort* blink_cohort_; 00673 const PropertyCache::Cohort* beacon_cohort_; 00674 const PropertyCache::Cohort* fix_reflow_cohort_; 00675 00681 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_; 00682 00684 std::vector<RewriteDriverPool*> additional_driver_pools_; 00685 00691 RewriteDriverSet active_rewrite_drivers_; 00692 00700 bool trying_to_cleanup_rewrite_drivers_; 00701 RewriteDriverSet deferred_release_rewrite_drivers_; 00702 bool shutdown_drivers_called_; 00703 00706 RewriteDriverFactory* factory_; 00707 00708 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_; 00709 00713 scoped_ptr<RewriteOptions> base_class_options_; 00714 00724 scoped_ptr<RewriteDriver> decoding_driver_; 00725 00726 QueuedWorkerPool* html_workers_; 00727 QueuedWorkerPool* rewrite_workers_; 00728 QueuedWorkerPool* low_priority_rewrite_workers_; 00729 00730 AtomicBool shutting_down_; 00731 00733 StaticAssetManager* static_asset_manager_; 00734 00737 scoped_ptr<ThreadSynchronizer> thread_synchronizer_; 00738 00740 scoped_ptr<ExperimentMatcher> experiment_matcher_; 00741 00742 UsageDataReporter* usage_data_reporter_; 00743 00745 GoogleString hostname_; 00746 00749 SimpleRandom simple_random_; 00750 00751 scoped_ptr<CachePropertyStore> cache_property_store_; 00752 00753 DISALLOW_COPY_AND_ASSIGN(ServerContext); 00754 }; 00755 00756 } 00757 00758 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_