Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/common/pure.h" 4 : #include "envoy/config/core/v3/http_uri.pb.h" 5 : #include "envoy/upstream/cluster_manager.h" 6 : 7 : namespace Envoy { 8 : namespace Config { 9 : namespace DataFetcher { 10 : 11 : /** 12 : * Failure reason. 13 : */ 14 : enum class FailureReason { 15 : /* A network error occurred causing remote data retrieval failure. */ 16 : Network, 17 : /* A failure occurred when trying to verify remote data using sha256. */ 18 : InvalidData, 19 : }; 20 : 21 : /** 22 : * Callback used by remote data fetcher. 23 : */ 24 : class RemoteDataFetcherCallback { 25 : public: 26 0 : virtual ~RemoteDataFetcherCallback() = default; 27 : 28 : /** 29 : * This function will be called when data is fetched successfully from remote. 30 : * @param data remote data 31 : */ 32 : virtual void onSuccess(const std::string& data) PURE; 33 : 34 : /** 35 : * This function is called when error happens during fetching data. 36 : * @param reason failure reason. 37 : */ 38 : virtual void onFailure(FailureReason reason) PURE; 39 : }; 40 : 41 : /** 42 : * Remote data fetcher. 43 : */ 44 : class RemoteDataFetcher : public Logger::Loggable<Logger::Id::config>, 45 : public Http::AsyncClient::Callbacks { 46 : public: 47 : RemoteDataFetcher(Upstream::ClusterManager& cm, const envoy::config::core::v3::HttpUri& uri, 48 : const std::string& content_hash, RemoteDataFetcherCallback& callback); 49 : 50 : ~RemoteDataFetcher() override; 51 : 52 : // Http::AsyncClient::Callbacks 53 : void onSuccess(const Http::AsyncClient::Request&, Http::ResponseMessagePtr&& response) override; 54 : void onFailure(const Http::AsyncClient::Request&, 55 : Http::AsyncClient::FailureReason reason) override; 56 : void onBeforeFinalizeUpstreamSpan(Envoy::Tracing::Span&, 57 0 : const Http::ResponseHeaderMap*) override {} 58 : 59 : /** 60 : * Fetch data from remote. 61 : * @param uri remote URI 62 : * @param content_hash for verifying data integrity 63 : * @param callback callback when fetch is done. 64 : */ 65 : void fetch(); 66 : 67 : /** 68 : * Cancel the fetch. 69 : */ 70 : void cancel(); 71 : 72 : private: 73 : Upstream::ClusterManager& cm_; 74 : const envoy::config::core::v3::HttpUri uri_; 75 : const std::string content_hash_; 76 : RemoteDataFetcherCallback& callback_; 77 : 78 : Http::AsyncClient::Request* request_{}; 79 : }; 80 : 81 : using RemoteDataFetcherPtr = std::unique_ptr<RemoteDataFetcher>; 82 : 83 : } // namespace DataFetcher 84 : } // namespace Config 85 : } // namespace Envoy