Page Speed Optimization Libraries  1.11.33.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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"
27 #include "pagespeed/kernel/base/basictypes.h"
28 #include "pagespeed/kernel/base/string.h"
29 #include "pagespeed/kernel/base/string_util.h"
30 #include "pagespeed/kernel/base/writer.h"
31 #include "pagespeed/kernel/http/request_headers.h"
32 #include "pagespeed/kernel/http/response_headers.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 
93  RequestHeaders* request_headers();
94 
100  void set_request_headers(RequestHeaders* headers);
101 
103  void SetRequestHeadersTakingOwnership(RequestHeaders* headers);
104 
108  const RequestHeaders* request_headers() const;
109 
111  ResponseHeaders* response_headers();
112  void set_response_headers(ResponseHeaders* headers);
113 
120  ResponseHeaders* extra_response_headers();
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 
147  GoogleString LoggingString();
148 
151  virtual const RequestContextPtr& request_context() { return request_ctx_; }
152 
155  virtual AbstractLogRecord* log_record();
156 
157  protected:
158  virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler) = 0;
159  virtual bool HandleFlush(MessageHandler* handler) = 0;
160  virtual void HandleDone(bool success) = 0;
161  virtual void HandleHeadersComplete() = 0;
162 
163  private:
164  RequestHeaders* request_headers_;
165  ResponseHeaders* response_headers_;
166  ResponseHeaders* extra_response_headers_;
167  RequestContextPtr request_ctx_;
168  bool owns_request_headers_;
169  bool owns_response_headers_;
170  bool owns_extra_response_headers_;
171  bool headers_complete_;
172  int64 content_length_;
173 
174 
175 };
176 
181 class StringAsyncFetch : public AsyncFetch {
182  public:
183  explicit StringAsyncFetch(const RequestContextPtr& request_ctx)
184  : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) {
185  Init();
186  }
187 
188  StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer)
189  : AsyncFetch(request_ctx), buffer_pointer_(buffer) {
190  Init();
191  }
192 
193  virtual ~StringAsyncFetch();
194 
195  virtual bool HandleWrite(const StringPiece& content,
196  MessageHandler* handler) {
197  content.AppendToString(buffer_pointer_);
198  return true;
199  }
200  virtual bool HandleFlush(MessageHandler* handler) { return true; }
201  virtual void HandleHeadersComplete() {}
202  virtual void HandleDone(bool success) {
203  success_ = success;
204  done_ = true;
205  }
206 
207  bool success() const { return success_; }
208  bool done() const { return done_; }
209  const GoogleString& buffer() const { return *buffer_pointer_; }
210 
211  virtual void Reset() {
212  done_ = false;
213  success_ = false;
214  buffer_pointer_->clear();
215  response_headers()->Clear();
216  extra_response_headers()->Clear();
217  request_headers()->Clear();
219  }
220 
221  protected:
224  void set_success(bool success) { success_ = success; }
225  void set_done(bool done) { done_ = done; }
226 
227  private:
228  void Init() {
229  success_ = false;
230  done_ = false;
231  }
232 
233  GoogleString buffer_;
234  GoogleString* buffer_pointer_;
235  bool success_;
236  bool done_;
237 
238 
239 };
240 
245  public:
246  AsyncFetchUsingWriter(const RequestContextPtr& request_context,
247  Writer* writer)
248  : AsyncFetch(request_context),
249  writer_(writer) {}
250  virtual ~AsyncFetchUsingWriter();
251 
252  protected:
253  virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler);
254  virtual bool HandleFlush(MessageHandler* handler);
255 
256  private:
257  Writer* writer_;
258 
259 };
260 
267 class SharedAsyncFetch : public AsyncFetch {
268  public:
269  explicit SharedAsyncFetch(AsyncFetch* base_fetch);
270  virtual ~SharedAsyncFetch();
271 
272  virtual const RequestContextPtr& request_context() {
273  return base_fetch_->request_context();
274  }
275 
276  protected:
277  virtual void HandleDone(bool success) {
278  base_fetch_->Done(success);
279  }
280 
281  virtual bool HandleWrite(const StringPiece& content,
282  MessageHandler* handler) {
283  return base_fetch_->Write(content, handler);
284  }
285 
286  virtual bool HandleFlush(MessageHandler* handler) {
287  return base_fetch_->Flush(handler);
288  }
289 
290  virtual void HandleHeadersComplete();
291 
292  virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
293  return base_fetch_->IsCachedResultValid(headers);
294  }
295 
296  virtual bool IsBackgroundFetch() const {
297  return base_fetch_->IsBackgroundFetch();
298  }
299 
301  void PropagateContentLength();
302 
303  private:
304  AsyncFetch* base_fetch_;
305 
306 };
307 
314  public:
316  static const char kStaleWarningHeaderValue[];
317 
318  FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback,
319  MessageHandler* handler);
320  virtual ~FallbackSharedAsyncFetch();
321 
322  void set_fallback_responses_served(Variable* x) {
323  fallback_responses_served_ = x;
324  }
325 
326  bool serving_fallback() const { return serving_fallback_; }
327 
328  protected:
329  virtual void HandleDone(bool success);
330  virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
331  virtual bool HandleFlush(MessageHandler* handler);
332  virtual void HandleHeadersComplete();
333 
334  private:
336  MessageHandler* handler_;
337  HTTPValue fallback_;
338  bool serving_fallback_;
339  Variable* fallback_responses_served_;
340 
341 
342 };
343 
354  public:
355  ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value,
356  MessageHandler* handler);
357  virtual ~ConditionalSharedAsyncFetch();
358 
359  void set_num_conditional_refreshes(Variable* x) {
360  num_conditional_refreshes_ = x;
361  }
362 
363  protected:
364  virtual void HandleDone(bool success);
365  virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
366  virtual bool HandleFlush(MessageHandler* handler);
367  virtual void HandleHeadersComplete();
368 
369  private:
371  MessageHandler* handler_;
372  HTTPValue cached_value_;
375  bool serving_cached_value_;
377  bool added_conditional_headers_to_request_;
378 
379  Variable* num_conditional_refreshes_;
380 
381 
382 };
383 
384 }
385 
386 #endif
Definition: http_value.h:38
void set_success(bool success)
Definition: async_fetch.h:224
virtual const RequestContextPtr & request_context()
Definition: async_fetch.h:151
ResponseHeaders * response_headers()
See doc for request_headers and set_request_headers.
virtual bool IsBackgroundFetch() const
Definition: async_fetch.h:296
virtual const RequestContextPtr & request_context()
Definition: async_fetch.h:272
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:316
virtual AbstractLogRecord * log_record()
virtual void Reset()
Definition: async_fetch.h:211
Definition: async_fetch.h:267
Definition: async_fetch.h:53
Definition: async_fetch.h:353
void PropagateContentLength()
Propagates any set_content_length from this to the base fetch.
virtual bool IsCachedResultValid(const ResponseHeaders &headers)
Definition: async_fetch.h:292
virtual bool IsBackgroundFetch() const
Definition: async_fetch.h:125
Definition: async_fetch.h:244
ResponseHeaders * extra_response_headers()
Definition: async_fetch.h:313
virtual bool IsCachedResultValid(const ResponseHeaders &headers)
Definition: async_fetch.h:85
Definition: async_fetch.h:181
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