partial_data.h 6.9 KB

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