Page Speed Optimization Libraries  1.6.29.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 CacheUrlAsyncFetcher;
00043 class InputInfo;
00044 class MessageHandler;
00045 class ResourceContext;
00046 class RewriteDriver;
00047 class RewriteFilter;
00048 class Statistics;
00049 class Variable;
00050 
00054 class InPlaceRewriteResourceSlot : public ResourceSlot {
00055  public:
00056   static const char kIproSlotLocation[];
00057   explicit InPlaceRewriteResourceSlot(const ResourcePtr& resource);
00058 
00060   virtual void Render();
00061 
00063   virtual GoogleString LocationString();
00064 
00065  protected:
00066   virtual ~InPlaceRewriteResourceSlot();
00067 
00068  private:
00069   DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteResourceSlot);
00070 };
00071 
00073 class InPlaceRewriteContext : public SingleRewriteContext {
00074  public:
00078   static const char kInPlaceOversizedOptStream[];
00079   static const char kInPlaceUncacheableRewrites[];
00080 
00081   InPlaceRewriteContext(RewriteDriver* driver, const StringPiece& url);
00082   virtual ~InPlaceRewriteContext();
00083 
00085   virtual void RewriteSingle(const ResourcePtr& input,
00086                              const OutputResourcePtr& output);
00088   virtual const char* id() const { return RewriteOptions::kInPlaceRewriteId; }
00090   virtual OutputResourceKind kind() const { return kRewrittenResource; }
00092   virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
00093                                MessageHandler* message_handler,
00094                                GoogleUrlStarVector* url_vector);
00096   virtual void StartFetchReconstruction();
00097 
00098   static void InitStats(Statistics* statistics);
00099 
00100   bool proxy_mode() const { return proxy_mode_; }
00101   void set_proxy_mode(bool x) { proxy_mode_ = x; }
00102 
00103   virtual int64 GetRewriteDeadlineAlarmMs() const;
00104 
00105   virtual GoogleString UserAgentCacheKey(
00106       const ResourceContext* resource_context) const;
00107   virtual void EncodeUserAgentIntoResourceContext(ResourceContext* context);
00108 
00109  private:
00110   friend class RecordingFetch;
00112   virtual void Harvest();
00113   void StartFetchReconstructionParent();
00115   virtual void FixFetchFallbackHeaders(ResponseHeaders* headers);
00117   virtual void FetchTryFallback(const GoogleString& url,
00118                                 const StringPiece& hash);
00120   virtual void FetchCallbackDone(bool success);
00121 
00122   RewriteFilter* GetRewriteFilter(const ContentType& type);
00123 
00125   void UpdateDateAndExpiry(const protobuf::RepeatedPtrField<InputInfo>& inputs,
00126                            int64* date_ms, int64* expiry_ms);
00129   bool InPlaceOptimizeForBrowserEnabled() const;
00132   bool ShouldAddVaryUserAgent() const;
00133 
00134   RewriteDriver* driver_;
00135   GoogleString url_;
00137   bool is_rewritten_;
00140   GoogleString rewritten_hash_;
00141 
00143   ResourcePtr input_resource_;
00144   OutputResourcePtr output_resource_;
00145 
00146   scoped_ptr<CacheUrlAsyncFetcher> cache_fetcher_;
00147 
00157   bool proxy_mode_;
00158 
00159   DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteContext);
00160 };
00161 
00164 class RecordingFetch : public SharedAsyncFetch {
00165  public:
00166   RecordingFetch(AsyncFetch* async_fetch,
00167                  const ResourcePtr& resource,
00168                  InPlaceRewriteContext* context,
00169                  MessageHandler* handler);
00170 
00171   virtual ~RecordingFetch();
00172 
00174   virtual void HandleHeadersComplete();
00176   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00178   virtual bool HandleFlush(MessageHandler* handler);
00180   virtual void HandleDone(bool success);
00181 
00182  private:
00183   void FreeDriver();
00184 
00185   bool CanInPlaceRewrite();
00186 
00190   bool ShouldStream() const;
00191 
00192   MessageHandler* handler_;
00193   ResourcePtr resource_;
00194   InPlaceRewriteContext* context_;
00195 
00198   bool can_in_place_rewrite_;
00199 
00201   bool streaming_;
00202   HTTPValue cache_value_;
00203   HTTPValueWriter cache_value_writer_;
00204   ResponseHeaders saved_headers_;
00205   Variable* in_place_oversized_opt_stream_;
00206   Variable* in_place_uncacheable_rewrites_;
00207   DISALLOW_COPY_AND_ASSIGN(RecordingFetch);
00208 };
00209 
00210 }  
00211 
00212 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IN_PLACE_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines