libzypp  17.31.31
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 
20 #include <fstream>
21 
22 namespace zypp::media
23 {
25  {
26  try {
27  const auto &conf = ZConfig::instance();
28  if ( !conf.geoipEnabled() ) {
29  MIL << "GeoIp rewrites disabled via ZConfig." << std::endl;
30  return Url();
31  }
32 
33  if ( !( url.getQueryParam("COUNTRY").empty() && url.getQueryParam("AVOID_COUNTRY").empty() )) {
34  MIL << "GeoIp rewrites disabled since the baseurl " << url << " uses an explicit country setting." << std::endl;
35  return Url();
36  }
37 
38  const auto &hostname = url.getHost();
39  auto geoipFile = conf.geoipCachePath() / hostname ;
40  if ( PathInfo( geoipFile ).isFile() ) {
41 
42  MIL << "Found GeoIP file for host: " << hostname << std::endl;
43 
44  std::ifstream in( geoipFile.asString() );
45  if (!in.is_open()) {
46  MIL << "Failed to open GeoIP for host: " << hostname << std::endl;
47  return Url();
48  }
49 
50  try {
51  std::string newHost;
52  in >> newHost;
53 
54  Url newUrl = url;
55  newUrl.setHost( newHost );
56 
57  MIL << "Found GeoIP rewrite: " << hostname << " -> " << newHost << std::endl;
58 
59  return newUrl;
60 
61  } catch ( const zypp::Exception &e ) {
62  ZYPP_CAUGHT(e);
63  MIL << "No valid GeoIP rewrite target found for " << url << std::endl;
64  }
65  }
66  } catch ( const zypp::Exception &e ) {
67  ZYPP_CAUGHT(e);
68  MIL << "Failed to query GeoIP data, url rewriting disabled." << std::endl;
69  }
70 
71  // no rewrite
72  return Url();
73  }
74 
76  {
77  static const zypp::str::regex invalidRewrites("^.*\\/repomd.xml(.asc|.key)?$|^\\/geoip$");
78 
79  const bool canRedir = _redirTarget.isValid() && !invalidRewrites.matches(filename_r.asString());
80  const auto &baseUrl = ( canRedir ) ? _redirTarget : _url;
81 
82  if ( canRedir )
83  MIL << "Redirecting " << filename_r << " request to geoip location." << std::endl;
84 
85  // Simply extend the URLs pathname. An 'absolute' URL path
86  // is achieved by encoding the leading '/' in an URL path:2
87  // URL: ftp://user@server -> ~user
88  // URL: ftp://user@server/ -> ~user
89  // URL: ftp://user@server// -> ~user
90  // URL: ftp://user@server/%2F -> /
91  // ^- this '/' is just a separator
92  Url newurl( baseUrl );
93  newurl.setPathName( ( Pathname("./"+baseUrl.getPathName()) / filename_r ).asString().substr(1) );
94  return newurl;
95  }
96 }
Url getFileUrl(const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
#define MIL
Definition: Logger.h:96
Regular expression.
Definition: Regex.h:94
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:922
void setHost(const std::string &host)
Set the hostname or IP in the URL authority.
Definition: Url.cc:748
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
Definition: Url.cc:764
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
const Url _url
Url to handle.
Definition: MediaHandler.h:113
const std::string & asString() const
String representation.
Definition: Pathname.h:91
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:489
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:436
Base class for Exception.
Definition: Exception.h:145
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:588
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:220
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...
Url manipulation class.
Definition: Url.h:91