• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2010 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "update_engine/common/multi_range_http_fetcher.h"
18 
19 #include <base/strings/stringprintf.h>
20 
21 #include <algorithm>
22 #include <string>
23 
24 #include "update_engine/common/utils.h"
25 
26 namespace chromeos_update_engine {
27 
28 // Begins the transfer to the specified URL.
29 // State change: Stopped -> Downloading
30 // (corner case: Stopped -> Stopped for an empty request)
BeginTransfer(const std::string & url)31 void MultiRangeHttpFetcher::BeginTransfer(const std::string& url) {
32   CHECK(!base_fetcher_active_) << "BeginTransfer but already active.";
33   CHECK(!pending_transfer_ended_) << "BeginTransfer but pending.";
34   CHECK(!terminating_) << "BeginTransfer but terminating.";
35 
36   if (ranges_.empty()) {
37     // Note that after the callback returns this object may be destroyed.
38     if (delegate_)
39       delegate_->TransferComplete(this, true);
40     return;
41   }
42   url_ = url;
43   current_index_ = 0;
44   bytes_received_this_range_ = 0;
45   LOG(INFO) << "starting first transfer";
46   base_fetcher_->set_delegate(this);
47   StartTransfer();
48 }
49 
50 // State change: Downloading -> Pending transfer ended
TerminateTransfer()51 void MultiRangeHttpFetcher::TerminateTransfer() {
52   if (!base_fetcher_active_) {
53     LOG(INFO) << "Called TerminateTransfer but not active.";
54     // Note that after the callback returns this object may be destroyed.
55     if (delegate_)
56       delegate_->TransferTerminated(this);
57     return;
58   }
59   terminating_ = true;
60 
61   if (!pending_transfer_ended_) {
62     pending_transfer_ended_ = true;
63     base_fetcher_->TerminateTransfer();
64   }
65 }
66 
67 // State change: Stopped or Downloading -> Downloading
StartTransfer()68 void MultiRangeHttpFetcher::StartTransfer() {
69   if (current_index_ >= ranges_.size()) {
70     return;
71   }
72 
73   Range range = ranges_[current_index_];
74   LOG(INFO) << "starting transfer of range " << range.ToString();
75 
76   bytes_received_this_range_ = 0;
77   base_fetcher_->SetOffset(range.offset());
78   if (range.HasLength())
79     base_fetcher_->SetLength(range.length());
80   else
81     base_fetcher_->UnsetLength();
82   if (delegate_)
83     delegate_->SeekToOffset(range.offset());
84   base_fetcher_active_ = true;
85   base_fetcher_->BeginTransfer(url_);
86 }
87 
88 // State change: Downloading -> Downloading or Pending transfer ended
ReceivedBytes(HttpFetcher * fetcher,const void * bytes,size_t length)89 bool MultiRangeHttpFetcher::ReceivedBytes(HttpFetcher* fetcher,
90                                           const void* bytes,
91                                           size_t length) {
92   CHECK_LT(current_index_, ranges_.size());
93   CHECK_EQ(fetcher, base_fetcher_.get());
94   CHECK(!pending_transfer_ended_);
95   size_t next_size = length;
96   Range range = ranges_[current_index_];
97   if (range.HasLength()) {
98     next_size =
99         std::min(next_size, range.length() - bytes_received_this_range_);
100   }
101   LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0";
102   // bytes_received_this_range_ needs to be updated regardless of the delegate_
103   // result, because it will be used to determine a successful transfer in
104   // TransferEnded().
105   bytes_received_this_range_ += length;
106   if (delegate_ && !delegate_->ReceivedBytes(this, bytes, next_size))
107     return false;
108 
109   if (range.HasLength() && bytes_received_this_range_ >= range.length()) {
110     // Terminates the current fetcher. Waits for its TransferTerminated
111     // callback before starting the next range so that we don't end up
112     // signalling the delegate that the whole multi-transfer is complete
113     // before all fetchers are really done and cleaned up.
114     pending_transfer_ended_ = true;
115     LOG(INFO) << "Terminating transfer.";
116     fetcher->TerminateTransfer();
117     return false;
118   }
119   return true;
120 }
121 
122 // State change: Downloading or Pending transfer ended -> Stopped
TransferEnded(HttpFetcher * fetcher,bool successful)123 void MultiRangeHttpFetcher::TransferEnded(HttpFetcher* fetcher,
124                                           bool successful) {
125   CHECK(base_fetcher_active_) << "Transfer ended unexpectedly.";
126   CHECK_EQ(fetcher, base_fetcher_.get());
127   pending_transfer_ended_ = false;
128   http_response_code_ = fetcher->http_response_code();
129   LOG(INFO) << "TransferEnded w/ code " << http_response_code_;
130   if (terminating_) {
131     LOG(INFO) << "Terminating.";
132     Reset();
133     // Note that after the callback returns this object may be destroyed.
134     if (delegate_)
135       delegate_->TransferTerminated(this);
136     return;
137   }
138 
139   // If we didn't get enough bytes, it's failure
140   Range range = ranges_[current_index_];
141   if (range.HasLength()) {
142     if (bytes_received_this_range_ < range.length()) {
143       // Failure
144       LOG(INFO) << "Didn't get enough bytes. Ending w/ failure.";
145       Reset();
146       // Note that after the callback returns this object may be destroyed.
147       if (delegate_)
148         delegate_->TransferComplete(this, false);
149       return;
150     }
151     // We got enough bytes and there were bytes specified, so this is success.
152     successful = true;
153   }
154 
155   // If we have another transfer, do that.
156   if (current_index_ + 1 < ranges_.size()) {
157     current_index_++;
158     LOG(INFO) << "Starting next transfer (" << current_index_ << ").";
159     StartTransfer();
160     return;
161   }
162 
163   LOG(INFO) << "Done w/ all transfers";
164   Reset();
165   // Note that after the callback returns this object may be destroyed.
166   if (delegate_)
167     delegate_->TransferComplete(this, successful);
168 }
169 
TransferComplete(HttpFetcher * fetcher,bool successful)170 void MultiRangeHttpFetcher::TransferComplete(HttpFetcher* fetcher,
171                                              bool successful) {
172   LOG(INFO) << "Received transfer complete.";
173   TransferEnded(fetcher, successful);
174 }
175 
TransferTerminated(HttpFetcher * fetcher)176 void MultiRangeHttpFetcher::TransferTerminated(HttpFetcher* fetcher) {
177   LOG(INFO) << "Received transfer terminated.";
178   TransferEnded(fetcher, false);
179 }
180 
Reset()181 void MultiRangeHttpFetcher::Reset() {
182   base_fetcher_active_ = pending_transfer_ended_ = terminating_ = false;
183   current_index_ = 0;
184   bytes_received_this_range_ = 0;
185 }
186 
ToString() const187 std::string MultiRangeHttpFetcher::Range::ToString() const {
188   std::string range_str = base::StringPrintf("%jd+", offset());
189   if (HasLength())
190     range_str += std::to_string(length());
191   else
192     range_str += "?";
193   return range_str;
194 }
195 
SetOffset(off_t offset)196 void MultiRangeHttpFetcher::SetOffset(off_t offset) {
197   current_index_ = 0;
198   for (const Range& range : ranges_) {
199     if (!range.HasLength() || static_cast<size_t>(offset) < range.length()) {
200       bytes_received_this_range_ = offset;
201 
202       base_fetcher_->SetOffset(range.offset() + offset);
203       if (range.HasLength())
204         base_fetcher_->SetLength(range.length());
205       else
206         base_fetcher_->UnsetLength();
207       if (delegate_)
208         delegate_->SeekToOffset(range.offset() + offset);
209       return;
210     }
211     current_index_++;
212     offset -= range.length();
213   }
214   if (offset > 0) {
215     LOG(ERROR) << "Offset too large.";
216   }
217 }
218 
219 }  // namespace chromeos_update_engine
220