Commit 2a745056 authored by Paweł Wegner's avatar Paweł Wegner
Browse files

Don't capture Request::provider into lambda.

parent 2391be9e
......@@ -34,20 +34,19 @@ namespace cloudstorage {
namespace {
void move(Request<EitherError<void>>::Ptr r,
void move(Request<EitherError<void>>::Ptr r, IHttp* http,
std::string metadata_url,
std::shared_ptr<std::vector<std::string>> lst, IItem::Pointer source,
IItem::Pointer destination,
std::function<void(EitherError<void>)> complete) {
if (lst->empty()) return complete(nullptr);
auto parent = lst->back();
auto p = static_cast<AmazonDrive*>(r->provider().get());
auto output = std::make_shared<std::stringstream>();
lst->pop_back();
r->sendRequest(
[=](util::Output stream) {
auto request = p->http()->create(
p->metadata_url() + "/nodes/" + destination->id() + "/children",
"POST");
auto request = http->create(
metadata_url + "/nodes/" + destination->id() + "/children", "POST");
request->setHeaderParameter("Content-Type", "application/json");
Json::Value json;
json["fromParent"] = parent;
......@@ -59,7 +58,7 @@ void move(Request<EitherError<void>>::Ptr r,
if (e.left())
complete(e.left());
else
move(r, lst, source, destination, complete);
move(r, http, metadata_url, lst, source, destination, complete);
},
output);
}
......@@ -100,8 +99,9 @@ ICloudProvider::MoveItemRequest::Pointer AmazonDrive::moveItemAsync(
MoveItemCallback callback) {
auto r = std::make_shared<Request<EitherError<void>>>(shared_from_this());
r->set([=](Request<EitherError<void>>::Ptr r) {
move(r, std::make_shared<std::vector<std::string>>(
static_cast<Item*>(source.get())->parents()),
move(r, http(), metadata_url(),
std::make_shared<std::vector<std::string>>(
static_cast<Item*>(source.get())->parents()),
source, destination, callback);
});
return r->run();
......
......@@ -33,8 +33,8 @@ namespace cloudstorage {
namespace {
void rename(Request<EitherError<void>>::Ptr r, std::string dest_id,
std::string source_id,
void rename(Request<EitherError<void>>::Ptr r, IHttp* http, std::string region,
std::string dest_id, std::string source_id,
std::function<void(EitherError<void>)> complete);
std::string escapePath(const std::string& str) {
......@@ -66,35 +66,33 @@ void remove(Request<EitherError<void>>::Ptr r,
}));
}
void rename(Request<EitherError<void>>::Ptr r,
void rename(Request<EitherError<void>>::Ptr r, IHttp* http, std::string region,
std::shared_ptr<std::vector<IItem::Pointer>> lst,
std::string dest_id, std::string source_id,
std::function<void(EitherError<void>)> complete) {
if (lst->empty()) return complete(nullptr);
auto item = lst->back();
lst->pop_back();
rename(r, lst, dest_id, source_id, [=](EitherError<void> e) {
rename(r, http, region, lst, dest_id, source_id, [=](EitherError<void> e) {
if (e.left())
complete(e.left());
else
rename(r, dest_id + "/" + item->filename(), item->id(), complete);
rename(r, http, region, dest_id + "/" + item->filename(), item->id(),
complete);
});
}
void rename(Request<EitherError<void>>::Ptr r, std::string dest_id,
std::string source_id,
void rename(Request<EitherError<void>>::Ptr r, IHttp* http, std::string region,
std::string dest_id, std::string source_id,
std::function<void(EitherError<void>)> complete) {
auto p = r->provider();
auto region = static_cast<AmazonS3*>(p.get())->region();
auto finalize = [=]() {
auto output = std::make_shared<std::stringstream>();
r->sendRequest(
[=](util::Output) {
auto data = AmazonS3::split(source_id);
return p->http()->create("https://" + data.first + ".s3." + region +
".amazonaws.com/" +
escapePath(data.second),
"DELETE");
return http->create("https://" + data.first + ".s3." + region +
".amazonaws.com/" + escapePath(data.second),
"DELETE");
},
[=](EitherError<util::Output> e) {
if (e.left()) {
......@@ -112,10 +110,10 @@ void rename(Request<EitherError<void>>::Ptr r, std::string dest_id,
r->sendRequest(
[=](util::Output) {
auto dest_data = AmazonS3::split(dest_id);
auto request = p->http()->create(
"https://" + dest_data.first + ".s3." + region +
".amazonaws.com/" + escapePath(dest_data.second),
"PUT");
auto request =
http->create("https://" + dest_data.first + ".s3." + region +
".amazonaws.com/" + escapePath(dest_data.second),
"PUT");
auto source_data = AmazonS3::split(source_id);
request->setHeaderParameter(
"x-amz-copy-source",
......@@ -144,7 +142,7 @@ void rename(Request<EitherError<void>>::Ptr r, std::string dest_id,
complete(e.left());
return r->done(e.left());
}
rename(r, e.right(), dest_id, source_id,
rename(r, http, region, e.right(), dest_id, source_id,
[=](EitherError<void> e) {
if (e.left()) {
complete(e.left());
......@@ -214,7 +212,8 @@ ICloudProvider::MoveItemRequest::Pointer AmazonS3::moveItemAsync(
MoveItemCallback callback) {
auto r = std::make_shared<Request<EitherError<void>>>(shared_from_this());
r->set([=](Request<EitherError<void>>::Ptr r) {
rename(r, destination->id() + source->filename(), source->id(), callback);
rename(r, http(), region(), destination->id() + source->filename(),
source->id(), callback);
});
return r->run();
}
......@@ -229,7 +228,7 @@ ICloudProvider::RenameItemRequest::Pointer AmazonS3::renameItemAsync(
path = split(item->id()).first + Auth::SEPARATOR;
else
path = split(item->id()).first + Auth::SEPARATOR + getPath(path) + "/";
rename(r, path + name, item->id(), callback);
rename(r, http(), region(), path + name, item->id(), callback);
});
return r->run();
}
......
......@@ -242,15 +242,9 @@ struct Buffer {
class HttpData : public IHttpServer::IResponse::ICallback {
public:
HttpData(Buffer::Pointer d) : buffer_(d) {}
HttpData(Buffer::Pointer d, MegaNz* p) : buffer_(d), mega_(p) {}
~HttpData() {
auto p = request_->provider();
if (p) {
auto mega = static_cast<MegaNz*>(request_->provider().get());
mega->removeStreamRequest(request_);
}
}
~HttpData() { mega_->removeStreamRequest(request_); }
int putData(char* buf, size_t max) override {
std::unique_lock<std::mutex> lock(buffer_->mutex_);
......@@ -268,6 +262,7 @@ class HttpData : public IHttpServer::IResponse::ICallback {
}
Buffer::Pointer buffer_;
MegaNz* mega_;
std::shared_ptr<Request<EitherError<void>>> request_;
};
......@@ -333,7 +328,7 @@ IHttpServer::IResponse::Pointer MegaNz::HttpServerCallback::receivedConnection(
code = IHttpRequest::Partial;
}
auto buffer = std::make_shared<Buffer>(connection);
auto data = util::make_unique<HttpData>(buffer);
auto data = util::make_unique<HttpData>(buffer, provider_);
auto request = std::make_shared<Request<EitherError<void>>>(
std::weak_ptr<CloudProvider>(provider_->shared_from_this()));
data->request_ = request;
......
......@@ -114,18 +114,18 @@ void AuthorizeRequest::set_server(std::shared_ptr<IHttpServer> p) {
}
void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
auto p = provider();
auto input = std::make_shared<std::stringstream>(),
output = std::make_shared<std::stringstream>(),
error_stream = std::make_shared<std::stringstream>();
auto r = p->auth()->refreshTokenRequest(*input);
auto r = provider()->auth()->refreshTokenRequest(*input);
auto auth = provider()->auth();
auto auth_callback = provider()->auth_callback();
send(r.get(),
[=](int code, util::Output, util::Output) {
if (IHttpRequest::isSuccess(code)) {
try {
auto lock = p->auth_lock();
p->auth()->set_access_token(
p->auth()->refreshTokenResponse(*output));
auto lock = provider()->auth_lock();
auth->set_access_token(auth->refreshTokenResponse(*output));
lock.unlock();
return complete(nullptr);
} catch (std::exception e) {
......@@ -135,24 +135,23 @@ void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
} else if (!IHttpRequest::isClientError(code) && r) {
return complete(Error{code, error_stream->str()});
}
if (p->auth_callback()->userConsentRequired(*provider()) ==
if (auth_callback->userConsentRequired(*provider()) ==
ICloudProvider::IAuthCallback::Status::WaitForAuthorizationCode) {
auto code = [=](EitherError<std::string> authorization_code) {
if (authorization_code.left())
return complete(authorization_code.left());
p->auth()->set_authorization_code(*authorization_code.right());
auth->set_authorization_code(*authorization_code.right());
auto input = std::make_shared<std::stringstream>(),
output = std::make_shared<std::stringstream>(),
error_stream = std::make_shared<std::stringstream>();
auto r = p->auth()->exchangeAuthorizationCodeRequest(*input);
auto r = auth->exchangeAuthorizationCodeRequest(*input);
send(r.get(),
[=](int code, util::Output, util::Output) {
if (IHttpRequest::isSuccess(code)) {
try {
auto lock = p->auth_lock();
p->auth()->set_access_token(
p->auth()->exchangeAuthorizationCodeResponse(
*output));
auto lock = provider()->auth_lock();
auth->set_access_token(
auth->exchangeAuthorizationCodeResponse(*output));
lock.unlock();
complete(nullptr);
} catch (std::exception e) {
......@@ -164,7 +163,7 @@ void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
},
input, output, error_stream);
};
set_server(p->auth()->requestAuthorizationCode(code));
set_server(auth->requestAuthorizationCode(code));
} else {
complete(Error{code, error_stream->str()});
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment