Page Speed Optimization Libraries
1.5.27.2
|
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/queued_worker_pool.h" 00036 #include "net/instaweb/util/public/ref_counted_ptr.h" 00037 #include "net/instaweb/util/public/scoped_ptr.h" 00038 #include "net/instaweb/util/public/string.h" 00039 #include "net/instaweb/util/public/string_util.h" 00040 00041 namespace net_instaweb { 00042 00043 class AbstractMutex; 00044 class BlinkCriticalLineDataFinder; 00045 class CacheHtmlInfoFinder; 00046 class CacheInterface; 00047 class ContentType; 00048 class CriticalCssFinder; 00049 class CriticalImagesFinder; 00050 class CriticalSelectorFinder; 00051 class DeviceProperties; 00052 class FileSystem; 00053 class FilenameEncoder; 00054 class FlushEarlyInfoFinder; 00055 class Function; 00056 class FuriousMatcher; 00057 class GoogleUrl; 00058 class Hasher; 00059 class MessageHandler; 00060 class NamedLock; 00061 class NamedLockManager; 00062 class PropertyCache; 00063 class RequestHeaders; 00064 class ResponseHeaders; 00065 class RewriteDriver; 00066 class RewriteDriverFactory; 00067 class RewriteDriverPool; 00068 class RewriteOptions; 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 00081 typedef RefCountedPtr<OutputResource> OutputResourcePtr; 00082 typedef std::vector<OutputResourcePtr> OutputResourceVector; 00083 00091 class ServerContext { 00092 public: 00093 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair; 00094 00096 static const int64 kGeneratedMaxAgeMs; 00097 00101 static const char kResourceEtagValue[]; 00102 static const char kCacheKeyResourceNamePrefix[]; 00103 00105 static const char kStatisticsGroup[]; 00106 00108 static const char kStaleHash[]; 00109 00110 explicit ServerContext(RewriteDriverFactory* factory); 00111 virtual ~ServerContext(); 00112 00118 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00119 ResponseHeaders* header) const { 00120 SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header); 00121 } 00122 00125 void SetDefaultLongCacheHeadersWithCharset( 00126 const ContentType* content_type, StringPiece charset, 00127 ResponseHeaders* header) const; 00128 00130 void SetContentType(const ContentType* content_type, ResponseHeaders* header); 00131 00132 void set_filename_prefix(const StringPiece& file_prefix); 00133 void set_statistics(Statistics* x) { statistics_ = x; } 00134 void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; } 00135 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; } 00136 void set_enable_property_cache(bool enabled); 00137 void set_message_handler(MessageHandler* x) { message_handler_ = x; } 00138 00139 StringPiece filename_prefix() const { return file_prefix_; } 00140 Statistics* statistics() const { return statistics_; } 00141 NamedLockManager* lock_manager() const { return lock_manager_; } 00142 RewriteDriverFactory* factory() const { return factory_; } 00143 ThreadSynchronizer* thread_synchronizer() { 00144 return thread_synchronizer_.get(); 00145 } 00146 FuriousMatcher* furious_matcher() { return furious_matcher_.get(); } 00147 00156 void ApplyInputCacheControl(const ResourceVector& inputs, 00157 ResponseHeaders* headers); 00158 00160 bool IsPagespeedResource(const GoogleUrl& url); 00161 00163 bool IsNonStalePagespeedResource(const GoogleUrl& url); 00164 00167 bool IsImminentlyExpiring(int64 start_date_ms, int64 expire_ms) const; 00168 00169 void ComputeSignature(RewriteOptions* rewrite_options) const; 00170 00172 Hasher* hasher() const { return hasher_; } 00173 const Hasher* lock_hasher() const { return &lock_hasher_; } 00174 const Hasher* contents_hasher() const { return &contents_hasher_; } 00175 FileSystem* file_system() { return file_system_; } 00176 void set_file_system(FileSystem* fs ) { file_system_ = fs; } 00177 FilenameEncoder* filename_encoder() const { return filename_encoder_; } 00178 void set_filename_encoder(FilenameEncoder* x) { filename_encoder_ = x; } 00179 UrlNamer* url_namer() const { return url_namer_; } 00180 void set_url_namer(UrlNamer* n) { url_namer_ = n; } 00181 StaticAssetManager* static_asset_manager() const { 00182 return static_asset_manager_; 00183 } 00184 void set_static_asset_manager(StaticAssetManager* manager) { 00185 static_asset_manager_ = manager; 00186 } 00187 Scheduler* scheduler() const { return scheduler_; } 00188 void set_scheduler(Scheduler* s) { scheduler_ = s; } 00189 bool has_default_system_fetcher() { return default_system_fetcher_ != NULL; } 00190 bool has_default_distributed_fetcher() { 00191 return default_distributed_fetcher_ != NULL; 00192 } 00195 UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; } 00196 00197 UrlAsyncFetcher* DefaultDistributedFetcher() { 00198 return default_distributed_fetcher_; 00199 } 00200 00201 Timer* timer() const { return http_cache_->timer(); } 00202 00203 void MakePropertyCaches(CacheInterface* backend_cache); 00204 00205 HTTPCache* http_cache() const { return http_cache_.get(); } 00206 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); } 00207 PropertyCache* page_property_cache() const { 00208 return page_property_cache_.get(); 00209 } 00210 PropertyCache* client_property_cache() const { 00211 return client_property_cache_.get(); 00212 } 00213 00218 CacheInterface* filesystem_metadata_cache() const { 00219 return filesystem_metadata_cache_; 00220 } 00221 void set_filesystem_metadata_cache(CacheInterface* x) { 00222 filesystem_metadata_cache_ = x; 00223 } 00224 00229 CacheInterface* metadata_cache() const { return metadata_cache_; } 00230 void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; } 00231 00232 CriticalCssFinder* critical_css_finder() const { 00233 return critical_css_finder_.get(); 00234 } 00235 void set_critical_css_finder(CriticalCssFinder* finder); 00236 00237 CriticalImagesFinder* critical_images_finder() const { 00238 return critical_images_finder_.get(); 00239 } 00240 void set_critical_images_finder(CriticalImagesFinder* finder); 00241 00242 CriticalSelectorFinder* critical_selector_finder() const { 00243 return critical_selector_finder_.get(); 00244 } 00245 void set_critical_selector_finder(CriticalSelectorFinder* finder); 00246 00247 FlushEarlyInfoFinder* flush_early_info_finder() const { 00248 return flush_early_info_finder_.get(); 00249 } 00250 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder); 00251 00252 UserAgentMatcher* user_agent_matcher() const { 00253 return user_agent_matcher_; 00254 } 00255 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; } 00256 00257 BlinkCriticalLineDataFinder* blink_critical_line_data_finder() const { 00258 return blink_critical_line_data_finder_.get(); 00259 } 00260 00261 void set_blink_critical_line_data_finder( 00262 BlinkCriticalLineDataFinder* finder); 00263 00264 CacheHtmlInfoFinder* cache_html_info_finder() const { 00265 return cache_html_info_finder_.get(); 00266 } 00267 00268 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder); 00269 00272 bool store_outputs_in_file_system() { return store_outputs_in_file_system_; } 00273 void set_store_outputs_in_file_system(bool store) { 00274 store_outputs_in_file_system_ = store; 00275 } 00276 00277 void RefreshIfImminentlyExpiring(Resource* resource, 00278 MessageHandler* handler) const; 00279 00280 RewriteStats* rewrite_stats() const { return rewrite_stats_; } 00281 MessageHandler* message_handler() const { return message_handler_; } 00282 00287 void ReadAsync(Resource::NotCacheablePolicy not_cacheable_policy, 00288 const RequestContextPtr& request_context, 00289 Resource::AsyncCallback* callback); 00290 00295 NamedLock* MakeCreationLock(const GoogleString& name); 00296 00298 NamedLock* MakeInputLock(const GoogleString& name); 00299 00301 bool TryLockForCreation(NamedLock* creation_lock); 00302 00306 void LockForCreation(NamedLock* creation_lock, 00307 QueuedWorkerPool::Sequence* worker, Function* callback); 00308 00310 void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00311 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) { 00312 default_system_fetcher_ = fetcher; 00313 } 00314 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) { 00315 default_distributed_fetcher_ = fetcher; 00316 } 00317 00324 bool HandleBeacon(StringPiece body, 00325 StringPiece user_agent, 00326 const RequestContextPtr& request_context); 00327 00333 RewriteOptions* global_options(); 00334 00337 const RewriteOptions* global_options() const; 00338 00342 void reset_global_options(RewriteOptions* options); 00343 00345 RewriteOptions* NewOptions(); 00346 00356 OptionsBoolPair GetQueryOptions(GoogleUrl* request_url, 00357 RequestHeaders* request_headers, 00358 ResponseHeaders* response_headers); 00359 00364 RewriteOptions* GetCustomOptions(RequestHeaders* request_headers, 00365 RewriteOptions* domain_options, 00366 RewriteOptions* query_options); 00367 00370 GoogleString GetPagePropertyCacheKey(StringPiece url, 00371 const RewriteOptions* options, 00372 StringPiece device_type_suffix); 00373 00374 GoogleString GetPagePropertyCacheKey(StringPiece url, 00375 StringPiece options_signature_hash, 00376 StringPiece device_type_suffix); 00377 00381 GoogleString GetFallbackPagePropertyCacheKey(StringPiece url, 00382 const RewriteOptions* options, 00383 StringPiece device_type_suffix); 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 PropertyCache* MakePropertyCache(const GoogleString& cache_key_prefix, 00547 CacheInterface* cache) const; 00548 00550 const GoogleString& hostname() const { 00551 return hostname_; 00552 } 00553 void set_hostname(const GoogleString& x) { 00554 hostname_ = x; 00555 } 00556 00559 void AddOriginalContentLengthHeader(const ResourceVector& inputs, 00560 ResponseHeaders* headers); 00561 00563 virtual RewriteDriverPool* SelectDriverPool(bool using_spdy); 00564 00567 virtual void ApplySessionFetchers(const RequestContextPtr& req, 00568 RewriteDriver* driver); 00569 00570 const RewriteDriver* decoding_driver() const { 00571 return decoding_driver_.get(); 00572 } 00573 00584 virtual bool ProxiesHtml() const = 0; 00585 00587 DeviceProperties* NewDeviceProperties(); 00588 00591 void DeleteCacheOnDestruction(CacheInterface* cache); 00592 00593 protected: 00596 void ManageRewriteDriverPool(RewriteDriverPool* pool) { 00597 additional_driver_pools_.push_back(pool); 00598 } 00599 00600 private: 00601 friend class ServerContextTest; 00602 typedef std::set<RewriteDriver*> RewriteDriverSet; 00603 00605 void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver); 00606 00611 void GetResourceInfo(const GoogleUrl& url, bool* is_pagespeed_resource, 00612 bool* is_stale); 00613 00615 ThreadSystem* thread_system_; 00616 RewriteStats* rewrite_stats_; 00617 GoogleString file_prefix_; 00618 FileSystem* file_system_; 00619 FilenameEncoder* filename_encoder_; 00620 UrlNamer* url_namer_; 00621 UserAgentMatcher* user_agent_matcher_; 00622 Scheduler* scheduler_; 00623 UrlAsyncFetcher* default_system_fetcher_; 00624 UrlAsyncFetcher* default_distributed_fetcher_; 00625 Hasher* hasher_; 00626 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00627 scoped_ptr<CriticalCssFinder> critical_css_finder_; 00628 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_; 00629 scoped_ptr<BlinkCriticalLineDataFinder> blink_critical_line_data_finder_; 00630 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_; 00631 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_; 00632 00636 MD5Hasher lock_hasher_; 00637 00640 MD5Hasher contents_hasher_; 00641 00642 Statistics* statistics_; 00643 00644 scoped_ptr<HTTPCache> http_cache_; 00645 scoped_ptr<PropertyCache> page_property_cache_; 00646 scoped_ptr<PropertyCache> client_property_cache_; 00647 CacheInterface* filesystem_metadata_cache_; 00648 CacheInterface* metadata_cache_; 00649 00650 bool store_outputs_in_file_system_; 00651 bool response_headers_finalized_; 00652 bool enable_property_cache_; 00653 00654 NamedLockManager* lock_manager_; 00655 MessageHandler* message_handler_; 00656 00662 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_; 00663 00665 std::vector<RewriteDriverPool*> additional_driver_pools_; 00666 00672 RewriteDriverSet active_rewrite_drivers_; 00673 00681 bool trying_to_cleanup_rewrite_drivers_; 00682 RewriteDriverSet deferred_release_rewrite_drivers_; 00683 00686 RewriteDriverFactory* factory_; 00687 00688 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_; 00689 00693 scoped_ptr<RewriteOptions> base_class_options_; 00694 00704 scoped_ptr<RewriteDriver> decoding_driver_; 00705 00706 QueuedWorkerPool* html_workers_; 00707 QueuedWorkerPool* rewrite_workers_; 00708 QueuedWorkerPool* low_priority_rewrite_workers_; 00709 00710 AtomicBool shutting_down_; 00711 00713 StaticAssetManager* static_asset_manager_; 00714 00717 scoped_ptr<ThreadSynchronizer> thread_synchronizer_; 00718 00720 scoped_ptr<FuriousMatcher> furious_matcher_; 00721 00722 UsageDataReporter* usage_data_reporter_; 00723 00725 GoogleString hostname_; 00726 00727 DISALLOW_COPY_AND_ASSIGN(ServerContext); 00728 }; 00729 00730 } 00731 00732 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_