Page Speed Optimization Libraries
1.7.30.3
|
00001 /* 00002 * Copyright 2010 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_HTTP_PUBLIC_MOCK_URL_FETCHER_H_ 00020 #define NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_ 00021 00022 #include <map> 00023 00024 #include "net/instaweb/http/public/response_headers.h" 00025 #include "net/instaweb/http/public/url_async_fetcher.h" 00026 #include "net/instaweb/util/public/basictypes.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 "pagespeed/kernel/base/abstract_mutex.h" 00031 00032 namespace net_instaweb { 00033 00034 class AsyncFetch; 00035 class MessageHandler; 00036 class ThreadSystem; 00037 class Timer; 00038 00041 class MockUrlFetcher : public UrlAsyncFetcher { 00042 public: 00043 MockUrlFetcher(); 00044 virtual ~MockUrlFetcher(); 00045 00046 void SetResponse(const StringPiece& url, 00047 const ResponseHeaders& response_header, 00048 const StringPiece& response_body); 00049 00052 void AddToResponse(const StringPiece& url, 00053 const StringPiece& name, 00054 const StringPiece& value); 00055 00059 void SetConditionalResponse(const StringPiece& url, 00060 int64 last_modified_date, 00061 const GoogleString& etag, 00062 const ResponseHeaders& response_header, 00063 const StringPiece& response_body); 00064 00066 virtual void Fetch(const GoogleString& url, 00067 MessageHandler* message_handler, 00068 AsyncFetch* fetch); 00069 00070 virtual bool SupportsHttps() const { 00071 ScopedMutex lock(mutex_.get()); 00072 return supports_https_; 00073 } 00074 00075 void set_fetcher_supports_https(bool supports_https) { 00076 ScopedMutex lock(mutex_.get()); 00077 supports_https_ = supports_https; 00078 } 00079 00081 const GoogleString& last_referer() { 00082 ScopedMutex lock(mutex_.get()); 00083 return last_referer_; 00084 } 00085 00092 void SetResponseFailure(const StringPiece& url); 00093 00095 void Clear(); 00096 00098 void RemoveResponse(const StringPiece& url); 00099 00102 void Disable() { 00103 ScopedMutex lock(mutex_.get()); 00104 enabled_ = false; 00105 } 00106 void Enable() { 00107 ScopedMutex lock(mutex_.get()); 00108 enabled_ = true; 00109 } 00110 00113 void set_fail_on_unexpected(bool x) { 00114 ScopedMutex lock(mutex_.get()); 00115 fail_on_unexpected_ = x; 00116 } 00117 00120 void set_update_date_headers(bool x) { 00121 ScopedMutex lock(mutex_.get()); 00122 update_date_headers_ = x; 00123 } 00124 00127 void set_omit_empty_writes(bool x) { 00128 ScopedMutex lock(mutex_.get()); 00129 omit_empty_writes_ = x; 00130 } 00131 00135 void set_fail_after_headers(bool x) { 00136 ScopedMutex lock(mutex_.get()); 00137 fail_after_headers_ = x; 00138 } 00139 00142 void set_verify_host_header(bool x) { 00143 ScopedMutex lock(mutex_.get()); 00144 verify_host_header_ = x; 00145 } 00146 00147 void set_verify_pagespeed_header_off(bool x) { 00148 ScopedMutex lock(mutex_.get()); 00149 verify_pagespeed_header_off_ = x; 00150 } 00151 00152 void set_timer(Timer* timer) { 00153 ScopedMutex lock(mutex_.get()); 00154 timer_ = timer; 00155 } 00156 00160 void set_split_writes(bool val) { 00161 ScopedMutex lock(mutex_.get()); 00162 split_writes_ = val; 00163 } 00164 00166 void set_error_message(const GoogleString& msg) { 00167 ScopedMutex lock(mutex_.get()); 00168 error_message_ = msg; 00169 } 00170 00171 private: 00172 class HttpResponse { 00173 public: 00174 HttpResponse(int64 last_modified_time, const GoogleString& etag, 00175 const ResponseHeaders& in_header, const StringPiece& in_body) 00176 : last_modified_time_(last_modified_time), 00177 etag_(etag), 00178 body_(in_body.data(), in_body.size()), 00179 success_(true) { 00180 header_.CopyFrom(in_header); 00181 } 00182 00183 const int64 last_modified_time() const { return last_modified_time_; } 00184 const GoogleString& etag() const { return etag_; } 00185 const ResponseHeaders& header() const { return header_; } 00186 ResponseHeaders* mutable_header() { return &header_; } 00187 const GoogleString& body() const { return body_; } 00188 void set_success(bool success) { success_ = success; } 00189 bool success() const { return success_; } 00190 00191 private: 00192 int64 last_modified_time_; 00193 GoogleString etag_; 00194 ResponseHeaders header_; 00195 GoogleString body_; 00196 bool success_; 00197 00198 DISALLOW_COPY_AND_ASSIGN(HttpResponse); 00199 }; 00200 typedef std::map<const GoogleString, HttpResponse*> ResponseMap; 00201 00204 ResponseMap response_map_; 00205 00206 bool enabled_; 00207 bool fail_on_unexpected_; 00208 bool update_date_headers_; 00209 bool omit_empty_writes_; 00210 bool fail_after_headers_; 00211 bool verify_host_header_; 00212 bool verify_pagespeed_header_off_; 00213 bool split_writes_; 00214 bool supports_https_; 00215 GoogleString error_message_; 00216 Timer* timer_; 00217 GoogleString last_referer_; 00218 scoped_ptr<ThreadSystem> thread_system_; 00219 scoped_ptr<AbstractMutex> mutex_; 00220 00221 DISALLOW_COPY_AND_ASSIGN(MockUrlFetcher); 00222 }; 00223 00224 } 00225 00226 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_