Page Speed Optimization Libraries  1.7.30.2
net/instaweb/apache/instaweb_context.h
Go to the documentation of this file.
00001 // Copyright 2010 Google Inc.
00017 
00018 #ifndef NET_INSTAWEB_APACHE_INSTAWEB_CONTEXT_H_
00019 #define NET_INSTAWEB_APACHE_INSTAWEB_CONTEXT_H_
00020 
00021 #include "net/instaweb/automatic/public/html_detector.h"
00022 #include "net/instaweb/http/public/content_type.h"
00023 #include "net/instaweb/http/public/request_context.h"
00024 #include "net/instaweb/http/public/response_headers.h"
00025 #include "net/instaweb/http/public/user_agent_matcher.h"
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/property_cache.h"
00028 #include "net/instaweb/util/public/scoped_ptr.h"
00029 #include "net/instaweb/util/public/string.h"
00030 #include "net/instaweb/util/public/string_util.h"
00031 #include "net/instaweb/util/public/string_writer.h"
00032 #include "net/instaweb/util/public/thread_system.h"
00033 
00037 #include "httpd.h"
00038 #include "apr_pools.h"
00039 
00040 struct apr_bucket_brigade;
00041 struct request_rec;
00042 struct server_rec;
00043 
00044 namespace net_instaweb {
00045 
00046 class ApacheServerContext;
00047 class GzipInflater;
00048 class RequestHeaders;
00049 class RewriteDriver;
00050 class RewriteOptions;
00051 
00052 const char kPagespeedOriginalUrl[] = "mod_pagespeed_original_url";
00053 
00055 template <class T>
00056 apr_status_t apache_cleanup(void* object) {
00057   T* resolved = static_cast<T*>(object);
00058   delete resolved;
00059   return APR_SUCCESS;
00060 }
00061 
00063 class PropertyCallback : public PropertyPage {
00064  public:
00065   PropertyCallback(const StringPiece& url,
00066                    const StringPiece& options_signature_hash,
00067                    UserAgentMatcher::DeviceType device_type,
00068                    RewriteDriver* driver,
00069                    ThreadSystem* thread_system);
00070 
00071   virtual void Done(bool success);
00072 
00073   void BlockUntilDone();
00074 
00075  private:
00076   RewriteDriver* driver_;
00077   GoogleString url_;
00078   bool done_;
00079   scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
00080   scoped_ptr<ThreadSystem::Condvar> condvar_;
00081   DISALLOW_COPY_AND_ASSIGN(PropertyCallback);
00082 };
00083 
00094 class InstawebContext {
00095  public:
00096   enum ContentEncoding { kNone, kGzip, kDeflate, kOther };
00097   enum ContentDetectionState { kStart, kHtml, kNotHtml };
00098 
00100   InstawebContext(request_rec* request,
00101                   RequestHeaders* request_headers,
00102                   const ContentType& content_type,
00103                   ApacheServerContext* server_context,
00104                   const GoogleString& base_url,
00105                   const RequestContextPtr& request_context,
00106                   bool use_custom_options,
00107                   const RewriteOptions& options);
00108   ~InstawebContext();
00109 
00110   void Rewrite(const char* input, int size);
00111   void Flush();
00112   void Finish();
00113 
00114   apr_bucket_brigade* bucket_brigade() const { return bucket_brigade_; }
00115   ContentEncoding content_encoding() const { return  content_encoding_; }
00116   ApacheServerContext* apache_server_context() { return server_context_; }
00117   const GoogleString& output() { return output_; }
00118   bool empty() const { return output_.empty(); }
00119   void clear() { output_.clear(); }  
00120 
00121   ResponseHeaders* response_headers() {
00122     return &response_headers_;
00123   }
00124 
00125   bool sent_headers() { return sent_headers_; }
00126   void set_sent_headers(bool sent) { sent_headers_ = sent; }
00127 
00129   void PopulateHeaders(request_rec* request);
00130 
00134   static ApacheServerContext* ServerContextFromServerRec(server_rec* server);
00135 
00137   static const char* MakeRequestUrl(const RewriteOptions& global_options,
00138                                     request_rec* request);
00139 
00140  private:
00141   void ComputeContentEncoding(request_rec* request);
00142 
00145   PropertyCallback* InitiatePropertyCacheLookup();
00146   void ProcessBytes(const char* input, int size);
00147 
00152   void SetExperimentStateAndCookie(request_rec* request,
00153                                    RewriteOptions* options);
00154 
00155   GoogleString output_; 
00156   apr_bucket_brigade* bucket_brigade_;
00157   ContentEncoding content_encoding_;
00158   const ContentType content_type_;
00159 
00160   ApacheServerContext* server_context_;
00161   RewriteDriver* rewrite_driver_;
00162   StringWriter string_writer_;
00163   scoped_ptr<GzipInflater> inflater_;
00164   HtmlDetector html_detector_;
00165   GoogleString absolute_url_;
00166   scoped_ptr<RequestHeaders> request_headers_;
00167   ResponseHeaders response_headers_;
00168   bool started_parse_;
00169   bool sent_headers_;
00170   bool populated_headers_;
00171 
00172   DISALLOW_COPY_AND_ASSIGN(InstawebContext);
00173 };
00174 
00175 }  
00176 
00177 #endif  ///< NET_INSTAWEB_APACHE_INSTAWEB_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines