Page Speed Optimization Libraries  1.3.25.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:
00171   StringAsyncFetch() : buffer_pointer_(&buffer_) { Init(); }
00172 
00173   explicit StringAsyncFetch(const RequestContextPtr& request_ctx)
00174       : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) {
00175     Init();
00176   }
00177 
00178   explicit StringAsyncFetch(GoogleString* buffer) : buffer_pointer_(buffer) {
00179     Init();
00180   }
00181 
00182   StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer)
00183       : AsyncFetch(request_ctx), buffer_pointer_(buffer) {
00184     Init();
00185   }
00186 
00187   virtual ~StringAsyncFetch();
00188 
00189   virtual bool HandleWrite(const StringPiece& content,
00190                            MessageHandler* handler) {
00191     content.AppendToString(buffer_pointer_);
00192     return true;
00193   }
00194   virtual bool HandleFlush(MessageHandler* handler) { return true; }
00195   virtual void HandleHeadersComplete() {}
00196   virtual void HandleDone(bool success) {
00197     success_ = success;
00198     done_ = true;
00199   }
00200 
00201   bool success() const { return success_; }
00202   bool done() const { return done_; }
00203   const GoogleString& buffer() const { return *buffer_pointer_; }
00204 
00205   virtual void Reset() {
00206     done_ = false;
00207     success_ = false;
00208     buffer_pointer_->clear();
00209     response_headers()->Clear();
00210     extra_response_headers()->Clear();
00211     request_headers()->Clear();
00212     AsyncFetch::Reset();
00213   }
00214 
00215  protected:
00218   void set_success(bool success) { success_ = success; }
00219   void set_done(bool done) { done_ = done; }
00220 
00221  private:
00222   void Init() {
00223     success_ = false;
00224     done_ = false;
00225   }
00226 
00227   GoogleString buffer_;
00228   GoogleString* buffer_pointer_;
00229   bool success_;
00230   bool done_;
00231 
00232   DISALLOW_COPY_AND_ASSIGN(StringAsyncFetch);
00233 };
00234 
00238 class AsyncFetchUsingWriter : public AsyncFetch {
00239  public:
00240   AsyncFetchUsingWriter(const RequestContextPtr& request_context,
00241                         Writer* writer)
00242      : AsyncFetch(request_context),
00243        writer_(writer) {}
00244   virtual ~AsyncFetchUsingWriter();
00245 
00246  protected:
00247   virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler);
00248   virtual bool HandleFlush(MessageHandler* handler);
00249 
00250  private:
00251   Writer* writer_;
00252   DISALLOW_COPY_AND_ASSIGN(AsyncFetchUsingWriter);
00253 };
00254 
00259 class SharedAsyncFetch : public AsyncFetch {
00260  public:
00261   explicit SharedAsyncFetch(AsyncFetch* base_fetch);
00262   virtual ~SharedAsyncFetch();
00263 
00264   AsyncFetch* base_fetch() { return base_fetch_; }
00265   const AsyncFetch* base_fetch() const { return base_fetch_; }
00266 
00267   virtual const RequestContextPtr& request_context() {
00268     return base_fetch_->request_context();
00269   }
00270 
00271  protected:
00272   virtual void HandleDone(bool success) {
00273     base_fetch_->Done(success);
00274   }
00275 
00276   virtual bool HandleWrite(const StringPiece& content,
00277                            MessageHandler* handler) {
00278     return base_fetch_->Write(content, handler);
00279   }
00280 
00281   virtual bool HandleFlush(MessageHandler* handler) {
00282     return base_fetch_->Flush(handler);
00283   }
00284 
00285   virtual void HandleHeadersComplete() {
00286     base_fetch_->HeadersComplete();
00287   }
00288 
00289   virtual bool EnableThreaded() const {
00290     return base_fetch_->EnableThreaded();
00291   }
00292 
00293   virtual bool IsCachedResultValid(const ResponseHeaders& headers) {
00294     return base_fetch_->IsCachedResultValid(headers);
00295   }
00296 
00297   virtual bool IsBackgroundFetch() const {
00298     return base_fetch_->IsBackgroundFetch();
00299   }
00300 
00301  private:
00302   AsyncFetch* base_fetch_;
00303   DISALLOW_COPY_AND_ASSIGN(SharedAsyncFetch);
00304 };
00305 
00311 class FallbackSharedAsyncFetch : public SharedAsyncFetch {
00312  public:
00314   static const char kStaleWarningHeaderValue[];
00315 
00316   FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback,
00317                            MessageHandler* handler);
00318   virtual ~FallbackSharedAsyncFetch();
00319 
00320   void set_fallback_responses_served(Variable* x) {
00321     fallback_responses_served_ = x;
00322   }
00323 
00324   bool serving_fallback() const { return serving_fallback_; }
00325 
00326  protected:
00327   virtual void HandleDone(bool success);
00328   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00329   virtual bool HandleFlush(MessageHandler* handler);
00330   virtual void HandleHeadersComplete();
00331 
00332  private:
00334   MessageHandler* handler_;
00335   HTTPValue fallback_;
00336   bool serving_fallback_;
00337   Variable* fallback_responses_served_; 
00338 
00339   DISALLOW_COPY_AND_ASSIGN(FallbackSharedAsyncFetch);
00340 };
00341 
00351 class ConditionalSharedAsyncFetch : public SharedAsyncFetch {
00352  public:
00353   ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value,
00354                               MessageHandler* handler);
00355   virtual ~ConditionalSharedAsyncFetch();
00356 
00357   void set_num_conditional_refreshes(Variable* x) {
00358     num_conditional_refreshes_ = x;
00359   }
00360 
00361  protected:
00362   virtual void HandleDone(bool success);
00363   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00364   virtual bool HandleFlush(MessageHandler* handler);
00365   virtual void HandleHeadersComplete();
00366 
00367  private:
00369   MessageHandler* handler_;
00370   HTTPValue cached_value_;
00373   bool serving_cached_value_;
00375   bool added_conditional_headers_to_request_;
00376 
00377   Variable* num_conditional_refreshes_; 
00378 
00379   DISALLOW_COPY_AND_ASSIGN(ConditionalSharedAsyncFetch);
00380 };
00381 
00382 }  
00383 
00384 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines