Page Speed Optimization Libraries  1.7.30.4
net/instaweb/rewriter/public/resource.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 
00025 
00026 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
00027 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
00028 
00029 #include <vector>
00030 
00031 #include "base/logging.h"
00032 #include "net/instaweb/http/public/http_value.h"
00033 #include "net/instaweb/http/public/meta_data.h"
00034 #include "net/instaweb/http/public/request_context.h"
00035 #include "net/instaweb/http/public/response_headers.h"
00036 #include "net/instaweb/util/public/basictypes.h"
00037 #include "net/instaweb/util/public/ref_counted_ptr.h"
00038 #include "net/instaweb/util/public/string.h"
00039 #include "net/instaweb/util/public/string_util.h"
00040 #include "pagespeed/kernel/base/callback.h"
00041 
00042 namespace net_instaweb {
00043 
00044 class CachedResult;
00045 struct ContentType;
00046 class GoogleUrl;
00047 class InputInfo;
00048 class MessageHandler;
00049 class Resource;
00050 class ServerContext;
00051 
00052 typedef RefCountedPtr<Resource> ResourcePtr;
00053 typedef std::vector<ResourcePtr> ResourceVector;
00054 
00055 class Resource : public RefCounted<Resource> {
00056  public:
00057   class AsyncCallback;
00058 
00059   enum HashHint {
00060     kOmitInputHash,
00061     kIncludeInputHash
00062   };
00063 
00066   enum NotCacheablePolicy {
00067     kLoadEvenIfNotCacheable,
00068     kReportFailureIfNotCacheable,
00069   };
00070 
00072   enum FetchResponseStatus {
00073     kFetchStatusNotSet,
00074     kFetchStatusOK,
00075     kFetchStatusUncacheable,
00076     kFetchStatus4xxError,
00077     kFetchStatusDropped,
00078     kFetchStatusOther,
00079   };
00080 
00081   Resource(ServerContext* server_context, const ContentType* type);
00082 
00084   ServerContext* server_context() const { return server_context_; }
00085 
00088   virtual bool IsValidAndCacheable() const;
00089 
00093   bool is_authorized_domain() { return is_authorized_domain_; }
00094   void set_is_authorized_domain(bool is_authorized) {
00095     is_authorized_domain_ = is_authorized;
00096   }
00097 
00102   bool IsSafeToRewrite(bool rewrite_uncacheable) const;
00103 
00106   bool loaded() const { return response_headers_.status_code() != 0; }
00107   bool HttpStatusOk() const {
00108     return (response_headers_.status_code() == HttpStatus::kOK);
00109   }
00110 
00119   void LoadAsync(NotCacheablePolicy not_cacheable_policy,
00120                  const RequestContextPtr& request_context,
00121                  AsyncCallback* callback);
00122 
00128   virtual void RefreshIfImminentlyExpiring();
00129 
00135   GoogleString ContentsHash() const;
00136 
00139   void AddInputInfoToPartition(HashHint suggest_include_content_hash,
00140                                int index, CachedResult* partition);
00141 
00152   virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash,
00153                                         InputInfo* input);
00154 
00155   void FillInPartitionInputInfoFromResponseHeaders(
00156       const ResponseHeaders& headers,
00157       InputInfo* input);
00158 
00162   int64 CacheExpirationTimeMs() const;
00163 
00164   StringPiece contents() const {
00165     StringPiece val;
00166     bool got_contents = value_.ExtractContents(&val);
00167     CHECK(got_contents) << "Resource contents read before loading: " << url();
00168     return val;
00169   }
00170   ResponseHeaders* response_headers() { return &response_headers_; }
00171   const ResponseHeaders* response_headers() const { return &response_headers_; }
00172   const ContentType* type() const { return type_; }
00173   virtual void SetType(const ContentType* type);
00174 
00176   StringPiece charset() const { return charset_; }
00177   void set_charset(StringPiece c) { c.CopyToString(&charset_); }
00178 
00180   virtual GoogleString url() const = 0;
00181 
00184   virtual GoogleString cache_key() const {
00185     return url();
00186   }
00187 
00190   void DetermineContentType();
00191 
00195   class AsyncCallback {
00196    public:
00197     explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {}
00198 
00199     virtual ~AsyncCallback();
00200     virtual void Done(bool lock_failure, bool resource_ok) = 0;
00201 
00202     const ResourcePtr& resource() { return resource_; }
00203 
00204    private:
00205     ResourcePtr resource_;
00206     DISALLOW_COPY_AND_ASSIGN(AsyncCallback);
00207   };
00208 
00211   class FreshenCallback : public AsyncCallback {
00212    public:
00213     explicit FreshenCallback(const ResourcePtr& resource)
00214         : AsyncCallback(resource) {}
00215 
00216     virtual ~FreshenCallback();
00219     virtual InputInfo* input_info() { return NULL; }
00220 
00223     virtual void Done(bool lock_failure, bool resource_ok) {
00224       delete this;
00225     }
00226 
00227    private:
00228     DISALLOW_COPY_AND_ASSIGN(FreshenCallback);
00229   };
00230 
00236   bool Link(HTTPValue* source, MessageHandler* handler);
00237 
00241   virtual void Freshen(FreshenCallback* callback, MessageHandler* handler);
00242 
00244   void LinkFallbackValue(HTTPValue* value);
00245 
00246   void set_is_background_fetch(bool x) { is_background_fetch_ = x; }
00247   bool is_background_fetch() const { return is_background_fetch_; }
00248 
00249   FetchResponseStatus fetch_response_status() {
00250     return fetch_response_status_;
00251   }
00252 
00253   void set_fetch_response_status(FetchResponseStatus x) {
00254     fetch_response_status_ = x;
00255   }
00256 
00260   virtual bool UseHttpCache() const = 0;
00261 
00262  protected:
00263   virtual ~Resource();
00264   REFCOUNT_FRIEND_DECLARATION(Resource);
00265   friend class ServerContext;
00266   friend class ReadAsyncHttpCacheCallback; 
00267   friend class RewriteDriver; 
00268   friend class UrlReadAsyncFetchCallback;
00269 
00279   virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
00280                                const RequestContextPtr& request_context,
00281                                AsyncCallback* callback) = 0;
00282 
00283   void set_enable_cache_purge(bool x) { enable_cache_purge_ = x; }
00284   ResponseHeaders::VaryOption respect_vary() const { return respect_vary_; }
00285   void set_respect_vary(ResponseHeaders::VaryOption x) { respect_vary_ = x; }
00286   void set_proactive_resource_freshening(bool x) {
00287     proactive_resource_freshening_ = x;
00288   }
00289 
00290   void set_disable_rewrite_on_no_transform(bool x) {
00291     disable_rewrite_on_no_transform_ = x;
00292   }
00293   ServerContext* server_context_;
00294 
00295   const ContentType* type_;
00296   GoogleString charset_;
00297   HTTPValue value_; 
00298   ResponseHeaders response_headers_;
00299 
00303   HTTPValue fallback_value_;
00304 
00305  private:
00307   FetchResponseStatus fetch_response_status_;
00308 
00314   bool is_background_fetch_;
00315   bool enable_cache_purge_;
00316   bool proactive_resource_freshening_;
00317   bool disable_rewrite_on_no_transform_;
00318   bool is_authorized_domain_;
00319   ResponseHeaders::VaryOption respect_vary_;
00320 
00321   DISALLOW_COPY_AND_ASSIGN(Resource);
00322 };
00323 
00328 typedef Callback2<const GoogleUrl&, bool*> ResourceUrlClaimant;
00329 
00330 }  
00331 
00332 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines