libzypp  17.31.31
detectmeta_p.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 ----------------------------------------------------------------------*/
9 
13 
14 #include "detectmeta_p.h"
15 #include "metalinkinfo_p.h"
16 
17 namespace zyppng {
18 
20  MIL << "Entering DetectMetalinkState for url: " << parent._spec.url() << std::endl;
21  }
22 
24  {
25  _request.reset();
26  _gotMetalink = false;
27 
28  auto &sm = stateMachine();
29  const auto &url = sm._spec.url();
30 
31  MIL_MEDIA << "Detecting if metalink is available on " << url << std::endl;
32 
33  _request = std::make_shared<Request>( ::internal::clearQueryString( url ), zypp::Pathname("/dev/null") );
34 
35  _request->_originalUrl = url;
36  _request->transferSettings() = sm._spec.settings();
37  _request->transferSettings().addHeader("Accept: */*, application/metalink+xml, application/metalink4+xml");
38  _request->setOptions( _request->options() | NetworkRequest::HeadRequest );
39 
40  _request->connectSignals( *this );
41  sm._requestDispatcher->enqueue( _request );
42  }
43 
45  {
46  if ( _request ) {
47  _request->disconnectSignals();
48  _request.reset();
49  }
50  }
51 
53  {
54  stateMachine()._sigStarted.emit( *stateMachine().z_func() );
55  }
56 
57  void DetectMetalinkState::onRequestProgress( NetworkRequest &, off_t, off_t dlnow, off_t, off_t )
58  {
59  stateMachine()._sigAlive.emit( *stateMachine().z_func(), dlnow );
60  }
61 
63  {
64  auto lck = stateMachine().z_func()->shared_from_this();
65  if ( req.hasError() ) {
66  WAR << req.nativeHandle() << " " << "Detecing if metalink is possible for url " << req.url() << " failed with error " << err.toString() << " falling back to download without metalink." << std::endl;
67  if ( req.lastRedirectInfo ().size () )
68  WAR << req.nativeHandle() << " Last redirection target was: " << req.lastRedirectInfo () << std::endl;
69 
70  _error = err;
71  _gotMetalink = false;
72  return _sigFinished.emit();
73  }
74 
75  std::string cType = req.contentType();
76  _gotMetalink = ( cType.find("application/metalink+xml") == 0 || cType.find("application/metalink4+xml") == 0 );
77  MIL << req.nativeHandle() << " " << "Metalink detection result on url " << req.url() << " is " << _gotMetalink << std::endl;
78  _sigFinished.emit();
79  }
80 
81  std::shared_ptr<DlMetaLinkInfoState> DetectMetalinkState::toDlMetaLinkInfoState()
82  {
83  _request->disconnectSignals();
84  auto nState = std::make_shared<DlMetaLinkInfoState>( std::move( _request ), stateMachine() );
85  _request = nullptr;
86  return nState;
87  }
88 
90  {
91 #if ENABLE_ZCHUNK_COMPRESSION
92  return !toMetalinkGuard() && !toZckHeadDownloadGuard();
93 #else
94  return !toMetalinkGuard();
95 #endif
96  }
97 
98 #if ENABLE_ZCHUNK_COMPRESSION
99  bool DetectMetalinkState::toZckHeadDownloadGuard() const
100  {
101  return !toMetalinkGuard() && stateMachine().hasZckInfo();
102  }
103 
104  std::shared_ptr<DLZckHeadState> DetectMetalinkState::toDLZckHeadState()
105  {
106  // we have no mirrors, the range downloader would need to fall back to using the base URL
107  if ( _error.isError() || !_request )
108  return std::make_shared<DLZckHeadState>( std::vector<Url> { stateMachine()._spec.url() }, stateMachine() );
109  else {
110  // reuse our request
111  _request->disconnectSignals();
112  auto nstate = std::make_shared<DLZckHeadState>( std::vector<Url> { stateMachine()._spec.url() }, std::move(_request), stateMachine() );
113  _request = nullptr;
114  return nstate;
115  }
116  }
117 #endif
118 
119 }
bool isError() const
isError Will return true if this is a actual error
#define MIL
Definition: Logger.h:96
void * nativeHandle() const
Definition: request.cc:840
DetectMetalinkState(DownloadPrivate &parent)
Definition: detectmeta_p.cc:19
DownloadSpec _spec
Definition: base_p.h:98
const std::string & lastRedirectInfo() const
Definition: request.cc:835
Url clearQueryString(const Url &url)
Definition: curlhelper.cc:373
std::string toString() const
toString Returns a string representation of the error
bool hasError() const
Checks if there was a error with the request.
Definition: request.cc:1001
bool toSimpleDownloadGuard() const
Definition: detectmeta_p.cc:89
void onRequestProgress(NetworkRequest &, off_t, off_t dlnow, off_t, off_t)
Definition: detectmeta_p.cc:57
#define WAR
Definition: Logger.h:97
The NetworkRequestError class Represents a error that occured in.
std::shared_ptr< DlMetaLinkInfoState > toDlMetaLinkInfoState()
Definition: detectmeta_p.cc:81
std::shared_ptr< Request > _request
Definition: detectmeta_p.h:71
#define MIL_MEDIA
Definition: mediadebug_p.h:29
std::string contentType() const
Returns the content type as reported from the server.
Definition: request.cc:924
Signal< void() > _sigFinished
Definition: detectmeta_p.h:76
NetworkRequestError _error
Definition: detectmeta_p.h:74
void onRequestStarted(NetworkRequest &)
Definition: detectmeta_p.cc:52
void onRequestFinished(NetworkRequest &req, const NetworkRequestError &err)
Definition: detectmeta_p.cc:62
const Url & url() const
Definition: downloadspec.cc:50