libzypp 17.31.23
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
24 {
25
26 }
27
28 std::unique_ptr<MediaHandler> MediaHandlerFactory::createHandler( const Url &o_url, const Pathname &preferred_attach_point )
29 {
30 if(!o_url.isValid()) {
31 MIL << "Url is not valid" << std::endl;
33 }
34
35 std::unique_ptr<MediaHandler> _handler;
36
37 UrlResolverPlugin::HeaderList custom_headers;
38 Url url = UrlResolverPlugin::resolveUrl(o_url, custom_headers);
39
40 std::string scheme = url.getScheme();
41 MIL << "Trying scheme '" << scheme << "'" << std::endl;
42
43 /*
44 ** WARNING: Don't forget to update MediaAccess::downloads(url)
45 ** if you are adding a new url scheme / handler!
46 */
47 if (scheme == "cd" || scheme == "dvd")
48 _handler = std::make_unique<MediaCD> (url,preferred_attach_point);
49 else if (scheme == "nfs" || scheme == "nfs4")
50 _handler = std::make_unique<MediaNFS> (url,preferred_attach_point);
51 else if (scheme == "iso")
52 _handler = std::make_unique<MediaISO> (url,preferred_attach_point);
53 else if (scheme == "file" || scheme == "dir")
54 _handler = std::make_unique<MediaDIR> (url,preferred_attach_point);
55 else if (scheme == "hd")
56 _handler = std::make_unique<MediaDISK> (url,preferred_attach_point);
57 else if (scheme == "cifs" || scheme == "smb")
58 _handler = std::make_unique<MediaCIFS> (url,preferred_attach_point);
59 else if (scheme == "ftp" || scheme == "tftp" || scheme == "http" || scheme == "https")
60 {
61 enum WhichHandler { choose, curl, multicurl, network };
62 WhichHandler which = choose;
63 // Leagcy: choose handler in UUrl query
64 if ( const std::string & queryparam = url.getQueryParam("mediahandler"); ! queryparam.empty() ) {
65 if ( queryparam == "network" )
66 which = network;
67 else if ( queryparam == "multicurl" )
68 which = multicurl;
69 else if ( queryparam == "curl" )
70 which = curl;
71 else
72 WAR << "Unknown mediahandler='" << queryparam << "' in URL; Choosing the default" << std::endl;
73 }
74 // Otherwise choose handler through ENV
75 if ( which == choose ) {
76 auto getenvIs = []( std::string_view var, std::string_view val )->bool {
77 const char * v = ::getenv( var.data() );
78 return v && v == val;
79 };
80
81 if ( getenvIs( "ZYPP_MEDIANETWORK", "1" ) ) {
82 WAR << "MediaNetwork backend enabled" << std::endl;
83 which = network;
84 }
85 else if ( getenvIs( "ZYPP_MULTICURL", "0" ) ) {
86 WAR << "multicurl manually disabled." << std::endl;
87 which = curl;
88 }
89 else
90 which = multicurl;
91 }
92 // Finally use the default
93 std::unique_ptr<MediaNetworkCommonHandler> handler;
94 switch ( which ) {
95 default:
96 case multicurl:
97 handler = std::make_unique<MediaMultiCurl>( url, preferred_attach_point );
98 break;
99
100 case network:
101 handler = std::make_unique<MediaNetwork>( url, preferred_attach_point );
102 break;
103
104 case curl:
105 handler = std::make_unique<MediaCurl>( url, preferred_attach_point );
106 break;
107 }
108 // Set up the handler
109 for ( const auto & el : custom_headers ) {
110 std::string header { el.first };
111 header += ": ";
112 header += el.second;
113 MIL << "Added custom header -> " << header << std::endl;
114 handler->settings().addHeader( std::move(header) );
115 }
116 _handler = std::move(handler);
117
118 }
119 else if (scheme == "plugin" )
120 _handler = std::make_unique<MediaPlugin> (url,preferred_attach_point);
121 else
122 {
124 }
125
126 // check created handler
127 if ( !_handler ){
128 ERR << "Failed to create media handler" << std::endl;
129 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
130 }
131
132 MIL << "Opened: " << *_handler << std::endl;
133 return _handler;
134 }
135
136}
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:533
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:660
bool isValid() const
Verifies the Url.
Definition: Url.cc:489
static std::unique_ptr< MediaHandler > createHandler(const Url &o_url, const Pathname &preferred_attach_point)
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:428
#define MIL
Definition: Logger.h:96
#define ERR
Definition: Logger.h:98
#define WAR
Definition: Logger.h:97