Page Speed Optimization Libraries  1.3.25.1
net/instaweb/rewriter/public/server_context.h
Go to the documentation of this file.
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 CriticalCssFinder;
00048 class CriticalImagesFinder;
00049 class FileSystem;
00050 class FilenameEncoder;
00051 class FlushEarlyInfoFinder;
00052 class Function;
00053 class FuriousMatcher;
00054 class GoogleUrl;
00055 class Hasher;
00056 class MessageHandler;
00057 class NamedLock;
00058 class NamedLockManager;
00059 class PropertyCache;
00060 class RequestHeaders;
00061 class ResponseHeaders;
00062 class RewriteDriver;
00063 class RewriteDriverFactory;
00064 class RewriteDriverPool;
00065 class RewriteOptions;
00066 class RewriteStats;
00067 class Scheduler;
00068 class StaticAssetManager;
00069 class Statistics;
00070 class ThreadSynchronizer;
00071 class ThreadSystem;
00072 class Timer;
00073 class UrlAsyncFetcher;
00074 class UrlNamer;
00075 class UsageDataReporter;
00076 class UserAgentMatcher;
00077 
00078 typedef RefCountedPtr<OutputResource> OutputResourcePtr;
00079 typedef std::vector<OutputResourcePtr> OutputResourceVector;
00080 
00088 class ServerContext {
00089  public:
00090   typedef std::pair<RewriteOptions*, bool> OptionsBoolPair;
00091 
00093   static const int64 kGeneratedMaxAgeMs;
00094 
00098   static const char kResourceEtagValue[];
00099   static const char kCacheKeyResourceNamePrefix[];
00100 
00102   static const char kStatisticsGroup[];
00103 
00104   explicit ServerContext(RewriteDriverFactory* factory);
00105   virtual ~ServerContext();
00106 
00112   void SetDefaultLongCacheHeaders(const ContentType* content_type,
00113                                   ResponseHeaders* header) const {
00114     SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header);
00115   }
00116 
00119   void SetDefaultLongCacheHeadersWithCharset(
00120       const ContentType* content_type, StringPiece charset,
00121       ResponseHeaders* header) const;
00122 
00124   void SetContentType(const ContentType* content_type, ResponseHeaders* header);
00125 
00126   void set_filename_prefix(const StringPiece& file_prefix);
00127   void set_statistics(Statistics* x) { statistics_ = x; }
00128   void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; }
00129   void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; }
00130   void set_enable_property_cache(bool enabled);
00131   void set_message_handler(MessageHandler* x) { message_handler_ = x; }
00132 
00133   StringPiece filename_prefix() const { return file_prefix_; }
00134   Statistics* statistics() const { return statistics_; }
00135   NamedLockManager* lock_manager() const { return lock_manager_; }
00136   RewriteDriverFactory* factory() const { return factory_; }
00137   ThreadSynchronizer* thread_synchronizer() {
00138     return thread_synchronizer_.get();
00139   }
00140   FuriousMatcher* furious_matcher() { return furious_matcher_.get(); }
00141 
00150   void ApplyInputCacheControl(const ResourceVector& inputs,
00151                               ResponseHeaders* headers);
00152 
00154   bool IsPagespeedResource(const GoogleUrl& url);
00155 
00158   bool IsImminentlyExpiring(int64 start_date_ms, int64 expire_ms) const;
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   StaticAssetManager* static_asset_manager() const {
00173     return static_asset_manager_;
00174   }
00175   void set_static_asset_manager(StaticAssetManager* manager) {
00176     static_asset_manager_ = manager;
00177   }
00178   Scheduler* scheduler() const { return scheduler_; }
00179   void set_scheduler(Scheduler* s) { scheduler_ = s; }
00180   bool has_default_system_fetcher() { return default_system_fetcher_ != NULL; }
00181 
00184   UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; }
00185 
00186   Timer* timer() const { return http_cache_->timer(); }
00187 
00188   void MakePropertyCaches(CacheInterface* backend_cache);
00189 
00190   HTTPCache* http_cache() const { return http_cache_.get(); }
00191   void set_http_cache(HTTPCache* x) { http_cache_.reset(x); }
00192   PropertyCache* page_property_cache() const {
00193     return page_property_cache_.get();
00194   }
00195   PropertyCache* client_property_cache() const {
00196     return client_property_cache_.get();
00197   }
00198 
00203   CacheInterface* filesystem_metadata_cache() const {
00204     return filesystem_metadata_cache_.get();
00205   }
00206   void set_filesystem_metadata_cache(CacheInterface* x) {
00207     filesystem_metadata_cache_.reset(x);
00208   }
00209 
00214   CacheInterface* metadata_cache() const { return metadata_cache_.get(); }
00215   void set_metadata_cache(CacheInterface* x) { metadata_cache_.reset(x); }
00216 
00218   CacheInterface* release_metadata_cache() { return metadata_cache_.release(); }
00219 
00224   void set_owned_cache(CacheInterface* owned_cache) {
00225     owned_cache_.reset(owned_cache);
00226   }
00227 
00228   CriticalCssFinder* critical_css_finder() const {
00229     return critical_css_finder_.get();
00230   }
00231   void set_critical_css_finder(CriticalCssFinder* finder);
00232 
00233   CriticalImagesFinder* critical_images_finder() const {
00234     return critical_images_finder_.get();
00235   }
00236   void set_critical_images_finder(CriticalImagesFinder* finder);
00237 
00238   FlushEarlyInfoFinder* flush_early_info_finder() const {
00239     return flush_early_info_finder_.get();
00240   }
00241   void set_flush_early_info_finder(FlushEarlyInfoFinder* finder);
00242 
00243   UserAgentMatcher* user_agent_matcher() {
00244     return user_agent_matcher_;
00245   }
00246   void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; }
00247 
00248   BlinkCriticalLineDataFinder* blink_critical_line_data_finder() const {
00249     return blink_critical_line_data_finder_.get();
00250   }
00251 
00252   void set_blink_critical_line_data_finder(
00253       BlinkCriticalLineDataFinder* finder);
00254 
00257   bool store_outputs_in_file_system() { return store_outputs_in_file_system_; }
00258   void set_store_outputs_in_file_system(bool store) {
00259     store_outputs_in_file_system_ = store;
00260   }
00261 
00262   void RefreshIfImminentlyExpiring(Resource* resource,
00263                                    MessageHandler* handler) const;
00264 
00265   RewriteStats* rewrite_stats() const { return rewrite_stats_; }
00266   MessageHandler* message_handler() const { return message_handler_; }
00267 
00272   void ReadAsync(Resource::NotCacheablePolicy not_cacheable_policy,
00273                  const RequestContextPtr& request_context,
00274                  Resource::AsyncCallback* callback);
00275 
00280   NamedLock* MakeCreationLock(const GoogleString& name);
00281 
00283   NamedLock* MakeInputLock(const GoogleString& name);
00284 
00286   bool TryLockForCreation(NamedLock* creation_lock);
00287 
00291   void LockForCreation(NamedLock* creation_lock,
00292                        QueuedWorkerPool::Sequence* worker, Function* callback);
00293 
00295   void set_hasher(Hasher* hasher) { hasher_ = hasher; }
00296   void set_default_system_fetcher(UrlAsyncFetcher* fetcher) {
00297     default_system_fetcher_ = fetcher;
00298   }
00299 
00304   bool HandleBeacon(const StringPiece& unparsed_url,
00305                     const RequestContextPtr& request_context);
00306 
00312   RewriteOptions* global_options();
00313 
00316   const RewriteOptions* global_options() const;
00317 
00321   void reset_global_options(RewriteOptions* options);
00322 
00324   RewriteOptions* NewOptions();
00325 
00335   OptionsBoolPair GetQueryOptions(GoogleUrl* request_url,
00336                                   RequestHeaders* request_headers,
00337                                   ResponseHeaders* response_headers);
00338 
00343   RewriteOptions* GetCustomOptions(RequestHeaders* request_headers,
00344                                    RewriteOptions* domain_options,
00345                                    RewriteOptions* query_options);
00346 
00349   GoogleString GetPagePropertyCacheKey(const StringPiece& url,
00350                                        const RewriteOptions* options,
00351                                        const StringPiece& device_type_suffix);
00352 
00361   RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx);
00362 
00365   RewriteDriver* NewRewriteDriverFromPool(
00366       RewriteDriverPool* pool, const RequestContextPtr& request_ctx);
00367 
00381   RewriteDriver* NewUnmanagedRewriteDriver(
00382       RewriteDriverPool* pool, RewriteOptions* options,
00383       const RequestContextPtr& request_ctx);
00384 
00395   RewriteDriver* NewCustomRewriteDriver(
00396       RewriteOptions* custom_options, const RequestContextPtr& request_ctx);
00397 
00410   void ReleaseRewriteDriver(RewriteDriver* rewrite_driver);
00411 
00412   ThreadSystem* thread_system() { return thread_system_; }
00413   UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; }
00414 
00417   void set_shutting_down() {
00418     shutting_down_.set_value(true);
00419   }
00420 
00421   bool shutting_down() const {
00422     return shutting_down_.value();
00423   }
00424 
00434   void ShutDownDrivers();
00435 
00456   void MergeNonCachingResponseHeaders(const ResourcePtr& input,
00457                                       const OutputResourcePtr& output) {
00458     MergeNonCachingResponseHeaders(*input->response_headers(),
00459                                    output->response_headers());
00460   }
00461 
00463   void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers,
00464                                       ResponseHeaders* output_headers);
00465 
00467   QueuedWorkerPool* html_workers() { return html_workers_; }
00468 
00470   QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; }
00471 
00474   QueuedWorkerPool* low_priority_rewrite_workers() {
00475     return low_priority_rewrite_workers_;
00476   }
00477 
00481   size_t num_active_rewrite_drivers();
00482 
00486   void InitWorkersAndDecodingDriver();
00487 
00490   static bool IsExcludedAttribute(const char* attribute);
00491 
00499   bool response_headers_finalized() const {
00500     return response_headers_finalized_;
00501   }
00502   void set_response_headers_finalized(bool x) {
00503     response_headers_finalized_ = x;
00504   }
00505 
00509   RewriteDriverPool* standard_rewrite_driver_pool() {
00510     return available_rewrite_drivers_.get();
00511   }
00512 
00514   PropertyCache* MakePropertyCache(const GoogleString& cache_key_prefix,
00515                                    CacheInterface* cache) const;
00516 
00518   const GoogleString& hostname() const {
00519     return hostname_;
00520   }
00521   void set_hostname(const GoogleString& x) {
00522     hostname_ = x;
00523   }
00524 
00527   void AddOriginalContentLengthHeader(const ResourceVector& inputs,
00528                                       ResponseHeaders* headers);
00529 
00531   virtual RewriteDriverPool* SelectDriverPool(bool using_spdy);
00532 
00535   virtual void ApplySessionFetchers(const RequestContextPtr& req,
00536                                     RewriteDriver* driver);
00537 
00538   const RewriteDriver* decoding_driver() const {
00539     return decoding_driver_.get();
00540   }
00541 
00542  protected:
00545   void ManageRewriteDriverPool(RewriteDriverPool* pool) {
00546     additional_driver_pools_.push_back(pool);
00547   }
00548 
00549  private:
00550   friend class ServerContextTest;
00551   typedef std::set<RewriteDriver*> RewriteDriverSet;
00552 
00554   void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver);
00555 
00557   ThreadSystem* thread_system_;
00558   RewriteStats* rewrite_stats_;
00559   GoogleString file_prefix_;
00560   FileSystem* file_system_;
00561   FilenameEncoder* filename_encoder_;
00562   UrlNamer* url_namer_;
00563   UserAgentMatcher* user_agent_matcher_;
00564   Scheduler* scheduler_;
00565   UrlAsyncFetcher* default_system_fetcher_;
00566   Hasher* hasher_;
00567   scoped_ptr<CriticalImagesFinder> critical_images_finder_;
00568   scoped_ptr<CriticalCssFinder> critical_css_finder_;
00569   scoped_ptr<BlinkCriticalLineDataFinder> blink_critical_line_data_finder_;
00570   scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_;
00571 
00575   MD5Hasher lock_hasher_; 
00576 
00579   MD5Hasher contents_hasher_;
00580 
00581   Statistics* statistics_;
00582 
00583   scoped_ptr<HTTPCache> http_cache_;
00584   scoped_ptr<PropertyCache> page_property_cache_;
00585   scoped_ptr<PropertyCache> client_property_cache_;
00586   scoped_ptr<CacheInterface> filesystem_metadata_cache_;
00587   scoped_ptr<CacheInterface> metadata_cache_;
00588 
00589   bool store_outputs_in_file_system_;
00590   bool response_headers_finalized_;
00591   bool enable_property_cache_;
00592 
00593   NamedLockManager* lock_manager_;
00594   MessageHandler* message_handler_;
00595 
00601   scoped_ptr<RewriteDriverPool> available_rewrite_drivers_;
00602 
00604   std::vector<RewriteDriverPool*> additional_driver_pools_;
00605 
00611   RewriteDriverSet active_rewrite_drivers_;
00612 
00620   bool trying_to_cleanup_rewrite_drivers_;
00621   RewriteDriverSet deferred_release_rewrite_drivers_;
00622 
00625   RewriteDriverFactory* factory_;
00626 
00627   scoped_ptr<AbstractMutex> rewrite_drivers_mutex_;
00628 
00632   scoped_ptr<RewriteOptions> base_class_options_;
00633 
00643   scoped_ptr<RewriteDriver> decoding_driver_;
00644 
00645   QueuedWorkerPool* html_workers_; 
00646   QueuedWorkerPool* rewrite_workers_; 
00647   QueuedWorkerPool* low_priority_rewrite_workers_; 
00648 
00649   AtomicBool shutting_down_;
00650 
00652   StaticAssetManager* static_asset_manager_;
00653 
00656   scoped_ptr<ThreadSynchronizer> thread_synchronizer_;
00657 
00659   scoped_ptr<FuriousMatcher> furious_matcher_;
00660 
00661   UsageDataReporter* usage_data_reporter_;
00662 
00663   scoped_ptr<CacheInterface> owned_cache_;
00664 
00666   GoogleString hostname_;
00667 
00668   DISALLOW_COPY_AND_ASSIGN(ServerContext);
00669 };
00670 
00671 }  
00672 
00673 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines