Page Speed Optimization Libraries  1.5.27.2
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 
00136   virtual bool do_stale_rewrite() const { return false; }
00137 
00138   RewriteDriver* driver_;
00139   GoogleString url_;
00141   bool is_rewritten_;
00144   GoogleString rewritten_hash_;
00145 
00147   ResourcePtr input_resource_;
00148   OutputResourcePtr output_resource_;
00149 
00150   scoped_ptr<CacheUrlAsyncFetcher> cache_fetcher_;
00151 
00161   bool proxy_mode_;
00162 
00163   DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteContext);
00164 };
00165 
00168 class RecordingFetch : public SharedAsyncFetch {
00169  public:
00170   RecordingFetch(AsyncFetch* async_fetch,
00171                  const ResourcePtr& resource,
00172                  InPlaceRewriteContext* context,
00173                  MessageHandler* handler);
00174 
00175   virtual ~RecordingFetch();
00176 
00178   virtual void HandleHeadersComplete();
00180   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00182   virtual bool HandleFlush(MessageHandler* handler);
00184   virtual void HandleDone(bool success);
00185 
00186  private:
00187   void FreeDriver();
00188 
00189   bool CanInPlaceRewrite();
00190 
00194   bool ShouldStream() const;
00195 
00196   MessageHandler* handler_;
00197   ResourcePtr resource_;
00198   InPlaceRewriteContext* context_;
00199 
00202   bool can_in_place_rewrite_;
00203 
00205   bool streaming_;
00206   HTTPValue cache_value_;
00207   HTTPValueWriter cache_value_writer_;
00208   ResponseHeaders saved_headers_;
00209   Variable* in_place_oversized_opt_stream_;
00210   Variable* in_place_uncacheable_rewrites_;
00211   DISALLOW_COPY_AND_ASSIGN(RecordingFetch);
00212 };
00213 
00214 }  
00215 
00216 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IN_PLACE_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines