add back CURL requesting

This commit is contained in:
Konstantin Käfer
2014-09-24 16:09:26 +02:00
parent 0fc1f66868
commit 992d9ff051
14 changed files with 997 additions and 635 deletions

View File

@@ -33,7 +33,7 @@ HTTPRequest::HTTPRequest(ResourceType type_, const std::string &path, uv_loop_t
std::unique_ptr<CacheRequestBaton> baton((CacheRequestBaton *)ptr);
if (baton->request) {
assert(uv_thread_self() == baton->request->thread_id);
baton->request->loadedCacheEntry(std::move(response));
baton->request->handleCacheResponse(std::move(response), baton->loop);
if (baton->request) {
// If we called notify(), the request object may already have ceased to exist.
baton->request->cache_baton = nullptr;
@@ -42,7 +42,7 @@ HTTPRequest::HTTPRequest(ResourceType type_, const std::string &path, uv_loop_t
}, cache_baton);
}
void HTTPRequest::loadedCacheEntry(std::unique_ptr<Response> &&response) {
void HTTPRequest::handleCacheResponse(std::unique_ptr<Response> &&response, uv_loop_t *loop) {
if (response) {
// This entry was stored in the cache. Now determine if we need to revalidate.
const int64_t now = std::chrono::duration_cast<std::chrono::seconds>(
@@ -60,25 +60,92 @@ void HTTPRequest::loadedCacheEntry(std::unique_ptr<Response> &&response) {
}
}
// Either, the cache entry is expired, or there is no cached version. In both cases,
// we need to do a new request. The requesting code will issue a conditional request in case
// we already have a modified date on the response object.
http_baton = new HTTPRequestBaton;
http_baton->request = this;
http_baton->path = std::move(cache_baton->path);
http_baton->async = new uv_async_t;
http_baton->response = std::move(response);
http_baton->async->data = http_baton;
uv_async_init(cache_baton->loop, http_baton->async, HTTPRequestBaton::notify);
http_baton->reset();
http_baton->start();
startRequest(loop);
}
void HTTPRequest::cancel() {
void HTTPRequest::startRequest(uv_loop_t *loop) {
assert(uv_thread_self() == thread_id);
assert(!http_baton);
http_baton = std::make_shared<HTTPRequestBaton>(path);
http_baton->request = this;
http_baton->async = new uv_async_t;
http_baton->response = std::move(response);
http_baton->async->data = new util::ptr<HTTPRequestBaton>(http_baton);
uv_async_init(loop, http_baton->async, [](uv_async_t *async) {
util::ptr<HTTPRequestBaton> &http_baton = *(util::ptr<HTTPRequestBaton> *)async->data;
if (http_baton->request) {
HTTPRequest *request = http_baton->request;
request->http_baton.reset();
http_baton->request = nullptr;
request->handleHTTPResponse(http_baton->type, std::move(http_baton->response), async->loop);
}
delete (util::ptr<HTTPRequestBaton> *)async->data;
uv_close((uv_handle_t *)async, [](uv_handle_t *handle) {
uv_async_t *async = (uv_async_t *)handle;
delete async;
});
});
HTTPRequestBaton::start(http_baton);
}
void HTTPRequest::handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&res, uv_loop_t *loop) {
assert(uv_thread_self() == thread_id);
assert(!http_baton);
assert(!response);
switch (responseType) {
case HTTPResponseType::TemporaryError:
case HTTPResponseType::SingularError:
case HTTPResponseType::ConnectionError:
fprintf(stderr, "error: %s\n", res->message.c_str());
// TODO: retry
break;
case HTTPResponseType::Canceled:
response.reset();
notify();
break;
case HTTPResponseType::PermanentError:
response = std::move(res);
notify();
break;
case HTTPResponseType::Successful:
store->put(path, type, *res);
response = std::move(res);
notify();
break;
case HTTPResponseType::NotModified:
store->updateExpiration(path, res->expires);
response = std::move(res);
notify();
break;
default:
assert(!"Response wasn't set");
break;
}
}
void HTTPRequest::removeHTTPBaton() {
assert(uv_thread_self() == thread_id);
if (http_baton) {
http_baton->request = nullptr;
HTTPRequestBaton::stop(http_baton);
http_baton.reset();
}
}
void HTTPRequest::removeCacheBaton() {
assert(uv_thread_self() == thread_id);
if (cache_baton) {
// Make sure that this object doesn't accidentally get accessed when it is destructed before
// Make sre that this object doesn't accidentally get accessed when it is destructed before
// the callback returned. They are being run in the same thread, so just setting it to
// null is sufficient.
// Note: We don't manually delete the CacheRequestBaton since it'll be deleted by the
@@ -86,13 +153,12 @@ void HTTPRequest::cancel() {
cache_baton->request = nullptr;
cache_baton = nullptr;
}
}
if (http_baton) {
http_baton->request = nullptr;
http_baton->stop();
http_baton = nullptr;
}
void HTTPRequest::cancel() {
assert(uv_thread_self() == thread_id);
removeCacheBaton();
removeHTTPBaton();
notify();
}