libzypp 17.35.9
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 UrlResolverPlugin::HeaderList custom_headers;
58 Url url = UrlResolverPlugin::resolveUrl(o_url, custom_headers);
59 MIL << "Trying scheme '" << url.getScheme() << "'" << std::endl;
60
61 const auto hdlType = handlerType( url );
62 if ( !hdlType ) {
64 }
65
66 std::unique_ptr<MediaHandler> _handler;
67 switch(*hdlType) {
68 case MediaCDType: {
69 _handler = std::make_unique<MediaCD> (url,preferred_attach_point);
70 break;
71 }
72 case MediaNFSType: {
73 _handler = std::make_unique<MediaNFS> (url,preferred_attach_point);
74 break;
75 }
76 case MediaISOType: {
77 _handler = std::make_unique<MediaISO> (url,preferred_attach_point);
78 break;
79 }
80 case MediaFileType: {
81 _handler = std::make_unique<MediaDIR> (url,preferred_attach_point);
82 break;
83 }
84 case MediaDISKType: {
85 _handler = std::make_unique<MediaDISK> (url,preferred_attach_point);
86 break;
87 }
88 case MediaCIFSType: {
89 _handler = std::make_unique<MediaCIFS> (url,preferred_attach_point);
90 break;
91 }
92 case MediaCURLType: {
93 enum WhichHandler { choose, curl, multicurl, network };
94 WhichHandler which = choose;
95 // Leagcy: choose handler in UUrl query
96 if ( const std::string & queryparam = url.getQueryParam("mediahandler"); ! queryparam.empty() ) {
97 if ( queryparam == "network" )
98 which = network;
99 else if ( queryparam == "multicurl" )
100 which = multicurl;
101 else if ( queryparam == "curl" )
102 which = curl;
103 else
104 WAR << "Unknown mediahandler='" << queryparam << "' in URL; Choosing the default" << std::endl;
105 }
106 // Otherwise choose handler through ENV
107 if ( which == choose ) {
108 auto getenvIs = []( std::string_view var, std::string_view val )->bool {
109 const char * v = ::getenv( var.data() );
110 return v && v == val;
111 };
112
113 if ( getenvIs( "ZYPP_MEDIANETWORK", "1" ) ) {
114 WAR << "MediaNetwork backend enabled" << std::endl;
115 which = network;
116 }
117 else if ( getenvIs( "ZYPP_MULTICURL", "0" ) ) {
118 WAR << "multicurl manually disabled." << std::endl;
119 which = curl;
120 }
121 else
122 which = multicurl;
123 }
124 // Finally use the default
125 std::unique_ptr<MediaNetworkCommonHandler> handler;
126 switch ( which ) {
127 default:
128 case multicurl:
129 handler = std::make_unique<MediaMultiCurl>( url, preferred_attach_point );
130 break;
131
132 case network:
133 handler = std::make_unique<MediaNetwork>( url, preferred_attach_point );
134 break;
135
136 case curl:
137 handler = std::make_unique<MediaCurl>( url, preferred_attach_point );
138 break;
139 }
140 // Set up the handler
141 for ( const auto & el : custom_headers ) {
142 std::string header { el.first };
143 header += ": ";
144 header += el.second;
145 MIL << "Added custom header -> " << header << std::endl;
146 handler->settings().addHeader( std::move(header) );
147 }
148 _handler = std::move(handler);
149 break;
150 }
151 case MediaPluginType: {
152 // bsc#1228208: MediaPluginType must be resolved to a valid schema by the
153 // above UrlResolverPlugin::resolveUrl call. MediaPlugin exists as a stub,
154 // but is not a usable handler type.
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