Page Speed Optimization Libraries
1.2.24.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 ContentType; 00047 class CriticalImagesFinder; 00048 class FileSystem; 00049 class FilenameEncoder; 00050 class FlushEarlyInfoFinder; 00051 class Function; 00052 class FuriousMatcher; 00053 class GoogleUrl; 00054 class Hasher; 00055 class MessageHandler; 00056 class NamedLock; 00057 class NamedLockManager; 00058 class PropertyCache; 00059 class RequestHeaders; 00060 class ResponseHeaders; 00061 class RewriteDriver; 00062 class RewriteDriverFactory; 00063 class RewriteDriverPool; 00064 class RewriteOptions; 00065 class RewriteStats; 00066 class Scheduler; 00067 class StaticJavascriptManager; 00068 class Statistics; 00069 class ThreadSynchronizer; 00070 class ThreadSystem; 00071 class Timer; 00072 class UrlAsyncFetcher; 00073 class UrlNamer; 00074 class UsageDataReporter; 00075 class UserAgentMatcher; 00076 00077 typedef RefCountedPtr<OutputResource> OutputResourcePtr; 00078 typedef std::vector<OutputResourcePtr> OutputResourceVector; 00079 00087 class ServerContext { 00088 public: 00089 typedef std::pair<RewriteOptions*, bool> OptionsBoolPair; 00090 00092 static const int64 kGeneratedMaxAgeMs; 00093 00097 static const char kResourceEtagValue[]; 00098 static const char kCacheKeyResourceNamePrefix[]; 00099 00101 static const char kStatisticsGroup[]; 00102 00103 explicit ServerContext(RewriteDriverFactory* factory); 00104 virtual ~ServerContext(); 00105 00111 void SetDefaultLongCacheHeaders(const ContentType* content_type, 00112 ResponseHeaders* header) const { 00113 SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header); 00114 } 00115 00118 void SetDefaultLongCacheHeadersWithCharset( 00119 const ContentType* content_type, StringPiece charset, 00120 ResponseHeaders* header) const; 00121 00123 void SetContentType(const ContentType* content_type, ResponseHeaders* header); 00124 00125 void set_filename_prefix(const StringPiece& file_prefix); 00126 void set_statistics(Statistics* x) { statistics_ = x; } 00127 void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; } 00128 void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; } 00129 void set_enable_property_cache(bool enabled); 00130 void set_message_handler(MessageHandler* x) { message_handler_ = x; } 00131 00132 StringPiece filename_prefix() const { return file_prefix_; } 00133 Statistics* statistics() const { return statistics_; } 00134 NamedLockManager* lock_manager() const { return lock_manager_; } 00135 RewriteDriverFactory* factory() const { return factory_; } 00136 ThreadSynchronizer* thread_synchronizer() { 00137 return thread_synchronizer_.get(); 00138 } 00139 FuriousMatcher* furious_matcher() { return furious_matcher_.get(); } 00140 00153 bool Write(const ResourceVector& inputs, const StringPiece& contents, 00154 const ContentType* content_type, StringPiece charset, 00155 OutputResource* output, MessageHandler* handler); 00156 00165 void ApplyInputCacheControl(const ResourceVector& inputs, 00166 ResponseHeaders* headers); 00167 00169 bool IsPagespeedResource(const GoogleUrl& url); 00170 00173 bool IsImminentlyExpiring(int64 start_date_ms, int64 expire_ms) const; 00174 00175 void ComputeSignature(RewriteOptions* rewrite_options) const; 00176 00178 Hasher* hasher() const { return hasher_; } 00179 const Hasher* lock_hasher() const { return &lock_hasher_; } 00180 const Hasher* contents_hasher() const { return &contents_hasher_; } 00181 FileSystem* file_system() { return file_system_; } 00182 void set_file_system(FileSystem* fs ) { file_system_ = fs; } 00183 FilenameEncoder* filename_encoder() const { return filename_encoder_; } 00184 void set_filename_encoder(FilenameEncoder* x) { filename_encoder_ = x; } 00185 UrlNamer* url_namer() const { return url_namer_; } 00186 void set_url_namer(UrlNamer* n) { url_namer_ = n; } 00187 StaticJavascriptManager* static_javascript_manager() const { 00188 return static_javascript_manager_; 00189 } 00190 void set_static_javascript_manager(StaticJavascriptManager* manager) { 00191 static_javascript_manager_ = manager; 00192 } 00193 Scheduler* scheduler() const { return scheduler_; } 00194 void set_scheduler(Scheduler* s) { scheduler_ = s; } 00195 bool has_default_system_fetcher() { return default_system_fetcher_ != NULL; } 00196 00199 UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; } 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_.get(); 00220 } 00221 void set_filesystem_metadata_cache(CacheInterface* x) { 00222 filesystem_metadata_cache_.reset(x); 00223 } 00224 00229 CacheInterface* metadata_cache() const { return metadata_cache_.get(); } 00230 void set_metadata_cache(CacheInterface* x) { metadata_cache_.reset(x); } 00231 00233 CacheInterface* release_metadata_cache() { return metadata_cache_.release(); } 00234 00239 void set_owned_cache(CacheInterface* owned_cache) { 00240 owned_cache_.reset(owned_cache); 00241 } 00242 00243 CriticalImagesFinder* critical_images_finder() const { 00244 return critical_images_finder_.get(); 00245 } 00246 void set_critical_images_finder(CriticalImagesFinder* finder); 00247 00248 FlushEarlyInfoFinder* flush_early_info_finder() const { 00249 return flush_early_info_finder_.get(); 00250 } 00251 void set_flush_early_info_finder(FlushEarlyInfoFinder* finder); 00252 00253 const UserAgentMatcher& user_agent_matcher() const { 00254 return *user_agent_matcher_; 00255 } 00256 void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; } 00257 00258 BlinkCriticalLineDataFinder* blink_critical_line_data_finder() const { 00259 return blink_critical_line_data_finder_.get(); 00260 } 00261 00262 void set_blink_critical_line_data_finder( 00263 BlinkCriticalLineDataFinder* finder); 00264 00267 bool store_outputs_in_file_system() { return store_outputs_in_file_system_; } 00268 void set_store_outputs_in_file_system(bool store) { 00269 store_outputs_in_file_system_ = store; 00270 } 00271 00272 void RefreshIfImminentlyExpiring(Resource* resource, 00273 MessageHandler* handler) const; 00274 00275 RewriteStats* rewrite_stats() const { return rewrite_stats_; } 00276 MessageHandler* message_handler() const { return message_handler_; } 00277 00282 void ReadAsync(Resource::NotCacheablePolicy not_cacheable_policy, 00283 const RequestContextPtr& request_context, 00284 Resource::AsyncCallback* callback); 00285 00290 NamedLock* MakeCreationLock(const GoogleString& name); 00291 00293 NamedLock* MakeInputLock(const GoogleString& name); 00294 00296 bool TryLockForCreation(NamedLock* creation_lock); 00297 00301 void LockForCreation(NamedLock* creation_lock, 00302 QueuedWorkerPool::Sequence* worker, Function* callback); 00303 00305 void set_hasher(Hasher* hasher) { hasher_ = hasher; } 00306 void set_default_system_fetcher(UrlAsyncFetcher* fetcher) { 00307 default_system_fetcher_ = fetcher; 00308 } 00309 00314 bool HandleBeacon(const StringPiece& unparsed_url); 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 RewriteOptions* GetCustomOptions(RequestHeaders* request_headers, 00353 RewriteOptions* domain_options, 00354 RewriteOptions* query_options); 00355 00364 RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx); 00365 00368 RewriteDriver* NewRewriteDriverFromPool( 00369 RewriteDriverPool* pool, const RequestContextPtr& request_ctx); 00370 00384 RewriteDriver* NewUnmanagedRewriteDriver( 00385 RewriteDriverPool* pool, RewriteOptions* options, 00386 const RequestContextPtr& request_ctx); 00387 00398 RewriteDriver* NewCustomRewriteDriver( 00399 RewriteOptions* custom_options, const RequestContextPtr& request_ctx); 00400 00413 void ReleaseRewriteDriver(RewriteDriver* rewrite_driver); 00414 00415 ThreadSystem* thread_system() { return thread_system_; } 00416 UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; } 00417 00420 void set_shutting_down() { 00421 shutting_down_.set_value(true); 00422 } 00423 00424 bool shutting_down() const { 00425 return shutting_down_.value(); 00426 } 00427 00437 void ShutDownDrivers(); 00438 00459 void MergeNonCachingResponseHeaders(const ResourcePtr& input, 00460 const OutputResourcePtr& output) { 00461 MergeNonCachingResponseHeaders(*input->response_headers(), 00462 output->response_headers()); 00463 } 00464 00466 void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers, 00467 ResponseHeaders* output_headers); 00468 00470 QueuedWorkerPool* html_workers() { return html_workers_; } 00471 00473 QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; } 00474 00477 QueuedWorkerPool* low_priority_rewrite_workers() { 00478 return low_priority_rewrite_workers_; 00479 } 00480 00484 size_t num_active_rewrite_drivers(); 00485 00489 void InitWorkersAndDecodingDriver(); 00490 00493 static bool IsExcludedAttribute(const char* attribute); 00494 00502 bool response_headers_finalized() const { 00503 return response_headers_finalized_; 00504 } 00505 void set_response_headers_finalized(bool x) { 00506 response_headers_finalized_ = x; 00507 } 00508 00512 RewriteDriverPool* standard_rewrite_driver_pool() { 00513 return available_rewrite_drivers_.get(); 00514 } 00515 00517 PropertyCache* MakePropertyCache(const GoogleString& cache_key_prefix, 00518 CacheInterface* cache) const; 00519 00521 const GoogleString& hostname() const { 00522 return hostname_; 00523 } 00524 void set_hostname(const GoogleString& x) { 00525 hostname_ = x; 00526 } 00527 00528 protected: 00531 void ManageRewriteDriverPool(RewriteDriverPool* pool) { 00532 additional_driver_pools_.push_back(pool); 00533 } 00534 00535 private: 00536 friend class ServerContextTest; 00537 typedef std::set<RewriteDriver*> RewriteDriverSet; 00538 00540 void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver); 00541 00544 void AddOriginalContentLengthHeader(const ResourceVector& inputs, 00545 ResponseHeaders* headers); 00546 00548 ThreadSystem* thread_system_; 00549 RewriteStats* rewrite_stats_; 00550 GoogleString file_prefix_; 00551 FileSystem* file_system_; 00552 FilenameEncoder* filename_encoder_; 00553 UrlNamer* url_namer_; 00554 UserAgentMatcher* user_agent_matcher_; 00555 Scheduler* scheduler_; 00556 UrlAsyncFetcher* default_system_fetcher_; 00557 Hasher* hasher_; 00558 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00559 scoped_ptr<BlinkCriticalLineDataFinder> blink_critical_line_data_finder_; 00560 scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_; 00561 00565 MD5Hasher lock_hasher_; 00566 00569 MD5Hasher contents_hasher_; 00570 00571 Statistics* statistics_; 00572 00573 scoped_ptr<HTTPCache> http_cache_; 00574 scoped_ptr<PropertyCache> page_property_cache_; 00575 scoped_ptr<PropertyCache> client_property_cache_; 00576 scoped_ptr<CacheInterface> filesystem_metadata_cache_; 00577 scoped_ptr<CacheInterface> metadata_cache_; 00578 00579 bool store_outputs_in_file_system_; 00580 bool response_headers_finalized_; 00581 bool enable_property_cache_; 00582 00583 NamedLockManager* lock_manager_; 00584 MessageHandler* message_handler_; 00585 00591 scoped_ptr<RewriteDriverPool> available_rewrite_drivers_; 00592 00594 std::vector<RewriteDriverPool*> additional_driver_pools_; 00595 00601 RewriteDriverSet active_rewrite_drivers_; 00602 00610 bool trying_to_cleanup_rewrite_drivers_; 00611 RewriteDriverSet deferred_release_rewrite_drivers_; 00612 00615 RewriteDriverFactory* factory_; 00616 00617 scoped_ptr<AbstractMutex> rewrite_drivers_mutex_; 00618 00622 scoped_ptr<RewriteOptions> base_class_options_; 00623 00633 scoped_ptr<RewriteDriver> decoding_driver_; 00634 00635 QueuedWorkerPool* html_workers_; 00636 QueuedWorkerPool* rewrite_workers_; 00637 QueuedWorkerPool* low_priority_rewrite_workers_; 00638 00639 AtomicBool shutting_down_; 00640 00642 StaticJavascriptManager* static_javascript_manager_; 00643 00646 scoped_ptr<ThreadSynchronizer> thread_synchronizer_; 00647 00649 scoped_ptr<FuriousMatcher> furious_matcher_; 00650 00651 UsageDataReporter* usage_data_reporter_; 00652 00653 scoped_ptr<CacheInterface> owned_cache_; 00654 00656 GoogleString hostname_; 00657 00658 DISALLOW_COPY_AND_ASSIGN(ServerContext); 00659 }; 00660 00661 } 00662 00663 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_