Page Speed Optimization Libraries  1.4.26.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 
00101   void SetRequestHeadersTakingOwnership(RequestHeaders* headers);
00102 
00106   const RequestHeaders* request_headers() const;
00107 
00109   ResponseHeaders* response_headers();
00110   void set_response_headers(ResponseHeaders* headers);
00111 
00118   ResponseHeaders* extra_response_headers();
00119   void set_extra_response_headers(ResponseHeaders* headers);
00120 
00121   virtual bool EnableThreaded() const { return false; }
00122 
00125   virtual bool IsBackgroundFetch() const { return false; }
00126 
00129   virtual void Reset() { headers_complete_ = false; }
00130 
00131   bool headers_complete() const { return headers_complete_; }
00132 
00135   GoogleString LoggingString();
00136 
00139   virtual const RequestContextPtr& request_context() { return request_ctx_; }
00140 
00143   virtual LogRecord* log_record();
00144 
00145  protected:
00146   virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler) = 0;
00147   virtual bool HandleFlush(MessageHandler* handler) = 0;
00148   virtual void HandleDone(bool success) = 0;
00149   virtual void HandleHeadersComplete() = 0;
00150 
00151  private:
00152   RequestHeaders* request_headers_;
00153   ResponseHeaders* response_headers_;
00154   ResponseHeaders* extra_response_headers_;
00155   RequestContextPtr request_ctx_;
00156   bool owns_request_headers_;
00157   bool owns_response_headers_;
00158   bool owns_extra_response_headers_;
00159   bool headers_complete_;
00160 
00161   DISALLOW_COPY_AND_ASSIGN(AsyncFetch);
00162 };
00163 
00168 class StringAsyncFetch : public AsyncFetch {
00169  public:
00170   explicit StringAsyncFetch(const RequestContextPtr& request_ctx)
00171       : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) {
00172     Init();
00173   }
00174 
00175   StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer)
00176       : AsyncFetch(request_ctx), buffer_pointer_(buffer) {
00177     Init();
00178   }
00179 
00180   virtual ~StringAsyncFetch();
00181 
00182   virtual bool HandleWrite(const StringPiece& content,
00183                            MessageHandler* handler) {
00184     content.AppendToString(buffer_pointer_);
00185     return true;
00186   }
00187   virtual bool HandleFlush(MessageHandler* handler) { return true; }
00188   virtual void HandleHeadersComplete() {}
00189   virtual void HandleDone(bool success) {
00190     success_ = success;
00191     done_ = true;
00192   }
00193 
00194   bool success() const { return success_; }
00195   bool done() const { return done_; }
00196   const GoogleString& buffer() const { return *buffer_pointer_; }
00197 
00198   virtual void Reset() {
00199     done_ = false;
00200     success_ = false;
00201     buffer_pointer_->clear();
00202     response_headers()->Clear();
00203     extra_response_headers()->Clear();
00204     request_headers()->Clear();
00205     AsyncFetch::Reset();
00206   }
00207 
00208  protected:
00211   void set_success(bool success) { success_ = success; }
00212   void set_done(bool done) { done_ = done; }
00213 
00214  private:
00215   void Init() {
00216     success_ = false;
00217     done_ = false;
00218   }
00219 
00220   GoogleString buffer_;
00221   GoogleString* buffer_pointer_;
00222   bool success_;
00223   bool done_;
00224 
00225   DISALLOW_COPY_AND_ASSIGN(StringAsyncFetch);
00226 };
00227 
00231 class AsyncFetchUsingWriter : public AsyncFetch {
00232  public:
00233   AsyncFetchUsingWriter(const RequestContextPtr& request_context,
00234                         Writer* writer)
00235      : AsyncFetch(request_context),
00236        writer_(writer) {}
00237   virtual ~AsyncFetchUsingWriter();
00238 
00239  protected:
00240   virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler);
00241   virtual bool HandleFlush(MessageHandler* handler);
00242 
00243  private:
00244   Writer* writer_;
00245   DISALLOW_COPY_AND_ASSIGN(AsyncFetchUsingWriter);
00246 };
00247 
00252 class SharedAsyncFetch : public AsyncFetch {
00253  public:
00254   explicit SharedAsyncFetch(AsyncFetch* base_fetch);
00255   virtual ~SharedAsyncFetch();
00256 
00257   AsyncFetch* base_fetch() { return base_fetch_; }
00258   const AsyncFetch* base_fetch() const { return base_fetch_; }
00259 
00260   virtual const RequestContextPtr& request_context() {
00261     return base_fetch_->request_context();
00262   }
00263 
00264  protected:
00265   virtual void HandleDone(bool success) {
00266     base_fetch_->Done(success);
00267   }
00268 
00269   virtual bool HandleWrite(const StringPiece& content,
00270                            MessageHandler* handler) {
00271     return base_fetch_->Write(content, handler);
00272   }
00273 
00274   virtual bool HandleFlush(MessageHandler* handler) {
00275     return base_fetch_->Flush(handler);
00276   }
00277 
00278   virtual void HandleHeadersComplete() {
00279     base_fetch_->HeadersComplete();
00280   }
00281 
00282   virtual bool EnableThreaded() const {
00283     return base_fetch_->EnableThreaded();
00284   }
00285 
00286   virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
00287     return base_fetch_->IsCachedResultValid(headers);
00288   }
00289 
00290   virtual bool IsBackgroundFetch() const {
00291     return base_fetch_->IsBackgroundFetch();
00292   }
00293 
00294  private:
00295   AsyncFetch* base_fetch_;
00296   DISALLOW_COPY_AND_ASSIGN(SharedAsyncFetch);
00297 };
00298 
00304 class FallbackSharedAsyncFetch : public SharedAsyncFetch {
00305  public:
00307   static const char kStaleWarningHeaderValue[];
00308 
00309   FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback,
00310                            MessageHandler* handler);
00311   virtual ~FallbackSharedAsyncFetch();
00312 
00313   void set_fallback_responses_served(Variable* x) {
00314     fallback_responses_served_ = x;
00315   }
00316 
00317   bool serving_fallback() const { return serving_fallback_; }
00318 
00319  protected:
00320   virtual void HandleDone(bool success);
00321   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00322   virtual bool HandleFlush(MessageHandler* handler);
00323   virtual void HandleHeadersComplete();
00324 
00325  private:
00327   MessageHandler* handler_;
00328   HTTPValue fallback_;
00329   bool serving_fallback_;
00330   Variable* fallback_responses_served_; 
00331 
00332   DISALLOW_COPY_AND_ASSIGN(FallbackSharedAsyncFetch);
00333 };
00334 
00344 class ConditionalSharedAsyncFetch : public SharedAsyncFetch {
00345  public:
00346   ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value,
00347                               MessageHandler* handler);
00348   virtual ~ConditionalSharedAsyncFetch();
00349 
00350   void set_num_conditional_refreshes(Variable* x) {
00351     num_conditional_refreshes_ = x;
00352   }
00353 
00354  protected:
00355   virtual void HandleDone(bool success);
00356   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00357   virtual bool HandleFlush(MessageHandler* handler);
00358   virtual void HandleHeadersComplete();
00359 
00360  private:
00362   MessageHandler* handler_;
00363   HTTPValue cached_value_;
00366   bool serving_cached_value_;
00368   bool added_conditional_headers_to_request_;
00369 
00370   Variable* num_conditional_refreshes_; 
00371 
00372   DISALLOW_COPY_AND_ASSIGN(ConditionalSharedAsyncFetch);
00373 };
00374 
00375 }  
00376 
00377 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines