Page Speed Optimization Libraries
1.7.30.2
|
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 00041 namespace net_instaweb { 00042 00043 class CachedResult; 00044 struct ContentType; 00045 class InputInfo; 00046 class MessageHandler; 00047 class Resource; 00048 class ServerContext; 00049 00050 typedef RefCountedPtr<Resource> ResourcePtr; 00051 typedef std::vector<ResourcePtr> ResourceVector; 00052 00053 class Resource : public RefCounted<Resource> { 00054 public: 00055 class AsyncCallback; 00056 00057 enum HashHint { 00058 kOmitInputHash, 00059 kIncludeInputHash 00060 }; 00061 00064 enum NotCacheablePolicy { 00065 kLoadEvenIfNotCacheable, 00066 kReportFailureIfNotCacheable, 00067 }; 00068 00070 enum FetchResponseStatus { 00071 kFetchStatusNotSet, 00072 kFetchStatusOK, 00073 kFetchStatusUncacheable, 00074 kFetchStatus4xxError, 00075 kFetchStatusDropped, 00076 kFetchStatusOther, 00077 }; 00078 00079 Resource(ServerContext* server_context, const ContentType* type); 00080 00082 ServerContext* server_context() const { return server_context_; } 00083 00086 virtual bool IsValidAndCacheable() const; 00087 00091 bool is_authorized_domain() { return is_authorized_domain_; } 00092 void set_is_authorized_domain(bool is_authorized) { 00093 is_authorized_domain_ = is_authorized; 00094 } 00095 00100 bool IsSafeToRewrite(bool rewrite_uncacheable) const; 00101 00104 bool loaded() const { return response_headers_.status_code() != 0; } 00105 bool HttpStatusOk() const { 00106 return (response_headers_.status_code() == HttpStatus::kOK); 00107 } 00108 00117 void LoadAsync(NotCacheablePolicy not_cacheable_policy, 00118 const RequestContextPtr& request_context, 00119 AsyncCallback* callback); 00120 00126 virtual void RefreshIfImminentlyExpiring(); 00127 00133 GoogleString ContentsHash() const; 00134 00137 void AddInputInfoToPartition(HashHint suggest_include_content_hash, 00138 int index, CachedResult* partition); 00139 00150 virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash, 00151 InputInfo* input); 00152 00153 void FillInPartitionInputInfoFromResponseHeaders( 00154 const ResponseHeaders& headers, 00155 InputInfo* input); 00156 00160 int64 CacheExpirationTimeMs() const; 00161 00162 StringPiece contents() const { 00163 StringPiece val; 00164 bool got_contents = value_.ExtractContents(&val); 00165 CHECK(got_contents) << "Resource contents read before loading: " << url(); 00166 return val; 00167 } 00168 ResponseHeaders* response_headers() { return &response_headers_; } 00169 const ResponseHeaders* response_headers() const { return &response_headers_; } 00170 const ContentType* type() const { return type_; } 00171 virtual void SetType(const ContentType* type); 00172 00174 StringPiece charset() const { return charset_; } 00175 void set_charset(StringPiece c) { c.CopyToString(&charset_); } 00176 00178 virtual GoogleString url() const = 0; 00179 00182 virtual GoogleString cache_key() const { 00183 return url(); 00184 } 00185 00188 void DetermineContentType(); 00189 00193 class AsyncCallback { 00194 public: 00195 explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {} 00196 00197 virtual ~AsyncCallback(); 00198 virtual void Done(bool lock_failure, bool resource_ok) = 0; 00199 00200 const ResourcePtr& resource() { return resource_; } 00201 00202 private: 00203 ResourcePtr resource_; 00204 DISALLOW_COPY_AND_ASSIGN(AsyncCallback); 00205 }; 00206 00209 class FreshenCallback : public AsyncCallback { 00210 public: 00211 explicit FreshenCallback(const ResourcePtr& resource) 00212 : AsyncCallback(resource) {} 00213 00214 virtual ~FreshenCallback(); 00217 virtual InputInfo* input_info() { return NULL; } 00218 00221 virtual void Done(bool lock_failure, bool resource_ok) { 00222 delete this; 00223 } 00224 00225 private: 00226 DISALLOW_COPY_AND_ASSIGN(FreshenCallback); 00227 }; 00228 00234 bool Link(HTTPValue* source, MessageHandler* handler); 00235 00239 virtual void Freshen(FreshenCallback* callback, MessageHandler* handler); 00240 00242 void LinkFallbackValue(HTTPValue* value); 00243 00244 void set_is_background_fetch(bool x) { is_background_fetch_ = x; } 00245 bool is_background_fetch() const { return is_background_fetch_; } 00246 00247 FetchResponseStatus fetch_response_status() { 00248 return fetch_response_status_; 00249 } 00250 00251 void set_fetch_response_status(FetchResponseStatus x) { 00252 fetch_response_status_ = x; 00253 } 00254 00258 virtual bool UseHttpCache() const = 0; 00259 00260 protected: 00261 virtual ~Resource(); 00262 REFCOUNT_FRIEND_DECLARATION(Resource); 00263 friend class ServerContext; 00264 friend class ReadAsyncHttpCacheCallback; 00265 friend class RewriteDriver; 00266 friend class UrlReadAsyncFetchCallback; 00267 00277 virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, 00278 const RequestContextPtr& request_context, 00279 AsyncCallback* callback) = 0; 00280 00281 void set_enable_cache_purge(bool x) { enable_cache_purge_ = x; } 00282 void set_proactive_resource_freshening(bool x) { 00283 proactive_resource_freshening_ = x; 00284 } 00285 00286 void set_disable_rewrite_on_no_transform(bool x) { 00287 disable_rewrite_on_no_transform_ = x; 00288 } 00289 ServerContext* server_context_; 00290 00291 const ContentType* type_; 00292 GoogleString charset_; 00293 HTTPValue value_; 00294 ResponseHeaders response_headers_; 00295 00299 HTTPValue fallback_value_; 00300 00301 private: 00303 FetchResponseStatus fetch_response_status_; 00304 00310 bool is_background_fetch_; 00311 bool enable_cache_purge_; 00312 bool proactive_resource_freshening_; 00313 bool disable_rewrite_on_no_transform_; 00314 bool is_authorized_domain_; 00315 00316 DISALLOW_COPY_AND_ASSIGN(Resource); 00317 }; 00318 00319 } 00320 00321 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_