libzypp  17.36.3
MediaNetworkCommonHandler.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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 {
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 
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 
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 
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. An 'absolute' URL path
116  // is achieved by encoding the leading '/' in an URL path:2
117  // URL: ftp://user@server -> ~user
118  // URL: ftp://user@server/ -> ~user
119  // URL: ftp://user@server// -> ~user
120  // URL: ftp://user@server/%2F -> /
121  // ^- this '/' is just a separator
122  Url newurl( baseUrl );
123  newurl.setPathName( ( Pathname("./"+baseUrl.getPathName()) / filename_r ).asString().substr(1) );
124  return newurl;
125  }
126 
128  // we need to add the release and identifier to the
129  // agent string.
130  // The target could be not initialized, and then this information
131  // is guessed.
132  // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
133  static const std::string _value(str::trim(str::form(
134  "ZYpp " LIBZYPP_VERSION_STRING " (curl %s) %s",
135  curl_version_info(CURLVERSION_NOW)->version,
136  Target::targetDistribution(Pathname() /*guess root*/).c_str())));
137  return _value.c_str();
138  }
139 } // namespace zypp::media
Url getFileUrl(const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
std::string targetDistribution() const
This is register.target attribute of the installed base product.
Definition: Target.cc:102
#define MIL
Definition: Logger.h:100
Regular expression.
Definition: Regex.h:94
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:935
void setHost(const std::string &host)
Set the hostname or IP in the URL authority.
Definition: Url.cc:766
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:37
Edition * _value
Definition: SysContent.cc:311
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
Definition: Url.cc:782
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:678
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:224
const Url _url
Url to handle.
Definition: MediaHandler.h:113
const std::string & asString() const
String representation.
Definition: Pathname.h:93
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition: Regex.cc:57
bool isValid() const
Verifies the Url.
Definition: Url.cc:507
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:440
Base class for Exception.
Definition: Exception.h:146
Url url() const
Url used.
Definition: MediaHandler.h:503
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Definition: Url.cc:606
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:225
std::string anonymousUniqueId() const
anonymous unique id
Definition: Target.cc:132
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...
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
Url manipulation class.
Definition: Url.h:92