Page Speed Optimization Libraries  1.8.31.3
net/instaweb/rewriter/public/in_place_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_IN_PLACE_REWRITE_CONTEXT_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_IN_PLACE_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 CachedResult;
00043 class CacheUrlAsyncFetcher;
00044 class InputInfo;
00045 class MessageHandler;
00046 class ResourceContext;
00047 class RewriteDriver;
00048 class RewriteFilter;
00049 class Statistics;
00050 class Variable;
00051 
00055 class InPlaceRewriteResourceSlot : public ResourceSlot {
00056  public:
00057   static const char kIproSlotLocation[];
00058   explicit InPlaceRewriteResourceSlot(const ResourcePtr& resource);
00059 
00061   virtual void Render();
00062 
00064   virtual GoogleString LocationString();
00065 
00066  protected:
00067   virtual ~InPlaceRewriteResourceSlot();
00068 
00069  private:
00070   DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteResourceSlot);
00071 };
00072 
00074 class InPlaceRewriteContext : public SingleRewriteContext {
00075  public:
00079   static const char kInPlaceOversizedOptStream[];
00080   static const char kInPlaceUncacheableRewrites[];
00081 
00082   InPlaceRewriteContext(RewriteDriver* driver, const StringPiece& url);
00083   virtual ~InPlaceRewriteContext();
00084 
00086   virtual void RewriteSingle(const ResourcePtr& input,
00087                              const OutputResourcePtr& output);
00089   virtual const char* id() const { return RewriteOptions::kInPlaceRewriteId; }
00091   virtual OutputResourceKind kind() const { return kRewrittenResource; }
00093   virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
00094                                MessageHandler* message_handler,
00095                                GoogleUrlStarVector* url_vector);
00097   virtual void StartFetchReconstruction();
00098 
00099   static void InitStats(Statistics* statistics);
00100 
00101   bool proxy_mode() const { return proxy_mode_; }
00102   void set_proxy_mode(bool x) { proxy_mode_ = x; }
00103 
00104   virtual int64 GetRewriteDeadlineAlarmMs() const;
00105 
00106   virtual GoogleString UserAgentCacheKey(
00107       const ResourceContext* resource_context) const;
00108   virtual void EncodeUserAgentIntoResourceContext(ResourceContext* context);
00109 
00112   virtual bool CreationLockBeforeStartFetch() { return false; }
00113 
00114  private:
00115   friend class RecordingFetch;
00117   virtual void Harvest();
00118   void StartFetchReconstructionParent();
00120   virtual void FixFetchFallbackHeaders(const CachedResult& cached_result,
00121                                        ResponseHeaders* headers);
00123   virtual void FetchTryFallback(const GoogleString& url,
00124                                 const StringPiece& hash);
00126   virtual void FetchCallbackDone(bool success);
00127 
00128   RewriteFilter* GetRewriteFilter(const ContentType& type);
00129 
00131   void UpdateDateAndExpiry(const protobuf::RepeatedPtrField<InputInfo>& inputs,
00132                            int64* date_ms, int64* expiry_ms);
00135   bool InPlaceOptimizeForBrowserEnabled() const;
00138   void AddVaryIfRequired(const CachedResult& cached_result,
00139                          ResponseHeaders* headers) const;
00140 
00141   GoogleString url_;
00143   bool is_rewritten_;
00146   GoogleString rewritten_hash_;
00147 
00149   ResourcePtr input_resource_;
00150   OutputResourcePtr output_resource_;
00151 
00152   scoped_ptr<CacheUrlAsyncFetcher> cache_fetcher_;
00153 
00163   bool proxy_mode_;
00164 
00165   DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteContext);
00166 };
00167 
00170 class RecordingFetch : public SharedAsyncFetch {
00171  public:
00172   RecordingFetch(bool proxy_mode,
00173                  AsyncFetch* async_fetch,
00174                  const ResourcePtr& resource,
00175                  InPlaceRewriteContext* context,
00176                  MessageHandler* handler);
00177 
00178   virtual ~RecordingFetch();
00179 
00181   virtual void HandleHeadersComplete();
00183   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00185   virtual bool HandleFlush(MessageHandler* handler);
00187   virtual void HandleDone(bool success);
00188 
00189  private:
00190   void FreeDriver();
00191 
00192   bool CanInPlaceRewrite();
00193 
00197   bool ShouldStream() const;
00198 
00199   bool proxy_mode_;
00200   MessageHandler* handler_;
00201   ResourcePtr resource_;
00202   InPlaceRewriteContext* context_;
00203 
00206   bool can_in_place_rewrite_;
00207 
00209   bool streaming_;
00210   HTTPValue cache_value_;
00211   HTTPValueWriter cache_value_writer_;
00212   ResponseHeaders saved_headers_;
00213   Variable* in_place_oversized_opt_stream_;
00214   Variable* in_place_uncacheable_rewrites_;
00215   DISALLOW_COPY_AND_ASSIGN(RecordingFetch);
00216 };
00217 
00218 }  
00219 
00220 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IN_PLACE_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines