Page Speed Optimization Libraries  1.2.24.1
net/instaweb/http/public/async_fetch.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00021 
00022 #ifndef NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
00023 #define NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
00024 
00025 #include "net/instaweb/http/public/http_value.h"
00026 #include "net/instaweb/http/public/request_context.h"
00027 #include "net/instaweb/http/public/request_headers.h"
00028 #include "net/instaweb/http/public/response_headers.h"
00029 #include "net/instaweb/util/public/basictypes.h"
00030 #include "net/instaweb/util/public/string.h"
00031 #include "net/instaweb/util/public/string_util.h"
00032 #include "net/instaweb/util/public/writer.h"
00033 
00034 
00035 namespace net_instaweb {
00036 
00037 class LogRecord;
00038 class MessageHandler;
00039 class Variable;
00040 
00053 class AsyncFetch : public Writer {
00054  public:
00055   AsyncFetch();
00056   explicit AsyncFetch(const RequestContextPtr& request_ctx);
00057 
00058   virtual ~AsyncFetch();
00059 
00066   void HeadersComplete();
00067 
00070   void Done(bool success);
00071 
00074   virtual bool Write(const StringPiece& content, MessageHandler* handler);
00075   virtual bool Flush(MessageHandler* handler);
00076 
00083   virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
00084     return true;
00085   }
00086 
00091   RequestHeaders* request_headers();
00092 
00098   void set_request_headers(RequestHeaders* headers);
00099 
00103   const RequestHeaders* request_headers() const;
00104 
00106   ResponseHeaders* response_headers();
00107   void set_response_headers(ResponseHeaders* headers);
00108 
00115   ResponseHeaders* extra_response_headers();
00116   void set_extra_response_headers(ResponseHeaders* headers);
00117 
00118   virtual bool EnableThreaded() const { return false; }
00119 
00122   virtual bool IsBackgroundFetch() const { return false; }
00123 
00126   virtual void Reset() { headers_complete_ = false; }
00127 
00128   bool headers_complete() const { return headers_complete_; }
00129 
00130 
00133   GoogleString LoggingString();
00134 
00137   virtual const RequestContextPtr& request_context() { return request_ctx_; }
00138 
00141   virtual LogRecord* log_record();
00142 
00143  protected:
00144   virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler) = 0;
00145   virtual bool HandleFlush(MessageHandler* handler) = 0;
00146   virtual void HandleDone(bool success) = 0;
00147   virtual void HandleHeadersComplete() = 0;
00148 
00149  private:
00150   RequestHeaders* request_headers_;
00151   ResponseHeaders* response_headers_;
00152   ResponseHeaders* extra_response_headers_;
00153   RequestContextPtr request_ctx_;
00154   bool owns_request_headers_;
00155   bool owns_response_headers_;
00156   bool owns_extra_response_headers_;
00157   bool headers_complete_;
00158 
00159   DISALLOW_COPY_AND_ASSIGN(AsyncFetch);
00160 };
00161 
00166 class StringAsyncFetch : public AsyncFetch {
00167  public:
00169   StringAsyncFetch() : buffer_pointer_(&buffer_) { Init(); }
00170 
00171   explicit StringAsyncFetch(const RequestContextPtr& request_ctx)
00172       : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) {
00173     Init();
00174   }
00175 
00176   explicit StringAsyncFetch(GoogleString* buffer) : buffer_pointer_(buffer) {
00177     Init();
00178   }
00179 
00180   StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer)
00181       : AsyncFetch(request_ctx), buffer_pointer_(buffer) {
00182     Init();
00183   }
00184 
00185   virtual ~StringAsyncFetch();
00186 
00187   virtual bool HandleWrite(const StringPiece& content,
00188                            MessageHandler* handler) {
00189     content.AppendToString(buffer_pointer_);
00190     return true;
00191   }
00192   virtual bool HandleFlush(MessageHandler* handler) { return true; }
00193   virtual void HandleHeadersComplete() {}
00194   virtual void HandleDone(bool success) {
00195     success_ = success;
00196     done_ = true;
00197   }
00198 
00199   bool success() const { return success_; }
00200   bool done() const { return done_; }
00201   const GoogleString& buffer() const { return *buffer_pointer_; }
00202 
00203   virtual void Reset() {
00204     done_ = false;
00205     success_ = false;
00206     buffer_pointer_->clear();
00207     response_headers()->Clear();
00208     extra_response_headers()->Clear();
00209     request_headers()->Clear();
00210     AsyncFetch::Reset();
00211   }
00212 
00213  protected:
00216   void set_success(bool success) { success_ = success; }
00217   void set_done(bool done) { done_ = done; }
00218 
00219  private:
00220   void Init() {
00221     success_ = false;
00222     done_ = false;
00223   }
00224 
00225   GoogleString buffer_;
00226   GoogleString* buffer_pointer_;
00227   bool success_;
00228   bool done_;
00229 
00230   DISALLOW_COPY_AND_ASSIGN(StringAsyncFetch);
00231 };
00232 
00236 class AsyncFetchUsingWriter : public AsyncFetch {
00237  public:
00238   AsyncFetchUsingWriter(const RequestContextPtr& request_context,
00239                         Writer* writer)
00240      : AsyncFetch(request_context),
00241        writer_(writer) {}
00242   virtual ~AsyncFetchUsingWriter();
00243 
00244  protected:
00245   virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler);
00246   virtual bool HandleFlush(MessageHandler* handler);
00247 
00248  private:
00249   Writer* writer_;
00250   DISALLOW_COPY_AND_ASSIGN(AsyncFetchUsingWriter);
00251 };
00252 
00257 class SharedAsyncFetch : public AsyncFetch {
00258  public:
00259   explicit SharedAsyncFetch(AsyncFetch* base_fetch);
00260   virtual ~SharedAsyncFetch();
00261 
00262   AsyncFetch* base_fetch() { return base_fetch_; }
00263   const AsyncFetch* base_fetch() const { return base_fetch_; }
00264 
00265   virtual const RequestContextPtr& request_context() {
00266     return base_fetch_->request_context();
00267   }
00268 
00269  protected:
00270   virtual void HandleDone(bool success) {
00271     base_fetch_->Done(success);
00272   }
00273 
00274   virtual bool HandleWrite(const StringPiece& content,
00275                            MessageHandler* handler) {
00276     return base_fetch_->Write(content, handler);
00277   }
00278 
00279   virtual bool HandleFlush(MessageHandler* handler) {
00280     return base_fetch_->Flush(handler);
00281   }
00282 
00283   virtual void HandleHeadersComplete() {
00284     base_fetch_->HeadersComplete();
00285   }
00286 
00287   virtual bool EnableThreaded() const {
00288     return base_fetch_->EnableThreaded();
00289   }
00290 
00291   virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
00292     return base_fetch_->IsCachedResultValid(headers);
00293   }
00294 
00295   virtual bool IsBackgroundFetch() const {
00296     return base_fetch_->IsBackgroundFetch();
00297   }
00298 
00299  private:
00300   AsyncFetch* base_fetch_;
00301   DISALLOW_COPY_AND_ASSIGN(SharedAsyncFetch);
00302 };
00303 
00309 class FallbackSharedAsyncFetch : public SharedAsyncFetch {
00310  public:
00312   static const char kStaleWarningHeaderValue[];
00313 
00314   FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback,
00315                            MessageHandler* handler);
00316   virtual ~FallbackSharedAsyncFetch();
00317 
00318   void set_fallback_responses_served(Variable* x) {
00319     fallback_responses_served_ = x;
00320   }
00321 
00322   bool serving_fallback() const { return serving_fallback_; }
00323 
00324  protected:
00325   virtual void HandleDone(bool success);
00326   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00327   virtual bool HandleFlush(MessageHandler* handler);
00328   virtual void HandleHeadersComplete();
00329 
00330  private:
00332   MessageHandler* handler_;
00333   HTTPValue fallback_;
00334   bool serving_fallback_;
00335   Variable* fallback_responses_served_; 
00336 
00337   DISALLOW_COPY_AND_ASSIGN(FallbackSharedAsyncFetch);
00338 };
00339 
00349 class ConditionalSharedAsyncFetch : public SharedAsyncFetch {
00350  public:
00351   ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value,
00352                               MessageHandler* handler);
00353   virtual ~ConditionalSharedAsyncFetch();
00354 
00355   void set_num_conditional_refreshes(Variable* x) {
00356     num_conditional_refreshes_ = x;
00357   }
00358 
00359  protected:
00360   virtual void HandleDone(bool success);
00361   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00362   virtual bool HandleFlush(MessageHandler* handler);
00363   virtual void HandleHeadersComplete();
00364 
00365  private:
00367   MessageHandler* handler_;
00368   HTTPValue cached_value_;
00371   bool serving_cached_value_;
00373   bool added_conditional_headers_to_request_;
00374 
00375   Variable* num_conditional_refreshes_; 
00376 
00377   DISALLOW_COPY_AND_ASSIGN(ConditionalSharedAsyncFetch);
00378 };
00379 
00380 }  
00381 
00382 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines