Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
proxy_fetch.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 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 
22 
23 #ifndef PAGESPEED_AUTOMATIC_PROXY_FETCH_H_
24 #define PAGESPEED_AUTOMATIC_PROXY_FETCH_H_
25 
26 #include <map>
27 #include <set>
28 #include <vector>
29 
31 #include "net/instaweb/http/public/request_context.h"
32 #include "net/instaweb/util/public/fallback_property_page.h"
33 #include "net/instaweb/util/public/property_cache.h"
44 #include "pagespeed/kernel/http/user_agent_matcher.h"
46 
47 namespace net_instaweb {
48 
49 class CacheUrlAsyncFetcher;
50 class GoogleUrl;
51 class MessageHandler;
52 class ProxyFetch;
53 class ProxyFetchPropertyCallbackCollector;
54 class QueuedAlarm;
55 class ServerContext;
56 class RewriteDriver;
57 class RewriteOptions;
58 class Timer;
59 
63  public:
64  explicit ProxyFetchFactory(ServerContext* server_context);
66 
69  void StartNewProxyFetch(
70  const GoogleString& url,
71  AsyncFetch* async_fetch,
72  RewriteDriver* driver,
73  ProxyFetchPropertyCallbackCollector* property_callback,
74  AsyncFetch* original_content_fetch);
75 
86  const GoogleString& url,
87  AsyncFetch* async_fetch,
88  RewriteDriver* driver,
89  ProxyFetchPropertyCallbackCollector* property_callback,
90  AsyncFetch* original_content_fetch);
91 
95  bool is_resource_fetch,
96  const GoogleUrl& request_url,
97  ServerContext* server_context,
98  RewriteOptions* options,
99  AsyncFetch* async_fetch);
100 
101  MessageHandler* message_handler() const { return handler_; }
102 
103  private:
104  friend class ProxyFetch;
105 
110  void RegisterNewFetch(ProxyFetch* proxy_fetch);
111  void RegisterFinishedFetch(ProxyFetch* proxy_fetch);
112 
113  ServerContext* server_context_;
114  Timer* timer_;
115  MessageHandler* handler_;
116 
117  scoped_ptr<AbstractMutex> outstanding_proxy_fetches_mutex_;
118  std::set<ProxyFetch*> outstanding_proxy_fetches_;
119 
120 
121 };
122 
132  public:
134  PropertyCache* property_cache,
135  const StringPiece& url,
136  const StringPiece& options_signature_hash,
137  UserAgentMatcher::DeviceType device_type,
139  AbstractMutex* mutex);
140 
141  PageType page_type() const { return page_type_; }
142 
144  virtual bool IsCacheValid(int64 write_timestamp_ms) const;
145 
146  virtual void Done(bool success);
147 
148  private:
149  PageType page_type_;
150  UserAgentMatcher::DeviceType device_type_;
152  GoogleString url_;
153 
154 };
155 
177  public:
179  const StringPiece& url,
180  const RequestContextPtr& req_ctx,
181  const RewriteOptions* options,
184 
187  void AddCallback(ProxyFetchPropertyCallback* callback);
188 
191  void RequestHeadersComplete();
192 
199  void ConnectProxyFetch(ProxyFetch* proxy_fetch);
200 
207  void Detach(HttpStatus::Code status_code);
208 
211  return fallback_property_page_ == NULL ?
212  NULL : fallback_property_page_->actual_property_page();
213  }
214 
217  return fallback_property_page_.get();
218  }
219 
224 
227  return fallback_property_page_.release();
228  }
229 
232  return origin_property_page_.release();
233  }
234 
245  void AddPostLookupTask(Function* func);
246 
250  bool IsCacheValid(int64 write_timestamp_ms) const;
251 
253  void Done(ProxyFetchPropertyCallback* callback);
254 
255  const RequestContextPtr& request_context() { return request_context_; }
256 
258  UserAgentMatcher::DeviceType device_type() { return device_type_; }
259 
260  private:
261  friend class ProxyFetchPropertyCallbackCollectorTest;
262  void ExecuteDone(ProxyFetchPropertyCallback* callback);
263  void ExecuteAddPostLookupTask(Function* func);
264  void ExecuteConnectProxyFetch(ProxyFetch* proxy_fetch);
265  void ExecuteDetach(HttpStatus::Code status_code);
266  void ExecuteRequestHeadersComplete();
267 
268  void RunPostLookupsAndCleanupIfSafe();
269 
271  void UpdateStatusCodeInPropertyCache();
272 
273  std::set<ProxyFetchPropertyCallback*> pending_callbacks_;
274  std::map<ProxyFetchPropertyCallback::PageType, PropertyPage*>
275  property_pages_;
277  ServerContext* const server_context_;
278  QueuedWorkerPool::Sequence* const sequence_;
279  const GoogleString url_;
280  const RequestContextPtr request_context_;
281  const UserAgentMatcher::DeviceType device_type_;
282  bool is_options_valid_;
283  bool detached_;
286  bool done_;
287  bool request_headers_ok_;
288  ProxyFetch* proxy_fetch_;
289  std::vector<Function*> post_lookup_task_vector_;
290  const RewriteOptions* options_;
291  HttpStatus::Code status_code_;
292  scoped_ptr<FallbackPropertyPage> fallback_property_page_;
293  scoped_ptr<PropertyPage> origin_property_page_;
294 
295 
296 };
297 
318 class ProxyFetch : public SharedAsyncFetch {
319  public:
323  static const char kCollectorDetachFinish[];
324  static const char kCollectorDoneFinish[];
325  static const char kCollectorFinish[];
326  static const char kCollectorDetachStart[];
327  static const char kCollectorRequestHeadersCompleteFinish[];
328 
331  static const char kHeadersSetupRaceAlarmQueued[];
332  static const char kHeadersSetupRaceDone[];
333  static const char kHeadersSetupRaceFlush[];
334  static const char kHeadersSetupRacePrefix[];
335  static const char kHeadersSetupRaceWait[];
336 
343  static const int kTestSignalTimeoutMs = 200;
344 
345  void set_trusted_input(bool trusted_input) { trusted_input_ = trusted_input; }
346 
347  protected:
349  virtual void HandleHeadersComplete();
350  virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
351  virtual bool HandleFlush(MessageHandler* handler);
352  virtual void HandleDone(bool success);
353  virtual bool IsCachedResultValid(const ResponseHeaders& headers);
354 
355  private:
356  friend class ProxyFetchFactory;
358  friend class MockProxyFetch;
359  FRIEND_TEST(ProxyFetchTest, TestInhibitParsing);
360  FRIEND_TEST(ProxyFetchTest, TestFollowFlushes);
361 
364  virtual void PropertyCacheComplete(
366 
369  ProxyFetch(const GoogleString& url,
370  bool cross_domain,
371  ProxyFetchPropertyCallbackCollector* property_cache_callback,
372  AsyncFetch* async_fetch,
373  AsyncFetch* original_content_fetch,
374  RewriteDriver* driver,
375  ServerContext* server_context,
376  Timer* timer,
377  ProxyFetchFactory* factory);
378  virtual ~ProxyFetch();
379 
380  const RewriteOptions* Options();
381 
383  void SetupForHtml();
384 
386  void AddPagespeedHeader();
387 
390  bool StartParse();
391 
393  void StartFetch();
394 
398  void DoFetch(bool prepare_success);
399 
402  void ExecuteQueued();
403 
406  void ScheduleQueueExecutionIfNeeded();
407 
412  void Finish(bool success);
413 
415  void CompleteFinishParse(bool success);
416 
419  void FlushDone();
420 
423 
425  void CancelIdleAlarm();
426 
428  void QueueIdleAlarm();
429 
431  void HandleIdleAlarm();
432 
433  GoogleString url_;
434  ServerContext* server_context_;
435  Timer* timer_;
436 
437  scoped_ptr<CacheUrlAsyncFetcher> cache_fetcher_;
438 
441  bool cross_domain_;
442 
444  bool claims_html_;
445 
448  bool started_parse_;
449 
451  bool parse_text_called_;
452 
454  bool done_called_;
455 
456  HtmlDetector html_detector_;
457 
462  ProxyFetchPropertyCallbackCollector* property_cache_callback_;
463 
467  AsyncFetch* original_content_fetch_;
468 
471  RewriteDriver* driver_;
472 
475  bool queue_run_job_created_;
476 
489  StringStarVector text_queue_;
490  bool network_flush_outstanding_;
491  QueuedWorkerPool::Sequence* sequence_;
492 
495  bool done_outstanding_;
496 
499  bool finishing_;
500 
503  bool done_result_;
504 
508  bool waiting_for_flush_to_finish_;
509 
512  QueuedAlarm* idle_alarm_;
513 
514  ProxyFetchFactory* factory_;
515 
518  bool trusted_input_;
519 
520 
521 };
522 
523 }
524 
525 #endif
class GoogleUrl
Definition: google_url.h:58
Definition: html_detector.h:54
Definition: proxy_fetch.h:318
static const int kTestSignalTimeoutMs
Definition: proxy_fetch.h:343
void AddCallback(ProxyFetchPropertyCallback *callback)
UserAgentMatcher::DeviceType device_type()
Returns DeviceType from device property page.
Definition: proxy_fetch.h:258
Adds property-semantics to a raw cache API.
Definition: property_cache.h:180
FallbackPropertyPage * fallback_property_page()
Returns the fallback property page.
Definition: proxy_fetch.h:216
static const char kCollectorConnectProxyFetchFinish[]
Definition: proxy_fetch.h:322
virtual bool IsCacheValid(int64 write_timestamp_ms) const
Delegates to collector_'s IsCacheValid.
void Detach(HttpStatus::Code status_code)
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
static const char kHeadersSetupRaceAlarmQueued[]
Definition: proxy_fetch.h:331
virtual void HandleHeadersComplete()
protected interface from AsyncFetch.
Read/write API for HTTP response headers.
Definition: response_headers.h:37
Definition: queued_alarm.h:32
Definition: property_cache.h:323
virtual void Done(bool success)
Called as a result of PropertyCache::Read when the data is available.
Definition: proxy_fetch.h:131
Definition: scoped_ptr.h:30
Definition: function.h:47
void ConnectProxyFetch(ProxyFetch *proxy_fetch)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
PageType
The cache type associated with this callback.
Definition: property_cache.h:326
static ProxyFetchPropertyCallbackCollector * InitiatePropertyCacheLookup(bool is_resource_fetch, const GoogleUrl &request_url, ServerContext *server_context, RewriteOptions *options, AsyncFetch *async_fetch)
Definition: async_fetch.h:278
FallbackPropertyPage * ReleaseFallbackPropertyPage()
Releases the ownership of fallback property page.
Definition: proxy_fetch.h:226
Definition: queued_worker_pool.h:65
Definition: async_fetch.h:53
PropertyPage * ReleasePropertyPage(ProxyFetchPropertyCallback::PageType page_type)
bool IsCacheValid(int64 write_timestamp_ms) const
Definition: rewrite_driver.h:100
Definition: server_context.h:99
PropertyPage * property_page()
Returns the actual property page.
Definition: proxy_fetch.h:210
Definition: message_handler.h:39
DeviceType
Definition: user_agent_matcher.h:49
ProxyFetch * CreateNewProxyFetch(const GoogleString &url, AsyncFetch *async_fetch, RewriteDriver *driver, ProxyFetchPropertyCallbackCollector *property_callback, AsyncFetch *original_content_fetch)
Definition: proxy_fetch.h:62
virtual bool IsCachedResultValid(const ResponseHeaders &headers)
Definition: rewrite_options.h:84
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
void Done(ProxyFetchPropertyCallback *callback)
Called by a ProxyFetchPropertyCallback when the former is complete.
Definition: fallback_property_page.h:38
void StartNewProxyFetch(const GoogleString &url, AsyncFetch *async_fetch, RewriteDriver *driver, ProxyFetchPropertyCallbackCollector *property_callback, AsyncFetch *original_content_fetch)
PropertyPage * ReleaseOriginPropertyPage()
Releases the ownership of origin property page.
Definition: proxy_fetch.h:231