Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
resource.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 
26 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
27 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
28 
29 #include <vector>
30 
31 #include "base/logging.h"
33 #include "net/instaweb/http/public/meta_data.h"
35 #include "net/instaweb/http/public/response_headers.h"
37 #include "net/instaweb/util/public/ref_counted_ptr.h"
40 #include "pagespeed/kernel/base/callback.h"
41 
42 namespace net_instaweb {
43 
44 class CachedResult;
45 struct ContentType;
46 class GoogleUrl;
47 class InputInfo;
48 class MessageHandler;
49 class Resource;
50 class RewriteDriver;
51 class ServerContext;
52 
53 typedef RefCountedPtr<Resource> ResourcePtr;
54 typedef std::vector<ResourcePtr> ResourceVector;
55 
56 class Resource : public RefCounted<Resource> {
57  public:
58  class AsyncCallback;
59 
60  enum HashHint {
61  kOmitInputHash,
62  kIncludeInputHash
63  };
64 
68  kLoadEvenIfNotCacheable,
69  kReportFailureIfNotCacheable,
70  };
71 
74  kFetchStatusNotSet,
75  kFetchStatusOK,
76  kFetchStatusUncacheable,
77  kFetchStatus4xxError,
78  kFetchStatusDropped,
79  kFetchStatusOther,
80  };
81 
82  Resource(const RewriteDriver* driver, const ContentType* type);
83 
85  ServerContext* server_context() const { return server_context_; }
86 
89  virtual bool IsValidAndCacheable() const;
90 
94  bool is_authorized_domain() { return is_authorized_domain_; }
95  void set_is_authorized_domain(bool is_authorized) {
96  is_authorized_domain_ = is_authorized;
97  }
98 
104  bool IsSafeToRewrite(bool rewrite_uncacheable, GoogleString* reason) const;
105  bool IsSafeToRewrite(bool rewrite_uncacheable) const {
107  GoogleString reason_ignored;
108  return IsSafeToRewrite(rewrite_uncacheable, &reason_ignored);
109  }
110 
113  bool loaded() const { return response_headers_.status_code() != 0; }
114  bool HttpStatusOk() const {
115  return (response_headers_.status_code() == HttpStatus::kOK);
116  }
117 
126  void LoadAsync(NotCacheablePolicy not_cacheable_policy,
127  const RequestContextPtr& request_context,
128  AsyncCallback* callback);
129 
135  virtual void RefreshIfImminentlyExpiring();
136 
142  GoogleString ContentsHash() const;
143 
146  void AddInputInfoToPartition(HashHint suggest_include_content_hash,
147  int index, CachedResult* partition);
148 
159  virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash,
160  InputInfo* input);
161 
162  void FillInPartitionInputInfoFromResponseHeaders(
163  const ResponseHeaders& headers,
164  InputInfo* input);
165 
169  int64 CacheExpirationTimeMs() const;
170 
171  StringPiece contents() const {
172  StringPiece val;
173  bool got_contents = value_.ExtractContents(&val);
174  CHECK(got_contents) << "Resource contents read before loading: " << url();
175  return val;
176  }
177  ResponseHeaders* response_headers() { return &response_headers_; }
178  const ResponseHeaders* response_headers() const { return &response_headers_; }
179  const ContentType* type() const { return type_; }
180  virtual void SetType(const ContentType* type);
181 
183  StringPiece charset() const { return charset_; }
184  void set_charset(StringPiece c) { c.CopyToString(&charset_); }
185 
187  virtual GoogleString url() const = 0;
188 
191  virtual GoogleString cache_key() const {
192  return url();
193  }
194 
197  void DetermineContentType();
198 
203  public:
204  explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {}
205 
206  virtual ~AsyncCallback();
207  virtual void Done(bool lock_failure, bool resource_ok) = 0;
208 
209  const ResourcePtr& resource() { return resource_; }
210 
211  private:
212  ResourcePtr resource_;
213  DISALLOW_COPY_AND_ASSIGN(AsyncCallback);
214  };
215 
219  public:
220  explicit FreshenCallback(const ResourcePtr& resource)
221  : AsyncCallback(resource) {}
222 
223  virtual ~FreshenCallback();
226  virtual InputInfo* input_info() { return NULL; }
227 
230  virtual void Done(bool lock_failure, bool resource_ok) {
231  delete this;
232  }
233 
234  private:
235  DISALLOW_COPY_AND_ASSIGN(FreshenCallback);
236  };
237 
243  bool Link(HTTPValue* source, MessageHandler* handler);
244 
248  virtual void Freshen(FreshenCallback* callback, MessageHandler* handler);
249 
251  void LinkFallbackValue(HTTPValue* value);
252 
253  void set_is_background_fetch(bool x) { is_background_fetch_ = x; }
254  bool is_background_fetch() const { return is_background_fetch_; }
255 
256  FetchResponseStatus fetch_response_status() {
257  return fetch_response_status_;
258  }
259 
260  void set_fetch_response_status(FetchResponseStatus x) {
261  fetch_response_status_ = x;
262  }
263 
267  virtual bool UseHttpCache() const = 0;
268 
269  protected:
270  virtual ~Resource();
271  REFCOUNT_FRIEND_DECLARATION(Resource);
272  friend class ServerContext;
274  friend class RewriteDriver;
275  friend class UrlReadAsyncFetchCallback;
276 
286  virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
287  const RequestContextPtr& request_context,
288  AsyncCallback* callback) = 0;
289 
290  void set_enable_cache_purge(bool x) { enable_cache_purge_ = x; }
291  ResponseHeaders::VaryOption respect_vary() const { return respect_vary_; }
292  void set_respect_vary(ResponseHeaders::VaryOption x) { respect_vary_ = x; }
293  void set_proactive_resource_freshening(bool x) {
294  proactive_resource_freshening_ = x;
295  }
296 
297  void set_disable_rewrite_on_no_transform(bool x) {
298  disable_rewrite_on_no_transform_ = x;
299  }
300  ServerContext* server_context_;
301 
302  const ContentType* type_;
303  GoogleString charset_;
305  ResponseHeaders response_headers_;
306 
311 
312  private:
315  Resource();
316  friend class DummyResource;
317 
319  FetchResponseStatus fetch_response_status_;
320 
326  bool is_background_fetch_;
327  bool enable_cache_purge_;
328  bool proactive_resource_freshening_;
329  bool disable_rewrite_on_no_transform_;
330  bool is_authorized_domain_;
331  ResponseHeaders::VaryOption respect_vary_;
332 
333  DISALLOW_COPY_AND_ASSIGN(Resource);
334 };
335 
340 typedef Callback2<const GoogleUrl&, bool*> ResourceUrlClaimant;
341 
342 }
343 
344 #endif
bool loaded() const
Definition: resource.h:113
Definition: http_value.h:36
ServerContext * server_context() const
Common methods across all deriviations.
Definition: resource.h:85
friend class ReadAsyncHttpCacheCallback
uses LoadAndCallback
Definition: resource.h:273
bool is_authorized_domain()
Definition: resource.h:94
Definition: resource.h:202
virtual bool IsValidAndCacheable() const
void LoadAsync(NotCacheablePolicy not_cacheable_policy, const RequestContextPtr &request_context, AsyncCallback *callback)
virtual bool UseHttpCache() const =0
bool IsSafeToRewrite(bool rewrite_uncacheable, GoogleString *reason) const
Definition: resource.h:218
virtual InputInfo * input_info()
Definition: resource.h:226
bool IsSafeToRewrite(bool rewrite_uncacheable) const
Definition: resource.h:105
Definition: resource.h:56
int64 CacheExpirationTimeMs() const
NotCacheablePolicy
Definition: resource.h:67
virtual GoogleString url() const =0
Gets the absolute URL of the resource.
Callback2< const GoogleUrl &, bool * > ResourceUrlClaimant
Definition: resource.h:340
void AddInputInfoToPartition(HashHint suggest_include_content_hash, int index, CachedResult *partition)
bool Link(HTTPValue *source, MessageHandler *handler)
HTTPValue value_
contains contents and meta-data
Definition: resource.h:304
void LinkFallbackValue(HTTPValue *value)
Links the stale fallback value that can be used in case a fetch fails.
bool ExtractContents(StringPiece *str) const
Definition: rewrite_driver.h:98
Definition: server_context.h:100
virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, const RequestContextPtr &request_context, AsyncCallback *callback)=0
FetchResponseStatus
This enumerates different states of the fetched response.
Definition: resource.h:73
virtual void RefreshIfImminentlyExpiring()
StringPiece charset() const
Note: this is empty if the header is not specified.
Definition: resource.h:183
GoogleString ContentsHash() const
virtual void Done(bool lock_failure, bool resource_ok)
Definition: resource.h:230
virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash, InputInfo *input)
virtual void Freshen(FreshenCallback *callback, MessageHandler *handler)
virtual GoogleString cache_key() const
Definition: resource.h:191
HTTPValue fallback_value_
Definition: resource.h:310