PushActivity does not work with async code since we have no such thing as promise-local storage. it will be confusing at best, and completely wrong at worst, with the current thread-local linking state. if we can find a way to get promise-local storage we may want to bring this back though, explicit context passing is rather error-prone. luckily we are not using parent links for anything important, just to keep the multi- line activity display from filling up with stuff we're already showing Change-Id: Ie373d713080a3db811b2d5abd681f78137735e45
117 lines
2.8 KiB
C++
117 lines
2.8 KiB
C++
#pragma once
|
|
///@file
|
|
|
|
#include "lix/libstore/binary-cache-store.hh"
|
|
#include "lix/libstore/filetransfer.hh"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
namespace nix {
|
|
|
|
struct HttpBinaryCacheStoreConfig : BinaryCacheStoreConfig
|
|
{
|
|
using BinaryCacheStoreConfig::BinaryCacheStoreConfig;
|
|
|
|
const std::string name() override
|
|
{
|
|
return "HTTP Binary Cache Store";
|
|
}
|
|
|
|
std::string doc() override;
|
|
};
|
|
|
|
class HttpBinaryCacheStore : public BinaryCacheStore
|
|
{
|
|
private:
|
|
|
|
HttpBinaryCacheStoreConfig config_;
|
|
Path cacheUri;
|
|
|
|
struct State
|
|
{
|
|
bool enabled = true;
|
|
std::chrono::steady_clock::time_point disabledUntil;
|
|
};
|
|
|
|
Sync<State> _state;
|
|
|
|
public:
|
|
|
|
HttpBinaryCacheStore(
|
|
const std::string & scheme, const Path & _cacheUri, HttpBinaryCacheStoreConfig config
|
|
);
|
|
|
|
HttpBinaryCacheStoreConfig & config() override
|
|
{
|
|
return config_;
|
|
}
|
|
const HttpBinaryCacheStoreConfig & config() const override
|
|
{
|
|
return config_;
|
|
}
|
|
|
|
std::string getUri() override
|
|
{
|
|
return cacheUri;
|
|
}
|
|
|
|
kj::Promise<Result<void>> init() override;
|
|
|
|
/** Override this to configure additional curl options on the request.
|
|
* e.g. authentication method or key material.
|
|
*
|
|
* This is meant mostly for plugins who wish to perform their own
|
|
* custom initialization.
|
|
*/
|
|
virtual FileTransferOptions makeOptions(Headers && headers = {});
|
|
|
|
static std::set<std::string> uriSchemes()
|
|
{
|
|
static bool forceHttp = getEnv("_NIX_FORCE_HTTP") == "1";
|
|
auto ret = std::set<std::string>({"http", "https"});
|
|
if (forceHttp) {
|
|
ret.insert("file");
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
protected:
|
|
|
|
void maybeDisable();
|
|
void checkEnabled();
|
|
|
|
kj::Promise<Result<bool>> fileExists(const std::string & path, const Activity * context) override;
|
|
kj::Promise<Result<void>> upsertFile(
|
|
const std::string & path,
|
|
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
const std::string & mimeType,
|
|
const Activity * context
|
|
) override;
|
|
kj::Promise<Result<box_ptr<AsyncInputStream>>> getFile(const std::string & path, const Activity * context) override;
|
|
|
|
std::string makeURI(const std::string & path)
|
|
{
|
|
return path.starts_with("https://") || path.starts_with("http://")
|
|
|| path.starts_with("file://")
|
|
? path
|
|
: cacheUri + "/" + path;
|
|
}
|
|
|
|
/**
|
|
* This isn't actually necessary read only. We support "upsert" now, so we
|
|
* have a notion of authentication via HTTP POST/PUT.
|
|
*
|
|
* For now, we conservatively say we don't know.
|
|
*
|
|
* \todo try to expose our HTTP authentication status.
|
|
*/
|
|
kj::Promise<Result<std::optional<TrustedFlag>>> isTrustedClient() override
|
|
{
|
|
return {result::success(std::nullopt)};
|
|
}
|
|
};
|
|
|
|
void registerHttpBinaryCacheStore();
|
|
|
|
}
|