libzypp  17.31.31
metalinkinfo_p.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 ----------------------------------------------------------------------*/
9 
14 
15 #include "metalinkinfo_p.h"
16 
17 namespace zyppng {
18 
19  namespace {
20  }
21 
23  : BasicDownloaderStateBase( parent )
24  {
25  MIL << "Downloading metalink/zsync on " << parent._spec.url() << std::endl;
26  }
27 
28  DlMetaLinkInfoState::DlMetaLinkInfoState(std::shared_ptr<Request> &&prevRequest, DownloadPrivate &parent)
29  : BasicDownloaderStateBase( std::move(prevRequest), parent )
30  {
31  MIL << "Downloading metalink/zsync on " << parent._spec.url() << std::endl;
32  }
33 
34  std::shared_ptr<FinishedState> DlMetaLinkInfoState::transitionToFinished()
35  {
36  MIL << "Downloading on " << stateMachine()._spec.url() << " transition to final state. " << std::endl;
37  return std::make_shared<FinishedState>( std::move(_error), stateMachine() );
38  }
39 
40  std::shared_ptr<PrepareMultiState> DlMetaLinkInfoState::transitionToPrepareMulti()
41  {
42  _request->disconnectSignals();
43  auto prepareMode = ( _detectedMetaType == MetaDataType::MetaLink ? PrepareMultiState::Metalink : PrepareMultiState::Zsync );
44  auto nState = std::make_shared<PrepareMultiState>( std::move( _request ), prepareMode, stateMachine() );
45  _request = nullptr;
46  return nState;
47  }
48 
49  bool DlMetaLinkInfoState::initializeRequest(std::shared_ptr<Request> &r )
50  {
51  MIL << "Requesting Metadata info from server!" << std::endl;
52  r->transferSettings().addHeader("Accept: */*, application/x-zsync, application/metalink+xml, application/metalink4+xml");
54  }
55 
57  {
58  // some proxies do not store the content type, so also look at the file to find
59  // out if we received a metalink (bnc#649925)
63  // Move to finished state
64  MIL << "Downloading on " << stateMachine()._spec.url() << " was successful, no metalink/zsync data. " << std::endl;
66  }
67 
68  auto &sm = stateMachine();
69  if ( sm._stopOnMetalink ) {
70  MIL << "Stopping after receiving MetaData as requested" << std::endl;
71  sm._stoppedOnMetalink = true;
73  }
74 
75  // Move to Prepare Multi state
77  MIL << "Downloading on " << sm._spec.url() << " returned a Zsync file " << std::endl;
78  else
79  MIL << "Downloading on " << sm._spec.url() << " returned a Metalink file" << std::endl;
80  _sigGotMetadata.emit();
81  }
82 
83  void DlMetaLinkInfoState::handleRequestProgress(NetworkRequest &req, off_t dltotal, off_t dlnow)
84  {
85  auto &sm = stateMachine();
86 
88  // can't tell yet, ...
89  return sm._sigAlive.emit( *sm.z_func(), dlnow );
90  }
91 
93  std::string cType = req.contentType();
94  if ( cType.find("application/x-zsync") == 0 )
96  else if ( cType.find("application/metalink+xml") == 0 || cType.find("application/metalink4+xml") == 0 )
97  _detectedMetaType = MetaDataType::MetaLink;
98  }
99 
102  }
103 
105  // this is a metalink file change the expected filesize
107  WAR << "Metadata file exceeds 2MB in filesize, aborting."<<std::endl;
108  sm._requestDispatcher->cancel( req, NetworkRequestErrorPrivate::customError( NetworkRequestError::ExceededMaxLen ) );
109  return;
110  }
111 
112  return sm._sigAlive.emit( *sm.z_func(), dlnow );
113 
114  } else {
115  // still no metalink, we assume a normal download, not perfect though
116  if ( !_fallbackMilWritten ) {
117  _fallbackMilWritten = true;
118  MIL << "No Metalink file detected after " << zypp::media::minMetalinkProbeSize << ", falling back to normal progress updates" << std::endl;
119  }
120  return BasicDownloaderStateBase::handleRequestProgress( req, dltotal, dlnow );
121  }
122  }
123 }
#define MIL
Definition: Logger.h:96
DownloadSpec _spec
Definition: base_p.h:98
Store and operate with byte count.
Definition: ByteCount.h:30
Signal< void() > _sigGotMetadata
zypp::ByteCount downloadedByteCount() const
Returns the number of already downloaded bytes as reported by the backend.
Definition: request.cc:946
Definition: Arch.h:363
static const Unit MB
1000^2 Byte
Definition: ByteCount.h:60
virtual void gotFinished() override
#define WAR
Definition: Logger.h:97
std::vector< char > peekData(off_t offset, size_t count) const
Definition: request.cc:873
std::shared_ptr< Request > _request
MetaDataType looks_like_meta_file(const Pathname &file)
virtual void handleRequestProgress(NetworkRequest &req, off_t dltotal, off_t dlnow)
std::shared_ptr< FinishedState > transitionToFinished()
std::shared_ptr< PrepareMultiState > transitionToPrepareMulti()
virtual bool initializeRequest(std::shared_ptr< Request > &r)
MetaDataType looks_like_meta_data(const std::vector< char > &data)
std::string contentType() const
Returns the content type as reported from the server.
Definition: request.cc:924
bool initializeRequest(std::shared_ptr< Request > &r) override
DlMetaLinkInfoState(DownloadPrivate &parent)
static zyppng::NetworkRequestError customError(NetworkRequestError::Type t, std::string &&errorMsg="", std::map< std::string, boost::any > &&extraInfo={})
const Url & url() const
Definition: downloadspec.cc:50
constexpr auto minMetalinkProbeSize
virtual void handleRequestProgress(NetworkRequest &req, off_t dltotal, off_t dlnow) override