Page Speed Optimization Libraries  1.2.24.1
net/instaweb/rewriter/public/ajax_rewrite_context.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 
00018 
00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_AJAX_REWRITE_CONTEXT_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_AJAX_REWRITE_CONTEXT_H_
00021 
00022 #include "net/instaweb/http/public/async_fetch.h"
00023 #include "net/instaweb/http/public/content_type.h"
00024 #include "net/instaweb/http/public/http_value.h"
00025 #include "net/instaweb/http/public/http_value_writer.h"
00026 #include "net/instaweb/http/public/response_headers.h"
00027 #include "net/instaweb/rewriter/public/output_resource_kind.h"
00028 #include "net/instaweb/rewriter/public/resource.h"
00029 #include "net/instaweb/rewriter/public/server_context.h"
00030 #include "net/instaweb/rewriter/public/resource_slot.h"
00031 #include "net/instaweb/rewriter/public/rewrite_context.h"
00032 #include "net/instaweb/rewriter/public/rewrite_options.h"
00033 #include "net/instaweb/rewriter/public/single_rewrite_context.h"
00034 #include "net/instaweb/util/public/basictypes.h"
00035 #include "net/instaweb/util/public/proto_util.h"
00036 #include "net/instaweb/util/public/scoped_ptr.h"
00037 #include "net/instaweb/util/public/string.h"
00038 #include "net/instaweb/util/public/string_util.h"
00039 
00040 namespace net_instaweb {
00041 
00042 class InputInfo;
00043 class MessageHandler;
00044 class RewriteDriver;
00045 class RewriteFilter;
00046 class UrlAsyncFetcher;
00047 
00051 class AjaxRewriteResourceSlot : public ResourceSlot {
00052  public:
00053   explicit AjaxRewriteResourceSlot(const ResourcePtr& resource);
00054 
00056   virtual void Render();
00057 
00059   virtual GoogleString LocationString() { return "ajax"; }
00060 
00061  protected:
00062   virtual ~AjaxRewriteResourceSlot();
00063 
00064  private:
00065   DISALLOW_COPY_AND_ASSIGN(AjaxRewriteResourceSlot);
00066 };
00067 
00069 class AjaxRewriteContext : public SingleRewriteContext {
00070  public:
00074   static const char kInPlaceOversizedOptStream[];
00075 
00076   AjaxRewriteContext(RewriteDriver* driver, const StringPiece& url);
00077   virtual ~AjaxRewriteContext();
00078 
00080   virtual void RewriteSingle(const ResourcePtr& input,
00081                              const OutputResourcePtr& output);
00083   virtual const char* id() const { return RewriteOptions::kAjaxRewriteId; }
00085   virtual OutputResourceKind kind() const { return kRewrittenResource; }
00087   virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
00088                                MessageHandler* message_handler,
00089                                GoogleUrlStarVector* url_vector);
00091   virtual void StartFetchReconstruction();
00092 
00093   static void InitStats(Statistics* statistics);
00094 
00095  private:
00096   friend class RecordingFetch;
00098   virtual void Harvest();
00099   void StartFetchReconstructionParent();
00101   virtual void FixFetchFallbackHeaders(ResponseHeaders* headers);
00103   virtual void FetchTryFallback(const GoogleString& url,
00104                                 const StringPiece& hash);
00106   virtual void FetchCallbackDone(bool success);
00107 
00108   RewriteFilter* GetRewriteFilter(const ContentType& type);
00109 
00111   void UpdateDateAndExpiry(const protobuf::RepeatedPtrField<InputInfo>& inputs,
00112                            int64* date_ms, int64* expiry_ms);
00113 
00114   RewriteDriver* driver_;
00115   GoogleString url_;
00117   bool is_rewritten_;
00120   GoogleString rewritten_hash_;
00121 
00123   ResourcePtr input_resource_;
00124   OutputResourcePtr output_resource_;
00125 
00126   scoped_ptr<UrlAsyncFetcher> cache_fetcher_;
00127 
00128   DISALLOW_COPY_AND_ASSIGN(AjaxRewriteContext);
00129 };
00130 
00133 class RecordingFetch : public SharedAsyncFetch {
00134  public:
00135   RecordingFetch(AsyncFetch* async_fetch,
00136                  const ResourcePtr& resource,
00137                  AjaxRewriteContext* context,
00138                  MessageHandler* handler);
00139 
00140   virtual ~RecordingFetch();
00141 
00143   virtual void HandleHeadersComplete();
00145   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00147   virtual bool HandleFlush(MessageHandler* handler);
00149   virtual void HandleDone(bool success);
00150 
00151  private:
00152   void FreeDriver();
00153 
00154   bool CanAjaxRewrite();
00155 
00159   bool ShouldStream();
00160 
00161   MessageHandler* handler_;
00162   ResourcePtr resource_;
00163   AjaxRewriteContext* context_;
00164   bool can_ajax_rewrite_;
00165   bool streaming_;
00166   HTTPValue cache_value_;
00167   HTTPValueWriter cache_value_writer_;
00168   ResponseHeaders saved_headers_;
00169   Variable* in_place_oversized_opt_stream_;
00170   DISALLOW_COPY_AND_ASSIGN(RecordingFetch);
00171 };
00172 
00173 }  
00174 
00175 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_AJAX_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines