1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // This file declares HttpCache::Transaction, a private class of HttpCache so 6 // it should only be included by http_cache.cc 7 8 #ifndef NET_HTTP_HTTP_CACHE_TRANSACTION_H_ 9 #define NET_HTTP_HTTP_CACHE_TRANSACTION_H_ 10 11 #include "net/http/http_cache.h" 12 #include "net/http/http_response_info.h" 13 #include "net/http/http_transaction.h" 14 15 namespace net { 16 17 class HttpResponseHeaders; 18 class PartialData; 19 20 // This is the transaction that is returned by the HttpCache transaction 21 // factory. 22 class HttpCache::Transaction : public HttpTransaction { 23 public: 24 Transaction(HttpCache* cache, bool enable_range_support); 25 virtual ~Transaction(); 26 27 // HttpTransaction methods: 28 virtual int Start(const HttpRequestInfo*, CompletionCallback*, LoadLog*); 29 virtual int RestartIgnoringLastError(CompletionCallback* callback); 30 virtual int RestartWithCertificate(X509Certificate* client_cert, 31 CompletionCallback* callback); 32 virtual int RestartWithAuth(const std::wstring& username, 33 const std::wstring& password, 34 CompletionCallback* callback); 35 virtual bool IsReadyToRestartForAuth(); 36 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); 37 virtual const HttpResponseInfo* GetResponseInfo() const; 38 virtual LoadState GetLoadState() const; 39 virtual uint64 GetUploadProgress(void) const; 40 41 // The transaction has the following modes, which apply to how it may access 42 // its cache entry. 43 // 44 // o If the mode of the transaction is NONE, then it is in "pass through" 45 // mode and all methods just forward to the inner network transaction. 46 // 47 // o If the mode of the transaction is only READ, then it may only read from 48 // the cache entry. 49 // 50 // o If the mode of the transaction is only WRITE, then it may only write to 51 // the cache entry. 52 // 53 // o If the mode of the transaction is READ_WRITE, then the transaction may 54 // optionally modify the cache entry (e.g., possibly corresponding to 55 // cache validation). 56 // 57 // o If the mode of the transaction is UPDATE, then the transaction may 58 // update existing cache entries, but will never create a new entry or 59 // respond using the entry read from the cache. 60 enum Mode { 61 NONE = 0, 62 READ_META = 1 << 0, 63 READ_DATA = 1 << 1, 64 READ = READ_META | READ_DATA, 65 WRITE = 1 << 2, 66 READ_WRITE = READ | WRITE, 67 UPDATE = READ_META | WRITE, // READ_WRITE & ~READ_DATA 68 }; 69 70 Mode mode() const { return mode_; } 71 72 const std::string& key() const { return cache_key_; } 73 74 // Associates this transaction with a cache entry. 75 int AddToEntry(); 76 77 // Called by the HttpCache when the given disk cache entry becomes accessible 78 // to the transaction. Returns network error code. 79 int EntryAvailable(ActiveEntry* entry); 80 81 // This transaction is being deleted and we are not done writing to the cache. 82 // We need to indicate that the response data was truncated. Returns true on 83 // success. 84 bool AddTruncatedFlag(); 85 86 private: 87 static const size_t kNumValidationHeaders = 2; 88 // Helper struct to pair a header name with its value, for 89 // headers used to validate cache entries. 90 struct ValidationHeaders { 91 ValidationHeaders() : initialized(false) {} 92 93 std::string values[kNumValidationHeaders]; 94 bool initialized; 95 }; 96 97 enum State { 98 STATE_NONE, 99 STATE_SEND_REQUEST, 100 STATE_SEND_REQUEST_COMPLETE, 101 STATE_SUCCESSFUL_SEND_REQUEST, 102 STATE_NETWORK_READ, 103 STATE_NETWORK_READ_COMPLETE, 104 STATE_INIT_ENTRY, 105 STATE_OPEN_ENTRY, 106 STATE_OPEN_ENTRY_COMPLETE, 107 STATE_CREATE_ENTRY, 108 STATE_CREATE_ENTRY_COMPLETE, 109 STATE_DOOM_ENTRY, 110 STATE_DOOM_ENTRY_COMPLETE, 111 STATE_ADD_TO_ENTRY, 112 STATE_ENTRY_AVAILABLE, 113 STATE_PARTIAL_CACHE_VALIDATION, 114 STATE_UPDATE_CACHED_RESPONSE, 115 STATE_UPDATE_CACHED_RESPONSE_COMPLETE, 116 STATE_OVERWRITE_CACHED_RESPONSE, 117 STATE_TRUNCATE_CACHED_DATA, 118 STATE_TRUNCATE_CACHED_DATA_COMPLETE, 119 STATE_PARTIAL_HEADERS_RECEIVED, 120 STATE_CACHE_READ_RESPONSE, 121 STATE_CACHE_READ_RESPONSE_COMPLETE, 122 STATE_CACHE_WRITE_RESPONSE, 123 STATE_CACHE_WRITE_TRUNCATED_RESPONSE, 124 STATE_CACHE_WRITE_RESPONSE_COMPLETE, 125 STATE_CACHE_QUERY_DATA, 126 STATE_CACHE_QUERY_DATA_COMPLETE, 127 STATE_CACHE_READ_DATA, 128 STATE_CACHE_READ_DATA_COMPLETE, 129 STATE_CACHE_WRITE_DATA, 130 STATE_CACHE_WRITE_DATA_COMPLETE 131 }; 132 133 // This is a helper function used to trigger a completion callback. It may 134 // only be called if callback_ is non-null. 135 void DoCallback(int rv); 136 137 // This will trigger the completion callback if appropriate. 138 int HandleResult(int rv); 139 140 // Runs the state transition loop. 141 int DoLoop(int result); 142 143 // Each of these methods corresponds to a State value. If there is an 144 // argument, the value corresponds to the return of the previous state or 145 // corresponding callback. 146 int DoSendRequest(); 147 int DoSendRequestComplete(int result); 148 int DoSuccessfulSendRequest(); 149 int DoNetworkRead(); 150 int DoNetworkReadComplete(int result); 151 int DoInitEntry(); 152 int DoOpenEntry(); 153 int DoOpenEntryComplete(int result); 154 int DoCreateEntry(); 155 int DoCreateEntryComplete(int result); 156 int DoDoomEntry(); 157 int DoDoomEntryComplete(int result); 158 int DoAddToEntry(); 159 int DoEntryAvailable(); 160 int DoPartialCacheValidation(); 161 int DoUpdateCachedResponse(); 162 int DoUpdateCachedResponseComplete(int result); 163 int DoOverwriteCachedResponse(); 164 int DoTruncateCachedData(); 165 int DoTruncateCachedDataComplete(int result); 166 int DoPartialHeadersReceived(); 167 int DoCacheReadResponse(); 168 int DoCacheReadResponseComplete(int result); 169 int DoCacheWriteResponse(); 170 int DoCacheWriteTruncatedResponse(); 171 int DoCacheWriteResponseComplete(int result); 172 int DoCacheQueryData(); 173 int DoCacheQueryDataComplete(int result); 174 int DoCacheReadData(); 175 int DoCacheReadDataComplete(int result); 176 int DoCacheWriteData(int num_bytes); 177 int DoCacheWriteDataComplete(int result); 178 179 // Sets request_ and fields derived from it. 180 void SetRequest(LoadLog* load_log, const HttpRequestInfo* request); 181 182 // Returns true if the request should be handled exclusively by the network 183 // layer (skipping the cache entirely). 184 bool ShouldPassThrough(); 185 186 // Called to begin reading from the cache. Returns network error code. 187 int BeginCacheRead(); 188 189 // Called to begin validating the cache entry. Returns network error code. 190 int BeginCacheValidation(); 191 192 // Called to begin validating an entry that stores partial content. Returns 193 // a network error code. 194 int BeginPartialCacheValidation(); 195 196 // Validates the entry headers against the requested range and continues with 197 // the validation of the rest of the entry. Returns a network error code. 198 int ValidateEntryHeadersAndContinue(bool byte_range_requested); 199 200 // Called to start requests which were given an "if-modified-since" or 201 // "if-none-match" validation header by the caller (NOT when the request was 202 // conditionalized internally in response to LOAD_VALIDATE_CACHE). 203 // Returns a network error code. 204 int BeginExternallyConditionalizedRequest(); 205 206 // Called to begin a network transaction. Returns network error code. 207 int BeginNetworkRequest(); 208 209 // Called to restart a network transaction after an error. Returns network 210 // error code. 211 int RestartNetworkRequest(); 212 213 // Called to restart a network transaction with a client certificate. 214 // Returns network error code. 215 int RestartNetworkRequestWithCertificate(X509Certificate* client_cert); 216 217 // Called to restart a network transaction with authentication credentials. 218 // Returns network error code. 219 int RestartNetworkRequestWithAuth(const std::wstring& username, 220 const std::wstring& password); 221 222 // Called to determine if we need to validate the cache entry before using it. 223 bool RequiresValidation(); 224 225 // Called to make the request conditional (to ask the server if the cached 226 // copy is valid). Returns true if able to make the request conditional. 227 bool ConditionalizeRequest(); 228 229 // Makes sure that a 206 response is expected. Returns true on success. 230 // On success, |partial_content| will be set to true if we are processing a 231 // partial entry. 232 bool ValidatePartialResponse(const HttpResponseHeaders* headers, 233 bool* partial_content); 234 235 // Handles a response validation error by bypassing the cache. 236 void IgnoreRangeRequest(); 237 238 // Reads data from the network. 239 int ReadFromNetwork(IOBuffer* data, int data_len); 240 241 // Reads data from the cache entry. 242 int ReadFromEntry(IOBuffer* data, int data_len); 243 244 // Called to write data to the cache entry. If the write fails, then the 245 // cache entry is destroyed. Future calls to this function will just do 246 // nothing without side-effect. Returns a network error code. 247 int WriteToEntry(int index, int offset, IOBuffer* data, int data_len, 248 CompletionCallback* callback); 249 250 // Called to write response_ to the cache entry. |truncated| indicates if the 251 // entry should be marked as incomplete. 252 int WriteResponseInfoToEntry(bool truncated); 253 254 // Called to append response data to the cache entry. Returns a network error 255 // code. 256 int AppendResponseDataToEntry(IOBuffer* data, int data_len, 257 CompletionCallback* callback); 258 259 // Called when we are done writing to the cache entry. 260 void DoneWritingToEntry(bool success); 261 262 // Deletes the current partial cache entry (sparse), and optionally removes 263 // the control object (partial_). 264 void DoomPartialEntry(bool delete_object); 265 266 // Performs the needed work after receiving data from the network, when 267 // working with range requests. 268 int DoPartialNetworkReadCompleted(int result); 269 270 // Performs the needed work after receiving data from the cache, when 271 // working with range requests. 272 int DoPartialCacheReadCompleted(int result); 273 274 // Performs the needed work after writing data to the cache. 275 int DoCacheWriteCompleted(int result); 276 277 // Sends a histogram with info about the response headers. 278 void HistogramHeaders(const HttpResponseHeaders* headers); 279 280 // Called to signal completion of asynchronous IO. 281 void OnIOComplete(int result); 282 283 State next_state_; 284 const HttpRequestInfo* request_; 285 scoped_refptr<LoadLog> load_log_; 286 scoped_ptr<HttpRequestInfo> custom_request_; 287 // If extra_headers specified a "if-modified-since" or "if-none-match", 288 // |external_validation_| contains the value of those headers. 289 ValidationHeaders external_validation_; 290 base::WeakPtr<HttpCache> cache_; 291 HttpCache::ActiveEntry* entry_; 292 HttpCache::ActiveEntry* new_entry_; 293 scoped_ptr<HttpTransaction> network_trans_; 294 CompletionCallback* callback_; // Consumer's callback. 295 HttpResponseInfo response_; 296 HttpResponseInfo auth_response_; 297 const HttpResponseInfo* new_response_; 298 std::string cache_key_; 299 Mode mode_; 300 State target_state_; 301 bool reading_; // We are already reading. 302 bool invalid_range_; // We may bypass the cache for this request. 303 bool enable_range_support_; 304 bool truncated_; // We don't have all the response data. 305 bool server_responded_206_; 306 bool cache_pending_; // We are waiting for the HttpCache. 307 scoped_refptr<IOBuffer> read_buf_; 308 int io_buf_len_; 309 int read_offset_; 310 int effective_load_flags_; 311 scoped_ptr<PartialData> partial_; // We are dealing with range requests. 312 uint64 final_upload_progress_; 313 CompletionCallbackImpl<Transaction> io_callback_; 314 scoped_refptr<CancelableCompletionCallback<Transaction> > cache_callback_; 315 scoped_refptr<CancelableCompletionCallback<Transaction> > 316 write_headers_callback_; 317 }; 318 319 } // namespace net 320 321 #endif // NET_HTTP_HTTP_CACHE_TRANSACTION_H_ 322