1 // Copyright (c) 2010 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 #ifndef NET_HTTP_PARTIAL_DATA_H_ 6 #define NET_HTTP_PARTIAL_DATA_H_ 7 #pragma once 8 9 #include "base/basictypes.h" 10 #include "net/base/completion_callback.h" 11 #include "net/http/http_byte_range.h" 12 #include "net/http/http_request_headers.h" 13 14 namespace disk_cache { 15 class Entry; 16 } 17 18 namespace net { 19 20 class HttpResponseHeaders; 21 class IOBuffer; 22 23 // This class provides support for dealing with range requests and the 24 // subsequent partial-content responses. We use sparse cache entries to store 25 // these requests. This class is tightly integrated with HttpCache::Transaction 26 // and it is intended to allow a cleaner implementation of that class. 27 // 28 // In order to fulfill range requests, we may have to perform a sequence of 29 // reads from the cache, interleaved with reads from the network / writes to the 30 // cache. This class basically keeps track of the data required to perform each 31 // of those individual network / cache requests. 32 class PartialData { 33 public: 34 PartialData(); 35 ~PartialData(); 36 37 // Performs initialization of the object by examining the request |headers| 38 // and verifying that we can process the requested range. Returns true if 39 // we can process the requested range, and false otherwise. 40 bool Init(const HttpRequestHeaders& headers); 41 42 // Sets the headers that we should use to make byte range requests. This is a 43 // subset of the request extra headers, with byte-range related headers 44 // removed. 45 void SetHeaders(const HttpRequestHeaders& headers); 46 47 // Restores the byte-range headers, by appending the byte range to the headers 48 // provided to SetHeaders(). 49 void RestoreHeaders(HttpRequestHeaders* headers) const; 50 51 // Starts the checks to perform a cache validation. Returns 0 when there is no 52 // need to perform more operations because we reached the end of the request 53 // (so 0 bytes should be actually returned to the user), a positive number to 54 // indicate that PrepareCacheValidation should be called, or an appropriate 55 // error code. If this method returns ERR_IO_PENDING, the |callback| will be 56 // notified when the result is ready. 57 int ShouldValidateCache(disk_cache::Entry* entry, 58 CompletionCallback* callback); 59 60 // Builds the required |headers| to perform the proper cache validation for 61 // the next range to be fetched. 62 void PrepareCacheValidation(disk_cache::Entry* entry, 63 HttpRequestHeaders* headers); 64 65 // Returns true if the current range is stored in the cache. 66 bool IsCurrentRangeCached() const; 67 68 // Returns true if the current range is the last one needed to fulfill the 69 // user's request. 70 bool IsLastRange() const; 71 72 // Extracts info from headers already stored in the cache. Returns false if 73 // there is any problem with the headers. |truncated| should be true if we 74 // have an incomplete 200 entry. 75 bool UpdateFromStoredHeaders(const HttpResponseHeaders* headers, 76 disk_cache::Entry* entry, bool truncated); 77 78 // Sets the byte current range to start again at zero (for a truncated entry). 79 void SetRangeToStartDownload(); 80 81 // Returns true if the requested range is valid given the stored data. 82 bool IsRequestedRangeOK(); 83 84 // Returns true if the response headers match what we expect, false otherwise. 85 bool ResponseHeadersOK(const HttpResponseHeaders* headers); 86 87 // Fixes the response headers to include the right content length and range. 88 // |success| is the result of the whole request so if it's false, we'll change 89 // the result code to be 416. 90 void FixResponseHeaders(HttpResponseHeaders* headers, bool success); 91 92 // Fixes the content length that we want to store in the cache. 93 void FixContentLength(HttpResponseHeaders* headers); 94 95 // Reads up to |data_len| bytes from the cache and stores them in the provided 96 // buffer (|data|). Basically, this is just a wrapper around the API of the 97 // cache that provides the right arguments for the current range. When the IO 98 // operation completes, OnCacheReadCompleted() must be called with the result 99 // of the operation. 100 int CacheRead(disk_cache::Entry* entry, IOBuffer* data, int data_len, 101 CompletionCallback* callback); 102 103 // Writes |data_len| bytes to cache. This is basically a wrapper around the 104 // API of the cache that provides the right arguments for the current range. 105 int CacheWrite(disk_cache::Entry* entry, IOBuffer* data, int data_len, 106 CompletionCallback* callback); 107 108 // This method should be called when CacheRead() finishes the read, to update 109 // the internal state about the current range. 110 void OnCacheReadCompleted(int result); 111 112 // This method should be called after receiving data from the network, to 113 // update the internal state about the current range. 114 void OnNetworkReadCompleted(int result); 115 initial_validation()116 bool initial_validation() const { return initial_validation_; } 117 118 private: 119 class Core; 120 // Returns the length to use when scanning the cache. 121 int GetNextRangeLen(); 122 123 // Completion routine for our callback. 124 void GetAvailableRangeCompleted(int result, int64 start); 125 126 int64 current_range_start_; 127 int64 cached_start_; 128 int64 resource_size_; 129 int cached_min_len_; 130 HttpByteRange byte_range_; // The range requested by the user. 131 // The clean set of extra headers (no ranges). 132 HttpRequestHeaders extra_headers_; 133 bool range_present_; // True if next range entry is already stored. 134 bool final_range_; 135 bool sparse_entry_; 136 bool truncated_; // We have an incomplete 200 stored. 137 bool initial_validation_; // Only used for truncated entries. 138 Core* core_; 139 CompletionCallback* callback_; 140 141 DISALLOW_COPY_AND_ASSIGN(PartialData); 142 }; 143 144 } // namespace net 145 146 #endif // NET_HTTP_PARTIAL_DATA_H_ 147