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 void 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 = std::min(next_size,
99                          range.length() - bytes_received_this_range_);
100   }
101   LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0";
102   if (delegate_) {
103     delegate_->ReceivedBytes(this, bytes, next_size);
104   }
105   bytes_received_this_range_ += length;
106   if (range.HasLength() && bytes_received_this_range_ >= range.length()) {
107     // Terminates the current fetcher. Waits for its TransferTerminated
108     // callback before starting the next range so that we don't end up
109     // signalling the delegate that the whole multi-transfer is complete
110     // before all fetchers are really done and cleaned up.
111     pending_transfer_ended_ = true;
112     LOG(INFO) << "terminating transfer";
113     fetcher->TerminateTransfer();
114   }
115 }
116 
117 // State change: Downloading or Pending transfer ended -> Stopped
TransferEnded(HttpFetcher * fetcher,bool successful)118 void MultiRangeHttpFetcher::TransferEnded(HttpFetcher* fetcher,
119                                           bool successful) {
120   CHECK(base_fetcher_active_) << "Transfer ended unexpectedly.";
121   CHECK_EQ(fetcher, base_fetcher_.get());
122   pending_transfer_ended_ = false;
123   http_response_code_ = fetcher->http_response_code();
124   LOG(INFO) << "TransferEnded w/ code " << http_response_code_;
125   if (terminating_) {
126     LOG(INFO) << "Terminating.";
127     Reset();
128     // Note that after the callback returns this object may be destroyed.
129     if (delegate_)
130       delegate_->TransferTerminated(this);
131     return;
132   }
133 
134   // If we didn't get enough bytes, it's failure
135   Range range = ranges_[current_index_];
136   if (range.HasLength()) {
137     if (bytes_received_this_range_ < range.length()) {
138       // Failure
139       LOG(INFO) << "Didn't get enough bytes. Ending w/ failure.";
140       Reset();
141       // Note that after the callback returns this object may be destroyed.
142       if (delegate_)
143         delegate_->TransferComplete(this, false);
144       return;
145     }
146     // We got enough bytes and there were bytes specified, so this is success.
147     successful = true;
148   }
149 
150   // If we have another transfer, do that.
151   if (current_index_ + 1 < ranges_.size()) {
152     current_index_++;
153     LOG(INFO) << "Starting next transfer (" << current_index_ << ").";
154     StartTransfer();
155     return;
156   }
157 
158   LOG(INFO) << "Done w/ all transfers";
159   Reset();
160   // Note that after the callback returns this object may be destroyed.
161   if (delegate_)
162     delegate_->TransferComplete(this, successful);
163 }
164 
TransferComplete(HttpFetcher * fetcher,bool successful)165 void MultiRangeHttpFetcher::TransferComplete(HttpFetcher* fetcher,
166                                              bool successful) {
167   LOG(INFO) << "Received transfer complete.";
168   TransferEnded(fetcher, successful);
169 }
170 
TransferTerminated(HttpFetcher * fetcher)171 void MultiRangeHttpFetcher::TransferTerminated(HttpFetcher* fetcher) {
172   LOG(INFO) << "Received transfer terminated.";
173   TransferEnded(fetcher, false);
174 }
175 
Reset()176 void MultiRangeHttpFetcher::Reset() {
177   base_fetcher_active_ = pending_transfer_ended_ = terminating_ = false;
178   current_index_ = 0;
179   bytes_received_this_range_ = 0;
180 }
181 
ToString() const182 std::string MultiRangeHttpFetcher::Range::ToString() const {
183   std::string range_str = base::StringPrintf("%jd+", offset());
184   if (HasLength())
185     range_str += std::to_string(length());
186   else
187     range_str += "?";
188   return range_str;
189 }
190 
191 }  // namespace chromeos_update_engine
192