libzypp  17.31.7
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_MEDIA << "Creating DetectMetalinkState" << 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 << "Detecing if metalink is possible for url " << req.url() << " failed with error " << err.toString() << " falling back to download without metalink." << std::endl;
67  _error = err;
68  _gotMetalink = false;
69  return _sigFinished.emit();
70  }
71 
72  std::string cType = req.contentType();
73  _gotMetalink = ( cType.find("application/metalink+xml") == 0 || cType.find("application/metalink4+xml") == 0 );
74  MIL << "Metalink detection result on url " << req.url() << " is " << _gotMetalink << std::endl;
75  _sigFinished.emit();
76  }
77 
78  std::shared_ptr<DlMetaLinkInfoState> DetectMetalinkState::toDlMetaLinkInfoState()
79  {
80  _request->disconnectSignals();
81  auto nState = std::make_shared<DlMetaLinkInfoState>( std::move( _request ), stateMachine() );
82  _request = nullptr;
83  return nState;
84  }
85 
87  {
88 #if ENABLE_ZCHUNK_COMPRESSION
89  return !toMetalinkGuard() && !toZckHeadDownloadGuard();
90 #else
91  return !toMetalinkGuard();
92 #endif
93  }
94 
95 #if ENABLE_ZCHUNK_COMPRESSION
96  bool DetectMetalinkState::toZckHeadDownloadGuard() const
97  {
98  return !toMetalinkGuard() && stateMachine().hasZckInfo();
99  }
100 
101  std::shared_ptr<DLZckHeadState> DetectMetalinkState::toDLZckHeadState()
102  {
103  // we have no mirrors, the range downloader would need to fall back to using the base URL
104  if ( _error.isError() || !_request )
105  return std::make_shared<DLZckHeadState>( std::vector<Url> { stateMachine()._spec.url() }, stateMachine() );
106  else {
107  // reuse our request
108  _request->disconnectSignals();
109  auto nstate = std::make_shared<DLZckHeadState>( std::vector<Url> { stateMachine()._spec.url() }, std::move(_request), stateMachine() );
110  _request = nullptr;
111  return nstate;
112  }
113  }
114 #endif
115 
116 }
bool isError() const
isError Will return true if this is a actual error
#define MIL
Definition: Logger.h:96
DetectMetalinkState(DownloadPrivate &parent)
Definition: detectmeta_p.cc:19
Url clearQueryString(const Url &url)
Definition: curlhelper.cc:332
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:1457
bool toSimpleDownloadGuard() const
Definition: detectmeta_p.cc:86
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:78
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:1380
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