Page Speed Optimization Libraries  1.8.31.2
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 
00123   void SetDefaultLongCacheHeaders(const ContentType* content_type,
00124                                   ResponseHeaders* header) const {
00125     SetDefaultLongCacheHeadersWithCharset(content_type, StringPiece(), header);
00126   }
00127 
00130   void SetDefaultLongCacheHeadersWithCharset(
00131       const ContentType* content_type, StringPiece charset,
00132       ResponseHeaders* header) const;
00133 
00134   void set_filename_prefix(const StringPiece& file_prefix);
00135   void set_statistics(Statistics* x) { statistics_ = x; }
00136   void set_rewrite_stats(RewriteStats* x) { rewrite_stats_ = x; }
00137   void set_lock_manager(NamedLockManager* x) { lock_manager_ = x; }
00138   void set_enable_property_cache(bool enabled);
00139   void set_message_handler(MessageHandler* x) { message_handler_ = x; }
00140 
00141   StringPiece filename_prefix() const { return file_prefix_; }
00142   Statistics* statistics() const { return statistics_; }
00143   NamedLockManager* lock_manager() const { return lock_manager_; }
00144   RewriteDriverFactory* factory() const { return factory_; }
00145   ThreadSynchronizer* thread_synchronizer() {
00146     return thread_synchronizer_.get();
00147   }
00148   ExperimentMatcher* experiment_matcher() { return experiment_matcher_.get(); }
00149 
00158   void ApplyInputCacheControl(const ResourceVector& inputs,
00159                               ResponseHeaders* headers);
00160 
00162   bool IsPagespeedResource(const GoogleUrl& url);
00163 
00166   const RewriteFilter* FindFilterForDecoding(const StringPiece& id) const;
00167 
00169   bool DecodeUrlGivenOptions(const GoogleUrl& url,
00170                              const RewriteOptions* options,
00171                              const UrlNamer* url_namer,
00172                              StringVector* decoded_urls) const;
00173 
00174   void ComputeSignature(RewriteOptions* rewrite_options) const;
00175 
00177   Hasher* hasher() const { return hasher_; }
00178   const Hasher* lock_hasher() const { return &lock_hasher_; }
00179   const Hasher* contents_hasher() const { return &contents_hasher_; }
00180   FileSystem* file_system() { return file_system_; }
00181   void set_file_system(FileSystem* fs ) { file_system_ = fs; }
00182   UrlNamer* url_namer() const { return url_namer_; }
00183   void set_url_namer(UrlNamer* n) { url_namer_ = n; }
00184   RewriteOptionsManager* rewrite_options_manager() const {
00185     return rewrite_options_manager_.get();
00186   }
00188   void SetRewriteOptionsManager(RewriteOptionsManager* rom);
00189   StaticAssetManager* static_asset_manager() const {
00190     return static_asset_manager_;
00191   }
00192   void set_static_asset_manager(StaticAssetManager* manager) {
00193     static_asset_manager_ = manager;
00194   }
00195   Scheduler* scheduler() const { return scheduler_; }
00196   void set_scheduler(Scheduler* s) { scheduler_ = s; }
00197   bool has_default_system_fetcher() const {
00198     return default_system_fetcher_ != NULL;
00199   }
00200   bool has_default_distributed_fetcher() {
00201     return default_distributed_fetcher_ != NULL;
00202   }
00205   UrlAsyncFetcher* DefaultSystemFetcher() { return default_system_fetcher_; }
00206 
00207   UrlAsyncFetcher* DefaultDistributedFetcher() {
00208     return default_distributed_fetcher_;
00209   }
00210 
00211   Timer* timer() const { return timer_; }
00212 
00214   void set_timer(Timer* timer) { timer_ = timer; }
00215 
00216   HTTPCache* http_cache() const { return http_cache_.get(); }
00217   void set_http_cache(HTTPCache* x) { http_cache_.reset(x); }
00218 
00220   void MakePagePropertyCache(PropertyStore* property_store);
00221 
00222   PropertyCache* page_property_cache() const {
00223     return page_property_cache_.get();
00224   }
00225 
00226   const PropertyCache::Cohort* dom_cohort() const { return dom_cohort_; }
00227   void set_dom_cohort(const PropertyCache::Cohort* c) { dom_cohort_ = c; }
00228 
00229   const PropertyCache::Cohort* blink_cohort() const { return blink_cohort_; }
00230   void set_blink_cohort(const PropertyCache::Cohort* c) { blink_cohort_ = c; }
00231 
00232   const PropertyCache::Cohort* beacon_cohort() const { return beacon_cohort_; }
00233   void set_beacon_cohort(const PropertyCache::Cohort* c) { beacon_cohort_ = c; }
00234 
00235   const PropertyCache::Cohort* fix_reflow_cohort() const {
00236     return fix_reflow_cohort_;
00237   }
00238   void set_fix_reflow_cohort(const PropertyCache::Cohort* c) {
00239     fix_reflow_cohort_ = c;
00240   }
00241 
00246   CacheInterface* filesystem_metadata_cache() const {
00247     return filesystem_metadata_cache_;
00248   }
00249   void set_filesystem_metadata_cache(CacheInterface* x) {
00250     filesystem_metadata_cache_ = x;
00251   }
00252 
00257   CacheInterface* metadata_cache() const { return metadata_cache_; }
00258   void set_metadata_cache(CacheInterface* x) { metadata_cache_ = x; }
00259 
00260   CriticalCssFinder* critical_css_finder() const {
00261     return critical_css_finder_.get();
00262   }
00263   void set_critical_css_finder(CriticalCssFinder* finder);
00264 
00265   CriticalImagesFinder* critical_images_finder() const {
00266     return critical_images_finder_.get();
00267   }
00268   void set_critical_images_finder(CriticalImagesFinder* finder);
00269 
00270   CriticalSelectorFinder* critical_selector_finder() const {
00271     return critical_selector_finder_.get();
00272   }
00273   void set_critical_selector_finder(CriticalSelectorFinder* finder);
00274 
00275   FlushEarlyInfoFinder* flush_early_info_finder() const {
00276     return flush_early_info_finder_.get();
00277   }
00278   void set_flush_early_info_finder(FlushEarlyInfoFinder* finder);
00279 
00280   UserAgentMatcher* user_agent_matcher() const {
00281     return user_agent_matcher_;
00282   }
00283   void set_user_agent_matcher(UserAgentMatcher* n) { user_agent_matcher_ = n; }
00284 
00285   CacheHtmlInfoFinder* cache_html_info_finder() const {
00286     return cache_html_info_finder_.get();
00287   }
00288 
00289   SimpleRandom* simple_random() {
00290     return &simple_random_;
00291   }
00292 
00293   void set_cache_html_info_finder(CacheHtmlInfoFinder* finder);
00294 
00295   CriticalLineInfoFinder* critical_line_info_finder() const {
00296     return critical_line_info_finder_.get();
00297   }
00298 
00300   void set_critical_line_info_finder(CriticalLineInfoFinder* finder);
00301 
00304   bool store_outputs_in_file_system() { return store_outputs_in_file_system_; }
00305   void set_store_outputs_in_file_system(bool store) {
00306     store_outputs_in_file_system_ = store;
00307   }
00308 
00309   RewriteStats* rewrite_stats() const { return rewrite_stats_; }
00310   MessageHandler* message_handler() const { return message_handler_; }
00311 
00316   NamedLock* MakeCreationLock(const GoogleString& name);
00317 
00319   NamedLock* MakeInputLock(const GoogleString& name);
00320 
00322   bool TryLockForCreation(NamedLock* creation_lock);
00323 
00327   void LockForCreation(NamedLock* creation_lock,
00328                        QueuedWorkerPool::Sequence* worker, Function* callback);
00329 
00331   void set_hasher(Hasher* hasher) { hasher_ = hasher; }
00332   void set_default_system_fetcher(UrlAsyncFetcher* fetcher) {
00333     default_system_fetcher_ = fetcher;
00334   }
00335   void set_default_distributed_fetcher(UrlAsyncFetcher* fetcher) {
00336     default_distributed_fetcher_ = fetcher;
00337   }
00338 
00345   bool HandleBeacon(StringPiece body,
00346                     StringPiece user_agent,
00347                     const RequestContextPtr& request_context);
00348 
00354   RewriteOptions* global_options();
00355 
00358   const RewriteOptions* global_options() const;
00359 
00363   void reset_global_options(RewriteOptions* options);
00364 
00366   RewriteOptions* NewOptions();
00367 
00381   bool GetQueryOptions(GoogleUrl* request_url,
00382                        RequestHeaders* request_headers,
00383                        ResponseHeaders* response_headers,
00384                        RewriteQuery* rewrite_query);
00385 
00390   static bool ScanSplitHtmlRequest(const RequestContextPtr& ctx,
00391                                    const RewriteOptions* options,
00392                                    GoogleString* url);
00393 
00398   RewriteOptions* GetCustomOptions(RequestHeaders* request_headers,
00399                                    RewriteOptions* domain_options,
00400                                    RewriteOptions* query_options);
00401 
00404   GoogleString GetRewriteOptionsSignatureHash(const RewriteOptions* options);
00405 
00414   RewriteDriver* NewRewriteDriver(const RequestContextPtr& request_ctx);
00415 
00418   RewriteDriver* NewRewriteDriverFromPool(
00419       RewriteDriverPool* pool, const RequestContextPtr& request_ctx);
00420 
00434   RewriteDriver* NewUnmanagedRewriteDriver(
00435       RewriteDriverPool* pool, RewriteOptions* options,
00436       const RequestContextPtr& request_ctx);
00437 
00448   RewriteDriver* NewCustomRewriteDriver(
00449       RewriteOptions* custom_options, const RequestContextPtr& request_ctx);
00450 
00463   void ReleaseRewriteDriver(RewriteDriver* rewrite_driver);
00464 
00465   ThreadSystem* thread_system() { return thread_system_; }
00466   UsageDataReporter* usage_data_reporter() { return usage_data_reporter_; }
00467 
00470   void set_shutting_down() {
00471     shutting_down_.set_value(true);
00472   }
00473 
00474   bool shutting_down() const {
00475     return shutting_down_.value();
00476   }
00477 
00487   void ShutDownDrivers();
00488 
00509   void MergeNonCachingResponseHeaders(const ResourcePtr& input,
00510                                       const OutputResourcePtr& output) {
00511     MergeNonCachingResponseHeaders(*input->response_headers(),
00512                                    output->response_headers());
00513   }
00514 
00516   void MergeNonCachingResponseHeaders(const ResponseHeaders& input_headers,
00517                                       ResponseHeaders* output_headers);
00518 
00520   QueuedWorkerPool* html_workers() { return html_workers_; }
00521 
00523   QueuedWorkerPool* rewrite_workers() { return rewrite_workers_; }
00524 
00527   QueuedWorkerPool* low_priority_rewrite_workers() {
00528     return low_priority_rewrite_workers_;
00529   }
00530 
00534   size_t num_active_rewrite_drivers();
00535 
00538   void InitWorkers();
00539 
00542   static bool IsExcludedAttribute(const char* attribute);
00543 
00551   bool response_headers_finalized() const {
00552     return response_headers_finalized_;
00553   }
00554   void set_response_headers_finalized(bool x) {
00555     response_headers_finalized_ = x;
00556   }
00557 
00561   RewriteDriverPool* standard_rewrite_driver_pool() {
00562     return available_rewrite_drivers_.get();
00563   }
00564 
00566   const GoogleString& hostname() const {
00567     return hostname_;
00568   }
00569   void set_hostname(const GoogleString& x) {
00570     hostname_ = x;
00571   }
00572 
00575   void AddOriginalContentLengthHeader(const ResourceVector& inputs,
00576                                       ResponseHeaders* headers);
00577 
00579   virtual RewriteDriverPool* SelectDriverPool(bool using_spdy);
00580 
00583   virtual void ApplySessionFetchers(const RequestContextPtr& req,
00584                                     RewriteDriver* driver);
00585 
00596   virtual bool ProxiesHtml() const = 0;
00597 
00599   RequestProperties* NewRequestProperties();
00600 
00603   void DeleteCacheOnDestruction(CacheInterface* cache);
00604 
00605   void set_cache_property_store(CachePropertyStore* p);
00606 
00609   void set_decoding_driver(RewriteDriver* rd) { decoding_driver_ = rd; }
00610 
00612   virtual PropertyStore* CreatePropertyStore(CacheInterface* cache_backend);
00613 
00617   const PropertyCache::Cohort* AddCohort(
00618       const GoogleString& cohort_name,
00619       PropertyCache* pcache);
00620 
00625   const PropertyCache::Cohort* AddCohortWithCache(
00626       const GoogleString& cohort_name,
00627       CacheInterface* cache,
00628       PropertyCache* pcache);
00629 
00632   const CacheInterface* pcache_cache_backend();
00633 
00634   const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns() const {
00635     return js_tokenizer_patterns_;
00636   }
00637 
00640   void ShowCacheHandler(StringPiece url, AsyncFetch* fetch,
00641                         RewriteOptions* options);
00642 
00646   GoogleString ShowCacheForm(const char* user_agent) const;
00647 
00648  protected:
00651   void ManageRewriteDriverPool(RewriteDriverPool* pool) {
00652     additional_driver_pools_.push_back(pool);
00653   }
00654 
00655  private:
00656   friend class ServerContextTest;
00657   typedef std::set<RewriteDriver*> RewriteDriverSet;
00658 
00660   void ReleaseRewriteDriverImpl(RewriteDriver* rewrite_driver);
00661 
00663   ThreadSystem* thread_system_;
00664   RewriteStats* rewrite_stats_;
00665   GoogleString file_prefix_;
00666   FileSystem* file_system_;
00667   UrlNamer* url_namer_;
00668   scoped_ptr<RewriteOptionsManager> rewrite_options_manager_;
00669   UserAgentMatcher* user_agent_matcher_;
00670   Scheduler* scheduler_;
00671   UrlAsyncFetcher* default_system_fetcher_;
00672   UrlAsyncFetcher* default_distributed_fetcher_;
00673   Hasher* hasher_;
00674   scoped_ptr<CriticalImagesFinder> critical_images_finder_;
00675   scoped_ptr<CriticalCssFinder> critical_css_finder_;
00676   scoped_ptr<CriticalSelectorFinder> critical_selector_finder_;
00677   scoped_ptr<CacheHtmlInfoFinder> cache_html_info_finder_;
00678   scoped_ptr<FlushEarlyInfoFinder> flush_early_info_finder_;
00679   scoped_ptr<CriticalLineInfoFinder> critical_line_info_finder_;
00680 
00684   MD5Hasher lock_hasher_; 
00685 
00688   MD5Hasher contents_hasher_;
00689 
00690   Statistics* statistics_;
00691 
00692   Timer* timer_;
00693   scoped_ptr<HTTPCache> http_cache_;
00694   scoped_ptr<PropertyCache> page_property_cache_;
00695   CacheInterface* filesystem_metadata_cache_;
00696   CacheInterface* metadata_cache_;
00697 
00698   bool store_outputs_in_file_system_;
00699   bool response_headers_finalized_;
00700   bool enable_property_cache_;
00701 
00702   NamedLockManager* lock_manager_;
00703   MessageHandler* message_handler_;
00704 
00705   const PropertyCache::Cohort* dom_cohort_;
00706   const PropertyCache::Cohort* blink_cohort_;
00707   const PropertyCache::Cohort* beacon_cohort_;
00708   const PropertyCache::Cohort* fix_reflow_cohort_;
00709 
00715   scoped_ptr<RewriteDriverPool> available_rewrite_drivers_;
00716 
00718   std::vector<RewriteDriverPool*> additional_driver_pools_;
00719 
00725   RewriteDriverSet active_rewrite_drivers_;
00726 
00734   bool trying_to_cleanup_rewrite_drivers_;
00735   RewriteDriverSet deferred_release_rewrite_drivers_;
00736   bool shutdown_drivers_called_;
00737 
00740   RewriteDriverFactory* factory_;
00741 
00742   scoped_ptr<AbstractMutex> rewrite_drivers_mutex_;
00743 
00746   scoped_ptr<RewriteOptions> base_class_options_;
00747 
00751   RewriteDriver* decoding_driver_;
00752 
00753   QueuedWorkerPool* html_workers_; 
00754   QueuedWorkerPool* rewrite_workers_; 
00755   QueuedWorkerPool* low_priority_rewrite_workers_; 
00756 
00757   AtomicBool shutting_down_;
00758 
00760   StaticAssetManager* static_asset_manager_;
00761 
00764   scoped_ptr<ThreadSynchronizer> thread_synchronizer_;
00765 
00767   scoped_ptr<ExperimentMatcher> experiment_matcher_;
00768 
00769   UsageDataReporter* usage_data_reporter_;
00770 
00772   GoogleString hostname_;
00773 
00776   SimpleRandom simple_random_;
00778   const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_;
00779 
00780   scoped_ptr<CachePropertyStore> cache_property_store_;
00781 
00782   DISALLOW_COPY_AND_ASSIGN(ServerContext);
00783 };
00784 
00785 }  
00786 
00787 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_SERVER_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines