Page Speed Optimization Libraries  1.8.31.3
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/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 pagespeed { namespace js { struct JsTokenizerPatterns; } }
00044 
00045 namespace net_instaweb {
00046 
00047 class AbstractMutex;
00048 class AsyncFetch;
00049 class CacheHtmlInfoFinder;
00050 class CacheInterface;
00051 class CachePropertyStore;
00052 class CriticalCssFinder;
00053 class CriticalImagesFinder;
00054 class CriticalLineInfoFinder;
00055 class CriticalSelectorFinder;
00056 class RequestProperties;
00057 class ExperimentMatcher;
00058 class FileSystem;
00059 class FlushEarlyInfoFinder;
00060 class Function;
00061 class GoogleUrl;
00062 class Hasher;
00063 class MessageHandler;
00064 class NamedLock;
00065 class NamedLockManager;
00066 class PropertyStore;
00067 class RequestHeaders;
00068 class ResponseHeaders;
00069 class RewriteDriver;
00070 class RewriteDriverFactory;
00071 class RewriteDriverPool;
00072 class RewriteFilter;
00073 class RewriteOptions;
00074 class RewriteOptionsManager;
00075 class RewriteQuery;
00076 class RewriteStats;
00077 class Scheduler;
00078 class StaticAssetManager;
00079 class Statistics;
00080 class ThreadSynchronizer;
00081 class ThreadSystem;
00082 class Timer;
00083 class UrlAsyncFetcher;
00084 class UrlNamer;
00085 class UsageDataReporter;
00086 class UserAgentMatcher;
00087 struct ContentType;
00088 
00089 typedef RefCountedPtr<OutputResource> OutputResourcePtr;
00090 typedef std::vector<OutputResourcePtr> OutputResourceVector;
00091 
00099 class ServerContext {
00100  public:
00101   typedef std::pair<RewriteOptions*, bool> OptionsBoolPair;
00102 
00104   static const int64 kGeneratedMaxAgeMs;
00105 
00109   static const char kResourceEtagValue[];
00110   static const char kCacheKeyResourceNamePrefix[];
00111 
00113   static const char kStatisticsGroup[];
00114 
00115   explicit ServerContext(RewriteDriverFactory* factory);
00116   virtual ~ServerContext();
00117 
00127   void SetDefaultLongCacheHeaders(
00128       const ContentType* content_type, StringPiece charset,
00129       StringPiece cache_control_suffix, ResponseHeaders* header) const;
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   ExperimentMatcher* experiment_matcher() { return experiment_matcher_.get(); }
00146 
00155   void ApplyInputCacheControl(const ResourceVector& inputs,
00156                               ResponseHeaders* headers);
00157 
00159   bool IsPagespeedResource(const GoogleUrl& url);
00160 
00163   const RewriteFilter* FindFilterForDecoding(const StringPiece& id) const;
00164 
00166   bool DecodeUrlGivenOptions(const GoogleUrl& url,
00167                              const RewriteOptions* options,
00168                              const UrlNamer* url_namer,
00169                              StringVector* decoded_urls) const;
00170 
00171   void ComputeSignature(RewriteOptions* rewrite_options) const;
00172 
00174   Hasher* hasher() const { return hasher_; }
00175   const Hasher* lock_hasher() const { return &lock_hasher_; }
00176   const Hasher* contents_hasher() const { return &contents_hasher_; }
00177   FileSystem* file_system() { return file_system_; }
00178   void set_file_system(FileSystem* fs ) { file_system_ = fs; }
00179   UrlNamer* url_namer() const { return url_namer_; }
00180   void set_url_namer(UrlNamer* n) { url_namer_ = n; }
00181   RewriteOptionsManager* rewrite_options_manager() const {
00182     return rewrite_options_manager_.get();
00183   }
00185   void SetRewriteOptionsManager(RewriteOptionsManager* rom);
00186   StaticAssetManager* static_asset_manager() const {
00187     return static_asset_manager_;
00188   }
00189   void set_static_asset_manager(StaticAssetManager* manager) {
00190     static_asset_manager_ = manager;
00191   }
00192   Scheduler* scheduler() const { return scheduler_; }
00193   void set_scheduler(Scheduler* s) { scheduler_ = s; }
00194   bool has_default_system_fetcher() const {
00195     return default_system_fetcher_ != NULL;
00196   }
00197   bool has_default_distributed_fetcher() {
00198     return default_distributed_fetcher_ != NULL;
00199   }
00202   UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; }
00203 
00204   UrlAsyncFetcher* DefaultDistributedFetcher() {
00205     return default_distributed_fetcher_;
00206   }
00207 
00208   Timer* timer() const { return timer_; }
00209 
00211   void set_timer(Timer* timer) { timer_ = timer; }
00212 
00213   HTTPCache* http_cache() const { return http_cache_.get(); }
00214   void set_http_cache(HTTPCache* x) { http_cache_.reset(x); }
00215 
00217   void MakePagePropertyCache(PropertyStore* property_store);
00218 
00219   PropertyCache* page_property_cache() const {
00220     return page_property_cache_.get();
00221   }
00222 
00223   const PropertyCache::Cohort* dom_cohort() const { return dom_cohort_; }
00224   void set_dom_cohort(const PropertyCache::Cohort* c) { dom_cohort_ = c; }
00225 
00226   const PropertyCache::Cohort* blink_cohort() const { return blink_cohort_; }
00227   void set_blink_cohort(const PropertyCache::Cohort* c) { blink_cohort_ = c; }
00228 
00229   const PropertyCache::Cohort* beacon_cohort() const { return beacon_cohort_; }
00230   void set_beacon_cohort(const PropertyCache::Cohort* c) { beacon_cohort_ = c; }
00231 
00232   const PropertyCache::Cohort* fix_reflow_cohort() const {
00233     return fix_reflow_cohort_;
00234   }
00235   void set_fix_reflow_cohort(const PropertyCache::Cohort* c) {
00236     fix_reflow_cohort_ = c;
00237   }
00238 
00243   CacheInterface* filesystem_metadata_cache() const {
00244     return filesystem_metadata_cache_;
00245   }
00246   void set_filesystem_metadata_cache(CacheInterface* x) {
00247     filesystem_metadata_cache_ = x;
00248   }
00249 
00254   CacheInterface* metadata_cache() const { return metadata_cache_; }
00255   void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; }
00256 
00257   CriticalCssFinder* critical_css_finder() const {
00258     return critical_css_finder_.get();
00259   }
00260   void set_critical_css_finder(CriticalCssFinder* finder);
00261 
00262   CriticalImagesFinder* critical_images_finder() const {
00263     return critical_images_finder_.get();
00264   }
00265   void set_critical_images_finder(CriticalImagesFinder* finder);
00266 
00267   CriticalSelectorFinder* critical_selector_finder() const {
00268     return critical_selector_finder_.get();
00269   }
00270   void set_critical_selector_finder(CriticalSelectorFinder* finder);
00271 
00272   FlushEarlyInfoFinder* flush_early_info_finder() const {
00273     return flush_early_info_finder_.get();
00274   }
00275   void set_flush_early_info_finder(FlushEarlyInfoFinder* finder);
00276 
00277   UserAgentMatcher* user_agent_matcher() const {
00278     return user_agent_matcher_;
00279   }
00280   void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; }
00281 
00282   CacheHtmlInfoFinder* cache_html_info_finder() const {
00283     return cache_html_info_finder_.get();
00284   }
00285 
00286   SimpleRandom* simple_random() {
00287     return &simple_random_;
00288   }
00289 
00290   void set_cache_html_info_finder(CacheHtmlInfoFinder* finder);
00291 
00292   CriticalLineInfoFinder* critical_line_info_finder() const {
00293     return critical_line_info_finder_.get();
00294   }
00295 
00297   void set_critical_line_info_finder(CriticalLineInfoFinder* finder);
00298 
00301   bool store_outputs_in_file_system() { return store_outputs_in_file_system_; }
00302   void set_store_outputs_in_file_system(bool store) {
00303     store_outputs_in_file_system_ = store;
00304   }
00305 
00306   RewriteStats* rewrite_stats() const { return rewrite_stats_; }
00307   MessageHandler* message_handler() const { return message_handler_; }
00308 
00313   NamedLock* MakeCreationLock(const GoogleString& name);
00314 
00316   NamedLock* MakeInputLock(const GoogleString& name);
00317 
00319   bool TryLockForCreation(NamedLock* creation_lock);
00320 
00324   void LockForCreation(NamedLock* creation_lock,
00325                        QueuedWorkerPool::Sequence* worker, Function* callback);
00326 
00328   void set_hasher(Hasher* hasher) { hasher_ = hasher; }
00329   void set_default_system_fetcher(UrlAsyncFetcher* fetcher) {
00330     default_system_fetcher_ = fetcher;
00331   }
00332   void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) {
00333     default_distributed_fetcher_ = fetcher;
00334   }
00335 
00342   bool HandleBeacon(StringPiece body,
00343                     StringPiece user_agent,
00344                     const RequestContextPtr& request_context);
00345 
00351   RewriteOptions* global_options();
00352 
00355   const RewriteOptions* global_options() const;
00356 
00360   void reset_global_options(RewriteOptions* options);
00361 
00363   RewriteOptions* NewOptions();
00364 
00384   bool GetQueryOptions(const RequestContextPtr& request_context,
00385                        const RewriteOptions* domain_options,
00386                        GoogleUrl* request_url,
00387                        RequestHeaders* request_headers,
00388                        ResponseHeaders* response_headers,
00389                        RewriteQuery* rewrite_query);
00390 
00395   static bool ScanSplitHtmlRequest(const RequestContextPtr& ctx,
00396                                    const RewriteOptions* options,
00397                                    GoogleString* url);
00398 
00403   RewriteOptions* GetCustomOptions(RequestHeaders* request_headers,
00404                                    RewriteOptions* domain_options,
00405                                    RewriteOptions* query_options);
00406 
00409   GoogleString GetRewriteOptionsSignatureHash(const RewriteOptions* options);
00410 
00419   RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx);
00420 
00423   RewriteDriver* NewRewriteDriverFromPool(
00424       RewriteDriverPool* pool, const RequestContextPtr& request_ctx);
00425 
00439   RewriteDriver* NewUnmanagedRewriteDriver(
00440       RewriteDriverPool* pool, RewriteOptions* options,
00441       const RequestContextPtr& request_ctx);
00442 
00453   RewriteDriver* NewCustomRewriteDriver(
00454       RewriteOptions* custom_options, const RequestContextPtr& request_ctx);
00455 
00468   void ReleaseRewriteDriver(RewriteDriver* rewrite_driver);
00469 
00470   ThreadSystem* thread_system() { return thread_system_; }
00471   UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; }
00472 
00475   void set_shutting_down() {
00476     shutting_down_.set_value(true);
00477   }
00478 
00479   bool shutting_down() const {
00480     return shutting_down_.value();
00481   }
00482 
00492   void ShutDownDrivers();
00493 
00514   void MergeNonCachingResponseHeaders(const ResourcePtr& input,
00515                                       const OutputResourcePtr& output) {
00516     MergeNonCachingResponseHeaders(*input->response_headers(),
00517                                    output->response_headers());
00518   }
00519 
00521   void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers,
00522                                       ResponseHeaders* output_headers);
00523 
00525   QueuedWorkerPool* html_workers() { return html_workers_; }
00526 
00528   QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; }
00529 
00532   QueuedWorkerPool* low_priority_rewrite_workers() {
00533     return low_priority_rewrite_workers_;
00534   }
00535 
00539   size_t num_active_rewrite_drivers();
00540 
00543   void InitWorkers();
00544 
00547   static bool IsExcludedAttribute(const char* attribute);
00548 
00556   bool response_headers_finalized() const {
00557     return response_headers_finalized_;
00558   }
00559   void set_response_headers_finalized(bool x) {
00560     response_headers_finalized_ = x;
00561   }
00562 
00566   RewriteDriverPool* standard_rewrite_driver_pool() {
00567     return available_rewrite_drivers_.get();
00568   }
00569 
00571   const GoogleString& hostname() const {
00572     return hostname_;
00573   }
00574   void set_hostname(const GoogleString& x) {
00575     hostname_ = x;
00576   }
00577 
00580   void AddOriginalContentLengthHeader(const ResourceVector& inputs,
00581                                       ResponseHeaders* headers);
00582 
00584   virtual RewriteDriverPool* SelectDriverPool(bool using_spdy);
00585 
00588   virtual void ApplySessionFetchers(const RequestContextPtr& req,
00589                                     RewriteDriver* driver);
00590 
00601   virtual bool ProxiesHtml() const = 0;
00602 
00604   RequestProperties* NewRequestProperties();
00605 
00608   void DeleteCacheOnDestruction(CacheInterface* cache);
00609 
00610   void set_cache_property_store(CachePropertyStore* p);
00611 
00614   void set_decoding_driver(RewriteDriver* rd) { decoding_driver_ = rd; }
00615 
00617   virtual PropertyStore* CreatePropertyStore(CacheInterface* cache_backend);
00618 
00622   const PropertyCache::Cohort* AddCohort(
00623       const GoogleString& cohort_name,
00624       PropertyCache* pcache);
00625 
00630   const PropertyCache::Cohort* AddCohortWithCache(
00631       const GoogleString& cohort_name,
00632       CacheInterface* cache,
00633       PropertyCache* pcache);
00634 
00637   const CacheInterface* pcache_cache_backend();
00638 
00639   const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns() const {
00640     return js_tokenizer_patterns_;
00641   }
00642 
00645   void ShowCacheHandler(StringPiece url, AsyncFetch* fetch,
00646                         RewriteOptions* options);
00647 
00651   GoogleString ShowCacheForm(const char* user_agent) const;
00652 
00653  protected:
00656   void ManageRewriteDriverPool(RewriteDriverPool* pool) {
00657     additional_driver_pools_.push_back(pool);
00658   }
00659 
00660  private:
00661   friend class ServerContextTest;
00662   typedef std::set<RewriteDriver*> RewriteDriverSet;
00663 
00665   void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver);
00666 
00668   ThreadSystem* thread_system_;
00669   RewriteStats* rewrite_stats_;
00670   GoogleString file_prefix_;
00671   FileSystem* file_system_;
00672   UrlNamer* url_namer_;
00673   scoped_ptr<RewriteOptionsManager> rewrite_options_manager_;
00674   UserAgentMatcher* user_agent_matcher_;
00675   Scheduler* scheduler_;
00676   UrlAsyncFetcher* default_system_fetcher_;
00677   UrlAsyncFetcher* default_distributed_fetcher_;
00678   Hasher* hasher_;
00679   scoped_ptr<CriticalImagesFinder> critical_images_finder_;
00680   scoped_ptr<CriticalCssFinder> critical_css_finder_;
00681   scoped_ptr<CriticalSelectorFinder> critical_selector_finder_;
00682   scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_;
00683   scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_;
00684   scoped_ptr<CriticalLineInfoFinder> critical_line_info_finder_;
00685 
00689   MD5Hasher lock_hasher_; 
00690 
00693   MD5Hasher contents_hasher_;
00694 
00695   Statistics* statistics_;
00696 
00697   Timer* timer_;
00698   scoped_ptr<HTTPCache> http_cache_;
00699   scoped_ptr<PropertyCache> page_property_cache_;
00700   CacheInterface* filesystem_metadata_cache_;
00701   CacheInterface* metadata_cache_;
00702 
00703   bool store_outputs_in_file_system_;
00704   bool response_headers_finalized_;
00705   bool enable_property_cache_;
00706 
00707   NamedLockManager* lock_manager_;
00708   MessageHandler* message_handler_;
00709 
00710   const PropertyCache::Cohort* dom_cohort_;
00711   const PropertyCache::Cohort* blink_cohort_;
00712   const PropertyCache::Cohort* beacon_cohort_;
00713   const PropertyCache::Cohort* fix_reflow_cohort_;
00714 
00720   scoped_ptr<RewriteDriverPool> available_rewrite_drivers_;
00721 
00723   std::vector<RewriteDriverPool*> additional_driver_pools_;
00724 
00730   RewriteDriverSet active_rewrite_drivers_;
00731 
00739   bool trying_to_cleanup_rewrite_drivers_;
00740   RewriteDriverSet deferred_release_rewrite_drivers_;
00741   bool shutdown_drivers_called_;
00742 
00745   RewriteDriverFactory* factory_;
00746 
00747   scoped_ptr<AbstractMutex> rewrite_drivers_mutex_;
00748 
00751   scoped_ptr<RewriteOptions> base_class_options_;
00752 
00756   RewriteDriver* decoding_driver_;
00757 
00758   QueuedWorkerPool* html_workers_; 
00759   QueuedWorkerPool* rewrite_workers_; 
00760   QueuedWorkerPool* low_priority_rewrite_workers_; 
00761 
00762   AtomicBool shutting_down_;
00763 
00765   StaticAssetManager* static_asset_manager_;
00766 
00769   scoped_ptr<ThreadSynchronizer> thread_synchronizer_;
00770 
00772   scoped_ptr<ExperimentMatcher> experiment_matcher_;
00773 
00774   UsageDataReporter* usage_data_reporter_;
00775 
00777   GoogleString hostname_;
00778 
00781   SimpleRandom simple_random_;
00783   const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_;
00784 
00785   scoped_ptr<CachePropertyStore> cache_property_store_;
00786 
00787   DISALLOW_COPY_AND_ASSIGN(ServerContext);
00788 };
00789 
00790 }  
00791 
00792 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines