Page Speed Optimization Libraries
1.5.27.2
|
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 00021 00022 #ifndef NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_ 00023 #define NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_ 00024 00025 #include "net/instaweb/http/public/http_value.h" 00026 #include "net/instaweb/http/public/request_context.h" 00027 #include "net/instaweb/http/public/request_headers.h" 00028 #include "net/instaweb/http/public/response_headers.h" 00029 #include "net/instaweb/util/public/basictypes.h" 00030 #include "net/instaweb/util/public/string.h" 00031 #include "net/instaweb/util/public/string_util.h" 00032 #include "net/instaweb/util/public/writer.h" 00033 00034 00035 namespace net_instaweb { 00036 00037 class AbstractLogRecord; 00038 class MessageHandler; 00039 class Variable; 00040 00053 class AsyncFetch : public Writer { 00054 public: 00055 static const int kContentLengthUnknown = -1; 00056 00057 AsyncFetch(); 00058 explicit AsyncFetch(const RequestContextPtr& request_ctx); 00059 00060 virtual ~AsyncFetch(); 00061 00068 void HeadersComplete(); 00069 00072 void Done(bool success); 00073 00076 virtual bool Write(const StringPiece& content, MessageHandler* handler); 00077 virtual bool Flush(MessageHandler* handler); 00078 00085 virtual bool IsCachedResultValid(const ResponseHeaders& headers) { 00086 return true; 00087 } 00088 00093 RequestHeaders* request_headers(); 00094 00100 void set_request_headers(RequestHeaders* headers); 00101 00103 void SetRequestHeadersTakingOwnership(RequestHeaders* headers); 00104 00108 const RequestHeaders* request_headers() const; 00109 00111 ResponseHeaders* response_headers(); 00112 void set_response_headers(ResponseHeaders* headers); 00113 00120 ResponseHeaders* extra_response_headers(); 00121 void set_extra_response_headers(ResponseHeaders* headers); 00122 00123 virtual bool EnableThreaded() const { return false; } 00124 00127 virtual bool IsBackgroundFetch() const { return false; } 00128 00131 virtual void Reset() { headers_complete_ = false; } 00132 00133 bool headers_complete() const { return headers_complete_; } 00134 00141 bool content_length_known() const { 00142 return content_length_ != kContentLengthUnknown; 00143 } 00144 int64 content_length() const { return content_length_; } 00145 void set_content_length(int64 x) { content_length_ = x; } 00146 00149 GoogleString LoggingString(); 00150 00153 virtual const RequestContextPtr& request_context() { return request_ctx_; } 00154 00157 virtual AbstractLogRecord* log_record(); 00158 00159 protected: 00160 virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler) = 0; 00161 virtual bool HandleFlush(MessageHandler* handler) = 0; 00162 virtual void HandleDone(bool success) = 0; 00163 virtual void HandleHeadersComplete() = 0; 00164 00165 private: 00166 RequestHeaders* request_headers_; 00167 ResponseHeaders* response_headers_; 00168 ResponseHeaders* extra_response_headers_; 00169 RequestContextPtr request_ctx_; 00170 bool owns_request_headers_; 00171 bool owns_response_headers_; 00172 bool owns_extra_response_headers_; 00173 bool headers_complete_; 00174 int64 content_length_; 00175 00176 DISALLOW_COPY_AND_ASSIGN(AsyncFetch); 00177 }; 00178 00183 class StringAsyncFetch : public AsyncFetch { 00184 public: 00185 explicit StringAsyncFetch(const RequestContextPtr& request_ctx) 00186 : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) { 00187 Init(); 00188 } 00189 00190 StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer) 00191 : AsyncFetch(request_ctx), buffer_pointer_(buffer) { 00192 Init(); 00193 } 00194 00195 virtual ~StringAsyncFetch(); 00196 00197 virtual bool HandleWrite(const StringPiece& content, 00198 MessageHandler* handler) { 00199 content.AppendToString(buffer_pointer_); 00200 return true; 00201 } 00202 virtual bool HandleFlush(MessageHandler* handler) { return true; } 00203 virtual void HandleHeadersComplete() {} 00204 virtual void HandleDone(bool success) { 00205 success_ = success; 00206 done_ = true; 00207 } 00208 00209 bool success() const { return success_; } 00210 bool done() const { return done_; } 00211 const GoogleString& buffer() const { return *buffer_pointer_; } 00212 00213 virtual void Reset() { 00214 done_ = false; 00215 success_ = false; 00216 buffer_pointer_->clear(); 00217 response_headers()->Clear(); 00218 extra_response_headers()->Clear(); 00219 request_headers()->Clear(); 00220 AsyncFetch::Reset(); 00221 } 00222 00223 protected: 00226 void set_success(bool success) { success_ = success; } 00227 void set_done(bool done) { done_ = done; } 00228 00229 private: 00230 void Init() { 00231 success_ = false; 00232 done_ = false; 00233 } 00234 00235 GoogleString buffer_; 00236 GoogleString* buffer_pointer_; 00237 bool success_; 00238 bool done_; 00239 00240 DISALLOW_COPY_AND_ASSIGN(StringAsyncFetch); 00241 }; 00242 00246 class AsyncFetchUsingWriter : public AsyncFetch { 00247 public: 00248 AsyncFetchUsingWriter(const RequestContextPtr& request_context, 00249 Writer* writer) 00250 : AsyncFetch(request_context), 00251 writer_(writer) {} 00252 virtual ~AsyncFetchUsingWriter(); 00253 00254 protected: 00255 virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler); 00256 virtual bool HandleFlush(MessageHandler* handler); 00257 00258 private: 00259 Writer* writer_; 00260 DISALLOW_COPY_AND_ASSIGN(AsyncFetchUsingWriter); 00261 }; 00262 00269 class SharedAsyncFetch : public AsyncFetch { 00270 public: 00271 explicit SharedAsyncFetch(AsyncFetch* base_fetch); 00272 virtual ~SharedAsyncFetch(); 00273 00274 virtual const RequestContextPtr& request_context() { 00275 return base_fetch_->request_context(); 00276 } 00277 00278 protected: 00279 virtual void HandleDone(bool success) { 00280 base_fetch_->Done(success); 00281 } 00282 00283 virtual bool HandleWrite(const StringPiece& content, 00284 MessageHandler* handler) { 00285 return base_fetch_->Write(content, handler); 00286 } 00287 00288 virtual bool HandleFlush(MessageHandler* handler) { 00289 return base_fetch_->Flush(handler); 00290 } 00291 00292 virtual void HandleHeadersComplete(); 00293 00294 virtual bool EnableThreaded() const { 00295 return base_fetch_->EnableThreaded(); 00296 } 00297 00298 virtual bool IsCachedResultValid(const ResponseHeaders& headers) { 00299 return base_fetch_->IsCachedResultValid(headers); 00300 } 00301 00302 virtual bool IsBackgroundFetch() const { 00303 return base_fetch_->IsBackgroundFetch(); 00304 } 00305 00307 void PropagateContentLength(); 00308 00309 private: 00310 AsyncFetch* base_fetch_; 00311 DISALLOW_COPY_AND_ASSIGN(SharedAsyncFetch); 00312 }; 00313 00319 class FallbackSharedAsyncFetch : public SharedAsyncFetch { 00320 public: 00322 static const char kStaleWarningHeaderValue[]; 00323 00324 FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback, 00325 MessageHandler* handler); 00326 virtual ~FallbackSharedAsyncFetch(); 00327 00328 void set_fallback_responses_served(Variable* x) { 00329 fallback_responses_served_ = x; 00330 } 00331 00332 bool serving_fallback() const { return serving_fallback_; } 00333 00334 protected: 00335 virtual void HandleDone(bool success); 00336 virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler); 00337 virtual bool HandleFlush(MessageHandler* handler); 00338 virtual void HandleHeadersComplete(); 00339 00340 private: 00342 MessageHandler* handler_; 00343 HTTPValue fallback_; 00344 bool serving_fallback_; 00345 Variable* fallback_responses_served_; 00346 00347 DISALLOW_COPY_AND_ASSIGN(FallbackSharedAsyncFetch); 00348 }; 00349 00359 class ConditionalSharedAsyncFetch : public SharedAsyncFetch { 00360 public: 00361 ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value, 00362 MessageHandler* handler); 00363 virtual ~ConditionalSharedAsyncFetch(); 00364 00365 void set_num_conditional_refreshes(Variable* x) { 00366 num_conditional_refreshes_ = x; 00367 } 00368 00369 protected: 00370 virtual void HandleDone(bool success); 00371 virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler); 00372 virtual bool HandleFlush(MessageHandler* handler); 00373 virtual void HandleHeadersComplete(); 00374 00375 private: 00377 MessageHandler* handler_; 00378 HTTPValue cached_value_; 00381 bool serving_cached_value_; 00383 bool added_conditional_headers_to_request_; 00384 00385 Variable* num_conditional_refreshes_; 00386 00387 DISALLOW_COPY_AND_ASSIGN(ConditionalSharedAsyncFetch); 00388 }; 00389 00390 } 00391 00392 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_