libzypp 17.35.15
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
17namespace 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)
60 if ( _detectedMetaType == MetaDataType::None )
62 if ( _detectedMetaType == MetaDataType::None ) {
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
76 if ( _detectedMetaType == MetaDataType::Zsync )
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
87 if ( _detectedMetaType == MetaDataType::None && dlnow < zypp::media::minMetalinkProbeSize ) {
88 // can't tell yet, ...
89 return sm._sigAlive.emit( *sm.z_func(), dlnow );
90 }
91
92 if ( _detectedMetaType == MetaDataType::None ) {
93 std::string cType = req.contentType();
94 if ( cType.find("application/x-zsync") == 0 )
95 _detectedMetaType = MetaDataType::Zsync;
96 else if ( cType.find("application/metalink+xml") == 0 || cType.find("application/metalink4+xml") == 0 )
97 _detectedMetaType = MetaDataType::MetaLink;
98 }
99
100 if ( _detectedMetaType == MetaDataType::None ) {
102 }
103
104 if ( _detectedMetaType != MetaDataType::None ) {
105 // this is a metalink file change the expected filesize
107 WAR << "Metadata file exceeds 2MB in filesize, aborting."<<std::endl;
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}
Store and operate with byte count.
Definition ByteCount.h:32
static const Unit MB
1000^2 Byte
Definition ByteCount.h:61
WeakPtr parent() const
Definition base.cc:26
const Url & url() const
static zyppng::NetworkRequestError customError(NetworkRequestError::Type t, std::string &&errorMsg="", std::map< std::string, boost::any > &&extraInfo={})
zypp::ByteCount downloadedByteCount() const
Returns the number of already downloaded bytes as reported by the backend.
Definition request.cc:962
std::vector< char > peekData(off_t offset, size_t count) const
Definition request.cc:889
std::string contentType() const
Returns the content type as reported from the server.
Definition request.cc:940
Definition Arch.h:364
MetaDataType looks_like_meta_data(const std::vector< char > &data)
constexpr auto minMetalinkProbeSize
MetaDataType looks_like_meta_file(const Pathname &file)
virtual void handleRequestProgress(NetworkRequest &req, off_t dltotal, off_t dlnow)
std::shared_ptr< Request > _request
virtual bool initializeRequest(std::shared_ptr< Request > &r)
Signal< void() > _sigGotMetadata
std::shared_ptr< FinishedState > transitionToFinished()
void handleRequestProgress(NetworkRequest &req, off_t dltotal, off_t dlnow) override
bool initializeRequest(std::shared_ptr< Request > &r) override
DlMetaLinkInfoState(DownloadPrivate &parent)
std::shared_ptr< PrepareMultiState > transitionToPrepareMulti()
#define MIL
Definition Logger.h:100
#define WAR
Definition Logger.h:101