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

@@ -178,6 +178,7 @@ void FileRequest::cancel() {
ptr = nullptr;
}
notify();
}
FileRequest::~FileRequest() {

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();
}

View File

@@ -1,126 +1,12 @@
#include <mbgl/storage/http_request_baton.hpp>
#include <mbgl/storage/http_request.hpp>
#include <mbgl/storage/sqlite_store.hpp>
#include <mbgl/util/uv.hpp>
#include <uv.h>
#include <cassert>
namespace mbgl {
HTTPRequestBaton::HTTPRequestBaton() : thread_id(uv_thread_self()) {
HTTPRequestBaton::HTTPRequestBaton(const std::string &path_) : thread_id(uv_thread_self()), path(path_) {
}
HTTPRequestBaton::~HTTPRequestBaton() {
assert(uv_thread_self() == thread_id);
if (timer) {
timer->data = nullptr;
}
}
void HTTPRequestBaton::reset() {
attempts++;
response.reset();
type = HTTPResponseType::Unknown;
}
void HTTPRequestBaton::retry(uint64_t delay) {
assert(uv_thread_self() == thread_id);
assert(!timer);
timer = new uv_timer_t;
timer->data = this;
uv_timer_init(async->loop, timer);
uv_timer_start(timer, [](uv_timer_t *timer) {
std::unique_ptr<uv_timer_t, uv::deleter> timer_guard(timer);
uv_timer_stop(timer);
if (timer->data) {
HTTPRequestBaton *baton = reinterpret_cast<HTTPRequestBaton *>(timer->data);
assert(uv_thread_self() == baton->thread_id);
baton->timer = nullptr;
baton->start();
}
}, delay, 0);
}
void HTTPRequestBaton::stop() {
if (timer) {
uv_timer_stop(timer);
uv::deleter()(timer);
timer = nullptr;
}
cancel();
}
void HTTPRequestBaton::notify(uv_async_t *async_unguarded) {
// Ensure deletion of pointers.
std::unique_ptr<uv_async_t, uv::deleter> async(async_unguarded);
std::unique_ptr<HTTPRequestBaton> baton((HTTPRequestBaton *)async->data);
baton->cleanup();
if (baton->request) {
assert(uv_thread_self() == baton->request->thread_id);
switch (baton->type) {
case HTTPResponseType::TemporaryError:
case HTTPResponseType::ConnectionError:
// Exponential back-off:
if (baton->attempts >= 4) {
// Fail immediately.
baton->request->response = std::move(baton->response);
baton->request->notify();
} else {
baton->reset();
baton->retry(1000 * (1 << baton->attempts));
// Make sure we're not deleting the objects, since we're now reusing them.
baton.release();
async.release();
}
break;
case HTTPResponseType::SingularError:
// Retry immediately.
if (baton->attempts >= 4) {
// Fail immediately.
baton->request->response = std::move(baton->response);
baton->request->notify();
} else {
baton->reset();
baton->start();
// Make sure we're not deleting the objects, since we're now reusing them.
baton.release();
async.release();
}
break;
case HTTPResponseType::Canceled:
baton->request->response.reset();
baton->request->notify();
break;
case HTTPResponseType::PermanentError:
case HTTPResponseType::Successful:
baton->request->store->put(baton->path, baton->request->type, *baton->response);
baton->request->response = std::move(baton->response);
baton->request->notify();
break;
case HTTPResponseType::NotModified:
baton->request->store->updateExpiration(baton->path, baton->response->expires);
baton->request->response = std::move(baton->response);
baton->request->notify();
break;
default:
// This should never happen.
assert(false);
break;
}
// Note: after calling notify(), the baton object may cease to exist.
} else {
// The HTTPRequest object has already been destructed. There is nobody we need to notify
// anymore. All we need to do is to cleanup.
}
}
}