Page Speed Optimization Libraries
1.3.25.1
|
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/util/public/basictypes.h" 00026 #include "net/instaweb/util/public/property_cache.h" 00027 #include "net/instaweb/util/public/scoped_ptr.h" 00028 #include "net/instaweb/util/public/string.h" 00029 #include "net/instaweb/util/public/string_util.h" 00030 #include "net/instaweb/util/public/string_writer.h" 00031 #include "net/instaweb/util/public/thread_system.h" 00032 00036 #include "httpd.h" 00037 #include "apr_pools.h" 00038 00039 struct apr_bucket_brigade; 00040 struct request_rec; 00041 struct server_rec; 00042 00043 namespace net_instaweb { 00044 00045 class ApacheServerContext; 00046 class GzipInflater; 00047 class RequestHeaders; 00048 class RewriteDriver; 00049 class RewriteOptions; 00050 00051 const char kPagespeedOriginalUrl[] = "mod_pagespeed_original_url"; 00052 00054 template <class T> 00055 apr_status_t apache_cleanup(void* object) { 00056 T* resolved = static_cast<T*>(object); 00057 delete resolved; 00058 return APR_SUCCESS; 00059 } 00060 00062 class PropertyCallback : public PropertyPage { 00063 public: 00064 PropertyCallback(RewriteDriver* driver, 00065 ThreadSystem* thread_system, 00066 const StringPiece& key); 00067 00068 virtual void Done(bool success); 00069 00070 void BlockUntilDone(); 00071 00072 private: 00073 RewriteDriver* driver_; 00074 GoogleString url_; 00075 bool done_; 00076 scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_; 00077 scoped_ptr<ThreadSystem::Condvar> condvar_; 00078 DISALLOW_COPY_AND_ASSIGN(PropertyCallback); 00079 }; 00080 00091 class InstawebContext { 00092 public: 00093 enum ContentEncoding { kNone, kGzip, kDeflate, kOther }; 00094 enum ContentDetectionState { kStart, kHtml, kNotHtml }; 00095 00097 InstawebContext(request_rec* request, 00098 RequestHeaders* request_headers, 00099 const ContentType& content_type, 00100 ApacheServerContext* server_context, 00101 const GoogleString& base_url, 00102 const RequestContextPtr& request_context, 00103 bool use_custom_options, 00104 const RewriteOptions& options); 00105 ~InstawebContext(); 00106 00107 void Rewrite(const char* input, int size); 00108 void Flush(); 00109 void Finish(); 00110 00111 apr_bucket_brigade* bucket_brigade() const { return bucket_brigade_; } 00112 ContentEncoding content_encoding() const { return content_encoding_; } 00113 ApacheServerContext* apache_server_context() { return server_context_; } 00114 const GoogleString& output() { return output_; } 00115 bool empty() const { return output_.empty(); } 00116 void clear() { output_.clear(); } 00117 00118 ResponseHeaders* response_headers() { 00119 return &response_headers_; 00120 } 00121 00122 bool sent_headers() { return sent_headers_; } 00123 void set_sent_headers(bool sent) { sent_headers_ = sent; } 00124 00126 void PopulateHeaders(request_rec* request); 00127 00131 static ApacheServerContext* ServerContextFromServerRec(server_rec* server); 00132 00134 static const char* MakeRequestUrl(const RewriteOptions& options, 00135 request_rec* request); 00136 00137 bool modify_caching_headers() const { return modify_caching_headers_; } 00138 00139 private: 00140 void ComputeContentEncoding(request_rec* request); 00141 00144 PropertyCallback* InitiatePropertyCacheLookup(); 00145 void ProcessBytes(const char* input, int size); 00146 00151 void SetFuriousStateAndCookie(request_rec* request, RewriteOptions* options); 00152 00153 GoogleString output_; 00154 apr_bucket_brigade* bucket_brigade_; 00155 ContentEncoding content_encoding_; 00156 const ContentType content_type_; 00157 00158 ApacheServerContext* server_context_; 00159 RewriteDriver* rewrite_driver_; 00160 StringWriter string_writer_; 00161 scoped_ptr<GzipInflater> inflater_; 00162 HtmlDetector html_detector_; 00163 GoogleString absolute_url_; 00164 scoped_ptr<RequestHeaders> request_headers_; 00165 ResponseHeaders response_headers_; 00166 bool started_parse_; 00167 bool sent_headers_; 00168 bool populated_headers_; 00169 bool modify_caching_headers_; 00170 00171 DISALLOW_COPY_AND_ASSIGN(InstawebContext); 00172 }; 00173 00174 } 00175 00176 #endif ///< NET_INSTAWEB_APACHE_INSTAWEB_CONTEXT_H_