blob: 18b2aefac77e0a00f25092c1fe1991165d70448b [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Andrew de los Reyes819fef22010-12-17 11:33:58 -080016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/multi_range_http_fetcher.h"
18
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/strings/stringprintf.h>
Andrew de los Reyes819fef22010-12-17 11:33:58 -080020
Alex Vakulenkod2779df2014-06-16 13:19:00 -070021#include <algorithm>
22#include <string>
23
Andrew de los Reyes819fef22010-12-17 11:33:58 -080024#include "update_engine/utils.h"
25
26namespace 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)
Gilad Arnold9bedeb52011-11-17 16:19:57 -080031void MultiRangeHttpFetcher::BeginTransfer(const std::string& url) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -080032 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
Gilad Arnold9bedeb52011-11-17 16:19:57 -080051void MultiRangeHttpFetcher::TerminateTransfer() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -080052 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;
Alex Vakulenkod2779df2014-06-16 13:19:00 -070060
Andrew de los Reyes819fef22010-12-17 11:33:58 -080061 if (!pending_transfer_ended_) {
62 base_fetcher_->TerminateTransfer();
63 }
64}
65
66// State change: Stopped or Downloading -> Downloading
Gilad Arnold9bedeb52011-11-17 16:19:57 -080067void MultiRangeHttpFetcher::StartTransfer() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -080068 if (current_index_ >= ranges_.size()) {
69 return;
70 }
Gilad Arnolde4ad2502011-12-29 17:08:54 -080071
72 Range range = ranges_[current_index_];
73 LOG(INFO) << "starting transfer of range " << range.ToString();
74
Andrew de los Reyes819fef22010-12-17 11:33:58 -080075 bytes_received_this_range_ = 0;
Gilad Arnolde4ad2502011-12-29 17:08:54 -080076 base_fetcher_->SetOffset(range.offset());
77 if (range.HasLength())
78 base_fetcher_->SetLength(range.length());
79 else
80 base_fetcher_->UnsetLength();
Andrew de los Reyes819fef22010-12-17 11:33:58 -080081 if (delegate_)
Gilad Arnolde4ad2502011-12-29 17:08:54 -080082 delegate_->SeekToOffset(range.offset());
Andrew de los Reyes819fef22010-12-17 11:33:58 -080083 base_fetcher_active_ = true;
84 base_fetcher_->BeginTransfer(url_);
85}
86
87// State change: Downloading -> Downloading or Pending transfer ended
Gilad Arnold9bedeb52011-11-17 16:19:57 -080088void MultiRangeHttpFetcher::ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080089 const void* bytes,
90 size_t length) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -080091 CHECK_LT(current_index_, ranges_.size());
92 CHECK_EQ(fetcher, base_fetcher_.get());
93 CHECK(!pending_transfer_ended_);
Gilad Arnolde4ad2502011-12-29 17:08:54 -080094 size_t next_size = length;
95 Range range = ranges_[current_index_];
96 if (range.HasLength()) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -080097 next_size = std::min(next_size,
Gilad Arnolde4ad2502011-12-29 17:08:54 -080098 range.length() - bytes_received_this_range_);
Andrew de los Reyes819fef22010-12-17 11:33:58 -080099 }
100 LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0";
101 if (delegate_) {
102 delegate_->ReceivedBytes(this, bytes, next_size);
103 }
104 bytes_received_this_range_ += length;
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800105 if (range.HasLength() && bytes_received_this_range_ >= range.length()) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800106 // Terminates the current fetcher. Waits for its TransferTerminated
107 // callback before starting the next range so that we don't end up
108 // signalling the delegate that the whole multi-transfer is complete
109 // before all fetchers are really done and cleaned up.
110 pending_transfer_ended_ = true;
111 LOG(INFO) << "terminating transfer";
112 fetcher->TerminateTransfer();
113 }
114}
115
116// State change: Downloading or Pending transfer ended -> Stopped
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800117void MultiRangeHttpFetcher::TransferEnded(HttpFetcher* fetcher,
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800118 bool successful) {
119 CHECK(base_fetcher_active_) << "Transfer ended unexpectedly.";
120 CHECK_EQ(fetcher, base_fetcher_.get());
121 pending_transfer_ended_ = false;
122 http_response_code_ = fetcher->http_response_code();
123 LOG(INFO) << "TransferEnded w/ code " << http_response_code_;
124 if (terminating_) {
125 LOG(INFO) << "Terminating.";
126 Reset();
127 // Note that after the callback returns this object may be destroyed.
128 if (delegate_)
129 delegate_->TransferTerminated(this);
130 return;
131 }
132
133 // If we didn't get enough bytes, it's failure
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800134 Range range = ranges_[current_index_];
135 if (range.HasLength()) {
136 if (bytes_received_this_range_ < range.length()) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800137 // Failure
138 LOG(INFO) << "Didn't get enough bytes. Ending w/ failure.";
139 Reset();
140 // Note that after the callback returns this object may be destroyed.
141 if (delegate_)
142 delegate_->TransferComplete(this, false);
143 return;
144 }
145 // We got enough bytes and there were bytes specified, so this is success.
146 successful = true;
147 }
148
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -0700149 // If we have another transfer, do that.
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800150 if (current_index_ + 1 < ranges_.size()) {
151 current_index_++;
152 LOG(INFO) << "Starting next transfer (" << current_index_ << ").";
153 StartTransfer();
154 return;
155 }
156
157 LOG(INFO) << "Done w/ all transfers";
158 Reset();
159 // Note that after the callback returns this object may be destroyed.
160 if (delegate_)
161 delegate_->TransferComplete(this, successful);
162}
163
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800164void MultiRangeHttpFetcher::TransferComplete(HttpFetcher* fetcher,
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800165 bool successful) {
166 LOG(INFO) << "Received transfer complete.";
167 TransferEnded(fetcher, successful);
168}
169
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800170void MultiRangeHttpFetcher::TransferTerminated(HttpFetcher* fetcher) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800171 LOG(INFO) << "Received transfer terminated.";
172 TransferEnded(fetcher, false);
173}
174
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800175void MultiRangeHttpFetcher::Reset() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800176 base_fetcher_active_ = pending_transfer_ended_ = terminating_ = false;
177 current_index_ = 0;
178 bytes_received_this_range_ = 0;
179}
180
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800181std::string MultiRangeHttpFetcher::Range::ToString() const {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700182 std::string range_str = base::StringPrintf("%jd+", offset());
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800183 if (HasLength())
Alex Deymoc00c98a2015-03-17 17:38:00 -0700184 range_str += std::to_string(length());
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800185 else
Alex Deymoc00c98a2015-03-17 17:38:00 -0700186 range_str += "?";
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800187 return range_str;
188}
189
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800190} // namespace chromeos_update_engine