Page Speed Optimization Libraries
1.6.29.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 CriticalCssFinder; 00049 class CriticalImagesFinder; 00050 class CriticalSelectorFinder; 00051 class RequestProperties; 00052 class ExperimentMatcher; 00053 class FileSystem; 00054 class FilenameEncoder; 00055 class FlushEarlyInfoFinder; 00056 class Function; 00057 class GoogleUrl; 00058 class Hasher; 00059 class MessageHandler; 00060 class NamedLock; 00061 class NamedLockManager; 00062 class RequestHeaders; 00063 class ResponseHeaders; 00064 class RewriteDriver; 00065 class RewriteDriverFactory; 00066 class RewriteDriverPool; 00067 class RewriteOptions; 00068 class RewriteOptionsManager; 00069 class RewriteStats; 00070 class Scheduler; 00071 class StaticAssetManager; 00072 class Statistics; 00073 class ThreadSynchronizer; 00074 class ThreadSystem; 00075 class Timer; 00076 class UrlAsyncFetcher; 00077 class UrlNamer; 00078 class UsageDataReporter; 00079 class UserAgentMatcher; 00080 struct ContentType; 00081 00082 typedef RefCountedPtr<OutputResource> OutputResourcePtr; 00083 typedef std::vector<OutputResourcePtr> OutputResourceVector; 00084 00092 class ServerContext { 00093 public: 00094 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair; 00095 00097 static const int64 kGeneratedMaxAgeMs; 00098 00102 static const char kResourceEtagValue[]; 00103 static const char kCacheKeyResourceNamePrefix[]; 00104 00106 static const char kStatisticsGroup[]; 00107 00108 explicit ServerContext(RewriteDriverFactory* factory); 00109 virtual ~ServerContext(); 00110 00116 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00117 ResponseHeaders* header) const { 00118 SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header); 00119 } 00120 00123 void SetDefaultLongCacheHeadersWithCharset( 00124 const ContentType* content_type, StringPiece charset, 00125 ResponseHeaders* header) const; 00126 00128 void SetContentType(const ContentType* content_type, ResponseHeaders* header); 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() { return default_system_fetcher_ != NULL; } 00186 bool has_default_distributed_fetcher() { 00187 return default_distributed_fetcher_ != NULL; 00188 } 00191 UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; } 00192 00193 UrlAsyncFetcher* DefaultDistributedFetcher() { 00194 return default_distributed_fetcher_; 00195 } 00196 00197 Timer* timer() const { return http_cache_->timer(); } 00198 00199 void MakePropertyCaches(CacheInterface* backend_cache); 00200 00201 HTTPCache* http_cache() const { return http_cache_.get(); } 00202 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); } 00203 PropertyCache* page_property_cache() const { 00204 return page_property_cache_.get(); 00205 } 00206 00207 const PropertyCache::Cohort* dom_cohort() const { return dom_cohort_; } 00208 void set_dom_cohort(const PropertyCache::Cohort* c) { dom_cohort_ = c; } 00209 00210 const PropertyCache::Cohort* blink_cohort() const { return blink_cohort_; } 00211 void set_blink_cohort(const PropertyCache::Cohort* c) { blink_cohort_ = c; } 00212 00213 const PropertyCache::Cohort* beacon_cohort() const { return beacon_cohort_; } 00214 void set_beacon_cohort(const PropertyCache::Cohort* c) { beacon_cohort_ = c; } 00215 00220 CacheInterface* filesystem_metadata_cache() const { 00221 return filesystem_metadata_cache_; 00222 } 00223 void set_filesystem_metadata_cache(CacheInterface* x) { 00224 filesystem_metadata_cache_ = x; 00225 } 00226 00231 CacheInterface* metadata_cache() const { return metadata_cache_; } 00232 void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; } 00233 00234 CriticalCssFinder* critical_css_finder() const { 00235 return critical_css_finder_.get(); 00236 } 00237 void set_critical_css_finder(CriticalCssFinder* finder); 00238 00239 CriticalImagesFinder* critical_images_finder() const { 00240 return critical_images_finder_.get(); 00241 } 00242 void set_critical_images_finder(CriticalImagesFinder* finder); 00243 00244 CriticalSelectorFinder* critical_selector_finder() const { 00245 return critical_selector_finder_.get(); 00246 } 00247 void set_critical_selector_finder(CriticalSelectorFinder* finder); 00248 00249 FlushEarlyInfoFinder* flush_early_info_finder() const { 00250 return flush_early_info_finder_.get(); 00251 } 00252 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder); 00253 00254 UserAgentMatcher* user_agent_matcher() const { 00255 return user_agent_matcher_; 00256 } 00257 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; } 00258 00259 CacheHtmlInfoFinder* cache_html_info_finder() const { 00260 return cache_html_info_finder_.get(); 00261 } 00262 00263 SimpleRandom* simple_random() { 00264 return &simple_random_; 00265 } 00266 00267 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder); 00268 00271 bool store_outputs_in_file_system() { return store_outputs_in_file_system_; } 00272 void set_store_outputs_in_file_system(bool store) { 00273 store_outputs_in_file_system_ = store; 00274 } 00275 00276 RewriteStats* rewrite_stats() const { return rewrite_stats_; } 00277 MessageHandler* message_handler() const { return message_handler_; } 00278 00283 NamedLock* MakeCreationLock(const GoogleString& name); 00284 00286 NamedLock* MakeInputLock(const GoogleString& name); 00287 00289 bool TryLockForCreation(NamedLock* creation_lock); 00290 00294 void LockForCreation(NamedLock* creation_lock, 00295 QueuedWorkerPool::Sequence* worker, Function* callback); 00296 00298 void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00299 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) { 00300 default_system_fetcher_ = fetcher; 00301 } 00302 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) { 00303 default_distributed_fetcher_ = fetcher; 00304 } 00305 00312 bool HandleBeacon(StringPiece body, 00313 StringPiece user_agent, 00314 const RequestContextPtr& request_context); 00315 00321 RewriteOptions* global_options(); 00322 00325 const RewriteOptions* global_options() const; 00326 00330 void reset_global_options(RewriteOptions* options); 00331 00333 RewriteOptions* NewOptions(); 00334 00344 OptionsBoolPair GetQueryOptions(GoogleUrl* request_url, 00345 RequestHeaders* request_headers, 00346 ResponseHeaders* response_headers); 00347 00352 static bool ScanSplitHtmlRequest(const RequestContextPtr& ctx, 00353 const RewriteOptions* options, 00354 GoogleString* url); 00355 00360 RewriteOptions* GetCustomOptions(RequestHeaders* request_headers, 00361 RewriteOptions* domain_options, 00362 RewriteOptions* query_options); 00363 00366 GoogleString GetPagePropertyCacheKey(StringPiece url, 00367 const RewriteOptions* options, 00368 StringPiece device_type_suffix); 00369 00370 GoogleString GetPagePropertyCacheKey(StringPiece url, 00371 StringPiece options_signature_hash, 00372 StringPiece device_type_suffix); 00373 00378 GoogleString GetFallbackPagePropertyCacheKey(const GoogleUrl& request_url, 00379 const RewriteOptions* options, 00380 StringPiece device_type_suffix); 00381 00390 RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx); 00391 00394 RewriteDriver* NewRewriteDriverFromPool( 00395 RewriteDriverPool* pool, const RequestContextPtr& request_ctx); 00396 00410 RewriteDriver* NewUnmanagedRewriteDriver( 00411 RewriteDriverPool* pool, RewriteOptions* options, 00412 const RequestContextPtr& request_ctx); 00413 00424 RewriteDriver* NewCustomRewriteDriver( 00425 RewriteOptions* custom_options, const RequestContextPtr& request_ctx); 00426 00439 void ReleaseRewriteDriver(RewriteDriver* rewrite_driver); 00440 00441 ThreadSystem* thread_system() { return thread_system_; } 00442 UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; } 00443 00446 void set_shutting_down() { 00447 shutting_down_.set_value(true); 00448 } 00449 00450 bool shutting_down() const { 00451 return shutting_down_.value(); 00452 } 00453 00463 void ShutDownDrivers(); 00464 00485 void MergeNonCachingResponseHeaders(const ResourcePtr& input, 00486 const OutputResourcePtr& output) { 00487 MergeNonCachingResponseHeaders(*input->response_headers(), 00488 output->response_headers()); 00489 } 00490 00492 void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers, 00493 ResponseHeaders* output_headers); 00494 00496 QueuedWorkerPool* html_workers() { return html_workers_; } 00497 00499 QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; } 00500 00503 QueuedWorkerPool* low_priority_rewrite_workers() { 00504 return low_priority_rewrite_workers_; 00505 } 00506 00510 size_t num_active_rewrite_drivers(); 00511 00515 void InitWorkersAndDecodingDriver(); 00516 00519 static bool IsExcludedAttribute(const char* attribute); 00520 00528 bool response_headers_finalized() const { 00529 return response_headers_finalized_; 00530 } 00531 void set_response_headers_finalized(bool x) { 00532 response_headers_finalized_ = x; 00533 } 00534 00538 RewriteDriverPool* standard_rewrite_driver_pool() { 00539 return available_rewrite_drivers_.get(); 00540 } 00541 00543 PropertyCache* MakePropertyCache(const GoogleString& cache_key_prefix, 00544 CacheInterface* cache) const; 00545 00547 const GoogleString& hostname() const { 00548 return hostname_; 00549 } 00550 void set_hostname(const GoogleString& x) { 00551 hostname_ = x; 00552 } 00553 00556 void AddOriginalContentLengthHeader(const ResourceVector& inputs, 00557 ResponseHeaders* headers); 00558 00560 virtual RewriteDriverPool* SelectDriverPool(bool using_spdy); 00561 00564 virtual void ApplySessionFetchers(const RequestContextPtr& req, 00565 RewriteDriver* driver); 00566 00567 const RewriteDriver* decoding_driver() const { 00568 return decoding_driver_.get(); 00569 } 00570 00581 virtual bool ProxiesHtml() const = 0; 00582 00584 RequestProperties* NewRequestProperties(); 00585 00588 void DeleteCacheOnDestruction(CacheInterface* cache); 00589 00590 protected: 00593 void ManageRewriteDriverPool(RewriteDriverPool* pool) { 00594 additional_driver_pools_.push_back(pool); 00595 } 00596 00597 private: 00598 friend class ServerContextTest; 00599 typedef std::set<RewriteDriver*> RewriteDriverSet; 00600 00602 void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver); 00603 00605 ThreadSystem* thread_system_; 00606 RewriteStats* rewrite_stats_; 00607 GoogleString file_prefix_; 00608 FileSystem* file_system_; 00609 FilenameEncoder* filename_encoder_; 00610 UrlNamer* url_namer_; 00611 scoped_ptr<RewriteOptionsManager> rewrite_options_manager_; 00612 UserAgentMatcher* user_agent_matcher_; 00613 Scheduler* scheduler_; 00614 UrlAsyncFetcher* default_system_fetcher_; 00615 UrlAsyncFetcher* default_distributed_fetcher_; 00616 Hasher* hasher_; 00617 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00618 scoped_ptr<CriticalCssFinder> critical_css_finder_; 00619 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_; 00620 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_; 00621 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_; 00622 00626 MD5Hasher lock_hasher_; 00627 00630 MD5Hasher contents_hasher_; 00631 00632 Statistics* statistics_; 00633 00634 scoped_ptr<HTTPCache> http_cache_; 00635 scoped_ptr<PropertyCache> page_property_cache_; 00636 CacheInterface* filesystem_metadata_cache_; 00637 CacheInterface* metadata_cache_; 00638 00639 bool store_outputs_in_file_system_; 00640 bool response_headers_finalized_; 00641 bool enable_property_cache_; 00642 00643 NamedLockManager* lock_manager_; 00644 MessageHandler* message_handler_; 00645 00646 const PropertyCache::Cohort* dom_cohort_; 00647 const PropertyCache::Cohort* blink_cohort_; 00648 const PropertyCache::Cohort* beacon_cohort_; 00649 00655 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_; 00656 00658 std::vector<RewriteDriverPool*> additional_driver_pools_; 00659 00665 RewriteDriverSet active_rewrite_drivers_; 00666 00674 bool trying_to_cleanup_rewrite_drivers_; 00675 RewriteDriverSet deferred_release_rewrite_drivers_; 00676 00679 RewriteDriverFactory* factory_; 00680 00681 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_; 00682 00686 scoped_ptr<RewriteOptions> base_class_options_; 00687 00697 scoped_ptr<RewriteDriver> decoding_driver_; 00698 00699 QueuedWorkerPool* html_workers_; 00700 QueuedWorkerPool* rewrite_workers_; 00701 QueuedWorkerPool* low_priority_rewrite_workers_; 00702 00703 AtomicBool shutting_down_; 00704 00706 StaticAssetManager* static_asset_manager_; 00707 00710 scoped_ptr<ThreadSynchronizer> thread_synchronizer_; 00711 00713 scoped_ptr<ExperimentMatcher> experiment_matcher_; 00714 00715 UsageDataReporter* usage_data_reporter_; 00716 00718 GoogleString hostname_; 00719 00722 SimpleRandom simple_random_; 00723 00724 DISALLOW_COPY_AND_ASSIGN(ServerContext); 00725 }; 00726 00727 } 00728 00729 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_