libzypp 17.36.7

MediaNetworkCommonHandler.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include "MediaNetworkCommonHandler.h"
14
15#include <zypp/ZConfig.h>
16#include <zypp-core/fs/PathInfo.h>
17#include <zypp/base/Logger.h>
18#include <zypp-core/base/Regex.h>
19#include <zypp/Target.h>
20
21#include <fstream>
22#include <curl/curl.h>
23
24 namespace zypp::media
25{
26 zypp::Url MediaNetworkCommonHandler::findGeoIPRedirect ( const zypp::Url &url )
27 {
28 try {
29 const auto &conf = ZConfig::instance();
30 if ( !conf.geoipEnabled() ) {
31 MIL << "GeoIp rewrites disabled via ZConfig." << std::endl;
32 return Url();
33 }
34
35 if ( !( url.getQueryParam("COUNTRY").empty() && url.getQueryParam("AVOID_COUNTRY").empty() )) {
36 MIL << "GeoIp rewrites disabled since the baseurl " << url << " uses an explicit country setting." << std::endl;
37 return Url();
38 }
39
40 const auto &hostname = url.getHost();
41 auto geoipFile = conf.geoipCachePath() / hostname ;
42 if ( PathInfo( geoipFile ).isFile() ) {
43
44 MIL << "Found GeoIP file for host: " << hostname << std::endl;
45
46 std::ifstream in( geoipFile.asString() );
47 if (!in.is_open()) {
48 MIL << "Failed to open GeoIP for host: " << hostname << std::endl;
49 return Url();
50 }
51
52 try {
53 std::string newHost;
54 in >> newHost;
55
56 Url newUrl = url;
57 newUrl.setHost( newHost );
58
59 MIL << "Found GeoIP rewrite: " << hostname << " -> " << newHost << std::endl;
60
61 return newUrl;
62
63 } catch ( const zypp::Exception &e ) {
64 ZYPP_CAUGHT(e);
65 MIL << "No valid GeoIP rewrite target found for " << url << std::endl;
66 }
67 }
68 } catch ( const zypp::Exception &e ) {
69 ZYPP_CAUGHT(e);
70 MIL << "Failed to query GeoIP data, url rewriting disabled." << std::endl;
71 }
72
73 // no rewrite
74 return Url();
75 }
76
77 const char *MediaNetworkCommonHandler::anonymousIdHeader()
78 {
79 // we need to add the release and identifier to the
80 // agent string.
81 // The target could be not initialized, and then this information
82 // is guessed.
83 // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
84 static const std::string _value( str::trim( str::form(
85 "X-ZYpp-AnonymousId: %s",
86 Target::anonymousUniqueId( Pathname()/*guess root*/ ).c_str()
87 )));
88 return _value.c_str();
89 }
90
91 const char *MediaNetworkCommonHandler::distributionFlavorHeader()
92 {
93 // we need to add the release and identifier to the
94 // agent string.
95 // The target could be not initialized, and then this information
96 // is guessed.
97 // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
98 static const std::string _value( str::trim( str::form(
99 "X-ZYpp-DistributionFlavor: %s",
100 Target::distributionFlavor( Pathname()/*guess root*/ ).c_str()
101 )));
102 return _value.c_str();
103 }
104
105 Url MediaNetworkCommonHandler::getFileUrl( const Pathname & filename_r ) const
106 {
107 static const zypp::str::regex invalidRewrites("^.*\\/repomd.xml(.asc|.key)?$|^\\/geoip$");
108
109 const bool canRedir = _redirTarget.isValid() && !invalidRewrites.matches(filename_r.asString());
110 const auto &baseUrl = ( canRedir ) ? _redirTarget : _url;
111
112 if ( canRedir )
113 MIL << "Redirecting " << filename_r << " request to geoip location." << std::endl;
114
115 // Simply extend the URLs pathname:
116 Url newurl { baseUrl };
117 newurl.appendPathName( filename_r );
118 return newurl;
119 }
120
121 const char *MediaNetworkCommonHandler::agentString() {
122 // we need to add the release and identifier to the
123 // agent string.
124 // The target could be not initialized, and then this information
125 // is guessed.
126 // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
127 static const std::string _value(str::trim(str::form(
128 "ZYpp " LIBZYPP_VERSION_STRING " (curl %s) %s",
129 curl_version_info(CURLVERSION_NOW)->version,
130 Target::targetDistribution(Pathname() /*guess root*/).c_str())));
131 return _value.c_str();
132 }
133} // namespace zypp::media
MediaNetworkCommonHandler.h
Target.h
ZConfig.h
zypp::Exception
Base class for Exception.
Definition Exception.h:147
zypp::Target::distributionFlavor
std::string distributionFlavor() const
This is flavor attribute of the installed base product but does not require the target to be loaded a...
Definition Target.cc:127
zypp::Target::anonymousUniqueId
std::string anonymousUniqueId() const
anonymous unique id
Definition Target.cc:132
zypp::Target::targetDistribution
std::string targetDistribution() const
This is register.target attribute of the installed base product.
Definition Target.cc:102
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::Url::setHost
void setHost(const std::string &host)
Set the hostname or IP in the URL authority.
Definition Url.cc:766
zypp::Url::appendPathName
void appendPathName(const Pathname &path_r, EEncoding eflag_r=zypp::url::E_DECODED)
Extend the path name.
Definition Url.cc:804
zypp::ZConfig::instance
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:940
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:93
zypp::media::MediaHandler::url
Url url() const
Url used.
Definition MediaHandler.h:503
zypp::media::MediaHandler::_url
const Url _url
Url to handle.
Definition MediaHandler.h:113
zypp::media::MediaNetworkCommonHandler::getFileUrl
Url getFileUrl(const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
Definition MediaNetworkCommonHandler.cc:105
zypp::media::MediaNetworkCommonHandler::_redirTarget
Url _redirTarget
Definition MediaNetworkCommonHandler.h:70
zypp::media::MediaNetworkCommonHandler::anonymousIdHeader
static const char * anonymousIdHeader()
Definition MediaNetworkCommonHandler.cc:77
zypp::media::MediaNetworkCommonHandler::distributionFlavorHeader
static const char * distributionFlavorHeader()
Definition MediaNetworkCommonHandler.cc:91
zypp::media::MediaNetworkCommonHandler::findGeoIPRedirect
static zypp::Url findGeoIPRedirect(const zypp::Url &url)
Rewrites the baseURL to the geoIP target if one is found in the metadata cache, otherwise simply retu...
Definition MediaNetworkCommonHandler.cc:26
zypp::media::MediaNetworkCommonHandler::agentString
static const char * agentString()
Definition MediaNetworkCommonHandler.cc:121
zypp::str::regex
Regular expression.
Definition Regex.h:95
zypp::str::regex::matches
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition Regex.cc:57
zypp::media
Definition MediaCD.cc:47
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:37
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:224
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:440
MIL
#define MIL
Definition Logger.h:100
Regex.h
PathInfo.h
Logger.h