Page Speed Optimization Libraries  1.3.25.1
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 InputInfo;
00043 class MessageHandler;
00044 class ResourceContext;
00045 class RewriteDriver;
00046 class RewriteFilter;
00047 class Statistics;
00048 class UrlAsyncFetcher;
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 
00080   InPlaceRewriteContext(RewriteDriver* driver, const StringPiece& url);
00081   virtual ~InPlaceRewriteContext();
00082 
00084   virtual void RewriteSingle(const ResourcePtr& input,
00085                              const OutputResourcePtr& output);
00087   virtual const char* id() const { return RewriteOptions::kInPlaceRewriteId; }
00089   virtual OutputResourceKind kind() const { return kRewrittenResource; }
00091   virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
00092                                MessageHandler* message_handler,
00093                                GoogleUrlStarVector* url_vector);
00095   virtual void StartFetchReconstruction();
00096 
00097   static void InitStats(Statistics* statistics);
00098 
00099   bool perform_http_fetch() const { return perform_http_fetch_; }
00100   void set_perform_http_fetch(bool x) { perform_http_fetch_ = x; }
00101 
00102   virtual int64 GetRewriteDeadlineAlarmMs() const;
00103 
00104   virtual GoogleString UserAgentCacheKey(
00105       const ResourceContext* resource_context) const;
00106   virtual void EncodeUserAgentIntoResourceContext(ResourceContext* context);
00107 
00108  private:
00109   friend class RecordingFetch;
00111   virtual void Harvest();
00112   void StartFetchReconstructionParent();
00114   virtual void FixFetchFallbackHeaders(ResponseHeaders* headers);
00116   virtual void FetchTryFallback(const GoogleString& url,
00117                                 const StringPiece& hash);
00119   virtual void FetchCallbackDone(bool success);
00120 
00121   RewriteFilter* GetRewriteFilter(const ContentType& type);
00122 
00124   void UpdateDateAndExpiry(const protobuf::RepeatedPtrField<InputInfo>& inputs,
00125                            int64* date_ms, int64* expiry_ms);
00128   bool InPlaceOptimizeForBrowserEnabled() const;
00131   bool ShouldAddVaryUserAgent() const;
00132 
00135   virtual bool do_stale_rewrite() const { return false; }
00136 
00137   RewriteDriver* driver_;
00138   GoogleString url_;
00140   bool is_rewritten_;
00143   GoogleString rewritten_hash_;
00144 
00146   ResourcePtr input_resource_;
00147   OutputResourcePtr output_resource_;
00148 
00149   scoped_ptr<UrlAsyncFetcher> cache_fetcher_;
00150 
00152   bool perform_http_fetch_;
00153 
00154   DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteContext);
00155 };
00156 
00159 class RecordingFetch : public SharedAsyncFetch {
00160  public:
00161   RecordingFetch(AsyncFetch* async_fetch,
00162                  const ResourcePtr& resource,
00163                  InPlaceRewriteContext* context,
00164                  MessageHandler* handler);
00165 
00166   virtual ~RecordingFetch();
00167 
00169   virtual void HandleHeadersComplete();
00171   virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
00173   virtual bool HandleFlush(MessageHandler* handler);
00175   virtual void HandleDone(bool success);
00176 
00177  private:
00178   void FreeDriver();
00179 
00180   bool CanInPlaceRewrite();
00181 
00185   bool ShouldStream();
00186 
00187   MessageHandler* handler_;
00188   ResourcePtr resource_;
00189   InPlaceRewriteContext* context_;
00190   bool can_in_place_rewrite_;
00191   bool streaming_;
00192   HTTPValue cache_value_;
00193   HTTPValueWriter cache_value_writer_;
00194   ResponseHeaders saved_headers_;
00195   Variable* in_place_oversized_opt_stream_;
00196   DISALLOW_COPY_AND_ASSIGN(RecordingFetch);
00197 };
00198 
00199 }  
00200 
00201 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IN_PLACE_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines