Page Speed Optimization Libraries  1.13.35.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"
34 #include "net/instaweb/http/public/request_context.h"
43 
44 namespace net_instaweb {
45 
46 class CachedResult;
47 class GoogleUrl;
48 class InputInfo;
49 class MessageHandler;
50 class Resource;
51 class RewriteDriver;
52 class ServerContext;
53 
54 typedef RefCountedPtr<Resource> ResourcePtr;
55 typedef std::vector<ResourcePtr> ResourceVector;
56 
57 class Resource : public RefCounted<Resource> {
58  public:
59  class AsyncCallback;
60 
61  enum HashHint {
62  kOmitInputHash,
63  kIncludeInputHash
64  };
65 
69  kLoadEvenIfNotCacheable,
70  kReportFailureIfNotCacheable,
71  };
72 
73 
74  Resource(const RewriteDriver* driver, const ContentType* type);
75 
77  ServerContext* server_context() const { return server_context_; }
78 
81  virtual bool IsValidAndCacheable() const;
82 
86  bool is_authorized_domain() { return is_authorized_domain_; }
87  void set_is_authorized_domain(bool is_authorized) {
88  is_authorized_domain_ = is_authorized;
89  }
90 
96  bool IsSafeToRewrite(bool rewrite_uncacheable, GoogleString* reason) const;
97  bool IsSafeToRewrite(bool rewrite_uncacheable) const {
99  GoogleString reason_ignored;
100  return IsSafeToRewrite(rewrite_uncacheable, &reason_ignored);
101  }
102 
105  bool loaded() const { return response_headers_.status_code() != 0; }
106  bool HttpStatusOk() const {
107  return (response_headers_.status_code() == HttpStatus::kOK);
108  }
109 
118  void LoadAsync(NotCacheablePolicy not_cacheable_policy,
119  const RequestContextPtr& request_context,
120  AsyncCallback* callback);
121 
127  virtual void RefreshIfImminentlyExpiring();
128 
134  GoogleString ContentsHash() const;
135 
138  void AddInputInfoToPartition(HashHint suggest_include_content_hash,
139  int index, CachedResult* partition);
140 
151  virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash,
152  InputInfo* input);
153 
154  void FillInPartitionInputInfoFromResponseHeaders(
155  const ResponseHeaders& headers,
156  InputInfo* input);
157 
161  int64 CacheExpirationTimeMs() const;
162 
166  StringPiece ExtractUncompressedContents() const;
167 
171  size_t UncompressedContentsSize() const {
172  StringPiece val = ExtractUncompressedContents();
173  return val.length();
174  }
175 
176  StringPiece raw_contents() const {
177  StringPiece val;
178  bool got_contents = value_.ExtractContents(&val);
179  CHECK(got_contents) << "Resource contents read before loading: "
180  << UrlForDebug();
181  return val;
182  }
183 
184  ResponseHeaders* response_headers() { return &response_headers_; }
185  const ResponseHeaders* response_headers() const { return &response_headers_; }
186  const ContentType* type() const { return type_; }
187  virtual void SetType(const ContentType* type);
188  bool IsContentsEmpty() const {
189  return raw_contents().empty();
190  }
191 
193  StringPiece charset() const { return charset_; }
194  void set_charset(StringPiece c) { c.CopyToString(&charset_); }
195 
197  virtual GoogleString url() const = 0;
200  virtual bool has_url() const { return true; }
202  virtual GoogleString UrlForDebug() const { return url(); }
203 
206  virtual GoogleString cache_key() const {
207  return url();
208  }
209 
212  void DetermineContentType();
213 
218  public:
219  explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {}
220 
221  virtual ~AsyncCallback();
222  virtual void Done(bool lock_failure, bool resource_ok) = 0;
223 
224  const ResourcePtr& resource() { return resource_; }
225 
226  private:
227  ResourcePtr resource_;
228 
229  };
230 
234  public:
235  explicit FreshenCallback(const ResourcePtr& resource)
236  : AsyncCallback(resource) {}
237 
238  virtual ~FreshenCallback();
241  virtual InputInfo* input_info() { return NULL; }
242 
245  virtual void Done(bool lock_failure, bool resource_ok) {
246  delete this;
247  }
248 
249  private:
250 
251  };
252 
258  bool Link(HTTPValue* source, MessageHandler* handler);
259 
263  virtual void Freshen(FreshenCallback* callback, MessageHandler* handler);
264 
266  void LinkFallbackValue(HTTPValue* value);
267 
268  void set_is_background_fetch(bool x) { is_background_fetch_ = x; }
269  bool is_background_fetch() const { return is_background_fetch_; }
270 
271  FetchResponseStatus fetch_response_status() {
272  return fetch_response_status_;
273  }
274 
275  void set_fetch_response_status(FetchResponseStatus x) {
276  fetch_response_status_ = x;
277  }
278 
282  virtual bool UseHttpCache() const = 0;
283 
284  protected:
285  virtual ~Resource();
286  REFCOUNT_FRIEND_DECLARATION(Resource);
287  friend class ServerContext;
289  friend class RewriteDriver;
290  friend class UrlReadAsyncFetchCallback;
291 
301  virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
302  const RequestContextPtr& request_context,
303  AsyncCallback* callback) = 0;
304 
305  void set_enable_cache_purge(bool x) { enable_cache_purge_ = x; }
306  ResponseHeaders::VaryOption respect_vary() const { return respect_vary_; }
307  void set_respect_vary(ResponseHeaders::VaryOption x) { respect_vary_ = x; }
308  void set_proactive_resource_freshening(bool x) {
309  proactive_resource_freshening_ = x;
310  }
311 
312  void set_disable_rewrite_on_no_transform(bool x) {
313  disable_rewrite_on_no_transform_ = x;
314  }
315  ServerContext* server_context_;
316 
317  const ContentType* type_;
318  GoogleString charset_;
320  ResponseHeaders response_headers_;
321 
326 
327  private:
330  Resource();
331  friend class DummyResource;
332 
334  FetchResponseStatus fetch_response_status_;
335 
341  bool is_background_fetch_;
342  bool enable_cache_purge_;
343  bool proactive_resource_freshening_;
344  bool disable_rewrite_on_no_transform_;
345  bool is_authorized_domain_;
346  ResponseHeaders::VaryOption respect_vary_;
347  mutable GoogleString extracted_contents_;
348  mutable bool extracted_;
349 
350 
351 };
352 
358 
359 }
360 
361 #endif
bool loaded() const
Definition: resource.h:105
Definition: http_value.h:38
ServerContext * server_context() const
Common methods across all deriviations.
Definition: resource.h:77
Definition: callback.h:167
friend class ReadAsyncHttpCacheCallback
uses LoadAndCallback
Definition: resource.h:288
bool is_authorized_domain()
Definition: resource.h:86
Definition: resource.h:217
virtual bool IsValidAndCacheable() const
Definition: ref_counted_ptr.h:41
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:233
virtual InputInfo * input_info()
Definition: resource.h:241
virtual bool has_url() const
Definition: resource.h:200
Read/write API for HTTP response headers.
Definition: response_headers.h:37
bool IsSafeToRewrite(bool rewrite_uncacheable) const
Definition: resource.h:97
Definition: resource.h:57
int64 CacheExpirationTimeMs() const
NotCacheablePolicy
Definition: resource.h:68
virtual GoogleString url() const =0
Gets the absolute URL of the resource.
Callback2< const GoogleUrl &, bool * > ResourceUrlClaimant
Definition: resource.h:357
void AddInputInfoToPartition(HashHint suggest_include_content_hash, int index, CachedResult *partition)
bool Link(HTTPValue *source, MessageHandler *handler)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
HTTPValue value_
contains contents and meta-data
Definition: resource.h:319
void LinkFallbackValue(HTTPValue *value)
Links the stale fallback value that can be used in case a fetch fails.
virtual GoogleString UrlForDebug() const
Override if resource does not have a URL.
Definition: resource.h:202
bool ExtractContents(StringPiece *str) const
Definition: rewrite_driver.h:100
Definition: server_context.h:99
StringPiece ExtractUncompressedContents() const
Definition: content_type.h:31
virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, const RequestContextPtr &request_context, AsyncCallback *callback)=0
Definition: message_handler.h:39
FetchResponseStatus
Definition: http_cache_failure.h:33
virtual void RefreshIfImminentlyExpiring()
StringPiece charset() const
Note: this is empty if the header is not specified.
Definition: resource.h:193
GoogleString ContentsHash() const
virtual void Done(bool lock_failure, bool resource_ok)
Definition: resource.h:245
size_t UncompressedContentsSize() const
Definition: resource.h:171
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:206
HTTPValue fallback_value_
Definition: resource.h:325