Page Speed Optimization Libraries
1.7.30.3
|
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 00125 virtual bool IsBackgroundFetch() const { return false; } 00126 00129 virtual void Reset() { headers_complete_ = false; } 00130 00131 bool headers_complete() const { return headers_complete_; } 00132 00139 bool content_length_known() const { 00140 return content_length_ != kContentLengthUnknown; 00141 } 00142 int64 content_length() const { return content_length_; } 00143 void set_content_length(int64 x) { content_length_ = x; } 00144 00147 GoogleString LoggingString(); 00148 00151 virtual const RequestContextPtr& request_context() { return request_ctx_; } 00152 00155 virtual AbstractLogRecord* log_record(); 00156 00157 protected: 00158 virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler) = 0; 00159 virtual bool HandleFlush(MessageHandler* handler) = 0; 00160 virtual void HandleDone(bool success) = 0; 00161 virtual void HandleHeadersComplete() = 0; 00162 00163 private: 00164 RequestHeaders* request_headers_; 00165 ResponseHeaders* response_headers_; 00166 ResponseHeaders* extra_response_headers_; 00167 RequestContextPtr request_ctx_; 00168 bool owns_request_headers_; 00169 bool owns_response_headers_; 00170 bool owns_extra_response_headers_; 00171 bool headers_complete_; 00172 int64 content_length_; 00173 00174 DISALLOW_COPY_AND_ASSIGN(AsyncFetch); 00175 }; 00176 00181 class StringAsyncFetch : public AsyncFetch { 00182 public: 00183 explicit StringAsyncFetch(const RequestContextPtr& request_ctx) 00184 : AsyncFetch(request_ctx), buffer_pointer_(&buffer_) { 00185 Init(); 00186 } 00187 00188 StringAsyncFetch(const RequestContextPtr& request_ctx, GoogleString* buffer) 00189 : AsyncFetch(request_ctx), buffer_pointer_(buffer) { 00190 Init(); 00191 } 00192 00193 virtual ~StringAsyncFetch(); 00194 00195 virtual bool HandleWrite(const StringPiece& content, 00196 MessageHandler* handler) { 00197 content.AppendToString(buffer_pointer_); 00198 return true; 00199 } 00200 virtual bool HandleFlush(MessageHandler* handler) { return true; } 00201 virtual void HandleHeadersComplete() {} 00202 virtual void HandleDone(bool success) { 00203 success_ = success; 00204 done_ = true; 00205 } 00206 00207 bool success() const { return success_; } 00208 bool done() const { return done_; } 00209 const GoogleString& buffer() const { return *buffer_pointer_; } 00210 00211 virtual void Reset() { 00212 done_ = false; 00213 success_ = false; 00214 buffer_pointer_->clear(); 00215 response_headers()->Clear(); 00216 extra_response_headers()->Clear(); 00217 request_headers()->Clear(); 00218 AsyncFetch::Reset(); 00219 } 00220 00221 protected: 00224 void set_success(bool success) { success_ = success; } 00225 void set_done(bool done) { done_ = done; } 00226 00227 private: 00228 void Init() { 00229 success_ = false; 00230 done_ = false; 00231 } 00232 00233 GoogleString buffer_; 00234 GoogleString* buffer_pointer_; 00235 bool success_; 00236 bool done_; 00237 00238 DISALLOW_COPY_AND_ASSIGN(StringAsyncFetch); 00239 }; 00240 00244 class AsyncFetchUsingWriter : public AsyncFetch { 00245 public: 00246 AsyncFetchUsingWriter(const RequestContextPtr& request_context, 00247 Writer* writer) 00248 : AsyncFetch(request_context), 00249 writer_(writer) {} 00250 virtual ~AsyncFetchUsingWriter(); 00251 00252 protected: 00253 virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler); 00254 virtual bool HandleFlush(MessageHandler* handler); 00255 00256 private: 00257 Writer* writer_; 00258 DISALLOW_COPY_AND_ASSIGN(AsyncFetchUsingWriter); 00259 }; 00260 00267 class SharedAsyncFetch : public AsyncFetch { 00268 public: 00269 explicit SharedAsyncFetch(AsyncFetch* base_fetch); 00270 virtual ~SharedAsyncFetch(); 00271 00272 virtual const RequestContextPtr& request_context() { 00273 return base_fetch_->request_context(); 00274 } 00275 00276 protected: 00277 virtual void HandleDone(bool success) { 00278 base_fetch_->Done(success); 00279 } 00280 00281 virtual bool HandleWrite(const StringPiece& content, 00282 MessageHandler* handler) { 00283 return base_fetch_->Write(content, handler); 00284 } 00285 00286 virtual bool HandleFlush(MessageHandler* handler) { 00287 return base_fetch_->Flush(handler); 00288 } 00289 00290 virtual void HandleHeadersComplete(); 00291 00292 virtual bool IsCachedResultValid(const ResponseHeaders& headers) { 00293 return base_fetch_->IsCachedResultValid(headers); 00294 } 00295 00296 virtual bool IsBackgroundFetch() const { 00297 return base_fetch_->IsBackgroundFetch(); 00298 } 00299 00301 void PropagateContentLength(); 00302 00303 private: 00304 AsyncFetch* base_fetch_; 00305 DISALLOW_COPY_AND_ASSIGN(SharedAsyncFetch); 00306 }; 00307 00313 class FallbackSharedAsyncFetch : public SharedAsyncFetch { 00314 public: 00316 static const char kStaleWarningHeaderValue[]; 00317 00318 FallbackSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* fallback, 00319 MessageHandler* handler); 00320 virtual ~FallbackSharedAsyncFetch(); 00321 00322 void set_fallback_responses_served(Variable* x) { 00323 fallback_responses_served_ = x; 00324 } 00325 00326 bool serving_fallback() const { return serving_fallback_; } 00327 00328 protected: 00329 virtual void HandleDone(bool success); 00330 virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler); 00331 virtual bool HandleFlush(MessageHandler* handler); 00332 virtual void HandleHeadersComplete(); 00333 00334 private: 00336 MessageHandler* handler_; 00337 HTTPValue fallback_; 00338 bool serving_fallback_; 00339 Variable* fallback_responses_served_; 00340 00341 DISALLOW_COPY_AND_ASSIGN(FallbackSharedAsyncFetch); 00342 }; 00343 00353 class ConditionalSharedAsyncFetch : public SharedAsyncFetch { 00354 public: 00355 ConditionalSharedAsyncFetch(AsyncFetch* base_fetch, HTTPValue* cached_value, 00356 MessageHandler* handler); 00357 virtual ~ConditionalSharedAsyncFetch(); 00358 00359 void set_num_conditional_refreshes(Variable* x) { 00360 num_conditional_refreshes_ = x; 00361 } 00362 00363 protected: 00364 virtual void HandleDone(bool success); 00365 virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler); 00366 virtual bool HandleFlush(MessageHandler* handler); 00367 virtual void HandleHeadersComplete(); 00368 00369 private: 00371 MessageHandler* handler_; 00372 HTTPValue cached_value_; 00375 bool serving_cached_value_; 00377 bool added_conditional_headers_to_request_; 00378 00379 Variable* num_conditional_refreshes_; 00380 00381 DISALLOW_COPY_AND_ASSIGN(ConditionalSharedAsyncFetch); 00382 }; 00383 00384 } 00385 00386 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_ASYNC_FETCH_H_