Skip to content

Commit

Permalink
WIP brokend code
Browse files Browse the repository at this point in the history
  • Loading branch information
meditans committed Jun 17, 2020
1 parent 270005f commit 069c3d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/libstore/filetransfer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <queue>
#include <random>
#include <thread>
#include <variant>

using namespace std::string_literals;

Expand Down Expand Up @@ -124,7 +125,7 @@ struct curlFileTransfer : public FileTransfer
if (requestHeaders) curl_slist_free_all(requestHeaders);
try {
if (!done)
fail(FileTransferError(Interrupted, "download of '%s' was interrupted", request.uri));
fail(FileTransferError(Interrupted, nullptr, "download of '%s' was interrupted", request.uri));
} catch (...) {
ignoreException();
}
Expand All @@ -144,7 +145,9 @@ struct curlFileTransfer : public FileTransfer
}

LambdaSink finalSink;
std::shared_ptr<CompressionSink> decompressionSink;
// std::shared_ptr< std::variant<Sink, CompressionSink> > decompressionSink;
// std::variant< std::shared_ptr<Sink>, std::shared_ptr<CompressionSink> > decompressionSink;
std::shared_ptr<Sink> decompressionSink;

std::exception_ptr writeException;

Expand All @@ -154,8 +157,13 @@ struct curlFileTransfer : public FileTransfer
size_t realSize = size * nmemb;
result.bodySize += realSize;

if (!decompressionSink)
decompressionSink = makeDecompressionSink(encoding, finalSink);
// if (!decompressionSink)
// // decompressionSink = makeDecompressionSink(encoding, finalSink);
// decompressionSink.reset(*makeDecompressionSink(encoding, finalSink));

// Let's see some types:
// makeDecompressionSink(...) :: ref<CompressionSink>
//

(*decompressionSink)((unsigned char *) contents, realSize);

Expand Down Expand Up @@ -184,6 +192,7 @@ struct curlFileTransfer : public FileTransfer
result.bodySize = 0;
acceptRanges = false;
encoding = "";
decompressionSink = std::make_shared<TeeSink>{*decompressionSink};
} else {
auto i = line.find(':');
if (i != string::npos) {
Expand Down Expand Up @@ -411,14 +420,14 @@ struct curlFileTransfer : public FileTransfer

auto exc =
code == CURLE_ABORTED_BY_CALLBACK && _isInterrupted
? FileTransferError(Interrupted, fmt("%s of '%s' was interrupted", request.verb(), request.uri))
? FileTransferError(Interrupted, nullptr, fmt("%s of '%s' was interrupted", request.verb(), request.uri))
: httpStatus != 0
? FileTransferError(err,
? FileTransferError(err, nullptr,
fmt("unable to %s '%s': HTTP error %d",
request.verb(), request.uri, httpStatus)
+ (code == CURLE_OK ? "" : fmt(" (curl error: %s)", curl_easy_strerror(code)))
)
: FileTransferError(err,
: FileTransferError(err, nullptr,
fmt("unable to %s '%s': %s (%d)",
request.verb(), request.uri, curl_easy_strerror(code), code));

Expand Down Expand Up @@ -676,7 +685,7 @@ struct curlFileTransfer : public FileTransfer
auto s3Res = s3Helper.getObject(bucketName, key);
FileTransferResult res;
if (!s3Res.data)
throw FileTransferError(NotFound, fmt("S3 object '%s' does not exist", request.uri));
throw FileTransferError(NotFound, nullptr, fmt("S3 object '%s' does not exist", request.uri));
res.data = s3Res.data;
callback(std::move(res));
#else
Expand Down
5 changes: 3 additions & 2 deletions src/libstore/filetransfer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ class FileTransferError : public Error
{
public:
FileTransfer::Error error;
std::optional<string> response;
template<typename... Args>
FileTransferError(FileTransfer::Error error, const Args & ... args)
: Error(args...), error(error)
FileTransferError(FileTransfer::Error error, std::optional<string> response, const Args & ... args)
: Error(args...), error(error), response(response)
{ }
};

Expand Down
1 change: 1 addition & 0 deletions src/libutil/serialise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ struct TeeSink : Sink
{
Sink & orig;
ref<std::string> data;
TeeSink(TeeSink &&) = default;
TeeSink(Sink & orig)
: orig(orig), data(make_ref<std::string>()) { }
void operator () (const unsigned char * data, size_t len) {
Expand Down

0 comments on commit 069c3d2

Please sign in to comment.