libzypp 17.35.1
MediaHandlerFactory.cc
Go to the documentation of this file.
2
3
4#include <zypp/base/Logger.h>
5
6#include <zypp-media/MediaException>
8
10#include <zypp/media/MediaCD.h>
11#include <zypp/media/MediaDIR.h>
17#include <zypp/media/MediaISO.h>
20
21namespace zypp::media {
22
27
28 std::optional<MediaHandlerFactory::MediaHandlerType> MediaHandlerFactory::handlerType(const Url &url)
29 {
30 std::string scheme = url.getScheme();
31 if (scheme == "cd" || scheme == "dvd")
32 return MediaCDType;
33 else if (scheme == "nfs" || scheme == "nfs4")
34 return MediaNFSType;
35 else if (scheme == "iso")
36 return MediaISOType;
37 else if (scheme == "file" || scheme == "dir")
38 return MediaFileType;
39 else if (scheme == "hd" )
40 return MediaDISKType;
41 else if (scheme == "cifs" || scheme == "smb")
42 return MediaCIFSType;
43 else if (scheme == "ftp" || scheme == "tftp" || scheme == "http" || scheme == "https")
44 return MediaCURLType;
45 else if (scheme == "plugin" )
46 return MediaPluginType;
47 return {};
48 }
49
50 std::unique_ptr<MediaHandler> MediaHandlerFactory::createHandler( const Url &o_url, const Pathname &preferred_attach_point )
51 {
52 if(!o_url.isValid()) {
53 MIL << "Url is not valid" << std::endl;
55 }
56
57 std::unique_ptr<MediaHandler> _handler;
58
59 UrlResolverPlugin::HeaderList custom_headers;
60 Url url = UrlResolverPlugin::resolveUrl(o_url, custom_headers);
61
62
63 MIL << "Trying scheme '" << o_url.getScheme() << "'" << std::endl;
64
65 const auto hdlType = handlerType( o_url );
66 if ( !hdlType ) {
68 }
69
70 switch(*hdlType) {
71 case MediaCDType: {
72 _handler = std::make_unique<MediaCD> (url,preferred_attach_point);
73 break;
74 }
75 case MediaNFSType: {
76 _handler = std::make_unique<MediaNFS> (url,preferred_attach_point);
77 break;
78 }
79 case MediaISOType: {
80 _handler = std::make_unique<MediaISO> (url,preferred_attach_point);
81 break;
82 }
83 case MediaFileType: {
84 _handler = std::make_unique<MediaDIR> (url,preferred_attach_point);
85 break;
86 }
87 case MediaDISKType: {
88 _handler = std::make_unique<MediaDISK> (url,preferred_attach_point);
89 break;
90 }
91 case MediaCIFSType: {
92 _handler = std::make_unique<MediaCIFS> (url,preferred_attach_point);
93 break;
94 }
95 case MediaCURLType: {
96 enum WhichHandler { choose, curl, multicurl, network };
97 WhichHandler which = choose;
98 // Leagcy: choose handler in UUrl query
99 if ( const std::string & queryparam = url.getQueryParam("mediahandler"); ! queryparam.empty() ) {
100 if ( queryparam == "network" )
101 which = network;
102 else if ( queryparam == "multicurl" )
103 which = multicurl;
104 else if ( queryparam == "curl" )
105 which = curl;
106 else
107 WAR << "Unknown mediahandler='" << queryparam << "' in URL; Choosing the default" << std::endl;
108 }
109 // Otherwise choose handler through ENV
110 if ( which == choose ) {
111 auto getenvIs = []( std::string_view var, std::string_view val )->bool {
112 const char * v = ::getenv( var.data() );
113 return v && v == val;
114 };
115
116 if ( getenvIs( "ZYPP_MEDIANETWORK", "1" ) ) {
117 WAR << "MediaNetwork backend enabled" << std::endl;
118 which = network;
119 }
120 else if ( getenvIs( "ZYPP_MULTICURL", "0" ) ) {
121 WAR << "multicurl manually disabled." << std::endl;
122 which = curl;
123 }
124 else
125 which = multicurl;
126 }
127 // Finally use the default
128 std::unique_ptr<MediaNetworkCommonHandler> handler;
129 switch ( which ) {
130 default:
131 case multicurl:
132 handler = std::make_unique<MediaMultiCurl>( url, preferred_attach_point );
133 break;
134
135 case network:
136 handler = std::make_unique<MediaNetwork>( url, preferred_attach_point );
137 break;
138
139 case curl:
140 handler = std::make_unique<MediaCurl>( url, preferred_attach_point );
141 break;
142 }
143 // Set up the handler
144 for ( const auto & el : custom_headers ) {
145 std::string header { el.first };
146 header += ": ";
147 header += el.second;
148 MIL << "Added custom header -> " << header << std::endl;
149 handler->settings().addHeader( std::move(header) );
150 }
151 _handler = std::move(handler);
152 break;
153 }
154 case MediaPluginType: {
155 _handler = std::make_unique<MediaPlugin> (url,preferred_attach_point);
156 break;
157 }
158 }
159
160 if ( !_handler ) {
162 }
163
164 // check created handler
165 if ( !_handler ){
166 ERR << "Failed to create media handler" << std::endl;
167 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
168 }
169
170 MIL << "Opened: " << *_handler << std::endl;
171 return _handler;
172 }
173
174}
std::unique_ptr< MediaHandler > _handler
Url manipulation class.
Definition Url.h:92
std::string getScheme() const
Returns the scheme name of the URL.
Definition Url.cc:537
std::string getQueryParam(const std::string &param, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
Definition Url.cc:664
bool isValid() const
Verifies the Url.
Definition Url.cc:493
static std::unique_ptr< MediaHandler > createHandler(const Url &o_url, const Pathname &preferred_attach_point)
static std::optional< MediaHandlerType > handlerType(const Url &url)
std::multimap< std::string, std::string > HeaderList
static Url resolveUrl(const Url &url, HeaderList &headers)
Resolves an url using the installed plugins If no plugin is found the url is resolved as its current ...
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:424
#define MIL
Definition Logger.h:98
#define ERR
Definition Logger.h:100
#define WAR
Definition Logger.h:99