Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
async_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 
21 
22 #ifndef NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
23 #define NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
24 
26 #include "net/instaweb/http/public/request_context.h"
33 
34 
35 namespace net_instaweb {
36 
37 class AbstractLogRecord;
38 class MessageHandler;
39 class Variable;
40 
53 class AsyncFetch : public Writer {
54  public:
55  static const int kContentLengthUnknown = -1;
56 
57  AsyncFetch();
58  explicit AsyncFetch(const RequestContextPtr& request_ctx);
59 
60  virtual ~AsyncFetch();
61 
68  void HeadersComplete();
69 
72  void Done(bool success);
73 
76  virtual bool Write(const StringPiece& content, MessageHandler* handler);
77  virtual bool Flush(MessageHandler* handler);
78 
85  virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
86  return true;
87  }
88 
94 
100  void set_request_headers(RequestHeaders* headers);
101 
104 
108  const RequestHeaders* request_headers() const;
109 
112  void set_response_headers(ResponseHeaders* headers);
113 
121  void set_extra_response_headers(ResponseHeaders* headers);
122 
125  virtual bool IsBackgroundFetch() const { return false; }
126 
129  virtual void Reset() { headers_complete_ = false; }
130 
131  bool headers_complete() const { return headers_complete_; }
132 
139  bool content_length_known() const {
140  return content_length_ != kContentLengthUnknown;
141  }
142  int64 content_length() const { return content_length_; }
143  void set_content_length(int64 x) { content_length_ = x; }
144 
148 
151  virtual const RequestContextPtr& request_context() { return request_ctx_; }
152 
155  virtual AbstractLogRecord* log_record();
156 
162 
166  static bool IsGoogleCacheVia(StringPiece via_value);
167 
168  protected:
169  virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler) = 0;
170  virtual bool HandleFlush(MessageHandler* handler) = 0;
171  virtual void HandleDone(bool success) = 0;
172  virtual void HandleHeadersComplete() = 0;
173 
174  private:
175  RequestHeaders* request_headers_;
176  ResponseHeaders* response_headers_;
177  ResponseHeaders* extra_response_headers_;
178  RequestContextPtr request_ctx_;
179  bool owns_request_headers_;
180  bool owns_response_headers_;
181  bool owns_extra_response_headers_;
182  bool headers_complete_;
183  int64 content_length_;
184 
185 
186 };
187 
192 class StringAsyncFetch : public AsyncFetch {
193  public:
194  explicit StringAsyncFetch(const RequestContextPtr& request_ctx)
195  : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) {
196  Init();
197  }
198 
199  StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer)
200  : AsyncFetch(request_ctx), buffer_pointer_(buffer) {
201  Init();
202  }
203 
204  virtual ~StringAsyncFetch();
205 
206  virtual bool HandleWrite(const StringPiece& content,
207  MessageHandler* handler) {
208  content.AppendToString(buffer_pointer_);
209  return true;
210  }
211  virtual bool HandleFlush(MessageHandler* handler) { return true; }
212  virtual void HandleHeadersComplete() {}
213  virtual void HandleDone(bool success) {
214  success_ = success;
215  done_ = true;
216  }
217 
218  bool success() const { return success_; }
219  bool done() const { return done_; }
220  const GoogleString& buffer() const { return *buffer_pointer_; }
221 
222  virtual void Reset() {
223  done_ = false;
224  success_ = false;
225  buffer_pointer_->clear();
226  response_headers()->Clear();
227  extra_response_headers()->Clear();
228  request_headers()->Clear();
230  }
231 
232  protected:
235  void set_success(bool success) { success_ = success; }
236  void set_done(bool done) { done_ = done; }
237 
238  private:
239  void Init() {
240  success_ = false;
241  done_ = false;
242  }
243 
244  GoogleString buffer_;
245  GoogleString* buffer_pointer_;
246  bool success_;
247  bool done_;
248 
249 
250 };
251 
256  public:
258  Writer* writer)
259  : AsyncFetch(request_context),
260  writer_(writer) {}
261  virtual ~AsyncFetchUsingWriter();
262 
263  protected:
264  virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler);
265  virtual bool HandleFlush(MessageHandler* handler);
266 
267  private:
268  Writer* writer_;
269 
270 };
271 
278 class SharedAsyncFetch : public AsyncFetch {
279  public:
280  explicit SharedAsyncFetch(AsyncFetch* base_fetch);
281  virtual ~SharedAsyncFetch();
282 
284  return base_fetch_->request_context();
285  }
286 
287  protected:
288  virtual void HandleDone(bool success) {
289  base_fetch_->Done(success);
290  }
291 
292  virtual bool HandleWrite(const StringPiece& content,
293  MessageHandler* handler) {
294  return base_fetch_->Write(content, handler);
295  }
296 
297  virtual bool HandleFlush(MessageHandler* handler) {
298  return base_fetch_->Flush(handler);
299  }
300 
301  virtual void HandleHeadersComplete();
302 
303  virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
304  return base_fetch_->IsCachedResultValid(headers);
305  }
306 
307  virtual bool IsBackgroundFetch() const {
308  return base_fetch_->IsBackgroundFetch();
309  }
310 
312  void PropagateContentLength();
313 
314  private:
315  AsyncFetch* base_fetch_;
316 
317 };
318 
325  public:
327  static const char kStaleWarningHeaderValue[];
328 
329  FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback,
330  MessageHandler* handler);
331  virtual ~FallbackSharedAsyncFetch();
332 
333  void set_fallback_responses_served(Variable* x) {
334  fallback_responses_served_ = x;
335  }
336 
337  bool serving_fallback() const { return serving_fallback_; }
338 
339  protected:
340  virtual void HandleDone(bool success);
341  virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
342  virtual bool HandleFlush(MessageHandler* handler);
343  virtual void HandleHeadersComplete();
344 
345  private:
347  MessageHandler* handler_;
348  HTTPValue fallback_;
349  bool serving_fallback_;
350  Variable* fallback_responses_served_;
351 
352 
353 };
354 
365  public:
366  ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value,
367  MessageHandler* handler);
368  virtual ~ConditionalSharedAsyncFetch();
369 
370  void set_num_conditional_refreshes(Variable* x) {
371  num_conditional_refreshes_ = x;
372  }
373 
374  protected:
375  virtual void HandleDone(bool success);
376  virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
377  virtual bool HandleFlush(MessageHandler* handler);
378  virtual void HandleHeadersComplete();
379 
380  private:
382  MessageHandler* handler_;
383  HTTPValue cached_value_;
386  bool serving_cached_value_;
388  bool added_conditional_headers_to_request_;
389 
390  Variable* num_conditional_refreshes_;
391 
392 
393 };
394 
395 }
396 
397 #endif
Definition: http_value.h:38
Read/write API for HTTP request (RequestHeaders is a misnomer).
Definition: request_headers.h:32
void set_success(bool success)
Definition: async_fetch.h:235
Definition: statistics.h:43
virtual const RequestContextPtr & request_context()
Definition: async_fetch.h:151
ResponseHeaders * response_headers()
See doc for request_headers and set_request_headers.
Read/write API for HTTP response headers.
Definition: response_headers.h:37
Definition: log_record.h:59
virtual bool IsBackgroundFetch() const
Definition: async_fetch.h:307
virtual const RequestContextPtr & request_context()
Definition: async_fetch.h:283
GoogleString LoggingString()
RequestHeaders * request_headers()
void SetRequestHeadersTakingOwnership(RequestHeaders *headers)
Same as above, but takes ownership.
void Done(bool success)
static const char kStaleWarningHeaderValue[]
Warning header to be added if a stale response is served.
Definition: async_fetch.h:327
virtual AbstractLogRecord * log_record()
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
static bool IsGoogleCacheVia(StringPiece via_value)
virtual void Reset()
Definition: async_fetch.h:222
Definition: async_fetch.h:278
Definition: async_fetch.h:53
Definition: async_fetch.h:364
void PropagateContentLength()
Propagates any set_content_length from this to the base fetch.
virtual bool IsCachedResultValid(const ResponseHeaders &headers)
Definition: async_fetch.h:303
Interface for writing bytes to an output stream.
Definition: writer.h:29
virtual bool IsBackgroundFetch() const
Definition: async_fetch.h:125
Definition: async_fetch.h:255
ResponseHeaders * extra_response_headers()
Definition: message_handler.h:39
Definition: async_fetch.h:324
virtual bool IsCachedResultValid(const ResponseHeaders &headers)
Definition: async_fetch.h:85
Definition: async_fetch.h:192
virtual bool Write(const StringPiece &content, MessageHandler *handler)
void set_request_headers(RequestHeaders *headers)
bool content_length_known() const
Definition: async_fetch.h:139
virtual void Reset()
Definition: async_fetch.h:129