Page Speed Optimization Libraries
1.4.26.1
|
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/cache_interface.h" 00035 #include "net/instaweb/util/public/md5_hasher.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 00042 namespace net_instaweb { 00043 00044 class AbstractMutex; 00045 class BlinkCriticalLineDataFinder; 00046 class CacheHtmlInfoFinder; 00047 class ContentType; 00048 class CriticalCssFinder; 00049 class CriticalImagesFinder; 00050 class CriticalSelectorFinder; 00051 class FileSystem; 00052 class FilenameEncoder; 00053 class FlushEarlyInfoFinder; 00054 class Function; 00055 class FuriousMatcher; 00056 class GoogleUrl; 00057 class Hasher; 00058 class MessageHandler; 00059 class NamedLock; 00060 class NamedLockManager; 00061 class PropertyCache; 00062 class RequestHeaders; 00063 class ResponseHeaders; 00064 class RewriteDriver; 00065 class RewriteDriverFactory; 00066 class RewriteDriverPool; 00067 class RewriteOptions; 00068 class RewriteStats; 00069 class Scheduler; 00070 class StaticAssetManager; 00071 class Statistics; 00072 class ThreadSynchronizer; 00073 class ThreadSystem; 00074 class Timer; 00075 class UrlAsyncFetcher; 00076 class UrlNamer; 00077 class UsageDataReporter; 00078 class UserAgentMatcher; 00079 00080 typedef RefCountedPtr<OutputResource> OutputResourcePtr; 00081 typedef std::vector<OutputResourcePtr> OutputResourceVector; 00082 00090 class ServerContext { 00091 public: 00092 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair; 00093 00095 static const int64 kGeneratedMaxAgeMs; 00096 00100 static const char kResourceEtagValue[]; 00101 static const char kCacheKeyResourceNamePrefix[]; 00102 00104 static const char kStatisticsGroup[]; 00105 00107 static const char kStaleHash[]; 00108 00109 explicit ServerContext(RewriteDriverFactory* factory); 00110 virtual ~ServerContext(); 00111 00117 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00118 ResponseHeaders* header) const { 00119 SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header); 00120 } 00121 00124 void SetDefaultLongCacheHeadersWithCharset( 00125 const ContentType* content_type, StringPiece charset, 00126 ResponseHeaders* header) const; 00127 00129 void SetContentType(const ContentType* content_type, ResponseHeaders* header); 00130 00131 void set_filename_prefix(const StringPiece& file_prefix); 00132 void set_statistics(Statistics* x) { statistics_ = x; } 00133 void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; } 00134 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; } 00135 void set_enable_property_cache(bool enabled); 00136 void set_message_handler(MessageHandler* x) { message_handler_ = x; } 00137 00138 StringPiece filename_prefix() const { return file_prefix_; } 00139 Statistics* statistics() const { return statistics_; } 00140 NamedLockManager* lock_manager() const { return lock_manager_; } 00141 RewriteDriverFactory* factory() const { return factory_; } 00142 ThreadSynchronizer* thread_synchronizer() { 00143 return thread_synchronizer_.get(); 00144 } 00145 FuriousMatcher* furious_matcher() { return furious_matcher_.get(); } 00146 00155 void ApplyInputCacheControl(const ResourceVector& inputs, 00156 ResponseHeaders* headers); 00157 00159 bool IsPagespeedResource(const GoogleUrl& url); 00160 00162 bool IsNonStalePagespeedResource(const GoogleUrl& url); 00163 00166 bool IsImminentlyExpiring(int64 start_date_ms, int64 expire_ms) const; 00167 00168 void ComputeSignature(RewriteOptions* rewrite_options) const; 00169 00171 Hasher* hasher() const { return hasher_; } 00172 const Hasher* lock_hasher() const { return &lock_hasher_; } 00173 const Hasher* contents_hasher() const { return &contents_hasher_; } 00174 FileSystem* file_system() { return file_system_; } 00175 void set_file_system(FileSystem* fs ) { file_system_ = fs; } 00176 FilenameEncoder* filename_encoder() const { return filename_encoder_; } 00177 void set_filename_encoder(FilenameEncoder* x) { filename_encoder_ = x; } 00178 UrlNamer* url_namer() const { return url_namer_; } 00179 void set_url_namer(UrlNamer* n) { url_namer_ = n; } 00180 StaticAssetManager* static_asset_manager() const { 00181 return static_asset_manager_; 00182 } 00183 void set_static_asset_manager(StaticAssetManager* manager) { 00184 static_asset_manager_ = manager; 00185 } 00186 Scheduler* scheduler() const { return scheduler_; } 00187 void set_scheduler(Scheduler* s) { scheduler_ = s; } 00188 bool has_default_system_fetcher() { return default_system_fetcher_ != NULL; } 00189 bool has_default_distributed_fetcher() { 00190 return default_distributed_fetcher_ != NULL; 00191 } 00194 UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; } 00195 00196 UrlAsyncFetcher* DefaultDistributedFetcher() { 00197 return default_distributed_fetcher_; 00198 } 00199 00200 Timer* timer() const { return http_cache_->timer(); } 00201 00202 void MakePropertyCaches(CacheInterface* backend_cache); 00203 00204 HTTPCache* http_cache() const { return http_cache_.get(); } 00205 void set_http_cache(HTTPCache* x) { http_cache_.reset(x); } 00206 PropertyCache* page_property_cache() const { 00207 return page_property_cache_.get(); 00208 } 00209 PropertyCache* client_property_cache() const { 00210 return client_property_cache_.get(); 00211 } 00212 00217 CacheInterface* filesystem_metadata_cache() const { 00218 return filesystem_metadata_cache_.get(); 00219 } 00220 void set_filesystem_metadata_cache(CacheInterface* x) { 00221 filesystem_metadata_cache_.reset(x); 00222 } 00223 00228 CacheInterface* metadata_cache() const { return metadata_cache_.get(); } 00229 void set_metadata_cache(CacheInterface* x) { metadata_cache_.reset(x); } 00230 00232 CacheInterface* release_metadata_cache() { return metadata_cache_.release(); } 00233 00238 void set_owned_cache(CacheInterface* owned_cache) { 00239 owned_cache_.reset(owned_cache); 00240 } 00241 00242 CriticalCssFinder* critical_css_finder() const { 00243 return critical_css_finder_.get(); 00244 } 00245 void set_critical_css_finder(CriticalCssFinder* finder); 00246 00247 CriticalImagesFinder* critical_images_finder() const { 00248 return critical_images_finder_.get(); 00249 } 00250 void set_critical_images_finder(CriticalImagesFinder* finder); 00251 00252 CriticalSelectorFinder* critical_selector_finder() const { 00253 return critical_selector_finder_.get(); 00254 } 00255 void set_critical_selector_finder(CriticalSelectorFinder* finder); 00256 00257 FlushEarlyInfoFinder* flush_early_info_finder() const { 00258 return flush_early_info_finder_.get(); 00259 } 00260 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder); 00261 00262 UserAgentMatcher* user_agent_matcher() { 00263 return user_agent_matcher_; 00264 } 00265 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; } 00266 00267 BlinkCriticalLineDataFinder* blink_critical_line_data_finder() const { 00268 return blink_critical_line_data_finder_.get(); 00269 } 00270 00271 void set_blink_critical_line_data_finder( 00272 BlinkCriticalLineDataFinder* finder); 00273 00274 CacheHtmlInfoFinder* cache_html_info_finder() const { 00275 return cache_html_info_finder_.get(); 00276 } 00277 00278 void set_cache_html_info_finder(CacheHtmlInfoFinder* finder); 00279 00282 bool store_outputs_in_file_system() { return store_outputs_in_file_system_; } 00283 void set_store_outputs_in_file_system(bool store) { 00284 store_outputs_in_file_system_ = store; 00285 } 00286 00287 void RefreshIfImminentlyExpiring(Resource* resource, 00288 MessageHandler* handler) const; 00289 00290 RewriteStats* rewrite_stats() const { return rewrite_stats_; } 00291 MessageHandler* message_handler() const { return message_handler_; } 00292 00297 void ReadAsync(Resource::NotCacheablePolicy not_cacheable_policy, 00298 const RequestContextPtr& request_context, 00299 Resource::AsyncCallback* callback); 00300 00305 NamedLock* MakeCreationLock(const GoogleString& name); 00306 00308 NamedLock* MakeInputLock(const GoogleString& name); 00309 00311 bool TryLockForCreation(NamedLock* creation_lock); 00312 00316 void LockForCreation(NamedLock* creation_lock, 00317 QueuedWorkerPool::Sequence* worker, Function* callback); 00318 00320 void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00321 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) { 00322 default_system_fetcher_ = fetcher; 00323 } 00324 void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) { 00325 default_distributed_fetcher_ = fetcher; 00326 } 00327 00334 bool HandleBeacon(StringPiece body, 00335 StringPiece user_agent, 00336 const RequestContextPtr& request_context); 00337 00343 RewriteOptions* global_options(); 00344 00347 const RewriteOptions* global_options() const; 00348 00352 void reset_global_options(RewriteOptions* options); 00353 00355 RewriteOptions* NewOptions(); 00356 00366 OptionsBoolPair GetQueryOptions(GoogleUrl* request_url, 00367 RequestHeaders* request_headers, 00368 ResponseHeaders* response_headers); 00369 00374 RewriteOptions* GetCustomOptions(RequestHeaders* request_headers, 00375 RewriteOptions* domain_options, 00376 RewriteOptions* query_options); 00377 00380 GoogleString GetPagePropertyCacheKey(StringPiece url, 00381 const RewriteOptions* options, 00382 StringPiece device_type_suffix); 00383 00384 GoogleString GetPagePropertyCacheKey(StringPiece url, 00385 StringPiece options_signature_hash, 00386 StringPiece device_type_suffix); 00387 00396 RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx); 00397 00400 RewriteDriver* NewRewriteDriverFromPool( 00401 RewriteDriverPool* pool, const RequestContextPtr& request_ctx); 00402 00416 RewriteDriver* NewUnmanagedRewriteDriver( 00417 RewriteDriverPool* pool, RewriteOptions* options, 00418 const RequestContextPtr& request_ctx); 00419 00430 RewriteDriver* NewCustomRewriteDriver( 00431 RewriteOptions* custom_options, const RequestContextPtr& request_ctx); 00432 00445 void ReleaseRewriteDriver(RewriteDriver* rewrite_driver); 00446 00447 ThreadSystem* thread_system() { return thread_system_; } 00448 UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; } 00449 00452 void set_shutting_down() { 00453 shutting_down_.set_value(true); 00454 } 00455 00456 bool shutting_down() const { 00457 return shutting_down_.value(); 00458 } 00459 00469 void ShutDownDrivers(); 00470 00491 void MergeNonCachingResponseHeaders(const ResourcePtr& input, 00492 const OutputResourcePtr& output) { 00493 MergeNonCachingResponseHeaders(*input->response_headers(), 00494 output->response_headers()); 00495 } 00496 00498 void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers, 00499 ResponseHeaders* output_headers); 00500 00502 QueuedWorkerPool* html_workers() { return html_workers_; } 00503 00505 QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; } 00506 00509 QueuedWorkerPool* low_priority_rewrite_workers() { 00510 return low_priority_rewrite_workers_; 00511 } 00512 00516 size_t num_active_rewrite_drivers(); 00517 00521 void InitWorkersAndDecodingDriver(); 00522 00525 static bool IsExcludedAttribute(const char* attribute); 00526 00534 bool response_headers_finalized() const { 00535 return response_headers_finalized_; 00536 } 00537 void set_response_headers_finalized(bool x) { 00538 response_headers_finalized_ = x; 00539 } 00540 00544 RewriteDriverPool* standard_rewrite_driver_pool() { 00545 return available_rewrite_drivers_.get(); 00546 } 00547 00549 PropertyCache* MakePropertyCache(const GoogleString& cache_key_prefix, 00550 CacheInterface* cache) const; 00551 00553 const GoogleString& hostname() const { 00554 return hostname_; 00555 } 00556 void set_hostname(const GoogleString& x) { 00557 hostname_ = x; 00558 } 00559 00562 void AddOriginalContentLengthHeader(const ResourceVector& inputs, 00563 ResponseHeaders* headers); 00564 00566 virtual RewriteDriverPool* SelectDriverPool(bool using_spdy); 00567 00570 virtual void ApplySessionFetchers(const RequestContextPtr& req, 00571 RewriteDriver* driver); 00572 00573 const RewriteDriver* decoding_driver() const { 00574 return decoding_driver_.get(); 00575 } 00576 00587 virtual bool ProxiesHtml() const = 0; 00588 00589 protected: 00592 void ManageRewriteDriverPool(RewriteDriverPool* pool) { 00593 additional_driver_pools_.push_back(pool); 00594 } 00595 00596 private: 00597 friend class ServerContextTest; 00598 typedef std::set<RewriteDriver*> RewriteDriverSet; 00599 00601 void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver); 00602 00607 void GetResourceInfo(const GoogleUrl& url, bool* is_pagespeed_resource, 00608 bool* is_stale); 00609 00611 ThreadSystem* thread_system_; 00612 RewriteStats* rewrite_stats_; 00613 GoogleString file_prefix_; 00614 FileSystem* file_system_; 00615 FilenameEncoder* filename_encoder_; 00616 UrlNamer* url_namer_; 00617 UserAgentMatcher* user_agent_matcher_; 00618 Scheduler* scheduler_; 00619 UrlAsyncFetcher* default_system_fetcher_; 00620 UrlAsyncFetcher* default_distributed_fetcher_; 00621 Hasher* hasher_; 00622 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00623 scoped_ptr<CriticalCssFinder> critical_css_finder_; 00624 scoped_ptr<CriticalSelectorFinder> critical_selector_finder_; 00625 scoped_ptr<BlinkCriticalLineDataFinder> blink_critical_line_data_finder_; 00626 scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_; 00627 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_; 00628 00632 MD5Hasher lock_hasher_; 00633 00636 MD5Hasher contents_hasher_; 00637 00638 Statistics* statistics_; 00639 00640 scoped_ptr<HTTPCache> http_cache_; 00641 scoped_ptr<PropertyCache> page_property_cache_; 00642 scoped_ptr<PropertyCache> client_property_cache_; 00643 scoped_ptr<CacheInterface> filesystem_metadata_cache_; 00644 scoped_ptr<CacheInterface> metadata_cache_; 00645 00646 bool store_outputs_in_file_system_; 00647 bool response_headers_finalized_; 00648 bool enable_property_cache_; 00649 00650 NamedLockManager* lock_manager_; 00651 MessageHandler* message_handler_; 00652 00658 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_; 00659 00661 std::vector<RewriteDriverPool*> additional_driver_pools_; 00662 00668 RewriteDriverSet active_rewrite_drivers_; 00669 00677 bool trying_to_cleanup_rewrite_drivers_; 00678 RewriteDriverSet deferred_release_rewrite_drivers_; 00679 00682 RewriteDriverFactory* factory_; 00683 00684 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_; 00685 00689 scoped_ptr<RewriteOptions> base_class_options_; 00690 00700 scoped_ptr<RewriteDriver> decoding_driver_; 00701 00702 QueuedWorkerPool* html_workers_; 00703 QueuedWorkerPool* rewrite_workers_; 00704 QueuedWorkerPool* low_priority_rewrite_workers_; 00705 00706 AtomicBool shutting_down_; 00707 00709 StaticAssetManager* static_asset_manager_; 00710 00713 scoped_ptr<ThreadSynchronizer> thread_synchronizer_; 00714 00716 scoped_ptr<FuriousMatcher> furious_matcher_; 00717 00718 UsageDataReporter* usage_data_reporter_; 00719 00720 scoped_ptr<CacheInterface> owned_cache_; 00721 00723 GoogleString hostname_; 00724 00725 DISALLOW_COPY_AND_ASSIGN(ServerContext); 00726 }; 00727 00728 } 00729 00730 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_