+
+ case MessageType::REDIRECT:
+ {
+ if (Itm == nullptr)
+ {
+ _error->Error("Method gave invalid 103 Redirect message");
+ break;
+ }
+
+ std::string const NewURI = LookupTag(Message,"New-URI",URI.c_str());
+ Itm->URI = NewURI;
+
+ ItemDone();
+
+ // Change the status so that it can be dequeued
+ for (auto const &O: Itm->Owners)
+ O->Status = pkgAcquire::Item::StatIdle;
+ // Mark the item as done (taking care of all queues)
+ // and then put it in the main queue again
+ std::vector<Item*> const ItmOwners = Itm->Owners;
+ OwnerQ->ItemDone(Itm);
+ Itm = nullptr;
+ for (auto const &Owner: ItmOwners)
+ {
+ pkgAcquire::ItemDesc &desc = Owner->GetItemDesc();
+ if (Owner->IsRedirectionLoop(NewURI))
+ {
+ std::string msg = Message;
+ msg.append("\nFailReason: RedirectionLoop");
+ Owner->Failed(msg, Config);
+ if (Log != nullptr)
+ Log->Fail(Owner->GetItemDesc());
+ continue;
+ }
+
+ if (Log != nullptr)
+ Log->Done(desc);
+
+ // if we change site, treat it as a mirror change
+ if (URI::SiteOnly(NewURI) != URI::SiteOnly(desc.URI))
+ {
+ auto const firstSpace = desc.Description.find(" ");
+ if (firstSpace != std::string::npos)
+ {
+ std::string const OldSite = desc.Description.substr(0, firstSpace);
+ if (likely(APT::String::Startswith(desc.URI, OldSite)))
+ {
+ std::string const OldExtra = desc.URI.substr(OldSite.length() + 1);
+ if (likely(APT::String::Endswith(NewURI, OldExtra)))
+ {
+ std::string const NewSite = NewURI.substr(0, NewURI.length() - OldExtra.length());
+ Owner->UsedMirror = URI::ArchiveOnly(NewSite);
+ desc.Description.replace(0, firstSpace, Owner->UsedMirror);
+ }
+ }
+ }
+ }
+ desc.URI = NewURI;
+ if (isDoomedItem(Owner) == false)
+ OwnerQ->Owner->Enqueue(desc);
+ }
+ break;
+ }
+
+ case MessageType::WARNING:
+ _error->Warning("%s: %s", Itm->Owner->DescURI().c_str(), LookupTag(Message,"Message").c_str());
+ break;
+
+ case MessageType::URI_START:
+ {
+ if (Itm == nullptr)
+ {
+ _error->Error("Method gave invalid 200 URI Start message");
+ break;
+ }
+
+ CurrentItem = Itm;
+ CurrentSize = 0;
+ TotalSize = strtoull(LookupTag(Message,"Size","0").c_str(), NULL, 10);
+ ResumePoint = strtoull(LookupTag(Message,"Resume-Point","0").c_str(), NULL, 10);
+ for (auto const Owner: Itm->Owners)
+ {
+ Owner->Start(Message, TotalSize);
+ // Display update before completion
+ if (Log != nullptr)
+ {
+ if (Log->MorePulses == true)
+ Log->Pulse(Owner->GetOwner());
+ Log->Fetch(Owner->GetItemDesc());
+ }
+ }
+
+ break;
+ }
+
+ case MessageType::URI_DONE:
+ {
+ if (Itm == nullptr)
+ {
+ _error->Error("Method gave invalid 201 URI Done message");
+ break;
+ }
+
+ PrepareFiles("201::URIDone", Itm);
+
+ // Display update before completion
+ if (Log != 0 && Log->MorePulses == true)
+ for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
+ Log->Pulse((*O)->GetOwner());
+
+ HashStringList ReceivedHashes;
+ {
+ std::string const givenfilename = LookupTag(Message, "Filename");
+ std::string const filename = givenfilename.empty() ? Itm->Owner->DestFile : givenfilename;
+ // see if we got hashes to verify
+ for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
+ {
+ std::string const tagname = std::string(*type) + "-Hash";
+ std::string const hashsum = LookupTag(Message, tagname.c_str());
+ if (hashsum.empty() == false)
+ ReceivedHashes.push_back(HashString(*type, hashsum));
+ }
+ // not all methods always sent Hashes our way
+ if (ReceivedHashes.usable() == false)
+ {
+ HashStringList const ExpectedHashes = Itm->GetExpectedHashes();
+ if (ExpectedHashes.usable() == true && RealFileExists(filename))
+ {
+ Hashes calc(ExpectedHashes);
+ FileFd file(filename, FileFd::ReadOnly, FileFd::None);
+ calc.AddFD(file);
+ ReceivedHashes = calc.GetHashStringList();
+ }
+ }
+
+ // only local files can refer other filenames and counting them as fetched would be unfair
+ if (Log != NULL && Itm->Owner->Complete == false && Itm->Owner->Local == false && givenfilename == filename)
+ Log->Fetched(ReceivedHashes.FileSize(),atoi(LookupTag(Message,"Resume-Point","0").c_str()));
+ }
+
+ std::vector<Item*> const ItmOwners = Itm->Owners;
+ OwnerQ->ItemDone(Itm);
+ Itm = NULL;
+
+ bool const isIMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false) ||
+ StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false);
+ auto const forcedHash = _config->Find("Acquire::ForceHash");
+ for (auto const Owner: ItmOwners)
+ {
+ HashStringList const ExpectedHashes = Owner->GetExpectedHashes();
+ if(_config->FindB("Debug::pkgAcquire::Auth", false) == true)
+ {
+ std::clog << "201 URI Done: " << Owner->DescURI() << endl
+ << "ReceivedHash:" << endl;
+ for (HashStringList::const_iterator hs = ReceivedHashes.begin(); hs != ReceivedHashes.end(); ++hs)
+ std::clog << "\t- " << hs->toStr() << std::endl;
+ std::clog << "ExpectedHash:" << endl;
+ for (HashStringList::const_iterator hs = ExpectedHashes.begin(); hs != ExpectedHashes.end(); ++hs)
+ std::clog << "\t- " << hs->toStr() << std::endl;
+ std::clog << endl;
+ }
+
+ // decide if what we got is what we expected
+ bool consideredOkay = false;
+ if ((forcedHash.empty() && ExpectedHashes.empty() == false) ||
+ (forcedHash.empty() == false && ExpectedHashes.usable()))
+ {
+ if (ReceivedHashes.empty())
+ {
+ /* IMS-Hits can't be checked here as we will have uncompressed file,
+ but the hashes for the compressed file. What we have was good through
+ so all we have to ensure later is that we are not stalled. */
+ consideredOkay = isIMSHit;
+ }
+ else if (ReceivedHashes == ExpectedHashes)
+ consideredOkay = true;
+ else
+ consideredOkay = false;
+
+ }
+ else
+ consideredOkay = !Owner->HashesRequired();
+
+ if (consideredOkay == true)
+ consideredOkay = Owner->VerifyDone(Message, Config);
+ else // hashsum mismatch
+ Owner->Status = pkgAcquire::Item::StatAuthError;
+
+
+ if (consideredOkay == true)
+ {
+ if (isDoomedItem(Owner) == false)
+ Owner->Done(Message, ReceivedHashes, Config);
+ if (Log != nullptr)
+ {
+ if (isIMSHit)
+ Log->IMSHit(Owner->GetItemDesc());
+ else
+ Log->Done(Owner->GetItemDesc());
+ }
+ }
+ else
+ {
+ if (isDoomedItem(Owner) == false)
+ {
+ if (Message.find("\nFailReason:") == std::string::npos)
+ {
+ if (ReceivedHashes != ExpectedHashes)
+ Message.append("\nFailReason: HashSumMismatch");
+ else
+ Message.append("\nFailReason: WeakHashSums");
+ }
+ Owner->Failed(Message,Config);
+ }
+ if (Log != nullptr)
+ Log->Fail(Owner->GetItemDesc());
+ }
+ }
+ ItemDone();
+ break;
+ }
+
+ case MessageType::URI_FAILURE:
+ {
+ if (Itm == nullptr)
+ {
+ std::string const msg = LookupTag(Message,"Message");
+ _error->Error("Method gave invalid 400 URI Failure message: %s", msg.c_str());
+ break;
+ }
+
+ PrepareFiles("400::URIFailure", Itm);
+
+ // Display update before completion
+ if (Log != nullptr && Log->MorePulses == true)
+ for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
+ Log->Pulse((*O)->GetOwner());
+
+ std::vector<Item*> const ItmOwners = Itm->Owners;
+ OwnerQ->ItemDone(Itm);
+ Itm = nullptr;
+
+ bool errTransient = false, errAuthErr = false;
+ {
+ std::string const failReason = LookupTag(Message, "FailReason");
+ {
+ auto const reasons = { "Timeout", "ConnectionRefused",
+ "ConnectionTimedOut", "ResolveFailure", "TmpResolveFailure" };
+ errTransient = std::find(std::begin(reasons), std::end(reasons), failReason) != std::end(reasons);
+ }
+ if (errTransient == false)
+ {
+ auto const reasons = { "HashSumMismatch", "WeakHashSums", "MaximumSizeExceeded" };
+ errAuthErr = std::find(std::begin(reasons), std::end(reasons), failReason) != std::end(reasons);
+ }
+ }
+
+ for (auto const Owner: ItmOwners)
+ {
+ if (errAuthErr && Owner->GetExpectedHashes().empty() == false)
+ Owner->Status = pkgAcquire::Item::StatAuthError;
+ else if (errTransient)
+ Owner->Status = pkgAcquire::Item::StatTransientNetworkError;
+
+ if (isDoomedItem(Owner) == false)
+ Owner->Failed(Message,Config);
+ if (Log != nullptr)
+ Log->Fail(Owner->GetItemDesc());
+ }
+ ItemDone();
+
+ break;
+ }
+
+ case MessageType::GENERAL_FAILURE:
+ _error->Error("Method %s General failure: %s",Access.c_str(),LookupTag(Message,"Message").c_str());