1 // Copyright (c) 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 #ifndef NET_HTTP_PARTIAL_DATA_H_ 6 #define NET_HTTP_PARTIAL_DATA_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "net/base/completion_callback.h" 12 #include "net/http/http_byte_range.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 : range_present_(false), final_range_(false), sparse_entry_(true), 36 truncated_(false) {} 37 ~PartialData() {} 38 39 // Performs initialization of the object by parsing the request |headers| 40 // and verifying that we can process the requested range. Returns true if 41 // we can process the requested range, and false otherwise. 42 bool Init(const std::string& headers); 43 44 // Sets the headers that we should use to make byte range requests. This is a 45 // subset of the request extra headers, with byte-range related headers 46 // removed. 47 void SetHeaders(const std::string& headers); 48 49 // Restores the byte-range headers, by appending the byte range to the headers 50 // provided to SetHeaders(). 51 void RestoreHeaders(std::string* headers) const; 52 53 // Builds the required |headers| to perform the proper cache validation for 54 // the next range to be fetched. Returns 0 when there is no need to perform 55 // more operations because we reached the end of the request (so 0 bytes 56 // should be actually returned to the user), a positive number to indicate 57 // that |headers| should be used to validate the cache, or an appropriate 58 // error code. 59 int PrepareCacheValidation(disk_cache::Entry* entry, std::string* headers); 60 61 // Returns true if the current range is stored in the cache. 62 bool IsCurrentRangeCached() const; 63 64 // Returns true if the current range is the last one needed to fulfill the 65 // user's request. 66 bool IsLastRange() const; 67 68 // Extracts info from headers already stored in the cache. Returns false if 69 // there is any problem with the headers. |truncated| should be true if we 70 // have an incomplete 200 entry. 71 bool UpdateFromStoredHeaders(const HttpResponseHeaders* headers, 72 disk_cache::Entry* entry, bool truncated); 73 74 // Returns true if the requested range is valid given the stored data. 75 bool IsRequestedRangeOK(); 76 77 // Returns true if the response headers match what we expect, false otherwise. 78 bool ResponseHeadersOK(const HttpResponseHeaders* headers); 79 80 // Fixes the response headers to include the right content length and range. 81 void FixResponseHeaders(HttpResponseHeaders* headers); 82 83 // Fixes the content length that we want to store in the cache. 84 void FixContentLength(HttpResponseHeaders* headers); 85 86 // Reads up to |data_len| bytes from the cache and stores them in the provided 87 // buffer (|data|). Basically, this is just a wrapper around the API of the 88 // cache that provides the right arguments for the current range. When the IO 89 // operation completes, OnCacheReadCompleted() must be called with the result 90 // of the operation. 91 int CacheRead(disk_cache::Entry* entry, IOBuffer* data, int data_len, 92 CompletionCallback* callback); 93 94 // Writes |data_len| bytes to cache. This is basically a wrapper around the 95 // API of the cache that provides the right arguments for the current range. 96 int CacheWrite(disk_cache::Entry* entry, IOBuffer* data, int data_len, 97 CompletionCallback* callback); 98 99 // This method should be called when CacheRead() finishes the read, to update 100 // the internal state about the current range. 101 void OnCacheReadCompleted(int result); 102 103 // This method should be called after receiving data from the network, to 104 // update the internal state about the current range. 105 void OnNetworkReadCompleted(int result); 106 107 private: 108 static void AddRangeHeader(int64 start, int64 end, std::string* headers); 109 110 int64 current_range_start_; 111 int64 cached_start_; 112 int64 resource_size_; 113 int cached_min_len_; 114 HttpByteRange byte_range_; // The range requested by the user. 115 std::string extra_headers_; // The clean set of extra headers (no ranges). 116 bool range_present_; // True if next range entry is already stored. 117 bool final_range_; 118 bool sparse_entry_; 119 bool truncated_; // We have an incomplete 200 stored. 120 121 DISALLOW_COPY_AND_ASSIGN(PartialData); 122 }; 123 124 } // namespace net 125 126 #endif // NET_HTTP_PARTIAL_DATA_H_ 127