libzypp 17.36.6
MediaNetworkCommonHandler.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
14
15#include <zypp/ZConfig.h>
17#include <zypp/base/Logger.h>
19#include <zypp/Target.h>
20
21#include <fstream>
22#include <curl/curl.h>
23
24namespace 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:
116 Url newurl { baseUrl };
117 newurl.appendPathName( filename_r );
118 return newurl;
119 }
120
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
Base class for Exception.
Definition Exception.h:147
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
std::string anonymousUniqueId() const
anonymous unique id
Definition Target.cc:132
std::string targetDistribution() const
This is register.target attribute of the installed base product.
Definition Target.cc:102
Url manipulation class.
Definition Url.h:93
void setHost(const std::string &host)
Set the hostname or IP in the URL authority.
Definition Url.cc:766
void appendPathName(const Pathname &path_r, EEncoding eflag_r=zypp::url::E_DECODED)
Extend the path name.
Definition Url.cc:804
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:940
Wrapper class for stat/lstat.
Definition PathInfo.h:226
const std::string & asString() const
String representation.
Definition Pathname.h:93
Url url() const
Url used.
const Url _url
Url to handle.
Url getFileUrl(const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
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...
Regular expression.
Definition Regex.h:95
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition Regex.cc:57
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:37
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:224
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:440
#define MIL
Definition Logger.h:100