| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_HTTP_PARTIAL_DATA_H_ | 5 #ifndef NET_HTTP_PARTIAL_DATA_H_ |
| 6 #define NET_HTTP_PARTIAL_DATA_H_ | 6 #define NET_HTTP_PARTIAL_DATA_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
| 12 #include "net/http/http_byte_range.h" | 12 #include "net/http/http_byte_range.h" |
| 13 #include "net/http/http_request_headers.h" |
| 13 | 14 |
| 14 namespace disk_cache { | 15 namespace disk_cache { |
| 15 class Entry; | 16 class Entry; |
| 16 } | 17 } |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 20 class HttpResponseHeaders; | 21 class HttpResponseHeaders; |
| 21 class IOBuffer; | 22 class IOBuffer; |
| 22 | 23 |
| 23 // This class provides support for dealing with range requests and the | 24 // This class provides support for dealing with range requests and the |
| 24 // subsequent partial-content responses. We use sparse cache entries to store | 25 // subsequent partial-content responses. We use sparse cache entries to store |
| 25 // these requests. This class is tightly integrated with HttpCache::Transaction | 26 // these requests. This class is tightly integrated with HttpCache::Transaction |
| 26 // and it is intended to allow a cleaner implementation of that class. | 27 // and it is intended to allow a cleaner implementation of that class. |
| 27 // | 28 // |
| 28 // In order to fulfill range requests, we may have to perform a sequence of | 29 // 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 // 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 // cache. This class basically keeps track of the data required to perform each |
| 31 // of those individual network / cache requests. | 32 // of those individual network / cache requests. |
| 32 class PartialData { | 33 class PartialData { |
| 33 public: | 34 public: |
| 34 PartialData() | 35 PartialData() |
| 35 : range_present_(false), final_range_(false), sparse_entry_(true), | 36 : range_present_(false), final_range_(false), sparse_entry_(true), |
| 36 truncated_(false) {} | 37 truncated_(false) {} |
| 37 ~PartialData() {} | 38 ~PartialData() {} |
| 38 | 39 |
| 39 // Performs initialization of the object by parsing the request |headers| | 40 // Performs initialization of the object by examining the request |headers| |
| 40 // and verifying that we can process the requested range. Returns true if | 41 // and verifying that we can process the requested range. Returns true if |
| 41 // we can process the requested range, and false otherwise. | 42 // we can process the requested range, and false otherwise. |
| 42 bool Init(const std::string& headers); | 43 bool Init(const HttpRequestHeaders& headers); |
| 43 | 44 |
| 44 // Sets the headers that we should use to make byte range requests. This is a | 45 // 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 // subset of the request extra headers, with byte-range related headers |
| 46 // removed. | 47 // removed. |
| 47 void SetHeaders(const std::string& headers); | 48 void SetHeaders(const HttpRequestHeaders& headers); |
| 48 | 49 |
| 49 // Restores the byte-range headers, by appending the byte range to the headers | 50 // Restores the byte-range headers, by appending the byte range to the headers |
| 50 // provided to SetHeaders(). | 51 // provided to SetHeaders(). |
| 51 void RestoreHeaders(std::string* headers) const; | 52 void RestoreHeaders(HttpRequestHeaders* headers) const; |
| 52 | 53 |
| 53 // Builds the required |headers| to perform the proper cache validation for | 54 // 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 // 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 // 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 // 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 // that |headers| should be used to validate the cache, or an appropriate |
| 58 // error code. | 59 // error code. |
| 59 int PrepareCacheValidation(disk_cache::Entry* entry, std::string* headers); | 60 int PrepareCacheValidation(disk_cache::Entry* entry, |
| 61 HttpRequestHeaders* headers); |
| 60 | 62 |
| 61 // Returns true if the current range is stored in the cache. | 63 // Returns true if the current range is stored in the cache. |
| 62 bool IsCurrentRangeCached() const; | 64 bool IsCurrentRangeCached() const; |
| 63 | 65 |
| 64 // Returns true if the current range is the last one needed to fulfill the | 66 // Returns true if the current range is the last one needed to fulfill the |
| 65 // user's request. | 67 // user's request. |
| 66 bool IsLastRange() const; | 68 bool IsLastRange() const; |
| 67 | 69 |
| 68 // Extracts info from headers already stored in the cache. Returns false if | 70 // 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 | 71 // there is any problem with the headers. |truncated| should be true if we |
| (...skipping 28 matching lines...) Expand all Loading... |
| 98 | 100 |
| 99 // This method should be called when CacheRead() finishes the read, to update | 101 // This method should be called when CacheRead() finishes the read, to update |
| 100 // the internal state about the current range. | 102 // the internal state about the current range. |
| 101 void OnCacheReadCompleted(int result); | 103 void OnCacheReadCompleted(int result); |
| 102 | 104 |
| 103 // This method should be called after receiving data from the network, to | 105 // This method should be called after receiving data from the network, to |
| 104 // update the internal state about the current range. | 106 // update the internal state about the current range. |
| 105 void OnNetworkReadCompleted(int result); | 107 void OnNetworkReadCompleted(int result); |
| 106 | 108 |
| 107 private: | 109 private: |
| 108 static void AddRangeHeader(int64 start, int64 end, std::string* headers); | |
| 109 | |
| 110 int64 current_range_start_; | 110 int64 current_range_start_; |
| 111 int64 cached_start_; | 111 int64 cached_start_; |
| 112 int64 resource_size_; | 112 int64 resource_size_; |
| 113 int cached_min_len_; | 113 int cached_min_len_; |
| 114 HttpByteRange byte_range_; // The range requested by the user. | 114 HttpByteRange byte_range_; // The range requested by the user. |
| 115 std::string extra_headers_; // The clean set of extra headers (no ranges). | 115 // The clean set of extra headers (no ranges). |
| 116 HttpRequestHeaders extra_headers_; |
| 116 bool range_present_; // True if next range entry is already stored. | 117 bool range_present_; // True if next range entry is already stored. |
| 117 bool final_range_; | 118 bool final_range_; |
| 118 bool sparse_entry_; | 119 bool sparse_entry_; |
| 119 bool truncated_; // We have an incomplete 200 stored. | 120 bool truncated_; // We have an incomplete 200 stored. |
| 120 | 121 |
| 121 DISALLOW_COPY_AND_ASSIGN(PartialData); | 122 DISALLOW_COPY_AND_ASSIGN(PartialData); |
| 122 }; | 123 }; |
| 123 | 124 |
| 124 } // namespace net | 125 } // namespace net |
| 125 | 126 |
| 126 #endif // NET_HTTP_PARTIAL_DATA_H_ | 127 #endif // NET_HTTP_PARTIAL_DATA_H_ |
| OLD | NEW |